def __setitem__(self,name,value,nocheck=False): ''' set item for class e.g. self['state'] = 3 ''' if nocheck or not self.__set_if_unset(name,value): if name in ['Data','Name']: self._state[name.lower()] = value elif name in ['Control','Location']: self._state[name.lower()] = value elif name == 'state': # NB 'self' during the __setitem__ call # will be self._state as far as we are # concerned here try: self._state.name.datatype = self.datatype except: pass SpecialVariable.__setattr__(self._state,name,value) #super( State, self ).__setattr__(name,value) # apply any overrides from options self.apply_defaults(self._state,self.options) if self.grid: if 'location' in self.Name.dict().keys(): #pdb.set_trace() self.regrid() else: ParamStorage.__setattr__(self,name,value)
def __setitem__(self, name, value, nocheck=False): ''' set item for class e.g. self['state'] = 3 ''' if nocheck or not self.__set_if_unset(name, value): if name in ['Data', 'Name']: self._state[name.lower()] = value elif name in ['Control', 'Location']: self._state[name.lower()] = value elif name == 'state': # NB 'self' during the __setitem__ call # will be self._state as far as we are # concerned here try: self._state.name.datatype = self.datatype except: pass SpecialVariable.__setattr__(self._state, name, value) #super( State, self ).__setattr__(name,value) # apply any overrides from options self.apply_defaults(self._state, self.options) if self.grid: if 'location' in self.Name.dict().keys(): #pdb.set_trace() self.regrid() else: ParamStorage.__setattr__(self, name, value)
def __getitem__(self,name): ''' get item for class, e.g. x = self['state'] ''' if name in ['Data','Name']: return self._state[name.lower()] elif name in ['Control','Location']: return self._state[name.lower()] elif name == 'state': return SpecialVariable.__getattr__(self._state,name) #return super( State, self ).__getattr__(name ) else: return self.__dict__.__getitem__ ( name )
def __getitem__(self, name): ''' get item for class, e.g. x = self['state'] ''' if name in ['Data', 'Name']: return self._state[name.lower()] elif name in ['Control', 'Location']: return self._state[name.lower()] elif name == 'state': return SpecialVariable.__getattr__(self._state, name) #return super( State, self ).__getattr__(name ) else: return self.__dict__.__getitem__(name)
def reinit(self,options,names=None,datatype=None,limits=None,\ bounds=None,control=None,location=None,env=None,header=None,\ logdir=None,writers={},grid=False,logger=None,\ datadir=None,logfile=None,name=None,info=[],readers=[],debug=None): ''' Method to re-initialise the class instance The setup is on the whole controlled by the datatype which contains e.g. 'x'. This is used to set up the members self.x and self.y as SpecialVariables (see SpecialVariable in eoldas_SpecialVariable.py). There are some special attributes for datatypes starting with 'y'. These are assumed to be observational data, which means that when they are read, the data names associated with them are not limited to those in self.names but rather set to whatever is read in in the data. This is because the data names for observational data may be terms such as waveband names etc that need special interpretation. Also, the default output format for observational data is different to that of other data. The elements self.state is a SpecialVariables which means that they can be assigned various data types (see SpecialVariables) and loaded accordingly (e.g. if a filename is specified, this is read in to the data structure. The SpecialVariables contain 'hidden' datasets, which here are mainly the 'control' and 'location' information. A SpecialVariable has two internal structures: `data` and `name`. The former is used to store data values (e.g. the state values) and the latter to store associated metadata. For example, `control` is passed here e.g. as [`mask`,`vza`] and this gives the metadata that are stored in `name`. The actual values of the control data are stored in the `data` section. For location, we might be passed [`time`,`row`,`col`], so this is set in names.location, and the data.location contains the values of the location at each of these elements. For the actual state dataset, this is stored according to its name, so for `x` the values are stored in data.x and the associated data names in name.x. State datasets must represent at least the mean and standard deviation of a state for them to be of value in EOLDAS. TThe mean is accessed as e.g. self.state for the state dataset. The sd is accessed can be accessed as self._state.sd if it has been set. This reference can also be used to directly set data associated with a SpecialVariable, e.g. self.Data.control = np.zeros([2,3]) to represent 2 samples with 3 control variables. You can access name information similarly with print self.Name.control but this will generate a KeyError if the term has not been set. You can check it exists with: key = 'control' if key in self.Name: this = (self.Data[key],self.Name[key]) To get e.g. a dictionary representation of a SpecialVariable you can use eg: self.Name.to_dict() to get the name dictionary, or thisdict = self._state.to_dict() to get the full representation, which then contains 'data' and 'name' as well as some other information stored in the SpecialVariable. You can similarly load them using e.g. self.Data.update( ParamStorage().from_dict(thisdict['data']) combine=True) ''' # set up a fakes dictionary from the data types self.set('datatype', datatype) self.set('fakes', {'state': '_state'}) # first check that options is sensible self.__check_type(options, ParamStorage, fatal=True) self.options = options from eoldas_Lib import set_default_limits,\ check_limits_valid,quantize_location, sortopt nSpecial = 1 if name == None: import time thistime = str(time.time()) name = type(self).__name__ name = "%s.%s" % (name, thistime) self.thisname = name self.options.thisname = str(name).replace(' ', '_') log_terms = {\ 'logfile':logfile or sortopt(self.options,'logfile',None),\ 'logdir':logdir or sortopt(self.options,'logdir',None),\ 'debug' : debug or sortopt(self.options,'debug',True)} self.datadir = datadir or sortopt(self.options, 'datadir', ["."]) self.header = header or "EOLDAS pickle V1.0 - plewis" env = env or sortopt(self.options, 'env', None) names = names or sortopt(self.options, 'names', None) location = location or sortopt(self.options, 'location', ['time']) control = control or sortopt(self.options, 'control', []) limits = limits or sortopt(self.options,'limits',\ set_default_limits(np.array(location))) limits = limits or self.options.limits limits = np.array(check_limits_valid(limits)) bounds = bounds or sortopt(self.options,'bounds',\ [[None,None]] * xlen(names)) self.options.bounds = bounds self.headers = {'PARAMETERS-V2':"PARAMETERS-V2", \ 'PARAMETERS':"PARAMETERS", \ 'BRDF-UCL':'BRDF-UCL',\ 'BRDF': 'BRDF'} self.headers_2 = {'BRDF-UCL': 'location'} # The ones pre-loaded are # self.read_functions = [self.read_pickle,self.read_numpy_fromfile] self._state = SpecialVariable(info=info,name=self.thisname,\ readers=readers,datadir=self.datadir,\ env=env,writers=writers,\ header=self.header,\ logger=logger,log_terms=log_terms,\ simple=False) # self._state is where data are read into # but self.Data and self.Name are where we access them from self.grid = grid # this is so we can access this object from # inside a SpecialVariable self.state = np.array([0.]) # a default data fmt output if datatype[0] == 'y': self.Name.fmt = 'BRDF' self.Name.state = np.array(['dummy']) else: self.Name.fmt = 'PARAMETERS' n_params = xlen(names) if not n_params: error_msg = \ "The field 'names' must be defined in options or"+ \ "passed directly to this method if you have the data type x" raise Exception(error_msg) self.Name.state = np.array(names) self.Name.location = np.array(location) self.Name.control = np.array(control) self.Name.header = self.header self.Name.bounds = np.array(bounds) self.Name.qlocation = np.array(limits) self.Name.datadir = datadir # # sort this object's name # sort logging self.logger = sortlog(self, log_terms['logfile'], logger, name=self.thisname, logdir=log_terms['logdir'], debug=log_terms['debug']) self.logger.info('Initialising %s' % type(self).__name__)