def test_temp_convert(): with pytest.warns(pvlibDeprecationWarning): amodel = GFS() data = pd.DataFrame({'temp_air': [273.15]}) data['temp_air'] = amodel.kelvin_to_celsius(data['temp_air']) assert_allclose(data['temp_air'].values, 0.0)
def test_queryvariables(): amodel = GFS() old_variables = amodel.variables new_variables = ['u-component_of_wind_height_above_ground'] data = amodel.get_data(_latitude, _longitude, _start, _end, query_variables=new_variables) data['u-component_of_wind_height_above_ground']
def test_bounding_box(): amodel = GFS() latitude = [31.2,32.2] longitude = [-111.9,-110.9] new_variables = {'temperature':'Temperature_surface'} data = amodel.get_query_data(latitude, longitude, _time, variables=new_variables)
def test_cloud_cover_to_ghi_linear(): amodel = GFS() ghi_clear = 1000 offset = 25 out = amodel.cloud_cover_to_ghi_linear(0, ghi_clear, offset=offset) assert_allclose(out, 1000) out = amodel.cloud_cover_to_ghi_linear(100, ghi_clear, offset=offset) assert_allclose(out, 250)
def test_queryvariables(): amodel = GFS() new_variables = ['u-component_of_wind_height_above_ground'] data = amodel.get_data(_latitude, _longitude, _start, _end, query_variables=new_variables) data['u-component_of_wind_height_above_ground']
def test_cloud_cover_to_ghi_linear(): with pytest.warns(pvlibDeprecationWarning): amodel = GFS() ghi_clear = 1000 offset = 25 out = amodel.cloud_cover_to_ghi_linear(0, ghi_clear, offset=offset) assert_allclose(out, 1000) out = amodel.cloud_cover_to_ghi_linear(100, ghi_clear, offset=offset) assert_allclose(out, 250)
def __get_weather(self, start: pd.Timestamp, end: pd.Timestamp) -> pd.DataFrame: """ Get weather data for period. :param start: - pd.Timestamp, begin of period :param end: - pd.Timestamp, end of period :return: pd.DataFrame, Column names are: ``ghi, dni, dhi`` """ fx_model = GFS() return fx_model.get_processed_data(self.geo["latitude"], self.geo["longitude"], start, end)
def test_queryvariables(): with pytest.warns(pvlibDeprecationWarning): amodel = GFS() new_variables = ['u-component_of_wind_height_above_ground'] data = amodel.get_data(_latitude, _longitude, _start, _end, query_variables=new_variables) data['u-component_of_wind_height_above_ground']
def get_irradiance(lat=latitude, lon=longitude, tz=tzo, intervals_of_3=1, time=datetime.datetime.now() + datetime.timedelta(hours=-5)): start = pd.Timestamp(time, tz=tz) end = start + pd.Timedelta(hours=3 * intervals_of_3) irrad_vars = ['ghi', 'dni', 'dhi'] print(start, end) model = GFS() raw_data = model.get_data(lat, lon, start, end) # print(raw_data.head()) data = raw_data data = model.rename(data) data['temp_air'] = model.kelvin_to_celsius(data['temp_air']) data['wind_speed'] = model.uv_to_speed(data) irrad_data = model.cloud_cover_to_irradiance(data['total_clouds']) data = data.join(irrad_data, how='outer') data = data[model.output_variables] data = model.rename(raw_data) irrads = model.cloud_cover_to_irradiance(data['total_clouds'], how='clearsky_scaling') # change this to list when taking more than one values return (irrads.ghi.values.tolist()), (data['total_clouds'].values / 100)
def load_GFS_data(latitude=33.8688, longitude=151.2093, tz='Australia/Sydney', days=7): # latitude, longitude, tz = 32.2, -110.9, 'US/Arizona' # latitude, longitude, tz = 32.2, -110.9, 'US/Arizona' # latitude = 33.8688 # longitude=151.2093 # tz='Australia/Sydney' start = pd.Timestamp(datetime.date.today(), tz=tz) end = start + pd.Timedelta(days=7) irrad_vars = ['ghi', 'dni', 'dhi'] model = GFS() raw_data = model.get_data(latitude, longitude, start, end) print(raw_data.head()) data = raw_data data = model.rename(data) data['temp_air'] = model.kelvin_to_celsius(data['temp_air']) data['wind_speed'] = model.uv_to_speed(data) irrad_data = model.cloud_cover_to_irradiance(data['total_clouds']) data = data.join(irrad_data, how='outer') data = data[model.output_variables] print(data.head()) data = model.process_data(raw_data) print(data.head()) data = model.get_processed_data(latitude, longitude, start, end) print(data.head()) return (data)
def download_forecasts(request): modules_per_string = request.session['modules_per_string'] strings_per_inverter = request.session['strings_per_inverter'] module = request.session['module'] inverter = request.session['inverter'] latitude = request.session['latitude'] longitude = request.session['longitude'] tz = request.session['timezone'] sandia_modules = retrieve_sam('SandiaMod') cec_inverters = retrieve_sam('SandiaInverter') # Parametros de la granja solar surface_tilt = 30 surface_azimuth = 180 # pvlib uses 0=North, 90=East, 180=South, 270=West convention albedo = 0.2 # Rango de tiempo start = pd.Timestamp(date.today(), tz=tz) # Pronostico a 3 días en adelante end = start + pd.Timedelta(days=3) module = pd.Series(sandia_modules[module]) inverter = cec_inverters[inverter] # model a big tracker for more fun system = SingleAxisTracker(module_parameters=module, inverter_parameters=inverter, modules_per_string=modules_per_string, strings_per_inverter=strings_per_inverter) # fx is a common abbreviation for forecast fx_model = GFS() fx_data = fx_model.get_processed_data(latitude, longitude, start, end) # use a ModelChain object to calculate modeling intermediates mc = ModelChain(system, fx_model.location) # extract relevant data for model chain mc.run_model(fx_data.index, weather=fx_data) AC = mc.ac.fillna(0) response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename=AC_5days_forecasts.csv' AC.to_csv(path_or_buf=response, sep=';', float_format='%.2f', index=True, decimal=",") return response
def _make_pv_forecast(self, forecast) -> pd.DataFrame: """Compile the forecast required for PV generation prediction Uses pvlib to generate solar irradiance predictions. Arguments: forecast {pandas.DataFrame} -- DarkSky originated forecast """ # Annoyingly, the PV & wind libraries want temperature named differently pv_forecast = forecast.rename(columns={ 'temperature': 'air_temp', 'windSpeed': 'wind_speed', }) # Use PV lib to get insolation based on the cloud cover reported here model = GFS() # Next up, we get hourly solar irradiance using interpolated cloud cover # We can get this from the clearsky GHI... if tables in sys.modules: # We can use Ineichen clear sky model (uses pytables for turbidity) clearsky = self.pv_location.get_clearsky(pv_forecast.index) else: # We can't, so use 'Simplified Solis' clearsky = self.pv_location.get_clearsky(pv_forecast.index, model='simplified_solis') # ... and by knowledge of where the sun is solpos = self.pv_location.get_solarposition(pv_forecast.index) ghi = model.cloud_cover_to_ghi_linear(pv_forecast['cloudCover'] * 100, clearsky['ghi']) dni = disc(ghi, solpos['zenith'], pv_forecast.index)['dni'] dhi = ghi - dni * np.cos(np.radians(solpos['zenith'])) # Whump it all together and we have our forecast! pv_forecast['dni'] = dni pv_forecast['dhi'] = dhi pv_forecast['ghi'] = ghi return pv_forecast
def test_model_define(): models = [ type(NAM()), type(GFS()), type(HRRR()), type(RAP()), type(NDFD()) ] model = PM.model_define() assert (type(model) in models) == True
def get_cast(start_date, end_date): from pvlib.pvsystem import PVSystem, retrieve_sam from pvlib.temperature import TEMPERATURE_MODEL_PARAMETERS from pvlib.tracking import SingleAxisTracker from pvlib.modelchain import ModelChain sandia_modules = retrieve_sam('sandiamod') cec_inverters = retrieve_sam('cecinverter') module = sandia_modules['SolarWorld_Sunmodule_250_Poly__2013_'] inverter = cec_inverters['ABB__TRIO_20_0_TL_OUTD_S1_US_480__480V_'] temperature_model_parameters = TEMPERATURE_MODEL_PARAMETERS['sapm'][ 'open_rack_glass_glass'] # model a single axis tracker system = SingleAxisTracker( module_parameters=module, inverter_parameters=inverter, temperature_model_parameters=temperature_model_parameters, modules_per_string=15, strings_per_inverter=4) # fx is a common abbreviation for forecast fx_model = GFS() forecast_mod = fx_model.get_processed_data(latitude, longitude, start_date, end_date) # use a ModelChain object to calculate modeling intermediates mchain = ModelChain(system, fx_model.location) # extract relevant data for model chain mchain.run_model(forecast_mod) acp = mchain.ac.fillna(0) return acp
def _get_forecast_gfs_day(lat: float, lon: float, date: datetime.date) -> pd.DataFrame: """ Suspected that multithreading this does not work, because of some internal logic within pvlib.forecast.ForecastModel.get_data """ logger.debug(f"GFS day {lat} {lon} {date}") today = datetime.datetime.today() date_after = date + datetime.timedelta(days=1) one_week_from_today = today + datetime.timedelta(weeks=1) assert lat % 0.25 == 0, "Latitude must be multiple of 0.25" assert lon % 0.25 == 0, "Longitude must be multiple of 0.25" # assert one_week_from_today.date() - date >= datetime.timedelta(), "Cannot be more than 1 week in the future" model = GFS(resolution="quarter") start = datetime.datetime.combine(date, datetime.time()) end = datetime.datetime.combine(date_after, datetime.time()) try: data: pd.DataFrame = model.get_data(lat, lon, start, end) except requests.exceptions.ConnectionError: raise ConnectionAbortedError("Connection Error while fetching forecast data. Check network connection.") return model.process_data(data)
def model_define(self): """Defines the PVLib Model which is to be instantiated. Returns: :mod: `pvlib` :class: `ForecastModel` """ if self.model_name == 'GFS': return GFS() elif self.model_name == 'HRRR': return HRRR() elif self.model_name == 'RAP': return RAP() elif self.model_name == 'NDFD': return NDFD() else: return NAM()
def forecast(latitude, longitude, tz='UTC'): # specify time range. start = Timestamp(datetime.date.today(), tz=tz) end = start + Timedelta(days=7) irrad_vars = ['ghi', 'dni', 'dhi'] # 0.25 deg available model = GFS() # retrieve data. returns pandas.DataFrame object raw_data = model.get_data(latitude, longitude, start, end) #print(raw_data.head()) data = raw_data data = data.resample('15min').asfreq() data = data.interpolate(method='linear', limit_direction='forward', axis=0) # rename the columns according the key/value pairs in model.variables. data = model.rename(data) # convert temperature data['temp_air'] = model.kelvin_to_celsius(data['temp_air']) # convert wind components to wind speed data['wind_speed'] = model.uv_to_speed(data) # calculate irradiance estimates from cloud cover. # uses a cloud_cover to ghi to dni model or a # uses a cloud cover to transmittance to irradiance model. # this step is discussed in more detail in the next section irrad_data = model.cloud_cover_to_irradiance(data['total_clouds']) data = data.join(irrad_data, how='outer') # keep only the final data data = data[model.output_variables] fig = data[['ghi', 'dni', 'dhi']].plot().get_figure() return fig #latitude, longitude, tz = -34, -58, 'America/Argentina/Buenos_Aires' #fig=forecast(latitude, longitude, tz) #fig.savefig('irradiance.png')
def calculate(self): ## Load forecasted irradiance model = GFS() forecast_data = irr_forecast(model, float(self.days.get()), float(self.lat.get()), float(self.long.get()), self.tz.get(), float(self.surf_tilt.get()), float(self.surf_azm.get()), float(self.albedo.get())) ## Calculate solar position solpos = sol_pos(forecast_data, model) ## Calculate extraterrestrial irradiance dni_extra = calc_dnix(model=model) ## Calculate POA sky diffused irradiance poa_sky_diffuse = poa_sd(forecast_data, solpos, dni_extra, float(self.surf_tilt.get()), float(self.surf_azm.get())) ## Calculate POA ground diffused irradiance poa_ground_diffuse = poa_gd(forecast_data, float(self.surf_tilt.get()), float(self.albedo.get())) ## Calcualte AOI aoi = AOI(float(self.surf_tilt.get()), float(self.surf_azm.get()), solpos) ## Calculate total POA irradiance poa_irrad = poa_tot(aoi, forecast_data, poa_sky_diffuse, poa_ground_diffuse) ## Calculate cell and module temperatures pvtemps = temp(poa_irrad, forecast_data) ## Calculate DC power output of module p_dc = DC_out(solpos, poa_irrad, aoi, pvtemps) ## Calculate AC power output of inverter p_ac = AC_out(p_dc) ## Plot AC output self.ax.cla() self.ax.set_title("Forecast of AC Power Output from Inverter") self.ax.set_ylabel("AC Power Output (W)") self.ax.set_xlabel("Date") self.ax.plot(p_ac) self.canvas.draw()
def test_set_location(): amodel = GFS() latitude, longitude = 32.2, -110.9 time = datetime.now(timezone('UTC')) amodel.set_location(time, latitude, longitude)
def test_temp_convert(): amodel = GFS() data = pd.DataFrame({'temp_air': [273.15]}) data['temp_air'] = amodel.kelvin_to_celsius(data['temp_air']) assert_allclose(data['temp_air'].values, 0.0)
def test_full(): GFS(set_type='full')
def test_latest(): GFS(set_type='latest')
import pandas as pd import datetime from pvlib.forecast import GFS # specify location (Tucson, AZ) latitude, longitude, tz = 32.2, -110.9, 'US/Arizona' # specify time range. start = pd.Timestamp(datetime.date.today(), tz=tz) end = start + pd.Timedelta(days=7) irrad_vars = ['ghi', 'dni', 'dhi'] model = GFS() processed_data = model.get_processed_data(latitude=latitude, longitude=longitude, start=start, end=end, headers={ 'User-Agent': 'UserNoAuth', 'Front-End-Https': 'on' }, verify=False)
def test_set_query_time_range_tzfail(): amodel = GFS() with pytest.raises(TypeError): amodel.set_query_time_range(datetime.now(), datetime.now())
def test_set_location(): amodel = GFS() latitude, longitude = 32.2, -110.9 time = 'UTC' amodel.set_location(time, latitude, longitude)
longitude = -110.9 tz = 'US/Mountain' surface_tilt = 30 surface_azimuth = 180 # pvlib uses 0=North, 90=East, 180=South, 270=West convention albedo = 0.2 start = pd.Timestamp(datetime.date.today(), tz=tz) # today's date end = start + pd.Timedelta(days=7) # 7 days from today # Define forecast model fm = GFS() #fm = NAM() #fm = NDFD() #fm = RAP() #fm = HRRR() # Retrieve data forecast_data = fm.get_processed_data(latitude, longitude, start, end) ghi = forecast_data['ghi'] sandia_modules = pvsystem.retrieve_sam('SandiaMod') sandia_module = sandia_modules.Canadian_Solar_CS5P_220M___2009_
def test_cloud_cover_to_transmittance_linear(): amodel = GFS() assert_allclose(amodel.cloud_cover_to_transmittance_linear(0), 0.75) assert_allclose(amodel.cloud_cover_to_transmittance_linear(100), 0.0)
def test_cloud_cover_to_transmittance_linear(): amodel = GFS() assert_allclose(amodel.cloud_cover_to_transmittance_linear(0), 0.75) assert_allclose(amodel.cloud_cover_to_transmittance_linear(100), 0.0) assert_allclose(amodel.cloud_cover_to_transmittance_linear(0, 0.5), 0.5)
longitude = 31.46824 # latitude = -28.893597 # longitude = 31.468293 tz = 'Africa/Johannesburg' surface_tilt = 30 surface_azimuth = 180 albedo = 0.2 #Set beginning and end date end = pd.Timestamp(datetime.date.today(), tz=tz) start = end - timedelta(12) # Define forecast model fm = GFS() # Retrieve data from forecast API and perform data preparation previous_forecast = fm.get_data(latitude, longitude, start, end) previous_forecast.index = previous_forecast.index.strftime('%Y-%m-%d %H:%M:%S') previous_forecast.index = pd.to_datetime(previous_forecast.index) #resample to three hours to match weather data sampling rate data_res = data.resample('3H', offset='2H').mean() #set datetime limits of solar farm data to match weather data forecast_dates = previous_forecast.index start_datetime = forecast_dates[0] list_r = data_res.index stop_datetime = list_r[-5]
inverter = cec_inverters[ 'SMA_America__SC630CP_US__with_ABB_EcoDry_Ultra_transformer_'] temperature_model_parameters = TEMPERATURE_MODEL_PARAMETERS['sapm'][ 'open_rack_glass_polymer'] system = PVSystem(surface_tilt=20, surface_azimuth=180, module_parameters=myModule, inverter_parameters=inverter, temperature_model_parameters=temperature_model_parameters, modules_per_string=25, strings_per_inverter=8) fx_model = GFS() fx_data = fx_model.get_processed_data(latitudeEsfahan, longitudeEsfahan, start, end) fx_data = fx_data.resample('60min').interpolate() mc = ModelChain(system, fx_model.location) mc.run_model(fx_data) data = (mc.results.ac.fillna(0) / 200 / 274) data = list(zip(data, data.index)) conn = psycopg2.connect( user="******", password="******", host="ec2-63-34-97-163.eu-west-1.compute.amazonaws.com",
timezone="US/Arizona") print(location.desc()) start_time = timeit.default_timer() radiationForecast = RadiationFrameHandler.forecast(location) elapsed = timeit.default_timer() - start_time print("Solcast Radiation Forecast Location: %s Time: %s (seconds)" % (location.name, '%.6f' % elapsed)) # specify time range with timezone start = pd.Timestamp(datetime.date.today(), tz=location.timezone) end = start + pd.Timedelta(days=7) start_time = timeit.default_timer() # fx is a common abbreviation for forecast fx_model = GFS( ) # From Forecast models http://pvlib-python.readthedocs.io/en/latest/api.html#forecast-models fx_data = fx_model.get_processed_data(location.lat, location.lng, start, end) elapsed = timeit.default_timer() - start_time print("pvlib (GFS) Radiation Forecast Location: %s Time: %s (seconds)" % (location.name, '%.6f' % elapsed)) plt.plot(fx_data.ghi, label="ghi - PVLIB (GFS)") plt.plot(radiationForecast.ghi, label="ghi - Solcast", linestyle='dashdot') plt.plot(fx_data.dhi, label="dhi - PVLIB (GFS)") plt.plot(radiationForecast.dhi, label="dhi - Solcast", linestyle='dashdot') plt.plot(fx_data.dni, label="dni - PVLIB (GFS)") plt.plot(radiationForecast.dni, label="dni - Solcast", linestyle='dashdot') plt.legend()
def test_gfs(): GFS(res='quarter')