<s> """<STR_LIT>""" <EOL> import logging <EOL> pvl_logger = logging . getLogger ( '<STR_LIT>' ) <EOL> import re <EOL> import datetime <EOL> import dateutil <EOL> import io <EOL> try : <EOL> from urllib2 import urlopen <EOL> except ImportError : <EOL> from urllib . request import urlopen <EOL> import pandas as pd <EOL> import numpy as np <EOL> from pvlib import tools <EOL> def readtmy3 ( filename = None , coerce_year = None , recolumn = True ) : <EOL> '''<STR_LIT>''' <EOL> if filename is None : <EOL> try : <EOL> filename = _interactive_load ( ) <EOL> except : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> head = [ '<STR_LIT>' , '<STR_LIT:Name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> try : <EOL> csvdata = open ( filename , '<STR_LIT:r>' ) <EOL> except IOError : <EOL> response = urlopen ( filename ) <EOL> csvdata = io . StringIO ( response . read ( ) . decode ( errors = '<STR_LIT:ignore>' ) ) <EOL> meta = dict ( zip ( head , csvdata . readline ( ) . rstrip ( '<STR_LIT:\n>' ) . split ( "<STR_LIT:U+002C>" ) ) ) <EOL> meta [ '<STR_LIT>' ] = float ( meta [ '<STR_LIT>' ] ) <EOL> meta [ '<STR_LIT>' ] = float ( meta [ '<STR_LIT>' ] ) <EOL> meta [ '<STR_LIT>' ] = float ( meta [ '<STR_LIT>' ] ) <EOL> meta [ '<STR_LIT>' ] = float ( meta [ '<STR_LIT>' ] ) <EOL> meta [ '<STR_LIT>' ] = int ( meta [ '<STR_LIT>' ] ) <EOL> TMYData = pd . read_csv ( filename , header = <NUM_LIT:1> , <EOL> parse_dates = { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] } , <EOL> date_parser = lambda * x : _parsedate ( * x , year = coerce_year ) , <EOL> index_col = '<STR_LIT>' ) <EOL> if recolumn : <EOL> _recolumn ( TMYData ) <EOL> TMYData = TMYData . tz_localize ( int ( meta [ '<STR_LIT>' ] * <NUM_LIT> ) ) <EOL> return TMYData , meta <EOL> def _interactive_load ( ) : <EOL> import Tkinter <EOL> from tkFileDialog import askopenfilename <EOL> Tkinter . Tk ( ) . withdraw ( ) <EOL> return askopenfilename ( ) <EOL> def _parsedate ( ymd , hour , year = None ) : <EOL> offset_hour = int ( hour [ : <NUM_LIT:2> ] ) - <NUM_LIT:1> <EOL> offset_datetime = '<STR_LIT>' . format ( ymd , offset_hour ) <EOL> offset_date = dateutil . parser . parse ( offset_datetime ) <EOL> true_date = offset_date + dateutil . relativedelta . relativedelta ( hours = <NUM_LIT:1> ) <EOL> if year is not None : <EOL> true_date = true_date . replace ( year = year ) <EOL> return true_date <EOL> def _recolumn ( tmy3_dataframe , inplace = True ) : <EOL> """<STR_LIT>""" <EOL> raw_columns = '<STR_LIT>' <EOL> new_columns = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> mapping = dict ( zip ( raw_columns . split ( '<STR_LIT:U+002C>' ) , new_columns ) ) <EOL> return tmy3_dataframe . rename ( columns = mapping , inplace = True ) <EOL> def readtmy2 ( filename ) : <EOL> '''<STR_LIT>''' <EOL> if filename is None : <EOL> try : <EOL> filename = _interactive_load ( ) <EOL> except : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> string = '<STR_LIT>' <EOL> columns = '<STR_LIT>' <EOL> hdr_columns = '<STR_LIT>' <EOL> TMY2 , TMY2_meta = _readTMY2 ( string , columns , hdr_columns , filename ) <EOL> return TMY2 , TMY2_meta <EOL> def _parsemeta_tmy2 ( columns , line ) : <EOL> """<STR_LIT>""" <EOL> rawmeta = "<STR_LIT:U+0020>" . join ( line . split ( ) ) . split ( "<STR_LIT:U+0020>" ) <EOL> meta = rawmeta [ : <NUM_LIT:3> ] <EOL> meta . append ( int ( rawmeta [ <NUM_LIT:3> ] ) ) <EOL> longitude = ( float ( rawmeta [ <NUM_LIT:5> ] ) + float ( rawmeta [ <NUM_LIT:6> ] ) / <NUM_LIT> ) * ( <NUM_LIT:2> * ( rawmeta [ <NUM_LIT:4> ] == '<STR_LIT:N>' ) - <NUM_LIT:1> ) <EOL> latitude = ( float ( rawmeta [ <NUM_LIT:8> ] ) + float ( rawmeta [ <NUM_LIT:9> ] ) / <NUM_LIT> ) * ( <NUM_LIT:2> * ( rawmeta [ <NUM_LIT:7> ] == '<STR_LIT:E>' ) - <NUM_LIT:1> ) <EOL> meta . append ( longitude ) <EOL> meta . append ( latitude ) <EOL> meta . append ( float ( rawmeta [ <NUM_LIT:10> ] ) ) <EOL> meta_dict = dict ( zip ( columns . split ( '<STR_LIT:U+002C>' ) , meta ) ) <EOL> pvl_logger . debug ( '<STR_LIT>' , meta_dict ) <EOL> return meta_dict <EOL> def _readTMY2 ( string , columns , hdr_columns , fname ) : <EOL> head = <NUM_LIT:1> <EOL> date = [ ] <EOL> with open ( fname ) as infile : <EOL> fline = <NUM_LIT:0> <EOL> for line in infile : <EOL> if head != <NUM_LIT:0> : <EOL> meta = _parsemeta_tmy2 ( hdr_columns , line ) <EOL> head -= <NUM_LIT:1> <EOL> continue <EOL> cursor = <NUM_LIT:1> <EOL> part = [ ] <EOL> for marker in string . split ( '<STR_LIT:%>' ) : <EOL> if marker == '<STR_LIT>' : <EOL> continue <EOL> increment = int ( re . findall ( '<STR_LIT>' , marker ) [ <NUM_LIT:0> ] ) <EOL> val = ( line [ cursor : cursor + increment ] ) <EOL> cursor = cursor + increment <EOL> if marker [ - <NUM_LIT:1> ] == '<STR_LIT:d>' : <EOL> try : <EOL> val = float ( val ) <EOL> except : <EOL> raise Exception ( '<STR_LIT>' + __name__ + '<STR_LIT>' + val + '<STR_LIT>' ) <EOL> elif marker [ - <NUM_LIT:1> ] == '<STR_LIT:s>' : <EOL> try : <EOL> val = str ( val ) <EOL> except : <EOL> raise Exception ( '<STR_LIT>' + __name__ + '<STR_LIT>' + val + '<STR_LIT>' ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + __name__ + '<STR_LIT>' + marker + '<STR_LIT>' ) <EOL> part . append ( val ) <EOL> if fline == <NUM_LIT:0> : <EOL> axes = [ part ] <EOL> year = part [ <NUM_LIT:0> ] + <NUM_LIT> <EOL> fline = <NUM_LIT:1> <EOL> else : <EOL> axes . append ( part ) <EOL> date . append ( datetime . datetime ( year = int ( year ) , month = int ( part [ <NUM_LIT:1> ] ) , day = int ( part [ <NUM_LIT:2> ] ) , hour = int ( part [ <NUM_LIT:3> ] ) - <NUM_LIT:1> ) ) <EOL> TMYData = pd . DataFrame ( axes , index = date , columns = columns . split ( '<STR_LIT:U+002C>' ) ) . tz_localize ( int ( meta [ '<STR_LIT>' ] * <NUM_LIT> ) ) <EOL> return TMYData , meta </s>
