Пример #1
0
    def test_point_collection(self):
        dt1 = datetime.date(2007, 12, 5)
        p1 = Point()
        p1.time = dt1
        p1.location = sPoint(-180, -90, 0)
        member1 = Member(value=5.4, unit='m', name='Sea Surface Height', description='sea height', standard='sea_surface_height')
        member2 = Member(value=8.1, unit='m', name='Wave Height', description='wave height', standard='wave_height')
        p1.add_member(member1)
        p1.add_member(member2)

        dt2 = datetime.date(2008, 2, 14)
        p2 = Point()
        p2.time = dt2
        p2.location = sPoint(-120, 50, 10)
        member3 = Member(value=5.4, unit='m', name='Sea Surface Height', description='sea height', standard='sea_surface_height')
        member4 = Member(value=8.1, unit='m', name='Wave Height', description='wave height', standard='wave_height')
        p2.add_member(member3)
        p2.add_member(member4)

        pc = PointCollection(elements=[p1,p2])
        pc.calculate_bounds()

        assert pc.size == 2
        assert pc.time_range[0] == dt1
        assert pc.time_range[-1] == dt2
        assert pc.depth_range[0] == p1.location.z
        assert pc.depth_range[-1] == p2.location.z
        assert pc.upper_right().equals(sPoint(p2.location.x, p2.location.y))
        assert pc.lower_left().equals(sPoint(p1.location.x, p1.location.y))
        
Пример #2
0
    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))
        
Пример #3
0
    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
Пример #4
0
	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
Пример #5
0
    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))
Пример #6
0
    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:
            stations = []
            station_lookup = []
            # Create a station for every MonitoringLocation
            for org in wqx_metadata.organizations:
                for ml in org.locations:
                    s = Station()
                    s.uid = ml.id
                    s.name = ml.name
                    s.set_property("station_type", ml.type)
                    s.set_property("location_description", ml.description)
                    s.set_property("huc", ml.huc)
                    s.set_property("county", ml.county)
                    s.set_property("state", ml.state)
                    s.set_property("country", ml.country)
                    s.set_property("organization_id", org.description.id)
                    s.set_property("organization_name", org.description.name)
                    s.set_property("vertical_units", ml.vertical_measure_units)
                    s.set_property("horizontal_crs", ml.horizontal_crs_name)
                    s.set_property("vertical_crs", ml.vertical_crs_name)

                    # Now set the station's location
                    vertical = 0
                    try:
                        vertical = float(ml.vertical_measure_value)
                    except:
                        pass

                    # convert the vertical to meters if it is ft (which it always is)
                    if ml.vertical_measure_units == "ft":
                        vertical /= 3.28084
                        s.set_property("vertical_units", "m")
                    s.location = sPoint(float(ml.longitude), float(ml.latitude), vertical)

                    stations.append(s)
                    station_lookup.append(s.uid)

            for org in wqx_data.organizations:
                for a in org.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))

                    # Assign data to the correct station pulled from the metadata
                    station = stations[station_lookup.index(a.location_id)]
                    p.location = station.location
                    station.add_element(p)

            self.feature = StationCollection(elements=stations)
Пример #7
0
    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:
            stations = []
            station_lookup = []
            # Create a station for every MonitoringLocation
            for org in wqx_metadata.organizations:
                for ml in org.locations:
                    s = Station()
                    s.uid = ml.id
                    s.name = ml.name
                    s.set_property("station_type", ml.type)
                    s.set_property("location_description", ml.description)
                    s.set_property("huc", ml.huc)
                    s.set_property("county", ml.county)
                    s.set_property("state", ml.state)
                    s.set_property("country", ml.country)
                    s.set_property("organization_id", org.description.id)
                    s.set_property("organization_name", org.description.name)
                    s.set_property("vertical_units", ml.vertical_measure_units)
                    s.set_property("horizontal_crs", ml.horizontal_crs_name)
                    s.set_property("vertical_crs", ml.vertical_crs_name)

                    # Now set the station's location
                    vertical = 0
                    try:
                        vertical = float(ml.vertical_measure_value)
                    except:
                        pass

                    # convert the vertical to meters if it is ft (which it always is)
                    if ml.vertical_measure_units == "ft":
                        vertical /= 3.28084
                        s.set_property("vertical_units", "m")
                    s.location = sPoint(float(ml.longitude), float(ml.latitude), vertical)

                    stations.append(s)
                    station_lookup.append(s.uid)

            for org in wqx_data.organizations:
                for a in org.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))

                    # Assign data to the correct station pulled from the metadata
                    station = stations[station_lookup.index(a.location_id)]
                    p.location = station.location
                    station.add_element(p)

            self.feature = StationCollection(elements=stations)
Пример #8
0
    def test_trajectory_collection(self):

        t_collection = TrajectoryCollection()

        # 20 trajectories
        for x in range(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 range(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"
Пример #9
0
    def __init__(self, response_list, nerrs_stations=None):
        assert isinstance(response_list, dict)

        if nerrs_stations is None:
            from pyoos.collectors.nerrs.nerrs_soap import NerrsSoap
            nerrs_stations = NerrsSoap().stations

        def get_station(feature):
            for s in nerrs_stations:
                if s['Station_Code'].lower() == feature.lower():
                    return s

        skip_tags = ["DateTimeStamp", "utcStamp", "data", "MaxWSpdT"]

        stations = []
        for feature, response in response_list.iteritems():
            if not isinstance(response, etree._Element):
                response = etree.fromstring(response)

            feature = get_station(feature)

            s = Station()
            s.uid = feature['Station_Code']
            s.name = feature['Station_Name']
            s.location = sPoint(float(feature['Longitude']),
                                float(feature['Latitude']), 0)
            s.set_property("state", feature['State'])
            s.set_property("siteid", feature['NERR_Site_ID'])
            s.set_property("horizontal_crs", "EPSG:4326")
            s.set_property("vertical_units", "m")
            s.set_property("vertical_crs", "EPSG:4297")
            s.set_property("location_description", feature['Reserve_Name'])

            for data in response.findall(".//data"):
                p = Point()
                t = AsaTime.parse(testXMLValue(data.find("utcStamp")))
                t = t.replace(tzinfo=pytz.utc)
                p.time = t
                p.location = s.location
                for child in data:
                    if child.tag not in skip_tags:
                        try:
                            val = float(child.text)
                            p.add_member(
                                Member(value=val,
                                       unit=units(child.tag),
                                       name=child.tag,
                                       description=child.tag,
                                       standard=standard(child.tag)))
                        except TypeError:
                            # Value was None
                            pass

                s.add_element(p)

            stations.append(s)

        self.feature = StationCollection(elements=stations)
Пример #10
0
    def test_section(self):

        day = 1
        hour = 0
        sc = Section()
        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)
            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)

        sc.calculate_bounds()

        assert len(sc.get_path()) == 10

        assert sc.size == 10
        assert sc.point_size == 200
        assert sc.type == "Section"

        assert len(sc.time_range) == 200
        assert sc.time_range[0] == datetime(2012, 4, 1, 0, 0)
        assert sc.time_range[-1] == dt - timedelta(minutes=2)

        assert len(sc.depth_range) == 200
        assert sc.depth_range[0] == 0
        assert sc.depth_range[-1] == 19
Пример #11
0
    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"
Пример #12
0
    def test_section_collection(self):

        s_collection = SectionCollection()

        # 20 sections
        for x in range(0,20):

            day = 1
            hour = 0
            sc = Section()
            dt = None

            # 10 profiles per section
            for x in range(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 range(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 Temperature', 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"
Пример #13
0
    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
Пример #14
0
    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)
Пример #15
0
    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 range(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 range(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 Temperature', 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"
Пример #17
0
    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"
Пример #18
0
    def __init__(self, response_list, nerrs_stations=None):
        assert isinstance(response_list, dict)

        if nerrs_stations is None:
            from pyoos.collectors.nerrs.nerrs_soap import NerrsSoap
            nerrs_stations = NerrsSoap().stations

        def get_station(feature):
            for s in nerrs_stations:
                if s['Station_Code'].lower() == feature.lower():
                    return s

        skip_tags = ["DateTimeStamp", "utcStamp", "data", "MaxWSpdT"]

        stations = []
        for feature, response in response_list.items():
            if not isinstance(response, etree._Element):
                response = etree.fromstring(response)

            feature = get_station(feature)

            s = Station()
            s.uid = feature['Station_Code']
            s.name = feature['Station_Name']
            s.location = sPoint(feature['Longitude'], feature['Latitude'], 0)
            s.set_property("state", feature['State'])
            s.set_property("siteid", feature['NERR_Site_ID'])
            s.set_property("horizontal_crs", "EPSG:4326")
            s.set_property("vertical_units", "m")
            s.set_property("vertical_crs", "EPSG:4297")
            s.set_property("location_description", feature['Reserve_Name'])

            for data in response.findall(".//data"):
                p = Point()
                t = AsaTime.parse(testXMLValue(data.find("utcStamp")))
                t = t.replace(tzinfo=pytz.utc)
                p.time = t
                p.location = s.location
                for child in data:
                    if child.tag not in skip_tags:
                        try:
                            val = float(child.text)
                            p.add_member(Member(value=val, name=child.tag, description=child.tag,
                                                unit=units(child.tag), standard=standard(child.tag)))
                        except TypeError:
                            # Value was None
                            pass

                s.add_element(p)

            stations.append(s)

        self.feature = StationCollection(elements=stations)
Пример #19
0
    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"
Пример #20
0
    def test_station_profile(self):

        sp = StationProfile()
        sp.name = "Profile Station"
        sp.location = sPoint(-77, 33)
        sp.uid = "1234"
        sp.set_property("authority", "IOOS")

        # add a sequence of profiles
        for y in range(3):
            dt1 = datetime(2013, 1, 1, 12, 0, 10 * y)
            prof1 = Profile()
            prof1.location = sPoint(-77, 33)
            prof1.time = dt1

            # add a string of points going down in z
            for x in range(5):
                p1 = Point()
                p1.time = dt1
                p1.location = sPoint(-77, 33, -5 * x)

                member1 = Member(value=30 - (2 * x),
                                 units='°C',
                                 name='Water Temperature',
                                 description='water temperature',
                                 standard='sea_water_temperature')
                member2 = Member(value=80 + (2 * x),
                                 units='PSU',
                                 name='Salinity',
                                 description='salinity',
                                 standard='salinity')
                p1.add_member(member1)
                p1.add_member(member2)

                prof1.add_element(p1)

            sp.add_element(prof1)

        sp.calculate_bounds()

        assert sp.size == 3
        assert len(sp.time_range) == 3

        assert sp.depth_range[0] == -20
        assert sp.depth_range[-1] == 0

        assert sp.get_property("authority") == "IOOS"
        assert sp.uid == "1234"

        assert len(sp.get_unique_members()) == 2
Пример #21
0
 def _get_point(self, profile, point):
     """
     Finds the given point in the profile, or adds it in sorted z order.
     """
     cur_points_z = [p.location.z for p in profile.elements]
     try:
         cur_idx = cur_points_z.index(point.z)
         return profile.elements[cur_idx]
     except ValueError:
         new_idx            = bisect_left(cur_points_z, point.z)
         new_point          = Point()
         new_point.location = sPoint(point)
         new_point.time     = profile.time
         profile.elements.insert(new_idx, new_point)
         return new_point
Пример #22
0
    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
Пример #23
0
    def test_station_profile(self):

        sp = StationProfile()
        sp.name = "Profile Station"
        sp.location = sPoint(-77, 33)
        sp.uid = "1234"
        sp.set_property("authority", "IOOS")

        # add a sequence of profiles
        for y in xrange(3):
            dt1 = datetime(2013, 1, 1, 12, 0, 10 * y)
            prof1          = Profile()
            prof1.location = sPoint(-77, 33)
            prof1.time     = dt1

            # add a string of points going down in z
            for x in xrange(5):
                p1 = Point()
                p1.time = dt1
                p1.location = sPoint(-77, 33, -5 * x)

                member1 = Member(value=30 - (2 * x), units='°C', name='Water Temperature', description='water temperature', standard='sea_water_temperature')
                member2 = Member(value=80 + (2 * x), units='PSU', name='Salinity', description='salinity', standard='salinity')
                p1.add_member(member1)
                p1.add_member(member2)

                prof1.add_element(p1)

            sp.add_element(prof1)

        sp.calculate_bounds()

        assert sp.size == 3
        assert len(sp.time_range) == 3

        assert sp.depth_range[0] == -20
        assert sp.depth_range[-1] == 0

        assert sp.get_property("authority") == "IOOS"
        assert sp.uid == "1234"

        assert len(sp.get_unique_members()) == 2
Пример #24
0
    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
Пример #25
0
    def __init__(self, waterml_data):
        
        if isinstance(waterml_data, str) or isinstance(waterml_data, unicode):
            try:
                self._root = etree.fromstring(str(waterml_data))
            except ValueError:
                # Strip out the XML header due to UTF8 encoding declaration
                self._root = etree.fromstring(waterml_data[56:])
        else:
            self._root = waterml_data

        response = WaterML_1_1(self._root).response

        stations = []
        station_lookup = []

        for timeseries in response.time_series:
            station_code = sorted(timeseries.source_info.site_codes)[0]
            # Create station if we have not seen it
            if station_code not in station_lookup:
                s = Station()
                s.uid = station_code

                info = timeseries.source_info
                s.name = info.site_name
                s.set_property("station_type", info.site_types)
                s.set_property("huc", info.site_properties.get("hucCd"))
                s.set_property("county", info.site_properties.get("countyCd"))
                s.set_property("state", info.site_properties.get("stateCd"))

                # Now set the station's location
                vertical = info.elevation
                if vertical is None:
                    vertical = 0

                try:
                    location = info.location.geo_coords[0]
                    srs = info.location.srs[0]
                except:
                    print "Could not find a location for %s... skipping station" % s.uid
                    continue

                s.location = sPoint(float(location[0]), float(location[1]), vertical)
                s.set_property("horizontal_crs", srs)
                s.set_property("vertical_units", "m")
                s.set_property("vertical_crs", info.vertical_datum)
                s.set_property("location_description", info.location.notes)

                stations.append(s)
                station_lookup.append(s.uid)


            times = {}
            variable = timeseries.variable
            for variable_timeseries in timeseries.values:
                for r in variable_timeseries:
                    dt = r.date_time
                    if dt.tzinfo is None:
                        dt = dt.replace(tzinfo=pytz.utc)
                    dt = dt.astimezone(pytz.utc)
                    
                    if dt not in times.keys():
                        times[dt] = []

                    times[dt].append(Member(value=r.value, unit=variable.unit.code, name=variable.variable_name, description=variable.variable_description, standard=variable.variable_code))

            station = stations[station_lookup.index(station_code)]
            for dts,members in times.iteritems():
                p = Point()
                p.time = dts
                p.location = station.location
                p.members = members
                station.add_element(p)
        
        self.feature = StationCollection(elements=stations)
Пример #26
0
    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
Пример #27
0
    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
Пример #28
0
    def __init__(self, element):

        record = DataRecord(element)

        # Top level org structure
        stations_field = record.get_by_name("stations")
        stations = {}
        sensors = {}
        for station in stations_field.content.field:
            s = Station()
            s.name  = station.name
            s.uid   = station.content.get_by_name("stationID").content.value

            # Location
            vector  = station.content.get_by_name("platformLocation").content
            srss = vector.referenceFrame.split("&amp;")
            hsrs = None
            try:
                hsrs = Crs(srss[0])
            except ValueError:
                pass

            vsrs = None
            try:
                vsrs = Crs(srss[-1].replace("2=http:", "http:"))
            except ValueError:
                pass

            s.set_property("horizontal_srs", hsrs)
            s.set_property("vertical_srs",   vsrs)
            s.set_property("localFrame",     vector.localFrame)

            lat = vector.get_by_name("latitude").content.value
            lon = vector.get_by_name("longitude").content.value
            z   = vector.get_by_name("height").content.value

            loc = [lon, lat]
            if z:
                loc.append(z)

            s.location = sPoint(*loc)

            # Sensors
            for sensor in station.content.get_by_name("sensors").content.field:
                name        = sensor.name
                uri         = sensor.content.get_by_name("sensorID").content.value
                height      = None
                location_quantity = sensor.content.get_by_name("height").content
                if location_quantity.referenceFrame == "#%s_frame" % s.name:
                    # Uses the station as reference frame
                    if location_quantity.value and z:
                        height      = z + location_quantity.value
                    horizontal_srs  = s.get_property("horizontal_srs")
                    vertical_srs    = s.get_property("vertical_srs")
                else:
                    # Uses its own height
                    if location_quantity.value:
                        height      = location_quantity.value
                    horizontal_srs  = None
                    vertical_srs    = None
                    if hasattr(sensor, 'referenceFrame'):
                        srss            = sensor.referenceFrame.split("&amp;")
                        try:
                            horizontal_srs = Crs(srss[0])
                        except ValueError:
                            pass
                        try:
                            vertical_srs = Crs(srss[-1].replace("2=http:", "http:"))
                        except ValueError:
                            pass

                loc = [s.location.x, s.location.y]
                if height:
                    loc.append(height)

                location            = sPoint(*loc)

                sensors[name] = {   'station'           : s.uid,
                                    'name'              : name,
                                    'uri'               : uri,
                                    'horizontal_srs'    : horizontal_srs,
                                    'vertical_srs'      : vertical_srs,
                                    'location'          : location,
                                    'columns'           : [], # Array of Members representing the columns
                                    'values'            : []  # Array of Points (the actual data)
                                }

            stations[s.uid] = s


        # Start building the column structure
        data_array = record.get_by_name("observationData").content
        data_record = data_array.elementType.content

        columns = []
        # Data outside of the <field name="sensors"> DataChoice element
        for f in data_record.field:
            columns.append(f)

        # Data inside of DataChoice
        sensor_data = data_record.get_by_name("sensor")
        for sendata in sensor_data.content.item:
            if sendata.content is not None:
                sensors[sendata.name]['columns'] = []
                sensors[sendata.name]['values'] = []
                for f in sendata.content.field:
                    # Create a model Member for each column that will be copied and filled with data from each row
                    sensors[sendata.name]['columns'].append(f)

        decimalSeparator    = data_array.encoding.decimalSeparator
        tokenSeparator      = data_array.encoding.tokenSeparator
        blockSeparator      = data_array.encoding.blockSeparator
        collapseWhiteSpaces = data_array.encoding.collapseWhiteSpaces

        data_values = data_array.values
        self.raw_data = copy(data_values)

        for row in filter(lambda x: x != "", data_values.split(blockSeparator)):

            pt = None
            members     = []
            values      = row.split(tokenSeparator)
            sensor_key  = None
            i           = 0

            for x in columns:

                if isinstance(x.content, Time) and x.content.definition == "http://www.opengis.net/def/property/OGC/0/SamplingTime":
                    pt      = Point()
                    pt.time = parser.parse(values[i])

                elif isinstance(x.content, DataChoice):
                    sensor_key = values[i]
                    dc_cols = sensors[sensor_key]['columns']

                    for j,c in enumerate(dc_cols):
                        if isinstance(c.content, AbstractSimpleComponent):
                            m = Member( units=c.content.uom,
                                        name=c.name,
                                        standard=c.content.definition,
                                        value=float(values[i+1]))
                            members.append(m)

                        elif isinstance(c.content, Time) and c.content.definition == "http://www.opengis.net/def/property/OGC/0/SamplingTime":
                            pt      = Point()
                            pt.time = parser.parse(v)

                        # For each data column
                        i += 1
                
                elif isinstance(x.content, AbstractSimpleComponent):
                    m = Member( units=x.content.uom,
                                name=x.name,
                                standard=x.content.definition,
                                value=float(values[i]))
                    members.append(m)

                else:
                    print "WHAT AM I"

                i += 1

            pt.members = members
            pt.location = stations[sensors[sensor_key]['station']].location
            sensors[sensor_key]['values'].append(pt)

        for k,v in stations.iteritems():
            for sk, sv in sensors.iteritems():
                # Match on station uid
                if sv['station'] == k:
                    v.elements = self._merge_points(v.elements or [], sv['values'])

        if len(stations) > 1:
            self.feature = StationCollection(elements=stations)
        elif len(stations) == 1:
            self.feature = next(stations.itervalues())
        else:
            print "No stations found!"
Пример #29
0
Файл: awc.py Проект: ioos/pyoos
    def __init__(self, awc_list):
        for awc_data in awc_list:
            try:
                self._root = etree.fromstring(awc_data.encode())
            except Exception as e:
                warnings.warn(
                    "Could not read from {!r}, got {}".format(awc_data, e)
                )
                continue

            """Code to get station iterator goes here."""
            stations = []
            station_lookup = []
            times = []

            for metar in self._root.iter("METAR"):
                uid = metar.find("station_id").text
                if uid not in station_lookup:
                    s = Station()
                    s.uid = uid
                    vertical = metar.find("elevation_m").text
                    if vertical is None:
                        vertical = 0
                    s.location = sPoint(
                        float(metar.find("latitude").text),
                        float(metar.find("longitude").text),
                        float(vertical),
                    )
                    s.set_property("metar_type", metar.find("metar_type").text)
                    s.set_property("horizontal_crs", "GCS")
                    s.set_property("vertical_units", "m")
                    s.set_property("vertical_crs", "AGL")

                    stations.append(s)
                    station_lookup.append(s.uid)
                    times.append({})
                else:
                    s = stations[station_lookup.index(uid)]

                variables = [
                    "elevation_m",
                    "raw_text",
                    "temp_c",
                    "dewpoint_c",
                    "wind_dir_degrees",
                    "wind_speed_kt",
                    "visibility_statute_mi",
                    "altim_in_hg",
                    "wx_string",
                    "sky_condition",
                    "flight_category",
                ]
                for variable in variables:
                    time_string = metar.find("observation_time").text
                    dt = datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%SZ")
                    if dt.tzinfo is None:
                        dt = dt.replace(tzinfo=pytz.utc)
                    dt = dt.astimezone(pytz.utc)

                    if dt not in list(
                        times[station_lookup.index(s.uid)].keys()
                    ):
                        times[station_lookup.index(s.uid)][dt] = []

                    if metar.find(variable):
                        if variable in {"raw_text", "flight_category", "wx_string"}:
                            times[station_lookup.index(s.uid)][dt].append(
                                Member(
                                    value=metar.find(variable).text,
                                    unit=None,
                                    name=variable,
                                    description=variable,
                                    standard=None,
                                )
                            )
                        elif variable == "sky_condition":
                            sky_condition = []
                            for cond in metar.findall(variable):
                                cover = cond.attrib["sky_cover"]
                                try:
                                    alt = cond.attrib["cloud_base_ft_agl"]
                                except Exception:
                                    alt = None
                                sky_condition.append(
                                    {
                                        "sky_cover": cover,
                                        "cloud_base_ft_agl": alt,
                                    }
                                )
                            times[station_lookup.index(s.uid)][dt].append(
                                Member(
                                    value=sky_condition,
                                    unit="ft_above_ground_level",
                                    name=variable,
                                    description=variable,
                                    standard=None,
                                )
                            )
                        else:
                            times[station_lookup.index(s.uid)][dt].append(
                                Member(
                                    value=float(metar.find(variable).text),
                                    unit=variable.split("_")[-1],
                                    name=variable,
                                    description=variable,
                                    standard=None,
                                )
                            )

            for time_dict, station in zip(times, stations):
                for dts, members in time_dict.items():
                    p = Point()
                    p.time = dts
                    p.location = station.location
                    p.members = members
                    station.add_element(p)

        self.feature = StationCollection(elements=stations)
Пример #30
0
    def __init__(self, waterml_data):

        if isinstance(waterml_data, str) or isinstance(waterml_data, unicode):
            try:
                self._root = etree.fromstring(str(waterml_data))
            except ValueError:
                # Strip out the XML header due to UTF8 encoding declaration
                self._root = etree.fromstring(waterml_data[56:])
        else:
            self._root = waterml_data

        response = WaterML_1_1(self._root).response

        stations = []
        station_lookup = []

        for timeseries in response.time_series:
            station_code = sorted(timeseries.source_info.site_codes)[0]
            # Create station if we have not seen it
            if station_code not in station_lookup:
                s = Station()
                s.uid = station_code

                info = timeseries.source_info
                s.name = info.site_name
                s.set_property("station_type", info.site_types)
                s.set_property("huc", info.site_properties.get("hucCd"))
                s.set_property("county", info.site_properties.get("countyCd"))
                s.set_property("state", info.site_properties.get("stateCd"))

                # Now set the station's location
                vertical = info.elevation
                if vertical is None:
                    vertical = 0

                try:
                    location = info.location.geo_coords[0]
                    srs = info.location.srs[0]
                except:
                    print "Could not find a location for %s... skipping station" % s.uid
                    continue

                s.location = sPoint(float(location[0]), float(location[1]),
                                    vertical)
                s.set_property("horizontal_crs", srs)
                s.set_property("vertical_units", "m")
                s.set_property("vertical_crs", info.vertical_datum)
                s.set_property("location_description", info.location.notes)

                stations.append(s)
                station_lookup.append(s.uid)

            times = {}
            variable = timeseries.variable
            for variable_timeseries in timeseries.values:
                for r in variable_timeseries:
                    dt = r.date_time
                    if dt.tzinfo is None:
                        dt = dt.replace(tzinfo=pytz.utc)
                    dt = dt.astimezone(pytz.utc)

                    if dt not in times.keys():
                        times[dt] = []

                    times[dt].append(
                        Member(value=r.value,
                               unit=variable.unit.code,
                               name=variable.variable_name,
                               description=variable.variable_description,
                               standard=variable.variable_code))

            station = stations[station_lookup.index(station_code)]
            for dts, members in times.iteritems():
                p = Point()
                p.time = dts
                p.location = station.location
                p.members = members
                station.add_element(p)

        self.feature = StationCollection(elements=stations)
Пример #31
0
    def __init__(self, awc_list):
        for awc_data in awc_list:
            self._root = etree.fromstring(awc_data.encode())
            '''Code to get station iterator goes here.'''
            stations = []
            station_lookup = []
            times = []

            for metar in self._root.iter('METAR'):
                uid = metar.find("station_id").text
                if uid not in station_lookup:
                    s = Station()
                    s.uid = uid
                    vertical = metar.find("elevation_m").text
                    if vertical is None:
                        vertical = 0
                    s.location = sPoint(float(metar.find("latitude").text),
                                        float(metar.find("longitude").text),
                                        float(vertical))
                    s.set_property("metar_type", metar.find("metar_type").text)
                    s.set_property("horizontal_crs", "GCS")
                    s.set_property("vertical_units", "m")
                    s.set_property("vertical_crs", "AGL")

                    stations.append(s)
                    station_lookup.append(s.uid)
                    times.append({})
                else:
                    s = stations[station_lookup.index(uid)]

                variables = [
                    "elevation_m", "raw_text", "temp_c", "dewpoint_c",
                    "wind_dir_degrees", "wind_speed_kt",
                    "visibility_statute_mi", "altim_in_hg", "wx_string",
                    "sky_condition", "flight_category"
                ]
                for variable in variables:
                    time_string = metar.find("observation_time").text
                    dt = datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%SZ")
                    if dt.tzinfo is None:
                        dt = dt.replace(tzinfo=pytz.utc)
                    dt = dt.astimezone(pytz.utc)

                    if dt not in list(times[station_lookup.index(
                            s.uid)].keys()):
                        times[station_lookup.index(s.uid)][dt] = []

                    if metar.find(variable) != None:
                        if variable in set(
                            ["raw_text", "flight_category", "wx_string"]):
                            times[station_lookup.index(s.uid)][dt].append(
                                Member(value=metar.find(variable).text,
                                       unit=None,
                                       name=variable,
                                       description=variable,
                                       standard=None))
                        elif variable == "sky_condition":
                            sky_condition = []
                            for cond in metar.findall(variable):
                                cover = cond.attrib["sky_cover"]
                                try:
                                    alt = cond.attrib["cloud_base_ft_agl"]
                                except:
                                    alt = None
                                sky_condition.append({
                                    "sky_cover": cover,
                                    "cloud_base_ft_agl": alt
                                })
                            times[station_lookup.index(s.uid)][dt].append(
                                Member(value=sky_condition,
                                       unit="ft_above_ground_level",
                                       name=variable,
                                       description=variable,
                                       standard=None))
                        else:
                            times[station_lookup.index(s.uid)][dt].append(
                                Member(value=float(metar.find(variable).text),
                                       unit=variable.split("_")[-1],
                                       name=variable,
                                       description=variable,
                                       standard=None))

            for time_dict, station in zip(times, stations):
                for dts, members in time_dict.items():
                    p = Point()
                    p.time = dts
                    p.location = station.location
                    p.members = members
                    station.add_element(p)

        self.feature = StationCollection(elements=stations)
Пример #32
0
    def __init__(self, element):

        record = DataRecord(element)

        # Top level org structure
        stations_field = record.get_by_name("stations")
        stations = {}
        sensors = {}
        for station in stations_field.content.field:
            s = Station()
            s.name = station.name
            s.uid = station.content.get_by_name("stationID").content.value

            # Location
            vector = station.content.get_by_name("platformLocation").content
            srss = vector.referenceFrame.split("&amp;")
            hsrs = None
            try:
                hsrs = Crs(srss[0])
            except ValueError:
                pass

            vsrs = None
            try:
                vsrs = Crs(srss[-1].replace("2=http:", "http:"))
            except ValueError:
                pass

            s.set_property("horizontal_srs", hsrs)
            s.set_property("vertical_srs", vsrs)
            s.set_property("localFrame", vector.localFrame)

            lat = vector.get_by_name("latitude").content.value
            lon = vector.get_by_name("longitude").content.value
            z = vector.get_by_name("height").content.value

            loc = [lon, lat]
            if z:
                loc.append(z)

            s.location = sPoint(*loc)

            # Sensors
            for sensor in station.content.get_by_name("sensors").content.field:
                name = sensor.name
                uri = sensor.content.get_by_name("sensorID").content.value
                height = None
                location_quantity = sensor.content.get_by_name(
                    "height").content
                if location_quantity.referenceFrame == "#%s_frame" % s.name:
                    # Uses the station as reference frame
                    if location_quantity.value and z:
                        height = z + location_quantity.value
                    horizontal_srs = s.get_property("horizontal_srs")
                    vertical_srs = s.get_property("vertical_srs")
                else:
                    # Uses its own height
                    if location_quantity.value:
                        height = location_quantity.value
                    horizontal_srs = None
                    vertical_srs = None
                    if hasattr(sensor, 'referenceFrame'):
                        srss = sensor.referenceFrame.split("&amp;")
                        try:
                            horizontal_srs = Crs(srss[0])
                        except ValueError:
                            pass
                        try:
                            vertical_srs = Crs(srss[-1].replace(
                                "2=http:", "http:"))
                        except ValueError:
                            pass

                loc = [s.location.x, s.location.y]
                if height:
                    loc.append(height)

                location = sPoint(*loc)

                sensors[name] = {
                    'station': s.uid,
                    'name': name,
                    'uri': uri,
                    'horizontal_srs': horizontal_srs,
                    'vertical_srs': vertical_srs,
                    'location': location,
                    'columns': [],  # Array of Members representing the columns
                    'values': []  # Array of Points (the actual data)
                }

            stations[s.uid] = s

        # Start building the column structure
        data_array = record.get_by_name("observationData").content
        data_record = data_array.elementType.content

        columns = []
        # Data outside of the <field name="sensors"> DataChoice element
        for f in data_record.field:
            columns.append(f)

        # Data inside of DataChoice
        sensor_data = data_record.get_by_name("sensor")
        for sendata in sensor_data.content.item:
            if sendata.content is not None:
                sensors[sendata.name]['columns'] = []
                sensors[sendata.name]['values'] = []
                for f in sendata.content.field:
                    # Create a model Member for each column that will be copied and filled with data from each row
                    sensors[sendata.name]['columns'].append(f)

        decimalSeparator = data_array.encoding.decimalSeparator
        tokenSeparator = data_array.encoding.tokenSeparator
        blockSeparator = data_array.encoding.blockSeparator
        collapseWhiteSpaces = data_array.encoding.collapseWhiteSpaces

        data_values = data_array.values
        self.raw_data = copy(data_values)

        for row in filter(lambda x: x != "",
                          data_values.split(blockSeparator)):

            pt = None
            members = []
            values = row.split(tokenSeparator)
            sensor_key = None
            i = 0

            for x in columns:

                if isinstance(
                        x.content, Time
                ) and x.content.definition == "http://www.opengis.net/def/property/OGC/0/SamplingTime":
                    pt = Point()
                    pt.time = parser.parse(values[i])

                elif isinstance(x.content, DataChoice):
                    sensor_key = values[i]
                    dc_cols = sensors[sensor_key]['columns']

                    for j, c in enumerate(dc_cols):
                        if isinstance(c.content, AbstractSimpleComponent):
                            m = Member(units=c.content.uom,
                                       name=c.name,
                                       standard=c.content.definition,
                                       value=float(values[i + 1]))
                            members.append(m)

                        elif isinstance(
                                c.content, Time
                        ) and c.content.definition == "http://www.opengis.net/def/property/OGC/0/SamplingTime":
                            pt = Point()
                            pt.time = parser.parse(values[i])

                        # For each data column
                        i += 1

                elif isinstance(x.content, AbstractSimpleComponent):
                    m = Member(units=x.content.uom,
                               name=x.name,
                               standard=x.content.definition,
                               value=float(values[i]))
                    members.append(m)

                else:
                    print "WHAT AM I"

                i += 1

            pt.members = members
            pt.location = stations[sensors[sensor_key]['station']].location
            sensors[sensor_key]['values'].append(pt)

        for k, v in stations.iteritems():
            for sk, sv in sensors.iteritems():
                # Match on station uid
                if sv['station'] == k:
                    v.elements = self._merge_points(v.elements or [],
                                                    sv['values'])

        if len(stations) > 1:
            self.feature = StationCollection(elements=stations)
        elif len(stations) == 1:
            self.feature = next(stations.itervalues())
        else:
            print "No stations found!"
Пример #33
0
    def _build_station_collection(self, parsed_metadata, parsed_data):

        stations = []
        for station_code, station_metadata in parsed_metadata.items():
            s = Station()
            s.uid = station_code
            s.name = station_metadata['nwsli']
            s.location = sPoint(station_metadata['longitude'],
                                station_metadata['latitude'],
                                0)  # hads always vertically zero

            s.set_property("location_description",
                           station_metadata['location_text'])
            s.set_property("state", station_metadata['state'])
            s.set_property("country", "USA")  # @TODO
            s.set_property("vertical_units", "ft")
            s.set_property("horizontal_crs", "EPSG:4326")
            s.set_property("vertical_crs", None)
            s.set_property("hsa", station_metadata['hsa'])
            s.set_property("init_transmit", station_metadata['init_transmit'])
            s.set_property("manufacturer", station_metadata['manufacturer'])
            s.set_property("owner", station_metadata['owner'])
            s.set_property("channel", station_metadata['channel'])

            stations.append(s)

            # data

            # possibility no data for this station, or vars filtered all out
            if station_code not in parsed_data:
                continue

            # need to group into distinct time/z value pairs

            # create a keyfunc (creates string of <z>-<timestamp>)
            zandtime = lambda x: str(x[3]) + "-" + str(
                time.mktime(x[1].timetuple()))

            # annotate data with z values, sort, group by keyfunc (z/time)
            grouped_data = groupby(
                sorted([(x[0], x[1], x[2], parsed_metadata[station_code]
                         ['variables'][x[0]]['base_elevation'])
                        for x in parsed_data[station_code]],
                       key=zandtime), zandtime)

            for _, group in grouped_data:

                # group is an iterator, turn it into a list (it will have at least one item)
                groupvals = list(group)

                p = Point()
                p.time = groupvals[0][1]
                p.location = sPoint(station_metadata['longitude'],
                                    station_metadata['latitude'],
                                    groupvals[0][3])

                for val in groupvals:
                    std_var = self.get_variable_info(val[0])
                    if std_var is None:
                        print("Unknown PE Code, ignoring: {} (station: {}).".
                              format(val[0], station_code))
                        continue

                    p.add_member(
                        Member(value=val[2],
                               standard=std_var[0],
                               unit=std_var[1],
                               name=std_var[2],
                               description=std_var[3]))

                s.add_element(p)

        return StationCollection(elements=stations)
Пример #34
0
    def _build_station_collection(self, parsed_metadata, parsed_data):

        stations = []
        for station_code, station_metadata in parsed_metadata.iteritems():
            s = Station()
            s.uid = station_code
            s.name = station_metadata['nwsli']
            s.location = sPoint(station_metadata['longitude'],
                                station_metadata['latitude'],
                                0)  # hads always vertically zero

            s.set_property("location_description" , station_metadata['location_text'])
            s.set_property("state"                , station_metadata['state'])
            s.set_property("country"              , "USA")   # @TODO
            s.set_property("vertical_units"       , "ft")
            s.set_property("horizontal_crs"       , "EPSG:4326")
            s.set_property("vertical_crs"         , None)
            s.set_property("hsa"                  , station_metadata['hsa'])
            s.set_property("init_transmit"        , station_metadata['init_transmit'])
            s.set_property("manufacturer"         , station_metadata['manufacturer'])
            s.set_property("owner"                , station_metadata['owner'])
            s.set_property("channel"              , station_metadata['channel'])

            stations.append(s)

            # data

            # possibility no data for this station, or vars filtered all out
            if station_code not in parsed_data:
                continue

            # need to group into distinct time/z value pairs

            # create a keyfunc (creates string of <z>-<timestamp>)
            zandtime = lambda x: str(x[3]) + "-" + str(time.mktime(x[1].timetuple()))

            # annotate data with z values, sort, group by keyfunc (z/time)
            grouped_data = groupby(sorted(map(lambda x: (x[0],
                                                         x[1],
                                                         x[2],
                                                         parsed_metadata[station_code]['variables'][x[0]]['base_elevation']),
                                              parsed_data[station_code]),
                                          key=zandtime),
                                   zandtime)

            for _, group in grouped_data:

                # group is an iterator, turn it into a list (it will have at least one item)
                groupvals = list(group)

                p = Point()
                p.time = groupvals[0][1]
                p.location = sPoint(station_metadata['longitude'],
                                    station_metadata['latitude'],
                                    groupvals[0][3])

                for val in groupvals:
                    std_var = self.get_variable_info(val[0])
                    if std_var is None:
                        print "Unknown PE Code, ignoring:", val[0], "(station:", station_code, ")"
                        continue

                    p.add_member(Member(value=val[2],
                                        standard=std_var[0],
                                        unit=std_var[1],
                                        name=std_var[2],
                                        description=std_var[3]))

                s.add_element(p)

        return StationCollection(elements=stations)
Пример #35
0
    def __init__(self, awc_list):
        for awc_data in awc_list:
            if isinstance(awc_data, str) or isinstance(awc_data, unicode):
                try:
                    self._root = etree.fromstring(str(awc_data))
                except ValueError:
                    # Strip out the XML header due to UTF8 encoding declaration
                    self._root = etree.fromstring(awc_data[56:])
            else:
                raise ValueError("Cannot parse response into ElementTree xml object")

            '''Code to get station iterator goes here
            '''
            stations = []
            station_lookup = []
            times = []

            for metar in self._root.iter('METAR'):
                uid = metar.find("station_id").text
                if uid not in station_lookup:
                    s = Station()
                    s.uid = uid
                    vertical = metar.find("elevation_m").text
                    if vertical is None:
                        vertical = 0
                    s.location = sPoint(float(metar.find("latitude").text), float(metar.find("longitude").text), float(vertical))
                    s.set_property("metar_type", metar.find("metar_type").text)
                    s.set_property("horizontal_crs", "GCS")
                    s.set_property("vertical_units", "m")
                    s.set_property("vertical_crs", "AGL")

                    stations.append(s)
                    station_lookup.append(s.uid)
                    times.append({})
                else:
                    s = stations[station_lookup.index(uid)]

                variables = ["elevation_m", "raw_text", "temp_c", "dewpoint_c", "wind_dir_degrees", "wind_speed_kt", "visibility_statute_mi", "altim_in_hg", "wx_string", "sky_condition", "flight_category"]
                for variable in variables:
                    time_string = metar.find("observation_time").text
                    dt = datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%SZ")
                    if dt.tzinfo is None:
                        dt = dt.replace(tzinfo=pytz.utc)
                    dt = dt.astimezone(pytz.utc)

                    if dt not in times[station_lookup.index(s.uid)].keys():
                        times[station_lookup.index(s.uid)][dt] = []

                    if metar.find(variable) != None:
                        if variable in set(["raw_text", "flight_category", "wx_string"]):
                            times[station_lookup.index(s.uid)][dt].append(Member(value=metar.find(variable).text, unit=None, name=variable, description=variable, standard=None))
                        elif variable == "sky_condition":
                            sky_condition = []
                            for cond in metar.findall(variable):
                                cover = cond.attrib["sky_cover"]
                                try:
                                    alt = cond.attrib["cloud_base_ft_agl"]
                                except:
                                    alt = None
                                sky_condition.append({"sky_cover" : cover, "cloud_base_ft_agl" : alt})
                            times[station_lookup.index(s.uid)][dt].append(Member(value=sky_condition, unit="ft_above_ground_level", name=variable, description=variable, standard=None))
                        else:
                            times[station_lookup.index(s.uid)][dt].append(Member(value=float(metar.find(variable).text), unit=variable.split("_")[-1], name=variable, description=variable, standard=None))

            for time_dict, station in zip(times, stations):
                for dts, members in time_dict.iteritems():
                    p = Point()
                    p.time = dts
                    p.location = station.location
                    p.members = members
                    station.add_element(p)

        self.feature = StationCollection(elements=stations)
Пример #36
0
    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)
Пример #37
0
    def test_section(self):

        day = 1
        hour = 0
        sc = Section()
        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)
            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)

        sc.calculate_bounds()

        assert len(sc.get_path()) == 10

        assert sc.size == 10
        assert sc.point_size == 200
        assert sc.type == "Section"

        assert len(sc.time_range) == 200
        assert sc.time_range[0] == datetime(2012, 4, 1, 0, 0)
        assert sc.time_range[-1] == dt - timedelta(minutes=2)

        assert len(sc.depth_range) == 200
        assert sc.depth_range[0] == 0
        assert sc.depth_range[-1] == 19