10000 initial io module prototype by dacoex · Pull Request #270 · pvlib/pvlib-python · GitHub
[go: up one dir, main page]

Skip to content

initial io module prototype #270

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 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
minor changes to improve structure
  • Loading branch information
dacoex committed Dec 1, 2016
commit 11228967a5904930985e9ed8b44612daa2385072
31 changes: 31 additions & 0 deletions pvlib/io/iotools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytz
# How to get a time zone from a location using
# latitude and longitude coordinates?
# http://stackoverflow.com/a/16086964
# upstream: https://github.com/MrMinimal64/timezonefinder
from timezonefinder import TimezoneFinder

def get_loc_latlon(lat, lon):
"""Returns the timezone for a coordinate pair.
"""


tf = TimezoneFinder()
tz = tf.timezone_at(lng=lon, lat=lat)

return tz

def localise_df(df_notz, tz_source_str='UTC', tz_target_str):
"""
localises a pandas.DataFrame (df) to the target time zone of the pvlib-Location

Assumes that the input df does not have a timezone

"""

tz_source_str = pytz.timezone(tz_source_str)
tz_target = pytz.timezone(tz_target_str)

df_tz_target = df_notz.tz_convert(tz_source)

return df_tz_target
103 changes: 84 additions & 19 deletions pvlib/io/maccrad.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
# standard library imports
import logging

# related third party imports
import pandas as pd
import pytz

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

# required dateconverters
def dtp_soda_pro_macc_rad(date):
"datetime converter for MACC-RAD data and others
"""
datetime converter for MACC-RAD data and others
"""
datetime_stamp = date.split('/')[0]

return datetime_stamp

#XXX read metadata / header

def read_maccrad_metadata(file_csv, name='maccrad'):
Expand Down Expand Up @@ -39,36 +50,90 @@ def read_maccrad_metadata(file_csv, name='maccrad'):
alt_line = line
alt = float(alt_line.split(':')[1])
# alt = float(line.split(':')[1])
if "Time reference" in line:
if "Universal time (UT)" in line:
tz_raw = 'UTC'
else:
logging.debug('No metadata on timezone found in input file')
logging.debug('Assuming UTC as default timezone')
tz_raw = 'UTC'

tz_loc = get_loc_latlon(lat, lon)

# How to get a time zone from a location using
# latitude and longitude coordinates?
# http://stackoverflow.com/a/16086964
# upstream: https://github.com/MrMinimal64/timezonefinder
from timezonefinder import TimezoneFinder
tf = TimezoneFinder()
tz = tf.timezone_at(lng=lon, lat=lat)

from pvlib.location import Location

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

return tz_raw, location

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

return location
* localisation
* column renaming

"""

if localise:
# timezone localisations
df_pvlib = localise_df(df_raw, tz_source_str=tz_raw, loc.tz)
# column renaming
df_pvlib.index.name = 'datetime'

return df_pvlib

#def maccrad_df_to_pvlib(df):
#
#
# pass


#XXX read data
def read_maccrad(file_csv, output='df'):
def read_maccrad(file_csv, loc_name=None, skiprows=40, output='all'):
"""
Read MACC-RAD current format for files into a pvlib-ready dataframe

Parameters
----------
file_csv : a csv file corresponding to the reader format
skiprows : skiprows as in pandas.io.read_csv
The example files require skipping 40 rows.
output : all / loc / df_pvlib
df_raw returns only the a pandas.DataFrame using the raw data from the
file (this can be helpful for debugging and comparison
with restuls obtained with other programs, e.g.
spreadsheet)
all returns df_raw, returns a pandas.DataFrame reformatted to match the
`variable naming convention <variables_style_rules>`
for pvliball outputs, and a location created from
the metadata in raw input file header
as tuple


"""
df = pd.read_csv(file_csv, sep=';', skiprows=40, header=0,
df_raw = pd.read_csv(file_csv, sep=';', skiprows=skiprows, header=0,
index_col=0, parse_dates=True,
date_parser=dtp_soda_pro_macc_rad)

if output == 'loc':
loc = read_maccrad_metadata(file_csv)
res = (loc, df)
#TODO: add loc_name
#TODO: add reformat needs loc!
#TODO: add simplify output options raw or all
if output == 'df_raw':
res = df_raw
if output == 'test':
res = df_pvlib
else:
res = df
tz_raw, loc = read_maccrad_metadata(file_csv)
df_pvlib = maccrad_df_to_pvlib(df_raw, tz_raw, loc, localise=True)
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
Expand Down
10 changes: 7 additions & 3 deletions pvlib/test/test_io_maccrad.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# standard library imports
import os

# local application/library specific imports
from pvlib.io.maccrad import read_maccrad


Expand All @@ -10,14 +12,16 @@
maccrad_csv_dir = os.path.join("..", "..", "..", "pvlib_data", "MACC-RAD", "carpentras")
maccrad_csv_path = os.path.join(maccrad_csv_dir, maccrad_csv)

data_maccrad = read_maccrad(maccrad_csv_path, output='loc')
data_maccrad = read_maccrad(maccrad_csv_path, output='all')
data_maccrad = read_maccrad(maccrad_csv_path, output='test')

maccrad_loc = data_maccrad[0]
maccrad_df = data_maccrad[1]

def test_location_coord():
assert (44.0830, 5.0590, 97.00) == (maccrad_loc.latitude, maccrad_loc.longitude,
maccrad_loc.altitude)
assert (44.0830, 5.0590, 97.00) == (maccrad_loc.latitude,
maccrad_loc.longitude,
maccrad_loc.altitude)


def test_location_tz():
Expand Down
0