def wf_readmap(self, name, default): """ Adjusted version of readmapNew. the style variable is used to indicated how the data is read:: 1 - default: reads pcrmaps 2 - memory: assumes the map is made available (in memory) using the in-memory interface .. note: the style variable is set using the variable list from the API section in the ini file """ directoryPrefix = "" nameSuffix = ".map" newName = "" varname = os.path.basename(name) # check and if neede file the variable name object thevars = self.exchnageitems.getvars() if size(thevars) == 0: self.wf_supplyVariableNamesAndRoles() style = self.exchnageitems.getvarStyle(varname) #print varname + " : " + str(style) if hasattr(self._userModel(), "_inStochastic"): if self._userModel()._inStochastic(): if self._userModel()._inPremc() or self._userModel()._inPostmc( ): newName = name + nameSuffix else: directoryPrefix = str( self._userModel().currentSampleNumber()) if hasattr(self._userModel(), "_inInitial"): if self._userModel()._inInitial(): newName = name + nameSuffix if self._inResume(): timestep = self._userModel().firstTimeStep() newName = generateNameT(name, timestep - 1) if hasattr(self._userModel(), "_inDynamic"): if self._userModel()._inDynamic() or self._inUpdateWeight(): timestep = self._userModel().currentTimeStep() newName = generateNameT(name, timestep) if style == 1: path = os.path.join(directoryPrefix, newName) assert path is not "" try: import PCRaster except ImportError: import pcraster as PCRaster if os.path.isfile(path): return cover(PCRaster.readmap(path), default) else: #logger.warn("returning 0.0") return scalar(default) elif style == 2: # first get basename (last bit of path) name = os.path.basename(name) if hasattr(self._userModel(), name): exec "retval = self._userModel()." + name return retval else: exec "self._userModel()." + name + " = cover(scalar(default))" exec "retval = self._userModel()." + name return retval else: return cover(scalar(default))
#Modules import os, calendar import PCRaster as pcr #Run time staYear= int(2000) endYear= int(2001) staMonth= int(1) endMonth= int(13) timeStep= int(1) #Global input cloneFile= ('globalclone.map') pcr.setclone(cloneFile) clone= pcr.readmap(cloneFile) cellArea= pcr.readmap('cellarea30.map') LDD= pcr.readmap('globalldd.map') country= pcr.readmap('country.map') gwab2000= pcr.readmap('groundwater\\gwab_2000_mlnm3.map') efficiency= pcr.readmap('efficiency\\efficiency.map') rf1= pcr.readmap('rf\\rfcrops1.map') rf2= pcr.readmap('rf\\rfcrops2.map') kfc2= pcr.readmap('irrigation\\kfc100.map') #import from PCR-GLOBWB per calibration time step ##esp= 'PCRGLOBWB\\esp\\esp0%04d.%03d' #potential bare soil evaporation (m/day) ##esa= 'PCRGLOBWB\\esa\\esac%04d.%03d' #actual bare soil evaporation (m/day) ##t1p= 'PCRGLOBWB\\t1p\\t1p0%04d.%03d' #potential transpiration from soil layer 1 (m/day) ##t2p= 'PCRGLOBWB\\t2p\\t2p0%04d.%03d' #potential transpiration from soil layer 2 (m/day) ##t1a= 'PCRGLOBWB\\t1a\\t1a0%04d.%03d' #actual transpiration from soil layer 1 (m/day)
import os, zlib,zipfile from scipy import optimize import numpy as np import PCRaster as pcr from PCRaster.NumPy import pcr2numpy, numpy2pcr from PCRaster.Framework import generateNameT monthlyArchiveFile= 'cru_alpha_sc_results.zip' specificQArchiveFile= 'cru_specificrunoff_results.zip' rootQSpecFileNames= ['waterrunoff%s.map','landrunoff%s.map'] rootQFileName= 'qc' MV= -999. startYear= 1958; endYear= 2001 yearList= range(startYear,endYear+1) rootR3AVGFileName= 'r3_avg%s.map' LDD= pcr.readmap('glwd_lddlake.map') LDDBasins= pcr.readmap('glwd130m_ldd.map') cellArea= pcr.readmap('cellarea30.map') fracWat= pcr.readmap('glwd130m_fracw.map') lakeMask= pcr.readmap('lake.map') != 0 catchments= pcr.catchment(LDDBasins,pcr.pit(LDDBasins)) pcr.report(catchments,'catchments.map') maximumCatchmentID= pcr.cellvalue(pcr.mapmaximum(pcr.scalar(catchments)),1)[0] catchmentSizeLimit= 0. #-main #-opening zip file print 'extracting information from zip file' currentPath= os.getcwd() zipArchive= zipfile.ZipFile(monthlyArchiveFile)
import PCRaster as pcr firstInput = pcr.readmap("first_map") for year in range(1960, 2001, 1): secondInput = pcr.readmap("second_map_%04d" % (year)) sum = firstInput + secondInput pcr.report(sum, "sum_map_%04d" % (year))
def __init__(self, tssFilename, model, idMap=None, noHeader=False): """ """ if not isinstance(tssFilename, str): raise Exception( "timeseries output filename must be of type string") self._outputFilename = tssFilename self._maxId = 1 self._spatialId = None self._spatialDatatype = None self._spatialIdGiven = False self._userModel = model self._writeHeader = not noHeader # array to store the timestep values self._sampleValues = None _idMap = False #if isinstance(idMap, str) or isinstance(idMap, PCRaster._PCRaster.Field): if isinstance(idMap, str) or isinstance(idMap, PCRaster._pcraster.Field): _idMap = True nrRows = self._userModel.nrTimeSteps() - self._userModel.firstTimeStep( ) + 1 if _idMap: self._spatialId = idMap if isinstance(idMap, str): self._spatialId = PCRaster.readmap(idMap) _allowdDataTypes = [ PCRaster.Nominal, PCRaster.Ordinal, PCRaster.Boolean ] if self._spatialId.dataType() not in _allowdDataTypes: raise Exception( "idMap must be of type Nominal, Ordinal or Boolean") if self._spatialId.isSpatial(): self._maxId, valid = PCRaster.cellvalue( PCRaster.mapmaximum(PCRaster.ordinal(self._spatialId)), 1) else: self._maxId = 1 # cell indices of the sample locations self._sampleAddresses = [] for cellId in range(1, self._maxId + 1): thecellId = self._getIndex(cellId) if thecellId != 0: self._sampleAddresses.append(thecellId) else: print "CellId " + str(cellId) + " not found." self._spatialIdGiven = True nrCols = self._maxId self._sampleValues = [[Decimal("NaN")] * nrCols for _ in [0] * nrRows] else: self._sampleValues = [[Decimal("NaN")] * 1 for _ in [0] * nrRows]