예제 #1
0
def main():

    # prepare logger and its directory
    log_file_location = output['folder'] + "/log/"
    try:
        os.makedirs(log_file_location)
    except:
        cmd = 'rm -r ' + log_file_location + "/*"
        os.system(cmd)
        pass
    vos.initialize_logging(log_file_location)

    # time object
    modelTime = ModelTime()  # timeStep info: year, month, day, doy, hour, etc
    modelTime.getStartEndTimeSteps(startDate, endDate, nrOfTimeSteps)

    calculationModel = CalcFramework(cloneMapFileName,\
                                     pcraster_files, \
                                     modelTime, \
                                     output, inputEPSG, outputEPSG, resample_method)

    dynamic_framework = DynamicFramework(calculationModel,
                                         modelTime.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
def main():

    # get the full path of configuration/ini file given in the system argument
    iniFileName = os.path.abspath(sys.argv[1])

    # debug option
    debug_mode = False
    if len(sys.argv) > 2:
        if sys.argv[2] == "debug": debug_mode = True

    # object to handle configuration/ini file
    configuration = Configuration(iniFileName = iniFileName, \
                                  debug_mode = debug_mode)

    # timeStep info: year, month, day, doy, hour, etc
    currTimeStep = ModelTime()

    # Running the deterministic_runner
    currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'],
                                      configuration.globalOptions['endTime'])
    logger.info('Model run starts.')
    deterministic_runner = DeterministicRunner(configuration, currTimeStep)

    dynamic_framework = DynamicFramework(deterministic_runner,
                                         currTimeStep.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
예제 #3
0
def main():

    # disclaimer.print_disclaimer()

    # get the full path of the config file provided as system argument
    iniFileName = os.path.abspath(sys.argv[1])
    debug_mode = False
    if len(sys.argv) > 2:
        if (sys.argv[2] == "debug"):
            debug_mode = True

    configuration = Configuration(iniFileName=iniFileName,
                                  debug_mode=debug_mode)
    currTimeStep = ModelTime()
    initial_state = None
    currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'],
                                      configuration.globalOptions['endTime'])
    currTimeStep.update(1)
    logger.info('Transient simulation run has started')
    deterministic_runner = DeterministicRunner(
        # FAO66_behaviour,
        WaterBalanceModel,
        configuration,
        currTimeStep,
        variable_list,
        initial_state)
    dynamic_framework = DynamicFramework(deterministic_runner,
                                         currTimeStep.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
예제 #4
0
def main():

    startDate = "2003-01-01"  #YYYY-MM-DD
    endDate = "2010-12-31"  #YYYY-MM-DD

    # time object
    modelTime = ModelTime()  # timeStep info: year, month, day, doy, hour, etc
    modelTime.getStartEndTimeSteps(startDate, endDate)
    #
    # upscaling the PCR-GLOBWB results from five minute to one degree:
    upscalingModel = UpscalingFramework(input_files,\
                                        output_files,\
                                        modelTime,\
                                        tmpDir)
    dynamic_framework = DynamicFramework(upscalingModel,\
                                         modelTime.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()

    # reset time object
    modelTime = ModelTime()  # timeStep info: year, month, day, doy, hour, etc
    modelTime.getStartEndTimeSteps(startDate, endDate)
    #
    # evaluate model results to grace product
    graceEvaluation = GraceEvaluation(input_files,\
                                      output_files,\
                                      modelTime,\
                                      tmpDir)
    dynamic_framework = DynamicFramework(graceEvaluation,\
                                         modelTime.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
예제 #5
0
def main():

    initial_state = None
    configuration = Configuration()

    spin_up = SpinUp(configuration)  # object for spin_up

    currTimeStep = ModelTime(
    )  # timeStep info: year, month, day, doy, hour, etc

    # spinningUp
    noSpinUps = int(configuration.globalOptions['maxSpinUpsInYears'])
    if noSpinUps > 0:

        logger.info('Spin-Up #Total Years: ' + str(noSpinUps))

        spinUpRun = 0
        has_converged = False
        while spinUpRun < noSpinUps and has_converged == False:
            spinUpRun += 1
            currTimeStep.getStartEndTimeStepsForSpinUp(
                configuration.globalOptions['startTime'], spinUpRun, noSpinUps)
            logger.info('Spin-Up Run No. ' + str(spinUpRun))
            deterministic_runner = DeterministicRunner(configuration,
                                                       currTimeStep,
                                                       initial_state)

            all_state_begin = deterministic_runner.model.getAllState()

            dynamic_framework = DynamicFramework(deterministic_runner,
                                                 currTimeStep.nrOfTimeSteps)
            dynamic_framework.setQuiet(True)
            dynamic_framework.run()

            all_state_end = deterministic_runner.model.getAllState()

            has_converged = spin_up.checkConvergence(
                all_state_begin, all_state_end, spinUpRun,
                deterministic_runner.model.routing.cellArea)

            initial_state = deterministic_runner.model.getState()
    #
    # Running the deterministic_runner (excluding DA scheme)
    currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'],
                                      configuration.globalOptions['endTime'])

    logger.info('Transient simulation run started.')
    deterministic_runner = DeterministicRunner(configuration, currTimeStep,
                                               initial_state)

    dynamic_framework = DynamicFramework(deterministic_runner,
                                         currTimeStep.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
예제 #6
0
def main():
    
    # time object
    modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc
    modelTime.getStartEndTimeSteps(startDate,endDate,nrOfTimeSteps)
    
    calculationModel = CalcFramework(cloneMapFileName,\
                                     pcraster_files, \
                                     output, \
                                     modelTime)
    dynamic_framework = DynamicFramework(calculationModel,modelTime.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
예제 #7
0
def main():

    # time object
    modelTime = ModelTime()  # timeStep info: year, month, day, doy, hour, etc
    modelTime.getStartEndTimeSteps(startDate, endDate, nrOfTimeSteps)

    calculationModel = CalcFramework(cloneMapFileName,\
                                     pcraster_files, \
                                     output, \
                                     modelTime)
    dynamic_framework = DynamicFramework(calculationModel,
                                         modelTime.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
def main():
    
    # time object
    modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc
    modelTime.getStartEndTimeSteps(startDate, endDate)
    
    # converting model
    convertModel = ConvertVolumeToHeightFramework(input_netcdf, \
                                                  output_netcdf, \
                                                  modelTime, \
                                                  tmpDir)
    dynamic_framework = DynamicFramework(convertModel, modelTime.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
예제 #9
0
def main():

    # time object
    modelTime = ModelTime()  # timeStep info: year, month, day, doy, hour, etc
    modelTime.getStartEndTimeSteps(startDate, endDate)

    # resample netcdf
    resampleModel = ResampleFramework(input_netcdf,\
                                      output_netcdf,\
                                      modelTime,\
                                      tmpDir)
    dynamic_framework = DynamicFramework(resampleModel,
                                         modelTime.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
def main():

    # input_file
    input_file = "/projects/0/dfguu/users/edwin/pcr-globwb-aqueduct/historical/1958-2001_watch/"
    input_file = sys.argv[1]
    
    # netcdf variable name
    input_variable_name = sys.argv[2]
    
    # timeStep info: year, month, day, doy, hour, etc
    start_date = "2015-01-01"
    end_date   = "2015-12-31"
    start_date = sys.argv[3]
    end_date   = sys.argv[4]
    #
    currTimeStep = ModelTime() 
    currTimeStep.getStartEndTimeSteps(start_date, end_date)
    
    # output folder from this calculation
    output_folder = "/scratch-shared/edwin/mekong_basin_temperature/test/"
    output_folder = sys.argv[5]

    # - if exists, cleaning the previous output directory:
    if os.path.isdir(output_folder): shutil.rmtree(output_folder)
    # - making the output folder
    os.makedirs(output_folder)

    # output file, variable name and unit
    output_file   = output_folder + "/" + sys.argv[6]
    variable_name = sys.argv[7]
    variable_unit  = sys.argv[8]

    # logger
    # - making a log directory
    log_file_directory = output_folder + "/" + "log/"
    os.makedirs(log_file_directory)
    # - initialize logging
    vos.initialize_logging(log_file_directory)
    
    
    # Running the deterministic_runner
    logger.info('Starting the calculation.')
    deterministic_runner = DeterministicRunner(currTimeStep, input_file, input_variable_name, output_file, variable_name, variable_unit)
    dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
def main():

    # TODO: print disclaimer
    # disclaimer.print_disclaimer()

    # get the full path of the configuration/ini file provided
    # as system argument
    iniFileName = os.path.abspath(sys.argv[1])
    model = sys.argv[2].lower()

    # TODO: debug option
    debug_mode = False
    # if len(sys.argv) > 2:
    #     if (sys.argv[2] == "debug": debug_mode = True

    # object to handle configuration/ini file
    configuration = Configuration(iniFileName=iniFileName,
                                  debug_mode=debug_mode)

    # timestep information
    currTimeStep = ModelTime()

    # NB spin-up currently not implemented (nor is it
    # implemented in AquaCrop-OS)
    initial_state = None

    currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'],
                                      configuration.globalOptions['endTime'])
    currTimeStep.update(
        1
    )  # this essentially allows us to call read_forcings in AquaCrop.__init__() method

    logger.info('Transient simulation run has started')
    deterministic_runner = None
    if (model == 'aquacrop'):
        deterministic_runner = run_AquaCrop(configuration, currTimeStep,
                                            initial_state)
    elif model == 'fao56':
        deterministic_runner = run_FAO56(configuration, currTimeStep,
                                         initial_state)
    # TODO: error handling

    dynamic_framework = DynamicFramework(deterministic_runner,
                                         currTimeStep.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
def main():

    # prepare the output directory
    try:
        os.makedirs(pcraster_output['output_folder'])
    except:
        os.system('rm -r ' + str(pcraster_output['output_folder']))
        pass
    # - making the directory for storing global extent output files
    os.makedirs(pcraster_output['output_folder'] + "/global/")
    # - making the directory for storing regional extent output files
    os.makedirs(pcraster_output['output_folder'] + "/regional/")

    # prepare logger and its directory
    log_file_location = pcraster_output['output_folder'] + "/log/"
    try:
        os.makedirs(log_file_location)
    except:
        pass
    vos.initialize_logging(log_file_location)

    # prepare a temporary folder
    tmp_file_location = pcraster_output['output_folder'] + "/tmp/"
    try:
        os.makedirs(tmp_file_location)
    except:
        pass

    # time object
    modelTime = ModelTime()  # timeStep info: year, month, day, doy, hour, etc
    modelTime.getStartEndTimeSteps(startDate, endDate)

    calculationModel = CalcFramework(globeCloneMapFileName, localCloneMapFileName, \
                                     netcdf_input, \
                                     pcraster_output, \
                                     modelTime, \
                                     inputEPSG, outputEPSG, resample_method)

    dynamic_framework = DynamicFramework(calculationModel,
                                         modelTime.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
예제 #13
0
def main():
    
    # prepare logger and its directory
    log_file_location = output['folder'] + "/log/"
    try:
        os.makedirs(log_file_location)
    except:
        pass
    vos.initialize_logging(log_file_location)
    
    # time object
    modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc
    modelTime.getStartEndTimeSteps(startDate, endDate)
    
    calculationModel = CalcFramework(cloneMapFileName,\
                                     input_files, \
                                     modelTime, \
                                     output)

    dynamic_framework = DynamicFramework(calculationModel, modelTime.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
예제 #14
0
def main():
    
    # - prepare the output folder
    if os.path.exists(output_folder): shutil.rmtree(output_folder)
    os.makedirs(output_folder)    

    # prepare logger and its directory
    log_file_location = output_folder + "/log/"
    os.makedirs(log_file_location)
    vos.initialize_logging(log_file_location)
    
    # time object
    modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc
    modelTime.getStartEndTimeSteps(startDate, endDate)
    
    #~ print modelTime.nrOfTimeSteps
    
    # calculation model/framework
    calculationModel = AreaOperationNetcdfToPCRasterTSS(netcdf_input_file = netcdf_input_file, \
                                                        areaMapFileName      = areaMapFileName, \
                                                        areaPointMapFileName = areaPointMapFileName, \
                                                        netcdf_input_clone_map_file = netcdf_input_clone_map_file, \
                                                        output_folder = output_folder, \
                                                        unit_conversion_factor = unit_conversion_factor, \
                                                        unit_conversion_offset = unit_conversion_offset, \
                                                        modelTime = modelTime, \
                                                        inputProjection  = inputProjection, \
                                                        outputProjection = outputProjection, \
                                                        resample_method  = resample_method, \
                                                        tss_daily_output_file = tss_daily_output_file, \
                                                        tss_10day_output_file = tss_10day_output_file, \
                                                        report_10day_pcr_files = True
                                                        )
    number_of_time_steps = modelTime.nrOfTimeSteps
    #~ number_of_time_steps = 100
    dynamic_framework = DynamicFramework(calculationModel, number_of_time_steps)
    dynamic_framework.setQuiet(True)
    # - start the calculation
    dynamic_framework.run()
예제 #15
0
def main():

    # the output folder from this calculation
    output_folder = "/scratch-shared/edwinvua/data_for_diede/netcdf_process/climatology_average/"
    output_folder = sys.argv[1]

    # totat_runoff_input_file
    totat_runoff_input_file = "/scratch-shared/edwinvua/data_for_diede/flow_scenarios/climatology_average_totalRunoff_monthTot_output_1979-2015.nc"
    totat_runoff_input_file = sys.argv[2]
    
    # timeStep info: year, month, day, doy, hour, etc
    start_date = "2015-01-01"
    end_date   = "2015-12-31"
    start_date = sys.argv[3]
    end_date   = sys.argv[4]
    #
    currTimeStep = ModelTime() 
    currTimeStep.getStartEndTimeSteps(start_date, end_date)
    
    # - if exists, cleaning the previous output directory:
    if os.path.isdir(output_folder): shutil.rmtree(output_folder)
    # - making the output folder
    os.makedirs(output_folder)

    # logger
    # - making a log directory
    log_file_directory = output_folder + "/" + "log/"
    os.makedirs(log_file_directory)
    # - initialize logging
    vos.initialize_logging(log_file_directory)
    
    
    # Running the deterministic_runner
    logger.info('Starting the calculation.')
    deterministic_runner = DeterministicRunner(currTimeStep, output_folder, totat_runoff_input_file)
    dynamic_framework = DynamicFramework(deterministic_runner,currTimeStep.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
def main():

    # print disclaimer
    disclaimer.print_disclaimer()

    # get the full path of configuration/ini file given in the system argument
    iniFileName = os.path.abspath(sys.argv[1])

    # debug option
    debug_mode = False
    if len(sys.argv) > 2:
        if sys.argv[2] == "debug" or sys.argv[2] == "debug_parallel":
            debug_mode = True

    # options to perform steady state calculation
    steady_state_only = False
    if len(sys.argv) > 3:
        if sys.argv[3] == "steady-state-only": steady_state_only = True

    # object to handle configuration/ini file
    configuration = Configuration(iniFileName = iniFileName, \
                                  debug_mode = debug_mode, \
                                  steady_state_only = steady_state_only)

    # timeStep info: year, month, day, doy, hour, etc
    currTimeStep = ModelTime()

    # Running the deterministic_runner
    currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'],
                                      configuration.globalOptions['endTime'])
    logger.info('Model run starts.')
    deterministic_runner = DeterministicRunner(configuration, currTimeStep)

    dynamic_framework = DynamicFramework(deterministic_runner,
                                         currTimeStep.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
예제 #17
0
def main():
    
    # prepare logger and its directory
    log_file_location = output['folder']+"/log/"
    try:
        os.makedirs(log_file_location)
    except:
        cmd = 'rm -r '+log_file_location+"/*"
        os.system(cmd)
        pass
    vos.initialize_logging(log_file_location)
    
    # time object
    modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc
    modelTime.getStartEndTimeSteps(startDate,endDate,nrOfTimeSteps)
    
    calculationModel = CalcFramework(cloneMapFileName,\
                                     pcraster_files, \
                                     modelTime, \
                                     output, inputEPSG, outputEPSG, resample_method)

    dynamic_framework = DynamicFramework(calculationModel,modelTime.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
def main():

    # get the full path of configuration/ini file given in the system argument
    iniFileName = os.path.abspath(sys.argv[1])

    # debug option
    debug_mode = False
    if len(sys.argv) > 2:
        if sys.argv[2] == "debug" or sys.argv[
                2] == "debug_parallel" or sys.argv[2] == "debug-parallel":
            debug_mode = True

    # object to handle configuration/ini file
    configuration = Configuration(iniFileName = iniFileName, \
                                  debug_mode = debug_mode, \
                                  no_modification = False)

    # parallel option
    this_run_is_part_of_a_set_of_parallel_run = False
    if len(sys.argv) > 2:
        if sys.argv[2] == "parallel" or sys.argv[
                2] == "debug_parallel" or sys.argv[2] == "debug-parallel":
            this_run_is_part_of_a_set_of_parallel_run = True

    # for a non parallel run (usually 30min), a specific directory given in the system argument (sys.argv[3]) will be assigned for a given parameter combination:
    if this_run_is_part_of_a_set_of_parallel_run == False:
        # modfiying 'outputDir' (based on the given system argument)
        configuration.globalOptions['outputDir'] += "/" + str(
            sys.argv[3]) + "/"

    # for a parallel run (usually 5min), we assign a specific directory based on the clone number/code:
    if this_run_is_part_of_a_set_of_parallel_run:
        # modfiying outputDir, clone-map and landmask (based on the given system arguments)
        clone_code = str(sys.argv[3])
        configuration.globalOptions['outputDir'] += "/" + clone_code + "/"
        configuration.globalOptions['cloneMap'] = configuration.globalOptions[
            'cloneMap'] % (clone_code)
        configuration.globalOptions['landmask'] = configuration.globalOptions[
            'landmask'] % (clone_code)

    # set configuration
    configuration.set_configuration(system_arguments=sys.argv)

    # timeStep info: year, month, day, doy, hour, etc
    currTimeStep = ModelTime()

    # object for spin_up
    spin_up = SpinUp(configuration)

    # spinning-up
    noSpinUps = int(configuration.globalOptions['maxSpinUpsInYears'])
    initial_state = None
    if noSpinUps > 0:

        logger.info('Spin-Up #Total Years: ' + str(noSpinUps))

        spinUpRun = 0
        has_converged = False
        while spinUpRun < noSpinUps and has_converged == False:
            spinUpRun += 1
            currTimeStep.getStartEndTimeStepsForSpinUp(
                configuration.globalOptions['startTime'], spinUpRun, noSpinUps)
            logger.info('Spin-Up Run No. ' + str(spinUpRun))
            deterministic_runner = DeterministicRunner(configuration,
                                                       currTimeStep,
                                                       initial_state, sys.argv)

            all_state_begin = deterministic_runner.model.getAllState()

            dynamic_framework = DynamicFramework(deterministic_runner,
                                                 currTimeStep.nrOfTimeSteps)
            dynamic_framework.setQuiet(True)
            dynamic_framework.run()

            all_state_end = deterministic_runner.model.getAllState()

            has_converged = spin_up.checkConvergence(
                all_state_begin, all_state_end, spinUpRun,
                deterministic_runner.model.routing.cellArea)

            initial_state = deterministic_runner.model.getState()
    #
    # Running the deterministic_runner (excluding DA scheme)
    currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'],
                                      configuration.globalOptions['endTime'])

    logger.info('Transient simulation run started.')
    deterministic_runner = DeterministicRunner(configuration, currTimeStep,
                                               initial_state, sys.argv)

    dynamic_framework = DynamicFramework(deterministic_runner,
                                         currTimeStep.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()
def main():

    # print disclaimer
    disclaimer.print_disclaimer()

    # get the full path of configuration/ini file given in the system argument
    iniFileName = os.path.abspath(sys.argv[1])

    # debug option
    debug_mode = False
    if len(sys.argv) > 2:
        if sys.argv[2] == "debug": debug_mode = True

    # object to handle configuration/ini file
    configuration = Configuration(iniFileName = iniFileName, \
                                  debug_mode = debug_mode)

    # timeStep info: year, month, day, doy, hour, etc
    currTimeStep = ModelTime()

    # object for spin_up
    spin_up = SpinUp(configuration)

    # spinningUp
    noSpinUps = int(configuration.globalOptions['maxSpinUpsInYears'])
    initial_state = None
    if noSpinUps > 0:

        logger.info('Spin-Up #Total Years: ' + str(noSpinUps))

        spinUpRun = 0
        has_converged = False
        while spinUpRun < noSpinUps and has_converged == False:
            spinUpRun += 1
            currTimeStep.getStartEndTimeStepsForSpinUp(
                configuration.globalOptions['startTime'], spinUpRun, noSpinUps)
            logger.info('Spin-Up Run No. ' + str(spinUpRun))
            deterministic_runner = DeterministicRunner(configuration,
                                                       currTimeStep,
                                                       initial_state)

            all_state_begin = deterministic_runner.model.getAllState()

            dynamic_framework = DynamicFramework(deterministic_runner,
                                                 currTimeStep.nrOfTimeSteps)
            dynamic_framework.setQuiet(True)
            dynamic_framework.run()

            all_state_end = deterministic_runner.model.getAllState()

            has_converged = spin_up.checkConvergence(
                all_state_begin, all_state_end, spinUpRun,
                deterministic_runner.model.routing.cellArea)

            initial_state = deterministic_runner.model.getState()

    # Running the deterministic_runner (excluding DA scheme)
    currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'],
                                      configuration.globalOptions['endTime'])
    logger.info('Transient simulation run started.')
    deterministic_runner = DeterministicRunner(configuration, currTimeStep,
                                               initial_state)
    dynamic_framework = DynamicFramework(deterministic_runner,
                                         currTimeStep.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()

    # for debugging to PCR-GLOBWB version one
    if configuration.debug_to_version_one:

        logger.info('\n\n\n\n\n' + 'Executing PCR-GLOBWB version 1.' +
                    '\n\n\n\n\n')

        # reset modelTime object
        currTimeStep = None
        currTimeStep = ModelTime()
        currTimeStep.getStartEndTimeSteps(
            configuration.globalOptions['startTime'],
            configuration.globalOptions['endTime'])

        # execute PCR-GLOBWB version 1
        # - including comparing model outputs (from versions one and two)
        pcrglobwb_one = oldcalc_framework.PCRGlobWBVersionOne(configuration, \
                                                              currTimeStep, \
                                                              deterministic_runner.model.routing.landmask, \
                                                              deterministic_runner.model.routing.cellArea)
        dynamic_framework = DynamicFramework(pcrglobwb_one,
                                             currTimeStep.nrOfTimeSteps)
        dynamic_framework.setQuiet(True)
        dynamic_framework.run()
예제 #20
0
class pcrglobwbBMI(object):
    def initialize(self, config_file_location=None):
        """
        Initializes the model: read config file, load variables, get timestep information, etc.
        """
        self.configuration = Configuration(config_file_location)

        self.initial_state = None

        self.currTimeStep = ModelTime(
        )  # timeStep info: year, month, day, doy, hour, etc

        self.currTimeStep.getStartEndTimeSteps(
            self.configuration.globalOptions['startTime'],
            self.configuration.globalOptions['endTime'])

        self.deterministic_runner = DeterministicRunner(
            self.configuration, self.currTimeStep, self.initial_state)

        self.dynamic_framework = DynamicFramework(self.deterministic_runner, 1)
        self.dynamic_framework.setQuiet(True)
        self.dynamic_framework._runInitial()
        self.dynamic_framework._runResume()

        # set timestep (to allow updates on a per-timestep-basis)
        self.currenttimestep = 0

        logger.info('Model initialized. Spin-up might be required.')

    def finalize(self):
        """
        Finalizes the model: shut down the model run, clean up resources, etc.
        """
        self.dynamic_framework._runSuspend()
        # dynamic_framework._wf_shutdown()   # commented out, special function from wflow Dynamic Framework

    def spinup(self):
        """
        Spin-up the model. This is required to obtain realistic starting conditions for the model run.
        It runs on a yearly basis until the required convergence or max. allowed spin-up runs is reached.
        """
        spin_up = SpinUp(self.configuration)  # object for spin_up

        self.currTimeStep = ModelTime(
        )  # timeStep info: year, month, day, doy, hour, etc

        # spin-up
        noSpinUps = int(self.configuration.globalOptions['maxSpinUpsInYears'])
        if noSpinUps > 0:

            logger.info('Spin-Up #Total Years: ' + str(noSpinUps))

            spinUpRun = 0
            has_converged = False
            while spinUpRun < noSpinUps and has_converged == False:
                spinUpRun += 1
                self.currTimeStep.getStartEndTimeStepsForSpinUp(
                    self.configuration.globalOptions['startTime'], spinUpRun,
                    noSpinUps)
                logger.info('Spin-Up Run No. ' + str(spinUpRun))
                deterministic_runner = DeterministicRunner(
                    self.configuration, self.currTimeStep, self.initial_state)

                all_state_begin = deterministic_runner.model.getAllState()

                self.dynamic_framework = DynamicFramework(
                    deterministic_runner, self.currTimeStep.nrOfTimeSteps)
                self.dynamic_framework.setQuiet(True)
                self.dynamic_framework.run()

                all_state_end = deterministic_runner.model.getAllState()

                has_converged = spin_up.checkConvergence(
                    all_state_begin, all_state_end, spinUpRun,
                    deterministic_runner.model.routing.cellArea)

                self.initial_state = deterministic_runner.model.getState()

                # setting model ready after spin-up
                self.currTimeStep.getStartEndTimeSteps(
                    self.configuration.globalOptions['startTime'],
                    self.configuration.globalOptions['endTime'])

                self.deterministic_runner = DeterministicRunner(
                    self.configuration, self.currTimeStep, self.initial_state)

        logger.info(
            'End of spin-up. Model is ready for transient simulation run.')

    def update(self, dt=-1):
        """
        Updates the model a number of timesteps, dependent on specified dt:
        dt = -1	-> runs the entire model from start time to end time
        dt = 1  -> updates the model 1 timestep (1 day)
        dt > 1  -> updates the model a number of timesteps (dt days)

        NOTE: the model can only run on a daily timestep!
        """

        if dt == 1:
            # update timestep
            self.currenttimestep += 1
            self.currTimeStep.update(self.currenttimestep)

            # commented out, already stated at initialization and at end of spin-up, not required at every timestep?
            #deterministic_runner = DeterministicRunner(self.configuration, self.currTimeStep, self.initial_state)

            # update model
            self.dynamic_framework = DynamicFramework(
                self.deterministic_runner, self.currenttimestep,
                self.currenttimestep)
            self.dynamic_framework.setQuiet(True)
            self.dynamic_framework.run()

            # update states (commented out, not required?)
            #self.initial_state = deterministic_runner.model.getState()

        elif dt == -1:
            # commented out, already stated at initialization and at end of spin-up, not required here as well?
            #deterministic_runner = DeterministicRunner(self.configuration, self.currTimeStep, self.initial_state)

            self.dynamic_framework = DynamicFramework(
                self.deterministic_runner, self.currTimeStep.nrOfTimeSteps)
            self.dynamic_framework.setQuiet(True)
            self.dynamic_framework.run()

        else:
            # update timestep
            self.currenttimestep += 1
            self.currTimeStep.update(self.currenttimestep)

            # update model
            self.dynamic_framework = DynamicFramework(
                self.deterministic_runner, self.currenttimestep + (dt - 1),
                self.currenttimestep)
            self.dynamic_framework.setQuiet(True)
            self.dynamic_framework.run()

            # update time
            self.currenttimestep += (dt - 1)
            self.currTimeStep.update(self.currenttimestep)

    def get_start_time(self):
        """
        Returns model start time
        Input:  -
        Output: time as datetime (YYYY,MM,DD)
        """
        return self.currTimeStep.startTime

    def get_end_time(self):
        """
        Returns model end time
        Input:  -
        Output: time as datetime (YYYY,MM,DD)
        """
        return self.currTimeStep.endTime

    def get_current_time(self):
        """
        Returns current model time
        Input:  -
        Output: time as datetime (YYYY,MM,DD)
        """
        return self.currTimeStep.currTime

    def get_time_step(self):
        """
        Return current model timestep
        Input:  -
        Output: timestep as int
        """
        return self.currTimeStep.timeStepPCR

    def get_var(self, name, missingValues=-999):
        """
        Returns a numpy array from model library
        Input:  variable/map name (string)
        Output: numpy array or single variable, depending on input

        NOTE1: to get a variable from a specific landCover type, a tuple containing two strings should be used, with:
        - string 1 = name of landCover type
        - string 2 = name of variable

        NOTE2: there are two options to create a numpy array:
        - pcr2numpy    -> requires a value for MV (optional, default = -999)
        - pcr_as_numpy -> automatically sets nan for all MV
        Currently using pcr2numpy!
        """

        # check size of name input
        if numpy.size(name) == 1:

            # check for 'name' in the different sections of the model
            if hasattr(self.deterministic_runner.model.landSurface, name):
                pcrmap = getattr(self.deterministic_runner.model.landSurface,
                                 name)
            elif hasattr(self.deterministic_runner.model.routing, name):
                pcrmap = getattr(self.deterministic_runner.model.routing, name)
            elif hasattr(self.deterministic_runner.model.meteo, name):
                pcrmap = getattr(self.deterministic_runner.model.meteo, name)
            elif hasattr(self.deterministic_runner.model.groundwater, name):
                pcrmap = getattr(self.deterministic_runner.model.groundwater,
                                 name)
            else:
                logger.warn(
                    name +
                    " cannot be found in the model, returning empty list!")

        else:

            # first check if a specific model section was used as input
            if name[0] == 'landSurface':
                if hasattr(self.deterministic_runner.model.landSurface,
                           name[1]):
                    pcrmap = getattr(
                        self.deterministic_runner.model.landSurface, name[1])
            elif name[0] == 'routing':
                if hasattr(self.deterministic_runner.model.routing, name[1]):
                    pcrmap = getattr(self.deterministic_runner.model.routing,
                                     name[1])
            elif name[0] == 'WaterBodies':
                if hasattr(self.deterministic_runner.model.routing.WaterBodies,
                           name[1]):
                    pcrmap = getattr(
                        self.deterministic_runner.model.routing.WaterBodies,
                        name[1])
            elif name[0] == 'pcrglobwb':
                if hasattr(self.deterministic_runner.model, name[1]):
                    pcrmap = getattr(self.deterministic_runner.model, name[1])
            # otherwise check if it is a variable from a landCover type
            else:
                # use the first entry of 'name' to find correct landCover type, second entry to find variable
                try:
                    if hasattr(
                            self.deterministic_runner.model.landSurface.
                            landCoverObj[name[0]], name[1]):
                        pcrmap = getattr(
                            self.deterministic_runner.model.landSurface.
                            landCoverObj[name[0]], name[1])
                    else:
                        logger.warn(
                            '(' + name[0] + ', ' + name[1] +
                            ") cannot be found in the model, returning empty list!"
                        )
                except:
                    logger.warn(
                        '(' + name[0] + ', ' + name[1] +
                        ") cannot be found in the model, returning empty list!"
                    )

        # attempt to create a numpy array, otherwise try to give the single value, or return empty list if this is both not possible
        try:
            return_value = pcr2numpy(pcrmap, missingValues)
            #return_value = pcr_as_numpy(pcrmap)
        except:
            try:
                return_value = pcrmap
            except:
                return []

        return return_value

    def set_var(self, name, var, missingValues=-999):
        """
        Sets a pcr map with values from a numpy array.
        Input:  variable/map name (string), values (numpy array or single value), missing values (optional, default = -999)
        Output: -

        NOTE: to set a variable from a specific landCover type, a tuple containing two strings should be used, with:
        - string 1 = name of landCover type
        - string 2 = name of variable
        """

        # try to create a pcr map from numpy array, otherwise just use the single value
        try:
            pcrmap = numpy2pcr(Scalar, var, missingValues)
        except:
            pcrmap = var

        # check if LDD (requires additional step)
        if 'lddMap' in name:
            pcrmap = ldd(pcrmap)

        # check size of name input
        if numpy.size(name) == 1:

            # update pcr map used in model with set values
            if hasattr(self.deterministic_runner.model.groundwater, name):
                exec("self.deterministic_runner.model.groundwater." + name +
                     " = pcrmap")
            elif hasattr(self.deterministic_runner.model.landSurface, name):
                exec("self.deterministic_runner.model.landSurface." + name +
                     " = pcrmap")
            elif hasattr(self.deterministic_runner.model.meteo, name):
                exec("self.deterministic_runner.model.meteo." + name +
                     " = pcrmap")
            elif hasattr(self.deterministic_runner.model.routing, name):
                exec("self.deterministic_runner.model.routing." + name +
                     " = pcrmap")
            else:
                logger.warn(name +
                            " is not defined in the model, doing nothing!")

        else:

            # first check if a specific model section was used as input
            if name[0] == 'landSurface':
                if hasattr(self.deterministic_runner.model.landSurface,
                           name[1]):
                    exec("self.deterministic_runner.model.landSurface." +
                         name[1] + " = pcrmap")
            elif name[0] == 'routing':
                if hasattr(self.deterministic_runner.model.routing, name[1]):
                    exec("self.deterministic_runner.model.routing." + name[1] +
                         " = pcrmap")
            elif name[0] == 'WaterBodies':
                if hasattr(self.deterministic_runner.model.routing.WaterBodies,
                           name[1]):
                    exec("self.deterministic_runner.model.routing.WaterBodies."
                         + name[1] + " = pcrmap")
            elif name[0] == 'pcrglobwb':
                if hasattr(self.deterministic_runner.model, name[1]):
                    exec("self.deterministic_runner.model." + name[1] +
                         " = pcrmap")
            # otherwise check if it is a variable from a landCover type
            else:
                try:
                    if hasattr(
                            self.deterministic_runner.model.landSurface.
                            landCoverObj[name[0]], name[1]):
                        exec(
                            "self.deterministic_runner.model.landSurface.landCoverObj['"
                            + name[0] + "']." + name[1] + " = pcrmap")
                    else:
                        logger.warn(
                            '(' + name[0] + ', ' + name[1] +
                            + ") is not defined in the model, doing nothing!")
                except:
                    logger.warn(
                        '(' + name[0] + ', ' + name[1] +
                        + ") is not defined in the model, doing nothing!")
예제 #21
0
def main():
    # get the full path of configuration/ini file given in the system argument
    iniFileName = os.path.abspath(sys.argv[1])

    # debug option
    debug_mode = False
    if len(sys.argv) > 2:
        if sys.argv[2] == "debug": debug_mode = True

    # object to handle configuration/ini file
    configuration = Configuration(iniFileName = iniFileName, \
                                  debug_mode = debug_mode)

    # timeStep info: year, month, day, doy, hour, etc
    currTimeStep = ModelTime()

    # object for spin_up
    spin_up = SpinUp(configuration)

    # spinningUp
    noSpinUps = int(configuration.globalOptions['maxSpinUpsInYears'])
    initial_state = None
    if noSpinUps > 0:

        logger.info('Spin-Up #Total Years: ' + str(noSpinUps))

        spinUpRun = 0
        has_converged = False
        while spinUpRun < noSpinUps and has_converged == False:
            spinUpRun += 1
            currTimeStep.getStartEndTimeStepsForSpinUp(
                configuration.globalOptions['startTime'], spinUpRun, noSpinUps)
            logger.info('Spin-Up Run No. ' + str(spinUpRun))
            deterministic_runner = DeterministicRunner(configuration,
                                                       currTimeStep,
                                                       initial_state)

            all_state_begin = deterministic_runner.model.getAllState()

            dynamic_framework = DynamicFramework(deterministic_runner,
                                                 currTimeStep.nrOfTimeSteps)
            dynamic_framework.setQuiet(True)
            dynamic_framework.run()

            all_state_end = deterministic_runner.model.getAllState()

            has_converged = spin_up.checkConvergence(
                all_state_begin, all_state_end, spinUpRun,
                deterministic_runner.model.routing.cellArea)

            initial_state = deterministic_runner.model.getState()
    #
    # Running the deterministic_runner (excluding DA scheme)
    currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'],
                                      configuration.globalOptions['endTime'])

    logger.info('Transient simulation run started.')
    deterministic_runner = DeterministicRunner(configuration, currTimeStep,
                                               initial_state)

    dynamic_framework = DynamicFramework(deterministic_runner,
                                         currTimeStep.nrOfTimeSteps)
    dynamic_framework.setQuiet(True)
    dynamic_framework.run()