def test_read_write_sa_sanity_check(self): sa_ref_array = file_io_tools.read_file(test_files.forcings_sa) file_io_tools.write_array_to_simple_ascii(data=sa_ref_array, out_file='sa_out_test_file.sa') sa_read_array = file_io_tools.read_file('sa_out_test_file.sa') self.assertIsNone(np.testing.assert_array_almost_equal(sa_ref_array, sa_read_array, decimal=12), 'should be able to write to .sa and get the same data back') os.remove('sa_out_test_file.sa')
def test_write_read_pfb(self): forcings_data = file_io_tools.read_file(test_files.forcings_pfb) file_io_tools.write_pfb(forcings_data, 'test_pfb_out.pfb') read_data = file_io_tools.read_file('test_pfb_out.pfb') self.assertIsNone(np.testing.assert_array_equal(forcings_data, read_data), 'writing and reading a pfb gives back the same array values') os.remove('test_pfb_out.pfb')
def test_clm_clip_latlon(self): if os.environ.get('TRAVIS'): pass elif os.path.isfile(test_files.conus1_latlon): bbox_list = test_files.huc10190004.get('conus1_bbox') bbox = BBox(bbox_list[0], bbox_list[1], bbox_list[2], bbox_list[3]) clm_clipper = ClmClipper(bbox) latlon_formatted, latlon_data = clm_clipper.clip_latlon( test_files.conus1_latlon) clm_clipper.write_lat_lon(latlon_formatted, 'WBDHU8_latlon_test.sa', x=latlon_data.shape[2], y=latlon_data.shape[1], z=latlon_data.shape[0]) self.assertIsNone( np.testing.assert_array_equal( file_io_tools.read_file('WBDHU8_latlon_test.sa'), file_io_tools.read_file( test_files.huc10190004.get( 'conus1_latlon').as_posix())), 'writing and reading a tif gives back the same array values') os.remove('WBDHU8_latlon_test.sa') else: print( 'WARNING! Unable to run test test_clm_clip_latlon because source file not found. ' 'copy conus1_Grid_Centers_Short_Deg.format.sa into test_inputs/CONUS1_Inputs to enable test!' ) pass
def test_read_write_tif_to_pfb(self): tif_array = file_io_tools.read_file(test_files.conus1_dem) file_io_tools.write_pfb(tif_array, outfile='conus1_dem_tif_to_pfb_test.pfb') pfb_array = file_io_tools.read_file('conus1_dem_tif_to_pfb_test.pfb') self.assertIsNone(np.testing.assert_array_equal(tif_array, pfb_array, 'Converting from tif to pfb gives back same data')) os.remove('conus1_dem_tif_to_pfb_test.pfb')
def test_read_write_tif_sanity_check(self): tif_array = file_io_tools.read_file(test_files.conus2_subsurface) tif_ref = file_io_tools.read_geotiff(test_files.conus2_subsurface) file_io_tools.write_array_to_geotiff('conus2_subsurface_tif_read_write_test.tif', tif_array, tif_ref.GetGeoTransform(), tif_ref.GetProjectionRef()) tif_read_array = file_io_tools.read_file('conus2_subsurface_tif_read_write_test.tif') self.assertIsNone(np.testing.assert_array_equal(tif_array, tif_read_array), 'reading and writing a tif should not change the data') os.remove('conus2_subsurface_tif_read_write_test.tif')
def test_box_clip_default(self): bulk_clipper.box_clip((1040, 717, 85, 30), self.good_input_file_list[:1]) ref_data = read_file( test_files.huc10190004.get('conus1_dem_box').as_posix()) written_data = read_file('./CONUS2.0_RawDEM_CONUS1clip_clip.pfb') self.assertIsNone(np.testing.assert_array_equal( ref_data, written_data)) os.remove('./CONUS2.0_RawDEM_CONUS1clip_clip.pfb')
def test_mask_clip_default(self): mask = test_files.huc10190004.get('conus1_mask').as_posix() bulk_clipper.mask_clip(mask, self.good_input_file_list[:1]) ref_data = read_file( test_files.huc10190004.get('conus1_dem').as_posix()) written_data = read_file('./CONUS2.0_RawDEM_CONUS1clip_clip.pfb') self.assertIsNone(np.testing.assert_array_equal( ref_data, written_data)) os.remove('./CONUS2.0_RawDEM_CONUS1clip_clip.pfb')
def test_conus1_raster_defaults(self): rasterize_shape.rasterize_shape(input_path=self.good_shape_file_path, shapefile=self.good_shape_file_name, ref_file=test_files.conus1_mask) ref_mask = read_file(test_files.huc10190004.get('conus1_mask')) ref_bbox = test_files.huc10190004.get('conus1_bbox') self.assertSequenceEqual(ref_bbox, read_bbox('bbox.txt')) self.assertIsNone(np.testing.assert_array_equal(ref_mask, read_file('WBDHU8.tif'))) self.assertIsNone(os.remove('bbox.txt')) self.assertIsNone(os.remove('WBDHU8.tif'))
def test_read_pfb_sa_tif(self): sa_array = file_io_tools.read_file(test_files.forcings_sa) pfb_array = file_io_tools.read_file(test_files.forcings_pfb) tif_array = file_io_tools.read_file(test_files.forcings_tif) self.assertIsNone(np.testing.assert_array_almost_equal(sa_array, pfb_array, decimal=3), 'reading a .sa file and a .pfb file result in same array values') self.assertIsNone(np.testing.assert_array_almost_equal(sa_array, tif_array, decimal=3), 'reading a .sa file and a .pfb file result in same array values') self.assertIsNone(np.testing.assert_array_almost_equal(tif_array, pfb_array, decimal=12), 'reading a .sa file and a .pfb file result in same array values')
def test_rasterize_no_data_values(self): rasterizer = ShapefileRasterizer(self.shape_path, shapefile_name=self.shape_name, reference_dataset=self.conus1_mask_datset, no_data=-9999999) rasterizer.reproject_and_mask() rasterizer.subset_mask.write_mask_to_tif(filename='testout.tif') self.assertIsNone( np.testing.assert_array_equal(file_io_tools.read_file( test_files.huc10190004.get('conus1_mask_-9999999').as_posix()), file_io_tools.read_file('testout.tif')), 'Should create a mask from CONUS1 with 1/-9999999') os.remove('testout.tif')
def test_reproject_conus2(self): rasterizer = ShapefileRasterizer(self.shape_path, self.shape_name, self.conus2_mask_dataset) rasterizer.reproject_and_mask() subset_mask = rasterizer.subset_mask subset_mask.add_bbox_to_mask() subset_mask.write_mask_to_tif(filename='testout.tif') self.assertIsNone( np.testing.assert_array_equal(file_io_tools.read_file(test_files.huc10190004.get('conus2_mask').as_posix()), file_io_tools.read_file('testout.tif')), 'Should create a mask from CONUS2 with 1/0s') os.remove('testout.tif')
def test_write_tiff(self): ds_ref = gdal.Open(os.fspath(test_files.regression_truth_tif)) file_io_tools.write_array_to_geotiff('test_write_tif_out.tif', file_io_tools.read_file(test_files.regression_truth_tif), ds_ref.GetGeoTransform(), ds_ref.GetProjection()) data_array = file_io_tools.read_file('test_write_tif_out.tif') self.assertIsNone(np.testing.assert_array_equal(data_array, file_io_tools.read_file( test_files.regression_truth_tif)), 'writing and reading a tif gives back the same array values') os.remove('test_write_tif_out.tif')
def test_write_pfb_to_tif_to_sa(self): pfb_array = file_io_tools.read_file(test_files.forcings_pfb) srs = osr.SpatialReference() srs.SetWellKnownGeogCS("WGS84") file_io_tools.write_array_to_geotiff(data=pfb_array, out_raster_path='tif_out_test_forcings_file.tif', geo_transform=[0, 1000, 0, 0, 0, -1000], projection=srs.ExportToWkt()) sa_array = file_io_tools.read_file(test_files.forcings_sa) tif_array = file_io_tools.read_file('tif_out_test_forcings_file.tif') self.assertIsNone(np.testing.assert_array_equal(tif_array, pfb_array, 'Converting from multi-layer pfb to tif gives back same data')) self.assertIsNone(np.testing.assert_array_almost_equal(tif_array, sa_array, decimal=4)) os.remove('tif_out_test_forcings_file.tif')
def test_conus1_subset_regression(self): if os.environ.get('TRAVIS'): pass else: test_dir = Path('test_outputs') test_dir.mkdir(exist_ok=True) subset_conus.subset_conus(input_path=self.good_shape_file_path, shapefile=self.good_shape_file_name, conus_files='/home/arezaii/git/subset_1/CONUS1_inputs', out_dir=test_dir, out_name='test_conus1_subset') ref_subsurface = read_file(test_files.huc10190004.get('conus1_subsurface')) ref_mask = read_file(test_files.huc10190004.get('conus1_mask')) ref_slopex = read_file(test_files.huc10190004.get('conus1_slopex')) ref_slopey = read_file(test_files.huc10190004.get('conus1_slopey')) self.assertIsNone(np.testing.assert_array_equal(ref_subsurface, read_file(test_dir / 'grid3d.v3_clip.pfb'))) self.assertIsNone(np.testing.assert_array_equal(ref_mask, read_file( test_dir / 'test_conus1_subset_raster_from_shapefile.tif'))) self.assertIsNone(np.testing.assert_array_equal(ref_slopex, read_file(test_dir / 'slopex_clip.pfb'))) self.assertIsNone(np.testing.assert_array_equal(ref_slopey, read_file(test_dir / 'slopey_clip.pfb'))) with open(test_dir / 'test_conus1_subset.pfsol', 'r') as test_file: with open(test_files.huc10190004.get('conus1_sol'), 'r') as ref_file: self.assertEqual(test_file.read(), ref_file.read(), 'Writing PFSOL file matches reference for conus1') """ make sure the vtk files match as well. skip the first two lines as they contain a version and filename that may vary """ with open(test_dir / 'test_conus1_subset.vtk', 'r') as test_file: with open(test_files.huc10190004.get('conus1_vtk'), 'r') as ref_file: self.assertEqual(test_file.read().split('\n')[2:], ref_file.read().split('\n')[2:], 'Writing vtk file matches reference for conus1') shutil.rmtree(test_dir)
def box_clip(bbox, data_files, out_dir='.', pfb_outs=1, tif_outs=0) -> None: """clip a list of files using a bounding box Parameters ---------- bbox : tuple tuple of x, y, nx, ny to specifies the bounding region data_files : list list of data files (tif, pfb) to clip from out_dir : str, optional output directory (optional) (Default value = '.') pfb_outs : int, optional write pfb files as outputs (optional) (Default value = 1) tif_outs : int, optional write tif files as outputs (optional) (Default value = 0) Returns ------- None """ # create clipper with bbox clipper = BoxClipper(ref_array=file_io_tools.read_file(data_files[0]), x=bbox[0], y=bbox[1], nx=bbox[2], ny=bbox[3]) # clip all inputs and write outputs clip_inputs(clipper, input_list=data_files, out_dir=out_dir, pfb_outs=pfb_outs, tif_outs=tif_outs)
def test_box_clip_with_padding(self): data_array = file_io_tools.read_file(test_files.conus1_dem.as_posix()) # css-like padding (top,right,bot,left) bbox = test_files.huc10190004.get('conus1_bbox') box_clipper = BoxClipper(ref_array=data_array, x=bbox[0], y=bbox[1], nx=bbox[2], ny=bbox[3], padding=(1, 6, 1, 5)) subset, _, _, _ = box_clipper.subset() self.assertEqual(1, subset.shape[0]) self.assertEqual(32, subset.shape[1]) self.assertEqual(96, subset.shape[2]) file_io_tools.write_pfb(subset, 'WBDHU8_conus1_dem_padded_test.pfb') padded_subset_ref = file_io_tools.read_file( test_files.huc10190004.get('conus1_dem_padded_box').as_posix()) self.assertIsNone( np.testing.assert_array_equal(padded_subset_ref, subset)) os.remove('WBDHU8_conus1_dem_padded_test.pfb')
def test_clm_clip_latlon(self): if os.environ.get('TRAVIS'): pass elif os.path.isfile(test_files.conus1_latlon): bbox_list = test_files.huc10190004.get('conus1_bbox') bbox = BBox(bbox_list[0], bbox_list[1], bbox_list[2], bbox_list[3]) clm_clipper = ClmClipper(bbox) latlon_formatted, latlon_data = clm_clipper.clip_latlon(test_files.conus1_latlon) self.assertSequenceEqual((1, 30, 85), latlon_data.shape, 'check that clm clip when mask has no padding works') clm_clipper.write_lat_lon(latlon_formatted, 'WBDHU8_latlon_test.sa', x=latlon_data.shape[2], y=latlon_data.shape[1], z=latlon_data.shape[0]) self.assertIsNone(np.testing.assert_array_equal(file_io_tools.read_file('WBDHU8_latlon_test.sa'), file_io_tools.read_file( test_files.huc10190004.get('conus1_latlon'))), 'clipping conus1 lat/lon values regression test works') os.remove('WBDHU8_latlon_test.sa') else: print('WARNING! Unable to run test test_clm_clip_latlon because source file not found. ' 'copy conus1_Grid_Centers_Short_Deg.format.sa into test_inputs/CONUS1_Inputs to enable test!') pass
def test_subset_tif_conus2(self): data_array = file_io_tools.read_file(test_files.conus2_dem.as_posix()) my_mask = SubsetMask( test_files.huc10190004.get('conus2_mask').as_posix()) clipper = MaskClipper(subset_mask=my_mask, no_data_threshold=-1) return_arr, new_geom, new_mask, bbox = clipper.subset(data_array) file_io_tools.write_array_to_geotiff("conus_2_clip_dem_test.tif", return_arr, new_geom, my_mask.mask_tif.GetProjection()) self.assertIsNone( np.testing.assert_array_equal( file_io_tools.read_file( test_files.huc10190004.get('conus2_dem').as_posix()), file_io_tools.read_file('conus_2_clip_dem_test.tif')), 'Clipping DEM matches reference') os.remove('conus_2_clip_dem_test.tif') file_io_tools.write_bbox(bbox, 'bbox_conus2_full.txt') self.assertSequenceEqual( file_io_tools.read_bbox('bbox_conus2_full.txt'), test_files.huc10190004.get('conus2_bbox'), 'Subset writes correct bounding box file') os.remove('bbox_conus2_full.txt')
def test_box_clip(self): data_array = file_io_tools.read_file(test_files.conus1_dem.as_posix()) box_clipper = BoxClipper(ref_array=data_array) subset, _, _, _ = box_clipper.subset() self.assertEqual(1, subset.shape[0]) self.assertEqual(3342, subset.shape[2]) self.assertEqual(1888, subset.shape[1]) self.assertIsNone( np.testing.assert_array_equal(data_array, subset), 'selecting the whole region should return exactly what you would expect' ) box_clipper.update_bbox(x=10, y=10, nx=3332, ny=1878) subset2, _, _, _ = box_clipper.subset() self.assertEqual(1, subset2.shape[0]) self.assertEqual(3332, subset2.shape[2]) self.assertEqual(1878, subset2.shape[1]) box_clipper.update_bbox(x=10, y=10, nx=201, ny=20) subset3, _, _, _ = box_clipper.subset() self.assertEqual(1, subset3.shape[0]) self.assertEqual(201, subset3.shape[2]) self.assertEqual(20, subset3.shape[1]) box_clipper.update_bbox(x=1, y=1, nx=500, ny=300) subset4, _, _, _ = box_clipper.subset() self.assertEqual(1, subset4.shape[0]) self.assertEqual(500, subset4.shape[2]) self.assertEqual(300, subset4.shape[1]) # create a 3d array for testing, z=4, y=3, x=2 data_array2 = np.array([[[1, 2], [ 3, 4, ], [5, 6]], [[7, 8], [9, 10], [11, 12]], [[13, 14], [15, 16], [17, 18]], [[19, 20], [21, 22], [23, 24]]]) box_clipper2 = BoxClipper(ref_array=data_array2) subset5, _, _, _ = box_clipper2.subset() self.assertIsNone(np.testing.assert_array_equal(data_array2, subset5)) self.assertEqual(1, subset5[0, 0, 0]) self.assertEqual(22, subset5[3, 1, 1]) box_clipper2.update_bbox(x=1, y=1, nx=1, ny=2) subset6, _, _, _ = box_clipper2.subset() self.assertEqual(1, subset6[0, 0, 0]) self.assertEqual(13, subset6[2, 0, 0]) self.assertEqual(15, subset6[2, 1, 0])
def _load_domain_tif(self, domain_mask_key='DOMAIN_MASK'): """Load the domain raster Parameters ---------- domain_mask_key : str, optional Key value to load from domain definition (Default value = 'DOMAIN_MASK') Returns ------- None """ tif_filename = os.path.join(self.local_path, self.required_files.get(domain_mask_key)) self.mask_tif = file_io_tools.read_geotiff(tif_filename) self.mask_array = file_io_tools.read_file(tif_filename)
def clip_inputs(clipper, input_list, out_dir='.', pfb_outs=1, tif_outs=0, no_data=NO_DATA) -> None: """clip a list of files using a clipper object Parameters ---------- clipper : Clipper clipper object prepared with full_dim_mask and reference dataset input_list : list list of data files (tif, pfb) to clip from out_dir : str, optional output directory (optional) (Default value = '.') pfb_outs : int, optional write pfb files as outputs (optional) (Default value = 1) tif_outs : int, optional write tif files as outputs (optional) (Default value = 0) no_data : int, optional no_data value for tifs (optional) (Default value = NO_DATA) Returns ------- None """ ref_proj = None if tif_outs: # identify projection ref_proj = clipper.subset_mask.mask_tif.GetProjection() # loop over and clip for data_file in input_list: filename = Path(data_file).stem return_arr, new_geom, _, _ = clipper.subset( file_io_tools.read_file(data_file)) if pfb_outs: file_io_tools.write_pfb( return_arr, os.path.join(out_dir, f'{filename}_clip.pfb')) if tif_outs and new_geom is not None and ref_proj is not None: file_io_tools.write_array_to_geotiff(os.path.join( out_dir, f'{filename}_clip.tif'), return_arr, new_geom, ref_proj, no_data=no_data) del return_arr
def clip_land_cover(self, lat_lon_array, land_cover_file): """Clip the domain land cover data to the bounding box of the mask Parameters ---------- lat_lon_array : numpy.ndarray clipped lat/lon tuple data for the masked area land_cover_file : str land cover file for the domain Returns ------- sa_formatted : numpy.ndarray the clipped data formatted in a 1d output arrray for writing to a .sa file output : numpy.ndarray vegm formatted representation of the data (2d) """ lat_lon_proper = np.char.split(lat_lon_array.astype(str), ' ') data = file_io_tools.read_file(land_cover_file) clipped_data, _, clipped_mask, bbox = self.clipper.subset( data_array=data) #sa_formatted = np.flip(clipped_data, axis=1).flatten() sa_formatted = clipped_data.flatten() sand = 0.16 clay = 0.26 color = 2 # get value of land cover for each coordinate npoints = sa_formatted.shape[0] # make output matrix output = np.zeros((npoints, 25)) output[:, 4] = sand output[:, 5] = clay output[:, 6] = color # assign x values, looping from 1 to x extent, holding y constant output[:, 0] = list(range( 1, clipped_data.shape[2] + 1)) * clipped_data.shape[1] # assign y values, repeating each y value from 1 to y extent for every x output[:, 1] = np.repeat(range(1, clipped_data.shape[1] + 1), clipped_data.shape[2]) # assign lat values output[:, 2] = [latlon[0] for latlon in lat_lon_proper] # assign lon values output[:, 3] = [latlon[1] for latlon in lat_lon_proper] cols = [int(i) + 6 for i in sa_formatted] rows = list(range(npoints)) output[rows, cols] = 1 return sa_formatted, output
def test_compare_box_clips(self): data_array = file_io_tools.read_file(test_files.conus1_dem.as_posix()) my_mask = SubsetMask( test_files.huc10190004.get('conus1_mask').as_posix()) clipper = MaskClipper(subset_mask=my_mask, no_data_threshold=-1) mask_subset, _, _, bbox = clipper.subset(data_array, crop_inner=0) box_clipper = BoxClipper(ref_array=data_array, x=bbox[0], y=bbox[1], nx=bbox[2], ny=bbox[3]) box_subset, _, _, _ = box_clipper.subset() self.assertEqual(mask_subset.shape[0], box_subset.shape[0]) self.assertEqual(mask_subset.shape[1], box_subset.shape[1]) self.assertEqual(mask_subset.shape[2], box_subset.shape[2]) self.assertIsNone( np.testing.assert_array_equal(mask_subset, box_subset))
def __init__(self, tif_file, bbox_val=0): """Create a new instance of SubsetMask Parameters ---------- tif_file : str path to tiff file containing mask bbox_val : int, optional integer value specifying the data value for bounding box cells Returns ------- SubsetMask """ self.mask_tif = read_geotiff(tif_file) self.mask_array = read_file(tif_file) self.bbox_val = bbox_val self.inner_mask = self._find_inner_object() # tight crop self.bbox_mask = self._find_bbox() # bbox crop self.no_data_value = self.mask_tif.GetRasterBand(1).GetNoDataValue() self.inner_mask_edges = self.find_mask_edges(self.inner_mask) # edges self.bbox_edges = self.find_mask_edges(self.bbox_mask) # edges
def clip_latlon(self, lat_lon_file): """Clip the domain lat/lon data to the bounding box of the mask Parameters ---------- lat_lon_file : str lat/lon data for the domain Returns ------- sa_formatted : numpy.ndarray the clipped data formatted in a 1d output arrray for writing to a .sa file clipped_data : numpy.ndarray the raw clipped data (3d) read from 'lat_lon_file' """ data = file_io_tools.read_file(lat_lon_file) clipped_data, _, clipped_mask, bbox = self.clipper.subset( data_array=data) #sa_formatted = np.flip(clipped_data, axis=1).flatten() sa_formatted = clipped_data.flatten() return sa_formatted, clipped_data
def __init__(self, tif_file, bbox_val=0, mask_value=1): """Create a new instance of SubsetMask Parameters ---------- tif_file : str path to tiff file containing mask bbox_val : int, optional integer value specifying the data value for bounding box cells mask_value : int or iterable of ints, optional integer value(s) specifying the data value in the tiff file to consider as the masking value Returns ------- SubsetMask """ self.mask_tif = read_geotiff(tif_file) self.mask_array = read_file(tif_file) try: iter(mask_value) except TypeError: self.mask_array = np.where(self.mask_array == mask_value, 1, 0) else: self.mask_array = np.where(np.isin(self.mask_array, mask_value), 1, 0) if not np.any(self.mask_array): raise Exception( 'Unable to create mask without a single masking location') self.bbox_val = bbox_val self.inner_mask = self._find_inner_object() # tight crop self.bbox_mask = self._find_bbox() # bbox crop self.no_data_value = self.mask_tif.GetRasterBand(1).GetNoDataValue() self.inner_mask_edges = self.find_mask_edges(self.inner_mask) # edges self.bbox_edges = self.find_mask_edges(self.bbox_mask) # edges
def build_tcl(out_file, temp_file, runname, slope_file, solid_file, evap_file, end_time, batches, p=2, q=1, r=1, evap_choice=0, k=0.02849, poros=0.39738, rain=-0.05, rec=0.0, constant=0, init=0.0, initw='bottom', flow='OverlandFlow', start_time=0.0, baseu=1, timestep=1, dump=1, dx=1000., dy=1000., dz=1000., nz=1, dz_scales=None): """Build a tcl control file based on a template Parameters ---------- out_file : str name for output file temp_file : str dict of ParFlow keys and template .tcl lines runname : str name to give domain slope_file : str input slope file name pattern for domain solid_file : str input solid file for domain evap_file : str input PME file for domain end_time : int stop time for simulation batches : list patch numbers in domain p : int, optional (Default value = 2) q : int, optional (Default value = 1) r : int, optional (Default value = 1) evap_choice : int, optional (Default value = 0) k : double, optional (Default value = 0.02849) poros : double, optional (Default value = 0.39738) rain : double, optional (Default value = -0.05) rec : double, optional (Default value = 0.0) constant : double, optional (Default value = 0) init : double, optional (Default value = 0.0) initw : str, optional (Default value = 'bottom') flow : str, optional (Default value = 'OverlandFlow') start_time : double, optional (Default value = 0.0) baseu : int, optional (Default value = 1) timestep : int, optional (Default value = 1) dump : int, optional (Default value = 1) dx : int, optional (Default value = 1000.) dy : int, optional (Default value = 1000.) dz : int, optional (Default value = 1000.) nz : int, optional (Default value = 1) dz_scales : int, optional (Default value = None) Returns ------- None """ # read input file if dz_scales is None: dz_scales = [.5] results, content = read_infile(temp_file) # change the content step by step # change runname results['runname']['vals'][0][-1] = runname # change processors results['Process.Topology.P']['vals'][0][-1] = str(p) results['Process.Topology.Q']['vals'][0][-1] = str(q) results['Process.Topology.R']['vals'][0][-1] = str(r) # change input files if 'slope_x' in slope_file: slope_file_y = slope_file.replace('slope_x', 'slope_y') slope_file_x = slope_file elif 'slopex' in slope_file: slope_file_y = slope_file.replace('slopex', 'slopey') slope_file_x = slope_file elif 'slope_y' in slope_file: slope_file_x = slope_file.replace('slope_y', 'slope_x') slope_file_y = slope_file elif 'slopey' in slope_file: slope_file_x = slope_file.replace('slopey', 'slopex') slope_file_y = slope_file else: print('please check slopefile names') sys.exit() results['file copy -force']['vals'][0][-2] = slope_file_x results['file copy -force']['vals'][1][-2] = slope_file_y results['file copy -force']['vals'][2][-2] = solid_file if evap_choice == 0: results['file copy -force']['vals'][3][ 0] = '#' + results['file copy -force']['vals'][3][0] else: results['file copy -force']['vals'][3][-2] = evap_file # change parameters results['Geom.domain.Perm.Value']['vals'][0][-1] = str(k) results['Geom.domain.Porosity.Value']['vals'][0][-1] = str(poros) # change boundary pressure of top layer if evap_choice == 0: # Comment out evap file results['Solver.EvapTransFile']['vals'][0][-1] = 'False' results['Solver.EvapTrans.FileName']['vals'][0][ 0] = '#' + results['Solver.EvapTrans.FileName']['vals'][0][0] if constant == 1: # Comment out rain rec results['Patch.top.BCPressure.Cycle']['vals'][0][0] = '#' + \ results['Patch.top.BCPressure.Cycle']['vals'][0][0] results['Patch.top.BCPressure.rain.Value']['vals'][0][0] = '#' + \ results['Patch.top.BCPressure.rain.Value'][ 'vals'][ 0][0] results['Patch.top.BCPressure.rec.Value']['vals'][0][0] = '#' + \ results['Patch.top.BCPressure.rec.Value']['vals'][ 0][ 0] # set constant input results['Patch.top.BCPressure.Cycle']['vals'][1][ -1] = '\"constant\"' results['Patch.top.BCPressure.alltime.Value']['vals'][0][-1] = str( rain) # set flow type for top layer results['Patch.top.BCPressure.Type']['vals'][0][-1] = flow else: # Change rain and rec values results['Patch.top.BCPressure.rain.Value']['vals'][0][-1] = str( rain) results['Patch.top.BCPressure.rec.Value']['vals'][0][-1] = str(rec) # comment out constant input results['Patch.top.BCPressure.Cycle']['vals'][1][0] = '#' + \ results['Patch.top.BCPressure.Cycle']['vals'][1][0] results['Patch.top.BCPressure.alltime.Value']['vals'][0][0] = '#' + \ results['Patch.top.BCPressure.alltime.Value'][ 'vals'][0][0] else: # Change evap files results['Patch.top.BCPressure.Cycle']['vals'][1][-1] = '\"constant\"' results['Patch.top.BCPressure.alltime.Value']['vals'][0][-1] = str(0.0) results['Solver.EvapTransFile']['vals'][0][-1] = 'True' results['Solver.EvapTrans.FileName']['vals'][0][-1] = os.path.basename( evap_file) # change initial pressure height results['Geom.domain.ICPressure.Value']['vals'][0][-1] = str(init) results['Geom.domain.ICPressure.RefPatch']['vals'][0][-1] = str(initw) # change timing info results['TimingInfo.BaseUnit']['vals'][0][-1] = str(baseu) results['TimingInfo.StartTime']['vals'][0][-1] = str(start_time) results['TimingInfo.StopTime']['vals'][0][-1] = str(end_time) results['TimingInfo.DumpInterval']['vals'][0][-1] = str(dump) results['TimeStep.Value']['vals'][0][-1] = str(timestep) # finally change domain info results['GeomInput.domaininput.FileName']['vals'][0][ -1] = os.path.basename(solid_file) results['TopoSlopesX.FileName']['vals'][0][-1] = os.path.basename( slope_file_x) results['TopoSlopesY.FileName']['vals'][0][-1] = os.path.basename( slope_file_y) results['pfdist']['vals'][0][-1] = os.path.basename(slope_file_x) results['pfdist']['vals'][1][-1] = os.path.basename(slope_file_y) if evap_choice == 1: results['pfdist']['vals'][2][-1] = os.path.basename(evap_file) else: results['pfdist']['vals'][2][0] = '#' + results['pfdist']['vals'][2][0] # read slope file x into array slope_x = file_io_tools.read_file(slope_file_x) nz0, ny0, nx0 = slope_x.shape results['ComputationalGrid.NX']['vals'][0][-1] = str(nx0) results['ComputationalGrid.NY']['vals'][0][-1] = str(ny0) results['ComputationalGrid.NZ']['vals'][0][-1] = str(nz) results['ComputationalGrid.DX']['vals'][0][-1] = str(dx) results['ComputationalGrid.DY']['vals'][0][-1] = str(dy) results['ComputationalGrid.DZ']['vals'][0][-1] = str(dz) results['dzScale.nzListNumber']['vals'][0][-1] = str(nz) for ni in range(nz): results['Cell.' + str(ni) + '.dzScale.Value']['vals'][0][-1] = str( dz_scales[ni]) # get batch name batch_dict = { 0: 'land', 1: 'ocean', 2: 'land', 3: 'top', 4: 'lake', 5: 'sink', 6: 'bottom', 8: 'stream', 9: 'reservoir' } patch_str = '\"' for batch in batches.rstrip(' ').split(' '): patch_str += batch_dict.get(int(batch), '') + ' ' patch_str += '\"' results['Geom.domain.Patches']['vals'][ 0] = results['Geom.domain.Patches']['vals'][0][:2] + [patch_str] results['BCPressure.PatchNames']['vals'][ 0] = results['BCPressure.PatchNames']['vals'][0][:2] + [patch_str] # write new file for k in results.keys(): locs = results[k]['locs'] vals = results[k]['vals'] for ix, loci in enumerate(locs): content[loci] = ' '.join(vals[ix]) with open(out_file, 'w') as fo: for line in content: fo.write(line + '\n')
def test_read_compare_tif_to_pfb(self): tif_array = file_io_tools.read_file(test_files.conus1_dem) pfb_array = file_io_tools.read_file(test_files.conus1_dem_pfb) self.assertIsNone(np.testing.assert_array_equal(tif_array, pfb_array))
def test_read_pfb(self): results = file_io_tools.read_file(test_files.forcings_pfb) self.assertEqual((24, 41, 41), results.shape) self.assertEqual(290.4337549299417, results[0, 0, 0]) self.assertEqual(292.95937295652664, results[-1, 40, 40])
def test_read_tif(self): results = file_io_tools.read_file(test_files.regression_truth_tif) self.assertEqual(3, len(results.shape), 'read a 2d tiff always returns a 3d array') results3d = file_io_tools.read_file(test_files.forcings_tif) self.assertEqual(3, len(results3d.shape), 'read a 3d tiff always returns a 3d array')