Skip to content

Instantly share code, notes, and snippets.

@dennissergeev
dennissergeev / jlab_shortcuts.json
Last active November 14, 2022 19:50
Useful JupyterLab shortcuts
{
"shortcuts":[
{
"args": {},
"command": "notebook:restart-and-run-to-selected",
"keys": [
"Ctrl Q", "Ctrl Q"
],
"selector": ".jp-Notebook:focus"
},
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.offsetbox import AnchoredText
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
# Set extent of the map
@dennissergeev
dennissergeev / extract_by_time.py
Last active March 5, 2024 18:35
Extract by time in iris
"""Extract a cube slice using a time constraint."""
from datetime import datetime
import iris
# The cube with a time dimension
cube = mycubelist.extract_cube("some_variable")
# For example, we want to extract the time slice at 2020-04-29 12:00 model time,
# which corresponds to a time index of 123.
@dennissergeev
dennissergeev / UM-Output-Iris.ipynb
Last active March 20, 2022 11:00
Getting started with Unified Model output analysis using iris.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/usr/bin/env python
import moviepy.editor as mpe
import sys
from pathlib import Path
fname = Path(sys.argv[1])
v = mpe.VideoFileClip(str(fname))
v.write_gif(fname.with_suffix(".gif"))
@dennissergeev
dennissergeev / download_era5_example.py
Last active August 23, 2019 12:53
Sample script to download ERA5
import cdsapi
c = cdsapi.Client()
c.retrieve(
'reanalysis-era5-single-levels',
{
'product_type':'reanalysis',
'format':'netcdf',
# Select area: lat_n/lon_w/lat_s/lon_e in degrees
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import pyvista as pv
import numpy as np
arr = np.random.rand(40, 72, 144)
grid = pv.UniformGrid()
grid.dimensions = np.array(arr.shape) + 1
grid.origin = (0, 0, 0) # The bottom left corner of the data set
@dennissergeev
dennissergeev / roll_xarray_longitude.py
Created June 20, 2019 14:40
Functions for rolling xarray DataArray along its longitudinal axis
def roll_da_to0360(da, lon_name='longitude'):
"""Roll DataArray's data with longitudes from (-180, 180) to (0, 360)."""
# Roll longitudes and corresponding data
out = da.roll(longitude=da[lon_name].shape[0]//2, roll_coords=True)
# Reset western (negative) longitudes to values within (180, 360) range
out[lon_name] = out[lon_name] - (out[lon_name] // 360) * 360
return out
@dennissergeev
dennissergeev / xgrid_utils.py
Last active May 10, 2024 10:00
Calculate a spatial mean of xarray's DataArray
# -*- coding: utf-8 -*-
"""Operations on cartesian geographical grid."""
import numpy as np
EARTH_RADIUS = 6371000.0 # m
def _guess_bounds(points, bound_position=0.5):
"""
Guess bounds of grid cells.