Skip to content

Instantly share code, notes, and snippets.

@dennissergeev
dennissergeev / Masked_Arrays_Gradient_MetPy.ipynb
Created May 16, 2018 09:28
Masked_Arrays_Gradient_MetPy.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import pandas as pd
import matplotlib.pyplot as plt
url = 'http://svo2.cab.inta-csic.es/theory/newov/ssap.php?model=bt-settl&fid=752&format=ascii'
df = pd.read_csv(url, skiprows=9, names=['wavelength', 'flux'], sep='\t', index_col=False,
skipfooter=1, engine='python', dtype={'wavelength': float, 'flux': float})
df['wavelength'] *= 1e-4
fig, ax = plt.subplots()
@dennissergeev
dennissergeev / roll_cube.py
Last active December 7, 2018 21:16
Takes a cube which goes longitude 0-360 back to -180-180.
def roll_cube_e2w(cube_in, inplace=False):
"""Takes a cube which goes longitude 0-360 back to -180-180."""
if inplace:
cube = cube_in
else:
cube = cube_in.copy()
lon = cube.coord('longitude')
if (lon.points >= 0.).all():
if (lon.points <= 360.).all():
subtract = -180.
def label_global_map(fig, ax, xticks, yticks, xoff=-10, yoff=-10, degree=False, **text_kw):
"""Label gridlines of a global cartopy map."""
geodetic_trans = ccrs.Geodetic()
xlab_kw = ylab_kw = dict(va="center", ha="center", **text_kw)
for xtick in xticks:
s = format_lonlat(xtick, "lon", degree=degree)
text_transform = offset_copy(
geodetic_trans._as_mpl_transform(ax), fig=fig, units="points", x=0, y=yoff
)
ax.text(xtick, -90, s, transform=text_transform, **xlab_kw)
@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.
@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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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