Skip to content
Extraits de code Groupes Projets
dummy_assimilation_operations.py 3,55 ko
Newer Older
  • Learn to ignore specific revisions
  • import argparse
    
    import utilities.generic
    
    import models.nemo.utilities.argument_parser
    import models.nemo.utilities.base_experiment_plan
    
    import models.nemo.enkf_tools.launch_enkf_experiment
    import models.nemo.enkf_tools.update_enkf_experiment_plan
    
    import models.nemo.enkf_tools.utilities.enkf_experiment
    import models.nemo.enkf_tools.utilities.enkf_experiment_plan as enkf_experiment_plan
    
    
    #############
    # functions #
    #############
    
    def dummy_assimilation_operations(version, configuration, name, leg_index):
      '''
      Operations done during a dummy assimilation job, which is submitted when observations are marked
      as missing.
      '''
    
      # update the experiment plan
      models.nemo.enkf_tools.update_enkf_experiment_plan.update_enkf_experiment_plan(version=version,
        configuration=configuration, name=name,
        call=models.nemo.enkf_tools.update_enkf_experiment_plan.FROM_DUMMY_ASSIMILATION)
    
      # create the experiment object
      experiment = models.nemo.enkf_tools.utilities.enkf_experiment.EnKFExperiment(version=version,
        configuration=configuration, name=name)
      experiment_plan = experiment.get_experiment_plan(for_modification=True)
    
      # check that all member experiments are completed
      leg = experiment_plan.get_leg(index=leg_index)
    
      if leg.status != enkf_experiment_plan.LEG_STATUS_ASSIMILATION_RUNNING:
        if leg.status == enkf_experiment_plan.LEG_STATUS_ALL_MEMBERS_EXTENDED:
          experiment_plan.unlock_and_close()
          raise utilities.generic.ClimateToolboxException(
            'Not all member experiments are completed up to leg %s.'
            %models.nemo.utilities.base_experiment_plan.format_leg_index(index=leg_index))
        else:
          assert False, 'the status of leg %s was not expected to be "%s"'%(
            models.nemo.utilities.base_experiment_plan.format_leg_index(index=leg_index), leg.status)
    
      # comment:
      #   - if the status was supposed to be "LEG_STATUS_ALL_MEMBERS_COMPLETED",
      #     "update_enkf_experiment_plan" has already set it to "LEG_STATUS_ASSIMILATION_RUNNING"
      #     to avoid coming back (for a very short time) to a state suggesting that the assimilation
      #     is not ongoing yet
      #   - the objective of this tool is actually only to wait that all members are completed, so
      #     there is nothing more to do
      #   - the tool is probably more complex than it needs to be, but it has been done in such a
      #     way that it follows the same philosophy than an actual assimilation job
    
      # mark the leg as skipped
      print('The assimilation for leg %s has been skipped, because'%leg.format_index()
        + ' observation files are marked as missing at date %s.\n'%leg.get_next_leg_start_date())
      leg.status = enkf_experiment_plan.LEG_STATUS_ASSIMILATION_SKIPPED
    
      # write the updated experiment plan
      experiment_plan.rewrite()
    
      # re-launch the experiment
      print('Re-launching the experiment to proceed with the next leg...\n')
      models.nemo.enkf_tools.launch_enkf_experiment.launch_enkf_experiment(version=version,
        configuration=configuration, name=name)
    
    
    #################
    # main function #
    #################
    
    def main():
    
      parser = argparse.ArgumentParser()
      models.nemo.utilities.argument_parser.add_argument_version(parser=parser)
      models.nemo.utilities.argument_parser.add_argument_configuration(parser=parser)
      models.nemo.utilities.argument_parser.add_argument_name(parser=parser)
      models.nemo.utilities.argument_parser.add_argument_leg_index(parser=parser)
      arguments = vars(parser.parse_args())
    
      try:
        dummy_assimilation_operations(**arguments)
      except utilities.generic.ClimateToolboxException as e:
        e.display()
    
    
    if __name__ == '__main__':
      main()