Created
December 9, 2009 00:44
-
-
Save pifantastic/252137 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import re | |
class Station: | |
def __init__(self, file): | |
file = open(file, 'r') | |
self.data = { "obs": {} } | |
for line in file.readlines(): | |
if "=" in line: | |
keyValue = line.split("=") | |
self.data[keyValue[0].strip().lower().replace(' ', '_')] = keyValue[1].strip() | |
elif "Obs:" not in line: | |
s = re.split("\s+", line.strip()) | |
self.data["obs"][s[0]] = s[1:] | |
if "normals" in self.data: | |
self.data["normals"] = [normal for normal in re.split("\s+", self.data["normals"].strip())] | |
if "standard_deviations" in self.data: | |
self.data["standard_deviations"] = [sd for sd in re.split("\s+", self.data["standard_deviations"].strip())] | |
def __getattr__(self, name): | |
if name in self.data: | |
return self.data[name] | |
else: | |
return "" | |
if __name__ == "__main__": | |
MET_DATA_DIR = "All" | |
stations = [] | |
for dir in os.listdir(MET_DATA_DIR): | |
for file in os.listdir(os.path.join(MET_DATA_DIR, dir)): | |
stations.append(Station(os.path.join(MET_DATA_DIR, dir, file))) | |
# stations is now an array of Station objects | |
# here is what you can do with a station object: | |
station = stations[0] | |
print station.number | |
print station.name | |
print station.country | |
print station.lat | |
print station.long | |
print station.height | |
print station.start_year | |
print station.end_year | |
print station.first_good_year | |
print station.source_id | |
print station.source_file | |
print station.jones_data_to | |
print station.normals_source | |
print station.normals_source_start_year | |
print station.normals_source_end_year | |
print station.normals | |
print station.standard_deviations_source | |
print station.standard_deviations_source_start_year | |
print station.standard_deviations_source_end_year | |
print station.standard_deviations | |
print station.obs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment