diff --git a/src/python/geoclaw/topotools.py b/src/python/geoclaw/topotools.py index c54baddc9..28e645eda 100644 --- a/src/python/geoclaw/topotools.py +++ b/src/python/geoclaw/topotools.py @@ -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 @@ -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. """ @@ -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 diff --git a/tests/test_topotools.py b/tests/test_topotools.py index 0f2824b55..e1d30074b 100644 --- a/tests/test_topotools.py +++ b/tests/test_topotools.py @@ -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), \ @@ -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" + \ @@ -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) @@ -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)