# ---
# 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
# ---

# <img src='../img/EU-Copernicus-EUM_3Logos.png' alt='Logo EU Copernicus EUMETSAT' align='right' width='50%'></img>

# <br>

# <a href="../00_index.ipynb"><< Index</a><br>
# <a href="./422_Sentinel-3_SLSTR_NRT_FRP_L2_exercise.ipynb"><< 422 - Sentinel-3 SLSTR NRT - FRP - Level 2 - Exercise</a><span style="float:right;"><a href="./431_CAMS_EAC4_tcco_exercise.ipynb">431 - CAMS Global reanalysis (EAC4) - TCCO - Exercise >></a></span>

# <div class="alert alert-block alert-danger">
# <b>40 - EXERCISES</b>
# </div>

# <div class="alert alert-block alert-danger">
#
# <b>PREREQUISITES </b>
#
# The following **20 - DATA DISCOVERY** module is a prerequisite:
#
# - [253 - Sentinel-3 SLSTR NRT - Aerosol Optical Depth - Level 2 - Load and browse](../20_data_discovery/253_Sentinel-3_SLSTR_NRT_AOD_L2_load_browse.ipynb)    
#     
# It is recommended to go through the module before you do this exercise workbook.
# </div>

# <hr>

# # 4.2.3 Sentinel-3 SLSTR Near-Real-Time - Aerosol Optical Depth (AOD)

# This notebook is an `exercise workbook`, where you can practise the data handling with Sentinel-3 Near Real Time SLSTR Fire Radiative Power (FRP) data. The exercise example is based on Sentinel-3 NRT SLSTR AOD data capturing the Australian fires sensed on 3 December 2019.
#

# <hr>

# ## How it works

# This exercise workbook consists of two types of exercises:

# #### Coding assignments
# Coding assignments ask you to fill an empty code cell with code. You recognize `coding assignments` as the yellow-coloured boxes.
#
# <div class="alert alert-block alert-warning">
# <b>Coding assignment</b>
# </div>

# #### Questions
#
# Questions ask you to reflect on a result and output. `Questions` are provided as green-coloured boxes.
#
# <div class="alert alert-block alert-success">
# <b>Question</b>
# </div>

# <hr>

# ## Outline
#   
#   
# * [Example: Australian Fires - December 2019](#australian_fires)
#   * [1 - Load Sentinel-3 Near-Real-Time SLSTR AOD data](#load)
#   * [2 - Extract AOD variables](#extract)
#   * [3 - Visualize AOD Ocean and AOD Land information](#visualize)

# <hr>

# #### Load required libraries

# +
import xarray as xr

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.pyplot as pltfacebook
import matplotlib.colors as colors
import matplotlib.cm as cm
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
# -

# #### Load helper functions

# %run ../functions.ipynb

# <hr>

# ## <a id='load'></a>Load Sentinel-3 Near-Real-Time SLSTR AOD data

# The Near-Real-Time Sentinel-3 Aerosol Optical Depth data are disseminated in `netCDF`. `netCDF` data can be loaded with the Python library [xarray](http://xarray.pydata.org/en/stable/) and its function `xr.open_dataset()`. 
#
# You see that the data file contains two `dimensions`:
# * `columns` and
# * `rows`.
#
# It further contains an long list of `data variables`, including:
# * `AOD_550`,
# * `AOD_550_uncertainty`,
# * `AOD_550_Ocean_NonFiltered`,
# * `AOD_550_Land_Experimental_PostFiltered`,
# ...
#
# A data file also contains a set of `attributes`, which give you more information about the data file and the data it contains, e.g the `start_time` and `stop_time` or the `product_name`.

# <div class="alert alert-block alert-warning">
# <b>Coding assignment:</b><br>
#
# Load the data file `AOD_Australia_20191203.nc` under the directory `./eodata/sentinel3/slstr/2019/12/03/` with the xarray function `xr.open_dataset()` and call the Dataset `file`. 
# </div>

# + solution2="hidden" solution2_first=true
# Enter your solution below






# + solution2="hidden"
file = xr.open_dataset('../eodata/sentinel3/slstr/2019/12/03/AOD_Australia_20191203.nc')
file

# + [markdown] solution2="hidden" solution2_first=true
# <div class="alert alert-block alert-success">
# <b>Question:</b><br>
#    Can you identify the dimensions of the dataset and description? What data variables are available in the data file?
# </div>

# + [markdown] solution2="hidden"
# The dataset has two dimensions: `columns` and `rows` and holds 48 data variables.
# -

# <br>

# ### <a id='extract'></a>Extract Aerosol Optical Depth variables

# The next step is to extract the variables of interest. Let us select the following two variables:
# * `AOD_550`: it is the Aerosol Optical Depth at 550nm. (*Note: it only covers ocean surfaces.*)
# * `AOD_550_Land_Experimental_PostFiltered`: it is the Aerosol Optical Depth at 550nm. (*Note: it only covers land surfaces.*)
#
# Both `DataArrays` have two dimensions (`rows` and `columns`) and the following attributes, which provide additional information about the variables:
# * `long_name`
# * `standard_name`
# * `valid_min`
# * `valid_max`
# * `coordinates`

# <div class="alert alert-block alert-warning">
# <b>Coding assignment:</b><br>
#
# Load the data variable `AOD_550` and `AOD_550_Land_Experimental_PostFiltered` from the xarray.Dataset `file` above and call them `aod_ocean` and `aod_land` respectively.
#
# <br>
#
# Print both variables.
# </div>

# + solution2="hidden" solution2_first=true
# Enter your solution here







# + solution2="hidden"
aod_ocean = file.AOD_550
aod_land = file.AOD_550_Land_Experimental_PostFiltered
print(aod_ocean)
print(' ')
print(aod_land)

# + [markdown] solution2="hidden" solution2_first=true
# <div class="alert alert-block alert-success">
# <b>Question:</b><br>
#
# What is the `long_name` of both variables?
# </div>

# + [markdown] solution2="hidden"
# * `aod_ocean`: <br>
# Aerosol optical thickness at 550 nm - Best quality (post-filtered due to e.g. cloud residuals, sedimentation...) - Only over ocean surfaces (Please read the EUMETSAT Product Notice for further details)
# * `aod_land`: <br>
# Aerosol optical thickness at 550 nm - Only over land surfaces  - Experimental version - Shall be only used with great caution following EUMETSAT recommendations in the Product Notice, not yet suitable for operational use of validation - Higher quality (Post-filtered of e.g. cloud residuals...)
# -

# <br>

# You can also load `latitude` and `longitude` information, which can be used later for visualizing the variables.

# <div class="alert alert-block alert-warning">
# <b>Coding assignment:</b><br>
#
# Load the `latitude` and `longitude` variables from the xarray Dataset `file` above and call them `lat_nc` and `lon_nc` respectively.
#
# <br>
#
# Print both variables.
# </div>

# + solution2="hidden" solution2_first=true
# Enter your solution here





# + solution2="hidden"
lat_nc = file.latitude
lon_nc = file.longitude
print(lat_nc)
print(' ')
print(lon_nc)
# -

# ### <a id='visualize'></a> Visualize AOD Ocean and AOD Land variables

# The final step is to visualize both variables, Aerosol Optical Depth over ocean and land together in one plot. You can use the function [visualize_s3_aod](../functions.ipynb#visualize_s3_aod) for it.

# <div class="alert alert-block alert-warning">
# <b>Coding assignment:</b><br>
#
# Visualize both variables, `aod_ocean` and `aod_land` with the function [visualize_s3_aod](../functions.ipynb#visualize_s3_aod).
#
# <br>
#
# HINT: with `?visualize_s3_aod` you can open the function's docstring and see what keyword arguments the function takes.
# </div>

# ?visualize_s3_aod

# + solution2="hidden" solution2_first=true
# Enter your solution here







# + solution2="hidden"
visualize_s3_aod(aod_ocean=aod_ocean, 
                     aod_land=aod_land, 
                     latitude=lat_nc, 
                     longitude=lon_nc, 
                     title='Aerosol Optical Thickness at 550 nm', 
                     unit='~', 
                     vmin=0.,
                     vmax=1.0,
                     color_scale=cm.RdYlBu_r,
                     projection=ccrs.Mercator())
# -

# <br>

# <a href="../00_index.ipynb"><< Index</a><br>
# <a href="./422_Sentinel-3_SLSTR_NRT_FRP_L2_exercise.ipynb"><< 422 - Sentinel-3 SLSTR NRT - FRP - Level 2 - Exercise</a><span style="float:right;"><a href="./431_CAMS_EAC4_tcco_exercise.ipynb">431 - CAMS Global reanalysis (EAC4) - TCCO - Exercise >></a></span>

# <hr>

# <p><img src='../img/copernicus_logo.png' align='left' alt='Logo EU Copernicus' width='25%'></img></p>
# <br clear=left>
# <p style="text-align:left;">This project is licensed under the <a href="../LICENSE">MIT License</a> <span style="float:right;"><a href="https://gitlab.eumetsat.int/eumetlab/atmosphere/atmosphere">View on GitLab</a> | <a href="https://training.eumetsat.int/">EUMETSAT Training</a>

StarcoderdataJupyterScriptsDedupFiltered