Skip to content

Commit 8e29054

Browse files
author
dacoex
committed
added pvsyst reader
1 parent b96c24f commit 8e29054

File tree

6 files changed

+168
-6
lines changed

6 files changed

+168
-6
lines changed

pvlib/iotools/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
from pvlib.iotools.tmy import readtmy2, readtmy3
2-
from pvlib.ioiotools.maccrad import read_maccrad
2+
from pvlib.iotools.maccrad import read_maccrad
3+
from pvlib.iotools.pvsyst import read_pvsyst_hourly
4+

pvlib/iotools/iotools.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""
2+
Common functions that support the different data readers / writers
3+
4+
TODO: add a sanity checker
5+
* is the data between sunrise & sunset --> avoid timezone / timeshift errors
6+
7+
"""
8+
9+
110
import pytz
211
# How to get a time zone from a location using
312
# latitude and longitude coordinates?

pvlib/iotools/maccrad.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# local application/library specific imports
99
from pvlib.location import Location
10-
from pvlib.io.iotools import get_loc_latlon, localise_df
10+
from pvlib.iotools.iotools import get_loc_latlon, localise_df
1111

1212
# required dateconverters
1313
def dtp_soda_pro_macc_rad(date):
@@ -68,9 +68,12 @@ def read_maccrad_metadata(file_csv, name='maccrad'):
6868
location = Location(lat, lon, name=name, altitude=alt,
6969
tz=tz_loc)
7070

71-
return tz_raw, location
72-
71+
#XXX
72+
metadata = None
73+
74+
return tz_raw, location, metadata
7375

76+
#XXX convert to pvlib conventions
7477
def maccrad_df_to_pvlib(df_raw, tz_raw, loc, localise=True):
7578
"""Change some properties of the dataframe to be more compliant with pvlib
7679
@@ -127,11 +130,11 @@ def read_maccrad(file_csv, loc_name=None, skiprows=40, output='all'):
127130
#TODO: add loc_name
128131
#TODO: add reformat needs loc!
129132
#TODO: add simplify output options raw or all
130-
print (output)
133+
# print (output)
131134
if output == 'df_raw':
132135
res = df_raw
133136
if output == 'all':
134-
tz_raw, loc = read_maccrad_metadata(file_csv)
137+
tz_raw, loc, metadata = read_maccrad_metadata(file_csv)
135138
loc.name = (loc.name + ' @ ' + 'lat (deg. N), lon (deg. E): ' +
136139
str(loc.latitude) + ', ' + str(loc.longitude))
137140
df_pvlib = maccrad_df_to_pvlib(df_raw, tz_raw, loc, localise=True)

pvlib/iotools/pvsyst.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,132 @@
22
"""
33
Read & Write PVSyst Output
44
"""
5+
# standard library imports
6+
import logging
57

8+
# related third party imports
9+
import numpy as np
10+
import pandas as pd
11+
import pytz
12+
13+
# local application/library specific imports
14+
from pvlib.location import Location
15+
from pvlib.iotools.iotools import get_loc_latlon, localise_df
16+
17+
18+
19+
#XXX read metadata / header
20+
def read_pvsyst_h_metadata(file_csv, name='pvsyst'):
21+
if not name:
22+
name = file_csv.split('.')[0]
23+
24+
## if file is on local drive
25+
f = open(file_csv)
26+
for line in f:
27+
if "Geographical Site" in line:
28+
site = line.split(";")[3]
29+
country = line.split(";")[4]
30+
continent = line.split(";")[5]
31+
name = site
32+
33+
meta_project = "Project"
34+
if meta_project in line:
35+
project = line.split(";")[1]
36+
meta_meteo = "Meteo data"
37+
if meta_meteo in line:
38+
meteo_data = line.split(";")[1]
39+
meta_variant = "Simulation variant"
40+
if meta_variant in line:
41+
variant = line.split(";")[1]
42+
43+
44+
lat = np.nan
45+
logging.debug('PVSyst output CSV file has not latitue information. \
46+
Check the site site file of the PVSyst project.')
47+
lon = np.nan
48+
logging.debug('PVSyst output CSV file has not longitude information. \
49+
Check the site site file of the PVSyst project.')
50+
alt = np.nan
51+
logging.debug('PVSyst output CSV file has not altitude information. \
52+
Check the site site file of the PVSyst project.')
53+
54+
tz_raw = ''
55+
logging.debug('PVSyst output CSV file has not timezone information. \
56+
Check the site site file of the PVSyst project.')
57+
58+
location = Location(lat, lon, name=name, altitude=alt,
59+
# tz=tz_raw
60+
)
61+
62+
#XXX other metadata
63+
metadata = {
64+
meta_project : project,
65+
meta_meteo : meteo_data,
66+
meta_variant : variant,
67+
}
68+
69+
return tz_raw, location, metadata
70+
71+
#XXX convert to pvlib conventions
72+
def pvsyst_h_to_pvlib(df_raw, tz_raw, loc, localise=False):
73+
"""Change some properties of the dataframe to be more compliant with pvlib
74+
75+
* localisation
76+
* column renaming
77+
* setting dataframe name description according to datasource
78+
79+
"""
80+
81+
if localise:
82+
# timezone localisations
83+
df_pvlib = localise_df(df_raw, tz_source_str=tz_raw,
84+
tz_target_str=loc.tz)
85+
else:
86+
df_pvlib = df_raw.copy()
87+
# TODO: adjust column renaming
88+
# column renaming
89+
df_pvlib.index.name = 'datetime'
90+
df_pvlib.rename(columns={'GlobHor': 'ghi',
91+
'T Amb': 'temp_air',
92+
'GlobEff': 'g_poa_effective',
93+
'EArrMPP': 'pdc, dc',
94+
'FTransp': 'transposition_factor',
95+
'AngInc': 'surface_tilt',
96+
'E_Grid': 'pac, ac',
97+
},
98+
inplace=True)
99+
100+
# name the dataframe according to data source
101+
df_pvlib.df_name = loc.name
102+
103+
return df_pvlib
104+
105+
#XXX read data
106+
def read_pvsyst_hourly(file_csv, output='all', localise=False):
107+
df_raw = pd.read_csv(file_csv, sep=';', skiprows=[11,12], index_col=0,
108+
parse_dates=True, header=8)
109+
110+
111+
if output == 'df_raw':
112+
res = df_raw
113+
if output == 'all':
114+
tz_raw, loc, metadata = read_pvsyst_h_metadata(file_csv)
115+
loc.name = (loc.name + ' of ' + metadata['Project'] +
116+
' using meteo data input "' + metadata['Meteo data'] +
117+
'" for simlation variant "' +
118+
metadata['Simulation variant'] + '"')
119+
120+
df_pvlib = pvsyst_h_to_pvlib(df_raw, tz_raw, loc, localise=localise)
121+
# res = df_pvlib
122+
res = (df_raw, df_pvlib, loc)
123+
# if output == 'loc':
124+
#
125+
# res = loc, df
126+
# if output == 'all':
127+
# # not calculated outside conditional to reduce overhead of metadata
128+
# # reading if not desired
129+
# loc = read_maccrad_metadata(file_csv)
130+
# res = (df_raw, df_pvlib, loc)
131+
132+
133+
return res

pvlib/test/test_io_maccrad.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
maccrad_csv = "irradiation-0e2a19f2-abe7-11e5-a880-5254002dbd9b.csv"
1111
maccrad_url_full = maccrad_url_base + maccrad_csv
1212
maccrad_csv_dir = os.path.join("..", "..", "..", "pvlib_data", "MACC-RAD", "carpentras")
13+
maccrad_csv_dir = os.path.join("..", "data", "iotools", "MACC-RAD", "carpentras")
1314
maccrad_csv_path = os.path.join(maccrad_csv_dir, maccrad_csv)
1415

1516
data_maccrad = read_maccrad(maccrad_csv_path, output='all')

pvlib/test/test_io_pvsyst.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Test pvsyst component of iotools
4+
5+
6+
"""
7+
# standard library imports
8+
import os
9+
10+
# local application/library specific imports
11+
from pvlib.iotools.pvsyst import read_pvsyst_hourly
12+
13+
14+
pvsyst_csv_h_dir = os.path.join("..", "data", "iotools", "PVSyst", "Userhourly")
15+
pvsyst_csv_h = "DEMO_Geneva_HourlyRes_0_many-vars.CSV"
16+
pvsyst_csv_h = "DEMO_Geneva_HourlyRes_1.CSV"
17+
pvsyst_csv_h_path = os.path.join(pvsyst_csv_h_dir, pvsyst_csv_h)
18+
19+
data_pvsyst = read_pvsyst_hourly(pvsyst_csv_h_path, output='all')

0 commit comments

Comments
 (0)