def setup_platform(opp, config, add_entities, discovery_info=None): """Set up the NOAA Tides and Currents sensor.""" station_id = config[CONF_STATION_ID] name = config.get(CONF_NAME) timezone = config.get(CONF_TIME_ZONE) if CONF_UNIT_SYSTEM in config: unit_system = config[CONF_UNIT_SYSTEM] elif opp.config.units.is_metric: unit_system = UNIT_SYSTEMS[1] else: unit_system = UNIT_SYSTEMS[0] try: station = coops.Station(station_id, unit_system) except KeyError: _LOGGER.error("NOAA Tides Sensor station_id %s does not exist", station_id) return except requests.exceptions.ConnectionError as exception: _LOGGER.error( "Connection error during setup in NOAA Tides Sensor for station_id: %s", station_id, ) raise PlatformNotReady from exception noaa_sensor = NOAATidesAndCurrentsSensor(name, station_id, timezone, unit_system, station) add_entities([noaa_sensor], True)
def __init__(self, name, station_id, timezone, unit_system): """Initialize the sensor.""" self._name = name self._station = nc.Station(station_id) self._timezone = timezone self._unit_system = unit_system self.data = None self.attr = None
def test_error_handling(): seattle = nc.Station(9447130) with pytest.raises(ValueError): seattle.get_data( begin_date="20150101", end_date="20150331", product="water_level", datum="navd88", # this is an invalid datum units="metric", time_zone="gmt")
def noaa_coops_update(self): if self._station is None: _LOGGER.debug("No station object exists yet- creating one.") try: self._station = nc.Station(self._station_id) except requests.exceptions.ConnectionError as err: _LOGGER.error(f"Couldn't create a NOAA station object. Will retry next update. Error: {err}") self._station = None return stn = self._station end = datetime.now() delta = timedelta(minutes=60) begin = end - delta temps = None air_temps = None try: temps = stn.get_data( begin_date=begin.strftime("%Y%m%d %H:%M"), end_date=end.strftime("%Y%m%d %H:%M"), product="water_temperature", units=self._unit_system, time_zone=self._timezone, ).tail(1) _LOGGER.debug( "Recent water temperature data queried with start time set to %s", begin.strftime("%m-%d-%Y %H:%M"), ) except ValueError as err: _LOGGER.error(f"Check NOAA Tides and Currents: {err.args}") except requests.exceptions.ConnectionError as err: _LOGGER.error(f"Couldn't connect to NOAA Ties and Currents API: {err}") try: air_temps = stn.get_data( begin_date=begin.strftime("%Y%m%d %H:%M"), end_date=end.strftime("%Y%m%d %H:%M"), product="air_temperature", units=self._unit_system, time_zone=self._timezone, ).tail(1) _LOGGER.debug( "Recent temperature data queried with start time set to %s", begin.strftime("%m-%d-%Y %H:%M"), ) except ValueError as err: _LOGGER.error(f"Check NOAA Tides and Currents: {err.args}") except requests.exceptions.ConnectionError as err: _LOGGER.error(f"Couldn't connect to NOAA Ties and Currents API: {err}") if temps is None and air_temps is None: self.data = None else: self.data = (temps, air_temps) _LOGGER.debug(f"Data = {self.data}")
def noaa_coops_update(self): _LOGGER.debug("update queried.") if self._station is None: _LOGGER.debug("No station object exists yet- creating one.") try: self._station = nc.Station(self._station_id) except requests.exceptions.ConnectionError as err: _LOGGER.error(f"Couldn't create a NOAA station object. Will retry next update. Error: {err}") self._station = None return begin = datetime.now() - timedelta(hours=24) begin_date=begin.strftime("%Y%m%d %H:%M") end = begin + timedelta(hours=48) end_date = end.strftime("%Y%m%d %H:%M") try: df_predictions = self._station.get_data( begin_date=begin_date, end_date=end_date, product="predictions", datum="MLLW", interval="hilo", units=self._unit_system, time_zone=self._timezone, ) self.data = df_predictions _LOGGER.debug(f"Data = {self.data}") _LOGGER.debug( "Recent Tide data queried with start time set to %s", begin_date, ) except ValueError as err: _LOGGER.error(f"Check NOAA Tides and Currents: {err.args}") except requests.exceptions.ConnectionError as err: _LOGGER.error(f"Couldn't connect to NOAA Ties and Currents API: {err}") return None
CONST_SWELL_ID = 8638610 CONST_PRODUCTS = ["water_level", "high_low"] CONST_BEGIN_YEAR = 2000 CONST_END_YEAR = 2019 CONST_DATUM = "MTL" CONST_UNIT = "english" CONST_TIME_ZONE = "lst" # Create data folder if it does not exist in the project directory if os.path.exists("Data"): pass else: os.mkdir("Data") # Initialize connection to Swells_Point and water/tide DataFrames swells_point = nc.Station(CONST_SWELL_ID) df_water_levels = pandas.DataFrame() df_tide_levels = pandas.DataFrame() # Hit Swells_Point for tide data using NOAA API and push the data to a csv print("Collecting tide data from Swells Point: ") for i in range(CONST_BEGIN_YEAR, CONST_END_YEAR): percent = 100 * float((i - CONST_BEGIN_YEAR)) / float( (CONST_END_YEAR - CONST_BEGIN_YEAR)) if i == CONST_END_YEAR - 1: percent = 100 sys.stdout.write("\r%d%%" % percent) sys.stdout.flush() begin_date = str(i) + "0101" end_date = str(i + 1) + "0101" temp = swells_point.get_data(begin_date, end_date, CONST_PRODUCTS[1],
import noaa_coops as nc oakland_outer_LB3 = nc.Station('s09010') currents = oakland_outer_LB3.get_data( begin_date="20210414", end_date="20210415", product="currents", bin_num=2, units="metric", time_zone="gmt", ) currents.head(20)