-
Notifications
You must be signed in to change notification settings - Fork 1
/
ii_to_PDT_common_csv.py
456 lines (350 loc) · 16.2 KB
/
ii_to_PDT_common_csv.py
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# Export all oxygen climatology data to common csv format: Profile data table
# Sources: IOS, NODC, MEDS
from xarray import open_dataset
import pandas as pd
import numpy as np
import glob
from os.path import basename
def ios_to_pdt(nclist, var):
if var == 'Oxy':
varcode = 'DOXMZZ01'
elif var == 'Temp':
varcode = 'TEMPS901'
elif var == 'Sal':
varcode = 'PSALST01'
df_cols = ["Source_data_file_name", "Institute", "Cruise_number",
"Instrument_type", "Date_string", "Latitude",
"Longitude", "Quality_control_flag"]
ios_df = pd.DataFrame(columns=df_cols)
# Open IOS files
for i, f in enumerate(nclist):
print(i, f)
ios_data = open_dataset(f)
# Check if var is available in each file
flag = 0
try:
var_data = ios_data[varcode].data
except KeyError:
print(basename(f))
print('Warning: requested variable', varcode, 'not available in dataset')
flag += 1
# If variable not present, skip to next iteration
if flag == 1:
continue
# Get unique profile indices to allow filtering through "row" dimension
indices = np.unique(ios_data.profile.data, return_index=True)[1]
ios_fname_array = np.repeat(basename(f), len(indices))
ios_institute_array = np.repeat(ios_data.institution, len(indices))
if 'CTD' in f:
inst = 'CTD'
elif 'BOT' in f:
inst = 'BOT'
print(inst)
ios_instrument_type_array = np.repeat(inst, len(indices))
# Time strings: yyyymmddhhmmsszzz; slow to run
ios_time_strings = pd.to_datetime(
ios_data.time.data[indices]).strftime('%Y%m%d%H%M%S')
# QC flags: good data by default, according to Germaine
ios_flags = np.ones(len(indices))
# Take transpose of arrays?
ios_df_add = pd.DataFrame(
data=np.array([ios_fname_array,
ios_institute_array,
ios_data.mission_id.data[indices],
ios_instrument_type_array,
ios_time_strings,
ios_data.latitude.data[indices],
ios_data.longitude.data[indices],
ios_flags]).transpose(), columns=df_cols)
ios_df = pd.concat([ios_df, ios_df_add])
# Close dataset
ios_data.close()
return ios_df
def ios_wp_to_pdt(nclist, var):
if var == 'Oxy':
varcode = 'DOXMZZ01'
elif var == 'Temp':
varcode = 'TEMPS901'
elif var == 'Sal':
varcode = 'PSALST01'
# df_cols = ["Source_data_file_name", "Institute", "Cruise_number",
# "Instrument_type", "Date_string", "Latitude",
# "Longitude", "Quality_control_flag"]
# Initialize dataframe for IOS data
# ios_wp_df = pd.DataFrame(columns=df_cols)
dict_list = []
for i, f in enumerate(nclist):
print(i, f)
# Open file
ncdata = open_dataset(f)
# Check if var is available in each file
flag = 0
try:
var_data = ncdata[varcode].data
except KeyError:
print(basename(f))
print('Warning: requested variable', varcode, 'not available in dataset')
flag += 1
# If variable not present, skip to next iteration
if flag == 1:
continue
if 'bot' in f:
instrument_type = 'BOT'
elif 'ctd' in f:
instrument_type = 'CTD'
# Initialize dataframe
# df_add = pd.DataFrame(
# data=np.array([]).transpose(),
# columns=df_cols)
# ios_wp_df = pd.concat([ios_wp_df, df_add])
dict_list.append({'Source_data_file_name': basename(f),
'Institute': ncdata.institution,
'Cruise_number': ncdata.mission_id.data,
'Instrument_type': instrument_type,
'Date_string': pd.to_datetime(ncdata.time.data).strftime(
'%Y%m%d%H%M%S'),
'Latitude': ncdata.latitude.data,
'Longitude': ncdata.longitude.data,
'Quality_control_flag': 1})
df_out = pd.DataFrame.from_dict(dict_list)
return df_out
def nodc_to_pdt(nodc_files, sourcetype, var, output_folder):
df_cols = ["Source_data_file_name", "Institute", "Cruise_number",
"Instrument_type", "Date_string", "Latitude",
"Longitude", "Quality_control_flag"]
nodc_df = pd.DataFrame(columns=df_cols)
for f in nodc_files:
# Read in netCDF file
nodc_nocad_data = open_dataset(f)
# Casts is the dim counting the number of profiles
nodc_nocad_fname_array = np.repeat(
basename(f), len(nodc_nocad_data.casts.data))
# Make array of institute name
nodc_nocad_institute_array = np.repeat(
nodc_nocad_data.institution, len(nodc_nocad_data.casts.data))
# Get instrument type from file name
if 'CTD' in f:
inst = 'CTD'
elif 'OSD' in f:
inst = 'BOT'
elif 'PFL' in f: # Profiling float (Argo) -- only temp (and sal?) data
inst = 'PFL'
elif 'DRB' in f:
inst = 'DRB' # Drifting buoy
elif 'GLD' in f:
inst = 'GLD' # Glider
nodc_nocad_instrument_array = np.repeat(
inst, len(nodc_nocad_data.casts.data))
# Convert time data to time string type
nodc_nocad_timestring = pd.to_datetime(
nodc_nocad_data.time.data).strftime('%Y%m%d%H%M%S%z')
if var == 'Oxy':
var_flag = 'Oxygen_WODprofileflag'
elif var == 'Temp':
var_flag = 'Temperature_WODprofileflag'
elif var == 'Sal':
var_flag = 'Salinity_WODprofileflag'
nodc_df_add = pd.DataFrame(
data=np.array([nodc_nocad_fname_array,
nodc_nocad_institute_array,
nodc_nocad_data.WOD_cruise_identifier.data.astype(str),
nodc_nocad_instrument_array,
nodc_nocad_timestring,
nodc_nocad_data.lat.data,
nodc_nocad_data.lon.data,
nodc_nocad_data[var_flag].data]).transpose(),
columns=df_cols)
# Append the new dataframe to the existing dataframe
nodc_df = pd.concat([nodc_df, nodc_df_add],
ignore_index=True)
print(nodc_df.columns)
print(nodc_df)
print(min(nodc_df['Date_string']), max(nodc_df['Date_string']))
# Export to csv file
# output_folder = '/home/hourstonh/Documents/climatology/data_extracts/'
nodc_name = 'NODC_{}_Profiles_{}_1991_2020.csv'.format(sourcetype, var)
nodc_df.to_csv(output_folder + nodc_name)
return nodc_name
def meds_to_pdt(csvfiles, var):
# MEDS is the only one that cals Salinity PSAL, so need to update
if var == 'Sal':
var = 'PSAL'
df_cols = ["Source_data_file_name", "Institute", "Cruise_number",
"Instrument_type", "Date_string", "Latitude",
"Longitude", "Quality_control_flag"]
# MEDS data: initialize empty dataframe
meds_df = pd.DataFrame(columns=df_cols)
# Iterate through csv files
for i in range(len(csvfiles)):
# Skip if var not in file
if var.upper() not in basename(csvfiles[i]):
continue
meds_data = pd.read_csv(csvfiles[i])
# print(meds_data.head())
# Get number of unique profiles
unique = np.unique(meds_data.loc[:, 'RowNum'], return_index=True)[1]
# Oxy data spans 1991-01-22 05:13:00 to 1995-03-09 23:35:00
meds_fname_array = np.repeat(basename(csvfiles[i]), len(unique))
# Get instrument from file name
if 'CD' in basename(csvfiles[i]):
inst = 'CTD'
elif 'BO' in basename(csvfiles[i]):
inst = 'BOT'
elif 'XB' in basename(csvfiles[i]):
inst = 'XBT'
meds_instrument_array = np.repeat(inst, len(unique))
# Time string data
meds_data['Hour'] = meds_data.Time.astype(str).apply(lambda x: ('000' + x)[-4:][:-2])
meds_data['Minute'] = meds_data.Time.astype(str).apply(lambda x: ('000' + x)[-4:][-2:])
# meds_data['Hour'] = meds_data.Time.astype(str).apply(lambda x: ('000' + x)[:-2])
# meds_data['Minute'] = meds_data.Time.astype(str).apply(lambda x: ('000' + x)[-2:])
print(np.where(pd.isnull(meds_data.Hour)))
print(np.where(pd.isnull(meds_data.Minute)))
meds_data['Timestring'] = pd.to_datetime(
meds_data[['Year', 'Month', 'Day', 'Hour', 'Minute']]).dt.strftime(
'%Y%m%d%H%M%S')
print(np.where(pd.isnull(meds_data.Timestring)))
# meds_data['Time_pd'] = pd.to_datetime(
# meds_data[['Year', 'Month', 'Day', 'Hour', 'Minute']])
#
# print(min(meds_data['Time_pd']), max(meds_data['Time_pd']))
# # DataFrame columns
# df_cols = ["Source_data_file_name", "Institute", "Cruise_number",
# "Instrument_type", "Date_string", "Latitude",
# "Longitude", "Quality_control_flag"]
# Need to convert MEDS longitude from positive towards West to positive
# towards East
meds_df_add = pd.DataFrame(
data=np.array([meds_fname_array,
meds_data.loc[unique, 'SourceID'],
meds_data.loc[unique, 'CruiseID'],
meds_instrument_array,
meds_data.loc[unique, 'Timestring'],
meds_data.loc[unique, 'Lat'],
-meds_data.loc[unique, 'Lon'],
meds_data.loc[unique, 'PP_flag']]).transpose(),
columns=df_cols
)
meds_df = pd.concat([meds_df, meds_df_add])
# print(np.where(pd.isna(meds_df)))
return meds_df
def gather_raw_data(var, output_folder):
# Find all oxygen data
# var = 'Oxy', 'Temp', or 'Sal'
# IOS CIOOS Pacific files
# ios_path = '/home/hourstonh/Documents/climatology/data/IOS_CIOOS/'
ios_path = 'C:\\Users\\HourstonH\\Documents\\NEP_climatology\\data\\' \
'source_format\\IOS_CIOOS\\'
ios_files = glob.glob(ios_path + '*{}*.nc'.format(var), recursive=False)
ios_files.sort()
print('Number of IOS files', len(ios_files))
# IOS Water Properties files
ios_wp_path = 'C:\\Users\\HourstonH\\Documents\\NEP_climatology\\data\\' \
'source_format\\SHuntington\\'
# Get bot files
ios_wp_files = glob.glob(ios_wp_path + '*.bot.nc', recursive=False)
# Get ctd files
ios_wp_files += glob.glob(ios_wp_path + 'WP_unique_CTD_forHana\\*.ctd.nc',
recursive=False)
print('Number of IOS WP files', len(ios_wp_files))
# NODC WODSelect files, non-Canadian
if var == 'Oxy':
nodc_nocad_path = 'C:\\Users\HourstonH\\Documents\\NEP_climatology\\' \
'data\\source_format\\WOD_extracts\\' \
'Oxy_WOD_May2021_extracts\\'
else:
nodc_nocad_path = 'C:\\Users\HourstonH\\Documents\\NEP_climatology\\data\\' \
'source_format\\WOD_extracts\\WOD_July_extracts\\'
# nodc_nocad_path = '/home/hourstonh/Documents/climatology/data/WOD_extracts/' \
# 'Oxy_WOD_May2021_extracts/'
nodc_nocad_files = glob.glob(nodc_nocad_path + '{}*.nc'.format(var),
recursive=False)
nodc_nocad_files.sort()
print('Number of NODC nocad files', len(nodc_nocad_files))
# NODC WODSelect files, Canadian non-IOS
nodc_cad_path = 'C:\\Users\HourstonH\\Documents\\NEP_climatology\\data\\' \
'source_format\\WOD_extracts\\WOD_July_CDN_nonIOS_extracts\\'
# nodc_cad_path = '/home/hourstonh/Documents/climatology/data/WOD_extracts/' \
# 'WOD_July_CDN_nonIOS_extracts/'
nodc_cad_files = glob.glob(nodc_cad_path + '{}*.nc'.format(var),
recursive=False)
nodc_cad_files.sort()
print('Number of NODC cad files', len(nodc_cad_files))
# MEDS files (Canadian waters)
meds_path = 'C:\\Users\HourstonH\\Documents\\NEP_climatology\\data\\' \
'source_format\\meds_data_extracts\\'
meds_files = glob.glob(meds_path + '*\\*{}*source.csv'.format(var.upper()),
recursive=False)
meds_files.sort()
print('number of meds files', len(meds_files))
# Create PDT files
# Start with IOS CIOOS files
ios_out_df = ios_to_pdt(ios_files, var=var)
ios_df_name = output_folder + 'IOS_Profiles_{}_1991_2020_pdt.csv'.format(var)
ios_out_df.to_csv(ios_df_name, index=False)
ios_wp_out_df = ios_wp_to_pdt(ios_wp_files, var=var)
ios_wp_name = output_folder + 'IOS_WP_Profiles_{}_1991_2020_pdt.csv'.format(var)
ios_wp_out_df.to_csv(ios_wp_name, index=False)
nodc_nocad_name = nodc_to_pdt(nodc_nocad_files, sourcetype='noCAD', var=var,
output_folder=output_folder)
nodc_cad_name = nodc_to_pdt(nodc_cad_files, sourcetype='CAD', var=var,
output_folder=output_folder)
meds_out_df = meds_to_pdt(meds_files, var=var)
meds_csv_name = output_folder + 'MEDS_Profiles_{}_1991_1995_pdt.csv'.format(var)
meds_out_df.to_csv(meds_csv_name)
return
outdir = 'C:\\Users\\HourstonH\\Documents\\NEP_climatology\\data\\' \
'profile_data_tables\\'
# Run the program
gather_raw_data('Temp', outdir)
gather_raw_data('Sal', outdir)
# -----------------------------------------------------------------------------
variable_name = 'Sal'
# Redo the program on NODC cdn data only since the glider data got messed up :)
# NODC WODSelect files, Canadian non-IOS
nodc_cad_path = 'C:\\Users\HourstonH\\Documents\\NEP_climatology\\data\\' \
'raw\\WOD_July_CDN_nonIOS_extracts\\'
# nodc_cad_path = '/home/hourstonh/Documents/climatology/data/WOD_extracts/' \
# 'WOD_July_CDN_nonIOS_extracts/'
nodc_cad_files = glob.glob(nodc_cad_path + '{}*.nc'.format(variable_name),
recursive=False)
nodc_cad_files.sort()
print('Number of NODC cad files', len(nodc_cad_files))
nodc_to_pdt(nodc_cad_files, 'CAD', variable_name, outdir)
# -------------------------------------------------------------------------------
# Second version of oxygen data that includes Argo oxygen sensor data
# argo_dir = 'C:\\Users\\HourstonH\\Documents\\NEP_climatology\\data\\' \
# 'profile_data_tables\\Argo\\'
indir = 'C:\\Users\HourstonH\\Documents\\NEP_climatology\\' \
'data\\source_format\\WOD_extracts\\' \
'Oxy_WOD_May2021_extracts\\'
argo_files = glob.glob(indir + 'Oxy*PFL.nc')
# nodc_to_pdt(argo_files, sourcetype="noCAD_PFL", var='Oxy',
# output_folder=argo_dir)
# -------------------------------------------------------------------------------------
# COMBINE ALL PROFILE DATA TABLES
def combine_all_pdt(var_name):
# extract_folder = '/home/hourstonh/Documents/climatology/data_extracts/'
extract_folder = 'C:\\Users\\HourstonH\\Documents\\NEP_climatology\\' \
'data\\profile_data_tables\\'
extracts = glob.glob(extract_folder + '*{}*.csv'.format(var_name), recursive=False)
print(len(extracts))
extracts.sort()
colnames = ["Source_data_file_name", "Institute", "Cruise_number",
"Instrument_type", "Date_string", "Latitude",
"Longitude", "Quality_control_flag"]
df_all = pd.DataFrame(columns=colnames)
for fi in extracts:
print(basename(fi))
df_add = pd.read_csv(fi)
df_all = pd.concat([df_all, df_add], ignore_index=True)
# Remove unwanted column
df_all = df_all.drop(columns=['Unnamed: 0'])
df_all['Quality_control_flag'] = df_all['Quality_control_flag'].astype(int)
# Write to new csv file for ease
df_all_name = 'ALL_Profiles_{}_1991_2020.csv'.format(var_name)
df_all.to_csv(extract_folder + df_all_name)
return
# Combine all pdt for each variable
variable_name = 'Temp' # 'Temp' # 'Sal'
combine_all_pdt(variable_name)