Skip to content

Instantly share code, notes, and snippets.

View matteodefelice's full-sized avatar
💭
Busy connecting things...

Matteo De Felice matteodefelice

💭
Busy connecting things...
View GitHub Profile
@matteodefelice
matteodefelice / enso.py
Last active February 13, 2024 20:23
ENSO anomaly
# %%
import xarray as xr
import pandas as pd
# %%
df = (
xr.open_mfdataset("C:/Users/matte/data/CPC/preci*")
.resample(time = "MS").sum()
.load()
)
# %%
library(lubridate)
library(tidyverse)
library(zoo)
read_station <- function(filename) {
read_csv(filename,
col_types = cols(DATE = col_datetime(format = "%Y%m%d")),
skip = 20
)
}
station_files <- c(
color_palette = c(
'Biomass' = '#7daf4bff',
'Coal' = '#af4b91b2',
'Gas' = '#d7642dff',
'Hydro-power' = '#00a0e1ff',
'Nuclear' = '#466eb4ff',
'Oil' = '#af4b91ff',
'Other' = '#b9c33799',
'Solar' = '#e6a532ff',
@matteodefelice
matteodefelice / essential_climate_variables_animation.R
Created April 3, 2020 12:43 — forked from khufkens/essential_climate_variables_animation.R
Downloads and animates CDS essential climate variables
# Before you start install all the referenced packages below
# including the development release of ecmwfr you will also
# need the rnaturalearthdata in addition to the normal package
# To run the code copy and paste segments as this is still
# rough around the edges and requires user interaction to
# set your CDS API key.
# load libraries
if(!require(devtools)){install.packages("devtools")}
devtools::install_github("khufkens/ecmwfr")
library(tidyverse)
df <- read_csv("ItalianCovidData/Daily Covis19 Italian Data Cumulative") %>%
select(1:5) %>%
gather(type, value, -Date, -Region) %>%
group_by(Date, Region)
total <- df %>%
dplyr::filter(Date == max(df$Date)) %>%
group_by(
library(tidyverse)
library(eurostat)
library(lubridate)
n_veih <- get_eurostat("tran_r_vehst") %>% # https://appsso.eurostat.ec.europa.eu/nui/show.do?dataset=tran_r_vehst&lang=en
dplyr::filter(
str_length(geo) == 2,
unit == "P_THAB", # NR: Number, P_THAB: Per thousand inhabitants
vehicle == "CAR" # Passenger cars
)
@matteodefelice
matteodefelice / efas_extract.py
Created October 26, 2019 19:58
Python code to extract river discharge on a specific coordinate from C3S EFAS
import xarray as xr
import numpy as np
import pandas as pd
from pyproj import Proj, transform
import os.path
# Data for projections
# CHECK WITH https://epsg.io/transform#s_srs=4326&t_srs=3035&x=17.6830006&y=48.4970017
inProj = Proj(init='epsg:4326')
outProj = Proj(init='epsg:3035')
@matteodefelice
matteodefelice / poor_man_correlation.R
Created April 15, 2019 06:58
Poor man's correlation between two 3D arrays in R
library(ncdf4)
# files are 229x161x8760
FILE_1 = nc_open('/emhires-data/data-warehouse/gridded/ERA5/era5-hourly-100m_v_component_of_wind-2018.europe.nc')
FILE_2 = nc_open('/emhires-data/data-warehouse/gridded/ERA5/era5-hourly-10m_v_component_of_wind-2018.europe.nc')
lat = ncvar_get(FILE_1, 'latitude')
lon = ncvar_get(FILE_1, 'longitude')
data_1 = ncvar_get(FILE_1, 'v100') # 2.5 GB in memory
@matteodefelice
matteodefelice / era5_download_cds.py
Created February 22, 2019 14:23
A script to download ERA5 entire dataset for a set of selected surface variables.
import cdsapi
import os
c = cdsapi.Client()
VARS = ['100m_u_component_of_wind','100m_v_component_of_wind','10m_u_component_of_wind', '10m_v_component_of_wind', '2m_temperature','surface_solar_radiation_downward_clear_sky','surface_solar_radiation_downwards', '2m_dewpoint_temperature','mean_sea_level_pressure']
YEARS = [x for x in map(str, range(1979, 2019))]
for V in VARS:
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.