示例#1
0
class AwcRestTest(unittest.TestCase):
    def setUp(self):
        self.c = AwcRest()

    def test_nwc_stations(self):
        stations = self.c.stations
        assert stations[0] == "AGGH"
        assert stations[-1] == "ZYTX"

    def test_bbox_filter_raw(self):
        self.c.filter(bbox=(-80, 30, -60, 50))
        response = self.c.raw()
        assert (
            '<response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XML-Schema-instance" version="1.2" xsi:noNamespaceSchemaLocation="http://aviationweather.gov/adds/schema/metar1_2.xsd">'
            in response[0]
        )

    def test_bigbbox_filter_paegan(self):
        self.c.filter(bbox=(-80, 30, -60, 50))
        response = self.c.collect()
        assert type(response) == StationCollection

    def test_smallbbox_filter_paegan(self):
        self.c.filter(bbox=(-74, 41, -73, 43))
        response = self.c.collect()
        assert type(response) == StationCollection
示例#2
0
class AwcRestTest(unittest.TestCase):
    def setUp(self):
        self.c = AwcRest()

    def test_nwc_stations(self):
        stations = self.c.stations
        assert stations[0] == "AAAD"
        assert stations[-1] == "ZYYY"

    def test_bbox_filter_raw(self):
        self.c.filter(bbox=(-80, 30, -60, 50))
        response = self.c.raw()
        assert (
            '<response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XML-Schema-instance" version="1.2" xsi:noNamespaceSchemaLocation="http://aviationweather.gov/adds/schema/metar1_2.xsd">'  # noqa
            in response[0])

    def test_bigbbox_filter_paegan(self):
        self.c.filter(bbox=(-80, 30, -60, 50))
        response = self.c.collect()
        assert type(response) == StationCollection

    def test_smallbbox_filter_paegan(self):
        self.c.filter(bbox=(-74, 41, -73, 43))
        response = self.c.collect()
        assert type(response) == StationCollection
示例#3
0
  def get_nws_data(self, site, observations, begin_date, units_coverter, db_obj):
    start_time = time.time()
    logger = logging.getLogger(self.__class__.__name__)
    logger.debug("Starting get_nws_data")

    row_entry_date = datetime.now()
    utc_tz = timezone('UTC')
    eastern_tz = timezone('US/Eastern')

    header = ["raw_text","station_id","observation_time","latitude","longitude","temp_c","dewpoint_c","wind_dir_degrees","wind_speed_kt","wind_gust_kt","visibility_statute_mi","altim_in_hg","sea_level_pressure_mb","corrected","auto","auto_station","maintenance_indicator_on","no_signal","lightning_sensor_off","freezing_rain_sensor_off","present_weather_sensor_off","wx_string","sky_cover","cloud_base_ft_agl","sky_cover","cloud_base_ft_agl","sky_cover","cloud_base_ft_agl","sky_cover","cloud_base_ft_agl","flight_category","three_hr_pressure_tendency_mb","maxT_c","minT_c","maxT24hr_c","minT24hr_c","precip_in","pcp3hr_in","pcp6hr_in","pcp24hr_in","snow_in","vert_vis_ft","metar_type","elevation_m"]

    platform_handle = 'nws.%s.met' % (site)
    if db_obj.platformExists(platform_handle) is None:
      obs_list = []
      for obs_setup in observations:
        if site in obs_setup['sites']:
          for xenia_obs in obs_setup['xenia_obs']:
            obs_list.append({'obs_name': xenia_obs['xenia_name'],
                             'uom_name': xenia_obs['xenia_units'],
                             's_order': 1})
      db_obj.buildMinimalPlatform(platform_handle, obs_list)
    # Build sensor_id and m_type_id list.
    for obs_setup in observations:
      if site in obs_setup['sites']:
        for xenia_obs in obs_setup['xenia_obs']:
          m_type_id = db_obj.mTypeExists(xenia_obs['xenia_name'], xenia_obs['xenia_units'])
          sensor_id = db_obj.sensorExists(xenia_obs['xenia_name'],
                                          xenia_obs['xenia_units'],
                                          platform_handle,
                                          1)
          xenia_obs['m_type_id'] = m_type_id
          xenia_obs['sensor_id'] = sensor_id

    awc_query = AwcRest()
    # dates.sort(reverse=True)

    logger.debug("Query site: %s for date: %s" % (site, begin_date))
    awc_query.clear()
    # utc_end_date = begin_date.astimezone(utc_tz) + timedelta(hours=24)
    utc_end_date = begin_date.astimezone(utc_tz)
    start_date = begin_date.astimezone(utc_tz) - timedelta(hours=24)

    awc_query.filter(station=site, start=start_date, end=utc_end_date)
    try:
      # response = awc_query.collect()
      temp_file = os.path.join(self.temp_directory, "nws_data.csv")
      with open(temp_file, "w") as nws_file_obj:
        nws_file_obj.write(awc_query.raw(responseFormat="csv")[0])
    except Exception as e:
      logger.exception(e)
    else:
      with open(temp_file, "rU") as nws_data_file:
        line_cnt = 0
        header_row = 5
        nws_csv_rdr = csv.DictReader(nws_data_file, header)
        for row_ndx, row in enumerate(nws_csv_rdr):
          if row_ndx > header_row:
            try:
              obs_date = utc_tz.localize(datetime.strptime(row['observation_time'], "%Y-%m-%dT%H:%M:%SZ"))
            except (ValueError, Exception) as e:
              logger.exception(e)
            else:
              for obs in nws_obs:
                try:
                  for xenia_obs in obs["xenia_obs"]:
                    src_obs_name = xenia_obs['obs_name']
                    src_obs_uom = xenia_obs['units']
                    xenia_obs_name = xenia_obs['xenia_name']
                    xenia_uom = xenia_obs['xenia_units']
                    sensor_id = xenia_obs['sensor_id']
                    m_type_id = xenia_obs['m_type_id']
                    try:
                      obs_value = float(row[src_obs_name])
                    except ValueError as e:
                      obs_value = 0.0
                    try:
                      latitude = float(row["latitude"])
                      longitude = float(row["longitude"])
                    except ValueError as e:
                      latitude = 0.0
                      longitude = 0.0
                    try:
                      elevation = float(row['elevation_m'])
                    except ValueError as e:
                      elevation = 0

                    obs_val = units_coverter.measurementConvert(obs_value,
                                                                src_obs_uom,
                                                                xenia_uom)
                    if obs_val is not None:
                      obs_rec = multi_obs(row_entry_date=row_entry_date,
                                          platform_handle=platform_handle,
                                          sensor_id=sensor_id,
                                          m_type_id=m_type_id,
                                          m_date=obs_date.strftime('%Y-%m-%dT%H:%M:%S'),
                                          m_lon=longitude,
                                          m_lat=latitude,
                                          m_z=elevation,
                                          m_value=obs_val
                                          )

                      rec_id = db_obj.addRec(obs_rec, True)
                      if rec_id is not None:
                        logger.debug("Adding obs: %s(%s) Date: %s Value: %s S_Order: 1" % \
                                     (xenia_obs_name, xenia_uom, obs_date, obs_val))
                      else:
                        logger.error("Failed adding obs: %s(%s) Date: %s Value: %s S_Order: 1" % \
                                     (xenia_obs_name, xenia_uom, obs_date, obs_val))

                except (Exception,ValueError) as e:
                  logger.exception(e)

    logger.debug("Finished get_nws_data in %f seconds" % (time.time() - start_time))

    return