forked from barbarabrooks/NCAS-Data-Project-Training-Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTData_common.py
More file actions
517 lines (461 loc) · 17.3 KB
/
Copy pathTData_common.py
File metadata and controls
517 lines (461 loc) · 17.3 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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
def global_attributes(nc, meta, ET, mode):
from datetime import datetime
for x in range(0,len(meta[:,0])):
nc.setncattr(meta[x,0], meta[x,1])
# write specific global attribute
nc.last_revised_date = datetime.utcnow().isoformat()
nc.time_coverage_start = datetime.utcfromtimestamp(ET[0]).isoformat()
nc.time_coverage_end = datetime.utcfromtimestamp(ET[len(ET)-1]).isoformat()
if 'land' in mode:
nc.deployment_mode = 'land'
nc.platform_type = 'stationary_platform'
nc.geospatial_bounds = '51.5733N -1.3147E'
if 'sea' in mode:
nc.deployment_mode = 'sea'
nc.platform_type = 'moving_platform'
nc.geospatial_bounds = '50.1514N -11.1214E, 51.1514N -10.1214E'
if 'air' in mode:
nc.deployment_mode = 'air'
nc.platform_type = 'moving_platform'
nc.geospatial_bounds = '51.5733N -1.3147E, 52.5733N -0.3147E'
del datetime
def global_attributes_sonde(nc, meta, ET, mode):
from datetime import datetime
for x in range(0,len(meta[:,0])):
nc.setncattr(meta[x,0], meta[x,1])
# write specific global attribute
nc.last_revised_date = datetime.utcnow().isoformat()
nc.time_coverage_start = datetime.utcfromtimestamp(ET[0]).isoformat()
nc.time_coverage_end = datetime.utcfromtimestamp(ET[len(ET)-1]).isoformat()
nc.geospatial_bounds = '51.5733N -1.3147E'
nc.featureType = 'trajectory'
if 'land' in mode:
nc.deployment_mode = 'land'
nc.platform_type = 'stationary_platform'
nc.platform = 'ral'
if 'sea' in mode:
nc.deployment_mode = 'sea'
nc.platform_type = 'moving_platform'
nc.platform = 'ral-sea'
if 'air' in mode:
nc.deployment_mode = 'air'
nc.platform_type = 'moving_platform'
nc.platform = 'ral-air'
del datetime
def dimensions(nc, ET, lat, lon):
import numpy as np
time = nc.createDimension('time', len(ET))
if np.isscalar(lat):
latitude = nc.createDimension('latitude', 1)
longitude = nc.createDimension('longitude', 1)
else:
latitude = nc.createDimension('latitude', len(lat))
longitude = nc.createDimension('longitude', len(lon))
del np
def dimensions_sonde(nc, ET):
time = nc.createDimension('time', len(ET))
def variables(nc, ET, DT, DoY, lat, lon, mode):
import numpy as np
#time
v = nc.createVariable('time', np.float64, ('time',))
#variable attributes
v.units = 'seconds since 1970-01-01 00:00:00'
v.standard_name = 'time'
v.long_name = 'Time (seconds since 1970-01-01 00:00:00)'
v.axis = 'T'
v.valid_min = np.float64(min(ET))
v.valid_max = np.float64(max(ET))
v.calendar = 'standard'
#write data
v[:] = np.float64(ET)
if mode == 'land':
#lat
v = nc.createVariable('latitude', np.float32, ('latitude',))
#variable attributes
v.units = 'degrees_north'
v.standard_name = 'latitude'
v.long_name = 'Latitude'
#write data
v[:] = np.float32(lat)
#lon
v = nc.createVariable('longitude', np.float32, ('longitude',))
#variable attributes
v.units = 'degrees_east'
v.standard_name = 'longitude'
v.long_name = 'Longitude'
#write data
v[:] = np.float32(lon)
#doy
v = nc.createVariable('day_of_year', np.float32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Day of Year'
v.valid_min = np.float32(min(DoY))
v.valid_max = np.float32(max(DoY))
#write data
v[:] = np.float32(DoY)
#year
v = nc.createVariable('year', np.int32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Year'
v.valid_min = np.int32(min(DT[:,0]))
v.valid_max = np.int32(max(DT[:,0]))
#write data
v[:] = np.int32(DT[:,0])
#month
v = nc.createVariable('month', np.int32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Month'
v.valid_min = np.int32(min(DT[:,1]))
v.valid_max = np.int32(max(DT[:,1]))
#write data
v[:] = np.int32(DT[:,1])
#day
v = nc.createVariable('day', np.int32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Day'
v.valid_min = np.int32(min(DT[:,2]))
v.valid_max = np.int32(max(DT[:,2]))
#write data
v[:] = np.int32(DT[:,2])
#hour
v = nc.createVariable('hour', np.int32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Hour'
v.valid_min = np.int32(min(DT[:,3]))
v.valid_max = np.int32(max(DT[:,3]))
#write data
v[:] = np.int32(DT[:,3])
#minute
v = nc.createVariable('minute', np.int32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Minute'
v.valid_min = np.int32(min(DT[:,4]))
v.valid_max = np.int32(max(DT[:,4]))
#write data
v[:] = np.int32(DT[:,4])
#second
v = nc.createVariable('second', np.float32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Second'
v.valid_min = np.float32(min(DT[:,5]))
v.valid_max = np.float32(max(DT[:,5]))
#write data
v[:] = np.float32(DT[:,5])
if ((mode == 'air') or ((mode == 'sea'))):
import TData_data as dat
[data_1d, flag_1d, min_dat, max_dat] = dat.create_data_flag(ET)
v = nc.createVariable('latitude', np.float32, ('latitude',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degrees_north'
v.standard_name = 'latitude'
v.long_name = 'Latitude'
v.axis = 'Y'
v.valid_min = np.float32(min(lat))
v.valid_max = np.float32(max(lat))
v.cell_methods = 'time: mean'
#write data
v[:] = np.float32(lat)
v = nc.createVariable('longitude', np.float32, ('longitude',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degrees_east'
v.standard_name = 'longitude'
v.long_name = 'Longitude'
v.axis = 'X'
v.valid_min = np.float32(min(lon))
v.valid_max = np.float32(max(lon))
v.cell_methods = 'time: mean'
#write data
v[:] = np.float32(lon)
v = nc.createVariable('platform_speed_wrt_ground', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'm s-1'
v.standard_name = 'platform_speed_wrt_ground'
v.long_name = 'Platform speed with respect to ground'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: mean'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('platform_course', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.standard_name = 'platform_course'
v.long_name = 'Direction in which the platform is travelling'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: mean'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('platform_orientation', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.standard_name = 'platform_orientation'
v.long_name = 'Direction in which "front" of platform is pointing'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: mean'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_pitch_angle', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Pitch Angle'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: mean'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_pitch_rate', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree s-1'
v.long_name = 'Instrument Pitch Angle Rate of Change'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: mean'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_pitch_minimum', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Pitch Angle Minimum'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: minimum'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_pitch_maximum', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Pitch Angle Maximum'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: maximum'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_pitch_standard_deviation', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Pitch Standard Deviation'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: standard_deviation'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_roll_angle', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Roll Angle'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: mean'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_roll_rate', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree s-1'
v.long_name = 'Instrument Roll Angle Rate of Change'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: mean'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_roll_minimum', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Roll Angle Minimum'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: minimum'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_roll_maximum', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Roll Angle Maximum'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: maximum'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_roll_standard_deviation', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Roll Standard Deviation'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: standard_deviation'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_yaw_angle', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Yaw Angle'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: mean'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_yaw_rate', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree s-1'
v.long_name = 'Instrument Yaw Angle Rate of Change'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: mean'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_yaw_minimum', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Yaw Angle Minimum'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: minimum'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_yaw_maximum', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Yaw Angle Maximum'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: maximum'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('instrument_yaw_standard_deviation', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'degree'
v.long_name = 'Instrument Yaw Standard Deviation'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: standard_deviation'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
del dat
if mode == 'air':
v = nc.createVariable('platform_speed_wrt_air', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'm s-1'
v.standard_name = 'platform_speed_wrt_air'
v.long_name = 'Platform speed with respect to air'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: mean'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
v = nc.createVariable('altitude', np.float32, ('time',), fill_value=-1.00e+20)
#variable attributes
v.units = 'm'
v.standard_name = 'altitude'
v.long_name = 'Geometric height above geoid (WGS84).'
v.axis = 'Z'
v.valid_min = np.float32(min_dat)
v.valid_max = np.float32(max_dat)
v.cell_methods = 'time: mean'
v.coordinates = 'latitude longitude'
#write data
v[:] = np.float32(data_1d)
del np
def variables_sonde(nc, ET, DT, DoY):
import numpy as np
#time
v = nc.createVariable('time', np.float64, ('time',))
#variable attributes
v.units = 'v since 1970-01-01 00:00:00'
v.standard_name = 'time'
v.long_name = 'Time (v since 1970-01-01) 00:00:00'
v.axis = 'T'
v.valid_min = np.float64(min(ET))
v.valid_max = np.float64(max(ET))
v.calendar = 'standard'
#write data
v[:] = np.float64(ET)
#doy
v = nc.createVariable('day_of_year', np.float32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Day of Year'
v.valid_min = np.float32(min(DoY))
v.valid_max = np.float32(max(DoY))
#write data
v[:] = np.float32(DoY)
#year
v = nc.createVariable('year', np.int32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Year'
v.valid_min = np.int32(min(DT[:,0]))
v.valid_max = np.int32(max(DT[:,0]))
#write data
v[:] = np.int32(DT[:,0])
#month
v = nc.createVariable('month', np.int32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Month'
v.valid_min = np.int32(min(DT[:,1]))
v.valid_max = np.int32(max(DT[:,1]))
#write data
v[:] = np.int32(DT[:,1])
#day
v = nc.createVariable('day', np.int32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Day'
v.valid_min = np.int32(min(DT[:,2]))
v.valid_max = np.int32(max(DT[:,2]))
#write data
v[:] = np.int32(DT[:,2])
#hour
v = nc.createVariable('hour', np.int32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Hour'
v.valid_min = np.int32(min(DT[:,3]))
v.valid_max = np.int32(max(DT[:,3]))
#write data
v[:] = np.int32(DT[:,3])
#minute
v = nc.createVariable('minute', np.int32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Minute'
v.valid_min = np.int32(min(DT[:,4]))
v.valid_max = np.int32(max(DT[:,4]))
#write data
v[:] = np.int32(DT[:,4])
#second
v = nc.createVariable('second', np.float32, ('time',))
#variable attributes
v.units = '1'
v.long_name = 'Second'
v.valid_min = np.float32(min(DT[:,5]))
v.valid_max = np.float32(max(DT[:,5]))
#write data
v[:] = np.float32(DT[:,5])