Skip to content
Snippets Groups Projects
Commit babb91c7 authored by Carsten Hinz's avatar Carsten Hinz
Browse files

added notebook for example of different versions to include contributors

edited formatting for contributors endpoint with names
parent d7699878
No related branches found
No related tags found
3 merge requests!11Creation of first beta release version,!10change in metadata of notebook,!9change in metadata of notebook
%% Cell type:markdown id: tags:
### Get Dataset from request
%% Cell type:code id: tags:
``` python
from datetime import datetime as dt
from pathlib import Path
import pandas as pd
import numpy as np
from toargridding.grids import RegularGrid
from toargridding.toar_rest_client import (
AnalysisServiceDownload,
STATION_LAT,
STATION_LON,
)
from toargridding.metadata import Metadata, TimeSample, AnalysisRequestResult, Coordinates
from toargridding.variables import Coordinate
from toargridding.contributors import contributionsManager, contributions_manager_by_name
import logging
from toargridding.defaultLogging import toargridding_defaultLogging
#setup of logging
logger = toargridding_defaultLogging()
logger.addShellLogger(logging.DEBUG)
logger.logExceptions()
endpoint = "https://toar-data.fz-juelich.de/api/v2/analysis/statistics/"
#starts in directory [path/to/toargridding]/tests
#maybe adopt the toargridding_base_path for your machine.
toargridding_base_path = Path(".")
cache_dir = toargridding_base_path / "cache"
data_download_dir = toargridding_base_path / "results"
analysis_service = AnalysisServiceDownload(endpoint, cache_dir, data_download_dir, use_downloaded=True)
my_grid = RegularGrid(1.9, 2.5)
time = TimeSample(dt(2016,1,1), dt(2016,2,28), "daily")
metadata = Metadata.construct("mole_fraction_of_ozone_in_air", time, "mean")
```
%% Cell type:markdown id: tags:
# Contributors as dedicated file
%% Cell type:code id: tags:
``` python
# this cell can runs longer than 30minutes
data = analysis_service.get_data(metadata)
# create contributors endpoint and write result to metadata
contrib = contributionsManager(metadata.get_id(), data_download_dir)
contrib.extract_contributors_from_data_frame(data.stations_data)
metadata.contributors_metadata_field = contrib.setup_contributors_endpoint_for_metadata()
ds = my_grid.as_xarray(data)
#store dataset
ds.to_netcdf(data_download_dir / f"curl+id-file_{metadata.get_id()}_by_names_inline_{my_grid.get_id()}.nc")
```
%% Cell type:markdown id: tags:
# Contributors as single line request
%% Cell type:code id: tags:
``` python
# this cell can runs longer than 30minutes
data = analysis_service.get_data(metadata)
# create contributors endpoint and write result to metadata
contrib = contributionsManager(metadata.get_id(), data_download_dir)
contrib.inline_mode = True
contrib.extract_contributors_from_data_frame(data.stations_data)
metadata.contributors_metadata_field = contrib.setup_contributors_endpoint_for_metadata()
ds = my_grid.as_xarray(data)
#store dataset
ds.to_netcdf(data_download_dir / f"request_in_field_{metadata.get_id()}_by_names_inline_{my_grid.get_id()}.nc")
```
%% Cell type:markdown id: tags:
# Contributors by name within field
%% Cell type:code id: tags:
``` python
# this cell can runs longer than 30minutes
data = analysis_service.get_data(metadata)
# create contributors endpoint and write result to metadata
contrib = contributions_manager_by_name(metadata.get_id(), data_download_dir)
contrib.inline_mode = True
contrib.extract_contributors_from_data_frame(data.stations_data)
metadata.contributors_metadata_field = contrib.setup_contributors_endpoint_for_metadata()
ds = my_grid.as_xarray(data)
#store dataset
ds.to_netcdf(data_download_dir / f"by_name_{metadata.get_id()}_by_names_inline_{my_grid.get_id()}.nc")
```
......@@ -64,7 +64,7 @@ class contributions_manager_by_name(contributionsManager):
self.timeseriesIDs.add(name)
def id_to_names(self, id : int) -> list[str]:
for pos in range(10):
req_res = requests.get(f"https://toar-data.fz-juelich.de/api/v2/timeseries/{id}")
req_res = requests.get(f"https://toar-data.fz-juelich.de/api/v2/timeseries/{id}", timeout=40)
try:
results = req_res.json()
break
......@@ -86,5 +86,12 @@ class contributions_manager_by_name(contributionsManager):
except:
raise RuntimeError("Could not find 'person' or 'organisation' in the response.\n" + str(r))
return names
def setup_contributors_inline(self) -> str:
return ", ".join([str(id) for id in sorted(self.timeseriesIDs)])
def setup_contributors_id_file(self) -> str:
ext = "contributors"
with open(self.contributors_path / f"{self.requestID}.{ext}", "w") as f:
for id in self.timeseriesIDs:
f.write(f"{id}\n")
return f'see file {self.requestID}.{ext}"'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment