def __init__(self, parameters=None, meta=None): #: Django model self.model = meta.model model_meta = getattr(self.model, '_meta') # make pycharm happy # model fields excluding AutoFields and related fields like one-to-many all_model_fields = [ f for f in model_meta.get_fields(include_parents=False) if not f.auto_created ] all_field_names = [f.name for f in all_model_fields] # field names # use all fields if no parameters given if parameters is None: parameters = DataParameter.fromkeys(all_field_names, {}) fields = getattr(meta, 'fields', all_field_names) # specified fields LOGGER.debug('fields:\n%r', fields) exclude = getattr(meta, 'exclude', []) # specifically excluded fields for f in all_model_fields: # skip any fields not specified in data source if f.name not in fields or f.name in exclude: LOGGER.debug('skipping %s', f.name) continue # add field to parameters or update parameters with field type param_dict = {'ftype': f.get_internal_type()} if f.name in parameters: parameters[f.name]['extras'].update(param_dict) else: parameters[f.name] = DataParameter(**param_dict) super(DjangoModelReader, self).__init__(parameters, meta)
class PythagoreanData(DataSource): adjacent_side = DataParameter(units='cm', uncertainty=1.0) opposite_side = DataParameter(units='cm', uncertainty=1.0) def __prepare_data__(self): for k, v in self.parameters.iteritems(): self.uncertainty[k] = {k: v['uncertainty'] * UREG.percent} class Meta: data_cache_enabled = False data_reader = ArgumentReader
class ArgSrcTest(DataSource): air_temp = DataParameter(**{'units': 'celsius', 'argpos': 0}) latitude = DataParameter(**{'units': 'degrees', 'isconstant': True}) longitude = DataParameter(**{'units': 'degrees', 'isconstant': True}) timezone = DataParameter(**{'units': 'hours'}) def __prepare_data__(self): pass class Meta: data_reader = ArgumentReader data_cache_enabled = False
class VelocityData(DataSource): distance = DataParameter(units='m', isconstant=True, uncertainty=0.01, argpos=0) elapsed_time = DataParameter(units='s', isconstant=True, uncertainty=0.01, argpos=1) def __prepare_data__(self): pass class Meta: data_reader = ArgumentReader data_cache_enabled = False
class DjangoSrcTest2(DataSource): """ Data source from Django model that excludes fields. """ # parameters air_temp = DataParameter(**{'units': 'celsius'}) latitude = DataParameter(**{'units': 'degrees'}) longitude = DataParameter(**{'units': 'degrees'}) class Meta: data_reader = DjangoModelReader data_cache_enabled = False model = MyModel exclude = ('timezone', 'pvmodule') def __prepare_data__(self): pass
class PythagorasData(DataSource): a = DataParameter(**{'units': 'cm', 'argpos': 0}) b = DataParameter(**{'units': 'cm', 'argpos': 2}) a_unc = DataParameter(**{'units': 'cm', 'argpos': 1}) b_unc = DataParameter(**{'units': 'cm', 'argpos': 3}) class Meta: data_cache_enabled = False data_reader = ArgumentReader def __prepare_data__(self): keys = self.parameters.keys() for k in keys: if k.endswith('_unc'): unc = self.data.pop(k) self.data_source.pop(k) kunc = k[:-4] v = self.data[kunc] if not unc.dimensionless: unc = (unc / v) # raises dimensionality error if not dimensionless self.uncertainty[kunc] = {kunc: unc.to(UREG.percent)} else: self.isconstant[k] = True
class DataSourceTest4(DataSource): """ Test data source with parameters in file. """ latitude = DataParameter( **{ "description": "latitude", "units": "radians", "isconstant": True, "dtype": "float", "uncertainty": 1.0 }) class Meta: data_file = 'pvpower.json' data_path = os.path.join(PROJ_PATH, 'data') def __prepare_data__(self): pass
class PVPowerData(DataSource): """ Data sources for PV Power demo. """ latitude = DataParameter(units="degrees", uncertainty=1.0) longitude = DataParameter(units="degrees", uncertainty=1.0) elevation = DataParameter(units="meters", uncertainty=1.0) timestamp_start = DataParameter() timestamp_count = DataParameter() module = DataParameter() inverter = DataParameter() module_database = DataParameter() inverter_database = DataParameter() Tamb = DataParameter(units="degC", uncertainty=1.0) Uwind = DataParameter(units="m/s", uncertainty=1.0) surface_azimuth = DataParameter(units="degrees", uncertainty=1.0) timezone = DataParameter() def __prepare_data__(self): parameters = getattr(self, DataSourceBase._param_attr) # set frequencies for k in ('HOURLY', 'MONTHLY', 'YEARLY'): self.data[k] = k self.isconstant[k] = True # apply metadata for k, v in parameters.items(): # TODO: this should be applied in data reader using _meta_names from # data registry which should use a meta class and all parameter # files should have same layout even xlrd and numpy readers, etc. self.isconstant[k] = True # set all data "isconstant" True # uncertainty is dictionary if 'uncertainty' in v: self.uncertainty[k] = {k: v['uncertainty'] * UREG.percent} # convert initial timestamp to datetime self.data['timestamp_start'] = datetime(*self.data['timestamp_start']) # get module and inverter databases self.data['module_database'] = pvlib.pvsystem.retrieve_sam( self.data['module_database'], path=SANDIA_MODULES ) self.data['inverter_database'] = pvlib.pvsystem.retrieve_sam( self.data['inverter_database'], path=CEC_INVERTERS ) # get module and inverter self.data['module'] = self.data['module_database'][self.data['module']] self.data['inverter'] = ( self.data['inverter_database'][self.data['inverter']] )
class DataSourceTest2(DataSource): """ Test data source with parameters in code. """ latitude = DataParameter( **{ "description": "latitude", "units": "degrees", "isconstant": True, "dtype": "float", "uncertainty": 1.0 }) longitude = DataParameter( **{ "description": "longitude", "units": "degrees", "isconstant": True, "dtype": "float", "uncertainty": 1.0 }) elevation = DataParameter( **{ "description": "altitude of site above sea level", "units": "meters", "isconstant": True, "dtype": "float", "uncertainty": 1.0 }) timestamp_start = DataParameter( **{ "description": "initial timestamp", "isconstant": True, "dtype": "datetime" }) timestamp_count = DataParameter( **{ "description": "number of timesteps", "isconstant": True, "dtype": "int" }) module = DataParameter(**{ "description": "PV module", "isconstant": True, "dtype": "str" }) inverter = DataParameter(**{ "description": "PV inverter", "isconstant": True, "dtype": "str" }) module_database = DataParameter(**{ "description": "module databases", "isconstant": True, "dtype": "str" }) inverter_database = DataParameter(**{ "description": "inverter database", "isconstant": True, "dtype": "str" }) Tamb = DataParameter( **{ "description": "average yearly ambient air temperature", "units": "degC", "isconstant": True, "dtype": "float", "uncertainty": 1.0 }) Uwind = DataParameter( **{ "description": "average yearly wind speed", "units": "m/s", "isconstant": True, "dtype": "float", "uncertainty": 1.0 }) surface_azimuth = DataParameter( **{ "description": "site rotation", "units": "degrees", "isconstant": True, "dtype": "float", "uncertainty": 1.0 }) timezone = DataParameter(**{ "description": "timezone", "isconstant": True, "dtype": "str" }) def __prepare_data__(self): pass