How to retrieve tco3 plots (jupyter)

This tutorial shows how to use a Jupyter notebook to retrieve data points of either tco3_zm plot or tco3_return plot in the JSON format and plot them. The Jupyter notebook can be downloaded from here.

System installations

If necessary, install required libraries or python packages

[1]:
### If needed, install additional modules:
#!pip3 install pandas
#!pip3 install matplotlib
### interactive plotting in jupyterlab requires node.js (pip does not install it!!):
#!apt update && apt install -y nodejs
#!pip3 install ipympl
#!jupyter labextension install @jupyter-widgets/jupyterlab-manager
#!jupyter labextension install jupyter-matplotlib
#!jupyter nbextension enable --py widgetsnbextension
### DON'T FORGET TO RESTART JupyterLab !!
# start jupyterlab:
# jupyter lab --ip=0.0.0.0

Import necessary packages

In this tutorial we need the following python packages:

json - to decode JSON data

matplotlib - to plot the data

numpy and pandas - to manipulate the data

os - to manipulate URL paths

requests - to communicate with the O3as API and retrieve data

[2]:
import json
import matplotlib.style as mplstyle
mplstyle.use('fast')
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import requests

Define global variables

we set a few variables: e.g. the base URL of O3AS API and the default figure size

[3]:
# for interactive plot, may change to widget, if installed
%matplotlib inline

# Set default size of plots
plt.rcParams['figure.figsize'] = [12, 8]

# A few variables needed for every REST API call:
url_o3api = "http://api.o3as.fedcloud.eu/api/v1/"  # base URL of the O3as API
headers = {'Content-Type': 'application/json',
           'Accept': 'application/json'}

# Define Reference Measurement and Reference Year
refMeasurement = 'SBUV_GSFC_merged-SAT-ozone'
refYear = 1980

Retrieve the list of models

We will analyse refC2 models, therefore we first request the list of corresponding models via the REST API

[4]:
# Use '/models' API Endpoint, i.e. append "models" to the base url_o3api:
url_o3api_tco3_zm_models = os.path.join(url_o3api, "models")

# request the list of modesl from O3as API, select "refC2" models:
tco3_zm_models = requests.request("GET",
                                  url=url_o3api_tco3_zm_models,
                                  params={'select': 'refc2'},
                                  headers=headers).json()
print(tco3_zm_models)
['CCMI-1_ACCESS_ACCESS-CCM-refC2', 'CCMI-1_CCCma_CMAM-refC2', 'CCMI-1_CESM1-CAM4Chem_refC2_r1i1p1', 'CCMI-1_CESM1-CAM4Chem_refC2_r2i1p1', 'CCMI-1_CESM1-CAM4Chem_refC2_r3i1p1', 'CCMI-1_CESM1-WACCM_refC2_r1i1p1', 'CCMI-1_CESM1-WACCM_refC2_r2i1p1', 'CCMI-1_CESM1-WACCM_refC2_r3i1p1', 'CCMI-1_CHASER-MIROC-ESM-refC2', 'CCMI-1_CNRM-CERFACS_CNRM-CM5-3-refC2', 'CCMI-1_CNRM-CERFACS_MOCAGE-refC2', 'CCMI-1_ETH-PMOD_SOCOL3-refC2', 'CCMI-1_GSFC_GEOSCCM-refC2', 'CCMI-1_MESSy_EMAC-L90MA-refC2', 'CCMI-1_MOHC_HadGEM3-ES-refC2', 'CCMI-1_MRI_ESM1r1-refC2', 'CCMI-1_NIES_CCSRNIES-MIROC3.2-refC2', 'CCMI-1_NIWA_NIWA-UKCA-refC2', 'CCMI-1_U-CAMBRIDGE_UMUKCA-UCAM-refC2', 'CCMI-1_U-LAQUILA_CCM-refC2', 'CCMI-1_U-LEEDS_UMSLIMCAT-refC2']

We also add a reference measurement, refMeasurement (defined above in the Global Variables, e.g. SBUV_GSFC_merged-SAT-ozone), to the list of models to be plotted

[5]:
tco3_zm_models.append(refMeasurement)

Request points of the tco3_zm plot

  1. we configure parameters of interest to get the plot. In the example we specify that we want a plot for:

  • Range of years: (1960, 2100)

  • month: September, October, November

  • South Hemisphere (SH, latitudes: -90, -60)

  • reference measurement: refMeasurement (e.g. SBUV_GSFC_merged-SAT-ozone)

  • reference year: refYear (e.g. 1980, see Global Variables)

[6]:
# initialize an empty dictionary
kwargs_tco3_zm = {}

# Build kwargs with tco3_zm parameters for O3as API.
# Keys have to correspond to expected by the API keys!

kwargs_tco3_zm = {
    'begin'   : 1960,
    'end'     : 2100,
    'month'   : '9,10,11',
    'lat_min' : -90,
    'lat_max' : -60,
    'ref_meas': refMeasurement,
    'ref_year': refYear
}
  1. We want to retrieve “tco3_zm” plot: we use ‘/plots’ endpoint, plot type ‘tco3_zm’, therefore we append ‘plots/tco3_zm’ to the base URL of the O3as API

  2. Then we retrieve tco3_zm plot data for the parameters of interest and the list of refC2 models defined above

[7]:
# Build the API URL, use /plots/tco3_zm endpoint:
url_o3api_plot_tco3_zm = os.path.join(url_o3api, "plots/tco3_zm")

# Request data
response = requests.request("POST",
                            url=url_o3api_plot_tco3_zm,
                            params=kwargs_tco3_zm,
                            headers=headers,
                            data=json.dumps(tco3_zm_models))
# print(response.request.url)  # uncomment if you like to see the API call
# print(response.request.body) # uncomment if you like to see the API call Request Body

# Read the status_code. Normal response => 200
print(response.status_code)
tco3_zm_data = response.json()
print(tco3_zm_data[:5])  # print first five for cross-checking
200
[{'legalinfo': 'https://o3as.data.kit.edu/policies/terms-of-use.html', 'model': 'CCMI-1_ACCESS_ACCESS-CCM-refC2', 'plotstyle': {'color': 'purple', 'linestyle': 'solid', 'marker': ''}, 'x': ['1960', '1961', '1962', '1963', '1964', '1965', '1966', '1967', '1968', '1969', '1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030', '2031', '2032', '2033', '2034', '2035', '2036', '2037', '2038', '2039', '2040', '2041', '2042', '2043', '2044', '2045', '2046', '2047', '2048', '2049', '2050', '2051', '2052', '2053', '2054', '2055', '2056', '2057', '2058', '2059', '2060', '2061', '2062', '2063', '2064', '2065', '2066', '2067', '2068', '2069', '2070', '2071', '2072', '2073', '2074', '2075', '2076', '2077', '2078', '2079', '2080', '2081', '2082', '2083', '2084', '2085', '2086', '2087', '2088', '2089', '2090', '2091', '2092', '2093', '2094', '2095', '2096', '2097', '2098', '2099', '2100'], 'y': [346.2776153564453, 346.2776153564453, 344.19089660644534, 345.2399810791016, 344.79651794433596, 343.2049926757813, 341.64759521484376, 341.9790252685547, 339.6935241699219, 339.7173004150391, 336.461865234375, 334.004965209961, 332.95016479492193, 330.1485961914063, 327.3143676757813, 326.07980957031253, 323.82362060546876, 320.05503234863284, 317.1924194335938, 314.53354187011723, 310.925, 307.60857543945315, 304.67973937988285, 302.41758117675784, 299.28678894042974, 295.69349060058596, 291.8769836425782, 287.95793762207035, 286.21589965820317, 281.78271789550786, 277.63332672119145, 273.80351562500005, 267.4904983520508, 261.4915878295899, 258.9700561523438, 255.55068511962895, 251.66219329833987, 248.30885620117192, 244.19300231933596, 243.56174621582034, 246.0753860473633, 243.7281616210938, 245.28644866943364, 247.0736755371094, 245.87856597900395, 245.76735992431645, 246.93642578125002, 248.34758758544925, 249.08704528808596, 247.95865173339848, 245.86597137451176, 247.63198699951175, 247.2372985839844, 250.01163787841801, 250.87575683593752, 251.80886383056645, 254.18242492675785, 253.67160949707034, 254.85982360839847, 256.2275970458985, 255.62308349609378, 257.060238647461, 258.2514282226563, 256.82307128906257, 257.3615539550782, 257.3642623901368, 256.9719482421875, 258.62582092285163, 258.0639465332032, 259.03634338378913, 261.9813705444336, 261.95432586669926, 263.4834625244141, 262.60323791503913, 264.1455886840821, 265.7035308837891, 266.6639846801758, 268.2940048217774, 271.5132202148438, 275.7468170166016, 279.5189178466797, 280.585482788086, 281.6898101806641, 284.5627624511719, 288.2847290039063, 290.4588958740235, 291.752035522461, 293.22461853027346, 297.2641906738282, 295.0239471435547, 296.0662506103516, 295.928125, 297.671859741211, 298.8583190917969, 298.3798034667969, 299.8877807617188, 301.73552856445315, 302.51728515625, 301.0359985351563, 303.35169677734376, 302.41975708007817, 305.3851501464844, 307.1610443115235, 308.31840515136724, 310.8793548583985, 311.1203918457032, 312.7027984619141, 312.83945007324223, 314.6934448242188, 317.3296325683594, 317.4012817382813, 318.46606750488286, 321.8202758789063, 323.72616577148443, 322.7880340576172, 325.6069763183594, 326.15849609375005, 330.8311218261719, 330.8529205322266, 328.95618896484376, 331.4684204101563, 331.71318359375005, 329.0909851074219, 331.5364959716797, 331.30587768554693, 330.81463317871095, 331.09461364746096, 329.43330078125, 330.2022674560547, 331.39458923339845, 331.2634979248047, 332.8045166015625, 334.02109680175784, 333.03064575195316, 334.6632781982422, 335.09021911621096, 334.4844055175782, 334.8290222167969, 335.1350799560547, 336.1991668701172, 336.1889068603516]}, {'legalinfo': 'https://o3as.data.kit.edu/policies/terms-of-use.html', 'model': 'CCMI-1_CCCma_CMAM-refC2', 'plotstyle': {'color': 'red', 'linestyle': 'solid', 'marker': ''}, 'x': ['1960', '1961', '1962', '1963', '1964', '1965', '1966', '1967', '1968', '1969', '1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030', '2031', '2032', '2033', '2034', '2035', '2036', '2037', '2038', '2039', '2040', '2041', '2042', '2043', '2044', '2045', '2046', '2047', '2048', '2049', '2050', '2051', '2052', '2053', '2054', '2055', '2056', '2057', '2058', '2059', '2060', '2061', '2062', '2063', '2064', '2065', '2066', '2067', '2068', '2069', '2070', '2071', '2072', '2073', '2074', '2075', '2076', '2077', '2078', '2079', '2080', '2081', '2082', '2083', '2084', '2085', '2086', '2087', '2088', '2089', '2090', '2091', '2092', '2093', '2094', '2095', '2096', '2097', '2098', '2099', '2100'], 'y': [351.86401214599607, 351.86401214599607, 351.35542449951174, 355.57737884521487, 354.23424530029297, 349.9771499633789, 356.3275833129883, 345.24928131103513, 344.44151458740237, 345.22654876708987, 346.2831344604492, 341.86380767822266, 342.0477890014648, 341.26673736572263, 339.75594024658204, 334.4859817504883, 326.0071304321289, 326.3966567993164, 321.5586196899414, 320.67137603759767, 310.925, 311.5148620605469, 307.7693115234375, 294.7178695678711, 292.1662796020508, 282.9051818847656, 275.12669982910154, 269.04498901367185, 259.7135833740234, 251.92035980224608, 256.55771484375, 248.9955078125, 238.1190200805664, 234.03816680908204, 224.7013168334961, 222.19364166259766, 220.26981506347656, 215.7968719482422, 219.48813018798828, 213.17967834472657, 207.5328384399414, 204.23307189941406, 207.8407974243164, 212.4969940185547, 212.18795318603514, 218.69286193847657, 221.2501998901367, 221.17481689453126, 221.1938903808594, 227.6576904296875, 225.0509796142578, 234.98327178955077, 234.98602294921875, 237.02869567871093, 238.43987426757812, 238.3752471923828, 236.18712615966797, 238.46255950927736, 240.05568084716796, 238.20476989746095, 238.92852935791015, 230.1319580078125, 230.97845153808595, 229.5811981201172, 231.7886001586914, 235.01458587646485, 238.0358139038086, 238.99495849609374, 239.9964614868164, 239.98670654296876, 240.03091430664062, 241.85446319580078, 242.5213394165039, 247.2451370239258, 246.31226196289063, 246.52247619628906, 246.8947525024414, 250.33602600097657, 246.45228424072266, 249.7035171508789, 260.76424102783204, 261.41114196777346, 264.77335968017576, 260.86240539550784, 269.04332427978517, 268.7472579956055, 274.6591110229492, 277.1489990234375, 282.8788528442383, 287.6033767700195, 279.7131683349609, 282.2228973388672, 282.45654296875, 289.9041122436523, 286.9389083862305, 284.4388595581055, 282.1350357055664, 278.26026153564453, 278.1993606567383, 279.877180480957, 282.81798095703124, 288.4872848510742, 292.3008804321289, 287.47315826416013, 289.4187515258789, 290.8007614135742, 291.95507659912107, 297.0407119750977, 298.56753997802736, 294.8441329956055, 292.26769866943357, 290.61450042724607, 288.0403823852539, 286.97046966552733, 286.7638961791992, 292.0441146850586, 294.35302276611327, 291.24938201904297, 293.5563430786133, 297.3076736450195, 303.9276718139648, 301.899592590332, 305.82518463134767, 311.614030456543, 310.26675567626955, 307.07715301513673, 306.7837661743164, 311.4630630493164, 311.95809478759764, 307.93372650146483, 307.5526840209961, 307.6130996704102, 303.91739959716796, 301.8792190551758, 306.58191070556643, 310.2178237915039, 313.8189407348633, 316.07857513427734, 316.5812423706055, 318.24852752685547, 315.255143737793]}, {'legalinfo': 'https://o3as.data.kit.edu/policies/terms-of-use.html', 'model': 'CCMI-1_CESM1-CAM4Chem_refC2_r1i1p1', 'plotstyle': {'color': 'darkorange', 'linestyle': 'dashed', 'marker': ''}, 'x': ['1960', '1961', '1962', '1963', '1964', '1965', '1966', '1967', '1968', '1969', '1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030', '2031', '2032', '2033', '2034', '2035', '2036', '2037', '2038', '2039', '2040', '2041', '2042', '2043', '2044', '2045', '2046', '2047', '2048', '2049', '2050', '2051', '2052', '2053', '2054', '2055', '2056', '2057', '2058', '2059', '2060', '2061', '2062', '2063', '2064', '2065', '2066', '2067', '2068', '2069', '2070', '2071', '2072', '2073', '2074', '2075', '2076', '2077', '2078', '2079', '2080', '2081', '2082', '2083', '2084', '2085', '2086', '2087', '2088', '2089', '2090', '2091', '2092', '2093', '2094', '2095', '2096', '2097', '2098', '2099', '2100'], 'y': [381.5714828491211, 381.5714828491211, 378.1163040161133, 376.7183303833008, 373.4070816040039, 373.9780899047852, 371.36494903564454, 374.6709915161133, 372.29052886962893, 368.84549713134766, 360.6159408569336, 357.20666656494143, 358.3512130737305, 353.1654281616211, 347.95308074951174, 343.93371734619143, 339.44506988525393, 332.83027496337894, 324.23018341064454, 316.62638092041016, 310.925, 309.10517578125, 295.56228790283205, 286.91331329345707, 282.9828369140625, 274.90110778808594, 268.45367126464845, 260.7116943359375, 255.36284942626955, 246.2650131225586, 240.69790496826172, 238.53372650146486, 236.68260955810547, 235.21825408935547, 232.4727493286133, 232.3526565551758, 228.981396484375, 229.0142776489258, 231.5090072631836, 233.72310180664064, 235.9229507446289, 239.2064651489258, 244.14745025634767, 243.71172943115235, 246.54490356445314, 244.62928771972656, 247.7969497680664, 246.57124481201174, 247.98087158203126, 249.27813110351565, 250.37484130859377, 240.65921478271486, 239.19200439453127, 238.1973861694336, 238.8849060058594, 241.07554931640627, 240.70039520263674, 241.2245315551758, 244.44255218505862, 250.5711639404297, 252.26995697021485, 255.21909790039064, 256.73286743164067, 260.87877502441404, 257.2423797607422, 256.60435638427737, 260.96625366210935, 265.16857147216797, 264.61548004150393, 261.60374450683594, 266.724592590332, 267.9991058349609, 267.2556259155274, 272.199365234375, 276.57763519287107, 279.6867080688477, 281.87621307373047, 280.0746719360352, 284.0301498413086, 286.6323913574219, 285.63535766601564, 289.1129653930664, 292.9555603027344, 290.94317321777345, 293.01483459472655, 294.6231369018555, 292.56831207275394, 296.7857070922852, 294.065885925293, 296.3155227661133, 296.8630111694336, 296.3875228881836, 297.7808303833008, 302.0857528686524, 303.6337173461914, 305.208317565918, 309.0577835083008, 311.7471145629883, 313.35450286865233, 315.879216003418, 315.09557342529297, 317.8024612426758, 318.81419219970707, 320.1314193725586, 321.8469467163086, 326.888151550293, 326.52049713134767, 325.1462661743164, 325.89070281982424, 324.04972381591796, 329.67862091064455, 330.1620834350586, 330.31318817138674, 328.13028717041016, 329.53011322021484, 326.86878509521483, 326.78084564208984, 330.5340835571289, 329.8897872924805, 334.3519912719727, 335.313688659668, 335.37488555908203, 334.43797454833987, 336.69957122802737, 339.33396759033207, 339.751188659668, 341.9354843139649, 340.37446441650394, 346.79536590576174, 344.0474655151367, 342.09180755615233, 343.31050262451174, 344.09497833251953, 342.79658355712894, 340.02554168701175, 340.67451324462894, 338.10611419677736, 337.42491302490237, 333.48226470947264, 333.1030715942383, 331.86889801025393]}, {'legalinfo': 'https://o3as.data.kit.edu/policies/terms-of-use.html', 'model': 'CCMI-1_CESM1-CAM4Chem_refC2_r2i1p1', 'plotstyle': {'color': 'darkorange', 'linestyle': 'dashed', 'marker': ''}, 'x': ['1960', '1961', '1962', '1963', '1964', '1965', '1966', '1967', '1968', '1969', '1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030', '2031', '2032', '2033', '2034', '2035', '2036', '2037', '2038', '2039', '2040', '2041', '2042', '2043', '2044', '2045', '2046', '2047', '2048', '2049', '2050', '2051', '2052', '2053', '2054', '2055', '2056', '2057', '2058', '2059', '2060', '2061', '2062', '2063', '2064', '2065', '2066', '2067', '2068', '2069', '2070', '2071', '2072', '2073', '2074', '2075', '2076', '2077', '2078', '2079', '2080', '2081', '2082', '2083', '2084', '2085', '2086', '2087', '2088', '2089', '2090', '2091', '2092', '2093', '2094', '2095', '2096', '2097', '2098', '2099', '2100'], 'y': [358.6359558105469, 358.6359558105469, 357.95749816894534, 358.44141235351566, 359.3844879150391, 359.0133544921875, 354.6981109619141, 353.3206817626953, 352.1751342773438, 350.15040283203126, 345.91631469726565, 342.6816680908203, 338.2875183105469, 333.2715118408203, 331.16810302734376, 327.3575012207031, 323.2553405761719, 321.2728179931641, 317.3791473388672, 309.56711425781253, 310.925, 309.24676818847655, 308.69524230957035, 309.64970703125, 304.7010070800781, 300.38481750488285, 300.2701385498047, 291.2830490112305, 280.99983673095704, 276.5548492431641, 266.0521423339844, 257.6220001220703, 252.01848449707032, 240.39391326904297, 231.2869857788086, 227.9185012817383, 221.7367172241211, 221.01917724609376, 223.24974060058594, 222.3900100708008, 223.3753387451172, 224.31804199218752, 222.1968185424805, 225.74407806396485, 227.56508789062502, 231.21707611083986, 231.67010650634768, 232.9335479736328, 235.1730728149414, 237.97268066406252, 237.90101165771486, 237.39969482421876, 238.73965606689455, 240.7696243286133, 243.3038299560547, 240.22039642333985, 240.6954299926758, 239.92261047363283, 242.710546875, 245.2356903076172, 246.5360336303711, 252.18057403564455, 253.35039672851565, 251.01851043701174, 253.4172805786133, 254.85916137695312, 256.3019271850586, 256.0974868774414, 255.30174102783204, 256.98720703125, 257.12850189208984, 258.35796508789065, 262.17308807373047, 268.0345748901367, 271.59240875244143, 274.1342208862305, 278.1360504150391, 284.0222351074219, 288.8552017211914, 292.12681732177737, 294.62740020751954, 298.9747817993164, 302.2896011352539, 303.68848114013673, 304.14416961669923, 306.8114929199219, 307.885986328125, 309.46814880371096, 306.3762573242188, 306.3796752929688, 308.3333374023438, 306.14597167968753, 304.1287475585938, 302.96336059570314, 304.9953887939453, 305.5959197998047, 308.2728546142578, 309.0998504638672, 313.55296325683594, 313.0412628173828, 314.37506103515625, 318.391796875, 318.3229461669922, 321.5603729248047, 321.32123107910155, 323.6040252685547, 322.73389892578126, 323.77886657714845, 326.3802795410156, 326.79912109375, 331.80604858398436, 328.8478546142578, 331.20375061035156, 331.2844146728516, 337.35638122558595, 337.2773895263672, 337.6394073486328, 341.7844207763672, 337.78045043945315, 341.5189453125, 342.05064086914064, 343.88139953613285, 346.0853668212891, 348.1514434814453, 343.58464660644535, 343.0266967773438, 345.3072845458984, 342.8584014892578, 346.1903106689453, 346.3204620361328, 344.6767913818359, 344.90953369140624, 343.5954986572266, 344.3072143554688, 342.5970825195313, 344.10809020996095, 343.57060852050785, 344.4231201171875, 342.29920349121096, 343.61365356445316, 342.4048767089844]}, {'legalinfo': 'https://o3as.data.kit.edu/policies/terms-of-use.html', 'model': 'CCMI-1_CESM1-CAM4Chem_refC2_r3i1p1', 'plotstyle': {'color': 'darkorange', 'linestyle': 'dashed', 'marker': ''}, 'x': ['1960', '1961', '1962', '1963', '1964', '1965', '1966', '1967', '1968', '1969', '1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030', '2031', '2032', '2033', '2034', '2035', '2036', '2037', '2038', '2039', '2040', '2041', '2042', '2043', '2044', '2045', '2046', '2047', '2048', '2049', '2050', '2051', '2052', '2053', '2054', '2055', '2056', '2057', '2058', '2059', '2060', '2061', '2062', '2063', '2064', '2065', '2066', '2067', '2068', '2069', '2070', '2071', '2072', '2073', '2074', '2075', '2076', '2077', '2078', '2079', '2080', '2081', '2082', '2083', '2084', '2085', '2086', '2087', '2088', '2089', '2090', '2091', '2092', '2093', '2094', '2095', '2096', '2097', '2098', '2099', '2100'], 'y': [365.4360290527344, 365.4360290527344, 366.3817901611328, 370.3562286376953, 370.0383636474609, 368.7543701171875, 363.65330505371094, 362.13975830078124, 362.7178039550781, 363.755908203125, 362.38750610351565, 360.49403991699216, 355.80242614746095, 350.2475311279297, 345.21844482421875, 343.0520751953125, 338.33714904785154, 333.865087890625, 327.39000244140624, 318.34383087158204, 310.925, 304.46538696289065, 299.0312774658203, 291.35950622558596, 288.34351806640626, 282.4365234375, 276.03794250488284, 271.8182373046875, 263.2386703491211, 262.28236083984376, 256.69469299316404, 252.03939971923828, 248.528076171875, 247.79768829345704, 240.75631256103514, 235.02481231689453, 230.83321533203124, 224.58261260986328, 226.68485412597656, 227.8892333984375, 230.0491973876953, 233.94886322021483, 235.38610076904297, 236.56617126464843, 239.3690414428711, 246.90138092041016, 253.57110748291015, 259.4573577880859, 258.7451919555664, 257.60504150390625, 257.0888076782227, 254.17039642333984, 255.1963134765625, 256.12268524169923, 259.8720077514648, 255.56302185058593, 260.1665542602539, 260.2986724853516, 264.5683395385742, 267.7353576660156, 270.566650390625, 276.5804504394531, 276.32234649658204, 276.3282241821289, 275.2374801635742, 278.22037506103516, 275.76917877197263, 277.9089553833008, 276.6732666015625, 277.34466552734375, 279.9870849609375, 277.62059631347654, 279.3583251953125, 280.8967742919922, 284.52840118408204, 285.99884643554685, 283.9988784790039, 286.9042510986328, 288.2472900390625, 288.2276809692383, 288.45092010498047, 290.03744354248045, 293.49230041503904, 294.37877502441404, 290.1622573852539, 289.8346633911133, 293.2915893554688, 294.0444580078125, 296.50605926513674, 298.96287231445314, 301.6950622558594, 304.6785858154297, 306.6046600341797, 308.448747253418, 310.9706726074219, 313.1002166748047, 312.72008056640624, 313.1872253417969, 314.9825103759766, 316.8722778320313, 316.04274597167966, 318.5152069091797, 318.5831359863281, 320.08416748046875, 324.6519805908203, 327.76747131347656, 330.9547088623047, 330.2920715332031, 331.4086059570312, 332.7617950439453, 334.48505249023435, 331.79441528320314, 333.61553649902345, 335.93740234375, 338.03192138671875, 339.3437164306641, 340.1692321777344, 345.2793029785156, 347.0900177001953, 346.4639862060547, 349.43590087890624, 349.12818298339846, 348.1135589599609, 346.2927307128906, 346.17779235839845, 342.52085266113284, 346.2075622558594, 343.29247131347654, 342.37383422851565, 343.1639038085938, 341.8174591064453, 342.67644958496095, 343.50169677734374, 346.4277709960937, 344.9670471191406, 347.34112548828125, 343.3644195556641, 343.538671875, 343.063525390625, 343.6079162597656, 343.7228485107422]}]
  1. The received JSON data are converted into pandas Dataframe for an easier data manipulation

[8]:
tco3_zm_pd = pd.json_normalize(tco3_zm_data)
  1. Finally, we plot the retrieved data:

[9]:
# set default linewidth:
linewidth_default = 2
tco3_zm_pd_plot = tco3_zm_pd.copy()

if not 'plotstyle.linewidth' in tco3_zm_pd_plot.columns:
  tco3_zm_pd_plot['plotstyle.linewidth'] = linewidth_default
else:
  tco3_zm_pd_plot['plotstyle.linewidth'] = tco3_zm_pd_plot['plotstyle.linewidth'].fillna(linewidth_default)

# plot every model
for index, c in tco3_zm_pd_plot.iterrows():
  plt.plot(pd.to_datetime(c['x']), c['y'], label=c['model'],
           color=c['plotstyle.color'], linestyle=c['plotstyle.linestyle'],
           marker=c['plotstyle.marker'], linewidth=c['plotstyle.linewidth'])

# let's fill the area of (MMMean +/- 1 Std)
tco3_zm_mmmean_minus_std_pd = tco3_zm_pd_plot[tco3_zm_pd_plot['model']=='MMMean-Std']
tco3_zm_mmmean_plus_std_pd = tco3_zm_pd_plot[tco3_zm_pd_plot['model']=='MMMean+Std']

plt.fill_between(pd.to_datetime(tco3_zm_mmmean_minus_std_pd['x'].iloc[0]),
                 tco3_zm_mmmean_minus_std_pd['y'].iloc[0],
                 tco3_zm_mmmean_plus_std_pd['y'].iloc[0],
                 color='g', alpha=0.2);

# tune the plot
# show the legend
ax = plt.gca() # get axis instance
ax.legend(bbox_to_anchor=(1.0, 1.0))

plt.xticks(fontsize=14)
plt.yticks(fontsize=14)

# put titels for axes
plt.xlabel('Year', fontsize='x-large')
plt.ylabel('tco3_zm (DU)', fontsize='x-large')
[9]:
Text(0, 0.5, 'tco3_zm (DU)')
../../_images/user_jupyter_o3api_plot_tco3_18_1.png

Request points of the tco3_return plot

  1. Similar to the previous task, we configure parameters of interest to get the tco3_return plot. In the example we specify that we want a plot for:

  • month: September, October, November

  • South Hemisphere (SH, latitudes: -90, -60)

  • reference measurement: refMeasurement (e.g. SBUV_GSFC_merged-SAT-ozone, see Global Variables)

  • reference year: refYear (e.g. 1980, see Global Variables)

  • we don’t provide a specific range of years, like in the previous example, but use all available data

[10]:
# initialize an empty dictionary
kwargs_tco3_return = {}

# Build kwargs with tco3_zm parameters for O3as API.
# Keys have to correspond to expected by the API keys!

kwargs_tco3_return = {
    'month'   : '9,10,11',
    'lat_min' : -90,
    'lat_max' : -60,
    'ref_meas': refMeasurement,
    'ref_year': refYear
}
  1. We want to retrieve “tco3_retrun” plot: we use ‘/plots’ endpoint, plot type ‘tco3_return’, therefore we append ‘plots/tco3_return’ to the base URL of the O3as API

  2. Then we retrieve tco3_retrun plot data for the parameters of interest and the list of refC2 models defined above (this time we exclude the reference measurement)

[11]:
# Build the API URL, use /plots/tco3_return endpoint:
url_o3api_plot_tco3_return = os.path.join(url_o3api, "plots/tco3_return")

# Request data
response = requests.request("POST",
                            url=url_o3api_plot_tco3_return,
                            params=kwargs_tco3_return,
                            headers=headers,
                            data=json.dumps(tco3_zm_models))
# Read the status_code. Normal response => 200
print(response.status_code)
tco3_return_data = response.json()
print(tco3_return_data[:5])  # print first five for cross-checking
200
[{'legalinfo': 'https://o3as.data.kit.edu/policies/terms-of-use.html', 'model': 'CCMI-1_ACCESS_ACCESS-CCM-refC2', 'plotstyle': {'color': 'purple', 'linestyle': 'none', 'marker': 'o'}, 'x': ['Antarctic(Oct)', 'SH mid-lat', 'Tropics', 'NH mid-lat', 'Arctic(Mar)', 'Near global', 'Global', 'User region'], 'y': [2064, 2051, 2060, 2041, 2040, 2049, 2052, 2065]}, {'legalinfo': 'https://o3as.data.kit.edu/policies/terms-of-use.html', 'model': 'CCMI-1_CCCma_CMAM-refC2', 'plotstyle': {'color': 'red', 'linestyle': 'none', 'marker': 'x'}, 'x': ['Antarctic(Oct)', 'SH mid-lat', 'NH mid-lat', 'Arctic(Mar)', 'Near global', 'Global', 'User region'], 'y': [2087.0, 2049.0, 1986.0, 1986.0, 2045.0, 2048.0, 2083.0]}, {'legalinfo': 'https://o3as.data.kit.edu/policies/terms-of-use.html', 'model': 'CCMI-1_CESM1-CAM4Chem_refC2_r1i1p1', 'plotstyle': {'color': 'darkorange', 'linestyle': 'none', 'marker': 'd'}, 'x': ['Antarctic(Oct)', 'SH mid-lat', 'Tropics', 'NH mid-lat', 'Arctic(Mar)', 'Near global', 'Global', 'User region'], 'y': [2056, 2049, 2026, 1987, 2009, 2026, 2038, 2057]}, {'legalinfo': 'https://o3as.data.kit.edu/policies/terms-of-use.html', 'model': 'CCMI-1_CESM1-CAM4Chem_refC2_r2i1p1', 'plotstyle': {'color': 'darkorange', 'linestyle': 'none', 'marker': 'd'}, 'x': ['Antarctic(Oct)', 'SH mid-lat', 'Tropics', 'NH mid-lat', 'Arctic(Mar)', 'Near global', 'Global', 'User region'], 'y': [2057, 2038, 2022, 2006, 2017, 2026, 2036, 2058]}, {'legalinfo': 'https://o3as.data.kit.edu/policies/terms-of-use.html', 'model': 'CCMI-1_CESM1-CAM4Chem_refC2_r3i1p1', 'plotstyle': {'color': 'darkorange', 'linestyle': 'none', 'marker': 'd'}, 'x': ['Antarctic(Oct)', 'SH mid-lat', 'Tropics', 'NH mid-lat', 'Arctic(Mar)', 'Near global', 'Global', 'User region'], 'y': [2055, 2037, 2025, 2002, 2020, 2022, 2034, 2054]}]
  1. The received JSON data can be converted into pandas Dataframe for an easier data manipulation

[12]:
tco3_return_pd = pd.json_normalize(tco3_return_data)
  1. Finally, we plot the retrieved data:

[13]:
# set default markersize:
markersize_default = 6
tco3_return_pd_plot = tco3_return_pd.copy()

if not 'plotstyle.markersize' in tco3_return_pd_plot.columns:
  tco3_return_pd_plot['plotstyle.markersize'] = markersize_default
else:
  tco3_return_pd_plot['plotstyle.markersize'] = tco3_return_pd_plot['plotstyle.markersize'].fillna(markersize_default)

# plot every model
for index, c in tco3_return_pd_plot.iterrows():
  plt.plot(c['x'], c['y'], label=c['model'],
           color=c['plotstyle.color'], linestyle=c['plotstyle.linestyle'],
           marker=c['plotstyle.marker'], markersize=c['plotstyle.markersize'])

# show the legend
ax = plt.gca() # get axis instance
ax.legend(bbox_to_anchor=(1.0, 1.0))

# put titels for axes
plt.xlabel('Region', fontsize='x-large')
plt.ylabel('tco3_zm (DU)', fontsize='x-large')
[13]:
Text(0, 0.5, 'tco3_zm (DU)')
../../_images/user_jupyter_o3api_plot_tco3_26_1.png