Esempio n. 1
0
    def archive(self):
        super(UnifiedModel, self).archive()

        # Delete all the stdout log files except the root PE
        # Sorts to ensure root PE is first entry
        files = sorted(glob.glob(
                       os.path.join(self.work_path, 'atm.fort6.pe*')),
                       key=lambda name: int(name.rpartition('.')[-1][2:]))
        if len(files) > 1:
            for f_path in files[1:]:
                os.remove(f_path)

        mkdir_p(self.restart_path)

        # Need to figure out the end date of the model.
        nml_path = os.path.join(self.work_path, 'namelists')
        nml = f90nml.read(nml_path)

        resubmit_inc = nml['NLSTCALL']['RUN_RESUBMIT_INC']
        runtime = um_time_to_time(resubmit_inc)
        # runtime = datetime.timedelta(seconds=runtime)

        basis_time = nml['NLSTCALL']['MODEL_BASIS_TIME']
        init_date = um_date_to_date(basis_time)

        end_date = cal.date_plus_seconds(init_date,
                                         runtime,
                                         cal.GREGORIAN)

        # Save model time to restart next run
        with open(os.path.join(self.restart_path,
                  self.restart_calendar_file), 'w') as restart_file:
            restart_file.write(yaml.dump({'end_date': end_date},
                               default_flow_style=False))

        end_date = date_to_um_dump_date(end_date)

        restart_dump = os.path.join(self.work_path,
                                    'aiihca.da{0}'.format(end_date))
        f_dst = os.path.join(self.restart_path, self.restart)
        if os.path.exists(restart_dump):
            shutil.copy(restart_dump, f_dst)
        else:
            print('payu: error: Model has not produced a restart dump file:\n'
                  '{} does not exist.\n'
                  'Check DUMPFREQim in namelists'.format(restart_dump))
Esempio n. 2
0
File: um.py Progetto: rmholmes/payu
    def archive(self):
        super(UnifiedModel, self).archive()

        # Delete all the stdout log files except the root PE
        # Sorts to ensure root PE is first entry
        files = sorted(glob.glob(os.path.join(self.work_path,
                                              'atm.fort6.pe*')),
                       key=lambda name: int(name.rpartition('.')[-1][2:]))
        if len(files) > 1:
            for f_path in files[1:]:
                os.remove(f_path)

        mkdir_p(self.restart_path)

        # Need to figure out the end date of the model.
        nml_path = os.path.join(self.work_path, 'namelists')
        nml = f90nml.read(nml_path)

        resubmit_inc = nml['NLSTCALL']['RUN_RESUBMIT_INC']
        runtime = um_time_to_time(resubmit_inc)
        # runtime = datetime.timedelta(seconds=runtime)

        basis_time = nml['NLSTCALL']['MODEL_BASIS_TIME']
        init_date = um_date_to_date(basis_time)

        end_date = cal.date_plus_seconds(init_date, runtime, cal.GREGORIAN)

        # Save model time to restart next run
        with open(os.path.join(self.restart_path, self.restart_calendar_file),
                  'w') as restart_file:
            restart_file.write(
                yaml.dump({'end_date': end_date}, default_flow_style=False))

        end_date = date_to_um_dump_date(end_date)

        restart_dump = os.path.join(self.work_path,
                                    'aiihca.da{0}'.format(end_date))
        f_dst = os.path.join(self.restart_path, self.restart)
        if os.path.exists(restart_dump):
            shutil.copy(restart_dump, f_dst)
        else:
            print('payu: error: Model has not produced a restart dump file:\n'
                  '{} does not exist.\n'
                  'Check DUMPFREQim in namelists'.format(restart_dump))
Esempio n. 3
0
File: cice.py Progetto: coecms/payu
    def setup(self):
        super(Cice, self).setup()

        setup_nml = self.ice_in['setup_nml']
        init_date = datetime.date(year=setup_nml['year_init'], month=1, day=1)

        if setup_nml['days_per_year'] == 365:
            caltype = cal.NOLEAP
        else:
            caltype = cal.GREGORIAN

        if self.prior_restart_path:
            # Generate ice.restart_file
            # TODO: better check of restart filename
            iced_restart_file = None
            iced_restart_files = [
                f for f in self.get_prior_restart_files()
                if f.startswith('iced.')
            ]

            if len(iced_restart_files) > 0:
                iced_restart_file = sorted(iced_restart_files)[-1]

            if iced_restart_file is None:
                print('payu: error: No restart file available.')
                sys.exit(errno.ENOENT)

            res_ptr_path = os.path.join(self.work_init_path,
                                        'ice.restart_file')
            if os.path.islink(res_ptr_path):
                # If we've linked in a previous pointer it should be deleted
                os.remove(res_ptr_path)
            with open(res_ptr_path, 'w') as res_ptr:
                res_dir = self.get_ptr_restart_dir()
                print(os.path.join(res_dir, iced_restart_file), file=res_ptr)

            # Update input namelist
            setup_nml['runtype'] = 'continue'
            setup_nml['restart'] = True

            prior_nml_path = os.path.join(self.prior_restart_path,
                                          self.ice_nml_fname)

            # With later versions this file exists in the prior restart path,
            # but this was not always the case, so check, and if not there use
            # prior output path
            if not os.path.exists(prior_nml_path) and self.prior_output_path:
                prior_nml_path = os.path.join(self.prior_output_path,
                                              self.ice_nml_fname)

            # If we cannot find a prior namelist, then we cannot determine
            # the start time and must abort the run.
            if not os.path.exists(prior_nml_path):
                print('payu: error: Cannot find prior namelist {nml}'.format(
                    nml=self.ice_nml_fname))
                sys.exit(errno.ENOENT)

            prior_setup_nml = f90nml.read(prior_nml_path)['setup_nml']

            # The total time in seconds since the beginning of the experiment
            total_runtime = prior_setup_nml['istep0'] + prior_setup_nml['npt']
            total_runtime = total_runtime * prior_setup_nml['dt']
            run_start_date = cal.date_plus_seconds(init_date, total_runtime,
                                                   caltype)
        else:
            # Locate and link any restart files (if required)
            if not setup_nml['ice_ic'] in ('none', 'default'):
                self.link_restart(setup_nml['ice_ic'])

            if setup_nml['restart']:
                self.link_restart(setup_nml['pointer_file'])

            # Initialise runtime
            total_runtime = 0
            run_start_date = init_date

        # Set runtime for this run.
        if self.expt.runtime:
            run_runtime = cal.runtime_from_date(
                run_start_date, self.expt.runtime['years'],
                self.expt.runtime['months'], self.expt.runtime['days'],
                self.expt.runtime.get('seconds', 0), caltype)
        else:
            run_runtime = setup_nml['npt'] * setup_nml['dt']

        # Now write out new run start date and total runtime.
        setup_nml['npt'] = run_runtime / setup_nml['dt']
        assert (total_runtime % setup_nml['dt'] == 0)
        setup_nml['istep0'] = int(total_runtime / setup_nml['dt'])

        # Force creation of a dump (restart) file at end of run
        setup_nml['dump_last'] = True

        nml_path = os.path.join(self.work_path, self.ice_nml_fname)
        self.ice_in.write(nml_path, force=True)
Esempio n. 4
0
File: cice.py Progetto: nicjhan/payu
    def setup(self):
        super(Cice, self).setup()

        # Change perms of restart files, these get overwritten so much be
        # writable.
        for fname in ['o2i.nc', 'i2o.nc', 'u_star.nc', 'grid.nc', 'kmt.nc',
                      'monthly_sstsss.nc', 'mice.nc']:
            path = os.path.join(self.work_input_path, fname)
            if os.path.exists(path):
                perm = (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
                        | stat.S_IWUSR)
                os.chmod(path, perm)

        setup_nml = self.ice_in['setup_nml']
        init_date = datetime.date(year=setup_nml['year_init'], month=1, day=1)

        if setup_nml['days_per_year'] == 365:
            caltype = cal.NOLEAP
        else:
            caltype = cal.GREGORIAN

        if self.prior_restart_path and not self.expt.repeat_run:

            # Generate ice.restart_file
            # TODO: better check of restart filename
            iced_restart_file = None
            for f in self.get_prior_restart_files():
                if 'iced.' in f:
                    iced_restart_file = f

            if iced_restart_file is None:
                print('payu: error: No restart file available.')
                sys.exit(errno.ENOENT)

            res_ptr_path = os.path.join(self.work_init_path,
                                        'ice.restart_file')
            with open(res_ptr_path, 'w') as res_ptr:
                res_dir = self.get_ptr_restart_dir()
                print(os.path.join(res_dir, iced_restart_file), file=res_ptr)

            # Update input namelist
            setup_nml['runtype'] = 'continue'
            setup_nml['restart'] = True

            prior_nml_path = os.path.join(self.prior_restart_path,
                                          self.ice_nml_fname)

            # With later versions this file exists in the prior restart path,
            # but this was not always the case, so check, and if not there use
            # prior output path
            if not os.path.exists(prior_nml_path):
                prior_nml_path = os.path.join(self.prior_output_path,
                                              self.ice_nml_fname)

            prior_setup_nml = f90nml.read(prior_nml_path)['setup_nml']

            # The total time in seconds since the beginning of the experiment
            total_runtime = prior_setup_nml['istep0'] + prior_setup_nml['npt']
            total_runtime = total_runtime * prior_setup_nml['dt']
            run_start_date = cal.date_plus_seconds(init_date, total_runtime,
                                                   caltype)
        else:
            # Locate and link any restart files (if required)
            if not setup_nml['ice_ic'] in ('none', 'default'):
                self.link_restart(setup_nml['ice_ic'])

            if setup_nml['restart']:
                self.link_restart(setup_nml['pointer_file'])

            # Initialise runtime
            total_runtime = 0
            run_start_date = init_date

        # Set runtime for this run.
        if self.expt.runtime:
            run_runtime = cal.runtime_from_date(
                run_start_date,
                self.expt.runtime['years'],
                self.expt.runtime['months'],
                self.expt.runtime['days'],
                self.expt.runtime.get('seconds', 0),
                caltype
            )
        else:
            run_runtime = setup_nml['npt']*setup_nml['dt']

        # Now write out new run start date and total runtime.
        setup_nml['npt'] = run_runtime / setup_nml['dt']
        assert(total_runtime % setup_nml['dt'] == 0)
        setup_nml['istep0'] = int(total_runtime / setup_nml['dt'])

        # Force creation of a dump (restart) file at end of run
        setup_nml['dump_last'] = True

        nml_path = os.path.join(self.work_path, self.ice_nml_fname)
        self.ice_in.write(nml_path, force=True)
Esempio n. 5
0
File: cice.py Progetto: coecms/payu
    def setup(self):
        super(Cice, self).setup()

        setup_nml = self.ice_in['setup_nml']
        init_date = datetime.date(year=setup_nml['year_init'], month=1, day=1)

        if setup_nml['days_per_year'] == 365:
            caltype = cal.NOLEAP
        else:
            caltype = cal.GREGORIAN

        if self.prior_restart_path:
            # Generate ice.restart_file
            # TODO: better check of restart filename
            iced_restart_file = None
            iced_restart_files = [f for f in self.get_prior_restart_files()
                                  if f.startswith('iced.')]

            if len(iced_restart_files) > 0:
                iced_restart_file = sorted(iced_restart_files)[-1]

            if iced_restart_file is None:
                print('payu: error: No restart file available.')
                sys.exit(errno.ENOENT)

            res_ptr_path = os.path.join(self.work_init_path,
                                        'ice.restart_file')
            if os.path.islink(res_ptr_path):
                # If we've linked in a previous pointer it should be deleted
                os.remove(res_ptr_path)
            with open(res_ptr_path, 'w') as res_ptr:
                res_dir = self.get_ptr_restart_dir()
                print(os.path.join(res_dir, iced_restart_file), file=res_ptr)

            # Update input namelist
            setup_nml['runtype'] = 'continue'
            setup_nml['restart'] = True

            prior_nml_path = os.path.join(self.prior_restart_path,
                                          self.ice_nml_fname)

            # With later versions this file exists in the prior restart path,
            # but this was not always the case, so check, and if not there use
            # prior output path
            if not os.path.exists(prior_nml_path) and self.prior_output_path:
                prior_nml_path = os.path.join(self.prior_output_path,
                                              self.ice_nml_fname)

            # If we cannot find a prior namelist, then we cannot determine
            # the start time and must abort the run.
            if not os.path.exists(prior_nml_path):
                print('payu: error: Cannot find prior namelist {nml}'.format(
                    nml=self.ice_nml_fname))
                sys.exit(errno.ENOENT)

            prior_setup_nml = f90nml.read(prior_nml_path)['setup_nml']

            # The total time in seconds since the beginning of the experiment
            total_runtime = prior_setup_nml['istep0'] + prior_setup_nml['npt']
            total_runtime = total_runtime * prior_setup_nml['dt']
            run_start_date = cal.date_plus_seconds(init_date, total_runtime,
                                                   caltype)
        else:
            # Locate and link any restart files (if required)
            if not setup_nml['ice_ic'] in ('none', 'default'):
                self.link_restart(setup_nml['ice_ic'])

            if setup_nml['restart']:
                self.link_restart(setup_nml['pointer_file'])

            # Initialise runtime
            total_runtime = 0
            run_start_date = init_date

        # Set runtime for this run.
        if self.expt.runtime:
            run_runtime = cal.runtime_from_date(
                run_start_date,
                self.expt.runtime['years'],
                self.expt.runtime['months'],
                self.expt.runtime['days'],
                self.expt.runtime.get('seconds', 0),
                caltype
            )
        else:
            run_runtime = setup_nml['npt']*setup_nml['dt']

        # Now write out new run start date and total runtime.
        setup_nml['npt'] = run_runtime / setup_nml['dt']
        assert(total_runtime % setup_nml['dt'] == 0)
        setup_nml['istep0'] = int(total_runtime / setup_nml['dt'])

        # Force creation of a dump (restart) file at end of run
        setup_nml['dump_last'] = True

        nml_path = os.path.join(self.work_path, self.ice_nml_fname)
        self.ice_in.write(nml_path, force=True)
Esempio n. 6
0
    def setup(self):
        if not self.top_level_model:
            return

        cpl_keys = {'cice': ('input_ice.nml', 'coupling', 'runtime0'),
                    'matm': ('input_atm.nml', 'coupling', 'truntime0')}

        # Keep track of this in order to set the oasis runtime.
        run_runtime = 0

        for model in self.expt.models:

            if model.model_type == 'cice' or model.model_type == 'cice5':

                # Horrible hack to make a link to o2i.nc in the
                # work/ice/RESTART directory
                f_name = 'o2i.nc'
                f_src = os.path.join(model.work_path, f_name)
                f_dst = os.path.join(model.work_restart_path, f_name)

                if os.path.isfile(f_src):
                    make_symlink(f_src, f_dst)

            if model.model_type == 'cice5':

                # Stage the supplemental input files
                if model.prior_restart_path:
                    for f_name in model.access_restarts:
                        f_src = os.path.join(model.prior_restart_path, f_name)
                        f_dst = os.path.join(model.work_input_path, f_name)

                        if os.path.isfile(f_src):
                            make_symlink(f_src, f_dst)

            if model.model_type in ('cice', 'matm'):

                # Update the supplemental OASIS namelists
                cpl_fname, cpl_group, runtime0_key = cpl_keys[model.model_type]

                cpl_fpath = os.path.join(model.work_path, cpl_fname)
                cpl_nml = f90nml.read(cpl_fpath)

                # Which calendar are we using, noleap or Gregorian.
                caltype = cpl_nml[cpl_group]['caltype']
                init_date = cal.int_to_date(cpl_nml[cpl_group]['init_date'])

                # Get time info about the beginning of this run. We're
                # interested in:
                #   1. start date of run
                #   2. total runtime of all previous runs.
                if model.prior_restart_path and not self.expt.repeat_run:

                    prior_cpl_fpath = os.path.join(model.prior_restart_path,
                                                   cpl_fname)

                    # With later versions this file exists in the prior restart
                    # path, but this was not always the case, so check, and if
                    # not there use prior output path
                    if not os.path.exists(prior_cpl_fpath):
                        print('payu: warning: {0} missing from prior restart '
                              'path; checking prior output.'.format(cpl_fname),
                              file=sys.stderr)
                        if not os.path.isdir(model.prior_output_path):
                            print('payu: error: No prior output path; '
                                  'aborting run.')
                            sys.exit(errno.ENOENT)

                        prior_cpl_fpath = os.path.join(model.prior_output_path,
                                                       cpl_fname)

                    try:
                        prior_cpl_nml = f90nml.read(prior_cpl_fpath)
                    except IOError as exc:
                        if exc.errno == errno.ENOENT:
                            print('payu: error: {0} does not exist; aborting.'
                                  ''.format(prior_cpl_fpath), file=sys.stderr)
                            sys.exit(exc.errno)
                        else:
                            raise

                    cpl_nml_grp = prior_cpl_nml[cpl_group]

                    # The total time in seconds since the beginning of
                    # the experiment.
                    total_runtime = int(cpl_nml_grp[runtime0_key] +
                                        cpl_nml_grp['runtime'])
                    run_start_date = cal.date_plus_seconds(init_date,
                                                           total_runtime,
                                                           caltype)

                else:
                    total_runtime = 0
                    run_start_date = init_date

                # Get new runtime for this run. We get this from either the
                # 'runtime' part of the payu config, or from the namelist
                if self.expt.runtime:
                    run_runtime = cal.runtime_from_date(
                        run_start_date,
                        self.expt.runtime['years'],
                        self.expt.runtime['months'],
                        self.expt.runtime['days'],
                        self.expt.runtime.get('seconds', 0),
                        caltype)
                else:
                    run_runtime = cpl_nml[cpl_group]['runtime']

                # Now write out new run start date and total runtime.
                cpl_nml[cpl_group]['inidate'] = cal.date_to_int(run_start_date)
                cpl_nml[cpl_group][runtime0_key] = total_runtime
                cpl_nml[cpl_group]['runtime'] = int(run_runtime)

                if model.model_type == 'cice':
                    if self.expt.counter and not self.expt.repeat_run:
                        cpl_nml[cpl_group]['jobnum'] = 1 + self.expt.counter
                    else:
                        cpl_nml[cpl_group]['jobnum'] = 1

                nml_work_path = os.path.join(model.work_path, cpl_fname)
                f90nml.write(cpl_nml, nml_work_path + '~')
                shutil.move(nml_work_path + '~', nml_work_path)

        # Now change the oasis runtime. This needs to be done after the others.
        for model in self.expt.models:
            if model.model_type == 'oasis':
                namcouple = os.path.join(model.work_path, 'namcouple')

                s = ''
                with open(namcouple, 'r+') as f:
                    s = f.read()
                    m = re.search(r"^[ \t]*\$RUNTIME.*?^[ \t]*(\d+)", s,
                                  re.MULTILINE | re.DOTALL)
                    assert(m is not None)
                    s = s[:m.start(1)] + str(run_runtime) + s[m.end(1):]

                with open(namcouple, 'w') as f:
                    f.write(s)
Esempio n. 7
0
    def setup(self):
        super(Cice, self).setup()

        setup_nml = self.ice_in['setup_nml']
        init_date = datetime.date(year=setup_nml['year_init'], month=1, day=1)

        if setup_nml['days_per_year'] == 365:
            caltype = cal.NOLEAP
        else:
            caltype = cal.GREGORIAN

        if self.prior_output_path and not self.expt.repeat_run:

            # Generate ice.restart_file
            # TODO: Check the filenames more aggressively
            try:
                prior_restart_file = self.get_prior_restart_files()[0]
            except IndexError:
                print('payu: error: No restart file available.')
                sys.exit(errno.ENOENT)

            res_ptr_path = os.path.join(self.work_init_path,
                                        'ice.restart_file')
            with open(res_ptr_path, 'w') as res_ptr:
                res_dir = self.get_ptr_restart_dir()
                print(os.path.join(res_dir, prior_restart_file), file=res_ptr)

            # Update input namelist
            setup_nml['runtype'] = 'continue'
            setup_nml['restart'] = True

            prior_nml_path = os.path.join(self.prior_output_path,
                                          self.ice_nml_fname)
            prior_setup_nml = f90nml.read(prior_nml_path)['setup_nml']

            # The total time in seconds since the beginning of the experiment
            total_runtime = prior_setup_nml['istep0'] + prior_setup_nml['npt']
            total_runtime = total_runtime * prior_setup_nml['dt']
            run_start_date = cal.date_plus_seconds(init_date, total_runtime,
                                                   caltype)
        else:
            # Locate and link any restart files (if required)
            if not setup_nml['ice_ic'] in ('none', 'default'):
                self.link_restart(setup_nml['ice_ic'])

            if setup_nml['restart']:
                self.link_restart(setup_nml['pointer_file'])

            # Initialise runtime
            total_runtime = 0
            run_start_date = init_date

        # Set runtime for this run.
        if self.expt.runtime:
            run_runtime = cal.runtime_from_date(
                run_start_date, self.expt.runtime['years'],
                self.expt.runtime['months'], self.expt.runtime['days'],
                self.expt.runtime.get('seconds', 0), caltype)
        else:
            run_runtime = setup_nml['npt'] * setup_nml['dt']

        # Now write out new run start date and total runtime.
        setup_nml['npt'] = run_runtime / setup_nml['dt']
        assert (total_runtime % setup_nml['dt'] == 0)
        setup_nml['istep0'] = int(total_runtime / setup_nml['dt'])

        # Force creation of a dump (restart) file at end of run
        setup_nml['dump_last'] = True

        nml_path = os.path.join(self.work_path, self.ice_nml_fname)
        self.ice_in.write(nml_path, force=True)
Esempio n. 8
0
    def setup(self):

        cpl_keys = {'cice': ('input_ice.nml', 'coupling_nml', 'runtime0'),
                    'matm': ('input_atm.nml', 'coupling', 'truntime0')}

        # Keep track of this in order to set the oasis runtime.
        run_runtime = 0

        for model in self.expt.models:

            if model.model_type == 'cice':

                # Stage the supplemental input files
                if model.prior_restart_path:
                    for f_name in model.access_restarts:
                        f_src = os.path.join(model.prior_restart_path, f_name)
                        f_dst = os.path.join(model.work_input_path, f_name)

                        if os.path.isfile(f_src):
                            make_symlink(f_src, f_dst)

            if model.model_type in ('cice', 'matm'):

                # Update the supplemental OASIS namelists
                cpl_fname, cpl_group, runtime0_key = cpl_keys[model.model_type]

                cpl_fpath = os.path.join(model.work_path, cpl_fname)
                cpl_nml = f90nml.read(cpl_fpath)

                # Which calendar are we using, noleap or Gregorian.
                caltype = cpl_nml[cpl_group]['caltype']
                init_date = cal.int_to_date(cpl_nml[cpl_group]['init_date'])

                # Get time info about the beginning of this run. We're
                # interested in:
                #   1. start date of run
                #   2. total runtime of all previous runs.
                if model.prior_output_path and not self.expt.repeat_run:

                    prior_cpl_fpath = os.path.join(model.prior_output_path,
                                                   cpl_fname)
                    prior_cpl_nml = f90nml.read(prior_cpl_fpath)
                    cpl_nml_grp = prior_cpl_nml[cpl_group]

                    # The total time in seconds since the beginning of
                    # the experiment.
                    total_runtime = int(cpl_nml_grp[runtime0_key] +
                                        cpl_nml_grp['runtime'])
                    run_start_date = cal.date_plus_seconds(init_date,
                                                           total_runtime,
                                                           caltype)

                else:
                    total_runtime = 0
                    run_start_date = init_date

                # Get new runtime for this run. We get this from either the
                # 'runtime' part of the payu config, or from the namelist
                if self.expt.runtime:
                    run_runtime = cal.runtime_from_date(
                        run_start_date,
                        self.expt.runtime['years'],
                        self.expt.runtime['months'],
                        self.expt.runtime['days'],
                        self.expt.runtime.get('seconds', 0),
                        caltype)
                else:
                    run_runtime = cpl_nml[cpl_group]['runtime']

                # Now write out new run start date and total runtime.
                cpl_nml[cpl_group]['inidate'] = cal.date_to_int(run_start_date)
                cpl_nml[cpl_group][runtime0_key] = total_runtime
                cpl_nml[cpl_group]['runtime'] = int(run_runtime)

                if model.model_type == 'cice':
                    if self.expt.counter and not self.expt.repeat_run:
                        cpl_nml[cpl_group]['jobnum'] = 1 + self.expt.counter
                    else:
                        cpl_nml[cpl_group]['jobnum'] = 1

                nml_work_path = os.path.join(model.work_path, cpl_fname)
                f90nml.write(cpl_nml, nml_work_path + '~')
                shutil.move(nml_work_path + '~', nml_work_path)

        # Now change the oasis runtime. This needs to be done after the others.
        for model in self.expt.models:
            if model.model_type == 'oasis':
                namcouple = os.path.join(model.work_path, 'namcouple')

                s = ''
                with open(namcouple, 'r+') as f:
                    s = f.read()
                    m = re.search(r"^[ \t]*\$RUNTIME.*?^[ \t]*(\d+)", s,
                                  re.MULTILINE | re.DOTALL)
                    assert(m is not None)
                    s = s[:m.start(1)] + str(run_runtime) + s[m.end(1):]

                with open(namcouple, 'w') as f:
                    f.write(s)
Esempio n. 9
0
    def setup(self):
        super(Cice, self).setup()

        setup_nml = self.ice_in['setup_nml']
        init_date = datetime.date(year=setup_nml['year_init'], month=1, day=1)

        if setup_nml['days_per_year'] == 365:
            caltype = cal.NOLEAP
        else:
            caltype = cal.GREGORIAN

        if self.prior_output_path and not self.expt.repeat_run:

            # Generate ice.restart_file
            # TODO: Check the filenames more aggressively
            try:
                prior_restart_file = self.get_prior_restart_files()[0]
            except IndexError:
                print('payu: error: No restart file available.')
                sys.exit(errno.ENOENT)

            res_ptr_path = os.path.join(self.work_init_path,
                                        'ice.restart_file')
            with open(res_ptr_path, 'w') as res_ptr:
                res_dir = self.get_ptr_restart_dir()
                print(os.path.join(res_dir, prior_restart_file), file=res_ptr)

            # Update input namelist
            setup_nml['runtype'] = 'continue'
            setup_nml['restart'] = True

            prior_nml_path = os.path.join(self.prior_output_path,
                                          self.ice_nml_fname)
            prior_setup_nml = f90nml.read(prior_nml_path)['setup_nml']

            # The total time in seconds since the beginning of the experiment
            total_runtime = prior_setup_nml['istep0'] + prior_setup_nml['npt']
            total_runtime = total_runtime * prior_setup_nml['dt']
            run_start_date = cal.date_plus_seconds(init_date, total_runtime,
                                                   caltype)
        else:
            # Locate and link any restart files (if required)
            if not setup_nml['ice_ic'] in ('none', 'default'):
                self.link_restart(setup_nml['ice_ic'])

            if setup_nml['restart']:
                self.link_restart(setup_nml['pointer_file'])

            # Initialise runtime
            total_runtime = 0
            run_start_date = init_date

        # Set runtime for this run.
        if self.expt.runtime:
            run_runtime = cal.runtime_from_date(
                                        run_start_date,
                                        self.expt.runtime['years'],
                                        self.expt.runtime['months'],
                                        self.expt.runtime['days'],
                                        self.expt.runtime.get('seconds', 0),
                                        caltype)
        else:
            run_runtime = setup_nml['npt']*setup_nml['dt']

        # Now write out new run start date and total runtime.
        setup_nml['npt'] = run_runtime / setup_nml['dt']
        assert(total_runtime % setup_nml['dt'] == 0)
        setup_nml['istep0'] = int(total_runtime / setup_nml['dt'])

        # Force creation of a dump (restart) file at end of run
        setup_nml['dump_last'] = True

        nml_path = os.path.join(self.work_path, self.ice_nml_fname)
        self.ice_in.write(nml_path, force=True)
Esempio n. 10
0
    def setup(self):

        cpl_keys = {
            'cice': ('input_ice.nml', 'coupling_nml', 'runtime0'),
            'matm': ('input_atm.nml', 'coupling', 'truntime0')
        }

        # Keep track of this in order to set the oasis runtime.
        run_runtime = 0

        for model in self.expt.models:

            if model.model_type == 'cice':

                # Stage the supplemental input files
                if model.prior_restart_path:
                    for f_name in model.access_restarts:
                        f_src = os.path.join(model.prior_restart_path, f_name)
                        f_dst = os.path.join(model.work_input_path, f_name)

                        if os.path.isfile(f_src):
                            make_symlink(f_src, f_dst)

            if model.model_type in ('cice', 'matm'):

                # Update the supplemental OASIS namelists
                cpl_fname, cpl_group, runtime0_key = cpl_keys[model.model_type]

                cpl_fpath = os.path.join(model.work_path, cpl_fname)
                cpl_nml = f90nml.read(cpl_fpath)

                # Which calendar are we using, noleap or Gregorian.
                caltype = cpl_nml[cpl_group]['caltype']
                init_date = cal.int_to_date(cpl_nml[cpl_group]['init_date'])

                # Get time info about the beginning of this run. We're
                # interested in:
                #   1. start date of run
                #   2. total runtime of all previous runs.
                if model.prior_output_path and not self.expt.repeat_run:

                    prior_cpl_fpath = os.path.join(model.prior_output_path,
                                                   cpl_fname)
                    prior_cpl_nml = f90nml.read(prior_cpl_fpath)
                    cpl_nml_grp = prior_cpl_nml[cpl_group]

                    # The total time in seconds since the beginning of
                    # the experiment.
                    total_runtime = int(cpl_nml_grp[runtime0_key] +
                                        cpl_nml_grp['runtime'])
                    run_start_date = cal.date_plus_seconds(
                        init_date, total_runtime, caltype)

                else:
                    total_runtime = 0
                    run_start_date = init_date

                # Get new runtime for this run. We get this from either the
                # 'runtime' part of the payu config, or from the namelist
                if self.expt.runtime:
                    run_runtime = cal.runtime_from_date(
                        run_start_date, self.expt.runtime['years'],
                        self.expt.runtime['months'], self.expt.runtime['days'],
                        self.expt.runtime.get('seconds', 0), caltype)
                else:
                    run_runtime = cpl_nml[cpl_group]['runtime']

                # Now write out new run start date and total runtime.
                cpl_nml[cpl_group]['inidate'] = cal.date_to_int(run_start_date)
                cpl_nml[cpl_group][runtime0_key] = total_runtime
                cpl_nml[cpl_group]['runtime'] = int(run_runtime)

                if model.model_type == 'cice':
                    if self.expt.counter and not self.expt.repeat_run:
                        cpl_nml[cpl_group]['jobnum'] = 1 + self.expt.counter
                    else:
                        cpl_nml[cpl_group]['jobnum'] = 1

                nml_work_path = os.path.join(model.work_path, cpl_fname)
                f90nml.write(cpl_nml, nml_work_path + '~')
                shutil.move(nml_work_path + '~', nml_work_path)

        # Now change the oasis runtime. This needs to be done after the others.
        for model in self.expt.models:
            if model.model_type == 'oasis':
                namcouple = os.path.join(model.work_path, 'namcouple')

                s = ''
                with open(namcouple, 'r+') as f:
                    s = f.read()
                    m = re.search(r"^[ \t]*\$RUNTIME.*?^[ \t]*(\d+)", s,
                                  re.MULTILINE | re.DOTALL)
                    assert (m is not None)
                    s = s[:m.start(1)] + str(run_runtime) + s[m.end(1):]

                with open(namcouple, 'w') as f:
                    f.write(s)
Esempio n. 11
0
File: um.py Progetto: HoWol76/payu
    def setup(self):
        super(UnifiedModel, self).setup()

        # Set up environment variables needed to run UM.
        # Look for a python file in the config directory.
        um_env = imp.load_source('um_env',
                                 os.path.join(self.control_path, 'um_env.py'))
        um_vars = um_env.vars

        # Stage the UM restart file.
        if self.prior_restart_path and not self.expt.repeat_run:
            f_src = os.path.join(self.prior_restart_path, self.restart)
            f_dst = os.path.join(self.work_input_path, self.restart)

            if os.path.isfile(f_src):
                make_symlink(f_src, f_dst)
                # every run is an NRUN with an updated ASTART file
                um_vars['ASTART'] = self.restart
                um_vars['TYPE'] = 'NRUN'

        # Set paths in environment variables.
        for k in um_vars.keys():
            um_vars[k] = um_vars[k].format(input_path=self.input_paths[0],
                                           work_path=self.work_path)
        os.environ.update(um_vars)

        # The above needs to be done in parexe also.
        # FIXME: a better way to do this or remove.
        parexe = os.path.join(self.work_path, 'parexe')
        for line in fileinput.input(parexe, inplace=True):
            line = line.format(input_path=self.input_paths[0],
                               work_path=self.work_path)
            print(line, end='')

        work_nml_path = os.path.join(self.work_path, 'namelists')
        work_nml = f90nml.read(work_nml_path)

        # Modify namelists for a continuation run.
        if self.prior_output_path and not self.expt.repeat_run:

            prior_nml_path = os.path.join(self.prior_output_path, 'namelists')
            prior_nml = f90nml.read(prior_nml_path)

            basis_time = prior_nml['NLSTCALL']['MODEL_BASIS_TIME']
            init_date = um_date_to_date(basis_time)
            resubmit_inc = prior_nml['NLSTCALL']['RUN_RESUBMIT_INC']
            runtime = um_time_to_time(resubmit_inc)

            run_start_date = cal.date_plus_seconds(init_date, runtime,
                                                   cal.GREGORIAN)

            # Write out and save new calendar information.
            run_start_date_um = date_to_um_date(run_start_date)
            work_nml['NLSTCALL']['MODEL_BASIS_TIME'] = run_start_date_um
            work_nml['NLSTCALL']['ANCIL_REFTIME'] = run_start_date_um

            # Tell CABLE that this is a continuation run.
            cable_nml_path = os.path.join(self.work_path, 'cable.nml')
            cable_nml = f90nml.read(cable_nml_path)
            cable_nml['cable']['cable_user']['CABLE_RUNTIME_COUPLED'] = False
            cable_nml.write(cable_nml_path, force=True)

        else:
            run_start_date = work_nml['NLSTCALL']['MODEL_BASIS_TIME']
            run_start_date = um_date_to_date(run_start_date)

        # Set the runtime for this run.
        if self.expt.runtime:
            run_runtime = cal.runtime_from_date(
                run_start_date, self.expt.runtime['years'],
                self.expt.runtime['months'], self.expt.runtime['days'],
                self.expt.runtime.get('seconds', 0), cal.GREGORIAN)
            run_runtime = time_to_um_time(run_runtime)
            work_nml['NLSTCALL']['RUN_RESUBMIT_INC'] = run_runtime
            work_nml['NLSTCALL']['RUN_TARGET_END'] = run_runtime
            work_nml['STSHCOMP']['RUN_TARGET_END'] = run_runtime

        work_nml.write(work_nml_path, force=True)
Esempio n. 12
0
File: um.py Progetto: nicjhan/payu
    def setup(self):
        super(UnifiedModel, self).setup()

        # Stage the UM restart file.
        if self.prior_restart_path and not self.expt.repeat_run:
            f_src = os.path.join(self.prior_restart_path, self.restart)
            f_dst = os.path.join(self.work_input_path, self.restart)

            if os.path.isfile(f_src):
                make_symlink(f_src, f_dst)

        # Set up environment variables needed to run UM.
        # Look for a python file in the config directory.
        um_env = imp.load_source("um_env", os.path.join(self.control_path, "um_env.py"))
        um_vars = um_env.vars

        assert len(self.input_paths) == 1

        # Set paths in environment variables.
        for k in um_vars.keys():
            um_vars[k] = um_vars[k].format(input_path=self.input_paths[0], work_path=self.work_path)
        os.environ.update(um_vars)

        # The above needs to be done in parexe also.
        # FIXME: a better way to do this or remove.
        parexe = os.path.join(self.work_path, "parexe")
        for line in fileinput.input(parexe, inplace=True):
            line = line.format(input_path=self.input_paths[0], work_path=self.work_path)
            print(line, end="")

        work_nml_path = os.path.join(self.work_path, "namelists")
        work_nml = f90nml.read(work_nml_path)

        # Modify namelists for a continuation run.
        if self.prior_output_path and not self.expt.repeat_run:

            prior_nml_path = os.path.join(self.prior_output_path, "namelists")
            prior_nml = f90nml.read(prior_nml_path)

            basis_time = prior_nml["NLSTCALL"]["MODEL_BASIS_TIME"]
            init_date = um_date_to_date(basis_time)
            resubmit_inc = prior_nml["NLSTCALL"]["RUN_RESUBMIT_INC"]
            runtime = um_time_to_time(resubmit_inc)

            run_start_date = cal.date_plus_seconds(init_date, runtime, cal.GREGORIAN)

            # Write out and save new calendar information.
            run_start_date_um = date_to_um_date(run_start_date)
            work_nml["NLSTCALL"]["MODEL_BASIS_TIME"] = run_start_date_um
            work_nml["NLSTCALL"]["ANCIL_REFTIME"] = run_start_date_um

            # Tell CABLE that this is a continuation run.
            cable_nml_path = os.path.join(self.work_path, "cable.nml")
            cable_nml = f90nml.read(cable_nml_path)
            cable_nml["cable"]["cable_user"]["CABLE_RUNTIME_COUPLED"] = False
            cable_nml.write(cable_nml_path, force=True)

        else:
            run_start_date = work_nml["NLSTCALL"]["MODEL_BASIS_TIME"]
            run_start_date = um_date_to_date(run_start_date)

        # Set the runtime for this run.
        if self.expt.runtime:
            run_runtime = cal.runtime_from_date(
                run_start_date,
                self.expt.runtime["years"],
                self.expt.runtime["months"],
                self.expt.runtime["days"],
                self.expt.runtime.get("seconds", 0),
                cal.GREGORIAN,
            )
            run_runtime = time_to_um_time(run_runtime)
            work_nml["NLSTCALL"]["RUN_RESUBMIT_INC"] = run_runtime
            work_nml["NLSTCALL"]["RUN_TARGET_END"] = run_runtime
            work_nml["STSHCOMP"]["RUN_TARGET_END"] = run_runtime

        work_nml.write(work_nml_path, force=True)
Esempio n. 13
0
File: um.py Progetto: coecms/payu
    def setup(self):
        super(UnifiedModel, self).setup()

        # Set up environment variables needed to run UM.
        # Look for a python file in the config directory.
        um_env = imp.load_source('um_env',
                                 os.path.join(self.control_path, 'um_env.py'))
        um_vars = um_env.vars

        # Stage the UM restart file.
        if self.prior_restart_path and not self.expt.repeat_run:
            f_src = os.path.join(self.prior_restart_path, self.restart)
            f_dst = os.path.join(self.work_input_path, self.restart)

            if os.path.isfile(f_src):
                make_symlink(f_src, f_dst)
                # every run is an NRUN with an updated ASTART file
                um_vars['ASTART'] = self.restart
                um_vars['TYPE'] = 'NRUN'

        # Set paths in environment variables.
        for k in um_vars.keys():
            um_vars[k] = um_vars[k].format(input_path=self.input_paths[0],
                                           work_path=self.work_path)
        os.environ.update(um_vars)

        # The above needs to be done in parexe also.
        # FIXME: a better way to do this or remove.
        parexe = os.path.join(self.work_path, 'parexe')
        for line in fileinput.input(parexe, inplace=True):
            line = line.format(input_path=self.input_paths[0],
                               work_path=self.work_path)
            print(line, end='')

        work_nml_path = os.path.join(self.work_path, 'namelists')
        work_nml = f90nml.read(work_nml_path)

        # Modify namelists for a continuation run.
        if self.prior_output_path and not self.expt.repeat_run:

            prior_nml_path = os.path.join(self.prior_output_path, 'namelists')
            prior_nml = f90nml.read(prior_nml_path)

            basis_time = prior_nml['NLSTCALL']['MODEL_BASIS_TIME']
            init_date = um_date_to_date(basis_time)
            resubmit_inc = prior_nml['NLSTCALL']['RUN_RESUBMIT_INC']
            runtime = um_time_to_time(resubmit_inc)

            run_start_date = cal.date_plus_seconds(init_date,
                                                   runtime,
                                                   cal.GREGORIAN)

            # Write out and save new calendar information.
            run_start_date_um = date_to_um_date(run_start_date)
            work_nml['NLSTCALL']['MODEL_BASIS_TIME'] = run_start_date_um
            work_nml['NLSTCALL']['ANCIL_REFTIME'] = run_start_date_um

            # Tell CABLE that this is a continuation run.
            cable_nml_path = os.path.join(self.work_path, 'cable.nml')
            cable_nml = f90nml.read(cable_nml_path)
            cable_nml['cable']['cable_user']['CABLE_RUNTIME_COUPLED'] = False
            cable_nml.write(cable_nml_path, force=True)

        else:
            run_start_date = work_nml['NLSTCALL']['MODEL_BASIS_TIME']
            run_start_date = um_date_to_date(run_start_date)

        # Set the runtime for this run.
        if self.expt.runtime:
            run_runtime = cal.runtime_from_date(
                run_start_date,
                self.expt.runtime['years'],
                self.expt.runtime['months'],
                self.expt.runtime['days'],
                self.expt.runtime.get('seconds', 0),
                cal.GREGORIAN)
            run_runtime = time_to_um_time(run_runtime)
            work_nml['NLSTCALL']['RUN_RESUBMIT_INC'] = run_runtime
            work_nml['NLSTCALL']['RUN_TARGET_END'] = run_runtime
            work_nml['STSHCOMP']['RUN_TARGET_END'] = run_runtime

        work_nml.write(work_nml_path, force=True)