# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
import cartopy.crs as ccrs

import netCDF4

# +
# import cdsapi

# c = cdsapi.Client()

# c.retrieve(
#     'cams-europe-air-quality-forecasts',
#     {
#         'model': 'ensemble',
#         'date': '2019-04-01/2019-04-30',
#         'format': 'netcdf',
#         'variable': 'nitrogen_dioxide',
#         'level': '0',
#         'type': 'analysis',
#         'time': [
#             '08:00', '12:00',
#         ],
#         'leadtime_hour': '0',
#     },
#     './data/download.nc')
# -

no2 = xr.open_dataarray("./data/download.nc")
no2

# ### Extracting and Plotting the point data

first_point = no2.isel(latitude=0,longitude=0)
area = no2.sel(latitude=50.05,longitude=8.05)
area.plot()

no2.sel(latitude=50.05,longitude=[1.05, 8.05],level=0.0).plot.line(x = "time")

# ### Histograms
#
# We can also plot histograms of the entire data set and in order to have more sensible looking bins we plot the logarithm of the no2 data

np.log(no2).plot()

# ## Maps
#
# Visualizing the data on a map

# +
# Using the geophysical units and 'robust' disregards outliers for the colormap creation

no2.sortby(['longitude','latitude'],ascending=True).isel(time=0).plot(robust=True)

# +
# Centered over Darmstadt

ax = plt.axes(projection=ccrs.EuroPP())
ax.coastlines()

# Use the last time step in array
p = no2.sortby(['longitude','latitude'],ascending=True).isel(time=-4).plot(transform=ccrs.PlateCarree(),robust=True)
# -

# ## Using a different dataset instead of dataarray

# +
filename1 = './data/OMI_trno2_0.10x0.10_201904_Col3_V4.nc'
filename2 = './data/OMI_trno2_0.10x0.10_202004_Col3_V4.nc'

ds1 = xr.open_dataset(filename1)
ds2 = xr.open_dataset(filename2)
# -

ds2



StarcoderdataJupyterScriptsDedupFiltered