Example #1
0
def test__get_interval_code():
    """edge_test.EdgeFactory_test.test__get_interval_code()
    """
    assert_equals(EdgeFactory()._get_interval_code('daily'), 'D')
    assert_equals(EdgeFactory()._get_interval_code('hourly'), 'H')
    assert_equals(EdgeFactory()._get_interval_code('minute'), 'M')
    assert_equals(EdgeFactory()._get_interval_code('second'), 'S')
def test__get_interval_code():
    """edge_test.EdgeFactory_test.test__get_interval_code()
    """
    assert_equal(EdgeFactory()._get_interval_code("day"), "D")
    assert_equal(EdgeFactory()._get_interval_code("hour"), "H")
    assert_equal(EdgeFactory()._get_interval_code("minute"), "M")
    assert_equal(EdgeFactory()._get_interval_code("second"), "S")
def test__get_edge_location():
    """edge_test.EdgeFactory_test.test__get_edge_location()"""
    # Call _get_edge_location, make certain it returns the correct edge
    # location code.
    assert_equal(EdgeFactory()._get_edge_location("", "", "variation", ""),
                 "R0")
    assert_equal(
        EdgeFactory()._get_edge_location("", "", "quasi-definitive", ""), "Q0")
    assert_equal(EdgeFactory()._get_edge_location("", "", "definitive", ""),
                 "D0")
Example #4
0
def test__get_edge_location():
    """edge_test.EdgeFactory_test.test__get_edge_location()
    """
    # Call _get_edge_location, make certain it returns the correct edge
    # location code.
    assert_equals(EdgeFactory()._get_edge_location(
            '', '', 'variation', ''), 'R0')
    assert_equals(EdgeFactory()._get_edge_location(
            '', '', 'quasi-definitive', ''), 'Q0')
    assert_equals(EdgeFactory()._get_edge_location(
            '', '', 'definitive', ''), 'D0')
def test__get_edge_channel():
    """edge_test.EdgeFactory_test.test__get_edge_channel()
    """
    # Call private function _get_edge_channel, make certain
    # it gets back the appropriate 2 character code.
    assert_equals(EdgeFactory()._get_edge_channel('', 'D', '', 'minute'),
                  'MVD')
    assert_equals(EdgeFactory()._get_edge_channel('', 'E', '', 'minute'),
                  'MVE')
    assert_equals(EdgeFactory()._get_edge_channel('', 'F', '', 'minute'),
                  'MSF')
    assert_equals(EdgeFactory()._get_edge_channel('', 'H', '', 'minute'),
                  'MVH')
    assert_equals(EdgeFactory()._get_edge_channel('', 'Z', '', 'minute'),
                  'MVZ')
    def output(self):
        ''' 
        Generate data wrapper for USGS geomagnetic data

        @return geomagnetic data wrapper
        '''

        observatory_list = self.ap_paramList[0]()

        # USGS Edge server
        base_url = 'cwbpub.cr.usgs.gov'
        factory = EdgeFactory(host=base_url, port=2060)

        data_dict = OrderedDict()
        for observatory in observatory_list:
            ret_data = factory.get_timeseries(
                observatory=observatory,
                interval=self.interval,
                type=self.data_type,
                channels=self.channels,
                starttime=UTCDateTime(self.start_time),
                endtime=UTCDateTime(self.end_time))

            obs_data = OrderedDict()
            for label, trace in zip(self.channels, ret_data):
                time = pd.to_datetime(
                    trace.stats['starttime'].datetime) + pd.to_timedelta(
                        trace.times(), unit='s')
                obs_data[label] = pd.Series(trace.data, time)

            data_dict[observatory] = pd.DataFrame(obs_data)

        return TableWrapper(data_dict, default_columns=self.channels)
Example #7
0
def test_raw_input_client():
    """edge_test.RawInputClient_test.test_raw_input_client()"""
    network = "NT"
    station = "BOU"
    channel = "MVH"
    location = "R0"
    data = [0, 1, 2, 3, 4, 5]
    starttime = UTCDateTime("2019-12-01")

    trace = Trace(
        numpy.array(data, dtype=numpy.float64),
        Stats({
            "channel": channel,
            "delta": 60.0,
            "location": location,
            "network": network,
            "npts": len(data),
            "starttime": starttime,
            "station": station,
        }),
    )

    client = MockRawInputClient(
        tag="tag",
        host="host",
        port="port",
        station=station,
        channel=channel,
        location=location,
        network=network,
    )
    trace_send = EdgeFactory()._convert_trace_to_int(trace.copy())
    client.send_trace("minute", trace_send)
    # verify data was sent
    assert_equal(len(client.last_send), 1)
 def __init__(self, factory=None, metadata=None):
     self.factory = factory or EdgeFactory()
     self.metadata = metadata or ObservatoryMetadata().metadata
     base = os.path.dirname(__file__)
     filepath = os.path.abspath(os.path.join(base, '..', 'package.json'))
     with open(filepath) as package:
         specifications = load(package)
     self.version = specifications['version']
 def __init__(self,
              factory=None,
              version=None,
              metadata=None,
              usage_documentation=None):
     self.factory = factory or EdgeFactory()
     self.metadata = metadata or ObservatoryMetadata().metadata
     self.version = version
     self.usage_documentation = usage_documentation or WebServiceUsage()
def test__set_metadata():
    """edge_test.EdgeFactory_test.test__set_metadata()"""
    # Call _set_metadata with 2 traces,  and make certain the stats get
    # set for both traces.
    trace1 = Trace()
    trace2 = Trace()
    stream = Stream(traces=[trace1, trace2])
    EdgeFactory()._set_metadata(stream, "BOU", "H", "variation", "minute")
    assert_equal(stream[0].stats["channel"], "H")
    assert_equal(stream[1].stats["channel"], "H")
Example #11
0
def test__set_metadata():
    """edge_test.EdgeFactory_test.test__set_metadata()
    """
    # Call _set_metadata with 2 traces,  and make certain the stats get
    # set for both traces.
    trace1 = Trace()
    trace2 = Trace()
    stream = Stream(traces=[trace1, trace2])
    EdgeFactory()._set_metadata(stream, 'BOU', 'H', 'variation', 'minute')
    assert_equals(stream[0].stats['channel'], 'H')
    assert_equals(stream[1].stats['channel'], 'H')
Example #12
0
def dont_get_timeseries():
    """edge_test.EdgeFactory_test.test_get_timeseries()"""
    # Call get_timeseries, and test stats for comfirmation that it came back.
    # TODO, need to pass in host and port from a config file, or manually
    #   change for a single test.
    edge_factory = EdgeFactory(host='TODO', port='TODO')
    timeseries = edge_factory.get_timeseries(
        UTCDateTime(2015, 3, 1, 0, 0, 0), UTCDateTime(2015, 3, 1, 1, 0, 0),
        'BOU', ('H'), 'variation', 'minute')
    assert_equals(timeseries.select(channel='H')[0].stats.station,
        'BOU', 'Expect timeseries to have stats')
    assert_equals(timeseries.select(channel='H')[0].stats.channel,
        'H', 'Expect timeseries stats channel to be equal to H')
def test__get_edge_channel():
    """edge_test.EdgeFactory_test.test__get_edge_channel()"""
    # Call private function _get_edge_channel, make certain
    # it gets back the appropriate 2 character code.
    assert_equal(EdgeFactory()._get_edge_channel("", "D", "", "minute"), "MVD")
    assert_equal(EdgeFactory()._get_edge_channel("", "E", "", "minute"), "MVE")
    assert_equal(EdgeFactory()._get_edge_channel("", "F", "", "minute"), "MSF")
    assert_equal(EdgeFactory()._get_edge_channel("", "H", "", "minute"), "MVH")
    assert_equal(EdgeFactory()._get_edge_channel("", "DIST", "", "minute"),
                 "MDT")
    assert_equal(EdgeFactory()._get_edge_channel("", "DST", "", "minute"),
                 "MGD")
    assert_equal(EdgeFactory()._get_edge_channel("", "E-E", "", "minute"),
                 "MQE")
    assert_equal(EdgeFactory()._get_edge_channel("", "E-N", "", "minute"),
                 "MQN")
def dont_get_timeseries():
    """edge_test.EdgeFactory_test.test_get_timeseries()"""
    # Call get_timeseries, and test stats for comfirmation that it came back.
    # TODO, need to pass in host and port from a config file, or manually
    #   change for a single test.
    edge_factory = EdgeFactory(host="TODO", port="TODO")
    timeseries = edge_factory.get_timeseries(
        UTCDateTime(2015, 3, 1, 0, 0, 0),
        UTCDateTime(2015, 3, 1, 1, 0, 0),
        "BOU",
        ("H"),
        "variation",
        "minute",
    )
    assert_equal(
        timeseries.select(channel="H")[0].stats.station,
        "BOU",
        "Expect timeseries to have stats",
    )
    assert_equal(
        timeseries.select(channel="H")[0].stats.channel,
        "H",
        "Expect timeseries stats channel to be equal to H",
    )
Example #15
0
                    and endtime is None):
                out_start = svsqdist[ch].next_starttime
            else:
                if starttime is not None:
                    out_start = starttime
                if endtime is not None:
                    out_end = endtime

            if out_start <= out_end:
                # possibly re-initialize with previous 90 days of data
                in_start, in_end = svsqdist[ch].get_input_interval(
                    out_start, out_end, observatory=ob, channels=ch)

                # create factory and pull data from USGS Edge
                in_factory = EdgeFactory(host=edge_url,
                                         port=edge_port,
                                         interval='minute',
                                         type='adjusted')
                in_stream += in_factory.get_timeseries(starttime=in_start,
                                                       endtime=in_end,
                                                       observatory=ob,
                                                       channels=ch)
                print('Retrieved from Edge: %s-%s' % (ob, ch), end="")
                print(' from', in_start, 'to', in_end)
            else:
                print("Decreasing interval requested (", end="")
                print(out_start, 'to', out_end, ")", end="")
                print(" skipping %s-%s..." % (ob, ch))

        if in_stream.count() is not len(channels):
            # if any channel was not read in, STOP PROCESSING
            print("No inputs processed or written...")
Example #16
0
def test__get_edge_network():
    """edge_test.EdgeFactory_test.test__get_edge_network()
    """
    # _get_edge_network should always return NT for use by USGS geomag
    assert_equals(EdgeFactory()._get_edge_network(' ', ' ', ' ', ' '), 'NT')
Example #17
0
                'No more than four elements allowed for iaga2002 format.')
        if self.starttime > self.endtime:
            raise WebServiceException('Starttime must be before endtime.')
        if self.data_type not in VALID_DATA_TYPES:
            raise WebServiceException('Bad type value "%s".'
                                      ' Valid values are: %s' %
                                      (self.data_type, VALID_DATA_TYPES))
        if self.sampling_period not in VALID_SAMPLING_PERIODS:
            raise WebServiceException(
                'Bad sampling_period value "%s".'
                ' Valid values are: %s' %
                (self.sampling_period, VALID_SAMPLING_PERIODS))
        if self.output_format not in VALID_OUTPUT_FORMATS:
            raise WebServiceException(
                'Bad format value "%s".'
                ' Valid values are: %s' %
                (self.output_format, VALID_OUTPUT_FORMATS))


class WebServiceException(Exception):
    """Base class for exceptions thrown by web services."""
    pass


if __name__ == '__main__':
    from wsgiref.simple_server import make_server

    app = WebService(EdgeFactory())
    httpd = make_server('', 7981, app)
    httpd.serve_forever()
Example #18
0
 def __init__(self, factory=None, version=None, metadata=None):
     self.factory = factory or EdgeFactory()
     self.metadata = metadata or ObservatoryMetadata().metadata
     self.version = version
Example #19
0
            elif starttime is not None:
               out_start = starttime

         # possibly re-initialize with previous 90 days of data
         in_start, in_end = svsqdist[ch].get_input_interval(
            out_start,
            out_end,
            observatory = ob,
            channels = ch
         )

         if in_start <= in_end:
            # create factory and pull data from USGS Edge
            in_factory = EdgeFactory(
               host = edge_url,
               port = edge_port,
               interval = 'minute',
               type = 'variation'
            )
            in_stream += in_factory.get_timeseries(
               starttime = in_start,
               endtime = in_end,
               observatory = ob,
               channels = ch
            )
            print 'Retreived from Edge: %s-%s'%(ob,ch),
            print 'from', in_start, 'to', in_end
         else:
            print "Non-monotonic interval requested (",
            print in_start, 'to', in_end, ")",
            print "skipping %s-%s..."%(ob,ch)
Example #20
0
def test__get_edge_station():
    """edge_test.EdgeFactory_test.test__get_edge_station()
    """
    # _get_edge_station will return the observatory code passed in.
    assert_equals(EdgeFactory()._get_edge_station('BOU', ' ', ' ', ' '), 'BOU')
def test__get_edge_network():
    """edge_test.EdgeFactory_test.test__get_edge_network()
    """
    # _get_edge_network should always return NT for use by USGS geomag
    assert_equal(EdgeFactory()._get_edge_network(" ", " ", " ", " "), "NT")
def test__get_edge_station():
    """edge_test.EdgeFactory_test.test__get_edge_station()
    """
    # _get_edge_station will return the observatory code passed in.
    assert_equal(EdgeFactory()._get_edge_station("BOU", " ", " ", " "), "BOU")