def parse_station(self, metadata, data): """ Creates a Station object from metadata and data for the station - metadata: NerrStation object - data: list of NerrData objects returns a Station object """ if not isinstance(metadata, NerrStation) or not isinstance(data, list) or len(data) < 1: return None retval = Station() retval.uid = metadata.code retval.name = metadata.name retval.description = str('%s-%s' % (metadata.id,metadata.code)) point_dict = dict() for d in data: for value_date in d.value_and_utc(): if value_date[0] is not None: param = d.get_top_param() if value_date[1] not in point_dict: point_dict[value_date[1]] = Point() point_dict[value_date[1]].time = value_date[1] if param.upper() == 'DEPTH': point_dict[value_date[1]].location = Location(metadata.location.longitude, metadata.location.latitude, float(value_date[0])) else: point_dict[value_date[1]].add_member(dict(name=param,value=value_date[0],unit=unit(param))) for point in point_dict.values(): retval.add_element(point) retval.set_location(Location(metadata.location.longitude, metadata.location.latitude)) return retval
def test_trajectory(self): dt1 = datetime(2012, 4, 1, 0) p1 = Point() p1.time = dt1 p1.location = sPoint(-121, 49, 40) member1 = Member(value=random.uniform(30,40), unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member2 = Member(value=random.uniform(80,100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) dt2 = datetime(2012, 4, 1, 1) p2 = Point() p2.time = dt2 p2.location = sPoint(-120, 50, 60) member3 = Member(value=random.uniform(30,40), unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') member4 = Member(value=random.uniform(80,100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p2.add_member(member3) p2.add_member(member4) tr = Trajectory(elements=[p1,p2]) tr.calculate_bounds() assert len(tr.get_path()) == 2 assert tr.size == 2 assert tr.type == "Trajectory" assert tr.time_range[0] == dt1 assert tr.time_range[-1] == dt2 assert tr.depth_range[0] == p1.location.z assert tr.depth_range[-1] == p2.location.z assert tr.upper_right().equals(sPoint(p2.location.x, p2.location.y)) assert tr.lower_left().equals(sPoint(p1.location.x, p1.location.y))
def get_pwqmn(station_id, **kwargs): conn = sqlite3.connect(app.config.get("DATABASE")) with conn: conn.row_factory = sqlite3.Row cur = conn.cursor() cur.execute("SELECT * FROM stations WHERE STATION='%s'" % station_id) row = cur.fetchone() if row is not None: # Serve out OME s = pStation() s.uid = row["STATION"] s.name = row["NAME"] s.description = row["LOCATION"] s.location = sPoint(row["Longitude"], row["Latitude"], 0) s.set_property("country", "CA") s.set_property("organization_name", "Ontario Ministry of the Environment") s.set_property("organization_id", "ENE") filters = [] starting = kwargs.get("starting", None) ending = kwargs.get("ending", None) obs = kwargs.get("observedProperties", None) if starting is not None: filters.append("AND DATE > %s" % starting.strftime("%Y-%m-%dT%H:%M:%S")) if ending is not None: filters.append("AND DATE < %s" % ending.strftime("%Y-%m-%dT%H:%M:%S")) if obs is not None: obs = map(lambda x: "'%s'" % x, obs) filters.append("AND PARM in (%s)" % ",".join(obs)) cur.execute("SELECT * FROM data WHERE STATION='%s' %s ORDER BY DATE ASC" % (station_id, " ".join(filters))) rows = cur.fetchall() for d, members in itertools.groupby(rows, key=lambda s: s[3]): p = Point() p.time = datetime.strptime(d, "%Y-%m-%dT%H:%M:%S").replace(tzinfo=pytz.utc) for m in members: p.add_member( Member( value=m["RESULT"], unit=m["UNITS"], name=m["PARM"], description=m["PARM_DESCRIPTION"], standard=None, method_id=m["METHOD"], method_name=m["METHOD"], ) ) s.add_element(p) s.calculate_bounds() publisher = { "name": "Ontario Ministry of the Environment", "url": "http://www.ene.gov.on.ca/environment/en/resources/collection/data_downloads/index.htm#PWQMN", } return s, publisher return None, None
def test_section_collection(self): s_collection = SectionCollection() # 20 sections for x in xrange(0,20): day = 1 hour = 0 sc = Section() dt = None # 10 profiles per section for x in xrange(0,10): lat = random.randint(40,44) lon = random.randint(-74,-70) loc = sPoint(lon,lat,0) minute = 0 dt = datetime(2012, 4, day, hour, minute) hour += 1 prof = Profile() prof.location = loc prof.time = dt # Each with 20 depths for y in xrange(0,20): p = Point() p.time = dt p.location = sPoint(loc.x, loc.y, y) m1 = Member(value=random.uniform(30,40), unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') m2 = Member(value=random.uniform(80,100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p.add_member(m1) p.add_member(m2) prof.add_element(p) # Next depth is 2 minutes from now dt = dt + timedelta(minutes=2) sc.add_element(prof) s_collection.add_element(sc) s_collection.calculate_bounds() assert s_collection.depth_range[0] == 0 assert s_collection.depth_range[-1] == 19 assert s_collection.time_range[0] == datetime(2012, 4, 1, 0) for section in s_collection: assert section.type == "Section" for profile in section: assert profile.type == "Profile" for point in profile: assert point.type == "Point" for point in s_collection.flatten(): assert point.type == "Point"
def __init__(self, **kwargs): super(SweTimeSeries,self).__init__(**kwargs) # Parse out GML point. Defaults to 0 depth if none specified self.geo_srs = Crs(testXMLAttribute(self._location.find(nsp("Point", self.GML_NS)), 'srsName')) geo = [float(v) for v in testXMLValue(self._location.find(nsp("Point/pos", self.GML_NS))).split(" ")] if self.geo_srs.axisorder == "yx": self.geo = sPoint(geo[1], geo[0]) else: self.geo = sPoint(geo[0], geo[1]) try: self.geo.z = geo[2] except: pass pc = PointCollection() for row in self.results.data: p = Point() time = None z = None lat = None lon = None for field in row: if field.axis == "time": t = dateutil.parser.parse(field.value) p.time = t.astimezone(dateutil.tz.tzutc()) elif field.axis == "Long": lon = field elif field.axis == "Lat": lat = field elif field.axis == "h": z = field else: m = Member(value=field.value, unit=field.units, units_definition=field.units_url, name=field.name, definition=field.definition, standard=field.definition) p.add_member(m) # Set the spatial point if lon.srs != lat.srs: raise ValueError("Longitude and Latitude need to have the same SRS/CRS!") p.location = sPoint(float(lon.value), float(lat.value), float(z.value)) pc.add_element(p) self.data = pc
def test_point(self): dt = datetime.utcnow() p = Point() p.location = sPoint(-123.17, 48.33, 10) p.time = dt assert p.location.x == -123.17 assert p.location.y == 48.33 assert p.location.z == 10 assert p.time == dt assert p.type == "Point"
def test_profile_collection(self): day = 1 pc = ProfileCollection() dt = None # 10 profiles for x in xrange(0,10): lat = random.randint(40,44) lon = random.randint(-74,-70) loc = sPoint(lon,lat,0) hour = 0 minute = 0 dt = datetime(2012, 4, day, hour, minute) prof = Profile() prof.location = loc prof.time = dt # Each with 20 depths for y in xrange(0,20): p = Point() p.time = dt p.location = sPoint(loc.x, loc.y, y) m1 = Member(value=random.uniform(30,40), unit='°C', name='Water Temperatire', description='water temperature', standard='sea_water_temperature') m2 = Member(value=random.uniform(80,100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p.add_member(m1) p.add_member(m2) prof.add_element(p) # Next depth is 2 minutes from now dt = dt + timedelta(minutes=2) pc.add_element(prof) pc.calculate_bounds() assert pc.size == 10 assert pc.point_size == 200 assert len(pc.time_range) == 200 assert pc.time_range[0] == datetime(2012, 4, 1, 0, 0) assert pc.time_range[-1] == dt - timedelta(minutes=2) assert len(pc.depth_range) == 200 assert pc.depth_range[0] == 0 assert pc.depth_range[-1] == 19 for profile in pc: assert profile.type == "Profile" for point in profile: assert point.type == "Point" for point in pc.flatten(): assert point.type == "Point"
def test_trajectory_collection(self): t_collection = TrajectoryCollection() # 20 trajectories for x in xrange(0,20): tr = Trajectory() month = random.randint(1,12) dt = datetime(2012, month, 1, 0) # Starting point lat = random.randint(40,44) lon = random.randint(-74,-70) depth = 0 # 100 points in each trajectory for l in xrange(0,100): lat += random.uniform(-0.25,0.25) lon += random.uniform(-0.25,0.25) depth += random.randint(-4,4) if depth < 0: depth = 0 p1 = Point() p1.location = sPoint(lon,lat,depth) dt += timedelta(hours=1) p1.time = dt member1 = Member(value=random.uniform(30,40), unit='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') member2 = Member(value=random.uniform(80,100), unit='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) tr.add_element(p1) t_collection.add_element(tr) t_collection.calculate_bounds() assert len(t_collection.time_range) == 2000 assert len(t_collection.depth_range) == 2000 for trajectory in t_collection: assert trajectory.type == "Trajectory" for point in trajectory: assert point.type == "Point" for point in t_collection.flatten(): assert point.type == "Point"
def __init__(self, wqx_metadata, wqx_data): if not isinstance(wqx_metadata, WqxOutbound): wqx_metadata = WqxOutbound(wqx_metadata) if not isinstance(wqx_data, WqxOutbound): wqx_data = WqxOutbound(wqx_data) if wqx_data.failed or wqx_metadata.failed: self.feature = None else: s = Station() s.uid = wqx_metadata.location.id s.name = wqx_metadata.location.name s.set_property("station_type", wqx_metadata.location.type) s.set_property("location_description", wqx_metadata.location.description) s.set_property("huc", wqx_metadata.location.huc) s.set_property("county", wqx_metadata.location.county) s.set_property("state", wqx_metadata.location.state) s.set_property("country", wqx_metadata.location.country) s.set_property("organization_id", wqx_metadata.organization.id) s.set_property("organization_name", wqx_metadata.organization.name) s.set_property("vertical_units", wqx_metadata.location.vertical_measure_units) s.set_property("horizontal_crs", wqx_metadata.location.horizontal_crs_name) s.set_property("vertical_crs", wqx_metadata.location.vertical_crs_name) for a in wqx_data.activities: p = Point() p.time = a.start_time for r in a.results: p.add_member(Member(value=r.value, unit=r.units, name=r.name, description=r.short_name, standard=None, quality=r.quality, method_id=a.method_id, method_name=a.method_name)) s.add_element(p) # Now set the station's location vertical = 0 try: vertical = float(wqx_metadata.location.vertical_measure_value) except: pass # convert the vertical to meters if it is ft (which it always is) if wqx_metadata.location.vertical_measure_units == "ft": vertical /= 3.28084 s.set_property("vertical_units", "m") s.location = sPoint(float(wqx_metadata.location.longitude), float(wqx_metadata.location.latitude), vertical) self.feature = s
def test_set_get_member(self): p = Point() member = Member(value=5.4, unit='m', name='Sea Surface Height', description='a description', standard='sea_surface_height') p.add_member(member) assert member == p.get_member(name='Sea Surface Height')
def test_profile(self): dt1 = datetime(2012, 1, 1, 12, 0) p1 = Point() p1.time = dt1 p1.location = sPoint(-120, 50, 0) member1 = Member( value=34.7, unit="°C", name="Water Temperatire", description="water temperature", standard="sea_water_temperature", ) member2 = Member(value=80, unit="PSU", name="Salinity", description="salinity", standard="salinity") p1.add_member(member1) p1.add_member(member2) dt2 = datetime(2012, 1, 1, 12, 10) p2 = Point() p2.time = dt2 p2.location = sPoint(-120, 50, 10) member3 = Member( value=34.1, unit="°C", name="Water Temperatire", description="water temperature", standard="sea_water_temperature", ) member4 = Member(value=70, unit="PSU", name="Salinity", description="salinity", standard="salinity") p2.add_member(member3) p2.add_member(member4) dt3 = datetime(2012, 1, 1, 12, 20) p3 = Point() p3.time = dt3 p3.location = sPoint(-120, 50, 20) member5 = Member( value=32.6, unit="°C", name="Water Temperatire", description="water temperature", standard="sea_water_temperature", ) member6 = Member(value=60, unit="PSU", name="Salinity", description="salinity", standard="salinity") p3.add_member(member5) p3.add_member(member6) pc = Profile(elements=[p1, p2, p3]) pc.location = sPoint(-120, 50) pc.time = dt1 pc.calculate_bounds() assert pc.size == 3 assert pc.time == dt1 assert len(pc.time_range) == 3 assert pc.time_range[0] == dt1 assert pc.time_range[-1] == dt3 assert len(pc.depth_range) == 3 assert pc.depth_range[0] == p1.location.z assert pc.depth_range[-1] == p3.location.z assert pc.upper_right().equals(pc.location) assert pc.lower_left().equals(pc.location)
def test_station(self): dt1 = datetime(2012, 1, 1, 12, 0) p1 = Point() p1.time = dt1 member1 = Member(value=34.7, unit='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') member2 = Member(value=80, unit='PSU', name='Salinity', description='salinity', standard='salinity') p1.add_member(member1) p1.add_member(member2) dt2 = datetime(2012, 1, 1, 12, 10) p2 = Point() p2.time = dt2 member3 = Member(value=34.1, unit='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') member4 = Member(value=70, unit='PSU', name='Salinity', description='salinity', standard='salinity') p2.add_member(member3) p2.add_member(member4) dt3 = datetime(2012, 1, 1, 12, 20) p3 = Point() p3.time = dt3 member5 = Member(value=32.6, unit='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature') member6 = Member(value=60, unit='PSU', name='Salinity', description='salinity', standard='salinity') member6 = Member(value=112, unit='%', name='DO', description='do', standard='do') p3.add_member(member5) p3.add_member(member6) pc = Station(elements=[p1,p2,p3]) pc.name = "Super Station" pc.location = sPoint(-120, 50, 0) pc.location_name = "Just south of the super pier" pc.uid = "123097SDFJL2" pc.set_property("authority", "IOOS") pc.calculate_bounds() assert pc.size == 3 assert len(pc.time_range) == 3 assert pc.time_range[0] == dt1 assert pc.time_range[-1] == dt3 assert len(pc.depth_range) == 3 assert pc.depth_range[0] == p1.location.z assert pc.upper_right().equals(pc.location) assert pc.lower_left().equals(pc.location) assert pc.get_property("authority") == "IOOS" assert pc.uid == "123097SDFJL2" assert pc.location_name == "Just south of the super pier" assert len(pc.get_unique_members()) == 3 filtered_elements = pc.filter_by_time(starting=dt1, ending=dt2) assert len(list(filtered_elements)) == 2 filtered_variables = pc.filter_by_variable("sea_water_temperature") assert len(list(filtered_variables)) == 3 filtered_variables = pc.filter_by_variable("do") assert len(list(filtered_variables)) == 1