-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_frame.py
1221 lines (1057 loc) · 56.1 KB
/
data_frame.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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Created on Jun 22, 2018
@author: savis
'''
from __future__ import print_function, division
import os
from datetime import timedelta, datetime
import numpy as np
from scipy import stats
import pandas as pd
import warnings
try:
import cPickle as pickle
except ModuleNotFoundError:
import pickle
from aeroct import aeronet
from aeroct import modis
from aeroct import metum
from aeroct import utils
SCRATCH_PATH = os.popen('echo $SCRATCH').read().rstrip('\n') + '/aeroct/'
MODIS_DUST_FILTERS = [['ARSL_TYPE_LAND'],['AE_LAND', 'SSA_LAND'],
['FM_FRC_OCEAN', 'AE_OCEAN', 'EFF_RAD_OCEAN', 'MASS_CONC', 'REGION_OCEAN']]
# How to output the names of the data sets
ds_printname = {'aeronet': 'AERONET',
'modis': 'MODIS',
'modis_t' : 'MODIS Terra',
'modis_a' : 'MODIS Aqua',
'metum': 'Unified Model'}
ds_filename = {'aeronet': 'AERONET',
'modis': 'MODIS',
'modis_t' : 'MODIS_Terra',
'modis_a' : 'MODIS_Aqua',
'metum': 'Unified_Model'}
def set_dust_filters(dust_filters):
'''
Set the dust filters to use for retrieving dust data for MODIS.
Parameters
----------
dust_filters : list of str
This lists the conditions to decide which AOD values are dominated by dust.
The conditions within a secondary array are combined using AND, while the
conditions in the first array are combined with OR.
ie. [['a', 'b'], 'c'] represents (filter['a'] AND filter['b]) OR filter['c'].
Available filters:
- 'ARSL_TYPE_LAND': If it has been flagged as dust already.
- 'AE_LAND': angstrom exponent <= 0.6 for land data.
- 'SSA_LAND': 0.878 < scattering albedo < 0.955 for land data.
- 'FM_FRC_OCEAN': fine mode fraction <= 0.45 for ocean data.
- 'AE_OCEAN': angstrom exponent <= 0.5 for ocean data.
- 'EFF_RAD_OCEAN' : effective radius > 1.0 micron.
- 'MASS_CONC' : mass concentration >= 1.2e-4 kg / m^2.
- 'REGION_OCEAN': Only ocean data within the dust regions are selected.
- 'NONE': No filter.
By default the following are used:
['ARSL_TYPE_LAND', ['AE_LAND', 'SSA_LAND'], ['FM_FRC_OCEAN', 'AE_OCEAN',
'EFF_RAD_OCEAN', 'MASS_CONC', 'REGION_OCEAN']]
However if the data has been retrieved from MetDB only ARSL_TYPE_LAND has an
effect.
'''
global MODIS_DUST_FILTERS
MODIS_DUST_FILTERS = dust_filters
class DataFrame():
'''
The data frame into which the AOD data is processed. Only a single day's data is
included from a single source (& forecast time).
The total AOD data is stored in the first index of the 'aod' attribute. If there are
values for the AOD due to dust at every location then these are stored in the second
index of 'aod'. If there are filters in the 'dust_filters' attribute (eg. MODIS data)
then the AOD values can be filtered using various combinations of these to obtain the
dust dominated data points.
The AOD data, longitudes, latitudes, and times for each data point are stored in 1D
NumPy arrays if the data frame is not loaded from an Iris cube (ie. not model data).
If it has been loaded from an iris cube then the AOD data will be 3D and the
longitudes, latitudes, and times attributes store only the axes data (the order for
which is time, lat, lon).
----------
Attributes
----------
aod : list of two 1D or 3D NumPy arrays
The first element of the list contains the total AOD, the second contains the
coarse mode AOD. These are only 3D when the data is gridded in which case 'cube'
contains an Iris cube. If 3D the order of indices is [time, latitude, longitude].
longitudes : 1D NumPy array
This contains the values of the longitude either at every data point or along an
axis. (In range -180 to 180)
latitudes : 1D NumPy array
This contains the values of the latitude either at every data point or along an
axis.
times : 1D NumPy array
This contains the values of the time in hours from 00:00 on the given date,
either at every data point or along an axis.
date : datetime, or datetime list
The date for which the DataFrame instance contains data. Note that this is not
the date of the beginning of the forecast if the DataFrame contains forecast
data. If the data has been concatenated then this is a list of datetime objects with one per
wavelength : int
The wavelength, in nm, for which the AOD data has been taken. (Usually 550 nm)
forecast_time : float
If the data is from a model this contains the forecast lead time in hours.
Otherwise it is None.
data_set : {'aeronet', 'metum', 'modis', 'modis_a', 'modis_t'}
This indicates the source of the data contained within the DataFrame.
name : str
This gives the name of the source in a format for printing. It also includes the
forecast lead time for model data.
sites : str or None
For AERONET data this contains all of the names of the sites in the data
dust_filters : dict or None
Dictionary containing lists of indices for the AOD data which satisfies
various dust filter conditions. Only currently used for MODIS data.
Possible MODIS fields:
- 'ARSL_TYPE_LAND': If it has been flagged as dust already.
- 'AE_LAND': Angstrom exponent <= 0.6 for land data.
- 'SSA_LAND': 0.878 < scattering albedo < 0.955 for land data.
- 'FM_FRC_OCEAN': Fine mode fraction <= 0.45 for ocean data.
- 'AE_OCEAN': Angstrom exponent <= 0.5 for ocean data.
- 'EFF_RAD_OCEAN': effective radius > 1.0 micron.
- 'MASS_CONC': mass concentration >= 1.2e-4 kg / m^2.
- 'REGION_OCEAN': Only ocean data within the regions with dust are selected.
- 'NONE': No filter.
cube : Iris cube or None
If the data is obtained from an Iris cube then it is supplied here.
additional_data : list of str
Extra descriptive data about the data frame such as whether it has been
extracted from another DataFrame and the bounds used.
-------
Methods
-------
datetimes :
Returns the times as a list of datetime objects rather than the time in hours.
get_data :
Get an array with either all the total / dust AOD data or the dust AOD data using
dust filters. This is returned along with the corresponding longitudes,
latitudes, and times.
dump :
Saves the DataFrame as a pickled file in the chosen location.
extract :
Return a new DataFrame containing only the data within the given bounds (inclusive).
------------
Initialising
------------
Parameters for calling the class directly:
aod : list of two 1D or 3D NumPy arrays
The first element of the list contains the total AOD, the second contains the
coarse mode AOD. These should only be 3D when a cube is passed as an argument;
in this case the order of indices is [time, latitude, longitude].
longitudes : 1D NumPy array
This contains the values of the longitude either at every data point or along an
axis. (In range -180 to 180)
latitudes : 1D NumPy array
This contains the values of the latitude either at every data point or along an
axis.
times : 1D NumPy array
This contains the values of the time in hours from 00:00 on the given date,
either at every data point or along an axis.
date : datetime
The date for which the DataFrame instance contains data. Note that this is not
the date of the beginning of the forecast if the DataFrame contains forecast
data.
wavelength : int, optional (Default: 550)
This is the wavelength in nm at which the AOD data has been taken.
data_set : {'aeronet', 'metum', 'modis', 'modis_a', 'modis_t'}
This indicates the source of the data contained within the DataFrame.
Optional kwargs:
forecast_time : float, optional (Default: None)
If the data is from a model this contains the forecast lead time in hours.
dust_filters : dict, optional (Default: None)
A dictionary containing lists of indices for the AOD data which satisfies
various dust filter conditions.
sites : NumPy array of str
The names of the AERONET sites for each of the data points if the data set is
AERONET.
cube : Iris cube (Default: None)
If the data is obtained from an Iris cube then it is supplied here.
additional_data : list of str (Default: [])
Extra descriptive data about the data frame.
Parameters for the from_cube class method:
cube : Iris cube
This is the Iris cube that contains the AOD data required to create the
DataFrame.
data_set : {'metum'}
This indicates the source of the data.
'''
def __init__(self, aod, longitudes, latitudes, times, date, wavelength=550,
data_set=None, **kwargs):
# Ensure longitudes are in range [-180, 180]
longitudes = longitudes.copy()
longitudes[longitudes > 180] -= 360
# Data and axes
self.aod = aod # AOD data [Total, Dust])
self.longitudes = longitudes # [degrees]
self.latitudes = latitudes # [degrees]
self.times = times # [hours since 00:00:00 on date]
# Meta-data
self.date = date # (datetime)
self.wavelength = wavelength # [nm]
self.data_set = data_set # The name of the data set
self.forecast_time = kwargs.setdefault('forecast_time', None) # [hours]
# Name for printing
self.name = ds_printname[data_set]
if self.forecast_time is not None:
self.name += ' (T+{0}h)'.format(int(self.forecast_time))
self.sites = kwargs.setdefault('sites', None)
self.dust_filters = kwargs.setdefault('dust_filters', None)
self.cube = kwargs.setdefault('cube', None)
self.additional_data = kwargs.setdefault('additional_data', [])
@classmethod
def from_cube(cls, cube, data_set):
# Create a DataFrame using a cube containing model data (dust AOD only)
aod = [None, cube.data]
lons = cube.coord('longitude').points
lats = cube.coord('latitude').points
times = cube.coord('time').points
date = cube.coord('date').points[0]
wl = cube.coord('wavelength').points[0]
fc_time = cube.coord('forecast_time').points[0]
return cls(aod, lons, lats, times, date, wl, data_set,
forecast_time=fc_time, cube=cube)
def datetimes(self):
return [self.date + timedelta(hours=h) for h in self.times]
def get_data(self, aod_type=None, dust_filter_fields=None, return_type=False):
'''
Get an array with either all the total AOD data or the dust AOD data. This is
returned along with the corresponding longitudes, latitudes, and times.
Return: aod, lon, lat, times(, data type).
Parameters
----------
aod_type : {None, 'total, or 'dust'}, optional (Default: None)
The type of AOD data to return.
None: Return the total AOD if the data frame contains both. If it contains
only one type of AOD data then that is returned instead.
If 'total' or 'dust' is selected and the data frame does not contain that
type of data then a ValueError is raised.
dust_filter_fields : list of str, optional
This is used if dust AOD is to be retrieved and the data frame contains dust
filters (ie. MODIS data). This lists the conditions to decide which AOD
values are dominated by dust. The conditions within a secondary array are
combined using AND, while the conditions in the first array are combined with OR.
ie. [['a', 'b'], 'c'] represents (filter['a'] AND filter['b]) OR filter['c'].
MODIS fields:
- 'ARSL_TYPE_LAND': If it has been flagged as dust already.
- 'AE_LAND': angstrom exponent <= 0.6 for land data.
- 'SSA_LAND': 0.878 < scattering albedo < 0.955 for land data.
- 'FM_FRC_OCEAN': fine mode fraction <= 0.45 for ocean data.
- 'AE_OCEAN': angstrom exponent <= 0.5 for ocean data.
- 'EFF_RAD_OCEAN' : effective radius > 1.0 micron.
- 'MASS_CONC' : mass concentration >= 1.2e-4 kg / m^2.
- 'REGION_OCEAN': Only ocean data within the dust regions are selected.
- 'NONE': No filter.
By default ['ARSL_TYPE_LAND'] is used if the data has been retrieved from
MetDB. If downloaded from NASA the following is used:
[['AE_LAND', 'SSA_LAND'], ['FM_FRC_OCEAN', 'AE_OCEAN', 'EFF_RAD_OCEAN',
'MASS_CONC', 'REGION_OCEAN']]
return_type : bool, optional (Default: False)
If True, return the AOD type as well, ie. 'dust' or 'total'.
'''
get_total = (aod_type=='total') | ((aod_type is None) &
(self.aod[0] is not None))
get_dust = (aod_type=='dust') | ((aod_type is None) &
(self.aod[0] is None))
# Total AOD selection
if get_total:
if self.aod[0] is None:
raise ValueError('The data frame does not include total AOD data.')
aod = self.aod[0]
lon = self.longitudes
lat = self.latitudes
times = self.times
# Dust AOD selection
elif get_dust:
if (self.aod[1] is None) & (self.dust_filters is None):
raise ValueError('The data frame does not include dust AOD data.')
if self.cube is None:
# If there are no dust filters
if self.dust_filters is None:
aod = self.aod[1]
lon = self.longitudes
lat = self.latitudes
times = self.times
# Use the dust filter to decide which AOD values are dominated by dust
else:
# Get default filter fields
if dust_filter_fields is None:
# From MetDB
if np.all(self.dust_filters['AE_LAND']):
if MODIS_DUST_FILTERS != ['NONE']:
dust_filter_fields = ['ARSL_TYPE_LAND']
else:
dust_filter_fields = ['NONE']
# From NASA
else:
dust_filter_fields = MODIS_DUST_FILTERS
# Perform AND over the filters in the second index and
# OR over the first index
dust_filter = []
for f in dust_filter_fields:
if isinstance(f, list):
dust_filter.append(np.all([self.dust_filters[f2]
for f2 in f], axis=0))
else:
dust_filter.append(self.dust_filters[f])
dust_filter = np.any(dust_filter, axis=0)
if self.aod[1] is None:
aod = self.aod[0][dust_filter]
else:
aod = self.aod[1][dust_filter]
lon = self.longitudes[dust_filter]
lat = self.latitudes[dust_filter]
times = self.times[dust_filter]
else:
# If a cube is used then get the longitude, latitude and time for every point
aod = self.aod[1].ravel()
axes = np.ix_(self.times, self.latitudes, self.longitudes)
grid = np.broadcast_arrays(*axes)
lon = grid[2].ravel()
lat = grid[1].ravel()
times = grid[0].ravel()
if return_type == False:
return aod, lon, lat, times
else:
if get_total:
return aod, lon, lat, times, 'total'
else:
return aod, lon, lat, times, 'dust'
def dump(self, filename=None, save_dir=SCRATCH_PATH+'data_frames/'):
'''
Saves the DataFrame as a pickled file in the chosen location. Note that some
DataFrames can be very large and take some time to save / load.
Parameters
----------
filename : str, optional (Default: '{data_set}-YYYYMMDD.pkl')
What to name the saved file.
save_dir : str, optional (Default: '/scratch/{USER}/aeroct/data_frames/')
The path to the directory where the file will be saved.
'''
# Make directory if it does not exist
os.system('mkdir -p {0}'.format(save_dir))
file_ext = 'pkl'
if filename != None:
pass
elif type(self.data_set) == str:
if self.forecast_time is None:
filename = '{0}-{1}'.format(self.data_set, self.date.strftime('%Y%m%d'))
else:
filename = '{0}{1}-{2}'.format(self.data_set, self.forecast_time,
self.date.strftime('%Y%m%d'))
else:
raise ValueError('data_set attribute invalid. Cannot create filename')
# Write file
filepath = save_dir + filename + file_ext
os.system('touch {0}'.format(filepath))
with open(filepath, 'w') as writer:
pickle.dump(self, writer, -1)
print('DataFrame saved successfully to:\n {0}'.format(filepath))
def extract(self, bounds=(-180, 180, -90, 90), time_bounds=(0, 24), aeronet_site=None):
'''
Return a new DataFrame only containing the data for the given AERONET site or
within longitude-latitude bounds (inclusive) if no AERONET site is provided.
Parameters
----------
bounds : 4-tuple, or list of 4-tuples, optional (Default: (-180, 180, -90, 90))
This contains the latitude and longitude bounds for which to extract data (if
aeronet_site is None). If it is a list of 4-tuples then each corresponds to a
region for which the data shall be extracted. The 4-tuples contain the bounds
as follows: (min lon, max lon, min lat, max lat)
time_bounds : float 2-tuple, optional (Default: (0, 24))
The bounds on the time (hours).
aeronet_site : str, optional (Default: None)
The name of the site for which to extract data. If this is provided then the
bounds are not used.
'''
# If an AERONET site is provided get the indices of the data at that site
if aeronet_site is not None:
if self.sites is None:
raise ValueError('DataFrame does not contain AERONET data.')
elif not (aeronet_site in self.sites):
raise ValueError('DataFrame contains no data for AERONET site: {0}'\
.format(aeronet_site))
selected = (self.sites == aeronet_site)
sites = self.sites[selected]
ext_description = 'Extracted for AERONET site: {0}'.format(aeronet_site)
# Get the indices for the data within the bounds
else:
if isinstance(bounds[0], (int, long, float)):
in_lon = (self.longitudes >= bounds[0]) & (self.longitudes <= bounds[1])
in_lat = (self.latitudes >= bounds[2]) & (self.latitudes <= bounds[3])
in_bounds = np.array(in_lon & in_lat)
else:
in_bounds = np.zeros_like(self.longitudes)
for bound in bounds:
in_lon = (self.longitudes >= bound[0]) & (self.longitudes <= bound[1])
in_lat = (self.latitudes >= bound[2]) & (self.latitudes <= bound[3])
in_bounds += (in_lon & in_lat)
in_bounds = np.array(in_bounds, dtype=bool)
in_time = (self.times >= time_bounds[0]) & (self.times <= time_bounds[1])
selected = (in_time & in_bounds)
ext_description = 'Extracted for lon, lat: {0}, time: {1}'\
.format(bounds, time_bounds)
# Select the data if it is 3D
if self.cube is not None:
lons = self.longitudes[in_lon]
lats = self.latitudes[in_lat]
times = self.times[in_time]
cube = self.cube[in_time, in_lat, in_lon]
if self.aod[0] is not None:
aod0 = self.aod[0][in_time, in_lat, in_lon]
else:
aod0 = None
if self.aod[1] is not None:
aod1 = self.aod[1][in_time, in_lat, in_lon]
else:
aod1 = None
if self.dust_filters is not None:
dust_filters = self.dust_filters.copy()
for key in dust_filters.keys():
dust_filters[key] = np.array(dust_filters[key])[in_time, in_lat, in_lon]
else:
dust_filters = None
if isinstance(self.date, list):
date = list(np.array(self.date)[in_time])
# Get the data within the bounds / AERONET site
if self.cube is None:
lons = self.longitudes[selected]
lats = self.latitudes[selected]
times = self.times[selected]
cube = None
if self.aod[0] is not None:
aod0 = self.aod[0][selected]
else:
aod0 = None
if self.aod[1] is not None:
aod1 = self.aod[1][selected]
else:
aod1 = None
sites = self.sites[selected] if (self.sites is not None) else None
if self.dust_filters is not None:
dust_filters = self.dust_filters.copy()
for key in dust_filters.keys():
dust_filters[key] = np.array(dust_filters[key])[selected]
else:
dust_filters = None
if isinstance(self.date, list):
date = list(np.array(self.date)[selected])
if not isinstance(self.date, list):
date = self.date
if hasattr(self, 'additional_data'):
additional_data = self.additional_data.append(ext_description)
else:
additional_data = [ext_description]
return DataFrame([aod0, aod1], lons, lats, times, date, self.wavelength,
self.data_set, forecast_time=self.forecast_time, sites=sites,
dust_filters=dust_filters, cube=cube,
additional_data=additional_data)
class MatchFrame():
'''
This data frame is used to contain AOD data matched between two data sources. Each
matched-up data point is obtained by taking the mean of the original data points
within a maximum distance and time. Various stats for the matched-up data are also
supplied.
Several attributes including the 'data' attribute containing matched-up AOD data have
two values for the first index corresponding to the two data-sets. When plotted on a
scatter plot the first of these (data[0]) is put on the x-axis, and the second
(data[1]) along the y-axis. Additionally, the AOD bias is calculated as:
data[1] - data[0].
----------
Attributes
----------
data : list of two 1D or 3D NumPy arrays
The two elements of the list contain the mean matched-up AOD data for the two
data-sets. These are only 3D when the data is gridded in which case 'cube'
contains an Iris cube. If 3D the order of indices is [time, latitude, longitude].
data_std : list of two 1D or 3D NumPy arrays
The standard deviations of the matched-up AOD at every point for each data-set.
data_num : list of two 1D or 3D NumPy arrays
The number of original data points used to obtain each matched-up AOD value for
each data-set.
time_diff : 1D or 3D NumPy array
The mean time difference between the original data points for each matched-up
AOD value.
longitudes : 1D NumPy array
This contains the values of the longitude either at every data point or along an
axis. (In range -180 to 180)
latitudes : 1D NumPy array
This contains the values of the latitude either at every data point or along an
axis.
times : 1D NumPy array
This contains the values of the time in hours from 00:00 on the data's date,
either at every data point or along an axis.
sites : NumPy array of str or None
The names of the AERONET sites for each of the data points if AERONET data has
been matched, otherwise it is None.
date : datetime, or list of datetimes
The date for which the MatchFrame instance contains data. It is a list if
multiple days have been concatenated into a single MatchFrame.
match_dist : int
The maximum distance for which data has been matched and averaged in km.
match_time : int
The maximum time over which data has been matched and averaged in hours.
wavelength : int
The wavelength, in nm, for which the AOD data has been taken. (Usually 550 nm)
forecast_times : 2-tuple of floats
If the data is from a model this contains the forecast lead time in hours,
otherwise it is None. eg. (None, 6) if the first data-set is not a forecast and
the second has a lead time of six hours.
aod_type : {'total' or 'dust'}
The type of AOD data which has been matched.
data_sets : 2-tuple of {'aeronet', 'metum', 'modis', 'modis_a', 'modis_t'}
This indicates the source of each set of data contained within the MatchFrame.
names : 2-tuple of str
This gives the name of the source of each data-set in a format for printing. It
also includes the forecast lead time for model data.
cube : Iris cube or None
If the two data-sets have Iris cubes then this contains a cube with the bias of
the data points.
additional_data : list of str
Extra descriptive data about the data frame such as whether it has been
extracted from another DataFrame and the bounds used.
num : int
The total number of matched-up data points.
rms : float
The root mean square value of the data: sqrt(mean((data[1] - data[0])**2)).
bias_mean : float
The mean bias between the data: mean(data[1] - data[0]).
bias_std : float
The standard deviation of the bias between the data: std(data[1] - data[0]).
r2 : float
The coefficient of determination for the correlation to the y=x line.
r_intercept, r_slope : float
The linear regression coefficients for the data.
r : float
The Pearson's correlation coefficient for the linear regression.
log_r_intercept, log_r_slope : float
The regression coefficients when fitting to the log of the data.
ie. log10(y) = log_r_intercept + log10(x) * log_r_slope
log_r : float
The Pearson's correlation coefficient for the logarithmic regression.
-------
Methods
-------
datetimes :
Returns the times as a list of datetime objects rather than the time in hours.
Only possible if the MatchFrame has not been concatenated.
pd_dataframe :
Returns a Pandas dataframe containing the data for every data point. It does not
contain metadata such as the match_time/dist and wavelength.
dump :
Saves the MatchFrame in the chosen location either as a pickle file or a csv.
extract :
Return a new MatchFrame containing only the data within the given bounds.
------------
Initialising
------------
Parameters:
data : list of two 1D or 3D NumPy arrays
The two elements of the list contain the mean matched-up AOD data for the two
data-sets. These should only be 3D when the data is gridded in which case 'cube'
should be assigned. If 3D the order of indices is [time, latitude, longitude].
data_std : list of two 1D or 3D NumPy arrays
The standard deviations of the matched-up AOD at every point for each data-set.
data_num : list of two 1D or 3D NumPy arrays
The number of original data points used to obtain each matched-up AOD value for
each data-set.
time_diff : 1D or 3D NumPy array
The mean time difference between the original data points for each matched-up
AOD value.
longitudes : 1D NumPy array
This contains the values of the longitude either at every data point or along an
axis. (In range -180 to 180)
latitudes : 1D NumPy array
This contains the values of the latitude either at every data point or along an
axis.
times : 1D NumPy array
This contains the values of the time in hours from 00:00 on the data's date,
either at every data point or along an axis.
date : datetime, or list of datetimes
The date for which the MatchFrame instance contains data. It should be a list if
multiple days have been concatenated into a single MatchFrame.
match_time : int
The maximum time over which data has been matched and averaged in hours.
match_dist : int
The maximum distance for which data has been matched and averaged in degrees.
wavelength : int, optional (Default: 550)
The wavelength, in nm, for which the AOD data has been taken.
forecast_times : 2-tuple of floats, optional (Default: (None, None))
If the data is from a model this contains the forecast lead time in hours,
otherwise it is None. eg. (None, 6) if the first data-set is not a forecast and
the second has a lead time of six hours.
data_sets : 2-tuple of {'aeronet', 'metum', 'modis', 'modis_a', 'modis_t'}
This indicates the source of each set of data contained within the MatchFrame.
aod_type : {'total' or 'dust'}
The type of AOD data which has been matched.
Optional kwargs:
sites : NumPy array of str
The names of the AERONET sites for each of the data points if AERONET data has
been matched.
cube : Iris cube or None
If the two data-sets are both gridded then this should be assigned to a cube
containg bias data.
additional_data : list of str
Extra descriptive data about the data frame.
'''
def __init__(self, data, data_std, data_num, time_diff, longitudes, latitudes, times,
date, match_time, match_dist, wavelength=550, forecast_times=(None, None),
data_sets=(None, None), aod_type='total', **kw):
# Data and axes
self.data = data # Averaged AOD data (Not flattend if cube != None)
self.data_std = data_std # Averaged AOD data standard deviations
self.data_num = data_num # Number of values that are averaged
self.time_diff = time_diff # The average difference in times (idx1 - idx0)
self.longitudes = longitudes # [degrees]
self.latitudes = latitudes # [degrees]
self.times = times # [hours since 00:00:00 on date]
self.sites = kw.setdefault('sites', None) # AERONET site names
# Meta-data
self.date = date # (datetime)
self.match_dist = match_dist # Maximum spacial difference between collocated points (km)
self.match_time = match_time # Maximum time difference between collocated points
self.wavelength = wavelength # [nm]
self.forecast_times = forecast_times # [hours] tuple
self.data_sets = data_sets # A tuple of the names of the data sets
self.aod_type = aod_type # Whether it is coarse mode AOD or total AOD
self.cube = kw.setdefault('cube', None) # Contains AOD difference for a model-model match
self.names = ['', '']
for i in [0,1]:
self.names[i] = ds_printname[data_sets[i]]
if self.forecast_times[i] is not None:
self.names[i] += ' (T+{0}h)'.format(int(self.forecast_times[i]))
self.additional_data = kw.setdefault('additional_data', [])
# Stats
self.num = self.data[0].size
# Suppress warnings from averaging over empty arrays
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
self.rms = np.sqrt(np.mean((self.data[1] - self.data[0])**2)) # Root mean square
self.bias_mean = np.mean(self.data[1] - self.data[0]) # y - x mean
self.bias_std = np.std(self.data[1] - self.data[0]) # standard deviation
if self.num > 1:
# R2
y_mean = np.mean(self.data[1])
ss_tot = np.sum((self.data[1] - y_mean) ** 2)
ss_res = np.sum((self.data[1] - self.data[0]) ** 2)
self.r2 = 1 - ss_res / ss_tot
data0 = self.data[0][(self.data[0] > 0) & (self.data[1] > 0)]
data1 = self.data[1][(self.data[0] > 0) & (self.data[1] > 0)]
if data0.size > 2:
# Linear Regression
self.r_slope, self.r_intercept, self.r = \
stats.linregress(self.data[0], self.data[1])[:3]
# Log Regression
self.log_r_slope, self.log_r_intercept, self.log_r = \
stats.linregress(np.log10(data0), np.log10(data1))[:3]
else:
self.r_slope, self.r_intercept, self.r = np.nan, np.nan, np.nan
self.log_r_slope, self.log_r_intercept, self.log_r = np.nan, np.nan, np.nan
else:
self.r_slope, self.r_intercept, self.r = np.nan, np.nan, np.nan
self.log_r_slope, self.log_r_intercept, self.log_r = np.nan, np.nan, np.nan
self.r2 = np.nan
def datetimes(self):
'''
Returns the times as a list of datetime objects rather than the time in hours.
Only possible if the MatchFrame has not been concatenated.
'''
if isinstance(self.date, datetime):
return [self.date + timedelta(hours=h) for h in self.times]
else:
return [self.date[i] + timedelta(hours=h) for i, h in enumerate(self.times)]
def pd_dataframe(self):
'''
Returns a Pandas dataframe containing the data for every data point. It does not
contain metadata such as the date and wavelength.
'''
data_array = [self.times, self.latitudes, self.longitudes,
self.data[0], self.data_std[0], self.data_num[0],
self.data[1], self.data_std[1], self.data_num[1],
self.time_diff]
headers = ['Time (hours)', 'Latitude', 'Longitude',
'1: AOD average'.format(self.names[0]),
'1: AOD stdev'.format(self.names[0]),
'1: Number of points'.format(self.names[0]),
'2: AOD average'.format(self.names[1]),
'2: AOD stdev'.format(self.names[1]),
'2: Number of points'.format(self.names[1]),
'Average time difference']
if self.sites is not None:
headers.insert(1, 'AERONET site')
data_array.insert(1, self.sites)
if isinstance(self.date, list):
headers.insert(0, 'Dates')
date_strs = np.array([date.strftime('%Y-%m-%d') for date in self.date])
data_array.insert(0, date_strs)
df = pd.DataFrame(np.array(data_array).T, columns=headers)
return df
def dump(self, filename=None, save_dir=SCRATCH_PATH+'match_frames/',
filetype='csv', subdir=True, verb=True):
'''
Save the data frame as a file in the chosen location if the file already exists
it will be overwritten. The filepath is returned. Note that only pickle files can
be used to load the MatchFrame as the csv files do not contain all of the
necessary metadata.
Parameters
----------
filename : str, optional (Default: '{dataset2}-{dataset1}-{aod-type}-{date str}')
What to name the saved file. {date str} is 'YYYYMMDD' if the MatchFrame has a
single day of data, or '{initial date}-{final date}' if it has been
concatenated from a list of MatchFrames.
save_dir : str, optional (Default: '/scratch/{USER}/aeroct/match_frames/')
The path to the directory where the file will be saved.
filetype : {'pickle', 'csv'}, optional (Default: 'csv')
The type of file to save. This will add a file extension.
subdir : bool, optional (Default: True)
Whether to save the MatchFrames within sub-directories.
verb : bool, optional (Default: True)
If True then a message is printed to the console if it saves successfully.
'''
if save_dir[-1] != '/': save_dir += '/'
# Put the forecast time onto the end of model data-set names for the filename
data_set_names = []
for i, data_set in enumerate(self.data_sets):
if self.forecast_times[i] is not None:
fc_str = str(int(self.forecast_times[i])).zfill(3)
else:
fc_str = ''
data_set_names.append(data_set + fc_str)
# Subdirectories
if subdir:
save_dir += '{0}-{1}-{2}/'.format(data_set_names[1], data_set_names[0],
self.aod_type[0])
# File extension
if filetype in ['pkl', 'pickle']:
save_dir += 'pkl/'
file_ext = '.pkl'
elif filetype == 'csv':
file_ext = '.csv'
# Make directory if it does not exist
os.system('mkdir -p {0}'.format(save_dir))
# Create the filename
if filename is None:
if isinstance(self.date, datetime):
filename_date = self.date.strftime('%Y%m%d')
else:
filename_date = '{0}-{1}'.format(min(self.date).strftime('%Y%m%d'),
max(self.date).strftime('%Y%m%d'))
filename = '{0}-{1}-{2}-{3}'.format(data_set_names[1], data_set_names[0],
self.aod_type[0], filename_date)
filepath = save_dir + filename + file_ext
os.system('touch {0}'.format(filepath))
# Write pickle file
if filetype in ['pkl', 'pickle']:
with open(filepath, 'w') as fout:
pickle.dump(self, fout, -1)
# Write csv file
elif filetype == 'csv':
if isinstance(self.date, list):
first_date = min(self.date).strftime('%Y-%m-%d')
last_date = max(self.date).strftime('%Y-%m-%d')
date_str = '{0} to {1}'.format(first_date, last_date)
else:
date_str = self.date.strftime('%Y-%m-%d')
df = self.pd_dataframe()
metadata = pd.Series([('Match up for {0}'.format(date_str)),
('Data-set 1 : {0}'.format(self.names[0])),
('Data-set 2 : {0}'.format(self.names[1])),
('Match distance (km): {0}'.format(self.match_dist)),
('Match time (minutes): {0}'.format(self.match_time)),
('AOD type : {0}'.format(self.aod_type)),
('Wavelength : {0}'.format(self.wavelength)),
(''),
('Number of matches : {0}'.format(self.num)),
('RMS : {0:.04f}'.format(self.rms)),
('Bias (2-1) mean : {0:.04f}'.format(self.bias_mean)),
('Bias (2-1) std : {0:.04f}'.format(self.bias_std)),
('Regression intercept : {0:.04f}'.format(self.r_intercept)),
('Regression slope : {0:.04f}'.format(self.r_slope)),
('Pearson R : {0:.04f}'.format(self.r)),
('Log reg intercept : {0:.04f}'.format(self.log_r_intercept)),
('Log reg slope : {0:.04f}'.format(self.log_r_slope)),
('Log Pearson R : {0:.04f}'.format(self.log_r)),
('')])
with open(filepath, 'w') as fout:
metadata.to_csv(fout, index=False)
df.to_csv(fout)
if verb: print('MatchFrame saved successfully to:\n {0}'.format(filepath))
return filepath
def extract(self, bounds=(-180, 180, -90, 90), time_bounds=(0, 24), aeronet_site=None):
'''
Return a new MatchFrame only containing the data for the given AERONET site or
within longitude-latitude bounds (inclusive) if no AERONET site is provided.
Parameters
----------
bounds : 4-tuple, or list of 4-tuples, optional (Default: (-180, 180, -90, 90))
This contains the latitude and longitude bounds for which to extract data (if
aeronet_site is None). If it is a list of 4-tuples then each corresponds to a
region for which the data shall be extracted. The 4-tuples contain the bounds
as follows: (min lon, max lon, min lat, max lat)
time_bounds : float 2-tuple, optional (Default: (0, 24))
The bounds on the time (hours).
aeronet_site : str, optional (Default: None)
The name of the site for which to extract data. If this is provided then the
bounds are not used.
'''
# If an AERONET site is provided get the indices of the data at that site
if aeronet_site is not None:
if self.sites is None:
raise ValueError('MatchFrame does not contain AERONET sites.')
elif not (aeronet_site in self.sites):
raise ValueError('MacthFrame contains no data for AERONET site: {0}'\
.format(aeronet_site))
selected = (self.sites == aeronet_site)
sites = self.sites[selected]
ext_description = 'Extracted for AERONET site: {0}'.format(aeronet_site)
# Get the indices for the data within the bounds
else:
if isinstance(bounds[0], (int, long, float)):
in_lon = (self.longitudes >= bounds[0]) & (self.longitudes <= bounds[1])
in_lat = (self.latitudes >= bounds[2]) & (self.latitudes <= bounds[3])
in_bounds = np.array(in_lon & in_lat)
else:
in_bounds = np.zeros_like(self.longitudes)
for bound in bounds:
in_lon = (self.longitudes >= bound[0]) & (self.longitudes <= bound[1])
in_lat = (self.latitudes >= bound[2]) & (self.latitudes <= bound[3])
in_bounds += (in_lon & in_lat)
in_bounds = np.array(in_bounds, dtype=bool)
in_time = (self.times >= time_bounds[0]) & (self.times <= time_bounds[1])
selected = np.array(in_bounds & in_time)
sites = None if (self.sites is None) else self.sites[selected]
ext_description = 'Extracted for lon, lat: {0}, time: {1}'\
.format(bounds, time_bounds)
# Obtain the data within the bounds if the data is gridded
if self.cube is not None:
lons = self.longitudes[in_lon]
lats = self.latitudes[in_lat]
times = self.times[in_time]
data = self.data[:, in_time, in_lat, in_lon]
data_std = self.data_std[:, in_time, in_lat, in_lon]
data_num = self.dat_num[:, in_time, in_lat, in_lon]
time_diff = self.time_diff[in_time, in_lat, in_lon]
cube = self.cube[in_time, in_lat, in_lon]
if isinstance(self.date, list):
date = list(np.array(self.date)[in_time])
# Get the data within the AERONET site / bounds
if self.cube is None:
lons = self.longitudes[selected]
lats = self.latitudes[selected]
times = self.times[selected]
data = self.data[:, selected]
data_std = self.data_std[:, selected]
data_num = self.data_num[:, selected]
time_diff = self.time_diff[selected]
cube = None
if isinstance(self.date, list):
date = list(np.array(self.date)[selected])
if not isinstance(self.date, list):
date = self.date
if hasattr(self, 'additional_data'):
additional_data = list(self.additional_data)
additional_data.append(ext_description)
else:
additional_data = [ext_description]
return MatchFrame(data, data_std, data_num, time_diff, lons, lats, times,
date, self.match_time, self.match_dist, self.wavelength,
self.forecast_times, self.data_sets, self.aod_type, cube=cube,
sites=sites, additional_data=additional_data)
def load(data_set, date, dl_dir=SCRATCH_PATH+'downloads/', forecast_time=0, src=None,
dl_again=False, verb=True):
'''
Load a data frame for a given date using data from either AERONET, MODIS, or the
Unified Model (metum). This will allow it to be matched and compared with other data
sets. If the necessary downloaded data exists within 'dl_dir' then that shall be
used, otherwise the data will be downloaded.
Parameters
----------
data_set: str
The data set to load. This may be 'aeronet', 'modis', 'modis_a', 'modis_t',
or 'metum'.
date: str or datetime
The date for the data that is to be loaded. Specify in format 'YYYYMMDD' for strings.
dl_dir : str, optional (Default: '/scratch/{USER}/aeroct/downloads/')
The directory in which to save downloaded data. The different data sets will be
saved within directories in this location.
forecast_time: int, optional (Default: 0)
The forecast lead time to use if a model is chosen.
src : str, optional (Default: None)