1+ import numpy as np
2+ import h5py
3+
4+
5+ def ATL06_to_dict (filename , dataset_dict ):
6+ """
7+ Read selected datasets from an ATL06 file
8+
9+ Input arguments:
10+ filename: ATl06 file to read
11+ dataset_dict: A dictinary describing the fields to be read
12+ keys give the group names to be read,
13+ entries are lists of datasets within the groups
14+ Output argument:
15+ D6: dictionary containing ATL06 data. Each dataset in
16+ dataset_dict has its own entry in D6. Each dataset
17+ in D6 contains a list of numpy arrays containing the
18+ data
19+ """
20+
21+ D6 = []
22+ pairs = [1 , 2 , 3 ]
23+ beams = ['l' ,'r' ]
24+ # open the HDF5 file
25+ with h5py .File (filename ) as h5f :
26+ # loop over beam pairs
27+ for pair in pairs :
28+ # loop over beams
29+ for beam_ind , beam in enumerate (beams ):
30+ # check if a beam exists, if not, skip it
31+ if '/gt%d%s/land_ice_segments' % (pair , beam ) not in h5f :
32+ continue
33+ # loop over the groups in the dataset dictionary
34+ temp = {}
35+ for group in dataset_dict .keys ():
36+ for dataset in dataset_dict [group ]:
37+ DS = '/gt%d%s/%s/%s' % (pair , beam , group , dataset )
38+ # since a dataset may not exist in a file, we're going to try to read it, and if it doesn't work, we'll move on to the next:
39+ try :
40+ temp [dataset ]= np .array (h5f [DS ])
41+ # some parameters have a _FillValue attribute. If it exists, use it to identify bad values, and set them to np.NaN
42+ if '_FillValue' in h5f [DS ].attrs :
43+ fill_value = h5f [DS ].attrs ['_FillValue' ]
44+ temp [dataset ][temp [dataset ]== fill_value ]= np .NaN
45+ except KeyError as e :
46+ pass
47+ if len (temp ) > 0 :
48+ # it's sometimes convenient to have the beam and the pair as part of the output data structure: This is how we put them there.
49+ temp ['pair' ]= np .zeros_like (temp ['h_li' ])+ pair
50+ temp ['beam' ]= np .zeros_like (temp ['h_li' ])+ beam_ind
51+ temp ['filename' ]= filename
52+ D6 .append (temp )
53+ return D6
0 commit comments