Skip to content

Commit

Permalink
modify Topography.write method to 1) have topo_type as second argumen…
Browse files Browse the repository at this point in the history
…t, same as read, rather than third, and 2) write with format 9.3f by default for smaller files, with new parameter Z_format to change. Also modified test problems for backward compatibility
  • Loading branch information
rjleveque committed Jun 4, 2016
1 parent 48b5014 commit c39dc25
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/python/geoclaw/topotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,8 @@ def read_header(self):

return num_cells

def write(self, path, no_data_value=None, topo_type=None, masked=True,
header_style='geoclaw'):
def write(self, path, topo_type=None, no_data_value=None, masked=True,
header_style='geoclaw', Z_format="%9.3f"):
r"""Write out a topography file to path of type *topo_type*.
Writes out a topography file of topo type specified with *topo_type* or
Expand All @@ -799,13 +799,17 @@ def write(self, path, no_data_value=None, topo_type=None, masked=True,
:Input:
- *path* (str) - file to write
- *no_data_value* - values used to indicate missing data
- *topo_type* (int) - GeoClaw format topo_type
- *no_data_value* - values used to indicate missing data
- *masked* (bool) - unused??
- *header_style* (str) - indicates format of header lines
'geoclaw' or 'default' ==> write value then label
'arcgis' or 'asc' ==> write label then value
(needed for .asc files in ArcGIS)
- *Z_format* (str) - string format to use for Z values
The default format "%9.3f" gives mm precision and gives a
smaller files than the previous default of "%22.15e" used in
GeoClaw version 5.3.1 and earlier.
"""

Expand Down Expand Up @@ -883,23 +887,25 @@ def write(self, path, no_data_value=None, topo_type=None, masked=True,

# Write out topography data
if topo_type == 2:
Z_format = Z_format + "\n"
if masked_Z:
Z_filled = numpy.flipud(self.Z.filled())
else:
Z_filled = numpy.flipud(self.Z)
for i in xrange(self.Z.shape[0]):
for j in xrange(self.Z.shape[1]):
outfile.write("%22.15e\n" % Z_filled[i,j])
outfile.write(Z_format % Z_filled[i,j])
if masked_Z:
del Z_filled
elif topo_type == 3:
Z_format = Z_format + " "
if masked_Z:
Z_flipped = numpy.flipud(self.Z.filled())
else:
Z_flipped = numpy.flipud(self.Z)
for i in xrange(self.Z.shape[0]):
for j in xrange(self.Z.shape[1]):
outfile.write("%22.15e " % (Z_flipped[i,j]))
outfile.write(Z_format % (Z_flipped[i,j]))
outfile.write("\n")
if masked_Z:
del Z_flipped
Expand Down
8 changes: 4 additions & 4 deletions tests/test_topotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_read_write_topo_bowl():
try:
for topo_type in xrange(1, 4):
path = os.path.join(temp_path, 'bowl.tt%s' % topo_type)
topo.write(path, topo_type=topo_type)
topo.write(path, topo_type=topo_type,Z_format="%22.15e")

topo_in = topotools.Topography(path)
assert numpy.allclose(topo.Z, topo_in.Z), \
Expand Down Expand Up @@ -172,7 +172,7 @@ def test_read_write_topo_bowl_hill():

for topo_type in xrange(1,4):
file_path = os.path.join(temp_path, 'bowl_hill.tt%s' % topo_type)
topo.write(file_path, topo_type=topo_type)
topo.write(file_path, topo_type=topo_type,Z_format="%22.15e")
topo_in = topotools.Topography(path=file_path, topo_type=topo_type)
assert numpy.allclose(topo.Z, topo_in.Z), \
"Written file of topo_type=%s does not equal read in" + \
Expand Down Expand Up @@ -207,7 +207,7 @@ def test_netcdf():
# Write out NetCDF version of file
ascii_topo = topotools.Topography(path=local_path)
ascii_topo.read()
ascii_topo.write(nc_path, topo_type=4)
ascii_topo.write(nc_path, topo_type=4,Z_format="%22.15e")

# Read back in NetCDF file
nc_topo = topotools.Topography(path=nc_path)
Expand Down Expand Up @@ -313,7 +313,7 @@ def func(x, y):
# Load (and save) test data and make the comparison
test_data_path = os.path.join(testdir, "data", "unstructured_test_data.tt3")
if save:
topo.write(test_data_path)
topo.write(test_data_path,Z_format="%22.15e")

compare_data = topotools.Topography(path=test_data_path)

Expand Down

0 comments on commit c39dc25

Please sign in to comment.