def __init__(self, config_params): super(randomizer, self).__init__(config_params) # .__ts = [(date,val),(date,val),] self.__ts = [] # build inputs and outputs io = mdl.build_exchange_items_from_config(config_params) # set inputs and outputs self.inputs(value=io[stdlib.ExchangeItemType.INPUT]) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT])
def __init__(self, config_params): super(weatherReader, self).__init__(config_params) # build inputs and outputs io = mdl.build_exchange_items_from_config(config_params) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT]) # set inputs and outputs self.p = self.outputs()['Precipitation'] # get weather data file self.weather_data_path = config_params['model inputs'][0]['weather_csv']
def __init__(self, config_params): super(weatherReader, self).__init__(config_params) # build inputs and outputs io = mdl.build_exchange_items_from_config(config_params) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT]) # set inputs and outputs self.p = self.outputs()['Precipitation'] # get weather data file self.weather_data_path = config_params['model inputs'][0][ 'weather_csv']
def __init__(self, config_params): super(slowloading, self).__init__(config_params) print "begin loading module" # loop designed to cause slow initialization # build inputs and outputs io = mdl.build_exchange_items_from_config(config_params) self.inputs(value=io[stdlib.ExchangeItemType.INPUT]) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT]) cnt = 0 for i in range(0, 50000000): cnt += i print "Done loading module"
def __init__(self,config_params): super(slowloading, self).__init__(config_params) print "begin loading module" # loop designed to cause slow initialization # build inputs and outputs io = mdl.build_exchange_items_from_config(config_params) self.inputs(value=io[stdlib.ExchangeItemType.INPUT]) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT]) cnt = 0 for i in range(0, 50000000): cnt += i print "Done loading module"
def __init__(self, config_params): """ initialization that will occur when loaded into a configuration """ super(multiply, self).__init__(config_params) # build inputs and outputs io = mdl.build_exchange_items_from_config(config_params) # set inputs and outputs self.inputs(value=io[stdlib.ExchangeItemType.INPUT]) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT]) sPrint('Multiplier initialization complete.')
def __init__(self,config_params): """ initialization that will occur when loaded into a configuration """ super(multiply,self).__init__(config_params) # build inputs and outputs io = mdl.build_exchange_items_from_config(config_params) # set inputs and outputs self.inputs(value=io[stdlib.ExchangeItemType.INPUT]) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT]) sPrint('Multiplier initialization complete.')
def test_create_simulation(self): papi = postgresdb(self.session) model_code = 'swat' sim_name = 'TonySwatSimulation' sim_description = 'My SWAT Simulation' #sim_start = dt.datetime(2014,01,01,0,0,0) #sim_end = dt.datetime(2014,02,01,0,0,0) timestepvalue = 1 timestepunitid = None # query this inputdatasetid = None # query this #startoffset=-6 #endoffset=-6 # build exchange items from file config = os.path.realpath('../../tests/test_data/configuration.ini') params = gui.parse_config(config) eitems = mdl.build_exchange_items_from_config(params) # add some data to simulate 'output' exchange items vals= [(dt.datetime(2014,1,1,0,0,0) + dt.timedelta(days=i), i) for i in range(0,100)] output_item1 = eitems['output'][0] output_item1.geometries()[0].datavalues().set_timeseries(vals) vals= [(dt.datetime(2014,1,1,0,0,0) + dt.timedelta(days=i), 2*i) for i in range(0,100)] output_item1.geometries()[1].datavalues().set_timeseries(vals) vals= [(dt.datetime(2014,1,1,0,0,0) + dt.timedelta(days=i), 2**i) for i in range(0,100)] output_item2 = eitems['output'][1] output_item2.geometries()[0].datavalues().set_timeseries(vals) outputs = [output_item1,output_item2] sim = papi.create_simulation(preferences_path='../../data/preferences', config_params=params, output_exchange_items= outputs) print 'Successfully inserted Simulation: %d'%sim.SimulationID
def __init__(self, config_params): super(ueb, self).__init__(config_params) # build inputs and outputs io = mdl.build_exchange_items_from_config(config_params) # set input and output exchange items self.inputs(value=io[stdlib.ExchangeItemType.INPUT]) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT]) # grab the C library path and the control file path lib = config_params['lib'] conFile = config_params['control'] # load the UEB C library self.__uebLib = cdll.LoadLibrary(join(os.path.dirname(__file__), lib)) # save the current directory for saving output data self.curdir = os.path.dirname(os.path.abspath(conFile)) # the base directory for the control file is used to convert relative paths into absolute paths self.base_dir = os.path.dirname(conFile) # get param, sitevar, input, output, and watershed files with open(conFile, 'r') as f: # lines = f.readlines() lines = f.read().splitlines() # this will auto strip the \n \r C_paramFile = os.path.join(self.base_dir, lines[1]) C_sitevarFile = os.path.join(self.base_dir, lines[2]) C_inputconFile = os.path.join(self.base_dir, lines[3]) C_outputconFile = os.path.join(self.base_dir, lines[4]) C_watershedFile = os.path.join(self.base_dir, lines[5]) C_wsvarName = lines[6].split(' ')[0] C_wsycorName = lines[6].split(' ')[1] C_wsxcorName = lines[6].split(' ')[2] C_aggoutputconFile = os.path.join(self.base_dir, lines[7]) C_aggoutputFile = os.path.join(self.base_dir, lines[8]) ModelStartDate = [int(float(l)) for l in lines[9].split(' ') if l != ''] ModelEndDate = [int(float(l)) for l in lines[10].split(' ') if l != ''] ModelDt = float(lines[11]) ModelUTCOffset = float(lines[12]) inpDailyorSubdaily = bool(lines[13]==True) self.outtStride, outyStep, outxStep = [int(s) for s in lines[14].split(' ')] C_wsxcorArray = c_float() C_wsycorArray = c_float() self.C_wsArray = pointer(pointer(c_int32())) self.C_dimlen1 = c_int() self.C_dimlen2 = c_int() totalgrid = 0 self.C_wsfillVal = c_int(-9999) npar = c_int(32) tinitTime = c_int(0) self.C_parvalArray = pointer(c_float(0)); numOut = 70 # hack: number of outputs? self.C_pOut = pointer(pointOutput()) C_aggOut = pointer(aggOutput()) self.C_ncOut = pointer(ncOutput()) self.C_npout = c_int(0) self.C_nncout = c_int(0) C_naggout = c_int(0) C_nZones = c_int(0) C_tNameout = c_char_p("time") tunits = (c_char*256)() C_tUnitsout = pointer(tunits) C_tlong_name = c_char_p("time") C_tcalendar = c_char_p("standard") self.t_out = pointer(c_float(0)) C_out_fillVal = c_float(-9999.0) self.C_outDimord = c_int(0) C_aggoutDimord = c_int(1) self.outvarindx = c_int(17) # aggoutvarindx = c_int(17) # size = c_int() # rank = c_int() # irank = c_int() # jrank = c_int() # startTimeT = c_double(0.0) # TotalTime = c_double(0.0) # totalmodelrunTime = c_double(0.0) # TsReadTime = c_double(0.0) # TSStartTime = c_double() # ComputeStartTime = c_double() # ComputeTime = c_double(0.0) # OutWriteTime = c_double() self.uebVars = (c_char_p * 70)("Year", "Month", "Day", "dHour", "atff", "HRI", "Eacl", "Ema", "conZen", "Ta", "P", "V", "RH", "Qsi", "Qli", "Qnet","Us", "SWE", "tausn", "Pr", "Ps", "Alb", "QHs", "QEs", "Es", "SWIT", "QMs", "Q", "FM", "Tave", "TSURFs", "cump", "cumes", "cumMr", "Qnet", "smelt", "refDepth", "totalRefDepth", "cf", "Taufb", "Taufd", "Qsib", "Qsid", "Taub", "Taud", "Qsns", "Qsnc", "Qlns", "Qlnc", "Vz", "Rkinsc", "Rkinc", "Inmax", "intc", "ieff", "Ur", "Wc", "Tc", "Tac", "QHc", "QEc", "Ec", "Qpc", "Qmc", "Mc", "FMc", "SWIGM", "SWISM", "SWIR", "errMB") C_zName = c_char_p("Outletlocations") C_tcorvar = pointer((c_float * 13)()) self.C_tsvarArray = pointer((c_float * 13)()) C_tsvarArrayTemp = pointer((c_float * 5)()) #todo: [#] == pointer, * == pointer self.C_tsvarArray = pointer((POINTER(c_float)*13)()) C_ntimesteps = pointer((c_int * 5)()) # create pointer to instance of sitevar struct array self.C_strsvArray = pointer((sitevar * 32)()) # create pointer to instance of inpforcvar struct array self.C_strinpforcArray = pointer((inpforcvar * 13)()) # mask = c_float() # pcap_lookupnet(dev, ctypes.byref(net), ctypes.byref(mask), errbuf) # read watershed netcdf file self.__uebLib.readwsncFile(C_watershedFile, C_wsvarName, C_wsycorName, C_wsxcorName, byref(C_wsycorArray), byref(C_wsxcorArray), byref(self.C_wsArray), byref(self.C_dimlen1), byref(self.C_dimlen2), byref(self.C_wsfillVal)) wsArray1D = numpy.empty((self.C_dimlen1.value*self.C_dimlen2.value),dtype=numpy.float) for i in xrange(self.C_dimlen1.value) : for j in xrange(self.C_dimlen2.value): wsArray1D[i*self.C_dimlen2.value + j] = self.C_wsArray[i][j]; # zvalues is the unique set of wsArray1D zValues = list(set(wsArray1D)) # fillset = [wsfillVal] # zVal is the set of zValues that do not equal wsFillVal zVal = [zValues[i] for i in xrange(len(zValues)) if zValues[i] != self.C_wsfillVal] C_nZones = len(zVal) z_ycor = [0.0 for i in xrange(C_nZones)] z_xcor = [0.0 for i in xrange(C_nZones)] # read params (#194) self.__uebLib.readParams(C_paramFile, byref(self.C_parvalArray), npar) # read site variables (#200) self.__uebLib.readSiteVars(C_sitevarFile, byref(self.C_strsvArray)) # read 2d NetCDF Data for i in range(0,32): a = self.C_strsvArray.contents[i] if a.svType == 1: # print "%d %s %s\n" % (i, a.svFile,a.svVarName) retvalue = self.__uebLib.read2DNC(os.path.join(self.base_dir, a.svFile), a.svVarName, byref(a.svArrayValues)) #//read input /forcing control file--all possible entries of input control have to be provided #readInputForcVars(inputconFile, strinpforcArray); # read input force variables (main.cpp, line 219) self.__uebLib.readInputForcVars(cast(C_inputconFile,c_char_p), self.C_strinpforcArray) # elog.info('UEB Start Date: %s' % sd.strftime("%m-%d-%Y %H:%M:%S")) # elog.info('UEB End Date: %s' % ed.strftime("%m-%d-%Y %H:%M:%S")) # calculate model time span as a julian date (main.cpp, line 220) modelSpan = jdutil.datetime_to_jd(datetime.datetime(*ModelEndDate)) - \ jdutil.datetime_to_jd(datetime.datetime(*ModelStartDate)) # setup the model start dates as UEB expects them ModelStartHour = ModelStartDate.pop(-1) # an integer representing the start hour (24 hour time) ModelEndHour = ModelEndDate.pop(-1) # an integer representing the end hour (24 hour time) # ModelStartDate is a 3 element array: [year, month, day] # ModelEndDate is a 3 element array: [year, month, day] # convert Simulation Time parameters into ctypes self.C_ModelStartDate = (c_int * len(ModelStartDate))(*ModelStartDate) self.C_ModelEndDate =(c_int * len(ModelEndDate))(*ModelEndDate) self.C_ModelDt = c_double(ModelDt) self.C_ModelUTCOffset = c_double(ModelUTCOffset) self.C_ModelStartHour = c_double(ModelStartHour) self.C_ModelEndHour = c_double(ModelEndHour) # calculate model time steps (main.cpp, line 222) self.numTimeStep = int(math.ceil(modelSpan*(24./ModelDt)) ) + 1 # initialize C_tsvarArray values (this replaces __uebLib.readTextData) self.initialize_timeseries_variable_array(self.C_strinpforcArray, self.numTimeStep) # NOTE: C_strinpforcArray stores info about the forcing data files # # read forcing data (main.cpp, line 226) # if self.C_strsvArray.contents[16].svType != 3: # no accumulation zone (fixme: ???) # for it in xrange(13): # inftype = self.C_strinpforcArray.contents[it].infType # print 'infFile: ',self.C_strinpforcArray.contents[it].infFile # if inftype == 0: # # # read the files stored in C_strinpforcArray and populated C_tsvarArray # self.__uebLib.readTextData(os.path.join(self.base_dir, self.C_strinpforcArray.contents[it].infFile), byref(self.C_tsvarArray.contents[it]), byref(C_ntimesteps[0])) # # elif inftype == 2 or inftype == -1: # self.C_tsvarArray.contents[it] = (c_float * 2)() # C_ntimesteps.contents[0] = 2 # # copy the default value if a single value is the option # self.C_tsvarArray.contents[it][0] = self.C_strinpforcArray.contents[it].infType # self.C_tsvarArray.contents[it][1] = self.C_strinpforcArray.contents[it].infdefValue # :: this array is initialized to (numOut+1, numTimeStep+1) rather than (numOut, numTimeStep) # :: b/c otherwise the calculations from RunUEB are incorrect for the first row. # :: e.g. # :: 2009 2010 5 30.000 23.999979 # :: rather than: # :: 2009 10 1 0.000 0.569902 # :: # :: I thought this was b/c numpy.float32 (and 64) are smaller than c_float, however this change # :: below didn't fix the problem. # :: # create a numpy array for outputs self.outvarArray = numpy.zeros(shape=(numOut+1, self.numTimeStep), dtype=numpy.float, order="C") # arrays_old = self.outvarArray.astype(numpy.float32) arrays = self.outvarArray.astype(c_float) rows, cols = self.outvarArray.shape arrays_as_list = list(arrays) #get ctypes handles ctypes_arrays = [numpy.ctypeslib.as_ctypes(array) for array in arrays_as_list] #Pack into pointer array self.C_outvarArray = (POINTER(c_float) * rows)(*ctypes_arrays) # x = numpy.zeros(shape=(numOut, self.numTimeStep), dtype=numpy.float, order="C") # _floatpp = numpy.ctypeslib.ndpointer(dtype=numpy.uintp, ndim=1, flags='C') # xpp = (x.__array_interface__['data'][0] + numpy.arange(x.shape[0])*x.strides[0]).astype(numpy.uintp) # self.C_outvarArray = pointer(((POINTER(c_float) * self.numTimeStep) * numOut)()) # a = (c_float * self.numTimeStep)() # outvarArray = pointer((a * numOut)()) # for i in xrange(numOut): # outvarArray[i] = pointer((c_float * self.numTimeStep)()) # total grid size to compute progess totalgrid = self.C_dimlen1.value*self.C_dimlen2.value # read output control file (main.cpp, line 251) # readOutputControl(outputconFile, aggoutputconFile, pOut, ncOut, aggOut, npout, nncout, naggout); self.__uebLib.readOutputControl(cast(C_outputconFile,c_char_p), cast(C_aggoutputconFile, c_char_p), byref(self.C_pOut), byref(self.C_ncOut), byref(C_aggOut), byref(self.C_npout), byref(self.C_nncout), byref(C_naggout)) # create output netcdf self.C_outtSteps = self.numTimeStep / self.outtStride self.t_out = numpy.empty(shape=(self.C_outtSteps), dtype=numpy.float, order="C") for i in xrange(self.C_outtSteps): self.t_out[i] = i*self.outtStride*ModelDt # initialize the output arrays aggoutvarArray = numpy.zeros((C_nZones,C_naggout.value, self.C_outtSteps), dtype=numpy.float) totalAgg = numpy.empty((self.C_outtSteps,), dtype=numpy.float) ZonesArr = numpy.zeros((C_nZones,), dtype=numpy.int32) # main.cpp, line 290 # CREATE 3D NC OUTPUT FILES # convert self.t_out into a float pointer C_t_out = self.t_out.ctypes.data_as(POINTER(c_float)) for i in xrange(self.C_nncout.value): ''' for (int icout = 0; icout < nncout; icout++) retvalue = create3DNC_uebOutputs(ncOut[icout].outfName, (const char*)ncOut[icout].symbol, (const char*)ncOut[icout].units, tNameout, tUnitsout, tlong_name, tcalendar, outtSteps, outDimord, self.t_out, &out_fillVal, watershedFile, wsvarName, wsycorName, wsxcorName); ''' retvalue = self.__uebLib.create3DNC_uebOutputs(self.C_ncOut[i].outfName, cast(self.C_ncOut[i].symbol, c_char_p), cast(self.C_ncOut[i].units, c_char_p), C_tNameout, C_tUnitsout, C_tlong_name, C_tcalendar, self.C_outtSteps, self.C_outDimord, C_t_out, byref(C_out_fillVal), C_watershedFile, C_wsvarName, C_wsycorName, C_wsxcorName); # CREATE 3D NC AGGREGATE OUTPUT FILE # convert z_ycor and x_xcor from list into ctype C_z_xcor = numpy.asarray(z_xcor).ctypes.data_as(POINTER(c_float)) C_z_ycor = numpy.asarray(z_ycor).ctypes.data_as(POINTER(c_float)) retvalue = self.__uebLib.create3DNC_uebAggregatedOutputs(C_aggoutputFile, C_aggOut, C_naggout, C_tNameout, C_tUnitsout, C_tlong_name, C_tcalendar, self.C_outtSteps, C_aggoutDimord, C_t_out, byref(C_out_fillVal), C_watershedFile, C_wsvarName, C_wsycorName, C_wsxcorName, C_nZones, C_zName, C_z_ycor, C_z_xcor); # todo: create output element set # print 'Output Calculations available at: ' # for pid in xrange(self.C_npout.value): # print " Point(",self.C_pOut[pid].xcoord,", ",self.C_pOut[pid].ycoord,') ' # todo: This is where UEB grid points are defined!, expose these as input/output spatial objects # main.cpp, line 303 self.activeCells = [] # print 'Calculations will be performed at: ' for iy in xrange(self.C_dimlen1.value): for jx in xrange(self.C_dimlen2.value): if self.C_wsArray[iy][jx] != self.C_wsfillVal.value and self.C_strsvArray.contents[16].svType != 3: # print " Point(",jx,", ",iy,') ' self.activeCells.append((iy, jx)) # build output exchange items xcoords = [] ycoords = [] for pid in xrange(self.C_npout.value): xcoords.append(self.C_pOut[pid].xcoord) ycoords.append(self.C_pOut[pid].ycoord) self.pts = geometry.build_point_geometries(xcoords, ycoords) self.__swe = self.outputs()['Snow Water Equivalent'] self.__swit = self.outputs()['Surface Water Input Total'] self.__swe.addGeometries2(self.pts) self.__swit.addGeometries2(self.pts) # build input exchange items ds = nc.Dataset(C_watershedFile) Xlist = ds.variables['x'] Ylist = ds.variables['y'] self.geoms = self.build_geometries(Xlist, Ylist) self.inputs()['Precipitation'].addGeometries2(self.geoms) self.inputs()['Temperature'].addGeometries2(self.geoms) # set start, end, and timestep parameters ts = datetime.timedelta(hours=ModelDt) self.time_step(ts.total_seconds()) sd = datetime.datetime(*ModelStartDate) ed = datetime.datetime(*ModelEndDate) self.simulation_start(sd) self.simulation_end(ed)
def __init__(self,config_params): """ initialization that will occur when loaded into a configuration """ super(topmodel,self).__init__(config_params) if LooseVersion(np.__version__) < LooseVersion('1.9.0'): elog.error('Could not load TOPMODEL, NumPY version 1.9.0 or greater required') sPrint('Could not load TOPMODEL, NumPY version 1.9.0 or greater required', MessageType.ERROR) raise Exception('Could not load TOPMODEL, NumPY version 1.9.0 or greater required') sPrint('Begin Component Initialization') # build inputs and outputs sPrint('Building exchange items') io = mdl.build_exchange_items_from_config(config_params) # set inputs and outputs self.inputs(value=io[stdlib.ExchangeItemType.INPUT]) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT]) # read input parameters sPrint('Reading input parameters') #read model input parameters self.topo_input = config_params['ti'] self.fac_input = config_params['fac'] self.c = config_params['m'] self.Tmax = config_params["tmax"] self.R = config_params["r"] self.interception = config_params["interception"] self.ti = [] self.freq = [] # read topographic input file sPrint('Reading topographic input data') self.read_topo_input() sPrint('Building input/output geometries') self.ti_geoms = None self.output_soil_moisture_geoms = None self.calc_ti_geometries() # set precipitation geometries sPrint('Setting excess precipitation geometries') self.outputs()['excess'].addGeometries2(self.ti_geoms) # set saturation geometries # elog.info('Setting soil saturation geometries') # self.outputs()['soil moisture'].addGeometries2(self.ti_geoms) # ---- calculate saturation deficit sPrint('Calculating initial saturation deficit') TI_freq = [x*y for x,y in zip(self.ti, self.freq)] self.lamda_average = sum(TI_freq) / sum(self.freq) # catchment average saturation deficit(S_bar) self.s_average = (-1.)*self.c * ((math.log10(self.R / self.Tmax)) + self.lamda_average) sPrint('Component Initialization Completed Successfully')
def __init__(self, config_params): """ initialization that will occur when loaded into a configuration """ super(topmodel, self).__init__(config_params) if LooseVersion(np.__version__) < LooseVersion('1.9.0'): elog.error( 'Could not load TOPMODEL, NumPY version 1.9.0 or greater required' ) sPrint( 'Could not load TOPMODEL, NumPY version 1.9.0 or greater required', MessageType.ERROR) raise Exception( 'Could not load TOPMODEL, NumPY version 1.9.0 or greater required' ) sPrint('Begin Component Initialization') # build inputs and outputs sPrint('Building exchange items') io = mdl.build_exchange_items_from_config(config_params) # set inputs and outputs self.inputs(value=io[stdlib.ExchangeItemType.INPUT]) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT]) # read input parameters sPrint('Reading input parameters') #read model input parameters self.topo_input = config_params['ti'] self.fac_input = config_params['fac'] self.c = config_params['m'] self.Tmax = config_params["tmax"] self.R = config_params["r"] self.interception = config_params["interception"] self.ti = [] self.freq = [] # read topographic input file sPrint('Reading topographic input data') self.read_topo_input() sPrint('Building input/output geometries') self.ti_geoms = None self.output_soil_moisture_geoms = None self.calc_ti_geometries() # set precipitation geometries sPrint('Setting excess precipitation geometries') self.outputs()['excess'].addGeometries2(self.ti_geoms) # set saturation geometries # elog.info('Setting soil saturation geometries') # self.outputs()['soil moisture'].addGeometries2(self.ti_geoms) # ---- calculate saturation deficit sPrint('Calculating initial saturation deficit') TI_freq = [x * y for x, y in zip(self.ti, self.freq)] self.lamda_average = sum(TI_freq) / sum(self.freq) # catchment average saturation deficit(S_bar) self.s_average = (-1.) * self.c * ( (math.log10(self.R / self.Tmax)) + self.lamda_average) sPrint('Component Initialization Completed Successfully')
def __init__(self, config_params): """ initialization that will occur when loaded into a configuration """ super(topmodel, self).__init__(config_params) if LooseVersion(np.__version__) < LooseVersion("1.9.0"): elog.error("Could not load TOPMODEL, NumPY version 1.9.0 or greater required") raise Exception("Could not load TOPMODEL, NumPY version 1.9.0 or greater required") elog.info("Begin Component Initialization") # build inputs and outputs elog.info("Building exchange items") io = mdl.build_exchange_items_from_config(config_params) # set inputs and outputs self.inputs(value=io[stdlib.ExchangeItemType.INPUT]) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT]) # model_inputs inputs = config_params["model inputs"][0] # read input parameters elog.info("Reading input parameters") self.topo_input = inputs["ti"] self.fac_input = inputs["fac"] # read model input parameters self.c = float(inputs["m"]) self.Tmax = float(inputs["tmax"]) self.R = float(inputs["r"]) self.interception = float(inputs["interception"]) self.ti = [] self.freq = [] # read topographic input file elog.info("Reading topographic input data") self.read_topo_input() elog.info("Building input/output geometries") self.ti_geoms = None self.output_soil_moisture_geoms = None self.calc_ti_geometries() # set precipitation geometries elog.info("Setting excess precipitation geometries") self.outputs()["excess"].addGeometries2(self.ti_geoms) # set saturation geometries # elog.info('Setting soil saturation geometries') # self.outputs()['soil moisture'].addGeometries2(self.ti_geoms) # ---- calculate saturation deficit elog.info("Calculating initial saturation deficit") TI_freq = [x * y for x, y in zip(self.ti, self.freq)] self.lamda_average = sum(TI_freq) / sum(self.freq) # catchment average saturation deficit(S_bar) self.s_average = (-1.0) * self.c * ((math.log10(self.R / self.Tmax)) + self.lamda_average) elog.info("Component Initialization Completed Successfully")
def __init__(self, config_params): super(ueb, self).__init__(config_params) # build inputs and outputs io = mdl.build_exchange_items_from_config(config_params) # set input and output exchange items self.inputs(value=io[stdlib.ExchangeItemType.INPUT]) self.outputs(value=io[stdlib.ExchangeItemType.OUTPUT]) # grab the C library path and the control file path lib = config_params['lib'] conFile = config_params['control'] # load the UEB C library self.__uebLib = cdll.LoadLibrary(join(os.path.dirname(__file__), lib)) # save the current directory for saving output data self.curdir = os.path.dirname(os.path.abspath(conFile)) # the base directory for the control file is used to convert relative paths into absolute paths self.base_dir = os.path.dirname(conFile) # get param, sitevar, input, output, and watershed files with open(conFile, 'r') as f: # lines = f.readlines() lines = f.read().splitlines() # this will auto strip the \n \r C_paramFile = os.path.join(self.base_dir, lines[1]) C_sitevarFile = os.path.join(self.base_dir, lines[2]) C_inputconFile = os.path.join(self.base_dir, lines[3]) C_outputconFile = os.path.join(self.base_dir, lines[4]) C_watershedFile = os.path.join(self.base_dir, lines[5]) C_wsvarName = lines[6].split(' ')[0] C_wsycorName = lines[6].split(' ')[1] C_wsxcorName = lines[6].split(' ')[2] C_aggoutputconFile = os.path.join(self.base_dir, lines[7]) C_aggoutputFile = os.path.join(self.base_dir, lines[8]) ModelStartDate = [ int(float(l)) for l in lines[9].split(' ') if l != '' ] ModelEndDate = [ int(float(l)) for l in lines[10].split(' ') if l != '' ] ModelDt = float(lines[11]) ModelUTCOffset = float(lines[12]) inpDailyorSubdaily = bool(lines[13] == True) self.outtStride, outyStep, outxStep = [ int(s) for s in lines[14].split(' ') ] C_wsxcorArray = c_float() C_wsycorArray = c_float() self.C_wsArray = pointer(pointer(c_int32())) self.C_dimlen1 = c_int() self.C_dimlen2 = c_int() totalgrid = 0 self.C_wsfillVal = c_int(-9999) npar = c_int(32) tinitTime = c_int(0) self.C_parvalArray = pointer(c_float(0)) numOut = 70 # hack: number of outputs? self.C_pOut = pointer(pointOutput()) C_aggOut = pointer(aggOutput()) self.C_ncOut = pointer(ncOutput()) self.C_npout = c_int(0) self.C_nncout = c_int(0) C_naggout = c_int(0) C_nZones = c_int(0) C_tNameout = c_char_p("time") tunits = (c_char * 256)() C_tUnitsout = pointer(tunits) C_tlong_name = c_char_p("time") C_tcalendar = c_char_p("standard") self.t_out = pointer(c_float(0)) C_out_fillVal = c_float(-9999.0) self.C_outDimord = c_int(0) C_aggoutDimord = c_int(1) self.outvarindx = c_int(17) # aggoutvarindx = c_int(17) # size = c_int() # rank = c_int() # irank = c_int() # jrank = c_int() # startTimeT = c_double(0.0) # TotalTime = c_double(0.0) # totalmodelrunTime = c_double(0.0) # TsReadTime = c_double(0.0) # TSStartTime = c_double() # ComputeStartTime = c_double() # ComputeTime = c_double(0.0) # OutWriteTime = c_double() self.uebVars = (c_char_p * 70)( "Year", "Month", "Day", "dHour", "atff", "HRI", "Eacl", "Ema", "conZen", "Ta", "P", "V", "RH", "Qsi", "Qli", "Qnet", "Us", "SWE", "tausn", "Pr", "Ps", "Alb", "QHs", "QEs", "Es", "SWIT", "QMs", "Q", "FM", "Tave", "TSURFs", "cump", "cumes", "cumMr", "Qnet", "smelt", "refDepth", "totalRefDepth", "cf", "Taufb", "Taufd", "Qsib", "Qsid", "Taub", "Taud", "Qsns", "Qsnc", "Qlns", "Qlnc", "Vz", "Rkinsc", "Rkinc", "Inmax", "intc", "ieff", "Ur", "Wc", "Tc", "Tac", "QHc", "QEc", "Ec", "Qpc", "Qmc", "Mc", "FMc", "SWIGM", "SWISM", "SWIR", "errMB") C_zName = c_char_p("Outletlocations") C_tcorvar = pointer((c_float * 13)()) self.C_tsvarArray = pointer((c_float * 13)()) C_tsvarArrayTemp = pointer((c_float * 5)()) #todo: [#] == pointer, * == pointer self.C_tsvarArray = pointer((POINTER(c_float) * 13)()) C_ntimesteps = pointer((c_int * 5)()) # create pointer to instance of sitevar struct array self.C_strsvArray = pointer((sitevar * 32)()) # create pointer to instance of inpforcvar struct array self.C_strinpforcArray = pointer((inpforcvar * 13)()) # mask = c_float() # pcap_lookupnet(dev, ctypes.byref(net), ctypes.byref(mask), errbuf) # read watershed netcdf file self.__uebLib.readwsncFile(C_watershedFile, C_wsvarName, C_wsycorName, C_wsxcorName, byref(C_wsycorArray), byref(C_wsxcorArray), byref(self.C_wsArray), byref(self.C_dimlen1), byref(self.C_dimlen2), byref(self.C_wsfillVal)) wsArray1D = numpy.empty((self.C_dimlen1.value * self.C_dimlen2.value), dtype=numpy.float) for i in xrange(self.C_dimlen1.value): for j in xrange(self.C_dimlen2.value): wsArray1D[i * self.C_dimlen2.value + j] = self.C_wsArray[i][j] # zvalues is the unique set of wsArray1D zValues = list(set(wsArray1D)) # fillset = [wsfillVal] # zVal is the set of zValues that do not equal wsFillVal zVal = [ zValues[i] for i in xrange(len(zValues)) if zValues[i] != self.C_wsfillVal ] C_nZones = len(zVal) z_ycor = [0.0 for i in xrange(C_nZones)] z_xcor = [0.0 for i in xrange(C_nZones)] # read params (#194) self.__uebLib.readParams(C_paramFile, byref(self.C_parvalArray), npar) # read site variables (#200) self.__uebLib.readSiteVars(C_sitevarFile, byref(self.C_strsvArray)) # read 2d NetCDF Data for i in range(0, 32): a = self.C_strsvArray.contents[i] if a.svType == 1: # print "%d %s %s\n" % (i, a.svFile,a.svVarName) retvalue = self.__uebLib.read2DNC( os.path.join(self.base_dir, a.svFile), a.svVarName, byref(a.svArrayValues)) #//read input /forcing control file--all possible entries of input control have to be provided #readInputForcVars(inputconFile, strinpforcArray); # read input force variables (main.cpp, line 219) self.__uebLib.readInputForcVars(cast(C_inputconFile, c_char_p), self.C_strinpforcArray) # elog.info('UEB Start Date: %s' % sd.strftime("%m-%d-%Y %H:%M:%S")) # elog.info('UEB End Date: %s' % ed.strftime("%m-%d-%Y %H:%M:%S")) # calculate model time span as a julian date (main.cpp, line 220) modelSpan = jdutil.datetime_to_jd(datetime.datetime(*ModelEndDate)) - \ jdutil.datetime_to_jd(datetime.datetime(*ModelStartDate)) # setup the model start dates as UEB expects them ModelStartHour = ModelStartDate.pop( -1) # an integer representing the start hour (24 hour time) ModelEndHour = ModelEndDate.pop( -1) # an integer representing the end hour (24 hour time) # ModelStartDate is a 3 element array: [year, month, day] # ModelEndDate is a 3 element array: [year, month, day] # convert Simulation Time parameters into ctypes self.C_ModelStartDate = (c_int * len(ModelStartDate))(*ModelStartDate) self.C_ModelEndDate = (c_int * len(ModelEndDate))(*ModelEndDate) self.C_ModelDt = c_double(ModelDt) self.C_ModelUTCOffset = c_double(ModelUTCOffset) self.C_ModelStartHour = c_double(ModelStartHour) self.C_ModelEndHour = c_double(ModelEndHour) # calculate model time steps (main.cpp, line 222) self.numTimeStep = int(math.ceil(modelSpan * (24. / ModelDt))) + 1 # initialize C_tsvarArray values (this replaces __uebLib.readTextData) self.initialize_timeseries_variable_array(self.C_strinpforcArray, self.numTimeStep) # NOTE: C_strinpforcArray stores info about the forcing data files # # read forcing data (main.cpp, line 226) # if self.C_strsvArray.contents[16].svType != 3: # no accumulation zone (fixme: ???) # for it in xrange(13): # inftype = self.C_strinpforcArray.contents[it].infType # print 'infFile: ',self.C_strinpforcArray.contents[it].infFile # if inftype == 0: # # # read the files stored in C_strinpforcArray and populated C_tsvarArray # self.__uebLib.readTextData(os.path.join(self.base_dir, self.C_strinpforcArray.contents[it].infFile), byref(self.C_tsvarArray.contents[it]), byref(C_ntimesteps[0])) # # elif inftype == 2 or inftype == -1: # self.C_tsvarArray.contents[it] = (c_float * 2)() # C_ntimesteps.contents[0] = 2 # # copy the default value if a single value is the option # self.C_tsvarArray.contents[it][0] = self.C_strinpforcArray.contents[it].infType # self.C_tsvarArray.contents[it][1] = self.C_strinpforcArray.contents[it].infdefValue # :: this array is initialized to (numOut+1, numTimeStep+1) rather than (numOut, numTimeStep) # :: b/c otherwise the calculations from RunUEB are incorrect for the first row. # :: e.g. # :: 2009 2010 5 30.000 23.999979 # :: rather than: # :: 2009 10 1 0.000 0.569902 # :: # :: I thought this was b/c numpy.float32 (and 64) are smaller than c_float, however this change # :: below didn't fix the problem. # :: # create a numpy array for outputs self.outvarArray = numpy.zeros(shape=(numOut + 1, self.numTimeStep), dtype=numpy.float, order="C") # arrays_old = self.outvarArray.astype(numpy.float32) arrays = self.outvarArray.astype(c_float) rows, cols = self.outvarArray.shape arrays_as_list = list(arrays) #get ctypes handles ctypes_arrays = [ numpy.ctypeslib.as_ctypes(array) for array in arrays_as_list ] #Pack into pointer array self.C_outvarArray = (POINTER(c_float) * rows)(*ctypes_arrays) # x = numpy.zeros(shape=(numOut, self.numTimeStep), dtype=numpy.float, order="C") # _floatpp = numpy.ctypeslib.ndpointer(dtype=numpy.uintp, ndim=1, flags='C') # xpp = (x.__array_interface__['data'][0] + numpy.arange(x.shape[0])*x.strides[0]).astype(numpy.uintp) # self.C_outvarArray = pointer(((POINTER(c_float) * self.numTimeStep) * numOut)()) # a = (c_float * self.numTimeStep)() # outvarArray = pointer((a * numOut)()) # for i in xrange(numOut): # outvarArray[i] = pointer((c_float * self.numTimeStep)()) # total grid size to compute progess totalgrid = self.C_dimlen1.value * self.C_dimlen2.value # read output control file (main.cpp, line 251) # readOutputControl(outputconFile, aggoutputconFile, pOut, ncOut, aggOut, npout, nncout, naggout); self.__uebLib.readOutputControl(cast(C_outputconFile, c_char_p), cast(C_aggoutputconFile, c_char_p), byref(self.C_pOut), byref(self.C_ncOut), byref(C_aggOut), byref(self.C_npout), byref(self.C_nncout), byref(C_naggout)) # create output netcdf self.C_outtSteps = self.numTimeStep / self.outtStride self.t_out = numpy.empty(shape=(self.C_outtSteps), dtype=numpy.float, order="C") for i in xrange(self.C_outtSteps): self.t_out[i] = i * self.outtStride * ModelDt # initialize the output arrays aggoutvarArray = numpy.zeros( (C_nZones, C_naggout.value, self.C_outtSteps), dtype=numpy.float) totalAgg = numpy.empty((self.C_outtSteps, ), dtype=numpy.float) ZonesArr = numpy.zeros((C_nZones, ), dtype=numpy.int32) # main.cpp, line 290 # CREATE 3D NC OUTPUT FILES # convert self.t_out into a float pointer C_t_out = self.t_out.ctypes.data_as(POINTER(c_float)) for i in xrange(self.C_nncout.value): ''' for (int icout = 0; icout < nncout; icout++) retvalue = create3DNC_uebOutputs(ncOut[icout].outfName, (const char*)ncOut[icout].symbol, (const char*)ncOut[icout].units, tNameout, tUnitsout, tlong_name, tcalendar, outtSteps, outDimord, self.t_out, &out_fillVal, watershedFile, wsvarName, wsycorName, wsxcorName); ''' retvalue = self.__uebLib.create3DNC_uebOutputs( self.C_ncOut[i].outfName, cast(self.C_ncOut[i].symbol, c_char_p), cast(self.C_ncOut[i].units, c_char_p), C_tNameout, C_tUnitsout, C_tlong_name, C_tcalendar, self.C_outtSteps, self.C_outDimord, C_t_out, byref(C_out_fillVal), C_watershedFile, C_wsvarName, C_wsycorName, C_wsxcorName) # CREATE 3D NC AGGREGATE OUTPUT FILE # convert z_ycor and x_xcor from list into ctype C_z_xcor = numpy.asarray(z_xcor).ctypes.data_as(POINTER(c_float)) C_z_ycor = numpy.asarray(z_ycor).ctypes.data_as(POINTER(c_float)) retvalue = self.__uebLib.create3DNC_uebAggregatedOutputs( C_aggoutputFile, C_aggOut, C_naggout, C_tNameout, C_tUnitsout, C_tlong_name, C_tcalendar, self.C_outtSteps, C_aggoutDimord, C_t_out, byref(C_out_fillVal), C_watershedFile, C_wsvarName, C_wsycorName, C_wsxcorName, C_nZones, C_zName, C_z_ycor, C_z_xcor) # todo: create output element set # print 'Output Calculations available at: ' # for pid in xrange(self.C_npout.value): # print " Point(",self.C_pOut[pid].xcoord,", ",self.C_pOut[pid].ycoord,') ' # todo: This is where UEB grid points are defined!, expose these as input/output spatial objects # main.cpp, line 303 self.activeCells = [] # print 'Calculations will be performed at: ' for iy in xrange(self.C_dimlen1.value): for jx in xrange(self.C_dimlen2.value): if self.C_wsArray[iy][ jx] != self.C_wsfillVal.value and self.C_strsvArray.contents[ 16].svType != 3: # print " Point(",jx,", ",iy,') ' self.activeCells.append((iy, jx)) # build output exchange items xcoords = [] ycoords = [] for pid in xrange(self.C_npout.value): xcoords.append(self.C_pOut[pid].xcoord) ycoords.append(self.C_pOut[pid].ycoord) self.pts = geometry.build_point_geometries(xcoords, ycoords) self.__swe = self.outputs()['Snow Water Equivalent'] self.__swit = self.outputs()['Surface Water Input Total'] self.__swe.addGeometries2(self.pts) self.__swit.addGeometries2(self.pts) # build input exchange items ds = nc.Dataset(C_watershedFile) Xlist = ds.variables['x'] Ylist = ds.variables['y'] self.geoms = self.build_geometries(Xlist, Ylist) self.inputs()['Precipitation'].addGeometries2(self.geoms) self.inputs()['Temperature'].addGeometries2(self.geoms) # set start, end, and timestep parameters ts = datetime.timedelta(hours=ModelDt) self.time_step(ts.total_seconds()) sd = datetime.datetime(*ModelStartDate) ed = datetime.datetime(*ModelEndDate) self.simulation_start(sd) self.simulation_end(ed)