-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolcat.py
executable file
·1458 lines (1274 loc) · 48.9 KB
/
volcat.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
# volcat.py
# A reader for VOLCAT ash data using xarray
# requires netcdf4 to be installed for the xarray netcdf reader.
import datetime
import logging
import os
import sys
from os import walk
import cartopy.crs as ccrs
import cartopy.feature as cfeat
#import monet
import numpy as np
import numpy.ma as ma
import pandas as pd
import xarray as xr
import hysplit
import hysplit_gridutil
from get_area import get_area
from helperinterface import FileNameInterface
logger = logging.getLogger(__name__)
# change log
# 2022 Nov 17 AMC updated correct_pc with better regrid support.
# 2022 Nov 17 AMC updated open_dataset
# 2022 Nov 22 AMC correct_pc need to use np.nanmin to get min values
# 2023 Feb 27 AMC moved some functions to volcat_legacy.py
# 2023 Mar 05 AMC update __pc__loop to use np.nanmax instead of np.max for heights
# 2023 Mar 05 AMC fixed bug in calculation of numd in set_array function inside correct_pc function
# 2023 Oct 13 AMC in VolcatName switched fid and feature_id.
# 2023 Oct 30 AMC created VolcatNameA class and get_name_class function to deal with Bezymianny data name format.
"""
This script contains routines that open/read VOLCAT data in xarray format,
manipulate arrays as necessary, and plots desirable variables.
-------------
Functions:
-------------
For opening files
open_dataset: opens single VOLCAT file
get_volcat_list: returns list of data-arrays with volcat data
find_volcat: finds volcat files in designated directory
get_volcat_name_df: puts parts of volcat file name in pandas dataframe
Regridding and parallax correction
correct_pc: corrects parallax
write_parallax_corrected_files: writes new files with parallax corrected lat/lon
Helper functions
__pc_loop
matchvals:
_get_time: set time dimension for VOLCAT data
_get_latlon: rename lat/lon, set coordinates of VOLCAT data
Utilities
combine_regridded : utilized in ash_inverse.py
test_volcat: tests to see if parallax corrected lat/lon values exist
bbox: finds bounding box around data - used for trimming arrays
Extracting variables
get_data: extracts desired data from large volcat file
check_names:
get_pc_latitude: uses parallax corrected latitude
get_pc_longitude: uses parallax corrected longitude
get_height: returns array of ash top height from VOLCAT
get_radius: returns array of ash effective radius from VOLCAT
get_total_mass: returns total mass in volcat file
get_mass: returns array of ash mass loading from VOLCAT
get_ashdet:
mass_sum:
get_time:
get_atherr: returns array of ash top height error from VOLCAT
Classes:
VolcatName
compare: compares volcat file names - shows difference
parse: parses name into components
create_name: in progress
------------
Functions moved to volcat_legacy.py
open_dataset2: opens single volcat file in Reventador format
open_mfdataset: opens multiple VOLCAT files
open_hdf: opens single NRT HDF VOLCAT file
create_netcdf: creates netcdf of import variables from NRT HDF
regrid_volcat
regrid_volcat_xesmf
regrid_volcat2
write_regridded_files: writes regridded files
_make2d_latlon
average_volcat :
average_volcat_new :
Improvements to be done:
the _get_time function which adds the time to the output data array was not working
on some of the reprocessed data for Popocatepetl eruption.
2023 18 July took expand_dims out of _get_time function and moved it to get_data function.
"""
def check_vals(idate, ghash, dset):
slist = []
slist.append(dset.time_bounds)
slist.append(dset.mean_feature_time)
slist.append(dset.full_image_start_time)
slist.append(dset.full_image_end_time)
slist.append(dset.first_detection_full_image_start_time)
slist.append(dset.first_detection_mean_feature_time)
slist.append(dset.attrs["event_observation_time"])
names = [
"time_bounds",
"mean_feature_time",
"full image start time",
"fill image end time",
"first detection full image start time",
"first detection mean feature time",
]
names.append("event_observation_time")
for iii, sval in enumerate(slist):
print("***", names[iii], type(sval))
if isinstance(sval, str):
dstr = "%Y-%m-%dT%H:%M:%SZ"
time = datetime.datetime.strptime(sval, dstr)
print(time)
elif isinstance(sval.values, (list, np.ndarray)):
for val in sval.values:
print(val, type(val))
else:
print(sval.values, type(sval.values))
if idate == pd.to_datetime(sval.values):
print("TRUE TRUE TRUE")
def check_name(fnn):
fn = fnn.split("/")[-1]
vn = get_name_class(fn)
# for key in vn.vhash.keys():
# print(key, vn.vhash[key])
edate = vn.vhash["event date"]
idate = vn.vhash["observation_date"]
return edate, idate
def check_global_attrs(dset):
ghash = {}
for attr in dset.attrs:
if "time" in attr:
# print(attr, dset.attrs[attr])
ghash[attr] = dset.attrs[attr]
return ghash
def match_times(ghash, edate, idate):
dfmt = "%Y-%m-%dT%H:%M%S.0z"
for key in ghash:
if ghash[key] == edate.strftime(dfmt):
print("match", key, "edate", edate)
if ghash[key] == idate.strftime(dfmt):
print("match", key, "idate", idate)
if idate != edate:
print("observation_date {}".format(idate))
print("event date {}".format(edate))
print(idate)
print(edate)
# print(edate, idate, type(ghash[key]), key)
def check_times(fname):
"""
check the times in the filename against times
in the file attributes.
"""
dset = xr.open_dataset(fname)
edate, idate = check_name(fname)
ghash = check_global_attrs(dset)
match_times(ghash, edate, idate)
check_vals(idate, ghash, dset)
def open_dataset(
fname,
gridspace=None,
correct_parallax=False,
mask_and_scale=True,
decode_times=True,
):
"""
Opens single VOLCAT file
gridspace: only necessary if doing parallax correction
mask_and_scale : needs to be set to True for Bezymianny data.
decode_times : needs to be True for parallax corrected files and some of hdf data.
"""
# 03/07/2021 The Bezy height data has a fill value of -1,
# scale_factor of 0.01 and offset of 0.
# The scale factor needs to be applied to get output in km.
# ash_mass_loading has no scale_factor of offset and fill value is -999.
if "pc.nc" in fname or "rg.nc" in fname:
dset = xr.open_dataset(fname, mask_and_scale=mask_and_scale, decode_times=True,engine='netcdf4')
return dset
else:
dset = xr.open_dataset(
fname, mask_and_scale=mask_and_scale, decode_times=decode_times
)
# not needed for new Bezy data.
if "Dim1" in dset.dims.keys() and "Dim2" in dset.dims.keys():
dset = dset.rename({"Dim1": "y", "Dim0": "x"})
# if "some_vars.nc" in fname:
# pass
# elif "Karymsky" in fname:
# pass
# else:
# use parallax corrected if available and flag is set.
dset = _get_latlon(dset, "latitude", "longitude")
dset = _get_time(dset)
# try:
# dset = _get_time(dset)
# except Exception as eee:
# print('_get_time error', eee)
if "pc_latitude" in dset.data_vars and correct_parallax:
if not gridspace:
dset = correct_pc(dset)
else:
dset = correct_pc(dset, gridspace=gridspace)
dset.attrs.update({"parallax corrected coordinates": "True"})
elif "pc_latitude" not in dset.data_vars and correct_parallax:
print("WARNING: cannot correct parallax. Data not found in file")
dset.attrs.update({"parallax corrected coordinates": "False"})
else:
dset.attrs.update({"parallax corrected coordinates": "False"})
return dset
def flist2eventdf(flist, inphash):
"""
create a dataframe from list of volcat filenames
inphash contains additional information should be in following format
VOLCANO_NAME : str
"""
# example usage in Raikoke2023Volcat
vlist = []
# change all keys to upper case
inphash = {k.upper(): val for k, val in inphash.items()}
for fle in flist:
try:
temp = get_name_class(fle)
except Exception as eee:
continue
vlist.append(temp.vhash)
vframe = pd.DataFrame.from_dict(vlist)
cols = vframe.columns
# rename columns which need to be renamed.
cols = [x if x != "satellite platform" else "sensor_name" for x in cols]
cols = [x if x != "idate" else "observation_date" for x in cols]
cols = [x if x != "filename" else "event_file" for x in cols]
cols = [x if x != "WMO satellite id" else "SENSOR_WMO_ID_INITIAL" for x in cols]
cols = [x if x != "FEATURE_ID" else "feature_id" for x in cols]
vframe.columns = cols
checklist = [
"VOLCANO_NAME",
"VOLCANO_LAT",
"VOLCANO_LON",
"VOLCANO_REGION",
"VOLCANO_ELEVATION",
]
for key in checklist:
if key in inphash.keys():
print('A adding {} : {}'.format(key,inphash[key]))
vframe[key.lower()] = inphash[key]
if key.lower() in inphash.keys():
print('B adding {}'.format(key))
vframe[key.lower()] = inphash[key.lower()]
return vframe
def get_volcat_name_df(tdir, daterange=None, vid=None, fid=None, include_last=False):
"""
Returns dataframe with columns being the information in the vhash
dictionary of the VolcatName class. This is all the information collected from the filename.
"""
tlist = find_volcat(tdir, vid=None, daterange=None, return_val=2)
# vlist is a list of dictionaries with information from the name.
vlist = [x.vhash for x in tlist]
if not vlist:
return pd.DataFrame()
temp = pd.DataFrame(vlist)
if isinstance(daterange, (list, np.ndarray)):
temp = temp[temp["observation_date"] >= daterange[0]]
if include_last:
temp = temp[temp["observation_date"] <= daterange[1]]
else:
temp = temp[temp["observation_date"] < daterange[1]]
if vid:
temp = temp[temp["event vid"] == vid]
if fid:
temp = temp[temp["fid"] == fid]
if "event vid" in temp.columns and "edate" in temp.columns:
if "fid" in temp.columns:
temp = temp.sort_values(["event vid", "fid", "edate"], axis=0)
else:
temp = temp.sort_values(["event vid", "edate"], axis=0)
return temp
def get_volcat_list(
tdir,
daterange=None,
vid=None,
fid=None,
fdate=None,
flist=None,
return_val=2,
correct_parallax=True,
mask_and_scale=True,
decode_times=True,
verbose=False,
include_last=True,
):
"""
returns list of data-arrays with volcat data.
Inputs:
tdir: string - directory of volcat files
daterange: datetime object - [datetime0, datetime1] or none
vid: string - volcano ID
fid: feature ID
fdate: Feature datetime - edate in tframe (datetime object or datetime64)
flist: list of filenames
return_val: integer (1,2,3) - see find_volcat() for explanation
correct_parallax: boolean
mask_and_scale: boolean
decode_times: boolean
verbose: boolean
include_last: boolean
Outputs:
das: list of datasets
"""
if isinstance(flist, (list, np.ndarray)):
filenames = flist
else:
tframe = get_volcat_name_df(
tdir, vid=vid, fid=fid, daterange=daterange, include_last=include_last
)
if fdate:
eventd = pd.to_datetime(fdate).to_datetime64()
filenames = tframe.loc[tframe["edate"] == eventd, "filename"].tolist()
else:
filenames = tframe.filename.values
das = []
for nnn, iii in enumerate(filenames):
if not os.path.isfile(os.path.join(tdir, iii)):
logger.warning("file not found, skipping file {}".format(iii))
print("file not found, skipping file {}".format(iii))
continue
if verbose:
logger.info("working on {} {} out of {}".format(nnn, iii, len(filenames)))
print("working on {} {} out of {}".format(nnn, iii, len(filenames)))
# opens volcat files using volcat.open_dataset
if not "_pc" in iii:
if verbose:
print("opening", iii, correct_parallax, mask_and_scale, decode_times)
das.append(
open_dataset(
os.path.join(tdir, iii),
correct_parallax=correct_parallax,
mask_and_scale=mask_and_scale,
decode_times=decode_times,
)
)
else:
das.append(xr.open_dataset(os.path.join(tdir, iii),engine='netcdf4'))
return das
def write_parallax_corrected_files(
tdir,
wdir,
vid=None,
daterange=None,
verbose=False,
flist=None,
gridspace=None,
tag="pc",
):
"""
***If flist is not specified, this does not work. There are folders in the tdir and they
cause a problem with the function. Flist must not include directories, just file names***
tdir : str : location of volcat files.
wdir : str : location to write new files
vid : volcano id : if None will find all
daterange : [datetime, datetime] : if None will find all.
verbose: boolean
flist: list of files? ***NEED TO SPECIFY FILE LIST***
gridspace: float : grid size of pc array
tag: used to create filename of new file.
creates netcdf files with parallax corrected values.
files have same name with _{tag}.nc added to the end.
Current convention is to use tag=pc.
These will be needed for input into MET.
Currently no overwrite option exists in this function. If the file
already exists, then this function returns a message to that effect and
does not overwrite the file.
"""
logger.info("volcat write_parallax_corrected_files function")
anum = 0
newnamelist = []
if isinstance(flist, (list, np.ndarray)):
if verbose:
print("Using filenamse from list")
vlist = flist
else:
if verbose:
print("Finding filenames")
vlist = find_volcat(tdir, vid, daterange, verbose=verbose, return_val=2)
for iii, val in enumerate(vlist):
if verbose:
print("working on {}".format(val))
if isinstance(val, str):
fname = val
else:
fname = val.fname
new_fname = fname.replace(".nc", "_{}.nc".format(tag))
newname = os.path.join(wdir, new_fname)
# print('wdir {}'.format(newname))
if os.path.isfile(os.path.join(wdir, new_fname)):
anum += 1
if verbose:
print(
"Netcdf file exists {} in directory {} cannot write ".format(
new_fname, wdir
)
)
else:
if verbose:
print("writing {} to {}".format(new_fname, wdir))
dset = open_dataset(
os.path.join(tdir, fname),
gridspace=gridspace,
decode_times=True,
correct_parallax=True,
)
newname = os.path.join(wdir, new_fname)
# print('wdir {}'.format(newname))
dset.to_netcdf(newname)
if not os.path.isfile(os.path.join(wdir, new_fname)):
logger.warning(
"Warning: file did not write {} {}".format(wdir, new_fname)
)
else:
logger.info("file did write {} {}".format(wdir, new_fname))
# print('Number of files which were already written {}'.format(anum))
def find_volcat(
tdir, vid=None, daterange=None, return_val=2, verbose=False, include_last=False
):
##NOT WORKING FOR NISHINOSHIMA DATA##
"""
Locates files in tdir which follow the volcat naming
convention as defined in VolcatName class.
If a daterange is defined will return only files
Inputs:
tdir : string - volcat files directory
vid: string - volcano id
daterange : [datetime, datetime] or None
include_last : boolean
True - includes volcat data with date = daterange[1]
False - only include data with date < daterange[1]
return_val : integer
1 - returns dictionary
2- returns list of VolcatName objects.
3 - returns list of filenames
Returns:
1 - returns dictionary. key is date. values is VolcatName object.
2 - returns list of VolcatName objects.
3 - returns list of filenames
"""
import sys
vhash = {} # dictionary
nflist = [] # list of filenames
vnlist = [] # list of filenames
if not os.path.isdir(tdir):
print("directory not valid {}".format(tdir))
for fln in os.listdir(tdir):
try:
vn = get_name_class(fln)
except:
if verbose:
print("Not VOLCAT filename {}".format(fln))
continue
if daterange and include_last:
if vn.image_date < daterange[0] or vn.image_date > daterange[1]:
if verbose:
print(
"date not in range", vn.image_date, daterange[0], daterange[1]
)
continue
elif daterange and not include_last:
if vn.image_date < daterange[0] or vn.image_date >= daterange[1]:
if verbose:
print(
"date not in range", vn.image_date, daterange[0], daterange[1]
)
continue
if vid and vn.vhash["event vid"] != vid:
continue
if return_val == 1:
if vn.image_date not in vhash.keys():
vhash[vn.image_date] = vn
else:
print("two files with same date")
print(vhash[vn.image_date].compare(vn))
elif return_val == 2:
vnlist.append(vn)
elif return_val == 3:
nflist.append(fln)
if return_val == 1:
return vhash
elif return_val == 2:
return vnlist
elif return_val == 3:
return nflist
def test_volcat(tdir, daterange=None, verbose=True):
"""
checks the pc_latitude field for values greater than 0.
"""
vnlist = find_volcat(tdir, daterange, verbose)
for key in vnlist.keys():
vname = vnlist[key].fname
dset = open_dataset(os.path.join(tdir, vname), pc_correct=False)
if np.nanmax(dset.pc_latitude) > 0:
print("passed")
else:
print("failed")
def get_name_class(fname):
original_name = fname
if "/" in fname:
temp = fname.split("/")
fname = temp[-1]
# if full_disk in filename replace with fulldisk because _ is used as separator
fname = fname.replace("Full_Disk", "FullDisk")
fname = fname.replace("FULL_DISK", "FullDisk")
temp = fname.split("_")
# current name style with two feature id tags.
if temp[5][0] == 'g': return VolcatName(fname,original_name)
# old name style with only one feature id tags. Bezymianny 2020 data is in this format.
elif temp[5][0] == 'v': return VolcatNameA(fname,original_name)
class VolcatName(FileNameInterface):
"""
12/18/2020 works with 'new' data format.
parse the volcat name to get information.
attributes:
self.fname name of file
self.date date associated with file
self.vhash is a dictionary which contains info
gleaned from the naming convention.
methods:
compare: returns what is different between two file names.
"""
def __init__(self, fname, original_name=None):
# if full directory path is input then just get the filename
self.fname = fname
if isinstance(fname, str):
if "/" in fname:
temp = fname.split("/")
self.fname = temp[-1]
self.vhash = {}
self.date = None
self.image_date = None
self.event_date = None
self.image_dtfmt = "s%Y%j_%H%M%S"
self.event_dtfmt = "b%Y%j_%H%M%S"
self.make_keylist()
self.make_datekeys()
self.pc_corrected = False
# parse only if a string is given.
if isinstance(fname, str):
self.parse(self.fname)
if isinstance(original_name,str):
self.vhash['filename']=original_name
#print('using original name')
else:
self.vhash["filename"] = fname
def make_datekeys(self):
self.datekeys = [3, 4, 10, 11]
def make_keylist(self):
self.keylist = ["algorithm name"]
self.keylist.append("satellite platform")
self.keylist.append("event scanning strategy")
self.keylist.append("observation_date") # should be image date (check)
self.keylist.append("image time")
self.keylist.append("feature_id")
self.keylist.append("event vid")
self.keylist.append("description")
self.keylist.append("WMO satellite id")
self.keylist.append("image scanning strategy")
self.keylist.append("event_date") # should be event date (check)
self.keylist.append("event_time")
self.keylist.append("original_feature_id")
def __lt__(self, other):
"""
sort by
volcano id first.
event date
image date
feature id if it exists.
"""
if self.vhash["event vid"] < other.vhash["event vid"]:
return True
if "fid" in self.vhash.keys() and "fid" in other.vhash.keys():
if self.vhash["fid"] < other.vhash["fid"]:
return True
if self.event_date < other.event_date:
return True
if self.image_date < other.image_date:
return True
sortlist = [
"feature id",
"image scanning strategy",
"WMO satellite id",
"description",
"event scanning strategy",
"satellite platform",
"algorithm name",
]
for key in sortlist:
if key in other.vhash.keys() and key in self.vhash.keys():
if self.vhash[key] < other.vhash[key]:
return True
def compare(self, other):
"""
other is another VolcatName object.
Returns
dictionary of information which is different.
values is a tuple of (other value, self value).
"""
diffhash = {}
for key in self.keylist:
if key in other.vhash.keys() and key in self.vhash.keys():
if other.vhash[key] != self.vhash[key]:
diffhash[key] = (other.vhash[key], self.vhash[key])
return diffhash
def __str__(self):
# 2023 14 Jan (amc) make sure keys are in the dictionary.
keys = self.vhash.keys()
keylist = [x for x in self.keylist if x in keys]
val = [str(self.vhash[x]) for x in keylist]
return str.join("_", val)
@staticmethod
def split_name(fname):
# if full_disk in filename replace with fulldisk because _ is used as separator
fname = fname.replace("Full_Disk", "FullDisk")
fname = fname.replace("FULL_DISK", "FullDisk")
temp = fname.split("_")
return temp
def parse(self, fname):
temp = self.split_name(fname)
if "pc" in temp[-1]:
self.pc_corrected = True
jjj = 0
for iii, key in enumerate(self.keylist):
val = temp[jjj]
# nishinoshima files have a g00? code before the volcano id.
if key == "fid":
if val[0] == "g":
self.vhash[key] = val
else:
continue
self.vhash[key] = val
jjj += 1
# Image date marks date of the data collection
dk = self.datekeys
if isinstance(dk[0], int) and isinstance(dk[1], int):
dstr = "{}_{}".format(
self.vhash[self.keylist[dk[0]]], self.vhash[self.keylist[dk[1]]]
)
self.image_date = datetime.datetime.strptime(dstr, self.image_dtfmt)
# Event date is start of event
if isinstance(dk[2], int) and isinstance(dk[3], int):
dstr = "{}_{}".format(
self.vhash[self.keylist[dk[2]]], self.vhash[self.keylist[dk[3]]]
)
self.event_date = datetime.datetime.strptime(dstr, self.event_dtfmt)
self.vhash[self.keylist[dk[3]]] = self.vhash[self.keylist[dk[3]]].replace(
".nc", ""
)
# this is the date associated with the data
self.vhash["observation_date"] = self.image_date
# this date may be the same as the image date or earlier
self.vhash["event date"] = self.event_date
self.date = self.image_date
return self.vhash
@property
def image_date_str(self):
return self.image_date.strftime(self.image_dtfmt)
def make_filename(self):
"""
To do: returns filename given some inputs.
"""
return -1
class VolcatNameA(VolcatName):
# for the Bezymianny data and some older data the first feature id is not there.
def make_datekeys(self):
self.datekeys = [3, 4, 9, 10]
def make_keylist(self):
self.keylist = ["algorithm name"]
self.keylist.append("satellite platform")
self.keylist.append("event scanning strategy")
self.keylist.append("observation_date") # should be image date (check)
self.keylist.append("image time")
self.keylist.append("event vid")
self.keylist.append("description")
self.keylist.append("WMO satellite id")
self.keylist.append("image scanning strategy")
self.keylist.append("event_date") # should be event date (check)
self.keylist.append("event_time")
self.keylist.append("feature_id")
def bbox(darray, fillvalue):
"""Returns bounding box around data
Input: Must be dataarray
Outupt: Lower left corner, upper right corner of bounding box
around data.
if fillvalue is None then assume Nan's.
"""
try:
# arr = darray[0, :, :].values
arr = darray[0, :, :].values
except:
print("bbox arr failed", darray)
if fillvalue:
a = np.where(arr != fillvalue)
else:
a = np.where(~np.isnan(arr))
if np.nanmin(a[0]) != 0.0 and np.nanmin(a[1]) != 0.0:
bbox = (
[np.nanmin(a[0] - 3), np.nanmin(a[1]) - 3],
[np.nanmax(a[0] + 3), np.nanmax(a[1]) + 3],
)
else:
bbox = ([np.nanmin(a[0]), np.nanmin(a[1])], [np.nanmax(a[0]), np.nanmax(a[1])])
return bbox
def _get_latlon(dset, name1="latitude", name2="longitude"):
dset = dset.set_coords([name1, name2])
return dset
def _get_time(dset):
# June 2023 There was some issue with this for the reprocessed Popo data.
import pandas as pd
# temp2 = dset.full_image_start_time.values
# scan start and end times
# temp2 = dset.time_bounds.values[0]
# full image stat. this corresponds to the idate.
# temp2 = dset.full_image_start_time.values
temp2 = dset.attrs["event_observation_time"]
# temp3 = dset.attrs["time_coverage_end"]
# if temp1 != temp2:
# logger.warning('Different times in volcat {} {}'.format(temp1,temp2,temp3))
dstr = "%Y-%m-%dT%H:%M:%SZ"
# time string sometimes has seconds as a decimal which is not recognized by strptime.
# Remove the decimal part.
iii = str.find(temp2, ".")
temp2 = temp2[0:iii] + "Z"
time = datetime.datetime.strptime(temp2, dstr)
dset["time"] = time
dset = dset.set_coords(["time"])
# expand_dims has been moved to the get_data function.
# it was failing here possibly because of the structure of the data.
# dset = dset.expand_dims(dim="time")
return dset
# no longer valid
# def _get_time2(dset)
# import pandas as pd
#
# date = "20" + str(dset.attrs["Image_Date"])[1:]
# time1 = str(dset.attrs["Image_Time"])
# if len(time1) == 5:
# time1 = "0" + str(dset.attrs["Image_Time"])
# time = pd.to_datetime(date + time1, format="%Y%j%H%M%S", errors="ignore")
# dset["time"] = time
# dset = dset.expand_dims(dim="time")
# dset = dset.set_coords(["time"])
# return dset
# Extracting variables
def get_data(dset, vname, clip=False):
# 18 July 2023 - took expand_dims out of _get_time function and placed it here.
gen = dset.data_vars[vname]
atvals = gen.attrs
fillvalue = None
if not "time" in gen.dims:
gen = gen.expand_dims(dim="time")
if "_FillValue" in gen.attrs:
fillvalue = gen._FillValue
gen = gen.where(gen != fillvalue)
fillvalue = None
if clip and gen.time.shape[0] == 1:
# do not clip if more than one time period.
# bounding box will only be created for first time period
# and may clip out relevant values later.
status = True
try:
box = bbox(gen, fillvalue)
except:
print("volcat get_data bbox for clipping failed", vname)
status = False
if status:
gen = gen[:, box[0][0] : box[1][0], box[0][1] : box[1][1]]
if "_FillValue" in gen.attrs:
gen = gen.where(gen != fillvalue)
else:
gen = gen.where(gen)
# applies scale_factor and offset if they are in the attributes.
if "scale_factor" in gen.attrs:
gen = gen * gen.attrs["scale_factor"]
if "offset" in gen.attrs:
gen = gen + gen.attrs["offset"]
if "add_offset" in gen.attrs:
gen = gen + gen.attrs["add_offset"]
# keep relevant attributes.
new_attr = {}
for key in atvals.keys():
if key not in ["_FillValue", "add_offset", "offset", "scale_factor"]:
new_attr[key] = atvals[key]
gen.attrs = new_attr
return gen
def check_names(dset, vname, checklist, clip=True):
if vname:
return get_data(dset, vname, clip=clip)
for val in checklist:
if val in dset.data_vars:
return get_data(dset, val, clip=clip)
return xr.DataArray()
def check_pc(dset):
checklist = ["pc_latitude", "pc_longitude"]
count = 0
for val in checklist:
if val in dset.data_vars:
count += 1
if count == 2:
return True
else:
return False
def get_pc_latitude(dset, vname=None, clip=True):
"""Returns array with retrieved height of the highest layer of ash."""
"""Default units are km above sea-level"""
checklist = ["pc_latitude"]
return check_names(dset, vname, checklist, clip=clip)
def get_pc_longitude(dset, vname=None, clip=True):
"""Returns array with retrieved height of the highest layer of ash."""
"""Default units are km above sea-level"""
checklist = ["pc_longitude"]
return check_names(dset, vname, checklist, clip=clip)
def get_height(dset, vname=None, clip=True):
"""Returns array with retrieved height of the highest layer of ash.
Default units are km above sea-level"""
checklist = ["ash_cth", "ash_cloud_height"]
return check_names(dset, vname, checklist, clip=clip)
def get_radius(dset, vname=None, clip=True):
"""Returns 2d array of ash effective radius
Default units are micrometer"""
checklist = ["ash_r_eff", "effective_radius_of_ash"]
return check_names(dset, vname, checklist, clip=clip)
def check_total_mass(dset):
try:
mass = get_mass(dset)
except:
mass = dset
area = get_area(mass)
# area returned in km2.
# mass in g/m2
# 1e6 to convert are to m2
# 1e-12 to convert g to Tg
masstot = mass * area * 1e-6
masstot = masstot.sum().values
# return unit is in Tg
return masstot
def get_polygon(dset):
lat = dset.pc_feature_polygon_latitude
lon = dset.pc_feature_polygon_longitude
return lon, lat
def get_total_mass(dset):
# unit is in Tg.
"""Units are in Tg"""
return dset.ash_mass_loading_total_mass.values
def get_mass(dset, vname=None, clip=True):
"""Returns 2d array of ash mass loading
Default units are grams / meter^2"""
checklist = ["ash_mass", "ash_mass_loading"]
return check_names(dset, vname, checklist, clip=clip)
def get_ashdet(dset, vname=None, clip=True):
"""Returns 2d array of detected ash
Values > 0.0 = detected ash
Values < 0.0 = no detected ash
Can be used to determine if ash was detected, but ash mass or ash height was not"""
checklist = ["ash_spectral_signature_strength"]
return check_names(dset, vname, checklist, clip=clip)
def mass_sum(dset):
mass = get_mass(dset)
mass2 = mass.where(mass > 0.0, 0.0).values
mass_sum = np.sum(mass2)
return mass_sum
def get_time(dset):
time = dset.time_coverage_start
return time
def get_atherr(dset):
"""Returns array with uncertainty in ash top height from VOLCAT."""
"""Default units are km above sea-level"""
height_err = dset.ash_cth_uncertainty
height_err = height_err.where(height_err != height_err._FillValue, drop=True)
return height_err