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 / example.py
Created October 10, 2025 18:56
Example using h3 grid
import marimo
__generated_with = "0.16.5"
app = marimo.App(width="medium")
@app.cell
def _(mo):
mo.md(
r"""
library(tidyverse)
# ERA5
e5 = read_csv('reanalysis-era5-single-levels-timeseries-sfc9vc2xo5f.csv') |>
mutate(
t2m = t2m - 273.15
) |>
group_by(
valid_time = floor_date(valid_time, unit = "days")
) |>
summarise(
@matteodefelice
matteodefelice / frost_days_planetary.py
Created May 1, 2025 20:32
Frost days with planetary
# %% [markdown]
# https://planetarycomputer.microsoft.com/dataset/cil-gdpcir-cc-by#overview
# %%
# required to locate and authenticate with the stac collection
import planetary_computer
import pystac_client
# required to load a zarr array using xarray
import xarray as xr
@matteodefelice
matteodefelice / mask_county.py
Created March 28, 2025 20:35
mask xarray using regionmask
# %%
import xarray as xr
import numpy as np
import geopandas as gpd
import regionmask
# %%
gd = xr.open_dataset("C:/Users/matte/data/tasmax_day_ACCESS-CM2_historical_r1i1p1f1_gn_1999.nc")
# gd.coords['lon'] = (gd.coords['lon'] + 180) % 360 - 180
# gd = gd.sortby(gd.lon)
# %%
library(tidyverse)
library(countrycode)
library(scico)
library(patchwork)
library(ggtext)
mapdata <- map_data("world") |>
mutate(iso3_code = countrycode(region, "country.name", "iso3c"))
@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(