Skip to content

Instantly share code, notes, and snippets.

View aulemahal's full-sized avatar

Pascal Bourgault aulemahal

View GitHub Profile
@aulemahal
aulemahal / dev-todo.sh
Created August 10, 2023 19:00
Proposition de traducteur
cd xscen/
pygettext.py -o xscen/data/messages.pot xscen/*.py
cp xscen/data/messages.pot xscen/data/fr
# edit xscen/data/fr/LC_MESSAGES/xscen.po
msgfmt.py xscen/data/*/LC_MESSAGES/xscen.po
git commit
@aulemahal
aulemahal / Local_Kerchunk_Benchmark.py
Created February 24, 2023 21:41
Very naive benchmarking for using kerchunk locally
import sys
import dask
from dask.diagnostics import ProgressBar
import time
import xarray as xr
import glob
import fsspec
# This was last run on commit f42b0c23aa0229cee6e5a394a860f0ad1149ace6
@aulemahal
aulemahal / creep_fill.py
Created February 9, 2023 21:29
Xarray creep fill
# Creep fill : fill missing points with the mean of the neighbors.
#
# The process here implemented first create weights and then computes the fill by a dot product, in a fashion similar to xESMF.
# It uses a sparse matrix to stores those weights. This enables the reuse of a creep filling mask and faster parallelization over non-creeped dims.
#
import itertools
import numpy as np
import sparse as sp
import xarray as xr
@aulemahal
aulemahal / cellarea.py
Last active March 7, 2023 21:04
Compute cell area on curvilinear grids
import pygeos # I'm not sure why, but it seems this must be imported before one of the other packages below
import xarray as xr
import cf_xarray as cfxr
from shapely.geometry import Polygon
import shapely
import ESMF
import xesmf as xe
import numpy as np
ds = xr.open_dataset('caw/series/200109/rhsmax_caw_200109_se.nc') # some 2D grid data
@aulemahal
aulemahal / update_xclim_yaml.py
Last active September 24, 2021 17:46
Adapt old xclim yamls to new structure.
# This function creates a xclim0.30-compliant yaml file from a pre-0.30 version.
import os
from yaml import safe_load, safe_dump
def update_yaml(raw: os.PathLike, adapted: os.PathLike):
"""Reads in a virtual module yaml that was created for xclim <= 0.29 and refactors it to fit xclim's current yaml specifications."""
freq_names = {"annual": "A", "seasonal": "Q", "monthly": "M", "weekly": "W"}
freq_defs = {"annual": "YS", "seasonal": "QS-DEC", "monthly": "MS", "weekly": "W"}
with open(raw) as f:
@aulemahal
aulemahal / gpd_to_cfxr.py
Created February 5, 2021 20:36
Convert geopandas (geojson) to CF-compliant datasets
"""
Convert a geopandas dataframe to a CF compliant dataset.
Follows conventions as noted here : http://cfconventions.org/Data/cf-conventions/cf-conventions-1.8/cf-conventions.html#geometries
Author: Pascal Bourgault
Date: 2021-02-05
"""
import geopandas as gpd
from numpy import arange
@aulemahal
aulemahal / grid_weights_from_poly.py
Last active June 1, 2023 00:41
Spatial intersection of an xarray defined grid and a shapely polygon, using pyproj to convert to an equal earth projection for more accuracy. With tools to get the grid corners and rotated_pole projections.
# Spatial intersection and average
# Compute weights corresponding to the intersection between grid cells and a polygon.
# Pascal bourgault, Sept 2020
from cartopy import crs
from shapely.geometry import Polygon
from shapely.ops import transform
import xarray as xr
import numpy as np
from functools import partial
import pyproj