Skip to content

Commit d44dca0

Browse files
committed
move extract product and version functions back to end of file
1 parent ddd2540 commit d44dca0

File tree

1 file changed

+76
-105
lines changed

1 file changed

+76
-105
lines changed

Diff for: icepyx/core/is2ref.py

+76-105
Original file line numberDiff line numberDiff line change
@@ -13,82 +13,6 @@
1313
# ICESat-2 specific reference functions
1414
# options to get customization options for ICESat-2 data (though could be used generally)
1515

16-
def extract_product(filepath, auth=None):
17-
"""
18-
Read the product type from the metadata of the file. Valid for local or s3 files, but must
19-
provide an auth object if reading from s3. Return the product as a string.
20-
21-
Parameters
22-
----------
23-
filepath: string
24-
local or remote location of a file. Could be a local string or an s3 filepath
25-
auth: earthaccess.auth.Auth, default None
26-
An earthaccess authentication object. Optional, but necessary if accessing data in an
27-
s3 bucket.
28-
"""
29-
# Generate a file reader object relevant for the file location
30-
if filepath.startswith('s3'):
31-
if not auth:
32-
raise AttributeError('Must provide credentials to `auth` if accessing s3 data')
33-
# Read the s3 file
34-
s3 = earthaccess.get_s3fs_session(daac="NSIDC", provider=auth)
35-
f = h5py.File(s3.open(filepath, 'rb'))
36-
else:
37-
# Otherwise assume a local filepath. Read with h5py.
38-
f = h5py.File(filepath, 'r')
39-
40-
# Extract the product information
41-
try:
42-
product = f.attrs['short_name']
43-
if isinstance(product, bytes):
44-
# For most products the short name is stored in a bytes string
45-
product = product.decode()
46-
elif isinstance(product, np.ndarray):
47-
# ATL14 saves the short_name as an array ['ATL14']
48-
product = product[0]
49-
product = _validate_product(product)
50-
except KeyError:
51-
raise 'Unable to parse the product name from file metadata'
52-
# Close the file reader
53-
f.close()
54-
return product
55-
56-
def extract_version(filepath, auth=None):
57-
"""
58-
Read the version from the metadata of the file. Valid for local or s3 files, but must
59-
provide an auth object if reading from s3. Return the version as a string.
60-
61-
Parameters
62-
----------
63-
filepath: string
64-
local or remote location of a file. Could be a local string or an s3 filepath
65-
auth: earthaccess.auth.Auth, default None
66-
An earthaccess authentication object. Optional, but necessary if accessing data in an
67-
s3 bucket.
68-
"""
69-
# Generate a file reader object relevant for the file location
70-
if filepath.startswith('s3'):
71-
if not auth:
72-
raise AttributeError('Must provide credentials to `auth` if accessing s3 data')
73-
# Read the s3 file
74-
s3 = earthaccess.get_s3fs_session(daac="NSIDC", provider=auth)
75-
f = h5py.File(s3.open(filepath, 'rb'))
76-
else:
77-
# Otherwise assume a local filepath. Read with h5py.
78-
f = h5py.File(filepath, 'r')
79-
80-
# Read the version information
81-
try:
82-
version = f['METADATA']['DatasetIdentification'].attrs['VersionID']
83-
if isinstance(version, np.ndarray):
84-
# ATL14 stores the version as an array ['00x']
85-
version = version[0]
86-
except KeyError:
87-
raise 'Unable to parse the version from file metadata'
88-
# Close the file reader
89-
f.close()
90-
return version
91-
9216

9317
def _validate_product(product):
9418
"""
@@ -127,8 +51,6 @@ def _validate_product(product):
12751

12852

12953
# DevGoal: See if there's a way to dynamically get this list so it's automatically updated
130-
131-
13254
def _validate_OA_product(product):
13355
"""
13456
Confirm a valid ICESat-2 product was specified
@@ -408,6 +330,7 @@ def gt2spot(gt, sc_orient):
408330

409331
return np.uint8(spot)
410332

333+
411334
def latest_version(product):
412335
"""
413336
Determine the most recent version available for the given product.
@@ -418,38 +341,86 @@ def latest_version(product):
418341
'006'
419342
"""
420343
_about_product = about_product(product)
421-
return max(
422-
[entry["version_id"] for entry in _about_product["feed"]["entry"]]
423-
)
344+
return max([entry["version_id"] for entry in _about_product["feed"]["entry"]])
424345

425-
def extract_product(filepath):
346+
347+
def extract_product(filepath, auth=None):
426348
"""
427-
Read the product type from the metadata of the file. Return the product as a string.
349+
Read the product type from the metadata of the file. Valid for local or s3 files, but must
350+
provide an auth object if reading from s3. Return the product as a string.
351+
352+
Parameters
353+
----------
354+
filepath: string
355+
local or remote location of a file. Could be a local string or an s3 filepath
356+
auth: earthaccess.auth.Auth, default None
357+
An earthaccess authentication object. Optional, but necessary if accessing data in an
358+
s3 bucket.
428359
"""
429-
with h5py.File(filepath, 'r') as f:
430-
try:
431-
product = f.attrs['short_name']
432-
if isinstance(product, bytes):
433-
# For most products the short name is stored in a bytes string
434-
product = product.decode()
435-
elif isinstance(product, np.ndarray):
436-
# ATL14 saves the short_name as an array ['ATL14']
437-
product = product[0]
438-
product = _validate_product(product)
439-
except KeyError:
440-
raise 'Unable to parse the product name from file metadata'
360+
# Generate a file reader object relevant for the file location
361+
if filepath.startswith("s3"):
362+
if not auth:
363+
raise AttributeError(
364+
"Must provide credentials to `auth` if accessing s3 data"
365+
)
366+
# Read the s3 file
367+
s3 = earthaccess.get_s3fs_session(daac="NSIDC", provider=auth)
368+
f = h5py.File(s3.open(filepath, "rb"))
369+
else:
370+
# Otherwise assume a local filepath. Read with h5py.
371+
f = h5py.File(filepath, "r")
372+
373+
# Extract the product information
374+
try:
375+
product = f.attrs["short_name"]
376+
if isinstance(product, bytes):
377+
# For most products the short name is stored in a bytes string
378+
product = product.decode()
379+
elif isinstance(product, np.ndarray):
380+
# ATL14 saves the short_name as an array ['ATL14']
381+
product = product[0]
382+
product = _validate_product(product)
383+
except KeyError:
384+
raise "Unable to parse the product name from file metadata"
385+
# Close the file reader
386+
f.close()
441387
return product
442388

443-
def extract_version(filepath):
389+
390+
def extract_version(filepath, auth=None):
444391
"""
445-
Read the version from the metadata of the file. Return the version as a string.
392+
Read the version from the metadata of the file. Valid for local or s3 files, but must
393+
provide an auth object if reading from s3. Return the version as a string.
394+
395+
Parameters
396+
----------
397+
filepath: string
398+
local or remote location of a file. Could be a local string or an s3 filepath
399+
auth: earthaccess.auth.Auth, default None
400+
An earthaccess authentication object. Optional, but necessary if accessing data in an
401+
s3 bucket.
446402
"""
447-
with h5py.File(filepath, 'r') as f:
448-
try:
449-
version = f['METADATA']['DatasetIdentification'].attrs['VersionID']
450-
if isinstance(version, np.ndarray):
451-
# ATL14 stores the version as an array ['00x']
452-
version = version[0]
453-
except KeyError:
454-
raise 'Unable to parse the version from file metadata'
403+
# Generate a file reader object relevant for the file location
404+
if filepath.startswith("s3"):
405+
if not auth:
406+
raise AttributeError(
407+
"Must provide credentials to `auth` if accessing s3 data"
408+
)
409+
# Read the s3 file
410+
s3 = earthaccess.get_s3fs_session(daac="NSIDC", provider=auth)
411+
f = h5py.File(s3.open(filepath, "rb"))
412+
else:
413+
# Otherwise assume a local filepath. Read with h5py.
414+
f = h5py.File(filepath, "r")
415+
416+
# Read the version information
417+
try:
418+
version = f["METADATA"]["DatasetIdentification"].attrs["VersionID"]
419+
if isinstance(version, np.ndarray):
420+
# ATL14 stores the version as an array ['00x']
421+
version = version[0]
422+
except KeyError:
423+
raise "Unable to parse the version from file metadata"
424+
# Close the file reader
425+
f.close()
455426
return version

0 commit comments

Comments
 (0)