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

icoscp_stilt - return basic dobj metadata together with observation results #193

Merged
merged 1 commit into from
Aug 22, 2024
Merged
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
29 changes: 19 additions & 10 deletions icoscp_stilt/src/icoscp_stilt/stilt.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ def has_observation_data(self) -> bool:
"""
return self.icosId is not None

@dataclass(frozen=True)
class ObservationResult:
dobj: DataObjectLite
columns: ArraysDict

@dataclass(frozen=True)
class ObservationDfResult:
dobj: DataObjectLite
df: pd.DataFrame

def list_stations() -> list[StiltStation]:
"""
Expand Down Expand Up @@ -244,7 +253,7 @@ def fetch_observations_pandas(
spec: URL,
stations: list[StiltStation],
columns: list[str] | None = None
) -> dict[str, pd.DataFrame]:
) -> dict[str, ObservationDfResult]:
"""
Batch-fetch observational datasets for a number of STILT stations

Expand All @@ -257,19 +266,19 @@ def fetch_observations_pandas(
within the observational datasets; if omitted, all columns are included
in the result

:return (dict[str, DataFrame]): a dictionary with STILT station IDs as
keys and pandas DataFrames with observational datasets as values
:return (dict[str, ObservationDfResult]): a dictionary with STILT station IDs as
keys and instances of ObservationDfResult as values
"""
return {
st_id: pd.DataFrame(arrs)
for st_id, arrs in fetch_observations(spec, stations, columns).items()
st_id: ObservationDfResult(obs_res.dobj, pd.DataFrame(obs_res.columns))
for st_id, obs_res in fetch_observations(spec, stations, columns).items()
}

def fetch_observations(
spec: URL,
stations: list[StiltStation],
columns: list[str] | None = None
) -> dict[str, ArraysDict]:
) -> dict[str, ObservationResult]:
"""
Batch-fetch observational datasets for a number of STILT stations. Is a
lower-level and better-performing version of `fetch_observations_pandas`.
Expand All @@ -287,8 +296,8 @@ def fetch_observations(
within the observational datasets; if omitted, all columns are included
in the result

:return (dict[str, dict[str, ndarray]]): a dictionary with STILT station IDs as
keys and as values, dictionaries of column names vs numpy arrays
:return (dict[str, ObservationResult]): a dictionary with STILT station IDs as
keys and ObservationResult instances as values
"""

icos2ss: dict[tuple[URL, float], StiltStation] = {
Expand All @@ -312,8 +321,8 @@ def lookup_ss(dobj: DataObjectLite) -> list[StiltStation]:
if lookup_ss(dobj)
]
return {
ss.id: arrs
for dobj, arrs in data.batch_get_columns_as_arrays(dobjs_to_fetch, columns)
ss.id: ObservationResult(dobj, cols)
for dobj, cols in data.batch_get_columns_as_arrays(dobjs_to_fetch, columns)
for ss in lookup_ss(dobj)
}

Expand Down
Loading