Skip to content

Commit fe33a2f

Browse files
author
dacoex
committed
PR for discussion
prototype for pvlib#261 & pvlib#29
1 parent d009751 commit fe33a2f

File tree

2 files changed

+118
-78
lines changed

2 files changed

+118
-78
lines changed

pvlib/io/maccrad.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,60 @@
22

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

78
return datetime_stamp
89

910
#XXX read metadata / header
1011

11-
def read_maccrad_metadata(file_csv):
12-
pass
12+
def read_maccrad_metadata(file_csv, name='maccrad'):
13+
"""
14+
Read metadata from commented lines of the file
15+
* coordinates
16+
* altitude
17+
18+
Retrieve timezone
19+
"""
20+
if not name:
21+
name = file_csv.split('.')[0]
22+
23+
## if file is on local drive
24+
f = open(file_csv)
25+
for line in f:
26+
if "Latitude" in line:
27+
# print (line)
28+
# if line.startswith( "# Latitude"):
29+
lat_line = line
30+
lat = float(lat_line.split(':')[1])
31+
# lat = float(line.split(':')[1])
32+
if "Longitude" in line:
33+
# if line.startswith( "# Longitude"):
34+
lon_line = line
35+
lon = float(lon_line.split(':')[1])
36+
lon = float(line.split(':')[1])
37+
# if line.startswith( "# Altitude"):
38+
if "Altitude" in line:
39+
alt_line = line
40+
alt = float(alt_line.split(':')[1])
41+
# alt = float(line.split(':')[1])
42+
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+
51+
from pvlib.location import Location
52+
53+
location = Location(lat, lon, name=name, altitude=alt,
54+
tz=tz)
55+
56+
return location
1357

1458
#XXX read data
15-
1659
def read_maccrad(file_csv, output='df'):
1760
"""
1861
Read MACC-RAD current format for files into a pvlib-ready dataframe

pvlib/test/test_io_maccrad.py

Lines changed: 72 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,84 +10,81 @@
1010
maccrad_csv_dir = os.path.join("..", "..", "..", "pvlib_data", "MACC-RAD", "carpentras")
1111
maccrad_csv_path = os.path.join(maccrad_csv_dir, maccrad_csv)
1212

13-
#data_maccrad = read_maccrad(maccrad_csv, output='loc')
14-
15-
16-
## if data is on remotely on github
17-
18-
import urllib
19-
20-
#f = urllib.request.urlopen(maccrad_url_full)
21-
22-
#from urllib.parse import urlparse
23-
#response = urlparse(maccrad_url_full)
24-
25-
26-
from urllib.request import urlopen
27-
response = urlopen(maccrad_url_full)
28-
response = response.decode('utf-8')
29-
30-
31-
32-
33-
req=urllib.request.urlopen(maccrad_url_full)
34-
charset=req.info().get_content_charset()
35-
content=req.read().decode(charset)
36-
37-
data = urllib.request.urlopen(maccrad_url_full).read()
38-
39-
lines = data.splitlines(True)
40-
# http://stackoverflow.com/questions/23131227/how-to-readlines-from-urllib
41-
42-
43-
44-
import requests
45-
46-
response = requests.get(maccrad_url_full).text
47-
48-
for line in response:
49-
# print (line)
50-
if line.startswith( "# Latitude"):
51-
# lat_line = line
52-
# lat = float(lat_line.split(':')[1])
53-
lat = float(line.split(':')[1])
54-
if line.startswith( "# Longitude"):
55-
# lon_line = line
56-
# lon = float(lon_line.split(':')[1])
57-
lon = float(line.split(':')[1])
58-
# if line.startswith( "# Altitude"):
59-
if "Altitude" in line:
60-
alt_line = line
61-
alt = float(alt_line.split(':')[1])
62-
# alt = float(line.split(':')[1])
63-
64-
65-
## if file is on local drive
66-
f = open(maccrad_csv_path)
67-
for line in f:
68-
if "Latitude" in line:
69-
# print (line)
13+
data_maccrad = read_maccrad(maccrad_csv_path, output='loc')
14+
15+
maccrad_loc = data_maccrad[0]
16+
maccrad_df = data_maccrad[1]
17+
18+
def test_location_coord():
19+
assert (44.0830, 5.0590, 97.00) == (maccrad_loc.latitude, maccrad_loc.longitude,
20+
maccrad_loc.altitude)
21+
22+
23+
def test_location_tz():
24+
assert 'Europe/Paris' == maccrad_loc.tz
25+
26+
27+
def test_maccrad_recolumn():
28+
assert 'Clear sky GHI' in maccrad_df.columns
29+
30+
def test_maccrad_norecolumn():
31+
assert 'Clear sky GHI' in maccrad_df.columns
32+
33+
def test_maccrad_coerce_year():
34+
coerce_year = 2010
35+
assert (maccrad_df.index.year[0] == coerce_year)
36+
37+
38+
def test_maccrad():
39+
read_maccrad(maccrad_csv_path)
40+
41+
##FIXME: this still crashes
42+
### if data is on remotely on github
43+
#
44+
#
45+
#import urllib
46+
#
47+
##f = urllib.request.urlopen(maccrad_url_full)
48+
#
49+
##from urllib.parse import urlparse
50+
##response = urlparse(maccrad_url_full)
51+
#
52+
#
53+
##from urllib.request import urlopen
54+
##response = urlopen(maccrad_url_full)
55+
##response = response.decode('utf-8')
56+
#
57+
#
58+
#
59+
## http://stackoverflow.com/questions/4981977/how-to-handle-response-encoding-from-urllib-request-urlopen
60+
#req=urllib.request.urlopen(maccrad_url_full)
61+
#charset=req.info().get_content_charset()
62+
#response=req.read().decode(charset)
63+
#
64+
##data = urllib.request.urlopen(maccrad_url_full).read()
65+
#
66+
#lines = response.splitlines(True)
67+
## http://stackoverflow.com/questions/23131227/how-to-readlines-from-urllib
68+
#
69+
#
70+
#
71+
##import requests
72+
##response = requests.get(maccrad_url_full).text
73+
#
74+
#for line in response:
75+
## print (line)
7076
# if line.startswith( "# Latitude"):
71-
lat_line = line
72-
lat = float(lat_line.split(':')[1])
77+
## lat_line = line
78+
## lat = float(lat_line.split(':')[1])
7379
# lat = float(line.split(':')[1])
74-
if "Longitude" in line:
7580
# if line.startswith( "# Longitude"):
76-
lon_line = line
77-
lon = float(lon_line.split(':')[1])
78-
lon = float(line.split(':')[1])
81+
## lon_line = line
82+
## lon = float(lon_line.split(':')[1])
83+
# lon = float(line.split(':')[1])
7984
# if line.startswith( "# Altitude"):
80-
if "Altitude" in line:
81-
alt_line = line
82-
alt = float(alt_line.split(':')[1])
83-
# alt = float(line.split(':')[1])
84-
85-
86-
from timezonefinder import TimezoneFinder
87-
tf = TimezoneFinder()
88-
tz = tf.timezone_at(lng=lon, lat=lat)
85+
## if "Altitude" in line:
86+
# alt_line = line
87+
# alt = float(alt_line.split(':')[1])
88+
## alt = float(line.split(':')[1])
8989

90-
from pvlib.location import Location
9190

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

0 commit comments

Comments
 (0)