示例#1
0
    def create_new(cls, **kwargs):
        """
        application: create an instance of AdjointForcingData from template with new data
        input: user-defined
        output: AdjointForcingData
        
        eg: new_forcing =  datadef.AdjointForcingData( filelist )
        """
        #each input arg is a dictionary, matching to a record in file_details[class_name]
        #arg name matches the record key
        #arg value is a dictionary, keys are variable in file, values are numpy arrays
        fdata = get_filedict(cls.__name__)
        msg = 'input args incompatible with file list'
        assert set(fdata.keys()) == set(kwargs.keys()), msg
        for label, data in kwargs.items():
            err_msg = "{} data doesn't match template.".format(label)
            assert ncf.validate(fdata[label]['template'], data), err_msg

        for label, record in fdata.items():
            ncf.create_from_template(record['template'],
                                     record['actual'],
                                     var_change=kwargs[label],
                                     date=record['date'],
                                     overwrite=True)
        return cls()
示例#2
0
 def load_from_template(cls):
     """
     extension: create a ModelInputData from the template files
     input: None
     output: ModelInputData
     """
     filedict = get_filedict(cls.__name__)
     for record in filedict.values():
         source = record['template']
         dest = record['actual']
         ncf.copy_compress(source, dest)
     return cls()
示例#3
0
 def load_from_archive(cls, dirname):
     """
     extension: create a SensitivityData from previous archived files.
     input: string (path/to/file)
     output: SensitivityData
     """
     pathname = os.path.realpath(dirname)
     assert os.path.isdir(pathname), 'dirname must be an existing directory'
     filedict = get_filedict(cls.__name__)
     for record in filedict.values():
         source = os.path.join(pathname, record['archive'])
         dest = record['actual']
         ncf.copy_compress(source, dest)
     return cls()
示例#4
0
 def get_kwargs_dict(cls):
     """
     extension: get a dict that will work as kwarg input
     input: None
     output: { file_label : { spcs : np.ndarray(<zeros>) } }
     """
     file_labels = get_filedict(cls.__name__).keys()
     spcs = ncf.get_attr(template.force, 'VAR-LIST').split()
     shape = ncf.get_variable(template.force, spcs[0]).shape
     argdict = {}
     for label in file_labels:
         data = {spc: np.zeros(shape) for spc in spcs}
         argdict[label] = data
     return argdict
示例#5
0
 def load_from_template(cls):
     """
     application: return a valid example with template values.
     input: None
     output: SensitivityData
     
     notes: only used for testing.
     """
     filedict = get_filedict(cls.__name__)
     for record in filedict.values():
         ncf.create_from_template(record['template'],
                                  record['actual'],
                                  var_change={},
                                  date=record['date'])
     return cls()
示例#6
0
    def __init__(self):
        """
        application: create an instance of AdjointForcingData
        input: None
        output: None
        
        notes: assumes all files already exist,
        to create files see create_new or load_from_archive.
        """
        self.file_data = get_filedict(self.__class__.__name__)

        for record in self.file_data.values():
            exists = os.path.isfile(record['actual'])
            assert exists, 'missing file {}'.format(record['actual'])
        return None
 def load_from_archive(cls, dirname):
     """
     extension: create a ModelOutputData from previous archived files
     input: string (path/to/file)
     output: ModelOutputData
     
     notes: this function assumes the filenames match archive default names
     """
     pathname = os.path.realpath(dirname)
     assert os.path.isdir(pathname), 'dirname must be an existing directory'
     filedict = get_filedict(cls.__name__)
     for record in filedict.values():
         source = os.path.join(pathname, record['archive'])
         dest = record['actual']
         ncf.copy_compress(source, dest)
     return cls()
示例#8
0
    def __init__(self):
        """
        application: create an instance of ModelInputData
        input: user-defined
        output: None
        
        notes: assumes all files already exist,
        to create files see create_new or load_from_archive
        """
        self.file_data = get_filedict(self.__class__.__name__)

        #check that required files exist
        for record in self.file_data.values():
            exists = os.path.isfile(record['actual'])
            assert exists, 'missing file {}'.format(record['actual'])
        return None
 def load_from_template(cls):
     """
     application: return a valid example with values from template.
     input: None
     output: ModelOutputData
     
     eg: mock_model_out = datadef.ModelOutputData.example()
     
     notes: only used for testing.
     """
     filedict = get_filedict(cls.__name__)
     for record in filedict.values():
         ncf.create_from_template(record['template'],
                                  record['actual'],
                                  var_change={},
                                  date=record['date'])
     return cls()
示例#10
0
 def __init__(self):
     """
     application: create an instance of ModelOutputData
     input: user-defined
     output: None
     
     eg: new_output =  datadef.ModelOutputData( filelist )
     """
     #check all required files exist and match attributes with templates
     self.file_data = get_filedict(self.__class__.__name__)
     for record in self.file_data.values():
         actual = record['actual']
         template = record['template']
         msg = 'missing {}'.format(actual)
         assert os.path.isfile(actual), msg
         msg = '{} is incompatible with template {}.'.format(
             actual, template)
         assert ncf.match_attr(actual, template,
                               self.checklist) is True, msg
     return None