def write_regridded_data_to_shapefile(dstfield): """ :param dstfield: The ESMF field object containing a mesh to write to shapefile. :type dstfield: :class:`ESMF.api.field.Field` :returns: Path to the output shapefile. :rtype: str """ # turn the shapefile into an OCGIS field and get the spatial information ofield = ocgis.RequestDataset(PATH_SHP).get() # get the time dimension from the original netCDF file otime = ocgis.RequestDataset(PATH_PR).get().temporal # create an OCGIS variable from the regridded data values pr = ocgis.Variable(name='pr', value=np.array(dstfield.reshape(1, otime.shape[0], 1, 1, ofield.shape[-1]))) # this holds our variables vc = ocgis.VariableCollection([pr]) # we want to maintain the original shapefile data, but it needs to reshaped to account for the new time dimension. for var in ofield.variables.itervalues(): newvalue = np.zeros(pr.shape, dtype=var.dtype) newvalue[:] = var.value newvar = ocgis.Variable(name=var.name, value=newvalue) vc[newvar.name] = newvar # combine the spatial data with time and the regridded values ofield2 = ocgis.Field(temporal=otime, spatial=ofield.spatial, variables=vc) # write this to shapefile path_out_shp = ocgis.OcgOperations(dataset=ofield2, output_format='shp', prefix='pr_catchments', add_auxiliary_files=False).execute() return path_out_shp
value=[10, 20, 30], dimensions='nsx') var_y = ocgis.Variable(name='nonstandard_y', value=[40, 50, 60, 70], dimensions='nsy') var_t = ocgis.Variable(name='nonstandard_time', value=[1, 2, 3], units='days since 2000-1-1', attrs={'calendar': 'standard'}, dimensions='nst') data_dimensions = [ var_t.dimensions[0], var_x.dimensions[0], var_y.dimensions[0] ] var_data = ocgis.Variable(name='some_data', dimensions=data_dimensions) vc = ocgis.VariableCollection(variables=[var_x, var_y, var_t, var_data]) vc.write(OUT_NC) ######################################################################################################################## # This metadata is not self-describing. Hence, no data or coordinate variables are interpretable. OpenClimateGIS will # use standard names for coordinate variables, but these names are not standard! rd = ocgis.RequestDataset(OUT_NC) try: assert rd.variable except ocgis.exc.NoDataVariablesFound: pass ######################################################################################################################## # Construct the dimension map as an object.
# Will hold node coordinates nodeCoords = np.zeros((nodeCount, 2), dtype=np.float32) # Insert the node coordinates nodeCoords[:, 0] = esmf_corners_x.flatten() nodeCoords[:, 1] = esmf_corners_y.flatten() # Get the center coordinates centerCoords = np.zeros((elementCount, 2), dtype=np.float32) centerCoords[:, 0] = grid.x.v().flatten() centerCoords[:, 1] = grid.y.v().flatten() # Number of nodes per element. Always four corners in this case numElementConn = np.ones(elementCount, dtype=np.int32) * 4 # Write out the data vc = ocgis.VariableCollection() nodeCoordsV = ocgis.Variable(name='nodeCoords', value=nodeCoords, dimensions=['nodeCount', 'coordDim'], attrs={'units': 'degrees'}, parent=vc) elementConnV = ocgis.Variable( name='elementConn', value=elementConn, dimensions=['elementCount', 'maxNodePElement'], attrs={'long_name': 'Node indices that define the element connectivity'}, parent=vc) numElementConnV = ocgis.Variable( name='numElementConn', value=numElementConn, dimensions=['elementCount'],