Skip to content

Instantly share code, notes, and snippets.

View raphaeldussin's full-sized avatar

raphael dussin raphaeldussin

View GitHub Profile

Download the fonts from https://github.com/stipub/stixfonts

dirstix = "/net2/rnd/dev/stixfonts/fonts/static_ttf"
import matplotlib.font_manager as font_manager
for font in font_manager.findSystemFonts(dirstix):
    font_manager.fontManager.addfont(font)

# Set font family globally
plt.rcParams['font.family'] = 'STIX Two Text'
name: meatpotatoes
channels:
- conda-forge
dependencies:
- python
- pip
- black
- cartopy
- cftime
- cmocean
@raphaeldussin
raphaeldussin / movie.py
Created March 23, 2023 22:30
rotating globe movie for surface speed
import xarray as xr
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import matplotlib.image as image
import cartopy.crs as ccrs
from owslib.wms import WebMapService
from matplotlib import cm
import os
from xgcm import Grid
@raphaeldussin
raphaeldussin / plot_temp100m_xarray_ROMS.py
Last active February 8, 2022 18:07
interpolate and plot temperature in ROMS
import cmocean
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
# extract temperature and depth for a given time
snapshot = CCS2.isel(time=50)
temp_snapshot = snapshot['temp']
depth_snapshot = snapshot['z_rho']
# compute temperature at 100 meters depth using xgcm.transform
@raphaeldussin
raphaeldussin / plot_section_xarray_ROMS.py
Last active December 8, 2020 01:18
plotting a ROMS velocity section with xarray
import matplotlib.pyplot as plt
import cmocean
plt.figure(figsize=[10,8])
CCS2['v'].isel(time=12, yq=500).plot(x='lon_v', y='z_v',
vmin=-0.25, vmax=0.25,
cmap=cmocean.cm.balance,
subplot_kws={'facecolor': 'grey'})
@raphaeldussin
raphaeldussin / compute_depth_ROMS.py
Last active December 8, 2020 00:01
compute depth of ROMS layers
def compute_depth_layers(ds, grid, hmin=0.1):
""" compute depths of ROMS vertical levels (Vtransform = 2) """
# compute vertical transformation functional
S_rho = (ds.hc * ds.s_rho + ds.Cs_r * ds.h) / (ds.hc + ds.h)
S_w = (ds.hc * ds.s_w + ds.Cs_w * ds.h) / (ds.hc + ds.h)
# compute depth of rho (layers) and w (interfaces) points
z_rho = ds.zeta + (ds.zeta + ds.h) * S_rho
z_w = ds.zeta + (ds.zeta + ds.h) * S_w
@raphaeldussin
raphaeldussin / open_zarr_store_ROMS.py
Last active December 8, 2020 22:06
opening a ROMS zarr store
import xarray as xr
from xgcm import Grid
CCS2 = xr.open_zarr(CCS2_store, consolidated=True)
# Create xgcm grid object
grid_ccs2 = Grid(CCS2, coords={'X': {'center': 'xh', 'outer': 'xq'},
'Y': {'center': 'yh', 'outer': 'yq'},
'Z': {'center': 's_rho', 'outer': 's_w'}},
periodic=False)
@raphaeldussin
raphaeldussin / main_roms2zarr.py
Created December 3, 2020 02:45
main code for roms2zarr conversion
import xarray as xr
import glob
# use glob to create a list of files to convert
filelist = glob.glob('data/CCS2_avg_*.nc')
# path to grid file
gridfile = 'grid/CCS2_grid.nc'
# output store name
store_name = 'CCS2_store'
@raphaeldussin
raphaeldussin / func4_roms2zarr.py
Created December 3, 2020 02:33
function for roms2zarr tutorial
def add_coords(ds):
""" set coordinate variables as xarray coordinates
Parameters:
ds (xarray.Dataset): ROMS dataset
"""
ds = ds.set_coords(['Cs_r', 'Cs_w', 'hc', 'h', 'Vtransform', 'time',
'lon_rho', 'lon_v', 'lon_u', 'lon_psi',
'lat_rho', 'lat_v', 'lat_u', 'lat_psi'])
return ds
@raphaeldussin
raphaeldussin / func3_roms2zarr.py
Created December 3, 2020 02:26
function for roms2zarr tutorial
def rename_dims(ds):
""" rename dimensions
Parameters:
ds (xarray.Dataset): ROMS dataset
"""
ds = ds.rename({'xi_rho': 'xh', 'xi_v': 'xh', 'xi_u': 'xq', 'xi_psi': 'xq',
'eta_rho': 'yh', 'eta_v': 'yq', 'eta_u': 'yh', 'eta_psi': 'yq',
'ocean_time': 'time'