Skip to content

Commit 5eed5dc

Browse files
committed
factor out json modeling params loading to common
1 parent 666d03d commit 5eed5dc

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

solarforecastarbiter/io/reference_observations/common.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,31 @@ def create_forecasts(api, site, variables, templates):
535535
create_one_forecast(api, site, template_fx, var,
536536
piggyback_on=piggyback_on))
537537
return created
538+
539+
540+
def apply_json_site_parameters(json_sitefile, site):
541+
"""Updates site metadata with modeling parameters found in a json file.
542+
543+
Parameters
544+
----------
545+
json_sitefile: str
546+
Absolute path of a json file with a 'sites' key containing a list of
547+
sites in the Solar Forecast Arbiter JSON format.
548+
site: dict
549+
550+
Returns
551+
-------
552+
dict
553+
Copy of inputs plus a new key 'modeling_parameters'.
554+
"""
555+
with open(json_sitefile) as fp:
556+
sites_metadata = json.load(fp)['sites']
557+
site_api_id = str(site['extra_parameters']['network_api_id'])
558+
for site_metadata in sites_metadata:
559+
site_extra_params = json.loads(site_metadata['extra_parameters'])
560+
if str(site_extra_params['network_api_id']) == site_api_id:
561+
site_out = site.copy()
562+
site_out['modeling_parameters'] = site_metadata[
563+
'modeling_parameters']
564+
return site_out
565+
return site

solarforecastarbiter/io/reference_observations/pvdaq.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,7 @@ def adjust_site_parameters(site):
4141
--------
4242
solarforecastarbiter.io.reference_observations.site_df_to_dicts
4343
"""
44-
with open(DEFAULT_SITEFILE) as fp:
45-
sites_metadata = json.load(fp)['sites']
46-
site_api_id = int(site['extra_parameters']['network_api_id'])
47-
for site_metadata in sites_metadata:
48-
site_extra_params = json.loads(site_metadata['extra_parameters'])
49-
if site_extra_params['network_api_id'] == site_api_id:
50-
site_out = site.copy()
51-
site_out['modeling_parameters'] = site_metadata[
52-
'modeling_parameters']
53-
site_out['extra_parameters'].update(site_extra_params)
54-
return site_out
44+
return common.apply_json_site_parameters(DEFAULT_SITEFILE, site)
5545

5646

5747
def initialize_site_observations(api, site):

solarforecastarbiter/io/reference_observations/srml.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,7 @@ def adjust_site_parameters(site):
5858
dict
5959
Copy of inputs plus a new key 'modeling_parameters'.
6060
"""
61-
with open(DEFAULT_SITEFILE) as fp:
62-
sites_metadata = json.load(fp)['sites']
63-
site_api_id = site['extra_parameters']['network_api_id']
64-
for site_metadata in sites_metadata:
65-
site_extra_params = json.loads(site_metadata['extra_parameters'])
66-
if site_extra_params['network_api_id'] == site_api_id:
67-
site_out = site.copy()
68-
site_out['modeling_parameters'] = site_metadata[
69-
'modeling_parameters']
70-
return site_out
71-
return site
61+
return common.apply_json_site_parameters(DEFAULT_SITEFILE, site)
7262

7363

7464
def request_data(site, year, month):

solarforecastarbiter/io/reference_observations/tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import copy
22
import json
3+
from pkg_resources import resource_filename, Requirement
4+
5+
36
import numpy as np
47
import pandas as pd
58
import pytest
@@ -150,3 +153,11 @@ def mock_fetch(mocker, fake_ghi_data):
150153
fetch = mocker.MagicMock()
151154
fetch.return_value = fake_ghi_data
152155
return fetch
156+
157+
158+
@pytest.fixture
159+
def test_json_site_file():
160+
return resource_filename(
161+
Requirement.parse('solarforecastarbiter'),
162+
'solarforecastarbiter/io/reference_observations/'
163+
'tests/data/test_site.json')

solarforecastarbiter/io/reference_observations/tests/test_common.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,3 +726,15 @@ def test_create_forecasts_outside(template_fx, mocker, log):
726726
templates = [template.replace(name='one'), template.replace(name='two')]
727727
with pytest.raises(ValueError):
728728
common.create_forecasts(api, site, vars_, templates)
729+
730+
731+
@pytest.mark.parametrize('params', [
732+
{'network_api_id': 2},
733+
{'network_api_id': '2'},
734+
])
735+
def test_apply_json_site_parameters_plant(test_json_site_file, params):
736+
new_site = common.apply_json_site_parameters(
737+
test_json_site_file,
738+
{'extra_parameters': params},
739+
)
740+
assert 'modeling_parameters' in new_site

0 commit comments

Comments
 (0)