Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/cams solar radiation timeseries #190

Merged
merged 18 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cads_adaptors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from cads_adaptors.adaptors import AbstractAdaptor, Context, DummyAdaptor
from cads_adaptors.adaptors.cadsobs.adaptor import ObservationsAdaptor
from cads_adaptors.adaptors.cams_solar_rad import CamsSolarRadiationTimeseriesAdaptor
from cads_adaptors.adaptors.cds import AbstractCdsAdaptor, DummyCdsAdaptor
from cads_adaptors.adaptors.insitu import (
InsituDatabaseCdsAdaptor,
Expand Down Expand Up @@ -55,4 +56,5 @@
"MultiMarsCdsAdaptor",
"RoocsCdsAdaptor",
"ObservationsAdaptor",
"CamsSolarRadiationTimeseriesAdaptor",
]
102 changes: 102 additions & 0 deletions cads_adaptors/adaptors/cams_solar_rad/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from typing import BinaryIO

from cads_adaptors.adaptors.cams_solar_rad.functions import (
BadRequest,
NoData,
determine_result_filename,
get_numeric_user_id,
solar_rad_retrieve,
)
from cads_adaptors.adaptors.cds import AbstractCdsAdaptor, Request
from cads_adaptors.exceptions import InvalidRequest


class CamsSolarRadiationTimeseriesAdaptor(AbstractCdsAdaptor):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# Schema required to ensure adaptor will not fall over with an uncaught exception
self.schemas.append(
{
"_draft": "7",
"type": "object", # Request should be a single dict
"required": [ # ... with at least these keys
"sky_type",
"location",
"altitude",
"date",
"time_step",
"time_reference",
"format",
],
"properties": {
"sky_type": {"type": "string"},
"location": {
"type": "object",
"properties": {
"latitude": {
"maximum": 90.0,
"minimum": -90.0,
"type": "number",
},
"longitude": {
"maximum": 180.0,
"minimum": -180.0,
"type": "number",
},
},
},
"altitude": {"type": "string", "format": "numeric string"},
"date": {"type": "string", "format": "date range"},
"time_step": {"type": "string"},
"time_reference": {"type": "string"},
"format": {"type": "string"},
},
"_defaults": {"format": "csv"},
}
)

def retrieve(self, request: Request) -> BinaryIO:
self.context.debug(f"Request is {request!r}")

# Intersect constraints
if self.config.get("intersect_constraints", False):
requests = self.intersect_constraints(request)
if len(requests) != 1:
if len(requests) == 0:
msg = "Error: no intersection with the constraints."
else:
# TODO: check if indeed this can never happen
msg = "Error: unexpected intersection with more than 1 constraint."
self.context.add_user_visible_error(msg)
raise InvalidRequest(msg)
request_after_intersection = requests[0]
else:
request_after_intersection = request

# Apply mapping
self._pre_retrieve(
request_after_intersection, default_download_format="as_source"
)
mreq = self.mapped_request
self.context.debug(f"Mapped request is {mreq!r}")

numeric_user_id = get_numeric_user_id(self.config["user_uid"])
result_filename = determine_result_filename(
self.config, request_after_intersection
)

try:
solar_rad_retrieve(
mreq,
user_id=numeric_user_id,
outfile=result_filename,
logger=self.context,
)

except (BadRequest, NoData) as e:
msg = e.args[0]
self.context.add_user_visible_error(msg)
raise InvalidRequest(msg)

return open(result_filename, "rb")
Loading