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

notebook is now running.

made adoptions to the updates by Simon
parent 0140944b
No related branches found
No related tags found
1 merge request!11Creation of first beta release version
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Get Dataset from request ### Get Dataset from request
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from datetime import datetime as dt from datetime import datetime as dt
from pathlib import Path from pathlib import Path
import pandas as pd import pandas as pd
import numpy as np import numpy as np
from toargridding.grids import RegularGrid from toargridding.grids import RegularGrid
from toargridding.toar_rest_client import ( from toargridding.toar_rest_client import (
AnalysisServiceDownload, AnalysisServiceDownload,
STATION_LAT, STATION_LAT,
STATION_LON, STATION_LON,
) )
from toargridding.metadata import Metadata, TimeSample, AnalysisRequestResult, Coordinates from toargridding.metadata import Metadata, TimeSample, AnalysisRequestResult, Coordinates
from toargridding.variables import Coordinate from toargridding.variables import Coordinate
endpoint = "https://toar-data.fz-juelich.de/api/v2/analysis/statistics/" endpoint = "https://toar-data.fz-juelich.de/api/v2/analysis/statistics/"
toargridding_base_path = Path("/home/simon/Projects/toar/toargridding/") #starts in diretory [path/to/toargridding]/tests
cache_dir = toargridding_base_path / "tests" / "results" #maybe adopt the toargridding_base_path for your machine.
data_download_dir = toargridding_base_path / "tests" / "data" toargridding_base_path = Path(".")
cache_dir = toargridding_base_path / "cache"
data_download_dir = toargridding_base_path / "data"
analysis_service = AnalysisServiceDownload(endpoint, cache_dir, data_download_dir) analysis_service = AnalysisServiceDownload(endpoint, cache_dir, data_download_dir)
my_grid = RegularGrid(1.9, 2.5) my_grid = RegularGrid(1.9, 2.5)
time = TimeSample(dt(2016,1,1), dt(2016,12,31), "daily") time = TimeSample(dt(2016,1,1), dt(2016,12,31), "daily")
metadata = Metadata.construct("mole_fraction_of_ozone_in_air", "mean", time) metadata = Metadata.construct("mole_fraction_of_ozone_in_air", "mean", time)
with open("data/daily_2010-01-01_2011-01-01.zip", "r+b") as sample_file: #not used in this notebook
response_content = sample_file.read() #with open("data/daily_2010-01-01_2011-01-01.zip", "r+b") as sample_file:
# response_content = sample_file.read()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# this cell can runs longer than 30minutes
data = analysis_service.get_data(metadata) data = analysis_service.get_data(metadata)
ds = my_grid.as_xarray(data) ds = my_grid.as_xarray(data)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Visual inspection ### Visual inspection
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#calculation of coordinates for plotting
import cartopy.crs as ccrs import cartopy.crs as ccrs
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import matplotlib.ticker as mticker import matplotlib.ticker as mticker
mean_data = ds["mean"] mean_data = ds["mean"]
clean_coords = analysis_service.get_clean_coords(timeseries_metadata) clean_coords = data.stations_coords
all_na = timeseries.isna().all(axis=1) all_na = data.stations_data.isna().all(axis=1)
clean_coords = all_na.to_frame().join(clean_coords)[["latitude", "longitude"]] clean_coords = all_na.to_frame().join(clean_coords)[["latitude", "longitude"]]
all_na_coords = clean_coords[all_na] all_na_coords = clean_coords[all_na]
not_na_coords = clean_coords[~all_na] not_na_coords = clean_coords[~all_na]
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import matplotlib as mpl import matplotlib as mpl
#definition of plot function
def plot_cells(data, stations, na_stations, discrete=True, plot_stations=False): def plot_cells(data, stations, na_stations, discrete=True, plot_stations=False):
fig = plt.figure(figsize=(9, 18)) fig = plt.figure(figsize=(9, 18))
ax = plt.axes(projection=ccrs.PlateCarree()) ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines() ax.coastlines()
gl = ax.gridlines(draw_labels=True) gl = ax.gridlines(draw_labels=True)
gl.top_labels = False gl.top_labels = False
gl.left_labels = False gl.left_labels = False
gl.xlocator = mticker.FixedLocator(data.longitude.values) gl.xlocator = mticker.FixedLocator(data.longitude.values)
gl.ylocator = mticker.FixedLocator(data.latitude.values) gl.ylocator = mticker.FixedLocator(data.latitude.values)
cmap = mpl.cm.viridis cmap = mpl.cm.viridis
if discrete: if discrete:
print(np.unique(data.values)) print(np.unique(data.values))
bounds = np.arange(8) bounds = np.arange(8)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N, extend="both") norm = mpl.colors.BoundaryNorm(bounds, cmap.N, extend="both")
ticks = np.arange(bounds.size + 1)[:-1] + 0.5 ticks = np.arange(bounds.size + 1)[:-1] + 0.5
ticklables = bounds ticklables = bounds
im = plt.pcolormesh( im = plt.pcolormesh(
data.longitude, data.longitude,
data.latitude, data.latitude,
data, data,
transform=ccrs.PlateCarree(), transform=ccrs.PlateCarree(),
cmap=cmap, cmap=cmap,
shading="nearest", shading="nearest",
norm=norm, norm=norm,
) )
cb = fig.colorbar(im, ax=ax, shrink=0.2, aspect=25) cb = fig.colorbar(im, ax=ax, shrink=0.2, aspect=25)
cb.set_ticks(ticks) cb.set_ticks(ticks)
cb.set_ticklabels(ticklables) cb.set_ticklabels(ticklables)
im = plt.pcolormesh( im = plt.pcolormesh(
data.longitude, data.longitude,
data.latitude, data.latitude,
data, data,
transform=ccrs.PlateCarree(), transform=ccrs.PlateCarree(),
cmap=cmap, cmap=cmap,
shading="nearest", shading="nearest",
norm=norm, norm=norm,
) )
else: else:
im = plt.pcolormesh( im = plt.pcolormesh(
data.longitude, data.longitude,
data.latitude, data.latitude,
data, data,
transform=ccrs.PlateCarree(), transform=ccrs.PlateCarree(),
cmap=cmap, cmap=cmap,
shading="nearest", shading="nearest",
) )
cb = fig.colorbar(im, ax=ax, shrink=0.2, aspect=25) cb = fig.colorbar(im, ax=ax, shrink=0.2, aspect=25)
if plot_stations: if plot_stations:
plt.scatter(na_stations[STATION_LON], na_stations[STATION_LAT], s=1, c="k") plt.scatter(na_stations[STATION_LON], na_stations[STATION_LAT], s=1, c="k")
plt.scatter(stations[STATION_LON], stations[STATION_LAT], s=1, c="r") plt.scatter(stations[STATION_LON], stations[STATION_LAT], s=1, c="r")
plt.tight_layout() plt.tight_layout()
plt.title(f"global ozon at {data.time.values}") plt.title(f"global ozon at {data.time.values}")
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
timestep = 2 timestep = 2
time = ds.time[timestep] time = ds.time[timestep]
data = ds.sel(time=time) data = ds.sel(time=time)
plot_cells(data["mean"], not_na_coords, all_na_coords, discrete=False) plot_cells(data["mean"], not_na_coords, all_na_coords, discrete=False)
plt.show() plt.show()
plot_cells(data["n"], not_na_coords, all_na_coords, discrete=True) plot_cells(data["n"], not_na_coords, all_na_coords, discrete=True)
plt.show() plt.show()
n_observations = ds["n"].sum(["latitude", "longitude"]) n_observations = ds["n"].sum(["latitude", "longitude"])
plt.plot(ds.time, n_observations) plt.plot(ds.time, n_observations)
print(np.unique(ds["n"])) print(np.unique(ds["n"]))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment