-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRMS_processing.py
More file actions
330 lines (276 loc) · 16.4 KB
/
CRMS_processing.py
File metadata and controls
330 lines (276 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# -*- coding: cp1252 -*-
print ('\n\nsetting things up...')
import os
import numpy as np
import os
import sys
import datetime as dt
import CRMS_stats
from builtins import Exception
################################################################
#### SET THE SETTINGS ####
################################################################
yr_start = 2006
yr_end = 2024
topdir = 'E:/CRMS_%04d-%04d' % (yr_start,yr_end)
masterCRMS = '%s/Full_Continuous_Hydrographic_20250117.csv' % topdir
masterCRMSlist = '%s/CRMS_sites.csv' % topdir
geoidfile = '%s/Hydro Shift from 99-12B.csv' % topdir # '%s/CRMS_GEOID99_TO_GEOID12A.csv' % topdir
survey_subsidence_file = '%s/CRMS_survey_dates_subsidence_rates.csv' % topdir
sub_conv = 0.001/0.3048 # subsidence rates are provided in mm/yr - hourly output files with corrected subsidence need to be in feet
split_files = 'no' # split_files is a flag to separate the master CRMS bulk download file into individual files for each site; if set to 'no' the split files must already exist;
build_files = 'yes' # build_files is a flag to build new daily and hourly data from the raw split files; if set to 'no' the split daily and hourly clean files must already exist and the stats will be generated from those files
sites = np.genfromtxt(masterCRMSlist,dtype='str')
################################################################################################
#### SETUP FOLDER DIRECTORIES FOR PROCESSED CRMS DATA ####
################################################################################################
if split_files == 'yes':
folds = ['raw','clean_hourly','clean_daily','stats']
for fol in folds:
if os.path.exists(r'%s\%s' % (topdir,fol)) == True:
print('\n\n\n\noutput directory \%s already exists in %s !!!!' % (fol,topdir))
print(' \n\ndo you want to....\n(a) continue with that folder and re-write old files \n(b) rename folder here \n(c) quit program and handle yourself? ')
warn = input('pick one: < a b c >')
if warn == 'a':
print('\nyou chose to continue. \n(a) you sure? \n(b) or would you prefer to rename the folder \n(c) or just quit, already?')
warn = str(input('pick one: < a b c >'))
if warn == 'c':
print('\nsee ya later.')
sys.exit()
elif warn == 'b':
apptext = str(input('what word would you like to append to the folder name? please reply within quotes (e.g. "old").'))
old = r'%s\%s' % (topdir,fol)
new = r'%s\%s_%s' % (topdir,fol,apptext)
print('\n renaming %s to %s' % (old, new))
os.rename(old,new)
os.mkdir(r'%s\%s' % (topdir,fol))
elif warn == 'a':
print('\nOK this will overwrite any old data...continuing on.')
else:
os.mkdir(r'%s\%s' % (topdir,fol))
################################################################################################
#### SPLIT BULK DOWNLOAD TABLE INTO INDIVIDUAL FILES ####
################################################################################################
# The below script parses the bulk data download into individual files - it requires that the file is structured such that the data for each CRMS site is lumped (vertically together)
if split_files == 'yes':
print('splitting master CRMS file by site ID')
# data_by_site = {}
# for s in sites:
# data_by_site[s] = []
with open(masterCRMS,mode='rt') as inf:
nline = 0
old_site = 'old'
current_site = 'current'
data_by_site = []
for line in inf:
if nline == 0:
hdr = line
else:
current_site = line.split(',')[0]
if current_site != old_site:
# if on to a new site, must first print out old site data to new file
print(' - writing individual CRMS file for %s' % old_site)
rawpath = r'%s\raw\%s_raw.csv' % (topdir,old_site)
with open(rawpath, mode='w') as splitfile:
splitfile.write(hdr)
for val in data_by_site:
splitfile.write(val)
# once old data is written, re-initialize data for new site
print('processing new site: %s' % current_site)
data_by_site = []
else: # still on current site
try:
data_by_site.append(line)
except Exception as e:
print(e)
old_site = current_site
nline += 1
# print('writing individual CRMS files')
#
# for site in sites:
# print(' - site %s' % site)
# rawpath = r'%s\raw\%s_raw.csv' % (topdir,site)
# with open(rawpath, mode='w') as splitfile:
# splitfile.write(hdr)
# for val in data_by_site[site]:
# splitfile.write(val)
print('done splitting CRMS data by site\n')
else:
print('using pre-split CRMS files saved in \raw folder')
################################################################################################
#### READ IN SUBSIDENCE RATES AND SURVEY DATES FOR EACH CRMS SITE ####
################################################################################################
if build_files == 'yes':
print('reading in subsidence rates and survey dates')
survey_sub_rates = np.genfromtxt(survey_subsidence_file,dtype='str',delimiter=',',skip_header=1)
survey_dates = {}
subsidence_rates = {}
for r in survey_sub_rates:
site = r[0]
dstr = r[1].split('-')
subrate = r[2]
survdate = dt.date(int(dstr[0]),int(dstr[1]),int(dstr[2]))
survey_dates[site] = survdate
subsidence_rates[site] = float(subrate)
################################################################################################
#### READ IN GEOID CONVERSION FACTORS FOR EACH CRMS SITE ####
################################################################################################
if build_files == 'yes':
print('reading in geoid conversions')
gc = {}
with open(geoidfile,mode='rt') as gf:
for line in gf:
if line[0:4] == 'CRMS':
gc[line.split(',')[0]] = float(line.split(',')[1])
########################################################################################################
#### STEP THROUGH CRMS SITES AND PROCESS RAW DATA THEN SAVE INTO CLEAN HOURLY AND DAILY FILES ####
########################################################################################################
ns = 0
for site in sites:
ns += 1
print('Processing site %03d/%03d - %s' %(ns,len(sites),site) )
crmssite = site.split('-')[0]
rawpath = r'%s\raw\%s_raw.csv' % (topdir,site)
hrpath = r'%s\clean_hourly\%s_hourly_English.csv' % (topdir,site)
daypath = r'%s\clean_daily\%s_daily_English.csv' % (topdir,site)
########################################################################
#### PROCESS HOURLY DATA AND SAVE 'CLEAN' HOURLY OUTPUT FILE ####
########################################################################
if build_files == 'yes':
print(' - building new clean hourly output file')
try:
sub = subsidence_rates[crmssite]
survey_date = survey_dates[crmssite]
note = ' - %s site was surveyed on %04d-%02d-%02d\n - using subsidence rate of %0.2f mm/yr.' % (site,survey_date.year,survey_date.month,survey_date.day,sub)
sbf = 'c'
except:
sub = 0.0
survey_date = dt.date(yr_start,1,1)
note = ' - %s site does not have survey data\n - no subsidence correction applied.' % site
sbf = ''
print(note)
with open(rawpath,mode='rt') as rawdata:
with open(hrpath,mode='wt') as cleanhour:
clean_hourly_header = r'stationID,Date_mm/dd/yyyy,time_hh:mm:ss,timezone,salinity_ppt,salinity_flag(r=raw a=adjusted na=nodata),stage_ft_NAVD88_12B,stage_flag(r=raw data used/no adjustments made; a=adjusted; s=datum shifted to geoid12b from 99; c= subsidence corrected based; nd=used Geoid99 but no datum conversion found for site; na=nodata)'
cleanhour.write('%s\n' % clean_hourly_header)
nr = 0
for allrow in rawdata:
if nr > 0:
row = allrow.split(',')
sid,d,t,tz,dat = row[0],row[1],row[2],row[3],row[19]
##########################################################################################
#### DETERMINE SUBSIDENCE OFFSET BASED ON SUBSIDENCE RATE AND TIME SINCE SURVEY ####
##########################################################################################
# subsidence offset sign convention:
# this sub_offset value should be subtracted from raw data to reduce water surface
# elevation if data is from after survey (e.g., gage has subsided and reading is
# higher than reality due to sunken gage) and to increase water surface elevation
# if data is from before survey. The survey_date will be the time where the raw water
# surface elevation is assumed to be accurately tied into the survey/geoid conversion
# and sub_offset=0 when dtdate=survey_date
dtdate = dt.date(int(d.split('/')[2]),int(d.split('/')[0]),int(d.split('/')[1])) # this format requires the DATE column in CRMS raw data files to be of the format M/D/YYYY
sub_offset = sub_conv*sub*(dtdate - survey_date).days/365.25
##############################################################
#### DETERMINE GEOID DATUM CONVERSION FACTOR TO USE ####
##############################################################
if dat == 'GEOID99':
try:
datc = gc[sid]
dcf = 's'
except:
try:
datc = gc[sid.split('-')[0]]
dcf = 's'
except:
datc = 0.0
dcf = 'nd'
else:
datc = 0.0
dcf = ''
#########################################################
#### PROCESS HOURLY DATA AND APPLY CONVERSIONS ####
#########################################################
if row[10] != '': # try for adjusted salinity
sal = row[10]
sal_f = 'a'
#elif row[9] != '': # if no adjusted salinity - use raw - COMMENTED OUT - DO NOT WANT TO USE RAW DATA AT ALL, ALWAYS USE ADJUSTED (per LAS)
# sal = row[9]
# sal_f = 'r'
else:
sal = 'na'
sal_f = 'na'
try:
if float(sal) < 0.0: # check for negative salinity values and set to na
sal = 'na'
except:
sal = 'na'
if row[16] != '':
stg_c = float(row[16]) + datc - sub_offset # try for adjusted stage and add datum and subsidence conversion factors
stg_f = 'a %s %s' % (dcf,sbf)
elif row[15] != '':
stg_c = float(row[15]) + datc - sub_offset # if no adjusted stage - use raw stage and add datum and subsidence conversion factors
stg_f = 'r %s %s' % (dcf,sbf)
else:
stg_c = 'na'
stg_f ='na'
writerow = '%s,%s,%s,%s,%s,%s,%s,%s\n' % (sid,d,t,tz,sal,sal_f,stg_c,stg_f)
cleanhour.write(writerow)
nr += 1
##########################################################################
#### PREPARE DAILY OUTPUT FILE FROM 'CLEAN' HOURLY OUTPUT FILE ####
##########################################################################
print(' - building new daily summary output file')
with open(hrpath,mode='rt') as cleanhour:
sal = {}
salorder = {}
stg = {}
stgorder = {}
nr = 0
for allrow in cleanhour:
if nr > 0:
row = allrow.split(',')
sid,d,t,tz,salh,stgh = row[0],row[1],row[2],row[3],row[4],row[6]
# add date as a key to stage and salinity dictionaries
if d not in sal.keys():
ndaysal = len(sal.keys())
sal[d] = []
salorder[ndaysal+1] = d
if d not in stg.keys():
ndaystg = len(stg.keys())
stg[d] = []
stgorder[ndaystg+1] = d
# append salinity and stage to daily value arrays
if salh != 'na':
sal[d].append(float(salh))
if stgh != 'na':
stg[d].append(float(stgh))
nr += 1
with open(daypath,mode='wt') as sumdaily:
daily_header = r'stationID,Date_mm/dd/yyyy,mean_salinity_ppt,median_salinity_ppt,min_salinity_ppt,max_salinity_ppt,stdev_salinity_ppt,ncount_salinity,mean_stage_ftNAVD88,median_stage_ftNAVD88,min_stage_ftNAVD88,max_stage_ftNAVD88,stdev_stage_ftNAVD88,ncount_stage_ftNAVD88'
sumdaily.write('%s\n' % daily_header)
for nday in stgorder.keys():
d = stgorder[nday]
if len(sal[d]) > 0:
salstring = '%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%d' % (np.mean(sal[d]),np.median(sal[d]),np.min(sal[d]),np.max(sal[d]),np.std(sal[d]),len(sal[d]))
else:
salstring = 'na,na,na,na,na,0'
if len(stg[d]) > 0:
stgstring = '%0.2f,%0.2f,%0.2f,%0.2f,%0.2f,%d' % (np.mean(stg[d]),np.median(stg[d]),np.min(stg[d]),np.max(stg[d]),np.std(stg[d]),len(stg[d]))
else:
stgstring = 'na,na,na,na,na,0'
sumdaily.write('%s,%s,%s,%s\n' % (site,d,salstring,stgstring))
else:
print('using existing clean hourly and daily files to perform stats calculations')
##################################################
#### CALCULATE STATISTICS FOR CRMS SITE ####
##################################################
if ns == 1:
write_hdr = 'True'
else:
write_hdr = 'False'
print(' - calculating stage summary statistics')
CRMS_stats.CRMS_hydro_stats(site,yr_start,yr_end,'stage_ft_NAVD88-g12b',topdir,write_hdr)
print(' - calculating salinity summary statistics')
CRMS_stats.CRMS_hydro_stats(site,yr_start,yr_end,'salinity_ppt',topdir,write_hdr)
print(' - calculating moving window salinity summary statistics')
CRMS_stats.CRMS_moving_window(site,yr_start,yr_end,'salinity_ppt',topdir,write_hdr)