MechatronicsBlog / Weather_data_Python_PVGIS

Weather data (Solar, Temperature & Wind) in Python
GNU General Public License v3.0
3 stars 1 forks source link

Modelchain #1

Closed PKMendes closed 3 years ago

PKMendes commented 4 years ago

Hi Javi,

Thank you for your code, it was exactly what I was looking. Nevertheless, I am trying to use your code, on: https://pvlib-python.readthedocs.io/en/stable/modelchain.html

I got the first part, which I need to change your columns to the following names:

weather_data = df.rename(columns={"DateTime": "", "GHI": "ghi", "DNI": "dni", "DHI": "dhi", "Tamb": "temp_air", "WS": "wind_speed"})

afterwards, when I run modelchain:

mc.run_model(weather_data)

I got the following warnings, and inspecting the results I conclude that the result is wrong:

" ..\lib\site-packages\pandas\core\indexes\base.py:3331: RuntimeWarning: '<' not supported between instances of 'int' and 'Timestamp', sort order is undefined for incomparable objects

..core\indexes\api.py:205: RuntimeWarning: '<' not supported between instances of 'int' and 'Timestamp', sort order is undefined for incomparable objects

..\indexes\base.py:2192: RuntimeWarning: '<' not supported between instances of 'int' and 'Timestamp', sort order is undefined for incomparable objects

I suspect that Modelchain is expecting integers on panda file, but there are other type on your file. Could you help me overcoming this issue?

JaviBonilla commented 4 years ago

Hi @PKMendes,

Maybe it is a problem with the dataframe (weather_data) index, Modelchain expects that the index is the date time column but in this dataframe the index I think it is just the row number.

Can you try this?

mc.run_model(times=weather_data['DateTime'], weather=weather_data)

But first, do not rename the date time column name, so change it for something like this.

weather_data = df.rename(columns={
"GHI": "ghi",
"DNI": "dni",
"DHI": "dhi",
"Tamb": "temp_air",
"WS": "wind_speed"})

Let me know if this helps.

JaviBonilla commented 4 years ago

I have just seen, that the times argument it not used by run_model and it is only included for compatibility reasons.

def run_model(self, weather, times=None):
        """
        Run the model.

        Parameters
        ----------
        weather : DataFrame
            Column names must be ``'dni'``, ``'ghi'``, ``'dhi'``,
            ``'wind_speed'``, ``'temp_air'``. All irradiance components
            are required. Air temperature of 20 C and wind speed
            of 0 m/s will be added to the DataFrame if not provided.
        times : None, deprecated
            Deprecated argument included for API compatibility, but not
            used internally. The index of the weather DataFrame is used
            for times.

So you have to make the DateTime column the dataframe index.

weather_data.set_index('DateTime')

PKMendes commented 4 years ago

Still with the same warnings. My weather_data looks ok:

DateTime ghi dni dhi TAmb Ws wind_speed temp_air 0 2014-01-01 00:55:00 0.0 0.0 0.0 6.56 2.14 0 20 1 2014-01-01 01:55:00 0.0 0.0 0.0 6.55 2.26 0 20 2 2014-01-01 02:55:00 0.0 0.0 0.0 6.55 2.38 0 20 .....

But the print(mc.aoi) shows data from 1970! 1970-01-01 00:00:00.000000000 162.427726 1970-01-01 00:00:00.000000001 162.427726 .... Name: aoi, Length: 8760, dtype: float64

Length:8760 is ok too.

This is my code:

from datetime import datetime from PvGis import PvGis import plotly.offline as py import plotly.graph_objs as go

import pandas as pd import numpy as np

pvlib imports

import pvlib from pvlib.pvsystem import PVSystem from pvlib.location import Location from pvlib.modelchain import ModelChain from pvlib.temperature import TEMPERATURE_MODEL_PARAMETERS

Create PvGis object and set its inputs

pvGis = PvGis() pvGis.latitude = 40.8991793 pvGis.longitude = -8.3698031 pvGis.start_date = datetime(2014, 1, 1, 00, 00, 00) pvGis.end_date = datetime(2015, 1, 1, 00, 00, 00)

Radiation database: PVGIS-CMSAF (for Europe and Africa), PVGIS-SARAH (for Europe, Africa and Asia)

or PVGIS-NSRDB (for the Americas between 60º N and 20º S)

pvGis.rad_database = 'PVGIS-CMSAF'

Get data

pvGis.request_hourly_time_series()

Save weather data to a CSV file

pvGis.save_csv('weather_data.csv')

Get Pandas DataFrame

df = pvGis.pandas_data_frame()

print (weather_data)

temperature_model_parameters = TEMPERATURE_MODEL_PARAMETERS['sapm']['open_rack_glass_glass']

load some module and inverter specifications

sandia_modules = pvlib.pvsystem.retrieve_sam('SandiaMod')

cec_inverters = pvlib.pvsystem.retrieve_sam('cecinverter') sandia_module = sandia_modules['Canadian_Solar_CS5P_220M2009'] cec_inverter = cec_inverters['ABBMICRO_0_25_I_OUTD_US_208_208V']

location = Location(pvGis.latitude, pvGis.longitude)

system = PVSystem(surface_tilt=20, surface_azimuth=200, module_parameters=sandia_module, inverter_parameters=cec_inverter, temperature_model_parameters=temperature_model_parameters)

mc = ModelChain(system, location)

weather_data = df.rename(columns={ "GHI": "ghi", "DNI": "dni", "DHI": "dhi", "Tamb": "temp_air", "WS": "wind_speed"})

weather_data.set_index('DateTime')

mc.run_model(times=weather_data['DateTime'], weather=weather_data)

mc.run_model(weather_data)

print(mc)

print(mc.aoi)

print(mc.dc)

print(weather_data)

PKMendes commented 4 years ago

I forgot to mention this warning, I believe that is related with what you commented:

...venv\lib\site-packages\pvlib\modelchain.py:956: pvlibDeprecationWarning:

times keyword argument is deprecated and will be removed in 0.8. The index of the weather DataFrame is used for times.

JaviBonilla commented 4 years ago

Hi @PKMendes,

Sorry, I think the index is not being updated in your dataframe, try this.

weather_data = weather_data.set_index('DateTime')

Or alternatively, you can also do this:

weather_data.set_index('DateTime', inplace=True)

PKMendes commented 4 years ago

Hi Javi,

It is working, the solution is what you suggested. Thank you very much for your help!