Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subset: support hdf5 <--> binary #1076

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
subset: support hdf5 <--> binary
+ support hdf5 input --> binary output

+ support binary input --> hdf5 output
  • Loading branch information
yunjunz committed Aug 23, 2023
commit 4da897faa378010978461be516a01f82328902b3
1 change: 1 addition & 0 deletions src/mintpy/cli/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
subset.py inputs/ifgramStack.h5 -y 400 1500 -x 200 600
subset.py geo_velocity.h5 -l 30.5 30.8 -L 130.3 130.9
subset.py 030405_090801.unw -t SinabungT495F50AlosA.template
subset.py demLat*.dem.wgs84 --lat 32.5 33.0 --lon 130.2 130.6 -o srtm1.h5

# subset to the same coverage as the reference file
subset.py geo_incidence.h5 -r subset_geo_velocity.h5
Expand Down
47 changes: 32 additions & 15 deletions src/mintpy/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,15 @@ def subset_file(fname, subset_dict_input, out_file=None):
# subset datasets one by one
dsNames = readfile.get_dataset_list(fname)

ext = os.path.splitext(out_file)[1]
if ext in ['.h5', '.he5']:
in_ext = os.path.splitext(fname)[1]
out_ext = os.path.splitext(out_file)[1]
if in_ext in ['.h5', '.he5']:

# initiate the output file
writefile.layout_hdf5(out_file, metadata=atr, ref_file=fname)
if out_ext in ['.h5', '.he5']:
writefile.layout_hdf5(out_file, metadata=atr, ref_file=fname)
else:
dsDict = dict()

# subset dataset one-by-one
for dsName in dsNames:
Expand All @@ -339,16 +344,24 @@ def subset_file(fname, subset_dict_input, out_file=None):
pix_box4subset[0]:pix_box4subset[2]] = data
data_out = np.array(data_out, dtype=data.dtype)

# write
block = [0, int(atr['LENGTH']), 0, int(atr['WIDTH'])]
writefile.write_hdf5_block(
out_file,
data=data_out,
datasetName=dsName,
block=block,
print_msg=True)
# write each dataset to HDF5 file
# OR save each dataset for binary file
if out_ext in ['.h5', '.he5']:
block = [0, int(atr['LENGTH']), 0, int(atr['WIDTH'])]
writefile.write_hdf5_block(
out_file,
data=data_out,
datasetName=dsName,
block=block,
print_msg=True)
else:
dsDict[dsName] = data_out

elif ds_ndim == 3:
# 3D dataset is not supported in binary
if out_ext not in ['.h5', '.he5']:
raise ValueError(f'Writing 3D dataset {dsName} into binary file is NOT supported!')

if ds_ndim == 3:
prog_bar = ptime.progressBar(maxValue=ds_shape[0])
for i in range(ds_shape[0]):
# read
Expand Down Expand Up @@ -378,8 +391,12 @@ def subset_file(fname, subset_dict_input, out_file=None):
prog_bar.close()
print(f'finished writing to file: {out_file}')

# write to binary file
if out_ext not in ['.h5', '.he5']:
writefile.write(dsDict, out_file=out_file, metadata=atr)

else:
# IO for binary files
# binary --> binary / hdf5 file
dsDict = dict()
for dsName in dsNames:
dsDict[dsName] = subset_dataset(
Expand All @@ -394,8 +411,8 @@ def subset_file(fname, subset_dict_input, out_file=None):
atr['BANDS'] = len(dsDict.keys())
writefile.write(dsDict, out_file=out_file, metadata=atr, ref_file=fname)

# write extra metadata files for ISCE data files
if os.path.isfile(fname+'.xml') or os.path.isfile(fname+'.aux.xml'):
# write extra metadata files for ISCE2 binary data files
if out_ext not in ['.h5', '.he5'] and (os.path.isfile(fname+'.xml') or os.path.isfile(fname+'.aux.xml')):
writefile.write_isce_xml(atr, out_file)

return out_file
Expand Down