Skip to content

Instantly share code, notes, and snippets.

@jgomezdans
Created April 28, 2022 14:56
Show Gist options
  • Save jgomezdans/f56f603734a42429002a5f0a0d3c503e to your computer and use it in GitHub Desktop.
Save jgomezdans/f56f603734a42429002a5f0a0d3c503e to your computer and use it in GitHub Desktop.
Meteogram for Tamale
import datetime as dt
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
"""
// Import image collection.
var era5_complete = ee.ImageCollection("ECMWF/ERA5_LAND/MONTHLY");
var era5_subset = era5_complete.filterDate('1990-01-01', '2021-01-01');
// #############################################################################
// ### Regional mean annual temperature time series chart ###
// #############################################################################
// Define an area of interest to calculate regional mean annual temperature for.
// Display AOI on map.
Map.centerObject(aoi, 10);
Map.addLayer(aoi, null, 'AOI');
// Map over the mean annual temperature collection - for each image calculate
// regional mean and return a feature. Result is a collection that can be
// plotted with ui.Chart.feature charts.
var regionalTemp = era5_subset.map(function(img) {
var regionReduce = img.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: aoi,
scale: 100});
return ee.Feature(aoi, regionReduce)
.set('Month', img.get('Month'));
});
Export.table.toDrive({
collection: regionalTemp,
description: 'monthly_clim_era5land',
fileFormat: 'CSV'
});
"""
date_parser=lambda x: dt.datetime.strptime(x, '%Y%m')
fich = "monthly_clim_era5land.csv"
df = pd.read_csv(fich, parse_dates=['system:index'],
date_parser=date_parser)
df["temperature_2m"] = df["temperature_2m"] - 273.15
df["total_precipitation"] = df["total_precipitation"] *1000/30.
df["surface_solar_radiation_downwards"] = df["surface_solar_radiation_downwards"]/1000.
xdf = df[["system:index", "temperature_2m",
"total_precipitation", "surface_solar_radiation_downwards"]
].groupby(df["system:index"].dt.month).aggregate(["mean", "std"]).reset_index(level=[0])
df_2021 = pd.read_csv("ERA5_Somewhere_9.40_-0.60_2021.csv", skiprows=7, parse_dates=["DAY"])
df_2020 = pd.read_csv("ERA5_Somewhere_9.40_-0.60_2020.csv", skiprows=7, parse_dates=["DAY"])
df_2021["TMEAN" ] = 0.5*(df_2021.TMAX + df_2021.TMIN)
df_2020["TMEAN" ] = 0.5*(df_2020.TMAX + df_2020.TMIN)
fig, axs = plt.subplots(nrows=3, ncols=1, sharex=True, facecolor=(1,1,1))
axs = axs.flatten()
parameters = ["temperature_2m", "total_precipitation",
"surface_solar_radiation_downwards"]
monthly_x = [dt.date(2021, i, 15) for i in range(1,13)]
for i,param in enumerate(parameters):
axs[i].plot(monthly_x, xdf[(param, "mean")], '-o')
axs[i].vlines(monthly_x, xdf[(param, "mean")] - xdf[(param, "std")],
xdf[(param, "mean")] + xdf[(param, "std")])
axs[0].plot(df_2021.DAY, df_2021.TMAX, '-', lw=0.5, label="Tmax 2021")
axs[0].plot(df_2021.DAY, df_2021.TMIN, '-', lw=0.5, label="Tmin 2021")
axs[0].plot(df_2021.DAY, df_2021.TMEAN, '-', lw=0.5, label="Tmean 2021")
axs2 = axs[1].twinx()
axs2.plot(df_2021.DAY, df_2021.RAIN, '-', lw=0.5, label="Precip 2021")
axs[2].plot(df_2021.DAY, df_2021.IRRAD, '-', lw=0.5, label="SW rad 2021")
axs[0].set_ylabel("2m temperature $[^{\circ}C]$", fontsize=10)
axs[1].set_ylabel("T. month. precip.\n $[mm\cdot month^{-1}]$", fontsize=10)
axs2.set_ylabel("T. daily precip.\n $[mm\cdot d^{-1}]$", fontsize=10)
axs[2].set_ylabel("Down. SW rad.\n$[KJ\cdot m^{-2}\cdot d^{-1}]$", fontsize=10)
axs[2].set_xlabel("Date")
axs[0].legend(loc="best", fontsize=8, frameon=False)
axs2.legend(loc="best", fontsize=8, frameon=False)
axs[2].legend(loc="best", fontsize=8, frameon=False)
plt.tight_layout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment