Skip to content

Commit 70a595f

Browse files
authored
feat: add option to keep file object open (#90)
1 parent cb43fd0 commit 70a595f

File tree

6 files changed

+103
-41
lines changed

6 files changed

+103
-41
lines changed

icesat2_toolkit/io/ATL03.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
h5py = import_dependency('h5py')
5050

5151
# PURPOSE: read ICESat-2 ATL03 HDF5 data files
52-
def read_granule(FILENAME, ATTRIBUTES=False, **kwargs):
52+
def read_granule(FILENAME, ATTRIBUTES=False, KEEP=False, **kwargs):
5353
"""
5454
Reads ICESat-2 ATL03 Global Geolocated Photons data files
5555
@@ -59,6 +59,8 @@ def read_granule(FILENAME, ATTRIBUTES=False, **kwargs):
5959
full path to ATL03 file
6060
ATTRIBUTES: bool, default False
6161
read file, group and variable attributes
62+
KEEP: bool, default False
63+
keep file object open
6264
6365
Returns
6466
-------
@@ -270,12 +272,14 @@ def read_granule(FILENAME, ATTRIBUTES=False, **kwargs):
270272
IS2_atl03_attrs[att_name] = att_val
271273

272274
# Closing the HDF5 file
273-
fileID.close()
275+
if not KEEP:
276+
fileID.close()
274277
# Return the datasets and variables
275278
return (IS2_atl03_mds, IS2_atl03_attrs, IS2_atl03_beams)
276279

277280
# PURPOSE: read ICESat-2 ATL09 HDF5 data file for specific variables
278-
def interpolate_ATL09(FILENAME, pfl, dtime, ATTRIBUTES=True, **kwargs):
281+
def interpolate_ATL09(FILENAME, pfl, dtime,
282+
ATTRIBUTES=True, KEEP=False, **kwargs):
279283
"""
280284
Reads ICESat-2 ATL09 Atmospheric Characteristics data files
281285
and interpolates a subset of variables to ATL03 segment lengths
@@ -288,8 +292,10 @@ def interpolate_ATL09(FILENAME, pfl, dtime, ATTRIBUTES=True, **kwargs):
288292
profile for a given beam
289293
dtime: float
290294
ATL03 reference photon delta_time
291-
ATTRIBUTES: bool, default False
295+
ATTRIBUTES: bool, default True
292296
read file, group and variable attributes
297+
KEEP: bool, default False
298+
keep file object open
293299
294300
Returns
295301
-------
@@ -349,12 +355,13 @@ def interpolate_ATL09(FILENAME, pfl, dtime, ATTRIBUTES=True, **kwargs):
349355
IS2_atl09_attrs[att_name] = att_val
350356

351357
# Closing the HDF5 file
352-
fileID.close()
358+
if not KEEP:
359+
fileID.close()
353360
# Return the datasets and variables
354361
return (IS2_atl09_mds, IS2_atl09_attrs)
355362

356363
# PURPOSE: find valid beam groups within ICESat-2 ATL03 HDF5 data files
357-
def find_beams(FILENAME, **kwargs):
364+
def find_beams(FILENAME, KEEP=False ,**kwargs):
358365
"""
359366
Find valid beam groups within ICESat-2 ATL03 Global Geolocated Photons
360367
data files
@@ -363,6 +370,8 @@ def find_beams(FILENAME, **kwargs):
363370
----------
364371
FILENAME: str
365372
full path to ATL03 file
373+
KEEP: bool, default False
374+
keep file object open
366375
367376
Returns
368377
-------
@@ -391,12 +400,13 @@ def find_beams(FILENAME, **kwargs):
391400
else:
392401
IS2_atl03_beams.append(gtx)
393402
# Closing the HDF5 file
394-
fileID.close()
403+
if not KEEP:
404+
fileID.close()
395405
# return the list of beams
396406
return IS2_atl03_beams
397407

398408
# PURPOSE: read ICESat-2 ATL03 HDF5 data files for main level variables
399-
def read_main(FILENAME, ATTRIBUTES=False, **kwargs):
409+
def read_main(FILENAME, ATTRIBUTES=False, KEEP=False, **kwargs):
400410
"""
401411
Reads ICESat-2 ATL03 Global Geolocated Photons data files
402412
for only the main-level variables and not the beam-level data
@@ -407,6 +417,8 @@ def read_main(FILENAME, ATTRIBUTES=False, **kwargs):
407417
full path to ATL03 file
408418
ATTRIBUTES: bool, default False
409419
read file, group and variable attributes
420+
KEEP: bool, default False
421+
keep file object open
410422
411423
Returns
412424
-------
@@ -558,12 +570,13 @@ def read_main(FILENAME, ATTRIBUTES=False, **kwargs):
558570
IS2_atl03_attrs[att_name] = att_val
559571

560572
# Closing the HDF5 file
561-
fileID.close()
573+
if not KEEP:
574+
fileID.close()
562575
# Return the datasets and variables
563576
return (IS2_atl03_mds, IS2_atl03_attrs, IS2_atl03_beams)
564577

565578
# PURPOSE: read ICESat-2 ATL03 HDF5 data files for beam variables
566-
def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
579+
def read_beam(FILENAME, gtx, ATTRIBUTES=False, KEEP=False, **kwargs):
567580
"""
568581
Reads ICESat-2 ATL03 Global Geolocated Photons data files
569582
for a specific beam
@@ -583,6 +596,8 @@ def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
583596
- ``'gt3r'``
584597
ATTRIBUTES: bool, default False
585598
read file, group and variable attributes
599+
KEEP: bool, default False
600+
keep file object open
586601
587602
Returns
588603
-------
@@ -666,6 +681,7 @@ def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
666681
IS2_atl03_attrs['geophys_corr'][key][att_name]=att_val
667682

668683
# Closing the HDF5 file
669-
fileID.close()
684+
if not KEEP:
685+
fileID.close()
670686
# Return the datasets and variables
671687
return (IS2_atl03_mds, IS2_atl03_attrs)

icesat2_toolkit/io/ATL06.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
# PURPOSE: read ICESat-2 ATL06 HDF5 data files
5050
def read_granule(FILENAME, ATTRIBUTES=False, HISTOGRAM=False,
51-
QUALITY=False, **kwargs):
51+
QUALITY=False, KEEP=False, **kwargs):
5252
"""
5353
Reads ICESat-2 ATL06 (Land Ice Along-Track Height Product) data files
5454
@@ -62,6 +62,8 @@ def read_granule(FILENAME, ATTRIBUTES=False, HISTOGRAM=False,
6262
read ATL06 residual_histogram variables
6363
QUALITY: bool, default False
6464
read ATL06 segment_quality variables
65+
KEEP: bool, default False
66+
keep file object open
6567
6668
Returns
6769
-------
@@ -266,12 +268,13 @@ def read_granule(FILENAME, ATTRIBUTES=False, HISTOGRAM=False,
266268
IS2_atl06_attrs['quality_assessment'][key][k][att_name]= att_val
267269

268270
# Closing the HDF5 file
269-
fileID.close()
271+
if not KEEP:
272+
fileID.close()
270273
# Return the datasets and variables
271274
return (IS2_atl06_mds, IS2_atl06_attrs, IS2_atl06_beams)
272275

273276
# PURPOSE: find valid beam groups within ICESat-2 ATL06 HDF5 data files
274-
def find_beams(FILENAME, **kwargs):
277+
def find_beams(FILENAME, KEEP=False, **kwargs):
275278
"""
276279
Find valid beam groups within ICESat-2 ATL06 (Land Ice Along-Track
277280
Height Product) data files
@@ -280,6 +283,8 @@ def find_beams(FILENAME, **kwargs):
280283
----------
281284
FILENAME: str
282285
full path to ATL06 file
286+
KEEP: bool, default False
287+
keep file object open
283288
284289
Returns
285290
-------
@@ -306,12 +311,13 @@ def find_beams(FILENAME, **kwargs):
306311
else:
307312
IS2_atl06_beams.append(gtx)
308313
# Closing the HDF5 file
309-
fileID.close()
314+
if not KEEP:
315+
fileID.close()
310316
# return the list of beams
311317
return IS2_atl06_beams
312318

313319
# PURPOSE: read ICESat-2 ATL06 HDF5 data files for beam variables
314-
def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
320+
def read_beam(FILENAME, gtx, ATTRIBUTES=False, KEEP=False, **kwargs):
315321
"""
316322
Reads ICESat-2 ATL06 (Land Ice Along-Track Height Product) data files
317323
for a specific beam
@@ -335,6 +341,8 @@ def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
335341
read ATL06 residual_histogram variables
336342
QUALITY: bool, default False
337343
read ATL06 segment_quality variables
344+
KEEP: bool, default False
345+
keep file object open
338346
339347
Returns
340348
-------
@@ -404,6 +412,7 @@ def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
404412
IS2_atl06_attrs[gtx]['land_ice_segments'][key][k][att_name] = att_val
405413

406414
# Closing the HDF5 file
407-
fileID.close()
415+
if not KEEP:
416+
fileID.close()
408417
# Return the datasets and variables
409418
return (IS2_atl06_mds, IS2_atl06_attrs)

icesat2_toolkit/io/ATL07.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
h5py = import_dependency('h5py')
4141

4242
# PURPOSE: read ICESat-2 ATL07 HDF5 data files
43-
def read_granule(FILENAME, ATTRIBUTES=False, **kwargs):
43+
def read_granule(FILENAME, ATTRIBUTES=False, KEEP=False, **kwargs):
4444
"""
4545
Reads ICESat-2 ATL07 (Sea Ice Height) data files
4646
@@ -50,6 +50,8 @@ def read_granule(FILENAME, ATTRIBUTES=False, **kwargs):
5050
full path to ATL07 file
5151
ATTRIBUTES: bool, default False
5252
read HDF5 attributes for groups and variables
53+
KEEP: bool, default False
54+
keep file object open
5355
5456
Returns
5557
-------
@@ -210,19 +212,22 @@ def read_granule(FILENAME, ATTRIBUTES=False, **kwargs):
210212
IS2_atl07_attrs['quality_assessment'][key][k][att_name]= att_val
211213

212214
# Closing the HDF5 file
213-
fileID.close()
215+
if not KEEP:
216+
fileID.close()
214217
# Return the datasets and variables
215218
return (IS2_atl07_mds, IS2_atl07_attrs, IS2_atl07_beams)
216219

217220
# PURPOSE: find valid beam groups within ICESat-2 ATL07 HDF5 data files
218-
def find_beams(FILENAME, **kwargs):
221+
def find_beams(FILENAME, KEEP=False, **kwargs):
219222
"""
220223
Find valid beam groups within ICESat-2 ATL07 (Sea Ice Height) data files
221224
222225
Parameters
223226
----------
224227
FILENAME: str
225228
full path to ATL07 file
229+
KEEP: bool, default False
230+
keep file object open
226231
227232
Returns
228233
-------
@@ -249,12 +254,13 @@ def find_beams(FILENAME, **kwargs):
249254
else:
250255
IS2_atl07_beams.append(gtx)
251256
# Closing the HDF5 file
252-
fileID.close()
257+
if not KEEP:
258+
fileID.close()
253259
# return the list of beams
254260
return IS2_atl07_beams
255261

256262
# PURPOSE: read ICESat-2 ATL07 HDF5 data files for beam variables
257-
def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
263+
def read_beam(FILENAME, gtx, ATTRIBUTES=False, KEEP=False, **kwargs):
258264
"""
259265
Reads ICESat-2 ATL07 (Sea Ice Height) data files for a specific beam
260266
@@ -273,6 +279,8 @@ def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
273279
- ``'gt3r'``
274280
ATTRIBUTES: bool, default False
275281
read HDF5 attributes for groups and variables
282+
KEEP: bool, default False
283+
keep file object open
276284
277285
Returns
278286
-------
@@ -340,6 +348,7 @@ def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
340348
IS2_atl07_attrs[gtx]['sea_ice_segments'][key][k][att_name] = att_val
341349

342350
# Closing the HDF5 file
343-
fileID.close()
351+
if not KEEP:
352+
fileID.close()
344353
# Return the datasets and variables
345354
return (IS2_atl07_mds, IS2_atl07_attrs)

icesat2_toolkit/io/ATL10.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
h5py = import_dependency('h5py')
3636

3737
# PURPOSE: read ICESat-2 ATL10 HDF5 data files
38-
def read_granule(FILENAME, ATTRIBUTES=False, **kwargs):
38+
def read_granule(FILENAME, ATTRIBUTES=False, KEEP=False, **kwargs):
3939
"""
4040
Reads ICESat-2 ATL10 (Sea Ice Freeboard) data files
4141
@@ -45,6 +45,8 @@ def read_granule(FILENAME, ATTRIBUTES=False, **kwargs):
4545
full path to ATL10 file
4646
ATTRIBUTES: bool, default False
4747
read HDF5 attributes for groups and variables
48+
KEEP: bool, default False
49+
keep file object open
4850
4951
Returns
5052
-------
@@ -208,19 +210,22 @@ def read_granule(FILENAME, ATTRIBUTES=False, **kwargs):
208210
IS2_atl10_attrs['quality_assessment'][key][k][att_name]= att_val
209211

210212
# Closing the HDF5 file
211-
fileID.close()
213+
if not KEEP:
214+
fileID.close()
212215
# Return the datasets and variables
213216
return (IS2_atl10_mds, IS2_atl10_attrs, IS2_atl10_beams)
214217

215218
# PURPOSE: find valid beam groups within ICESat-2 ATL10 HDF5 data files
216-
def find_beams(FILENAME, **kwargs):
219+
def find_beams(FILENAME, KEEP=False, **kwargs):
217220
"""
218221
Find valid beam groups within ICESat-2 ATL10 (Sea Ice Freeboard) data files
219222
220223
Parameters
221224
----------
222225
FILENAME: str
223226
full path to ATL10 file
227+
KEEP: bool, default False
228+
keep file object open
224229
225230
Returns
226231
-------
@@ -248,12 +253,13 @@ def find_beams(FILENAME, **kwargs):
248253
else:
249254
IS2_atl10_beams.append(gtx)
250255
# Closing the HDF5 file
251-
fileID.close()
256+
if not KEEP:
257+
fileID.close()
252258
# return the list of beams
253259
return IS2_atl10_beams
254260

255261
# PURPOSE: read ICESat-2 ATL10 HDF5 data files for beam variables
256-
def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
262+
def read_beam(FILENAME, gtx, ATTRIBUTES=False, KEEP=False, **kwargs):
257263
"""
258264
Reads ICESat-2 ATL10 (Sea Ice Freeboard) data files for a specific beam
259265
@@ -272,6 +278,8 @@ def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
272278
- ``'gt3r'``
273279
ATTRIBUTES: bool, default False
274280
read HDF5 attributes for groups and variables
281+
KEEP: bool, default False
282+
keep file object open
275283
276284
Returns
277285
-------
@@ -341,6 +349,7 @@ def read_beam(FILENAME, gtx, ATTRIBUTES=False, **kwargs):
341349
IS2_atl10_attrs[gtx][group][key][k][att_name] = att_val
342350

343351
# Closing the HDF5 file
344-
fileID.close()
352+
if not KEEP:
353+
fileID.close()
345354
# Return the datasets and variables
346355
return (IS2_atl10_mds, IS2_atl10_attrs)

0 commit comments

Comments
 (0)