Ejemplo n.º 1
0
    def read_fertiliser_price(self, ncFile, ncVarName, date=None):

        fert_price = vos.netcdf2PCRobjClone(ncFile,
                                            ncVarName,
                                            date,
                                            useDoy=None,
                                            cloneMapFileName=self.var.cloneMap,
                                            LatitudeLongitude=True)
        fert_price = fert_price[self.var.landmask]
        return fert_price
Ejemplo n.º 2
0
    def dynamic(self):

        # TODO: only read data if the year has changed
        
        if self.var._modelTime.timeStepPCR == 1 or self.var._modelTime.doy == 1:
            date = '%04i-%02i-%02i' %(self.var._modelTime.year, 1, 1)
            self.var.conc = vos.netcdf2PCRobjClone(self.var.co2FileNC,
                                               self.var.co2VarName,
                                               date,
                                               useDoy = None,
                                               cloneMapFileName = self.var.cloneMap,
                                               LatitudeLongitude = True)
Ejemplo n.º 3
0
    def read(self):

        # reading canal supply:
        if self.include_canal_system:
            if self.VariableCanalSupply:
                if self.DailyCanalNC:
                    # introduce this test so that we do not ask the model to read
                    # a file from a timestep prior to the current simulation.
                    if not (self._modelTime.isFirstTimestep() &
                            (self.canalTimeLag > 0)):

                        day, month, year = self._modelTime.day, self._modelTime.month, self._modelTime.year
                        canalFileNC = self.canalFileNC.format(day=day,
                                                              month=month,
                                                              year=year)
                        exists = os.path.exists(canalFileNC)
                        max_wait_time = 60
                        wait_time = 0.1
                        total_wait_time = 0
                        while exists is False and total_wait_time <= max_wait_time:
                            time.sleep(wait_time)
                            exists = os.path.exists(canalFileNC)
                            total_wait_time += wait_time

                        if not exists:
                            msg = "canal file doesn't exist and maximum wait time exceeded"
                            raise AQError(msg)

                        CanalSupply = vos.netcdf2PCRobjCloneWithoutTime(
                            canalFileNC,
                            self.canalVarName,
                            cloneMapFileName=self.cloneMap,
                            LatitudeLongitude=True)
                        self.CanalSupply = CanalSupply[self.landmask]

                else:
                    CanalSupply = vos.netcdf2PCRobjClone(
                        self.canalFileNC,
                        self.canalVarName,
                        str(currTimeStep.fulldate),
                        useDoy=method_for_time_index,
                        cloneMapFileName=self.cloneMap,
                        LatitudeLongitude=True)
                    self.CanalSupply = CanalSupply[self.landmask]

            else:
                CanalSupply = vos.netcdf2PCRobjCloneWithoutTime(
                    self.canalFileNC,
                    self.canalVarName,
                    cloneMapFileName=self.cloneMap,
                    LatitudeLongitude=True)
                self.CanalSupply = CanalSupply[self.landmask]
Ejemplo n.º 4
0
    def update_cover_fraction(self):

        # TODO: make flexible the day on which land cover is changed

        start_of_model_run = (self.var._modelTime.timeStepPCR == 1)
        start_of_year = (self.var._modelTime.doy == 1)
        if self.dynamicLandCover:
            if start_of_model_run or start_of_year:
                date = datetime.datetime(self.var._modelTime.year, 1, 1, 0, 0,
                                         0)
                self.var.coverFraction = vos.netcdf2PCRobjClone(
                    self.coverFractionNC.format(
                        day=self.var._modelTime.currTime.day,
                        month=self.var._modelTime.currTime.month,
                        year=self.var._modelTime.currTime.year),
                    self.coverFractionVarName,
                    date,
                    # useDoy = method_for_time_index,
                    # cloneMapAttributes = self.cloneMapAttributes,
                    cloneMapFileName=self.var.cloneMap,
                    LatitudeLongitude=True)[self.var.landmask]
        else:
            if start_of_model_run:
                date = datetime.datetime(self.staticLandCoverYear, 1, 1, 0, 0,
                                         0)
                self.var.coverFraction = vos.netcdf2PCRobjClone(
                    self.coverFractionNC.format(
                        day=self.var._modelTime.currTime.day,
                        month=self.var._modelTime.currTime.month,
                        year=self.var._modelTime.currTime.year),
                    self.coverFractionVarName,
                    date,
                    # useDoy = method_for_time_index,
                    # cloneMapAttributes = self.cloneMapAttributes,
                    cloneMapFileName=self.var.cloneMap,
                    LatitudeLongitude=True)[self.var.landmask]
Ejemplo n.º 5
0
 def set_diesel_price(self):
     start_of_model_run = (self.var._modelTime.timeStepPCR == 1)
     start_of_year = (self.var._modelTime.doy == 1)
     if not self.DieselPriceFileNC == "None":
         if start_of_model_run or start_of_year:
             date = datetime.datetime(self.var._modelTime.year, 1, 1, 0, 0,
                                      0)
             diesel_price = vos.netcdf2PCRobjClone(
                 self.DieselPriceFileNC,
                 self.DieselPriceVarName,
                 date,
                 useDoy=None,
                 cloneMapFileName=self.var.cloneMap,
                 LatitudeLongitude=True)
             diesel_price = diesel_price[self.var.landmask]
             self.var.DieselPrice = diesel_price
Ejemplo n.º 6
0
    def update_crop_coefficient(self):

        start_of_model_run = (self.var._modelTime.timeStepPCR == 1)
        if start_of_model_run or (self.var._modelTime.day in [1, 11, 21]):
            self.var.cropCoefficient = vos.netcdf2PCRobjClone(
                self.cropCoefficientNC.format(
                    day=self.var._modelTime.currTime.day,
                    month=self.var._modelTime.currTime.month,
                    year=self.var._modelTime.currTime.year),
                self.cropCoefficientVarName,
                datetime.datetime(2000, self.var._modelTime.currTime.month,
                                  self.var._modelTime.currTime.day),
                # useDoy = method_for_time_index,
                # cloneMapAttributes = self.cloneMapAttributes,
                cloneMapFileName=self.var.cloneMap,
                LatitudeLongitude=True)[self.var.landmask]
Ejemplo n.º 7
0
 def read_potential_crop_yield(self):
     if self.var.AnnualChangeInPotYield:
         if self.var._modelTime.timeStepPCR == 1 or self.var._modelTime.doy == 1:
             date = '%04i-%02i-%02i' % (self.var._modelTime.year, 1, 1)
             self.var.Yx = vos.netcdf2PCRobjClone(
                 self.var.PotYieldFileNC,
                 self.var.PotYieldVarName,
                 date,
                 useDoy=None,
                 cloneMapFileName=self.var.cloneMap,
                 LatitudeLongitude=True)
     else:
         if self.var._modelTime.timeStepPCR == 1:
             self.var.Yx = vos.netcdf2PCRobjCloneWithoutTime(
                 self.var.PotYieldFileNC,
                 self.var.PotYieldVarName,
                 cloneMapFileName=self.var.cloneMap)
Ejemplo n.º 8
0
 def update_intercept_capacity(self):
     if self.interceptCapNC != "None":
         start_of_model_run = (self.var._modelTime.timeStepPCR == 1)
         if start_of_model_run or (self.var._modelTime.day in [1, 11, 21]):
             self.var.interception_capacity = vos.netcdf2PCRobjClone(
                 self.interceptCapNC.format(
                     day=self.var._modelTime.currTime.day,
                     month=self.var._modelTime.currTime.month,
                     year=self.var._modelTime.currTime.year),
                 self.interceptCapVarName,
                 datetime.datetime(2000, self.var._modelTime.currTime.month,
                                   self.var._modelTime.currTime.day),
                 # useDoy = method_for_time_index,
                 # cloneMapAttributes = self.cloneMapAttributes,
                 cloneMapFileName=self.var.cloneMap,
                 LatitudeLongitude=True)[self.var.landmask]
     self.var.interception_capacity = self.var.interception_capacity.clip(
         self.var.minInterceptCap, None)
 def read_crop_area(self, date = None):
     if date is None:
         crop_area = vos.netcdf2PCRobjCloneWithoutTime(
             self.CropAreaFileNC,
             self.CropAreaVarName,
             cloneMapFileName = self.var.cloneMap,
             LatitudeLongitude = True)
     else:
         crop_area = vos.netcdf2PCRobjClone(
             self.CropAreaFileNC,
             self.CropAreaVarName,
             date,
             useDoy = None,
             cloneMapFileName = self.var.cloneMap,
             LatitudeLongitude = True)
     crop_area = np.reshape(
         crop_area[self.var.landmask_crop],
         (self.var.nCrop, self.var.nCell))
     crop_area = np.float64(crop_area)
     return crop_area
Ejemplo n.º 10
0
 def set_crop_price(self):
     """Function to read crop area"""
     start_of_model_run = (self.var._modelTime.timeStepPCR == 1)
     start_of_year = (self.var._modelTime.doy == 1)
     if start_of_model_run or start_of_year:
         if not self.CropPriceFileNC == "None":
             date = datetime.datetime(self.var._modelTime.year, 1, 1, 0, 0,
                                      0)
             crop_price = vos.netcdf2PCRobjClone(
                 self.CropPriceFileNC,
                 self.CropPriceVarName,
                 date,
                 useDoy=None,
                 cloneMapFileName=self.var.cloneMap,
                 LatitudeLongitude=True)
             crop_price = np.reshape(crop_price[self.var.landmask_crop],
                                     (self.var.nCrop, self.var.nCell))
             crop_price = np.broadcast_to(
                 crop_price[None, :, :],
                 (self.var.nFarm, self.var.nCrop, self.var.nCell)).copy()
             self.var.CropPrice = crop_price
 def read_potential_crop_yield(self):        
     start_of_model_run = (self.var._modelTime.timeStepPCR == 1)
     start_of_year = (self.var._modelTime.doy == 1)
     if self.AnnualChangeInPotYield:
         if start_of_model_run or start_of_year:
             date = datetime.datetime(self.var._modelTime.year, 1, 1, 0, 0, 0)
             Yx = vos.netcdf2PCRobjClone(
                 self.PotYieldNC,
                 self.PotYieldVarName,
                 date,
                 useDoy = None,
                 cloneMapFileName = self.var.cloneMap,
                 LatitudeLongitude = True)
             self.var.Yx = Yx[self.var.landmask_crop].reshape(self.var.nCrop, self.var.nCell)
     else:
         if start_of_model_run:
             # date = datetime.datetime(self.staticLandCoverYear, 1, 1, 0, 0, 0) # ***TODO***
             Yx = vos.netcdf2PCRobjCloneWithoutTime(
                 self.PotYieldNC,
                 self.PotYieldVarName,
                 cloneMapFileName = self.var.cloneMap)
             self.var.Yx = Yx[self.var.landmask_crop].reshape(self.var.nCrop, self.var.nCell)
Ejemplo n.º 12
0
    def set_farm_category(self):
        if not self.FarmCategoryFileNC == "None":
            start_of_model_run = (self.var._modelTime.timeStepPCR == 1)
            start_of_year = (self.var._modelTime.doy == 1)
            if self.AnnualChangeInFarmCategory:
                if start_of_model_run or start_of_year:
                    date = datetime.datetime(self.var._modelTime.year, 1, 1, 0,
                                             0, 0)
                    farm_category_new = vos.netcdf2PCRobjClone(
                        self.FarmCategoryFileNC,
                        self.FarmCategoryVarName,
                        date,
                        useDoy=None,
                        cloneMapFileName=self.var.cloneMap,
                        LatitudeLongitude=True)
                    mask = np.broadcast_to(
                        self.var.landmask,
                        (self.var.nFarm, self.var.nLat, self.var.nLon))
                    farm_category_new = np.reshape(
                        farm_category_new[mask],
                        (self.var.nFarm, self.var.nCell))

                if np.any(self.var.GrowingSeasonDayOne):
                    self.var.FarmCategory[self.var.GrowingSeasonDayOne[
                        0, :, :]] = (farm_category_new[
                            self.var.GrowingSeasonDayOne[0, :, :]])
            else:
                if start_of_model_run:
                    farm_category = vos.netcdf2PCRobjCloneWithoutTime(
                        self.FarmCategoryFileNC,
                        self.FarmCategoryVarName,
                        cloneMapFileName=self.var.cloneMap)
                    mask = np.broadcast_to(
                        self.var.landmask,
                        (self.var.nFarm, self.var.nLat, self.var.nLon))
                    farm_category = np.reshape(
                        farm_category[mask], (self.var.nFarm, self.var.nCell))
                    self.var.FarmCategory = farm_category.copy()
Ejemplo n.º 13
0
    def read(self):

        # method for finding time indexes in the groundwater netdf file:
        # - the default one
        method_for_time_index = None
        # - based on the ini/configuration file (if given)
        # if 'time_index_method_for_groundwater_netcdf' in self._configuration.GROUNDWATER.keys() and\
        #                                                    self._configuration.GROUNDWATER['time_index_method_for_groundwater_netcdf'] != "None":
        #     method_for_time_index = self._configuration.GROUNDWATER['time_index_method_for_groundwater_netcdf']

        # reading groundwater:
        if self.WaterTable:
            if self.VariableWaterTable:

                # DailyGroundwaterNC is a logical indicating whether a separate
                # netCDF is used for each time step - use this for coupling
                if self.DailyGroundwaterNC:

                    # introduce this test so that we do not ask the model to read
                    # a file from a timestep prior to the current simulation.
                    if not (self._modelTime.isFirstTimestep() &
                            (self.gwTimeLag > 0)):

                        tm = self._modelTime.currTime - datetime.timedelta(
                            self.gwTimeLag)
                        day, month, year = tm.day, tm.month, tm.year

                        # Fill named placeholders (NB we have already checked that
                        # the specified filename contains these placeholders)
                        gwFileNC = self.gwFileNC.format(day=day,
                                                        month=month,
                                                        year=year)

                        # Check whether the file is present in the filesystem; if
                        # it doesn't, enter a while loop which periodically checks
                        # whether the file exists. We specify a maximum wait time
                        # in order to prevent the model hanging if the file never
                        # materialises.
                        exists = os.path.exists(gwFileNC)
                        max_wait_time = 60
                        wait_time = 0.1
                        total_wait_time = 0
                        while exists is False and total_wait_time <= max_wait_time:
                            time.sleep(wait_time)
                            exists = os.path.exists(gwFileNC)
                            total_wait_time += wait_time

                        if not exists:
                            msg = "groundwater file doesn't exist and maximum wait time exceeded"
                            raise AQError(msg)

                        zGW = vos.netcdf2PCRobjCloneWithoutTime(
                            gwFileNC,
                            self.gwVarName,
                            cloneMapFileName=self.cloneMap,
                            LatitudeLongitude=True)
                        self.zGW = zGW[self.landmask]

                else:
                    zGW = vos.netcdf2PCRobjClone(
                        self.gwFileNC,
                        self.gwVarName,
                        str(currTimeStep.fulldate),
                        useDoy=method_for_time_index,
                        cloneMapFileName=self.cloneMap,
                        LatitudeLongitude=True)
                    self.zGW = zGW[self.landmask]

            else:
                zGW = vos.netcdf2PCRobjCloneWithoutTime(
                    self.gwFileNC,
                    self.gwVarName,
                    cloneMapFileName=self.cloneMap,
                    LatitudeLongitude=True)
                self.zGW = zGW[self.landmask]
Ejemplo n.º 14
0
    def dynamic(self):

        # method for finding time indexes in the precipitation netdf file:
        # - the default one
        method_for_time_index = None
        # - based on the ini/configuration file (if given)
        if 'time_index_method_for_precipitation_netcdf' in self.var._configuration.meteoOptions.keys(
        ) and self.var._configuration.meteoOptions[
                'time_index_method_for_precipitation_netcdf'] != "None":
            method_for_time_index = self.var._configuration.meteoOptions[
                'time_index_method_for_precipitation_netcdf']

        # reading precipitation:
        if self.var.precipitation_set_per_year:
            nc_file_per_year = self.var.preFileNC % (float(
                self.var._modelTime.year), float(self.var._modelTime.year))
            self.var.precipitation = vos.netcdf2PCRobjClone(\
                                      nc_file_per_year, self.var.preVarName,\
                                      str(self.var._modelTime.fulldate),
                                      useDoy = method_for_time_index,
                                      cloneMapFileName = self.var.cloneMap,\
                                      LatitudeLongitude = True)
        else:
            self.var.precipitation = vos.netcdf2PCRobjClone(self.var.preFileNC,\
                                                            self.var.preVarName,\
                                                            str(self.var._modelTime.fulldate),\
                                                            useDoy = method_for_time_index,\
                                                            cloneMapFileName = self.var.cloneMap,\
                                                            LatitudeLongitude = True)

        # TODO: decided where np.nan is an appropriate missing value
        self.var.precipitation = self.var.preConst + self.var.preFactor * np.where(
            self.var.landmask, self.var.precipitation, np.nan)

        # make sure that precipitation is always positive
        self.var.precipitation = np.maximum(0.0, self.var.precipitation)
        self.var.precipitation[np.isnan(self.var.precipitation)] = 0.0

        # ignore very small values of precipitation (less than 0.00001 m/day or less than 0.01 kg.m-2.day-1 )
        if self.var.usingDailyTimeStepForcingData:
            self.var.precipitation = np.floor(
                self.var.precipitation * 100000.) / 100000.

        # method for finding time index in the temperature netdf file:
        # - the default one
        method_for_time_index = None
        # - based on the ini/configuration file (if given)
        if 'time_index_method_for_temperature_netcdf' in self.var._configuration.meteoOptions.keys() and\
                                                         self.var._configuration.meteoOptions['time_index_method_for_temperature_netcdf'] != "None":
            method_for_time_index = self.var._configuration.meteoOptions[
                'time_index_method_for_temperature_netcdf']

        # reading temperature
        if self.var.temperature_set_per_year:
            tmn_nc_file_per_year = self.var.tmpFileNC % (int(
                self.var._modelTime.year), int(self.var._modelTime.year))
            tmx_nc_file_per_year = self.var.tmpFileNC % (int(
                self.var._modelTime.year), int(self.var._modelTime.year))
            self.var.tmin = vos.netcdf2PCRobjClone(tmn_nc_file_per_year,\
                                               self.var.tmnVarName,\
                                               str(self.var._modelTime.fulldate),\
                                               useDoy = method_for_time_index,\
                                               cloneMapFileName = self.var.cloneMap,\
                                               LatitudeLongitude = True)

            self.var.tmax = vos.netcdf2PCRobjClone(tmx_nc_file_per_year,\
                                               self.var.tmxVarName,\
                                               str(self.var._modelTime.fulldate),\
                                               useDoy = method_for_time_index,\
                                               cloneMapFileName = self.var.cloneMap,\
                                               LatitudeLongitude = True)
        else:
            self.var.tmin = vos.netcdf2PCRobjClone(self.var.tmpFileNC,\
                                               self.var.tmnVarName,\
                                               str(self.var._modelTime.fulldate),\
                                               useDoy = method_for_time_index,\
                                               cloneMapFileName = self.var.cloneMap,\
                                               LatitudeLongitude = True)

            self.var.tmax = vos.netcdf2PCRobjClone(self.var.tmpFileNC,\
                                               self.var.tmxVarName,\
                                               str(self.var._modelTime.fulldate),\
                                               useDoy = method_for_time_index,\
                                               cloneMapFileName = self.var.cloneMap,\
                                               LatitudeLongitude = True)

        self.var.tmin = self.var.tmpConst + self.var.tmpFactor * np.where(
            self.var.landmask, self.var.tmin, np.nan)
        self.var.tmax = self.var.tmpConst + self.var.tmpFactor * np.where(
            self.var.landmask, self.var.tmax, np.nan)

        # round to nearest mm
        self.var.tmin = np.round(self.var.tmin * 1000.) / 1000.
        self.var.tmax = np.round(self.var.tmax * 1000.) / 1000.

        if 'time_index_method_for_ref_pot_et_netcdf' in self.var._configuration.meteoOptions.keys(
        ) and self.var._configuration.meteoOptions[
                'time_index_method_for_ref_pot_et_netcdf'] != "None":
            method_for_time_index = self.var._configuration.meteoOptions[
                'time_index_method_for_ref_pot_et_netcdf']

        if self.var.refETPotFileNC_set_per_year:
            nc_file_per_year = self.var.etpFileNC % (int(
                self.var._modelTime.year), int(self.var._modelTime.year))
            self.var.referencePotET = vos.netcdf2PCRobjClone(
                nc_file_per_year,
                self.var.refETPotVarName,
                str(self.var._modelTime.fulldate),
                useDoy=method_for_time_index,
                cloneMapFileName=self.var.cloneMap,
                LatitudeLongitude=True)
        else:
            self.var.referencePotET = vos.netcdf2PCRobjClone(
                self.var.etpFileNC,
                self.var.refETPotVarName,
                str(self.var._modelTime.fulldate),
                useDoy=method_for_time_index,
                cloneMapFileName=self.var.cloneMap,
                LatitudeLongitude=True)

        self.var.referencePotET = self.var.refETPotConst + self.var.refETPotFactor * np.where(
            self.var.landmask, self.var.referencePotET, np.nan)
Ejemplo n.º 15
0
    def read(self):

        # method for finding time indexes in the groundwater netdf file:
        # - the default one
        method_for_time_index = None
        # - based on the ini/configuration file (if given)
        # if 'time_index_method_for_groundwater_netcdf' in self.var._configuration.groundwaterOptions.keys() and\
        #                                                    self.var._configuration.groundwaterOptions['time_index_method_for_groundwater_netcdf'] != "None":
        #     method_for_time_index = self.var._configuration.groundwaterOptions['time_index_method_for_groundwater_netcdf']
        
        # reading groundwater:
        if self.var.WaterTable:
            if self.var.VariableWaterTable:

                # DailyForcingData is a logical indicating whether a separate
                # netCDF is used for each time step - use this for coupling
                if self.var.DailyForcingData:

                    day, month, year = self.var._modelTime.day, self.var._modelTime.month, self.var._modelTime.year

                    # Fill named placeholders (NB we have already checked that
                    # the specified filename contains these placeholders)
                    gwFileNC = self.var.gwFileNC.format(day=day, month=month, year=year)

                    # Check whether the file is present in the filesystem; if
                    # it doesn't, enter a while loop which periodically checks
                    # whether the file exists. We specify a maximum wait time
                    # in order to prevent the model hanging if the file never
                    # materialises.
                    
                    exists = os.path.exists(gwFileNC)
                    max_wait_time = 60
                    wait_time = 0.1
                    total_wait_time = 0
                    while exists is False and total_wait_time <= max_wait_time:
                        time.sleep(wait_time)
                        exists = os.path.exists(gwFileNC)
                        total_wait_time += wait_time

                    if not exists:
                        print "groundwater file doesn't exist and maximum wait time exceeded"
                        raise   # TODO: make error class

                    self.var.zGW = vos.netcdf2PCRobjCloneWithoutTime(gwFileNC,
                                                                     self.var.gwVarName,
                                                                     cloneMapFileName = self.var.cloneMap,
                                                                     LatitudeLongitude = True)
                        
                else:
                    self.var.zGW = vos.netcdf2PCRobjClone(self.var.gwFileNC,
                                                          self.var.gwVarName,
                                                          str(currTimeStep.fulldate),
                                                          useDoy = method_for_time_index,
                                                          cloneMapFileName = self.var.cloneMap,
                                                          LatitudeLongitude = True)
                    
            else:
                self.var.zGW = vos.netcdf2PCRobjCloneWithoutTime(self.var.gwFileNC,
                                                                 self.var.gwVarName,
                                                                 cloneMapFileName = self.var.cloneMap,
                                                                 LatitudeLongitude = True)
                
        else:
            self.var.zGW = None