Ejemplo n.º 1
0
    def __init__(self, calc_interface):
        self.__dict__ = vars(calc_interface)
        self._print_verbose('Initializing Calc instance:', self.__str__())

        #[mod.set_grid_data() for mod in self.model]

        if isinstance(calc_interface.ens_mem, int):
            self.data_in_direc = self.data_in_direc[calc_interface.ens_mem]

        self.dt_set = False

        self.dir_scratch = self._dir_scratch()
        self.dir_archive = self._dir_archive()
        self.file_name = {d: self._file_name(d) for d in self.dtype_out_time}
        self.path_scratch = {d: self._path_scratch(d)
                             for d in self.dtype_out_time}
        self.path_archive = self._path_archive()

        self.data_out = {}

        # Add rows to database.
        session = create_session()
        rn = self.run[0].get_db_entry(session)
        vr, isin = get_or_create(session, dbVar, defaults=None,
                                 name=self.name)
        for d in self.dtype_out_time:
            clc, isin = get_or_create(session, dbCalc, defaults=None,
                                      name=self.name,
                                      filepath=self.path_scratch[d],
                                      var=vr,
                                      run=rn,
                                      intvl_in=self.intvl_in,
                                      intvl_out=self.intvl_out,
                                      dtype_out_time=d,
                                      start_date=self.start_date,
                                      end_date=self.end_date,
                                      pressure_type=str(self.level))
        session.commit()
        session.close()
Ejemplo n.º 2
0
    def __init__(self, name, vars={}, models={}, default_models={}, regions={},
                 direc_out='', nc_dir_struc=False, verbose=True):
        self.verbose = verbose
        if self.verbose:
            print ("Initializing Project instance: %s (%s)"
                   % (name, time.ctime()))
        self.name = name
        self.direc_out = direc_out
        self.nc_dir_struc = nc_dir_struc

        if models:
            self.models = dict_name_keys(models)
        else:
            self.models = {}
        if default_models == 'all':
            self.default_models = self.models
        elif default_models:
            self.default_models = dict_name_keys(default_models)
        else:
            self.default_models = {}
        if regions:
            self.regions = dict_name_keys(regions)
        else:
            self.regions = {}

        for obj_dict in (self.models, self.regions):
            for obj in obj_dict.values():
                setattr(obj, 'proj', self)

        db_models = [m.db_entry for m in models]
        # Add row to database.
        session = create_session()
        self.db_entry, isin = get_or_create(session, dbProj, defaults=None,
                                            name=self.name,
                                            direc_out=self.direc_out)
        if not self.db_entry.models:
            self.db_entry.models = db_models
        session.commit()
        session.close()
Ejemplo n.º 3
0
    def __init__(self, name='', description='', proj=False, grid_file_paths=(),
                 data_in_direc=False, data_in_dir_struc=False,
                 data_in_dur=False, data_in_start_date=False,
                 data_in_end_date=False, default_date_range=False, runs={},
                 default_runs={}, load_grid_data=False, repo_version=False):
        self.name = name
        self.description = description
        self.proj = proj

        self.grid_file_paths = grid_file_paths
        self.repo_version = repo_version

        self.data_in_direc = data_in_direc
        self.data_in_dir_struc = data_in_dir_struc
        self.data_in_dur = data_in_dur
        self.data_in_start_date = data_in_start_date
        self.data_in_end_date = data_in_end_date
        self.default_date_range = default_date_range

        self.runs = dict_name_keys(runs)
        [setattr(run, 'parent', self) for run in self.runs.values()]
        if default_runs:
            self.default_runs = dict_name_keys(default_runs)
        else:
            self.default_runs = {}

        self.grid_data_is_set = False

        # Add row to database.
        session = create_session()
        self.db_entry, isin = get_or_create(session, dbModel, defaults=None,
                                            name=self.name)
        if not self.db_entry.runs:
            self.db_entry.runs = [r.db_entry for r in runs]
        session.commit()
        session.close()
Ejemplo n.º 4
0
    def __init__(self, proj=None, model=None, run=None, ens_mem=None, var=None,
                 date_range=None, region=None, intvl_in=None, intvl_out=None,
                 dtype_in_time=None, dtype_in_vert=None, dtype_out_time=None,
                 dtype_out_vert=None, level=None, chunk_len=False,
                 verbose=True):
        """Create the CalcInterface object with the given parameters."""
        if run not in model.runs.values():
            raise AttributeError("Model '{}' has no run '{}'.  Calc object "
                                 "will not be generated.".format(model, run))
        # 2015-10-13 S. Hill: This tuple-izing is for support of calculations
        # where variables come from different runs.  However, this is a very
        # fragile way of implementing that functionality.  Eventually it will
        # be replaced with something better.
        proj = tuple([proj])
        model = tuple([model])
        if not isinstance(run, (list, tuple)):
            run = tuple([run])
        # Make tuples the same length.
        if len(proj) == 1 and (len(model) > 1 or len(run) > 1):
            proj = tuple(list(proj)*len(run))
        if len(model) == 1 and len(run) > 1:
            model = tuple(list(model)*len(run))

        self.proj = proj
        self.model = model
        self.run = run

        self._set_data_in_attrs()

        self.proj_str = '_'.join(set([p.name for p in self.proj]))
        self.model_str = '_'.join(set([m.name for m in self.model]))
        run_names = [r.name for r in self.run]
        self.run_str = '_'.join(set(run_names))
        self.run_str_full = '_'.join(run_names)

        self.var = var
        self.name = self.var.name
        self.domain = self.var.domain
        self.def_time = self.var.def_time
        self.def_vert = self.var.def_vert
        self.verbose = verbose

        try:
            self.function = self.var.func
        except AttributeError:
            self.function = lambda x: x
        if getattr(self.var, 'variables', False):
            self.variables = self.var.variables
        else:
            self.variables = (self.var,)

        self.ens_mem = ens_mem
        self.level = level
        self.intvl_in = intvl_in
        self.intvl_out = intvl_out
        self.dtype_in_time = dtype_in_time
        self.dtype_in_vert = dtype_in_vert
        self.ps = ps
        if isinstance(dtype_out_time, (list, tuple)):
            self.dtype_out_time = tuple(dtype_out_time)
        else:
            self.dtype_out_time = tuple([dtype_out_time])
        self.dtype_out_vert = dtype_out_vert
        self.region = region

        self.months = TimeManager.month_indices(intvl_out)
        self.start_date = TimeManager.str_to_datetime(date_range[0])
        self.end_date = TimeManager.str_to_datetime(date_range[-1])
        tm = TimeManager(self.start_date, self.end_date, intvl_out)
        self.date_range = tm.create_time_array()

        self.start_date_xray = tm.apply_year_offset(self.start_date)
        self.end_date_xray = tm.apply_year_offset(self.end_date)

        # Add row to database.
        session = create_session()
        db_entry_var, isin = get_or_create(session, dbVar, defaults=None,
                                           name=self.name)
        session.commit()
        session.close()