-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18fa9db
commit 5382026
Showing
5 changed files
with
155 additions
and
28 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
from cht_observations.observation_stations import StationSource | ||
from NDBC.NDBC import DataBuoy | ||
|
||
from cht_observations import utils | ||
|
||
class Source(StationSource): | ||
def __init__(self): | ||
self.db = DataBuoy() | ||
|
||
def list_stations(self): | ||
pass | ||
def get_active_stations(self): | ||
url = "https://www.ndbc.noaa.gov/activestations.xml" | ||
obj = utils.xml2obj(url) | ||
station_list = [] | ||
for station in obj.station: | ||
station_list.append({"name": station.name, | ||
"id": station.id, | ||
"lon": float(station.lon), | ||
"lat": float(station.lat)}) | ||
self.active_stations = station_list | ||
return station_list | ||
|
||
def get_meta_data(self, id): | ||
self.db.set_station_id(id) | ||
return getattr(self.db, "station_info", None) | ||
try: | ||
meta_data = self.db.station_info | ||
except: | ||
meta_data = None | ||
return meta_data | ||
|
||
def get_data(self, id, variable=None): | ||
pass |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from datetime import datetime | ||
|
||
import urllib | ||
import xml.etree.ElementTree as ET | ||
|
||
def xml2py(node): | ||
|
||
name = node.tag | ||
|
||
pytype = type(name, (object, ), {}) | ||
pyobj = pytype() | ||
|
||
for attr in node.attrib.keys(): | ||
setattr(pyobj, attr, node.get(attr)) | ||
|
||
if node.text and node.text.strip() != '' and node.text.strip() != '\n': | ||
setattr(pyobj, 'text', node.text) | ||
setattr(pyobj, 'value', node.text) | ||
# Convert | ||
if node.attrib: | ||
if "type" in node.attrib.keys(): | ||
if node.attrib["type"] == "float": | ||
lst = node.text.split(",") | ||
if len(lst)==1: | ||
pyobj.value = float(node.text) | ||
else: | ||
float_list = [float(s) for s in lst] | ||
pyobj.value = float_list | ||
elif node.attrib["type"] == "int": | ||
if "," in node.text: | ||
pyobj.value = [int(s) for s in node.text.split(',')] | ||
else: | ||
pyobj.value = int(node.text) | ||
elif node.attrib["type"] == "datetime": | ||
pyobj.value = datetime.strptime(node.text, | ||
'%Y%m%d %H%M%S') | ||
|
||
for cn in node: | ||
if not hasattr(pyobj, cn.tag): | ||
setattr(pyobj, cn.tag, []) | ||
getattr(pyobj, cn.tag).append(xml2py(cn)) | ||
|
||
return pyobj | ||
|
||
def xml2obj(file_name): | ||
"""Converts an xml file to a python object. """ | ||
|
||
if file_name[0:4] == "http": | ||
with urllib.request.urlopen(file_name) as f: | ||
tree = ET.parse(f) | ||
xml_root = tree.getroot() | ||
else: | ||
xml_root = ET.parse(file_name).getroot() | ||
obj = xml2py(xml_root) | ||
|
||
return obj | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
Example: NOAA COOPS | ||
""" | ||
|
||
import datetime | ||
import cht_observations.observation_stations as obs | ||
|
||
coops = obs.source("noaa_coops") | ||
t0 = datetime.datetime(2015, 1, 1) | ||
t1 = datetime.datetime(2015, 1, 10) | ||
df = coops.get_data("9447130", t0, t1) |