Skip to content

Commit 516f2e4

Browse files
authored
Merge pull request #214 from ghrcdaac/Lucy-trmmlisv5-fixing-HDF4-netCDF-errors
Updated process_lislip.py for zero-record data fields
2 parents 19e3544 + bf7eebb commit 516f2e4

File tree

3 files changed

+67
-44
lines changed

3 files changed

+67
-44
lines changed

mdx/granule_metadata_extractor/processing/process_lislip.py

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,16 @@ def get_variables_min_max(self, filename):
3939
def get_nc_metadata(self, filename):
4040
nc = Dataset(filename, 'r')
4141
# get time variable
42-
timevar = nc.variables['one_second_TAI93_time'][:]
42+
try:
43+
timevar = nc.variables['one_second_TAI93_time'][:]
44+
minTime, maxTime = [datetime(1993, 1, 1) + timedelta(seconds=min(timevar)),
45+
datetime(1993, 1, 1) + timedelta(seconds=max(timevar))]
46+
except: #if one_second_TAI93_time unavailable, use orbit_summary_TAI93_xxxxx
47+
timevar0 = nc.variables['orbit_summary_TAI93_start'][:]
48+
timevar1 = nc.variables['orbit_summary_TAI93_end'][:]
49+
minTime, maxTime = [datetime(1993, 1, 1) + timedelta(seconds=timevar0.item()),
50+
datetime(1993, 1, 1) + timedelta(seconds=timevar1.item())]
51+
4352
# get bounding box from bg_summary as this field is also
4453
# for background isslis data
4554
try:
@@ -51,8 +60,7 @@ def get_nc_metadata(self, filename):
5160
# for ISSLIS orbital data, set wlon as starting point
5261
# and elon as ending point of the orbit
5362
wlon, elon = [lon[0], lon[lon.size-1]]
54-
else:
55-
# set default bbox for files without valid lat, lon data
63+
else:#set default bbox
5664
slat, nlat, wlon, elon = [-35.0,35.0,-180.0,180.0]
5765
except:
5866
# if bg_summary field is not available, try viewtime field
@@ -63,72 +71,82 @@ def get_nc_metadata(self, filename):
6371
if len(lat) > 0:
6472
slat, nlat = [min(lat), max(lat)]
6573
wlon, elon = [lon[0], lon[lon.size-1]]
66-
else:
67-
# set default bbox for files without valid viewtime
74+
bounding_flag = 1
75+
else: #set default bbox
6876
slat, nlat, wlon, elon = [-35.0,35.0,-180.0,180.0]
69-
except:
70-
# if viewtime field is not available, set default bbox
77+
except: #if viewtime unavailable, set default bbox directly
7178
slat, nlat, wlon, elon = [-35.0,35.0,-180.0,180.0]
7279

7380
nc.close()
7481

75-
minTime, maxTime = [datetime(1993, 1, 1) + timedelta(seconds=min(timevar)),
76-
datetime(1993, 1, 1) + timedelta(seconds=max(timevar))]
77-
7882
return minTime, maxTime, slat, nlat, wlon, elon
7983

8084
def get_hdf_metadata(self, filename):
8185
f = HDF(filename)
8286
vs = f.vstart()
87+
8388
# from one_second vdata to extract TAI93_time data field
8489
vd = vs.attach('one_second')
85-
# get all records
86-
recs = vd[:]
87-
# extract TAI93_time which is the first field
88-
timevar = [recs[i][0] for i in range(len(recs))]
89-
minTime, maxTime = [datetime(1993, 1, 1) + timedelta(seconds=min(timevar)),
90-
datetime(1993, 1, 1) + timedelta(seconds=max(timevar))]
91-
vd.detach()
90+
if vd._nrecs > 0:
91+
#get all records from 'one_second' vdata
92+
recs = vd[:]
93+
#extract TAI93_time which is the first field
94+
timevar = [recs[i][0] for i in range(len(recs))]
95+
minTime, maxTime = [datetime(1993, 1, 1) + timedelta(seconds=min(timevar)),
96+
datetime(1993, 1, 1) + timedelta(seconds=max(timevar))]
97+
vd.detach() #detach 'one_second'
98+
else: #if 'one_second' vdata has no records
99+
vd.detach() #detach 'one_second'
100+
vd = vs.attach('orbit_summary')
101+
#extract time info from 'orbit_summary'
102+
#'orbit_summary' has one record
103+
#data fields: ['id_number', 'TAI93_start', 'UTC_start', 'GPS_start', 'TAI93_end',...]
104+
recs = vd[0]
105+
minTime, maxTime = [datetime(1993, 1, 1) + timedelta(seconds=recs[1]),
106+
datetime(1993, 1, 1) + timedelta(seconds=recs[4])]
107+
vd.detach() #detach 'orbit_summary'
108+
92109
# extract lat and lon from bg_summary field as it is available to
93110
# both science and background files
94-
try:
95-
vd = vs.attach('bg_summary')
111+
bounding_flag = 0
112+
vd = vs.attach('bg_summary')
113+
#bg_summary contains data fields 'TAI93_time', 'address', 'boresight', 'corners'
114+
if vd._nrecs > 0:
96115
recs = vd[:]
97-
boresight = [recs[i][2] for i in range(len(recs))]
116+
#extract 'boresight' which is the third field
98117
# boresight contains lat/lon pair of a point
118+
boresight = [recs[i][2] for i in range(len(recs))]
99119
lat = [boresight[i][0] for i in range(len(boresight))]
100120
lon = [boresight[i][1] for i in range(len(boresight))]
101121
# need to filter out lat = -90.0
102122
lat = [val for val in lat if val != -90.0]
103123
# some files may just have only missing value (not good file)
104-
# still assign default bbox for these files
105124
if len(lat) > 0:
106125
slat, nlat = [min(lat), max(lat)]
107126
wlon, elon = [lon[0], lon[len(lon)-1]]
108-
else:
109-
slat, nlat, wlon, elon = [-35.0, 35.0, -180.0, 180.0]
110-
vd.detach()
111-
except:
112-
vd.detach() #detach 'bg_summary' in try block
113-
# extract location info from viewtime vgroup first field (location)
114-
try:
115-
vd = vs.attach('viewtime')
116-
recs = vd[:]
117-
lat = [recs[i][0][0] for i in range(len(recs))]
118-
lon = [recs[i][0][1] for i in range(len(recs))]
119-
lat = [val for val in lat if val != -90.0]
120-
if len(lat) > 0:
121-
slat, nlat = [min(lat), max(lat)]
122-
wlon, elon = [lon[0], lon[len(lon)-1]]
123-
else:
124-
slat, nlat, wlon, elon = [-35.0, 35.0, -180.0, 180.0]
125-
vd.detach()
126-
except:
127-
vd.detach() #detach 'viewtime' in try block
128-
slat, nlat, wlon, elon = [-35.0, 35.0, -180.0, 180.0]
129-
127+
bounding_flag = 1
128+
vd.detach() #detach 'bg_summary'
129+
130+
if bounding_flag == 0:
131+
#if 'bg_summary' vdata cannot provide lat/lon info
132+
# extract location info from viewtime vgroup first field (location)
133+
vd = vs.attach('viewtime')
134+
if vd._nrecs > 0:
135+
recs = vd[:]
136+
lat = [recs[i][0][0] for i in range(len(recs))]
137+
lon = [recs[i][0][1] for i in range(len(recs))]
138+
lat = [val for val in lat if val != -90.0]
139+
if len(lat) > 0:
140+
slat, nlat = [min(lat), max(lat)]
141+
wlon, elon = [lon[0], lon[len(lon)-1]]
142+
bounding_flag = 1
143+
vd.detach() #detach 'viewtime'
144+
145+
if bounding_flag == 0:
146+
#if 'viewtime' cannot provide lat/lon info either
147+
#set default bbox
148+
slat, nlat, wlon, elon = [-35.0, 35.0, -180.0, 180.0]
130149

131-
# extract location info from viewtime vgroup first field (location)
132150
vs.end()
133151
f.close()
134152

mdx/test/test_extract_lislip_hdf_metadata.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class TestProcessLislip(TestCase):
1212
This will test if isslisv1 metadata will be extracted correctly
1313
"""
1414
granule_name = "TRMM_LIS_SC.04.1_2002.277.27868"
15+
#granule_name = "TRMM_LIS_SC.05.0_1998.194.03582.hdf" #vdata issue testing
16+
#granule_name = 'TRMM_LIS_SC.05.0_2008.060.58621'
17+
#granule_name = 'TRMM_LIS_SC.05.0_2015.098.99100'
1518
input_file = path.join(path.dirname(__file__), f"fixtures/{granule_name}")
1619
time_var_key = 'time'
1720
lon_var_key = 'lon'

mdx/test/test_extract_lislip_nc_metadata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class TestProcessLislip(TestCase):
1212
This will test if isslisv1 metadata will be extracted correctly
1313
"""
1414
granule_name = "TRMM_LIS_SC.04.1_2002.277.27868.nc"
15+
#granule_name = 'TRMM_LIS_SC.05.0_1998.194.03582.nc'
16+
#granule_name = 'TRMM_LIS_SC.05.0_2012.206.83681.nc'
1517
input_file = path.join(path.dirname(__file__), f"fixtures/{granule_name}")
1618
time_var_key = 'time'
1719
lon_var_key = 'lon'

0 commit comments

Comments
 (0)