2
2
"""
3
3
Read & Write PVSyst Output
4
4
"""
5
+ # standard library imports
6
+ import logging
5
7
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
0 commit comments