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

replace xml.etree with defusedxml for ElementTree #340

Merged
merged 1 commit into from
May 9, 2020
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
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ ts_data, meta = readfile.read('timeseries_ERA5_ramp_demErr.h5')

### 3. [Documentation](https://mintpy.readthedocs.io/) ###

Detailed algorithms implemented in the software are described in [Yunjun et al. (2019)](https://doi.org/10.1016/j.cageo.2019.104331).
Algorithms implemented in the software are described in details in [Yunjun et al. (2019)](https://doi.org/10.1016/j.cageo.2019.104331).

+ [Tutorials in Jupyter Notebook](https://github.com/insarlab/MintPy-tutorial)
+ [Example datasets](./demo_dataset.md)
Expand Down
1 change: 1 addition & 0 deletions docs/conda.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cdsapi
cvxopt
dask>=1.0
dask-jobqueue>=0.3
defusedxml
ecCodes
h5py
lxml
Expand Down
1 change: 1 addition & 0 deletions docs/conda_env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
- cvxopt
- dask>=1.0
- dask-jobqueue>=0.3
- defusedxml
- ecCodes
- h5py
- lxml
Expand Down
1 change: 1 addition & 0 deletions docs/ports.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ swig
swig-python
gdal +expat +geos +hdf5 +netcdf +openjpeg +postgresql95 +sqlite3
py37-cython
py37-defusedxml
py37-mpi4py +gcc7 +openmpi
py37-numpy +gcc7 +openblas
py37-scipy +gcc7 +openblas
Expand Down
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cvxopt
dask>=1.0
dask-jobqueue>=0.3
defusedxml
h5py
lxml
matplotlib
Expand Down
96 changes: 60 additions & 36 deletions mintpy/dem_gsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def write_dem_file(SNWE, dem_file, grid_dir):
return meta


def write_rsc_file(meta, out_file):
def write_rsc_file(meta, fname):
# initiate meta dict
rsc = dict()
rsc['FILE_LENGTH'] = meta.length
Expand All @@ -148,12 +148,15 @@ def write_rsc_file(meta, out_file):
rsc['PROJECTION'] = 'LATLON'
rsc['DATE12'] = '111111-222222'
rsc['DATA_TYPE'] = 'int16'

# write rsc file
writefile.write_roipac_rsc(rsc, out_file, print_msg=True)
return out_file
rsc_file = fname + '.rsc'
writefile.write_roipac_rsc(rsc, rsc_file, print_msg=True)

return rsc_file


def write_vrt_file(meta, out_file):
def write_vrt_file(meta, fname):
# initiate vrt string
vrt_str = """<VRTDataset rasterXSize="{w}" rasterYSize="{l}">
<SRS>EPSG:4326</SRS>
Expand All @@ -176,40 +179,74 @@ def write_vrt_file(meta, out_file):
lo=2*meta.width)

# write to vrt file
with open(out_file, 'w') as f:
vrt_file = fname + '.vrt'
with open(vrt_file, 'w') as f:
f.write(vrt_str)
print('write {}'.format(out_file))
return out_file
print('write {}'.format(vrt_file))

return vrt_file


def write_isce_metadata(meta, fname):
"""
Write metadata files in ISCE format (.vrt and .xml files)
"""
import isce
import isceobj

# create isce object for xml file
img = isceobj.createDemImage()
img.setFilename(os.path.abspath(fname))
img.setWidth(meta.width)
img.setLength(meta.length)
img.setAccessMode('READ')
img.bands = 1
img.dataType = 'SHORT'
img.scheme = 'BIP'
img.reference = 'WGS84'

img.firstLatitude = meta.north + meta.lat_step / 2.
img.firstLongitude = meta.west + meta.lon_step / 2.
img.deltaLatitude = meta.lat_step
img.deltaLongitude = meta.lon_step

# write to xml file
xml_file = fname + '.xml'
img.dump(xml_file)

return xml_file


def add_reference_datum(xml_file):
"""
Example of modifying an existing XML file
"""

import xml.etree.ElementTree as ET
from xml.dom import minidom
print('add <reference> info to xml file: {}'.format(os.path.basename(xml_file)))
tree = ET.parse(xml_file)
root = tree.getroot()

# get property element for reference
ref = ET.Element("property")
ref.attrib = {'name': 'reference'}
ref = ET.Element("property", attrib={'name': 'reference'})

elm1 = ET.Element("value")
elm1.text = "WGS84"
ref.append(elm1)
val = ET.SubElement(ref, "value")
val.text = "WGS84"

elm1 = ET.Element("doc")
elm1.text = "Geodetic datum"
ref.append(elm1)

ref = ET.fromstring(minidom.parseString(ET.tostring(ref)).toprettyxml(indent=" "))
doc = ET.SubElement(ref, "doc")
doc.text = "Geodetic datum"

# pretty xml
ref_str = minidom.parseString(ET.tostring(ref)).toprettyxml(indent=" ")
yunjunz marked this conversation as resolved.
Show resolved Hide resolved
ref = ET.fromstring(ref_str)

# write back to xml file
tree = ET.parse(xml_file)
root = tree.getroot()
root.append(ref)
tree.write(xml_file)
return xml_file



##################################################################################################
def main(iargs=None):
inps = cmd_line_parse(iargs)
Expand All @@ -219,24 +256,11 @@ def main(iargs=None):
grid_dir=inps.grid_dir)

# rsc file for roipac
write_rsc_file(meta, out_file='{}.rsc'.format(inps.outfile))

# vrt file for gdal
write_vrt_file(meta, out_file='{}.vrt'.format(inps.outfile))

# vrt file to xml file
try:
print('generate {}.xml file using ISCE command'.format(inps.outfile))
cmd = 'gdal2isce_xml.py -i {}.vrt'.format(inps.outfile)
print(cmd)
os.system(cmd)
write_rsc_file(meta, inps.outfile)

except RuntimeError:
raise RuntimeError('$ISCE_HOME/applications/gdal2isce_xml.py command not found!')
# vrt/xml file for isce
write_isce_metadata(meta, inps.outfile)

# DEHM from GSI is already in ellipsoid, add the information to xml file
if os.path.isfile(inps.outfile+'.xml'):
add_reference_datum(inps.outfile+'.xml')
return


Expand Down
19 changes: 9 additions & 10 deletions mintpy/utils/readfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import os
import re
import warnings
import xml.etree.ElementTree as ET
import defusedxml.ElementTree as ET

import h5py
import json
Expand Down Expand Up @@ -960,12 +960,11 @@ def read_isce_xml(fname, standardize=True):
# in form: root/component coordinate*/property name/value
for coord_name, prefix in zip(['coordinate1', 'coordinate2'], ['X', 'Y']):
child = root.find("./component[@name='{}']".format(coord_name))
if ET.iselement(child):
v_step = float(child.find("./property[@name='delta']").find('value').text)
v_first = float(child.find("./property[@name='startingvalue']").find('value').text)
if abs(v_step) < 1. and abs(v_step) > 1e-7:
xmlDict['{}_STEP'.format(prefix)] = v_step
xmlDict['{}_FIRST'.format(prefix)] = v_first - v_step / 2.
v_step = float(child.find("./property[@name='delta']").find('value').text)
v_first = float(child.find("./property[@name='startingvalue']").find('value').text)
if abs(v_step) < 1. and abs(v_step) > 1e-7:
xmlDict['{}_STEP'.format(prefix)] = v_step
xmlDict['{}_FIRST'.format(prefix)] = v_first - v_step / 2.

# PAMDataset, e.g. hgt.rdr.aux.xml
elif root.tag == 'PAMDataset':
Expand Down Expand Up @@ -1006,16 +1005,16 @@ def read_envi_hdr(fname, standardize=True):


def read_gdal_vrt(fname, standardize=True):
"""Read GDAL .vrt file into a python dict structure
"""Read GDAL .vrt file into a python dict structure using gdal

Modified from $ISCE_HOME/applications/gdal2isce_xml.gdal2isce_xml() written by David Bekaert.
"""

# read dataset using gdal
try:
import gdal
except ImportError:
raise ImportError('Cannot import gdal!')

# read dataset using gdal
ds = gdal.Open(fname, gdal.GA_ReadOnly)

atr = {}
Expand Down