def get_reopt_results(self, force_download=False): """ Call the reopt_api Parameters --------- force_download: bool Whether to force a new api call if the results file already is on disk Returns ------ results: dict A dictionary of REopt results, as defined """ logger.info("REopt getting results") results = dict() success = os.path.isfile(self.fileout) if not success or force_download: post_url = self.reopt_api_post_url + '&api_key={api_key}'.format( api_key=get_developer_nrel_gov_key()) resp = requests.post(post_url, json.dumps(self.post), verify=False) if resp.ok: run_id_dict = json.loads(resp.text) try: run_id = run_id_dict['run_uuid'] except KeyError: msg = "Response from {} did not contain run_uuid.".format( post_url) raise KeyError(msg) poll_url = self.reopt_api_poll_url + '{run_uuid}/results/?api_key={api_key}'.format( run_uuid=run_id, api_key=get_developer_nrel_gov_key()) results = self.poller(url=poll_url) with open(self.fileout, 'w') as fp: json.dump(obj=results, fp=fp) logger.info("Received REopt response, exported to {}".format( self.fileout)) else: text = json.loads(resp.text) if "messages" in text.keys(): logger.error("REopt response reading error: " + str(text['messages'])) raise Exception(text["messages"]) resp.raise_for_status() elif success: with open(self.fileout, 'r') as fp: results = json.load(fp=fp) logger.info("Read REopt response from {}".format(self.fileout)) return results
def __init__(self, lat, lon, interconnection_limit_kw: float, load_profile: Sequence, urdb_label: str, solar_model: PVPlant = None, wind_model: WindPlant = None, storage_model: Battery = None, fin_model: Singleowner = None, off_grid=False, fileout=None): """ Initialize REopt API call Parameters --------- lat: float The latitude lon: float The longitude wholesale_rate_above_site_load_us_dollars_per_kwh: float Price of electricity sold back to the grid above the site load, regardless of net metering interconnection_limit_kw: float The limit on system capacity size that can be interconnected to grid load_profile: list The kW demand of the site at every hour (length 8760) urdb_label: string The identifier of a urdb_rate to use *_model: PySAM models Models initialized with correct parameters fileout: string Filename where REopt results should be written """ self.latitude = lat self.longitude = lon self.interconnection_limit_kw = interconnection_limit_kw self.urdb_label = urdb_label self.load_profile = load_profile self.results = None self.api_key = get_developer_nrel_gov_key() # paths self.path_current = os.path.dirname(os.path.abspath(__file__)) self.path_results = os.path.join(self.path_current) self.path_rates = os.path.join(self.path_current, '..', 'resource_files', 'utility_rates') if not os.path.exists(self.path_rates): os.makedirs(self.path_rates) # self.fileout = os.path.join(self.path_results, 'REoptResults.json') if fileout is not None: self.fileout = fileout if off_grid: self.reopt_api_url = 'https://offgrid-electrolyzer-reopt-dev-api.its.nrel.gov/v1/job/' else: self.reopt_api_url = 'https://developer.nrel.gov/api/reopt/v1/job/' self.post = self.create_post(solar_model, wind_model, storage_model, fin_model) logger.info("Created REopt post")
def download_resource(self): success = os.path.isfile(self.filename) if not success: for height, f in self.file_resource_heights.items(): url = 'https://developer.nrel.gov/api/wind-toolkit/v2/wind/wtk-srw-download?year={year}&lat={lat}&lon={lon}&hubheight={hubheight}&api_key={api_key}&email={email}'.format( year=self.year, lat=self.latitude, lon=self.longitude, hubheight=height, api_key=get_developer_nrel_gov_key(), email=self.email) success = self.call_api(url, filename=f) if not success: raise ValueError('Unable to download wind data') # combine into one file to pass to SAM if len(list(self.file_resource_heights.keys())) > 1: success = self.combine_wind_files() if not success: raise ValueError( 'Could not combine wind resource files successfully') return success
def download_resource(self): url = 'https://developer.nrel.gov/api/solar/nsrdb_psm3_download.csv?wkt=POINT({lon}+{lat})&names={year}&leap_day={leap}&interval={interval}&utc={utc}&full_name={name}&email={email}&affiliation={affiliation}&mailing_list={mailing_list}&reason={reason}&api_key={api}&attributes={attr}'.format( year=self.year, lat=self.latitude, lon=self.longitude, leap=self.leap_year, interval=self.interval, utc=self.utc, name=self.name, email=self.email, mailing_list=self.mailing_list, affiliation=self.affiliation, reason=self.reason, api=get_developer_nrel_gov_key(), attr=self.solar_attributes) success = self.call_api(url, filename=self.filename) return success