def main(): """ A main function to convert polygon files """ parser = create_arg_parser() args = parser.parse_args() if args.input.endswith('.yaml'): polygons = read_polygons(args.input) if args.output.endswith('.shp'): write_polygons(args.output, polygons) else: raise ValueError("Not supported output file type") elif args.input.endswith('.shp'): polygons = read_polygons(args.input) write_polygons(args.output, polygons) else: raise ValueError("Not supported input file type")
def test_write_polygons_to_shapefile(self): polygons = self.read_test_polygons() fpath_out_name = 'tempout' fpath_out = os.path.join(self.test_dir, fpath_out_name + '.shp') try: write_polygons(fpath_out, polygons) polygons_written = read_polygons(fpath_out) self.assertEqual(polygons, polygons_written) finally: for fpath in [ os.path.join(self.test_dir, fpath_out_name + ext) for ext in ('.dbf', '.prj', '.shp', '.shx') ]: if os.path.exists(fpath): os.remove(fpath)
def bay_delta_hotstart_with_cdec_stations(args): """ Create a hotstart file with USGS cruise data and CDEC data If CDEC data is not given, a constant salinity is set Parameters ---------- args: namespace command line arguments parsed by argparse """ logger = init_logger('bay_delta_hotstart') generator = HotstartGenerator(logger=logger) fpath_mesh = args.hgrid fpath_vgrid = args.vgrid fpath_elev_polygons = args.elev fpath_usgs_cruise = args.usgs_data fpath_usgs_cruise_stations = args.usgs_station fpath_cdec_data = args.cdec_data fpath_cdec_stations = args.cdec_station fpath_estuary_polygons = args.estuary fpath_hotstart = args.hotstart polygons_estuary = read_polygons(fpath_estuary_polygons) if fpath_elev_polygons is not None: polygons_elev = read_polygons(fpath_elev_polygons) else: polygons_elev = None usgs_stations = read_stations(fpath_usgs_cruise_stations) usgs_data = read_station_data(fpath_usgs_cruise) for row in usgs_data: if 'station number' in row: row['id'] = row['station number'] del row['station number'] if fpath_cdec_stations is not None: cdec_stations = read_stations(fpath_cdec_stations) else: cdec_stations = None if fpath_cdec_data is not None: cdec_data = read_station_data(fpath_cdec_data) else: cdec_data = None # Load mesh information if not os.path.exists(fpath_mesh): logger.error("A mesh file not found: %s", fpath_mesh) raise ValueError("A mesh file not found") if not os.path.exists(fpath_vgrid): logger.error("A vertical mesh file not found: %s", fpath_vgrid) raise ValueError("A vertical mesh file not found") logger.info("Reading a mesh...") mesh = read_mesh(fpath_mesh, fpath_vgrid) logger.info("Setting up initializers...") # Salinity ocean_salt = args.ocean_salt if cdec_data is not None: generator.gen_salt = \ RegionalInitializer(mesh, polygons_estuary, {'default': ocean_salt, 'bay': NearestNeighborInitializer(mesh, usgs_stations, usgs_data, 'salinity'), 'delta': NearestNeighborInitializer(mesh, cdec_stations, cdec_data, 'salinity'), }) else: delta_salt = args.delta_salt generator.gen_salt = \ RegionalInitializer(mesh, polygons_estuary, {'default': ocean_salt, 'bay': NearestNeighborInitializer(mesh, usgs_stations, usgs_data, 'salinity'), 'delta': delta_salt }) # Temperature generator.gen_temp = RegionalInitializer(mesh, polygons_estuary, {'default': 12., 'bay': NearestNeighborInitializer(mesh, usgs_stations, usgs_data, 'temperature'), 'delta': 10.5 }) # Elevation if polygons_elev is not None: generator.gen_elev = RegionalInitializer(mesh, polygons_elev, {'default': 0.96, 'ccfb': 0.525}) else: generator.gen_elev = None # Create hotstart file logger.info("Start creating a hotstart file...") generator.create_hotstart(mesh, fpath_hotstart)
def vgrid_gen(hgrid, vgrid_out, eta, minmaxlayerfile, ngen, maxiter, theta, b, hc, dx2fac=20.0, curvewgt=100.0, foldwgt=20., foldfrac=0.35, archive_nlayer=None, nlayer_gr3=None): from numlayer import tabu_numlayer from lsc2 import default_num_layers from vgrid_opt2 import * print("Reading the mesh ") mesh = read_mesh(hgrid) h0 = mesh.nodes[:, 2] depth = eta + h0 if archive_nlayer == 'out': print("Reading the polygons...") polygons = read_polygons(minmaxlayerfile) minlayer = np.ones_like(h0, dtype='int') #minlayer[:] = 8 # todo need polygons maxlayer = np.ones_like(h0, dtype='int') * 10000 dztarget = np.full_like(h0, 100., dtype='d') #maxlayer[:] = 31 print("Assign min/max layers to nodes based on polygons...") for polygon in polygons: box = [polygon.bounds[i] for i in (0, 2, 1, 3)] candidates = mesh.find_nodes_in_box(box) n_layers_min = int(polygon.prop['minlayer']) n_layers_max = int(polygon.prop['maxlayer']) dz0 = float(polygon.prop['dz_target']) for node_i in candidates: if polygon.intersects(mesh.nodes[node_i, :2]): minlayer[node_i] = n_layers_min maxlayer[node_i] = n_layers_max dztarget[node_i] = dz0 if np.any(np.isnan(minlayer)): print(np.where(np.isnan(minlayer))) raise ValueError('Nan value in minlayer') print("Creating vertical grid...") #meshsmooth = read_mesh("hgrid_depth_smooth.gr3") hsmooth = mesh.nodes[:, 2] etazero = 0. assert len(hsmooth) == len(h0) # todo: was eta and h0 dztarget = dztarget * 0 + 1. dztarget = 0.6 + 0.4 * (hsmooth - 12.) / (22. - 12.) dztarget = np.minimum(1.0, dztarget) dztarget = np.maximum(0.65, dztarget) #dztarget2 = np.minimum(1.3,0.65+0.65*(2.-hsmooth)) #dztarget[hsmooth<2.] = dztarget2[hsmooth<2.] nlayer_default = default_num_layers(eta, hsmooth, minlayer, maxlayer, dztarget) #print nlayer_default #nlayer = deap_numlayer(depth,mesh.edges,nlayer_default,minlayer,ngen) nlayer = tabu_numlayer(depth, mesh.edges, nlayer_default, minlayer, maxlayer, ngen) print depth.shape print nlayer.shape if archive_nlayer == "out": print "writing out number of layers" write_mesh(mesh, nlayer_gr3.replace(".gr3", "_default.gr3"), node_attr=nlayer_default) write_mesh(mesh, nlayer_gr3, node_attr=nlayer) write_mesh(mesh, nlayer_gr3.replace(".gr3", "_dztarget.gr3"), node_attr=dztarget) elif archive_nlayer == "in": nlayer_mesh = read_mesh(nlayer_gr3) dztarget = read_mesh(nlayer_gr3.replace(".gr3", "_dztarget.gr3")).nodes[:, 2] nlayer = nlayer_mesh.nodes[:, 2].astype('i') if long(nlayer_mesh.n_nodes()) != long(mesh.n_nodes()): raise ValueError( "NLayer gr3 file (%s)\nhas %s nodes, hgrid file (%s) has %s" % (nlayer_gr3, nlayer_mesh.n_nodes(), hgrid, mesh.n_nodes())) #print("Reading the polygons for dz_target...") #polygons = read_polygons(minmaxlayerfile) #dztarget = np.full_like(h0, np.nan, dtype='d') #maxlayer[:] = 31 #print("Assign dz_target to nodes based on polygons...") #for polygon in polygons: # box = [polygon.bounds[i] for i in (0, 2, 1, 3)] # candidates = mesh.find_nodes_in_box(box) # dz0 = float(polygon.prop['dz_target']) # for node_i in candidates: # if polygon.intersects(mesh.nodes[node_i, :2]): # dztarget[node_i] = dz0 else: raise ValueError("archive_nlayer must be one of 'out', 'in' or None") #np.savez("savevar.npz",nlayer,nlayer_default,depth,h0) sigma, nlayer_revised = gen_sigma(nlayer, dztarget, eta, h0, theta, b, hc, mesh=mesh) print "sigma shape" print sigma.shape nlayer = nlayer_revised nlevel = nlayer + 1 #fsigma = flip_sigma(sigma) #print fsigma[0,:] vmesh = SchismLocalVerticalMesh(flip_sigma(sigma)) vgrid0 = vgrid_out.replace(".in", "_int.in") write_vmesh(vmesh, vgrid0) # Gradient based stuff nodes = mesh.nodes edges = mesh.edges x = nodes[:, 0:2] # x positions for xsect. These have no analog in 3D sidelen2 = np.sum((x[edges[:, 1], :] - x[edges[:, 0], :])**2., axis=1) #nodemasked = (depth < 0.75) #| (nlayer <= 2) #todo hardwire was 0.75 nodemasked = (depth < 0.75) #| (nlayer <= 2) sidemasked = nodemasked[edges[:, 0]] | nodemasked[edges[:, 1]] gradmat = gradient_matrix(mesh, sidelen2, sidemasked) print "Nodes excluded: %s" % np.sum(nodemasked) print "Sides excluded: %s" % np.sum(sidemasked) zcor = vmesh.build_z(mesh, eta)[:, ::-1] # todo: test this #zcor[depth < 0., :] = np.nan # todo: why is kbps-1 so common? What does it mean and why the funny offset nvlevel = (vmesh.n_vert_levels() - vmesh.kbps) #nvlevel[depth<0] = 0 nlayer, ndx = index_interior(zcor, nodemasked, nvlevel) xvar = zcor[ndx >= 0].flatten() xvarbase = xvar.copy() zcorold = zcor.copy() zmin = np.nanmin(zcorold, axis=1) deptharr = np.tile(zmin, (zcorold.shape[1], 1)).T zcorold = np.where(np.isnan(zcorold), deptharr, zcorold) do_opt = False if do_opt: curvewgt = np.zeros_like(zcorold) + curvewgt #todo: hardwire #bigcurvewgt = 4 #for iii in range(zcor.shape[0]): # nlev = nlayer[iii]+1 # curvewgt[iii,0:nlev]+=np.maximum(np.linspace(bigcurvewgt-nlev,bigcurvewgt-nlev,nlev),1) # todo ata = laplace2(mesh, nodemasked, sidemasked) href_hess, grad_hess, laplace_hess = hess_base( xvar, zcorold, mesh, nlayer, ndx, eta, depth, gradmat, sidelen2, nodemasked, sidemasked, ata, dx2fac, curvewgt, foldwgt, foldfrac) zcor1 = mesh_opt(zcorold, mesh, nlayer, ndx, eta, depth, gradmat, sidelen2, nodemasked, sidemasked, ata, dx2fac, curvewgt, foldwgt, foldfrac, href_hess, grad_hess, laplace_hess) else: zcor1 = zcorold sigma1 = z_sigma(zcor1) nlevel = nlayer + 1 for i in range(len(depth)): nlev = nlevel[i] if depth[i] <= 0: sigma1[i, 0:nlev] = np.linspace(0, -1.0, nlev) sigma1[i, nlev:] = np.nan vmesh1 = SchismLocalVerticalMesh(flip_sigma(sigma1)) print("Writing vgrid.in output file...") write_vmesh(vmesh1, vgrid_out) print "Done"
def vgrid_gen(hgrid, vgrid_out, eta, minmaxlayerfile, archive_nlayer=None, nlayer_gr3=None): from lsc2 import default_num_layers meshfun = BilinearMeshDensity() dummydepth = np.linspace(0, 14, 15) dummyk = np.linspace(0, 14, 15) dummyout = meshfun.depth(dummyk, dummydepth, 0.) print("Reading the mesh ") mesh = read_mesh(hgrid) h0 = mesh.nodes[:, 2] places_on = np.array( [[626573.490000, 4260349.590000], [626635.000000, 4260391.7]], dtype='d') dists_on = np.min(scipy.spatial.distance.cdist(mesh.nodes[:, 0:2], places_on), axis=1) print(np.where(dists_on < 100)) depth = eta + h0 print("Reading the polygons...") polygons = read_polygons(minmaxlayerfile) minlayer = np.ones_like(h0, dtype='int') #minlayer[:] = 8 # todo need polygons maxlayer = np.ones_like(h0, dtype='int') * 10000 dztarget = np.full_like(h0, 100., dtype='d') print("Assign min/max layers to nodes based on polygons...") for polygon in polygons: box = [polygon.bounds[i] for i in (0, 2, 1, 3)] candidates = mesh.find_nodes_in_box(box) n_layers_min = int(polygon.prop['minlayer']) n_layers_max = int(polygon.prop['maxlayer']) dz0 = float(polygon.prop['dz_target']) for node_i in candidates: if polygon.intersects(mesh.nodes[node_i, :2]): minlayer[node_i] = n_layers_min maxlayer[node_i] = n_layers_max dztarget[node_i] = dz0 if np.any(np.isnan(minlayer)): print((np.where(np.isnan(minlayer)))) raise ValueError('Nan value in minlayer') if archive_nlayer == 'out': dztarget = 0. #todo: these will ruin the code if fix_minmax: minlayer = minlayer * 0 + fixed_min maxlayer = maxlayer * 0 + fixed_max #np.max(maxlayer) xdummy = 0. nlayer_default = default_num_layers(xdummy, eta, h0, minlayer, maxlayer, dztarget, meshfun) nlayer = nlayer_default if archive_nlayer == "out": print("writing out number of layers") write_mesh(mesh, nlayer_gr3.replace(".gr3", "_default.gr3"), node_attr=nlayer_default) write_mesh(mesh, nlayer_gr3, node_attr=nlayer) #write_mesh(mesh,nlayer_gr3.replace(".gr3","_dztarget.gr3"),node_attr=dztarget) elif archive_nlayer == "in": nlayer_mesh = read_mesh(nlayer_gr3) #dztarget=read_mesh(nlayer_gr3.replace(".gr3","_dztarget.gr3")).nodes[:,2] nlayer = nlayer_mesh.nodes[:, 2].astype('i') if int(nlayer_mesh.n_nodes()) != int(mesh.n_nodes()): raise ValueError( "NLayer gr3 file (%s)\nhas %s nodes, hgrid file (%s) has %s" % (nlayer_gr3, nlayer_mesh.n_nodes(), hgrid, mesh.n_nodes())) else: raise ValueError("archive_nlayer must be one of 'out', 'in' or None") # inclusion of minlayer and maxlayer has to do with experiment regenerating # layers after smoothing # this will ruin code generally, and if the experiment goes well we need to make sure this is available when archive_nlayer="in" if fix_minmax: minlayer = nlayer * 0 + fixed_min maxlayer = nlayer * 0 + fixed_max #np.max(maxlayer) sigma2, nlayer_revised = gen_sigma(nlayer, minlayer, maxlayer, eta, h0, mesh, meshfun) print("Returned nlayer revised: {}".format(np.max(nlayer_revised))) nlayer = nlayer_revised nlevel = nlayer + 1 vmesh = SchismLocalVerticalMesh(flip_sigma(sigma2)) #vgrid0 = vgrid_out.replace(".in", "_int.in") #write_vmesh(vmesh, vgrid0) #vmesh1 = SchismLocalVerticalMesh(flip_sigma(sigma1)) print("Writing vgrid.in output file...") write_vmesh(vmesh, vgrid_out) print("Done")
def test_polygon_reader_yaml(self): fpath = os.path.join(self.test_dir, 'polygons.yaml') polygons = read_polygons(fpath) self.check_polygons(polygons)
def read_test_polygons(self): fpath = os.path.join(self.test_dir, 'polygons.yaml') return read_polygons(fpath)