def __init__(self, clone_map): pcraster.framework.DynamicModel.__init__(self) pcraster.framework.MonteCarloModel.__init__(self) pcraster.setclone(clone_map) self.nr_state_variables = nr_state_variables self.process = psutil.Process(os.getpid()) self.log_file = file("memory_report.col", "w")
def __init__(self, cloneMapFileName, resetClone=None, attributeDictionary=None): # cloneMap if resetClone != None: pcr.setclone(cloneMapFileName) cloneMap = pcr.boolean(pcr.readmap(cloneMapFileName)) cloneMap = pcr.boolean(pcr.scalar(1.0)) # latitudes and longitudes self.latitudes = np.unique( pcr.pcr2numpy(pcr.ycoordinate(cloneMap), vos.MV))[::-1] self.longitudes = np.unique( pcr.pcr2numpy(pcr.xcoordinate(cloneMap), vos.MV)) # reset clone (if necessary) if resetClone != None: pcr.setclone(resetClone) # netcdf format: self.format = 'NETCDF3_CLASSIC' self.attributeDictionary = {} if attributeDictionary == None: self.attributeDictionary['institution'] = "None" self.attributeDictionary['title'] = "None" self.attributeDictionary['source'] = "None" self.attributeDictionary['history'] = "None" self.attributeDictionary['references'] = "None" self.attributeDictionary['description'] = "None" self.attributeDictionary['comment'] = "None" else: self.attributeDictionary = attributeDictionary
def __init__(self, iniItems): # cloneMap pcr.setclone(iniItems.cloneMap) cloneMap = pcr.boolean(1.0) # latitudes and longitudes self.latitudes = np.unique(pcr2numpy(pcr.ycoordinate(cloneMap), vos.MV))[::-1] self.longitudes = np.unique( pcr2numpy(pcr.xcoordinate(cloneMap), vos.MV)) # TODO: Let users decide what their preference regarding latitude order. # Consult with Stefanie regarding CF convention. # netCDF format and attributes: self.attributeDictionary = {} self.attributeDictionary['institution'] = iniItems.globalOptions[ 'institution'] self.attributeDictionary['title'] = iniItems.globalOptions['title'] self.attributeDictionary['description'] = iniItems.globalOptions[ 'description'] # netcdf format and zlib setup self.format = 'NETCDF3_CLASSIC' self.zlib = False if "formatNetCDF" in iniItems.reportingOptions.keys(): self.format = str(iniItems.reportingOptions['formatNetCDF']) if "zlib" in iniItems.reportingOptions.keys(): if iniItems.reportingOptions['zlib'] == "True": self.zlib = True
def test_001(self): """ nonspatial and pcr2numpy """ nrRows, nrCols, cellSize = 5, 8, 1.0 west, north = 0.0, 0.0 pcraster.setclone(nrRows, nrCols, cellSize, west, north) value = 1.23456 nonspatial = pcraster.scalar(value) array = pcraster.pcr2numpy(nonspatial, numpy.nan) for row in range(0, nrRows): for col in range(0, nrCols): self.assertAlmostEqual(array[row][col], value) value = 3 nonspatial = pcraster.nominal(value) array = pcraster.pcr2numpy(nonspatial, numpy.nan) for row in range(0, nrRows): for col in range(0, nrCols): self.assertAlmostEqual(array[row][col], value) value = True nonspatial = pcraster.boolean(value) array = pcraster.pcr2numpy(nonspatial, numpy.nan) for row in range(0, nrRows): for col in range(0, nrCols): self.assertAlmostEqual(array[row][col], value)
def test_004(self): """ numpy2pcr with different incorrect shapes """ pcraster.setclone(3, 4, 1, 0, 0) a = numpy.array([12, 5, 21, 9, 9, 7, 3, 2, 20, 8, 2, -3]) with self.assertRaises(Exception) as context_manager: n2p = pcraster.numpy2pcr(pcraster.Nominal, a, 20) self.assertEqual(str(context_manager.exception), "Input must be two-dimensional NumPy array") a = numpy.array([[12, 5, 21], [9, 7, 3], [20, 8, 2], [5, 6, -3]]) with self.assertRaises(Exception) as context_manager: n2p = pcraster.numpy2pcr(pcraster.Nominal, a, 20) self.assertEqual( str(context_manager.exception), "Number of rows from input array (4) and current raster (3) are different" ) pcraster.setclone(3, 2, 1, 0, 0) a = numpy.array([[12, 5, 21], [9, 7, 3], [20, 8, -3]]) with self.assertRaises(Exception) as context_manager: n2p = pcraster.numpy2pcr(pcraster.Nominal, a, 20) self.assertEqual( str(context_manager.exception), "Number of columns from input array (3) and current raster (2) are different" )
def testNonSpatialConversions(self): pcraster.setclone("map2asc_PCRmap.map") nonSpatialValue = mapmaximum(pcraster.readmap("map2asc_PCRmap.map")) # Ordinal. nonSpatial = ordinal(nonSpatialValue) self.assertEqual(bool(nonSpatial), True) self.assertEqual(int(nonSpatial), 124) self.assertEqual(float(nonSpatial), 124.0) # Nominal. nonSpatial = nominal(nonSpatialValue) self.assertEqual(bool(nonSpatial), True) self.assertEqual(int(nonSpatial), 124) self.assertEqual(float(nonSpatial), 124) # Boolean. nonSpatial = boolean(nonSpatialValue) self.assertEqual(bool(nonSpatial), True) self.assertEqual(int(nonSpatial), 1) self.assertEqual(float(nonSpatial), 1.0) # Scalar. nonSpatial = scalar(mapmaximum("abs_Expr.map")) self.assertEqual(bool(nonSpatial), True) self.assertEqual(int(nonSpatial), 14) self.assertEqual(float(nonSpatial), 14.0)
def testSetCloneUsingFile(self): pcraster.setclone(os.path.join("validated", "ordinal_Result.map")) self.assertEqual(pcraster.clone().nrRows(), 3) self.assertEqual(pcraster.clone().nrCols(), 3) self.assertEqual(pcraster.clone().cellSize(), 1.0) self.assertEqual(pcraster.clone().west(), 0.0) self.assertEqual(pcraster.clone().north(), 0.0)
def __init__(self,configuration,model,specificAttributeDictionary=None): # Set clone map pcr.setclone(configuration.cloneMap) cloneMap = pcr.boolean(1.0) # map with all cell values equal to 1 # Retrieve latitudes and longitudes from clone map self.latitudes = np.unique(pcr.pcr2numpy(pcr.ycoordinate(cloneMap), vos.MV))[::-1] self.longitudes = np.unique(pcr.pcr2numpy(pcr.xcoordinate(cloneMap), vos.MV)) self.crops = np.arange(1, model.nCrop + 1) self.depths = np.arange(1, model.nComp + 1) # Let users decide what their preference regarding latitude order self.netcdf_y_orientation_follow_cf_convention = False if 'netcdf_y_orientation_follow_cf_convention' in configuration.reportingOptions.keys() and\ configuration.reportingOptions['netcdf_y_orientation_follow_cf_convention'] == "True": msg = "Latitude (y) orientation for output netcdf files start from the bottom to top." self.netcdf_y_orientation_follow_cf_convention = True self.latitudes = np.unique(pcr.pcr2numpy(pcr.ycoordinate(cloneMap), vos.MV)) # Set general netcdf attributes (based on the information given in the ini/configuration file) self.set_general_netcdf_attributes(configuration, specificAttributeDictionary) # netcdf format and zlib setup self.format = 'NETCDF3_CLASSIC' self.zlib = False if "formatNetCDF" in configuration.reportingOptions.keys(): self.format = str(configuration.reportingOptions['formatNetCDF']) if "zlib" in configuration.reportingOptions.keys(): if configuration.reportingOptions['zlib'] == "True": self.zlib = True
def __init__(self, configuration, currTimeStep, initialState = None): self._configuration = configuration self._modelTime = currTimeStep pcr.setclone(configuration.cloneMap) # Read the ldd map. self.lddMap = vos.readPCRmapClone(\ configuration.routingOptions['lddMap'], configuration.cloneMap,configuration.tmpDir,configuration.globalOptions['inputDir'],True) #ensure ldd map is correct, and actually of type "ldd" self.lddMap = pcr.lddrepair(pcr.ldd(self.lddMap)) if configuration.globalOptions['landmask'] != "None": self.landmask = vos.readPCRmapClone(\ configuration.globalOptions['landmask'], configuration.cloneMap,configuration.tmpDir,configuration.globalOptions['inputDir']) else: self.landmask = pcr.defined(self.lddMap) # ADDED: variables necessary for 2-way coupling functions # ---------------------------------------------------------------------------------------------------------------- # variable to control activation of 2-way coupling functions (can be changed through BMI) self.ActivateCoupling = self._configuration.globalOptions['ActivateCoupling'] # ---------------------------------------------------------------------------------------------------------------- # defining catchment areas self.catchment_class = 1.0 # number of upperSoilLayers: self.numberOfSoilLayers = int(configuration.landSurfaceOptions['numberOfUpperSoilLayers']) self.createSubmodels(initialState)
def testCellValueScalar(self): pcraster.setclone("abs_Expr.map") raster = scalar(pcraster.readmap("abs_Expr.map")) value, isValid = pcraster.cellvalue(raster, 1) self.assertEqual(isValid, True) self.assertTrue(isinstance(value, float)) self.assertEqual(value, 2.0) value, isValid = pcraster.cellvalue(raster, 2) self.assertEqual(isValid, True) self.assertTrue(isinstance(value, float)) self.assertEqual(value, -7.0) value, isValid = pcraster.cellvalue(raster, 3) self.assertEqual(isValid, True) self.assertTrue(isinstance(value, float)) self.assertEqual(value, 3.5) value, isValid = pcraster.cellvalue(raster, 6) self.assertEqual(isValid, False) value, isValid = pcraster.cellvalue(raster, 7) self.assertEqual(isValid, True) self.assertTrue(isinstance(value, float)) self.assertEqual(value, 0.0) value, isValid = pcraster.cellvalue(raster, 8) self.assertEqual(isValid, True) self.assertTrue(isinstance(value, float)) self.assertEqual(value, 14.0)
def __init__(self, globeCloneMapFileName, localCloneMapFileName, \ netcdf_input, \ pcraster_output, \ modelTime, \ inputEPSG, outputEPSG, resample_method): DynamicModel.__init__(self) # clone map file names: self.globeCloneMapFileName = globeCloneMapFileName self.localCloneMapFileName = localCloneMapFileName # time variable/object self.modelTime = modelTime # netcdf input and pcraster output files self.netcdf_input = netcdf_input self.pcraster_output = pcraster_output # input and output projection/coordinate systems self.inputEPSG = inputEPSG self.outputEPSG = outputEPSG # resampling method self.resample_method = resample_method # initial clone pcr.setclone(self.globeCloneMapFileName) # monthly step self.i_month = 0
def test_03(self): """ nonspatial condition in ifthen """ nr_rows = 2 nr_cols = 3 nr_cells = nr_rows * nr_cols pcraster.setclone(nr_rows, nr_cols, 5, 1, 1) raster = pcraster.ifthen(pcraster.boolean(1), pcraster.scalar(4.567)) for idx in range(1, nr_cells + 1): value, isValid = pcraster.cellvalue(raster, idx) self.assertEqual(isValid, True) self.assertAlmostEqual(value, 4.567, 6) raster = pcraster.ifthen(pcraster.boolean(0), pcraster.scalar(4.567)) for idx in range(1, nr_cells + 1): value, isValid = pcraster.cellvalue(raster, idx) self.assertEqual(isValid, False) raster = pcraster.ifthen(pcraster.scalar(1), pcraster.scalar(4.567)) for idx in range(1, nr_cells + 1): value, isValid = pcraster.cellvalue(raster, idx) self.assertEqual(isValid, True) self.assertAlmostEqual(value, 4.567, 6) raster = pcraster.ifthen(pcraster.scalar(0), pcraster.scalar(4.567)) for idx in range(1, nr_cells + 1): value, isValid = pcraster.cellvalue(raster, idx) self.assertEqual(isValid, False)
def __init__(self, configuration, currTimeStep, initialState = None): self._configuration = configuration self._modelTime = currTimeStep pcr.setclone(configuration.cloneMap) # Read the ldd map. self.lddMap = vos.readPCRmapClone(\ configuration.routingOptions['lddMap'], configuration.cloneMap,configuration.tmpDir,configuration.globalOptions['inputDir'],True) #ensure ldd map is correct, and actually of type "ldd" self.lddMap = pcr.lddrepair(pcr.ldd(self.lddMap)) if configuration.globalOptions['landmask'] != "None": self.landmask = vos.readPCRmapClone(\ configuration.globalOptions['landmask'], configuration.cloneMap,configuration.tmpDir,configuration.globalOptions['inputDir']) else: self.landmask = pcr.defined(self.lddMap) # defining catchment areas self.catchment_class = 1.0 # number of upperSoilLayers: self.numberOfSoilLayers = int(configuration.landSurfaceOptions['numberOfUpperSoilLayers']) self.createSubmodels(initialState)
def initialize_config(self, fileName): logger.info("PCRGlobWB: initialize_config") try: self.configuration = Configuration(fileName, relative_ini_meteo_paths=True) pcr.setclone(self.configuration.cloneMap) # set start and end time based on configuration self.model_time = ModelTime() self.model_time.getStartEndTimeSteps( self.configuration.globalOptions['startTime'], self.configuration.globalOptions['endTime']) self.model_time.update(0) self.shape = self.calculate_shape() logger.info("Shape of maps is %s", str(self.shape)) self.model = None except: import traceback traceback.print_exc() raise
def test7(self): """ test reading some values from disk (1dim) """ pcraster.setclone("clone.map") Plants = Index.Index(["TG", "SG"]) QMax = vcMod.VariableCollection([Plants], value=vcMod.ValueFromParameterTable( "QMax", "parameterFile.tbl", pcraster.Scalar)) self.assertEqual(QMax[Plants.TG], 12000) self.assertEqual(QMax[Plants.SG], 18000) Cvr = vcMod.VariableCollection([Plants], value=vcMod.ValueFromParameterTable( "Cvr", "parameterFile.tbl", pcraster.Scalar)) self.assertTrue(isinstance(Cvr[Plants.TG], pcraster._pcraster.Field)) self.assertTrue(isinstance(Cvr[Plants.SG], pcraster._pcraster.Field)) kv = vcMod.VariableCollection([Plants], value=vcMod.ValueFromParameterTable( "kv", "parameterFile.tbl", pcraster.Nominal)) self.assertEqual(kv[Plants.TG], 3) self.assertEqual(kv[Plants.SG], 7)
def test11(self): """ test reading values from disk with external names """ pcraster.setclone("clone.map") Plants = Index.Index(["TG=TallGrass", "SG=ShortGrass"]) PlantsAvailable = vcMod.VariableCollection( [Plants], value=vcMod.ValueFromParameterTable("PlantsAvailable", "parameterFile.tbl", pcraster.Scalar)) self.assertEqual(PlantsAvailable[Plants.TG], 1.3) self.assertEqual(PlantsAvailable[Plants.SG], 4.5) Herbivores = Index.Index([ "Cow=CowWithLongName", "Horse=HorseWithLongName", "Sheep=SheepWithLongName" ]) InteractionExt = vcMod.VariableCollection( [Herbivores, Plants], value=vcMod.ValueFromParameterTable("InteractionExt", "parameterFile.tbl", pcraster.Scalar)) self.assertEqual(InteractionExt[Herbivores.Cow, Plants.TG], 0.6) self.assertEqual(InteractionExt[Herbivores.Cow, Plants.SG], 0.67) self.assertEqual(InteractionExt[Herbivores.Horse, Plants.TG], 0.73) self.assertEqual(InteractionExt[Herbivores.Horse, Plants.SG], 0.74) self.assertEqual(InteractionExt[Herbivores.Sheep, Plants.TG], 0.87) self.assertEqual(InteractionExt[Herbivores.Sheep, Plants.SG], 0.89)
def __init__(self, cloneMapFileName,\ pcraster_files, \ output, \ modelTime): DynamicModel.__init__(self) # pcr.setclone(cloneMapFileName) self.modelTime = modelTime self.pcraster_files = pcraster_files # move to the input folder os.chdir(self.pcraster_files['directory']) self.output = output self.output['file_name'] = vos.getFullPath(self.output['file_name'], self.output['folder']) # object for reporting self.netcdf_report = OutputNetcdf(cloneMapFileName, self.output['description']) print(self.output['long_name']) # make a netcdf file self.netcdf_report.createNetCDF(self.output['file_name'],\ self.output['variable_name'],\ self.output['unit'],\ self.output['long_name'])
def __init__(self, input_netcdf,\ output_netcdf,\ modelTime,\ tmpDir = "/dev/shm/"): DynamicModel.__init__(self) self.input_netcdf = input_netcdf self.output_netcdf = output_netcdf self.tmpDir = tmpDir self.modelTime = modelTime # set clone self.clone_map_file = self.input_netcdf['cell_area'] pcr.setclone(self.clone_map_file) self.clone = {} self.clone['cellsize'] = pcr.clone().cellSize() ; print self.clone['cellsize'] self.clone['rows'] = int(pcr.clone().nrRows()) self.clone['cols'] = int(pcr.clone().nrCols()) self.clone['xUL'] = round(pcr.clone().west(), 2) self.clone['yUL'] = round(pcr.clone().north(), 2) # cell area (unit: m2) self.cell_area = pcr.readmap(self.input_netcdf['cell_area']) # an object for netcdf reporting self.output = OutputNetcdf(self.clone, self.output_netcdf) # preparing the netcdf file and make variable: self.output.createNetCDF(self.output_netcdf['file_name'], self.output_netcdf['gross_variable_name'], self.output_netcdf['variable_unit']) self.output.addNewVariable(self.output_netcdf['file_name'], self.output_netcdf['netto_variable_name'], self.output_netcdf['variable_unit'])
def __init__(self, iniItems, specificAttributeDictionary=None): # cloneMap pcr.setclone(iniItems.cloneMap) cloneMap = pcr.boolean(1.0) # latitudes and longitudes self.latitudes = np.unique(pcr.pcr2numpy(pcr.ycoordinate(cloneMap), vos.MV))[ ::-1 ] self.longitudes = np.unique(pcr.pcr2numpy(pcr.xcoordinate(cloneMap), vos.MV)) # Let users decide what their preference regarding latitude order. self.netcdf_y_orientation_follow_cf_convention = False if ( "netcdf_y_orientation_follow_cf_convention" in list(iniItems.reportingOptions.keys()) and iniItems.reportingOptions["netcdf_y_orientation_follow_cf_convention"] == "True" ): msg = "Latitude (y) orientation for output netcdf files start from the bottom to top." self.netcdf_y_orientation_follow_cf_convention = True self.latitudes = np.unique(pcr.pcr2numpy(pcr.ycoordinate(cloneMap), vos.MV)) # set the general netcdf attributes (based on the information given in the ini/configuration file) self.set_general_netcdf_attributes(iniItems, specificAttributeDictionary) # netcdf format and zlib setup self.format = "NETCDF3_CLASSIC" self.zlib = False if "formatNetCDF" in list(iniItems.reportingOptions.keys()): self.format = str(iniItems.reportingOptions["formatNetCDF"]) if "zlib" in list(iniItems.reportingOptions.keys()): if iniItems.reportingOptions["zlib"] == "True": self.zlib = True # if given in the ini file, use the netcdf as given in the section 'specific_attributes_for_netcdf_output_files' if "specific_attributes_for_netcdf_output_files" in iniItems.allSections: for key in list( iniItems.specific_attributes_for_netcdf_output_files.keys() ): self.attributeDictionary[ key ] = iniItems.specific_attributes_for_netcdf_output_files[key] if self.attributeDictionary[key] == "None": self.attributeDictionary[key] = "" if key == "history" and self.attributeDictionary[key] == "Default": self.attributeDictionary[ key ] = "created on " + datetime.datetime.today().isoformat(" ") if self.attributeDictionary[key] == "Default" and ( key == "date_created" or key == "date_issued" ): self.attributeDictionary[key] = datetime.datetime.today().isoformat( " " )
def testScalarArray2Raster(self): pcraster.setclone("boolean_Expr.map") try: a = numpy.array([ [0.5, 0.34202, 0.310676], [20, 0, -0.981627], [0.707107, 0.144356, 0.0174524] ]) result = pcraster.numpy2pcr(pcraster.Scalar, a, 20) self.failUnless(self.mapEqualsValidated(result, "sin_Result.map"), "test1: %s" % ("Result and validated result are not the same")) except Exception as exception: self.failUnless(False, "test1: %s" % (str(exception)))
def testBooleanArray2Raster(self): pcraster.setclone("boolean_Expr.map") try: a = numpy.array([ [1, 0, 1], [20, 1, 1], [1, 1, 0] ], numpy.uint8) # uint8 is bugzilla #271 result = pcraster.numpy2pcr(pcraster.Boolean, a, 20) self.failUnless(self.mapEqualsValidated(result, "boolean_Result.map"), "test1: %s" % ("Result and validated result are not the same")) except Exception as exception: self.failUnless(False, "test1: %s" % (str(exception)))
def testLddArray2Raster(self): pcraster.setclone("and_Expr1.map") try: a = numpy.array([ [6, 5, 4], [6, 8, 7], [8, 8, 8] ]) result = pcraster.numpy2pcr(pcraster.Ldd, a, 20) self.failUnless(self.mapEqualsValidated(result, "ldd_Result.map"), "test1: %s" % ("Result and validated result are not the same")) except Exception as exception: self.failUnless(False, "test1: %s" % (str(exception)))
def testOrdinalArray2Raster(self): pcraster.setclone("boolean_Expr.map") try: a = numpy.array([ [0, 1, 3], [20, -3, -2], [0, 9, 8] ]) result = pcraster.numpy2pcr(pcraster.Ordinal, a, 20) self.failUnless(self.mapEqualsValidated(result, "ordinal_Result.map"), "test1: %s" % ("Result and validated result are not the same")) except Exception as exception: self.failUnless(False, "test1: %s" % (str(exception)))
def __init__(self, clone_map, log_file): pcraster.framework.DynamicModel.__init__(self) pcraster.framework.MonteCarloModel.__init__(self) pcraster.setclone(clone_map) self.log_file = log_file self.process = psutil.Process(os.getpid())
def dynamic(self): # re-calculate current model time using current pcraster timestep value self.modelTime.update(self.currentTimeStep()) # perform the operation only at the last day of the month (as the input netcdf file has a monthly resolution with the last date of the month as its time stamp) if self.modelTime.isLastDayOfMonth(): self.i_month = self.i_month + 1 # reading a netcdf file (global extent, 5 arcmin resolution): pcr.setclone(self.globeCloneMapFileName) global_pcraster_map = vos.netcdf2PCRobjClone(ncFile = self.netcdf_input['file_name'], \ varName = self.netcdf_input['variable_name'], \ dateInput = self.modelTime.fulldate) # save it to pcraster maps (still at a global extent and 5 arcmin resolution); # there will be two files as follows: # - Format 1: example file names: htop0000.001 (for the 1st time step), htop0000.002, htop0000.003, etc. ... pcraster_file_name = self.pcraster_output[ 'output_folder'] + "/global/" + self.pcraster_output[ 'file_name'] pcraster_file_name = pcr.framework.frameworkBase.generateNameT( pcraster_file_name, self.i_month) pcr.report(global_pcraster_map, pcraster_file_name) # - Format 2: example file names: htop_2000_01.map, htop_2000_02.map, etc. pcraster_file_name = self.pcraster_output[ 'output_folder'] + "/global/" + self.pcraster_output[ 'file_name'] + "_" + self.modelTime.fulldate[0:7].replace( "-", "_") + ".map" pcr.report(global_pcraster_map, pcraster_file_name) # reproject and resample it to a local coordinate system pcr.setclone(self.localCloneMapFileName) local_pcraster_map = vos.readPCRmapClone(v = pcraster_file_name, \ cloneMapFileName = self.localCloneMapFileName, \ tmpDir = self.pcraster_output['output_folder'] + "/tmp/", \ absolutePath = None,\ isLddMap = False, \ cover = None, \ isNomMap = False, \ inputEPSG = self.inputEPSG, outputEPSG = self.outputEPSG, method = "near") # save it to pcraster maps (now already at the extent, the resolution and the coordinate system of the local model) # there will be two files as follows: # - Format 1: example file names: htop0000.001 (for the 1st time step), htop0000.002, htop0000.003, etc. ... pcraster_file_name = self.pcraster_output[ 'output_folder'] + "/regional/" + self.pcraster_output[ 'file_name'] pcraster_file_name = pcr.framework.frameworkBase.generateNameT( pcraster_file_name, self.i_month) pcr.report(local_pcraster_map, pcraster_file_name) # - Format 2: example file names: htop_2000_01.map, htop_2000_02.map, etc. pcraster_file_name = self.pcraster_output[ 'output_folder'] + "/regional/" + self.pcraster_output[ 'file_name'] + "_" + self.modelTime.fulldate[0:7].replace( "-", "_") + ".map" pcr.report(local_pcraster_map, pcraster_file_name)
def __init__(self, iniItems, specificAttributeDictionary=None): # cloneMap pcr.setclone(iniItems.cloneMap) cloneMap = pcr.boolean(1.0) # latitudes and longitudes self.latitudes = np.unique( pcr.pcr2numpy(pcr.ycoordinate(cloneMap), vos.MV))[::-1] self.longitudes = np.unique( pcr.pcr2numpy(pcr.xcoordinate(cloneMap), vos.MV)) # Let users decide what their preference regarding latitude order. self.netcdf_y_orientation_follow_cf_convention = False if ("netcdf_y_orientation_follow_cf_convention" in list( iniItems.reportingOptions.keys()) and iniItems. reportingOptions["netcdf_y_orientation_follow_cf_convention"] == "True"): msg = "Latitude (y) orientation for output netcdf files start from the bottom to top." self.netcdf_y_orientation_follow_cf_convention = True self.latitudes = np.unique( pcr.pcr2numpy(pcr.ycoordinate(cloneMap), vos.MV)) # set the general netcdf attributes (based on the information given in the ini/configuration file) self.set_general_netcdf_attributes(iniItems, specificAttributeDictionary) # netcdf format and zlib setup self.format = "NETCDF3_CLASSIC" self.zlib = False if "formatNetCDF" in list(iniItems.reportingOptions.keys()): self.format = str(iniItems.reportingOptions["formatNetCDF"]) if "zlib" in list(iniItems.reportingOptions.keys()): if iniItems.reportingOptions["zlib"] == "True": self.zlib = True # if given in the ini file, use the netcdf as given in the section 'specific_attributes_for_netcdf_output_files' if "specific_attributes_for_netcdf_output_files" in iniItems.allSections: for key in list( iniItems.specific_attributes_for_netcdf_output_files.keys( )): self.attributeDictionary[ key] = iniItems.specific_attributes_for_netcdf_output_files[ key] if self.attributeDictionary[key] == "None": self.attributeDictionary[key] = "" if key == "history" and self.attributeDictionary[ key] == "Default": self.attributeDictionary[ key] = "created on " + datetime.datetime.today( ).isoformat(" ") if self.attributeDictionary[key] == "Default" and ( key == "date_created" or key == "date_issued"): self.attributeDictionary[key] = datetime.datetime.today( ).isoformat(" ")
def test6(self): """ test wrong external names format """ pcraster.setclone("clone.map") exceptionThrown = False try: PlantSpecies = Index.Index(["TG=Tall=Grass", "SG=ShortGass"]) except Exception, e: self.assertEqual(str(e), "Error in initialisation of class Index: format of TG=Tall=Grass does not match Modelname = Externalname") exceptionThrown = True
def test1(self): """ test multiple indices """ pcraster.setclone("clone.map") exceptionThrown = False try: PlantSpecies = Index.Index([ "Species1", "Species1", "Species3" ]) except Exception, e: self.assertEqual(str(e), "Error in initialisation of class Index: array indices must be unique, Species1 already used") exceptionThrown = True
def test5(self): """ test empty index """ pcraster.setclone("clone.map") exceptionThrown = False try: PlantSpecies = Index.Index([]) except Exception, e: self.assertEqual(str(e), "Error in initialisation of class Index: no array indices provided") exceptionThrown = True
def assignPCR2cells(landmask_pcr, hydroCoords, verbose): """ Function converting single indices of coupled PCR cells to double (array,column) indices. This is the key function coupling the grids of PCR-GLOBWB and the hydrodynamic models. Input: ----- :param landmask_pcr: land mask of hydrological model :param hydroCoords: list of (x,y) coordinates of each hydrodynamic cell :param verbose: print information yes/no Output: ------ coupledHydro2PCR: double containing coupled PCR cell per cell of hydrodynamic model coupledPCR2hydro: double containing all hydrodynamic cells per coupled PCR cell zipPCRIndices: indices pointing to all coupled PCR cells in an array """ #-read in x and y coordinates set clone, get its attributes and read in landmask pcr.setclone(landmask_pcr) landMask= pcr.readmap(landmask_pcr) cloneAttributes= spatialDataSet2PCR.spatialAttributes(landmask_pcr) #-call function to get cell centres lon_hydro, lat_hydro= getMidPointsFromVertices(hydroCoords) #-print extent of coupled cells of hydrodynamic models if specified if verbose == True: print ' - coordinates of cell centres contain %d entries with a latitudes spanning %f-%f and longitudes %f-%f' %\ (len(lat_hydro), min(lat_hydro), max(lat_hydro), min(lon_hydro), max(lon_hydro)) #-read in lat and lon for DFM index_hydro = np.arange(len(lon_hydro)) #-get corresponding PCRaster indices cellIndexPCR, rowIndexPCR, colIndexPCR= \ getPCRIndices(index_hydro, lat_hydro, lon_hydro, landMask, cloneAttributes, verbose= False) #-check on assignment numberDFMCells= np.zeros((cloneAttributes.numberRows, cloneAttributes.numberCols)) pcrCellID= np.zeros((cloneAttributes.numberRows, cloneAttributes.numberCols)) for iCnt in xrange(len(cellIndexPCR)): rowCnt= rowIndexPCR[iCnt] colCnt= colIndexPCR[iCnt] numberDFMCells[rowCnt, colCnt]+= 1 pcrCellID[rowCnt, colCnt]= cellIndexPCR[iCnt] #-return information for coupling coupledHydro2PCR, coupledPCR2hydro, zipPCRIndices= \ returnCoupledPCRIndices(index_hydro, lat_hydro, lon_hydro, landMask, cloneAttributes, verbose= False) #-check on assignment # check for PCRaster first numberDFMCells= np.zeros((cloneAttributes.numberRows, cloneAttributes.numberCols)) pcrCellID= np.zeros((cloneAttributes.numberRows, cloneAttributes.numberCols)) sortedRows, sortedCols= zip(*zipPCRIndices) for iCnt in xrange(len(coupledPCR2hydro)): cellID= coupledPCR2hydro[iCnt][0] rowCnt= sortedRows[iCnt] colCnt= sortedCols[iCnt] numberEntries= np.size(coupledPCR2hydro[iCnt][1]) return coupledHydro2PCR, coupledPCR2hydro, zipPCRIndices
def testIfThenElse(self): pcraster.setclone("and_Expr1.map") exceptionThrown = False try: result = pcraster.ifthenelse(1.0 == 2.0, 3.0, 4.0) except RuntimeError, exception: message = str(exception) self.assert_(string.find(message, "conversion function to pick a data type") != -1) exceptionThrown = True
def test4(self): """ test remove index """ pcraster.setclone("clone.map") exceptionThrown = False PlantSpecies = Index.Index([ "Species1", "Species2", "Species3" ]) try: PlantSpecies.__delattr__("Species3") except Exception, e: self.assertEqual(str(e), "Removal of an Index attribute not permitted") exceptionThrown = True
def test3(self): """ test add index """ pcraster.setclone("clone.map") exceptionThrown = False PlantSpecies = Index.Index([ "Species1", "Species2", "Species3" ]) try: PlantSpecies.__setattr__("Species4", 4) except Exception, e: self.assertEqual(str(e), "Modification of an Index attribute not permitted") exceptionThrown = True
def test3(self): """ test looping over indices """ pcraster.setclone("clone.map") PlantSpecies = Index.Index([ "Species3", "Species1", "Species2" ]) Coll = vcMod.VariableCollection([PlantSpecies], value=0) res = "" for plant in Coll: res += str(plant) self.assertEqual(res, "('Species3',)('Species1',)('Species2',)")
def testComException(self): exceptionThrown = False try: # Calculating the slope of a boolean map sucks. pcraster.setclone("and_Expr1.map") res = slope("and_Expr1.map") except RuntimeError, exception: message = str(exception) self.assert_(string.find(message, "argument nr. 1 of function 'slope': type is boolean, legal type is scalar") != -1) exceptionThrown = True
def test3(self): """ test looping over indices """ pcraster.setclone("clone.map") PlantSpecies = Index.Index(["Species3", "Species1", "Species2"]) Coll = vcMod.VariableCollection([PlantSpecies], value=0) res = "" for plant in Coll: res += str(plant) self.assertEqual(res, "('Species3',)('Species1',)('Species2',)")
def testCellValueNonSpatial(self): pcraster.setclone("abs_Expr.map") raster = pcraster.readmap("abs_Expr.map") value, isValid = pcraster.cellvalue(mapmaximum(raster), 1, 1) self.assertEqual(isValid, True) self.assertTrue(isinstance(value, float)) self.assertEqual(value, 14.0) value, isValid = pcraster.cellvalue(mapmaximum(raster), 1) self.assertEqual(isValid, True) self.assertTrue(isinstance(value, float)) self.assertEqual(value, 14.0)
def _set_current_clone(area_property, item_idx): west = area_property.space_domain.p1.xcoord[item_idx] north = area_property.space_domain.p1.ycoord[item_idx] rows = int(area_property.space_domain.row_discr[item_idx]) cols = int(area_property.space_domain.col_discr[item_idx]) cellsize = (area_property.space_domain.p2.xcoord[item_idx] - west) / cols pcraster.setclone(rows, cols, cellsize, west, north)
def test9(self): """ test initialisation with non-existing key """ pcraster.setclone("clone.map") exceptionThrown = False Plants = Index.Index([ "TG", "SG"]) try: QMax2 = vcMod.VariableCollection([Plants], value=vcMod.ValueFromParameterTable("QMax2", "parameterFile.tbl", pcraster.Scalar)) except ValueError, e: self.assertEqual(str(e), "Error reading parameterFile.tbl line 26, 'sG' unknown collection index") exceptionThrown = True
def test8(self): """ test multiple initialisation of values """ pcraster.setclone("clone.map") exceptionThrown = False Plants = Index.Index([ "TG", "SG"]) try: QMax1 = vcMod.VariableCollection([Plants], value=vcMod.ValueFromParameterTable("QMax1", "parameterFile.tbl", pcraster.Scalar)) except ValueError, e: self.assertEqual(str(e), "Error reading parameterFile.tbl line 21, QMax1 'TG' already initialised") exceptionThrown = True
def testDirectionalArray2Raster(self): pcraster.setclone("boolean_Expr.map") pcraster.setglobaloption("degrees") try: a = numpy.array([ [math.radians(350),math.radians(0),math.radians(0.01)],\ [20,math.radians(350),math.radians(21)],\ [math.radians(359),math.radians(40),math.radians(0)] ]) result = pcraster.numpy2pcr(pcraster.Directional, a, 20) self.failUnless(self.mapEqualsValidated(result, "directional_Result2.map"), "test1: %s" % ("Result and validated result are not the same")) except Exception as exception: self.failUnless(False, "test1: %s" % (str(exception)))
def test2(self): """ test change index """ pcraster.setclone("clone.map") exceptionThrown = False PlantSpecies = Index.Index([ "Species1", "Species2", "Species3" ]) try: PlantSpecies.Species1 = 5 except Exception as e: self.assertEqual(str(e), "Modification of an Index attribute not permitted") exceptionThrown = True self.assertTrue(exceptionThrown)
def test3(self): """ test add index """ pcraster.setclone("clone.map") exceptionThrown = False PlantSpecies = Index.Index(["Species1", "Species2", "Species3"]) try: PlantSpecies.__setattr__("Species4", 4) except Exception as e: self.assertEqual( str(e), "Modification of an Index attribute not permitted") exceptionThrown = True self.assertTrue(exceptionThrown)
def test4(self): """ test remove index """ pcraster.setclone("clone.map") exceptionThrown = False PlantSpecies = Index.Index(["Species1", "Species2", "Species3"]) try: PlantSpecies.__delattr__("Species3") except Exception as e: self.assertEqual(str(e), "Removal of an Index attribute not permitted") exceptionThrown = True self.assertTrue(exceptionThrown)
def testSetCloneUsingValues(self): nrRows = 4 nrCols = 5 cellSize = 6.0 west = 7.0 north = 8.0 pcraster.setclone(nrRows, nrCols, cellSize, west, north) self.assertEqual(pcraster.clone().nrRows(), nrRows) self.assertEqual(pcraster.clone().nrCols(), nrCols) self.assertEqual(pcraster.clone().cellSize(), cellSize) self.assertEqual(pcraster.clone().west(), west) self.assertEqual(pcraster.clone().north(), north)
def test2(self): """ test adding indices """ pcraster.setclone("clone.map") exceptionThrown = False PlantSpecies = Index.Index([ "Species1", "Species2", "Species3" ]) Coll = vcMod.VariableCollection([PlantSpecies], value=None) try: Coll["Species4"] = 5 except Exception, e: self.assertEqual(str(e), "cannot add elements to a VariableCollection") exceptionThrown = True
def test7(self): """ test external names """ pcraster.setclone("clone.map") PlantSpecies = Index.Index(["TG=TallGrass", "SG=ShortGrass"]) self.assertEqual(PlantSpecies.__dict__["_values"], ['TG', 'SG']) self.assertEqual('TallGrass', PlantSpecies.__dict__["_externalNames"].get("TG")) self.assertEqual('ShortGrass', PlantSpecies.__dict__["_externalNames"].get("SG")) PlantSpecies = Index.Index(["TG = TallGrass", "SG = ShortGrass"]) self.assertEqual(PlantSpecies.__dict__["_values"], ['TG', 'SG']) self.assertEqual('TallGrass', PlantSpecies.__dict__["_externalNames"].get("TG")) self.assertEqual('ShortGrass', PlantSpecies.__dict__["_externalNames"].get("SG"))
def __init__(self, clone_map, nr_state_variables, nr_locations_to_write): pcraster.framework.DynamicModel.__init__(self) pcraster.framework.MonteCarloModel.__init__(self) pcraster.setclone(clone_map) self.nr_state_variables = nr_state_variables self.nr_locations_to_write = nr_locations_to_write self.process = psutil.Process(os.getpid()) self.memory_log_file = file("memory.col", "w") self.runtime_log_file = file("runtime.col", "w")
def testCellValueOrdinal(self): pcraster.setclone("areaarea_Class.map") raster = ordinal(pcraster.readmap("areaarea_Class.map")) value, isValid = pcraster.cellvalue(raster, 1) self.assertEqual(isValid, True) self.assertTrue(isinstance(value, int)) self.assertEqual(value, 2) value, isValid = pcraster.cellvalue(raster, 2) self.assertEqual(isValid, True) self.assertTrue(isinstance(value, int)) self.assertEqual(value, 6) value, isValid = pcraster.cellvalue(raster, 5) self.assertEqual(isValid, False)