Skip to content

Commit

Permalink
Merge pull request #43 from CDJellen/user/cjellen/netcdf-support
Browse files Browse the repository at this point in the history
Add firsdt-class NetCDF4 file support through NDBC DODS/THREDDS
  • Loading branch information
CDJellen authored Sep 1, 2024
2 parents 1ea6407 + 48fbd64 commit b44fac1
Show file tree
Hide file tree
Showing 173 changed files with 2,420 additions and 870 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
import pandas as pd

from ndbc_api.api.handlers._base import BaseHandler
from ndbc_api.api.parsers.adcp import AdcpParser
from ndbc_api.api.parsers.cwind import CwindParser
from ndbc_api.api.parsers.ocean import OceanParser
from ndbc_api.api.parsers.spec import SpecParser
from ndbc_api.api.parsers.stdmet import StdmetParser
from ndbc_api.api.parsers.supl import SuplParser
from ndbc_api.api.parsers.swden import SwdenParser
from ndbc_api.api.parsers.swdir import SwdirParser
from ndbc_api.api.parsers.swdir2 import Swdir2Parser
from ndbc_api.api.parsers.swr1 import Swr1Parser
from ndbc_api.api.parsers.swr2 import Swr2Parser
from ndbc_api.api.requests.adcp import AdcpRequest
from ndbc_api.api.requests.cwind import CwindRequest
from ndbc_api.api.requests.ocean import OceanRequest
from ndbc_api.api.requests.spec import SpecRequest
from ndbc_api.api.requests.stdmet import StdmetRequest
from ndbc_api.api.requests.supl import SuplRequest
from ndbc_api.api.requests.swden import SwdenRequest
from ndbc_api.api.requests.swdir import SwdirRequest
from ndbc_api.api.requests.swdir2 import Swdir2Request
from ndbc_api.api.requests.swr1 import Swr1Request
from ndbc_api.api.requests.swr2 import Swr2Request
from ndbc_api.api.parsers.http.adcp import AdcpParser
from ndbc_api.api.parsers.http.cwind import CwindParser
from ndbc_api.api.parsers.http.ocean import OceanParser
from ndbc_api.api.parsers.http.spec import SpecParser
from ndbc_api.api.parsers.http.stdmet import StdmetParser
from ndbc_api.api.parsers.http.supl import SuplParser
from ndbc_api.api.parsers.http.swden import SwdenParser
from ndbc_api.api.parsers.http.swdir import SwdirParser
from ndbc_api.api.parsers.http.swdir2 import Swdir2Parser
from ndbc_api.api.parsers.http.swr1 import Swr1Parser
from ndbc_api.api.parsers.http.swr2 import Swr2Parser
from ndbc_api.api.requests.http.adcp import AdcpRequest
from ndbc_api.api.requests.http.cwind import CwindRequest
from ndbc_api.api.requests.http.ocean import OceanRequest
from ndbc_api.api.requests.http.spec import SpecRequest
from ndbc_api.api.requests.http.stdmet import StdmetRequest
from ndbc_api.api.requests.http.supl import SuplRequest
from ndbc_api.api.requests.http.swden import SwdenRequest
from ndbc_api.api.requests.http.swdir import SwdirRequest
from ndbc_api.api.requests.http.swdir2 import Swdir2Request
from ndbc_api.api.requests.http.swr1 import Swr1Request
from ndbc_api.api.requests.http.swr2 import Swr2Request
from ndbc_api.exceptions import RequestException, ResponseException


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import pandas as pd

from ndbc_api.api.handlers._base import BaseHandler
from ndbc_api.api.parsers.station_historical import HistoricalParser
from ndbc_api.api.parsers.station_metadata import MetadataParser
from ndbc_api.api.parsers.station_realtime import RealtimeParser
from ndbc_api.api.parsers.active_stations import ActiveStationsParser
from ndbc_api.api.parsers.historical_stations import HistoricalStationsParser
from ndbc_api.api.requests.station_historical import HistoricalRequest
from ndbc_api.api.requests.station_metadata import MetadataRequest
from ndbc_api.api.requests.station_realtime import RealtimeRequest
from ndbc_api.api.requests.active_stations import ActiveStationsRequest
from ndbc_api.api.requests.historical_stations import HistoricalStationsRequest
from ndbc_api.api.parsers.http.station_historical import HistoricalParser
from ndbc_api.api.parsers.http.station_metadata import MetadataParser
from ndbc_api.api.parsers.http.station_realtime import RealtimeParser
from ndbc_api.api.parsers.http.active_stations import ActiveStationsParser
from ndbc_api.api.parsers.http.historical_stations import HistoricalStationsParser
from ndbc_api.api.requests.http.station_historical import HistoricalRequest
from ndbc_api.api.requests.http.station_metadata import MetadataRequest
from ndbc_api.api.requests.http.station_realtime import RealtimeRequest
from ndbc_api.api.requests.http.active_stations import ActiveStationsRequest
from ndbc_api.api.requests.http.historical_stations import HistoricalStationsRequest
from ndbc_api.exceptions import ParserException, ResponseException


Expand All @@ -32,7 +32,7 @@ def stations(cls, handler: Any) -> pd.DataFrame:
"""Get all active stations from NDBC."""
req = ActiveStationsRequest.build_request()
try:
resp = handler.handle_request('stn', req)
resp = handler.handle_request('stn_active', req)
except (AttributeError, ValueError, TypeError) as e:
raise ResponseException(
'Failed to execute `station` request.') from e
Expand All @@ -43,7 +43,7 @@ def historical_stations(cls, handler: Any) -> pd.DataFrame:
"""Get historical stations from NDBC."""
req = HistoricalStationsRequest.build_request()
try:
resp = handler.handle_request('stn', req)
resp = handler.handle_request('stn_historical', req)
except (AttributeError, ValueError, TypeError) as e:
raise ResponseException(
'Failed to execute `station` request.') from e
Expand Down
185 changes: 185 additions & 0 deletions ndbc_api/api/handlers/opendap/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
from datetime import datetime, timedelta
from typing import Any

import netCDF4 as nc

from ndbc_api.api.handlers._base import BaseHandler
from ndbc_api.api.parsers.opendap.adcp import AdcpParser
from ndbc_api.api.parsers.opendap.cwind import CwindParser
from ndbc_api.api.parsers.opendap.ocean import OceanParser
from ndbc_api.api.parsers.opendap.pwind import PwindParser
from ndbc_api.api.parsers.opendap.stdmet import StdmetParser
from ndbc_api.api.parsers.opendap.swden import SwdenParser
from ndbc_api.api.parsers.opendap.wlevel import WlevelParser
from ndbc_api.api.requests.opendap.adcp import AdcpRequest
from ndbc_api.api.requests.opendap.cwind import CwindRequest
from ndbc_api.api.requests.opendap.ocean import OceanRequest
from ndbc_api.api.requests.opendap.pwind import PwindRequest
from ndbc_api.api.requests.opendap.stdmet import StdmetRequest
from ndbc_api.api.requests.opendap.swden import SwdenRequest
from ndbc_api.api.requests.opendap.wlevel import WlevelRequest
from ndbc_api.exceptions import RequestException, ResponseException


class OpenDapDataHandler(BaseHandler):

@classmethod
def adcp(
cls,
handler: Any,
station_id: str,
start_time: datetime = datetime.now() - timedelta(days=30),
end_time: datetime = datetime.now(),
use_timestamp: bool = True,
) -> 'nc.Dataset':
"""adcp"""
try:
reqs = AdcpRequest.build_request(station_id=station_id,
start_time=start_time,
end_time=end_time)
except Exception as e:
raise RequestException('Failed to build request.') from e
try:
resps = handler.handle_requests(station_id=station_id, reqs=reqs)
except Exception as e:
raise ResponseException('Failed to execute requests.') from e
return AdcpParser.nc_from_responses(responses=resps,
use_timestamp=use_timestamp)

@classmethod
def cwind(
cls,
handler: Any,
station_id: str,
start_time: datetime = datetime.now() - timedelta(days=30),
end_time: datetime = datetime.now(),
use_timestamp: bool = True,
) -> 'nc.Dataset':
"""cwind"""
try:
reqs = CwindRequest.build_request(station_id=station_id,
start_time=start_time,
end_time=end_time)
except Exception as e:
raise RequestException('Failed to build request.') from e
try:
resps = handler.handle_requests(station_id=station_id, reqs=reqs)
except Exception as e:
raise ResponseException('Failed to execute requests.') from e
return CwindParser.nc_from_responses(responses=resps,
use_timestamp=use_timestamp)

@classmethod
def ocean(
cls,
handler: Any,
station_id: str,
start_time: datetime = datetime.now() - timedelta(days=30),
end_time: datetime = datetime.now(),
use_timestamp: bool = True,
) -> 'nc.Dataset':
"""ocean"""
try:
reqs = OceanRequest.build_request(station_id=station_id,
start_time=start_time,
end_time=end_time)
except Exception as e:
raise RequestException('Failed to build request.') from e
try:
resps = handler.handle_requests(station_id=station_id, reqs=reqs)
except Exception as e:
raise ResponseException('Failed to execute requests.') from e
return OceanParser.nc_from_responses(responses=resps,
use_timestamp=use_timestamp)

@classmethod
def pwind(
cls,
handler: Any,
station_id: str,
start_time: datetime = datetime.now() - timedelta(days=30),
end_time: datetime = datetime.now(),
use_timestamp: bool = True,
) -> 'nc.Dataset':
"""pwind"""
try:
reqs = PwindRequest.build_request(station_id=station_id,
start_time=start_time,
end_time=end_time)
except Exception as e:
raise RequestException('Failed to build request.') from e
try:
resps = handler.handle_requests(station_id=station_id, reqs=reqs)
except Exception as e:
raise ResponseException('Failed to execute requests.') from e
return PwindParser.nc_from_responses(responses=resps,
use_timestamp=use_timestamp)

@classmethod
def stdmet(
cls,
handler: Any,
station_id: str,
start_time: datetime = datetime.now() - timedelta(days=30),
end_time: datetime = datetime.now(),
use_timestamp: bool = True,
) -> 'nc.Dataset':
"""stdmet"""
try:
reqs = StdmetRequest.build_request(station_id=station_id,
start_time=start_time,
end_time=end_time)
except Exception as e:
raise RequestException('Failed to build request.') from e
try:
resps = handler.handle_requests(station_id=station_id, reqs=reqs)
except Exception as e:
raise ResponseException('Failed to execute requests.') from e
return StdmetParser.nc_from_responses(responses=resps,
use_timestamp=use_timestamp)

@classmethod
def swden(
cls,
handler: Any,
station_id: str,
start_time: datetime = datetime.now() - timedelta(days=30),
end_time: datetime = datetime.now(),
use_timestamp: bool = True,
) -> 'nc.Dataset':
"""swden"""
try:
reqs = SwdenRequest.build_request(station_id=station_id,
start_time=start_time,
end_time=end_time)
except Exception as e:
raise RequestException('Failed to build request.') from e
try:
resps = handler.handle_requests(station_id=station_id, reqs=reqs)
except Exception as e:
raise ResponseException('Failed to execute requests.') from e
return SwdenParser.nc_from_responses(responses=resps,
use_timestamp=use_timestamp)

@classmethod
def wlevel(
cls,
handler: Any,
station_id: str,
start_time: datetime = datetime.now() - timedelta(days=30),
end_time: datetime = datetime.now(),
use_timestamp: bool = True,
) -> 'nc.Dataset':
"""wlevel"""
try:
reqs = WlevelRequest.build_request(station_id=station_id,
start_time=start_time,
end_time=end_time)
except Exception as e:
raise RequestException('Failed to build request.') from e
try:
resps = handler.handle_requests(station_id=station_id, reqs=reqs)
except Exception as e:
raise ResponseException('Failed to execute requests.') from e
return WlevelParser.nc_from_responses(responses=resps,
use_timestamp=use_timestamp)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import datetime
from io import StringIO
from typing import List, Tuple

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pandas as pd
from bs4 import BeautifulSoup

from ndbc_api.api.parsers._base import BaseParser
from ndbc_api.api.parsers.http._base import BaseParser


class HtmlParser(BaseParser):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
import xml.etree.ElementTree as ET

from ndbc_api.api.parsers._base import BaseParser
from ndbc_api.api.parsers.http._base import BaseParser
from ndbc_api.exceptions import ParserException


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pandas as pd

from ndbc_api.exceptions import ParserException
from ndbc_api.api.parsers._xml import XMLParser
from ndbc_api.api.parsers.http._xml import XMLParser


class ActiveStationsParser(XMLParser):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pandas as pd

from ndbc_api.api.parsers._base import BaseParser
from ndbc_api.api.parsers.http._base import BaseParser


class AdcpParser(BaseParser):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pandas as pd

from ndbc_api.api.parsers._base import BaseParser
from ndbc_api.api.parsers.http._base import BaseParser


class CwindParser(BaseParser):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pandas as pd

from ndbc_api.exceptions import ParserException
from ndbc_api.api.parsers._xml import XMLParser
from ndbc_api.api.parsers.http._xml import XMLParser


class HistoricalStationsParser(XMLParser):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pandas as pd

from ndbc_api.api.parsers._base import BaseParser
from ndbc_api.api.parsers.http._base import BaseParser


class OceanParser(BaseParser):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pandas as pd

from ndbc_api.api.parsers._base import BaseParser
from ndbc_api.api.parsers.http._base import BaseParser


class SpecParser(BaseParser):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import bs4

from ndbc_api.api.parsers._station import StationParser
from ndbc_api.api.parsers.http._station import StationParser


class HistoricalParser(StationParser):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import bs4

from ndbc_api.api.parsers._station import StationParser
from ndbc_api.api.parsers.http._station import StationParser


class MetadataParser(StationParser):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bs4

from ndbc_api.api.parsers._station import StationParser
from ndbc_api.api.parsers.http._station import StationParser


class RealtimeParser(StationParser):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pandas as pd

from ndbc_api.api.parsers._base import BaseParser
from ndbc_api.api.parsers.http._base import BaseParser


class StdmetParser(BaseParser):
Expand Down
Loading

0 comments on commit b44fac1

Please sign in to comment.