|
| 1 | +# standard library imports |
| 2 | +import logging |
| 3 | + |
| 4 | +# related third party imports |
1 | 5 | import pandas as pd
|
| 6 | +import pytz |
| 7 | + |
| 8 | +# local application/library specific imports |
| 9 | +from pvlib.location import Location |
| 10 | +from pvlib.io.iotools import get_loc_latlon, localise_df |
2 | 11 |
|
3 | 12 | # required dateconverters
|
4 | 13 | def dtp_soda_pro_macc_rad(date):
|
5 |
| - "datetime converter for MACC-RAD data and others |
| 14 | + """ |
| 15 | + datetime converter for MACC-RAD data and others |
| 16 | + """ |
6 | 17 | datetime_stamp = date.split('/')[0]
|
7 | 18 |
|
8 | 19 | return datetime_stamp
|
9 |
| - |
| 20 | + |
10 | 21 | #XXX read metadata / header
|
11 | 22 |
|
12 | 23 | def read_maccrad_metadata(file_csv, name='maccrad'):
|
@@ -39,36 +50,90 @@ def read_maccrad_metadata(file_csv, name='maccrad'):
|
39 | 50 | alt_line = line
|
40 | 51 | alt = float(alt_line.split(':')[1])
|
41 | 52 | # alt = float(line.split(':')[1])
|
| 53 | + if "Time reference" in line: |
| 54 | + if "Universal time (UT)" in line: |
| 55 | + tz_raw = 'UTC' |
| 56 | + else: |
| 57 | + logging.debug('No metadata on timezone found in input file') |
| 58 | + logging.debug('Assuming UTC as default timezone') |
| 59 | + tz_raw = 'UTC' |
| 60 | + |
| 61 | + tz_loc = get_loc_latlon(lat, lon) |
42 | 62 |
|
43 |
| - # How to get a time zone from a location using |
44 |
| - # latitude and longitude coordinates? |
45 |
| - # http://stackoverflow.com/a/16086964 |
46 |
| - # upstream: https://github.com/MrMinimal64/timezonefinder |
47 |
| - from timezonefinder import TimezoneFinder |
48 |
| - tf = TimezoneFinder() |
49 |
| - tz = tf.timezone_at(lng=lon, lat=lat) |
50 | 63 |
|
51 |
| - from pvlib.location import Location |
52 | 64 |
|
53 | 65 | location = Location(lat, lon, name=name, altitude=alt,
|
54 |
| - tz=tz) |
| 66 | + tz=tz_loc) |
| 67 | + |
| 68 | + return tz_raw, location |
| 69 | + |
| 70 | +def maccrad_df_to_pvlib(df_raw, tz_raw, loc, localise=True): |
| 71 | + """Change some properties of the dataframe to be more compliant with pvlib |
55 | 72 |
|
56 |
| - return location |
| 73 | + * localisation |
| 74 | + * column renaming |
| 75 | + |
| 76 | + """ |
| 77 | + |
| 78 | + if localise: |
| 79 | + # timezone localisations |
| 80 | + df_pvlib = localise_df(df_raw, tz_source_str=tz_raw, loc.tz) |
| 81 | + # column renaming |
| 82 | + df_pvlib.index.name = 'datetime' |
| 83 | + |
| 84 | + return df_pvlib |
| 85 | + |
| 86 | +#def maccrad_df_to_pvlib(df): |
| 87 | +# |
| 88 | +# |
| 89 | +# pass |
| 90 | + |
57 | 91 |
|
58 | 92 | #XXX read data
|
59 |
| -def read_maccrad(file_csv, output='df'): |
| 93 | +def read_maccrad(file_csv, loc_name=None, skiprows=40, output='all'): |
60 | 94 | """
|
61 | 95 | Read MACC-RAD current format for files into a pvlib-ready dataframe
|
| 96 | + |
| 97 | + Parameters |
| 98 | + ---------- |
| 99 | + file_csv : a csv file corresponding to the reader format |
| 100 | + skiprows : skiprows as in pandas.io.read_csv |
| 101 | + The example files require skipping 40 rows. |
| 102 | + output : all / loc / df_pvlib |
| 103 | + df_raw returns only the a pandas.DataFrame using the raw data from the |
| 104 | + file (this can be helpful for debugging and comparison |
| 105 | + with restuls obtained with other programs, e.g. |
| 106 | + spreadsheet) |
| 107 | + all returns df_raw, returns a pandas.DataFrame reformatted to match the |
| 108 | + `variable naming convention <variables_style_rules>` |
| 109 | + for pvliball outputs, and a location created from |
| 110 | + the metadata in raw input file header |
| 111 | + as tuple |
| 112 | +
|
| 113 | + |
62 | 114 | """
|
63 |
| - df = pd.read_csv(file_csv, sep=';', skiprows=40, header=0, |
| 115 | + df_raw = pd.read_csv(file_csv, sep=';', skiprows=skiprows, header=0, |
64 | 116 | index_col=0, parse_dates=True,
|
65 | 117 | date_parser=dtp_soda_pro_macc_rad)
|
66 |
| - |
67 |
| - if output == 'loc': |
68 |
| - loc = read_maccrad_metadata(file_csv) |
69 |
| - res = (loc, df) |
| 118 | +#TODO: add loc_name |
| 119 | +#TODO: add reformat needs loc! |
| 120 | +#TODO: add simplify output options raw or all |
| 121 | + if output == 'df_raw': |
| 122 | + res = df_raw |
| 123 | + if output == 'test': |
| 124 | + res = df_pvlib |
70 | 125 | else:
|
71 |
| - res = df |
| 126 | + tz_raw, loc = read_maccrad_metadata(file_csv) |
| 127 | + df_pvlib = maccrad_df_to_pvlib(df_raw, tz_raw, loc, localise=True) |
| 128 | + res = (df_raw, df_pvlib, loc) |
| 129 | +# if output == 'loc': |
| 130 | +# |
| 131 | +# res = loc, df |
| 132 | +# if output == 'all': |
| 133 | +# # not calculated outside conditional to reduce overhead of metadata |
| 134 | +# # reading if not desired |
| 135 | +# loc = read_maccrad_metadata(file_csv) |
| 136 | +# res = (df_raw, df_pvlib, loc) |
72 | 137 |
|
73 | 138 |
|
74 | 139 | return res
|
|
0 commit comments