def sourceAreaMask(fmsk, xO, yO): # Read the mask raster info. Rdict = readNumpyZTile(fmsk) R = Rdict['R'] R_dims = np.array(np.shape(R)) ROrig = Rdict['GlobOrig'] dPx = Rdict['dPx'] Rdict = None dPx = entry2Int(dPx) # Resolution as a single number idM = (R[::-1, :] > 0) # True where the mask is nonzero. R = None # Clear memory ixO = (xO / dPx).astype(int) # indecies of particle origins. iyO = (yO / dPx).astype(int) iPrt = np.zeros(np.shape(ixO), bool) # Tmp array of particles, all False. iPrt[:] = idM[iyO[:], ixO[:]] # Set True only the ones where particles exist. print(' From {} particles {} are contained within the mask.'.format( len(xO), np.sum(iPrt))) idM = None ixO = None iyO = None # Clear. return iPrt
def processBuildingType(fname, ds, vars, dims): buildingTypeDict = readNumpyZTile(fname, verbose=False) buildingTypeR = buildingTypeDict['R'][::-1, :] buildingTypeDPx = buildingTypeDict['dPx'] buildingTypeNPx = np.shape(buildingTypeR) if ('building_type' in vars): ds.variables['building_type'][:] = buildingTypeR return ds.variables['building_type'] else: x_dim = createXDim(ds, buildingTypeNPx, buildingTypeDPx, dims) y_dim = createYDim(ds, buildingTypeNPx, buildingTypeDPx, dims) buildingTypeNCVar = createNetcdfVariable(ds, buildingTypeR, 'building_type', 0, 'm', 'b', ('y', 'x'), False, False, fill_value=-127, verbose=False) buildingTypeNCVar.long_name = "building type classification" return buildingTypeNCVar
def processSurfaceFraction(fname, ds, vars, dims): surfaceFractionDict = readNumpyZTile(fname, verbose=False) surfaceFractionR = surfaceFractionDict['R'][::-1, :, :] surfaceFractionDPx = surfaceFractionDict['dPx'] surfaceFractionNPx = np.shape(surfaceFractionR) # Same here as in buildings_3d, idk why this has to be done for 3D arrays surfaceFractionR = np.swapaxes(surfaceFractionR, 0, 2) surfaceFractionR = np.swapaxes(surfaceFractionR, 2, 1) if ('surface_fraction' in vars): ds.variables['surface_fraction'][:] = surfaceFractionR return ds.variables['surface_fraction'] else: x_dim = createXDim(ds, surfaceFractionNPx, surfaceFractionDPx, dims) y_dim = createYDim(ds, surfaceFractionNPx, surfaceFractionDPx, dims) znsurface_fraction_dim = createZnsurfaceFractionDim( ds, surfaceFractionNPx, surfaceFractionDPx, dims) surfaceFractionNCVar = createNetcdfVariable( ds, surfaceFractionR, 'surface_fraction', 0, 'm', 'f4', ('nsurface_fraction', 'y', 'x'), False, False, fill_value=-9999., verbose=False) surfaceFractionNCVar.long_name = "surface fraction" return surfaceFractionNCVar
def processLAD(fname, ds, vars, dims): ladDict = readNumpyZTile(fname, verbose=False) ladR = ladDict['R'] ladDPx = ladDict['dPx'] ladNPx = np.shape(ladR) # Same here as in buildings_3d, idk why this has to be done for 3D arrays print('1 lad shape = {}'.format(np.shape(ladR))) ladR = np.swapaxes(ladR, 0, 2) print('2 lad shape = {}'.format(np.shape(ladR))) if ('lad' in vars): print(' lad is in vars ') ds.variables['lad'][:] = ladR return ds.variables['lad'] else: print(' lad is NOT in vars ') x_dim = createXDim(ds, ladNPx, ladDPx, dims) y_dim = createYDim(ds, ladNPx, ladDPx, dims) zlad_dim = createZLADDim(ds, ladNPx, ladDPx, dims) ladNCVar = createNetcdfVariable(ds, ladR, 'lad', 0, 'm', 'f4', ('zlad', 'y', 'x'), False, False, fill_value=-9999., verbose=False) ladNCVar.long_name = "basal area density" return ladNCVar
def processOrography(fname, ds, vars, dims): # Write orography data to given ds oroDict = readNumpyZTile(fname, verbose=False) oroR = oroDict['R'][::-1, :] oroDPx = oroDict['dPx'] oroNPx = np.shape(oroR) if ('zt' in vars): ds.variables['zt'][:] = oroR return ds.variables['zt'] else: # Create new dimensions or check if the orography matches old ones x_dim = createXDim(ds, oroNPx, oroDPx, dims) y_dim = createYDim(ds, oroNPx, oroDPx, dims) oroNCVar = createNetcdfVariable(ds, oroR, 'zt', 0, 'm', 'f4', ('y', 'x'), False, False, fill_value=-9999., verbose=False) oroNCVar.long_name = "terrain_height" return oroNCVar
def processWaterType(fname, ds, vars, dims): waterTypeDict = readNumpyZTile(fname, verbose=False) waterTypeR = waterTypeDict['R'][::-1, :] waterTypeDPx = waterTypeDict['dPx'] waterTypeNPx = np.shape(waterTypeR) if ('water_type' in vars): ds.variables['water_type'][:] = waterTypeR return ds.variables['water_type'] else: x_dim = createXDim(ds, waterTypeNPx, waterTypeDPx, dims) y_dim = createYDim(ds, waterTypeNPx, waterTypeDPx, dims) waterTypeNCVar = createNetcdfVariable(ds, waterTypeR, 'water_type', 0, 'm', 'b', ('y', 'x'), False, False, fill_value=-127, verbose=False) waterTypeNCVar.long_name = "water type classification" return waterTypeNCVar
def processStreetType(fname, ds, vars, dims): streetTypeDict = readNumpyZTile(fname, verbose=False) streetTypeR = streetTypeDict['R'][::-1, :] streetTypeDPx = streetTypeDict['dPx'] streetTypeNPx = np.shape(streetTypeR) if ('street_type' in vars): ds.variables['street_type'][:] = streetTypeR return ds.variables['street_type'] else: x_dim = createXDim(ds, streetTypeNPx, streetTypeDPx, dims) y_dim = createYDim(ds, streetTypeNPx, streetTypeDPx, dims) streetTypeNCVar = createNetcdfVariable(ds, streetTypeR, 'street_type', 0, 'm', 'b', ('y', 'x'), False, False, fill_value=-127, verbose=False) streetTypeNCVar.long_name = "street type classification" return streetTypeNCVar
def processPavementType(fname, ds, vars, dims): pavementTypeDict = readNumpyZTile(fname, verbose=False) pavementTypeR = pavementTypeDict['R'][::-1, :] pavementTypeDPx = pavementTypeDict['dPx'] pavementTypeNPx = np.shape(pavementTypeR) if ('pavement_type' in vars): ds.variables['pavement_type'][:] = pavementTypeR return ds.variables['pavement_type'] else: x_dim = createXDim(ds, pavementTypeNPx, pavementTypeDPx, dims) y_dim = createYDim(ds, pavementTypeNPx, pavementTypeDPx, dims) pavementTypeNCVar = createNetcdfVariable(ds, pavementTypeR, 'pavement_type', 0, 'm', 'b', ('y', 'x'), False, False, fill_value=-127, verbose=False) pavementTypeNCVar.long_name = "pavement type classification" return pavementTypeNCVar
def processBuildingIDs(fname, ds, vars, dims): buildIDDict = readNumpyZTile(fname, verbose=False) buildIDR = buildIDDict['R'][::-1, :] buildIDDPx = buildIDDict['dPx'] buildIDNPx = np.shape(buildIDR) if ('building_id' in vars): ds.variables['building_id'][:] = buildIDR return ds.variables['building_id'] else: x_dim = createXDim(ds, buildIDNPx, buildIDDPx, dims) y_dim = createYDim(ds, buildIDNPx, buildIDDPx, dims) buildIDNCVar = createNetcdfVariable(ds, buildIDR, 'building_id', 0, 'm', 'i4', ('y', 'x'), False, False, fill_value=-9999, verbose=False) buildIDNCVar.long_name = "building id numbers" return buildIDNCVar
def processVegetationType(fname, ds, vars, dims): vegetationTypeDict = readNumpyZTile(fname, verbose=False) vegetationTypeR = vegetationTypeDict['R'][::-1, :] vegetationTypeDPx = vegetationTypeDict['dPx'] vegetationTypeNPx = np.shape(vegetationTypeR) if ('vegetation_type' in vars): ds.variables['vegetation_type'][:] = vegetationTypeR return ds.variables['vegetation_type'] else: x_dim = createXDim(ds, vegetationTypeNPx, vegetationTypeDPx, dims) y_dim = createYDim(ds, vegetationTypeNPx, vegetationTypeDPx, dims) vegetationTypeNCVar = createNetcdfVariable(ds, vegetationTypeR, 'vegetation_type', 0, 'm', 'b', ('y', 'x'), False, False, fill_value=-127, verbose=False) vegetationTypeNCVar.long_name = "vegetation type classification" return vegetationTypeNCVar
def processBuildings(fname, ds, vars, dims): buildDict = readNumpyZTile(fname, verbose=False) buildR = buildDict['R'][::-1, :] buildDPx = buildDict['dPx'] buildNPx = np.shape(buildR) buildLOD = len(buildNPx) - 1 # 1=2D height field, 2=3D mask if (buildLOD == 1): # Save as a 2D building height array if ('buildings_2d' in vars): ds.variables['buildings_2d'][:] = buildR return ds.variables['buildings_2d'] else: x_dim = createXDim(ds, buildNPx, buildDPx, dims) y_dim = createYDim(ds, buildNPx, buildDPx, dims) buildNCVar = createNetcdfVariable(ds, buildR, 'buildings_2d', 0, 'm', 'f4', ('y', 'x'), False, False, fill_value=-9999., verbose=False) buildNCVar.long_name = "building_height" buildNCVar.lod = int(buildLOD) return buildNCVar elif (buildLOD == 2): # Save as a 3D boolean mask array #topo=fillTopographyArray(buildR, buildNPx, buildDPx, int) # I'm quite not sure why axes swapping has to be done but at least it works topo = np.swapaxes(buildR, 0, 2) if ('buildings_3d' in vars): ds.variables['buildings_3d'][:] = topo return ds.variables['buildings_3d'] else: x_dim = createXDim(ds, buildNPx, buildDPx, dims) y_dim = createYDim(ds, buildNPx, buildDPx, dims) z_dim = createZDim(ds, buildNPx, buildDPx, dims) buildNCVar = createNetcdfVariable(ds, topo, 'buildings_3d', 0, 'm', 'b', ('z', 'y', 'x'), False, False, fill_value=-127, verbose=False) buildNCVar.long_name = "building_flag" buildNCVar.lod = int(buildLOD) return buildNCVar else: raise ValueError( "invalid number of dimensions in buildings array: {}".format( buildLOD + 1))
#==========================================================# parser = argparse.ArgumentParser(prog='rasterToNetCdf.py') parser.add_argument("-f", "--filename", type=str, help="Name of the input topography raster data file.") parser.add_argument("-fo", "--fileout", type=str, help="Name of the output NetCDF file.", default='output.ncdf') parser.add_argument("-N", "--NdZ", type=int, help="Number of grid points in z direction. Leave empty to calculate automatically.") parser.add_argument("-dz", "--dZ", type=float, help="Resolution of z axis. Defaults to resolution of N axis.") parser.add_argument("-flat", "--flatarray", action="store_true", help="Save as an 2D array instead of a 3D mask.", default=False) parser.add_argument("-vn", "--varname", type=str, help="Name of the variable in NetCDF. Default 'buildings_0'.", default='buildings_0') parser.add_argument("-c", "--compress", help="Compress netCDF variables with zlib.", action="store_true", default=False) args = parser.parse_args() writeLog( parser, args ) #==========================================================# # Read input raster data file. Rdict = readNumpyZTile(args.filename) Rtopo = Rdict['R'] Rdims = np.shape(Rtopo) Rdpx = Rdict['dPx'] # Create a 3D mask instead of an 2D array mask = not(args.flatarray) # Set z axis resolution if (args.dZ and mask): Rdpx = np.append(Rdpx, args.dZ) elif (mask): Rdpx = np.append(Rdpx, Rdpx[0]) # Set vertical grid dimensions if (args.NdZ and mask):
# Rename ... that's all. filename = args.filename filemask = args.filemask printOnly = args.printOnly saveOn = args.save # save the fig # = = = = = = = = = = = = = = = = = = = = = = = = = = = = # # xO := origin coords. # xt := target coords. # ut := target speed try: Fp, X, Y, Z, C, IDict = readNumpyZFootprint( filename, True ) # IdsOn=True except: sys.exit(' Could not read the footprint file: {}'.format(filename)) try: Rdict = readNumpyZTile( filemask ) Rm = Rdict['R'] Rmdims = np.array(np.shape(R)) RmOrig = Rdict['GlobOrig'] dPx = Rdict['dPx'] Rdict = None except: sys.exit(' Could not read the mask file: {}'.format(filemask)) # To unify the treatment, let's add 100% to the IDict. if(not IDict): IDict = {} # In case IDict comes in as <None> IDict[100] = np.ones( np.shape(Fp) , bool ) # All true Nm = int(np.max(Rm))+1 # Number of different mask ID's (assuming first is zero)
absOn = args.abs limsOn = args.lims gridOn = args.grid infoOnly = args.infoOnly labels = args.labels footprintOn = args.footprint save = args.save plt.rc('xtick', labelsize=14) #plt.rc('ytick.major', size=10) plt.rc('ytick', labelsize=14) #plt.rc('ytick.minor', size=6) plt.rc('axes', titlesize=18) if (not footprintOn): Rdict = readNumpyZTile(rasterfile) Rdict = initRdict(Rdict) R = Rdict['R'] Rdims = np.array(np.shape(R)) ROrig = Rdict['GlobOrig'] dPx = Rdict['dPx'] gridRot = Rdict['gridRot'] ROrigBL = None if ('GlobOrigBL' in Rdict): ROrigBL = Rdict['GlobOrigBL'] Rdict = None else: R, X, Y, Z, C = readNumpyZFootprint(rasterfile) Rdims = np.array(np.shape(R)) ROrig = np.zeros(2) dPx = np.array([(Y[1, 0] - Y[0, 0]), (X[0, 1] - X[0, 0])]) # dN, dE
# Write the footprint in npz format. IDict = {} IDict[50] = id50[::-1,:]; IDict[75] = id75[::-1,:]; IDict[90] = id90[::-1,:] writeNumpyZFootprint(fileout, Ft[::-1,:], Xt, Yt, Zt, Ct, IDict ) # Write also the Kormann-Meixner footprint IDict = {} IDict[75] = id75_km[::-1,:]; IDict[90] = id90_km[::-1,:] writeNumpyZFootprint(fileout+'_KormannMeixner', F_km[::-1,:], Xt, Yt, Zt, Ct, IDict ) if( vtkOn ): # Footprint to VTK-format together with the complete topography. Ftmp = np.zeros( np.shape(Ft), float ) Rdict = readNumpyZTile( filetopo ) R = Rdict['R'] Rdims = np.array(np.shape(R)) ROrig = Rdict['GlobOrig'] dPx = Rdict['dPx'] Rdict = None if( all(Rdims != np.shape(Xt)) ): sys.exit(' Error! Mismatch Topo_dims={} vs. Fp_dims={}'.format(Rdims,np.shape(Xt))) f_vtk = vtkWriteHeaderAndGridStructured2d( Xt, Yt, R[::-1,:], fileout, 'Footprint'); R=None f_vtk = vtkWritePointDataHeader( f_vtk, Ft, 5 ) # ======= Write 100% Ft ================ f_vtk = vtkWritePointDataStructured2D( f_vtk, Ft , Xt, 'fp' )
help="Print the resulting raster data.") parser.add_argument("-pp", "--printOnly", help="Only print the resulting data. Don't save.",\ action="store_true", default=False) args = parser.parse_args() writeLog(parser, args, args.printOnly) #==========================================================# filename = args.filename fileout = args.fileout Nbd = args.Nbidial beta = np.array(args.beta) printOn = args.printOn printOnly = args.printOnly #==========================================================# # Read the raster tile to be processed. Rdict = readNumpyZTile(filename) R = Rdict['R'] Rdims = np.array(np.shape(R)) ROrig = Rdict['GlobOrig'] print(' Rdims = {} '.format(Rdims)) print(' ROrig = {} '.format(ROrig)) idm = np.isnan(R) Rt2 = R.copy() # Make a copy if (Nbd > 0): for i in range(Nbd): idm = sn.binary_dilation(idm) print('iter {}: number of nonzeros = {} '.format( i, np.count_nonzero(idm)))
def processAerosolEmissionValues(emiStr, fname, emiDmid, ds, vars, dims): # Aerosol emission values with lod of 2 # This is far from ready... # Everything is hardcoded etc. sourceDict = readNumpyZTile(fname, verbose=False) sourceR = sourceDict['R'].T[:, ::-1] sourceDPx = sourceDict['dPx'] sourceNPx = np.shape(sourceR) if ('aerosol_emission_values' in vars): raise NotImplementedError( "Updating aerosol_emission_values not implemented yet.") else: try: emiDmids = map(float, emiDmid.split(",")) except TypeError: print( "Error: invalid value for aerosol_emission_dmid in configuration file, expected a comma-delimited list" ) exit(1) try: emiVals = map(float, emiStr.split(",")) except TypeError: print( "Error: invalid value for aerosol_emission_values in configuration file, expected a comma-delimited list" ) exit(1) x_dim = createXDim(ds, sourceNPx, sourceDPx, dims) y_dim = createYDim(ds, sourceNPx, sourceDPx, dims) ncat_dim = createNcatDim(ds, [1], dims) nbins = len(emiDmids) # FIX aerosol_emission_values = np.zeros( [nbins, 1, sourceNPx[1], sourceNPx[0]], dtype=float) - 9999.9 for i in range(nbins): aerosol_emission_values[i, 0, sourceR.T == 1] = emiVals[i] dmid_Dim = createNetcdfVariable(ds, emiDmids, 'Dmid', len(emiDmids), '', 'f4', ('Dmid', ), parameter=True, verbose=False) dims.append('Dmid') emiValVar = createNetcdfVariable(ds, aerosol_emission_values, 'aerosol_emission_values', 0, 'm', 'f4', ( 'Dmid', 'ncat', 'y', 'x', ), False, False, fill_value=-9999., verbose=False) # FIX: dimensions emiValVar.long_name = 'aerosol emission values' emiValVar.standard_name = 'aerosol_emission_values' emiValVar.lod = 2 # FIX: lod emiValVar.unit = '#/m2/s' return emiValVar