8000 iotools component for maccrad by dacoex · Pull Request #274 · pvlib/pvlib-python · GitHub
[go: up one dir, main page]

Skip to content

iotools component for maccrad #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Prev Previous commit
Next Next commit
added pvsyst reader
  • Loading branch information
dacoex committed Dec 2, 2016
commit 8e29054dc723be7a745ec4b709fbcea32696cb38
4 changes: 3 additions & 1 deletion pvlib/iotools/api.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from pvlib.iotools.tmy import readtmy2, readtmy3
from pvlib.ioiotools.maccrad import read_maccrad
from pvlib.iotools.maccrad import read_maccrad
from pvlib.iotools.pvsyst import read_pvsyst_hourly

9 changes: 9 additions & 0 deletions pvlib/iotools/iotools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
"""
Common functions that support the different data readers / writers

TODO: add a sanity checker
* is the data between sunrise & sunset --> avoid timezone / timeshift errors

"""


import pytz
# How to get a time zone from a location using
# latitude and longitude coordinates?
Expand Down
13 changes: 8 additions & 5 deletions pvlib/iotools/maccrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# local application/library specific imports
from pvlib.location import Location
from pvlib.io.iotools import get_loc_latlon, localise_df
from pvlib.iotools.iotools import get_loc_latlon, localise_df

# required dateconverters
def dtp_soda_pro_macc_rad(date):
Expand Down Expand Up @@ -68,9 +68,12 @@ def read_maccrad_metadata(file_csv, name='maccrad'):
location = Location(lat, lon, name=name, altitude=alt,
tz=tz_loc)

return tz_raw, location

#XXX
metadata = None

return tz_raw, location, metadata

#XXX convert to pvlib conventions
def maccrad_df_to_pvlib(df_raw, tz_raw, loc, localise=True):
"""Change some properties of the dataframe to be more compliant with pvlib

Expand Down Expand Up @@ -127,11 +130,11 @@ def read_maccrad(file_csv, loc_name=None, skiprows=40, output='all'):
#TODO: add loc_name
#TODO: add reformat needs loc!
#TODO: add simplify output options raw or all
print (output)
# print (output)
if output == 'df_raw':
res = df_raw
if output == 'all':
tz_raw, loc = read_maccrad_metadata(file_csv)
tz_raw, loc, metadata = read_maccrad_metadata(file_csv)
loc.name = (loc.name + ' @ ' + 'lat (deg. N), lon (deg. E): ' +
str(loc.latitude) + ', ' + str(loc.longitude))
df_pvlib = maccrad_df_to_pvlib(df_raw, tz_raw, loc, localise=True)
Expand Down
128 changes: 128 additions & 0 deletions pvlib/iotools/pvsyst.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,132 @@
"""
Read & Write PVSyst Output
"""
# standard library imports
import logging

# related third party imports
import numpy as np
import pandas as pd
import pytz

# local application/library specific imports
from pvlib.location import Location
from pvlib.iotools.iotools import get_loc_latlon, localise_df



#XXX read metadata / header
def read_pvsyst_h_metadata(file_csv, name='pvsyst'):
if not name:
name = file_csv.split('.')[0]

## if file is on local drive
f = open(file_csv)
for line in f:
if "Geographical Site" in line:
site = line.split(";")[3]
country = line.split(";")[4]
continent = line.split(";")[5]
name = site

meta_project = "Project"
if meta_project in line:
project = line.split(";")[1]
meta_meteo = "Meteo data"
if meta_meteo in line:
meteo_data = line.split(";")[1]
meta_variant = "Simulation variant"
if meta_variant in line:
variant = line.split(";")[1]


lat = np.nan
logging.debug('PVSyst output CSV file has not latitue information. \
Check the site site file of the PVSyst project.')
lon = np.nan
logging.debug('PVSyst output CSV file has not longitude information. \
Check the site site file of the PVSyst project.')
alt = np.nan
logging.debug('PVSyst output CSV file has not altitude information. \
Check the site site file of the PVSyst project.')

tz_raw = ''
logging.debug('PVSyst output CSV file has not timezone information. \
Check the site site file of the PVSyst project.')

location = Location(lat, lon, name=name, altitude=alt,
# tz=tz_raw
)

#XXX other metadata
metadata = {
meta_project : project,
meta_meteo : meteo_data,
meta_variant : variant,
}

return tz_raw, location, metadata

#XXX convert to pvlib conventions
def pvsyst_h_to_pvlib(df_raw, tz_raw, loc, localise=False):
"""Change some properties of the dataframe to be more compliant with pvlib

* localisation
* column renaming
* setting dataframe name description according to datasource

"""

if localise:
# timezone localisations
df_pvlib = localise_df(df_raw, tz_source_str=tz_raw,
tz_target_str=loc.tz)
else:
df_pvlib = df_raw.copy()
# TODO: adjust column renaming
# column renaming
df_pvlib.index.name = 'datetime'
df_pvlib.rename(columns={'GlobHor': 'ghi',
'T Amb': 'temp_air',
'GlobEff': 'g_poa_effective',
'EArrMPP': 'pdc, dc',
'FTransp': 'transposition_factor',
'AngInc': 'surface_tilt',
'E_Grid': 'pac, ac',
},
inplace=True)

# name the dataframe according to data source
df_pvlib.df_name = loc.name

return df_pvlib

#XXX read data
def read_pvsyst_hourly(file_csv, output='all', localise=False):
df_raw = pd.read_csv(file_csv, sep=';', skiprows=[11,12], index_col=0,
parse_dates=True, header=8)


if output == 'df_raw':
res = df_raw
if output == 'all':
tz_raw, loc, metadata = read_pvsyst_h_metadata(file_csv)
loc.name = (loc.name + ' of ' + metadata['Project'] +
' using meteo data input "' + metadata['Meteo data'] +
'" for simlation variant "' +
metadata['Simulation variant'] + '"')

df_pvlib = pvsyst_h_to_pvlib(df_raw, tz_raw, loc, localise=localise)
# res = df_pvlib
res = (df_raw, df_pvlib, loc)
# if output == 'loc':
#
# res = loc, df
# if output == 'all':
# # not calculated outside conditional to reduce overhead of metadata
# # reading if not desired
# loc = read_maccrad_metadata(file_csv)
# res = (df_raw, df_pvlib, loc)


return res
1 change: 1 addition & 0 deletions pvlib/test/test_io_maccrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
maccrad_csv = "irradiation-0e2a19f2-abe7-11e5-a880-5254002dbd9b.csv"
maccrad_url_full = maccrad_url_base + maccrad_csv
maccrad_csv_dir = os.path.join("..", "..", "..", "pvlib_data", "MACC-RAD", "carpentras")
maccrad_csv_dir = os.path.join("..", "data", "iotools", "MACC-RAD", "carpentras")
maccrad_csv_path = os.path.join(maccrad_csv_dir, maccrad_csv)

data_maccrad = read_maccrad(maccrad_csv_path, output='all')
Expand Down
19 changes: 19 additions & 0 deletions pvlib/test/test_io_pvsyst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
"""
Test pvsyst component of iotools


"""
# standard library imports
import os

# local application/library specific imports
from pvlib.iotools.pvsyst import read_pvsyst_hourly


pvsyst_csv_h_dir = os.path.join("..", "data", "iotools", "PVSyst", "Userhourly")
pvsyst_csv_h = "DEMO_Geneva_HourlyRes_0_many-vars.CSV"
pvsyst_csv_h = "DEMO_Geneva_HourlyRes_1.CSV"
pvsyst_csv_h_path = os.path.join(pvsyst_csv_h_dir, pvsyst_csv_h)

data_pvsyst = read_pvsyst_hourly(pvsyst_csv_h_path, output='all')
0