def create_site(api, site): """Post a new site to the API and create it's applicable reference observations. Parameters ---------- api : io.APISession An APISession with a valid JWT for accessing the Reference Data user. site : dict Dictionary describing the site to post. This will be instantiated as a datamodel.Site object and the value of 'extra_parameters' will be serialized to json. Returns ------- datamodel.Site The created site object. """ # get a reference to network before we serialize extra_parameters network = site['extra_parameters']['network'] network_handler = NETWORKHANDLER_MAP.get(network) if network_handler is None: logger.warning(f'Unrecognized network, {network} on Site ' f'{site["name"]} Observations cannot be ' 'automatically generated.') return site.update({'extra_parameters': json.dumps(site['extra_parameters'])}) site_name = f"{network} {common.clean_name(site['name'])}" existing = common.existing_sites(api) if site_name in existing: logger.info('Site, %s, already exists', site_name) created = existing[site_name] else: site['name'] = site_name site_to_create = Site.from_dict(site) try: created = api.create_site(site_to_create) except HTTPError as e: logger.error(f"Failed to create Site {site['name']}.") logger.debug(f'HTTP Error: {e.response.text}') return False else: logger.info(f'Created Site {created.name} successfully.') network_handler.initialize_site_observations(api, created) try: network_handler.initialize_site_forecasts(api, created) except ValueError as e: logger.error('Cannot create forecasts for %s: %s', site_name, e) return created
def test_initialize_site_forecasts(mocker, test_site): mock_create_fx = mocker.patch( 'solarforecastarbiter.io.reference_observations.srml.common.' 'create_forecasts') mock_api = mocker.MagicMock() srml.initialize_site_forecasts(mock_api, test_site) assert 'ac_power' in mock_create_fx.call_args[0][2] assert 'dc_power' in mock_create_fx.call_args[0][2] regular_site_dict = test_site_dict.copy() regular_site_dict.pop('modeling_parameters') reg_site = Site.from_dict(regular_site_dict) srml.initialize_site_forecasts(mock_api, reg_site) assert 'ac_power' not in mock_create_fx.call_args[0][2] assert 'dc_power' not in mock_create_fx.call_args[0][2]
def test_decode_extra_parameters_error(site): with pytest.raises(ValueError): common.decode_extra_parameters(Site.from_dict(site))
def test_decode_extra_parameters(): metadata = Site.from_dict(site_string_dicts[0]) params = common.decode_extra_parameters(metadata) assert params['network'] == 'DOE ARM' assert params['observation_interval_length'] == 1
def test_check_network(networks, site, expected): metadata = Site.from_dict(site) assert common.check_network(networks, metadata) == expected
def test_create_observation_exists(mock_api, site_objects_param, observation_objects_param): variable = 'ghi' site = site_objects_param[0] common.create_observation(mock_api, site, variable) mock_api.create_observation.assert_not_called() long_site_name = Site.from_dict({ 'name': 'ARM site with just abouts sixty four characters in its name oops', 'latitude': 1, 'longitude': 1, 'elevation': 5, 'timezone': 'Etc/GMT+8', 'extra_parameters': ('{"network": "DOE ARM", "network_api_abbreviation": ' '"site_abbrev", "network_api_id": "thing", ' '"observation_interval_length": 1}'), }) observation_long_site_name = Observation.from_dict({ 'name': 'site_abbrev air_temperature', 'variable': 'air_temperature', 'interval_label': 'ending', 'interval_value_type':
"network_api_id": 'BMS', "network_api_abbreviation": 'abbrv'}, } ]] def expected_site(site): new_site = site.copy() network = site['extra_parameters'].get('network', '') new_site['name'] = f"{network} {site['name']}" new_site.update({'extra_parameters': json.dumps(site['extra_parameters'])}) return new_site site_string_dicts = [expected_site(site) for site in site_dicts()] site_objects = [Site.from_dict(site) for site in site_string_dicts] @pytest.fixture def site_dicts_param(): return site_string_dicts @pytest.fixture def site_objects_param(): return site_objects def site_to_obs(site): ep = json.loads(site.extra_parameters) interval_length = ep['observation_interval_length']
"ac_capacity": 0.02, "ac_loss_factor": 0.0, "dc_capacity": 0.02, "dc_loss_factor": 0.0, "surface_azimuth": 180.0, "surface_tilt": 15.0, "temperature_coefficient": 0.3, "tracking_type": "fixed" }, "name": "Ashland OR PV", "timezone": "Etc/GMT+8", "provider": "", "site_id": "", } test_site_object = Site.from_dict(test_site_dict) @pytest.fixture() def test_site(): return test_site_object srml_df = pd.DataFrame({ 'ghi_1': { Timestamp('2020-03-01 00:00:00-0800', tz='Etc/GMT+8'): 0.0 }, 'ghi_1_flag': { Timestamp('2020-03-01 00:00:00-0800', tz='Etc/GMT+8'): 11 }, 'dni_1': {