8000 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
PR for discussion
prototype for #261 & #29
  • Loading branch information
dacoex committed Nov 30, 2016
commit fe33a2f6d412acb13521e54152c16ea150785c61
49 changes: 46 additions & 3 deletions pvlib/io/maccrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,60 @@

# required dateconverters
def dtp_soda_pro_macc_rad(date):
"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):
pass
def read_maccrad_metadata(file_csv, name='maccrad'):
"""
Read metadata from commented lines of the file
* coordinates
* altitude

Retrieve timezone
"""
if not name:
name = file_csv.split('.')[0]

## if file is on local drive
f = open(file_csv)
for line in f:
if "Latitude" in line:
# print (line)
# if line.startswith( "# Latitude"):
lat_line = line
lat = float(lat_line.split(':')[1])
# lat = float(line.split(':')[1])
if "Longitude" in line:
# if line.startswith( "# Longitude"):
lon_line = line
lon = float(lon_line.split(':')[1])
lon = float(line.split(':')[1])
# if line.startswith( "# Altitude"):
if "Altitude" in line:
alt_line = line
alt = float(alt_line.split(':')[1])
# alt = float(line.split(':')[1])

# 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)

return location

#XXX read data

def read_maccrad(file_csv, output='df'):
"""
Read MACC-RAD current format for files into a pvlib-ready dataframe
Expand Down
147 changes: 72 additions & 75 deletions pvlib/test/test_io_maccrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,81 @@
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, output='loc')


## if data is on remotely on github

import urllib

#f = urllib.request.urlopen(maccrad_url_full)

#from urllib.parse import urlparse
#response = urlparse(maccrad_url_full)


from urllib.request import urlopen
response = urlopen(maccrad_url_full)
response = response.decode('utf-8')




req=urllib.request.urlopen(maccrad_url_full)
charset=req.info().get_content_charset()
content=req.read().decode(charset)

data = urllib.request.urlopen(maccrad_url_full).read()

lines = data.splitlines(True)
# http://stackoverflow.com/questions/23131227/how-to-readlines-from-urllib



import requests

response = requests.get(maccrad_url_full).text

for line in response:
# print (line)
if line.startswith( "# Latitude"):
# lat_line = line
# lat = float(lat_line.split(':')[1])
lat = float(line.split(':')[1])
if line.startswith( "# Longitude"):
# lon_line = line
# lon = float(lon_line.split(':')[1])
lon = float(line.split(':')[1])
# if line.startswith( "# Altitude"):
if "Altitude" in line:
alt_line = line
alt = float(alt_line.split(':')[1])
# alt = float(line.split(':')[1])


## if file is on local drive
f = open(maccrad_csv_path)
for line in f:
if "Latitude" in line:
# print (line)
data_maccrad = read_maccrad(maccrad_csv_path, output='loc')

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)


def test_location_tz():
assert 'Europe/Paris' == maccrad_loc.tz


def test_maccrad_recolumn():
assert 'Clear sky GHI' in maccrad_df.columns

def test_maccrad_norecolumn():
assert 'Clear sky GHI' in maccrad_df.columns

def test_maccrad_coerce_year():
coerce_year = 2010
assert (maccrad_df.index.year[0] == coerce_year)


def test_maccrad():
read_maccrad(maccrad_csv_path)

##FIXME: this still crashes
### if data is on remotely on github
#
#
#import urllib
#
##f = urllib.request.urlopen(maccrad_url_full)
#
##from urllib.parse import urlparse
##response = urlparse(maccrad_url_full)
#
#
##from urllib.request import urlopen
##response = urlopen(maccrad_url_full)
##response = response.decode('utf-8')
#
#
#
## http://stackoverflow.com/questions/4981977/how-to-handle-response-encoding-from-urllib-request-urlopen
#req=urllib.request.urlopen(maccrad_url_full)
#charset=req.info().get_content_charset()
#response=req.read().decode(charset)
#
##data = urllib.request.urlopen(maccrad_url_full).read()
#
#lines = response.splitlines(True)
## http://stackoverflow.com/questions/23131227/how-to-readlines-from-urllib
#
#
#
##import requests
##response = requests.get(maccrad_url_full).text
#
#for line in response:
## print (line)
# if line.startswith( "# Latitude"):
lat_line = line
lat = float(lat_line.split(':')[1])
## lat_line = line
## lat = float(lat_line.split(':')[1])
# lat = float(line.split(':')[1])
if "Longitude" in line:
# if line.startswith( "# Longitude"):
lon_line = line
lon = float(lon_line.split(':')[1])
lon = float(line.split(':')[1])
## lon_line = line
## lon = float(lon_line.split(':')[1])
# lon = float(line.split(':')[1])
# if line.startswith( "# Altitude"):
if "Altitude" in line:
alt_line = line
alt = float(alt_line.split(':')[1])
# alt = float(line.split(':')[1])


from timezonefinder import TimezoneFinder
tf = TimezoneFinder()
tz = tf.timezone_at(lng=lon, lat=lat)
## if "Altitude" in line:
# alt_line = line
# alt = float(alt_line.split(':')[1])
## alt = float(line.split(':')[1])

from pvlib.location import Location

location = Location(lat, lon, name=maccrad_csv.split('.')[0], altitude=alt,
tz=tz)
0