Last active
June 8, 2020 15:01
-
-
Save kaedonkers/439085b58a94c649de9dfc0b114ca026 to your computer and use it in GitHub Desktop.
Minimal amount of code to regrid UKV data to OSGB grid, keeping native grid size of original UKV data. Based on Gist by @DPeterK https://gist.github.com/DPeterK/c5061f336a91a3ce9790c206a5459b4a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (C) Crown Copyright, Met Office. All rights reserved. 2020 | |
# | |
# Python code written for demonstrative purposes only. | |
# Authored by Kevin Donkers (@kaedonkers) and Peter Killick (@DPeterK) | |
# See https://gist.github.com/DPeterK/c5061f336a91a3ce9790c206a5459b4a | |
import iris | |
import numpy as np | |
import cartopy.crs as ccrs | |
# --- UKV data --- | |
fname_ukv = './ukv_daily_t1o5m_mean_20200101.nc' | |
cube_ukv = iris.load_cube(fname_ukv) | |
crs_ukv = cube_ukv.coord_system().as_cartopy_crs() | |
# --- Subset UKV to extent of OSGB --- | |
osgb = ccrs.OSGB() | |
uk_extent_bl = crs_ukv.transform_point(osgb.x_limits[0], osgb.y_limits[0], osgb) | |
uk_extent_tr = crs_ukv.transform_point(osgb.x_limits[1], osgb.y_limits[1], osgb) | |
cube_ukv_uk = cube_ukv.intersection(grid_latitude=[uk_extent_bl[1], uk_extent_tr[1]], | |
grid_longitude=[uk_extent_bl[0], uk_extent_tr[0]]) | |
# --- OSGB grid cube --- | |
# Here is where you can change the grid spacing to your liking | |
ny, nx = cube_ukv_uk.shape | |
y_points = np.linspace(osgb.y_limits[0], osgb.y_limits[1], ny) | |
x_points = np.linspace(osgb.x_limits[0], osgb.x_limits[1], nx) | |
# Construct dimension coordinates | |
x_coord = iris.coords.DimCoord(x_points, | |
standard_name="projection_x_coordinate", | |
units='m', | |
coord_system=iris.coord_systems.OSGB()) | |
y_coord = iris.coords.DimCoord(y_points, | |
standard_name="projection_y_coordinate", | |
units='m', | |
coord_system=iris.coord_systems.OSGB()) | |
# Mapping of coordinates to data dims. | |
dcad = [(y_coord, 0), (x_coord, 1)] | |
# Make cube with data array of zeros | |
data = np.zeros(cube_ukv_uk.shape) | |
grid_cube = iris.cube.Cube(data, | |
units="1", | |
long_name="grid cube", | |
dim_coords_and_dims=dcad) | |
grid_cube.coord_system = iris.coord_systems.OSGB() | |
grid_cube | |
# --- Regrid UKV to OSGB --- | |
regrid_scheme = iris.analysis.Linear(extrapolation_mode='mask') | |
ukv_osgb = cube_ukv_uk.regrid(grid_cube, regrid_scheme) | |
# Save regridded cube to another NetCDF file | |
iris.save(ukv_osgb, 'ukv_osgb_outputfile.nc') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment