credit.grid#

Generate SCRIP-format NetCDF files for use with ESMFRegridWeightGen.

Supports two grid types:
  • Rectilinear : 1D lon + 1D lat arrays (uniform, regional, or Gaussian spacing)

  • Curvilinear : 2D lon + 2D lat arrays (HRRR, GOES, ocean tripolar, etc.)

All functions produce SCRIP files containing both cell centers and cell corners, making them compatible with all ESMFRegridWeightGen methods (bilinear, patch, nearest-neighbor, 1st- and 2nd-order conservative).

Entry points#

scrip_from_netcdf(nc_file, scrip_file, grid_name=None, mask_var=None)

Auto-detects grid type from file; preferred entry point.

scrip_from_rectilinear(lon_1d, lat_1d, grid_name, grid_file, mask=None) scrip_from_curvilinear(lon_2d, lat_2d, grid_name, grid_file, mask=None)

Direct API for when coordinates are already in hand.

Dependencies#

numpy, xarray

Attributes#

Functions#

_write_scrip(lon_centers, lat_centers, lon_corners, ...)

Write a SCRIP-format NetCDF file from pre-computed center and corner arrays.

_corners_rectilinear(lons_1d, lats_1d)

Compute cell corners for a rectilinear grid from 1D center arrays.

_corners_curvilinear(lons_2d, lats_2d)

Compute cell corners for a curvilinear grid from 2D center arrays.

_find_coord_pair(ds)

Find a lon/lat coordinate pair in an xr.Dataset.

scrip_from_rectilinear(lon_1d, lat_1d, grid_name, ...)

Generate a SCRIP file for a rectilinear (regular lat/lon) grid.

scrip_from_curvilinear(lon_2d, lat_2d, grid_name, ...)

Generate a SCRIP file for a curvilinear grid.

scrip_from_netcdf(nc_file, scrip_file[, grid_name, ...])

Auto-detect grid type from a NetCDF file and write a SCRIP file.

Module Contents#

credit.grid._write_scrip(lon_centers, lat_centers, lon_corners, lat_corners, grid_name, grid_file, mask=None, grid_dims=None)#

Write a SCRIP-format NetCDF file from pre-computed center and corner arrays.

Parameters:
  • lon_centers (np.ndarray, shape (grid_size,))

  • lat_centers (np.ndarray, shape (grid_size,))

  • lon_corners (np.ndarray, shape (grid_size, 4)) – Corner longitudes in CCW order: SW, SE, NE, NW.

  • lat_corners (np.ndarray, shape (grid_size, 4)) – Corner latitudes in CCW order: SW, SE, NE, NW.

  • grid_name (str)

  • grid_file (str)

  • mask (np.ndarray of int32, shape (grid_size,), optional) – 1 = valid, 0 = masked. Defaults to all-ones (fully unmasked).

Return type:

xr.Dataset written to grid_file.

credit.grid._corners_rectilinear(lons_1d, lats_1d)#

Compute cell corners for a rectilinear grid from 1D center arrays.

Edges are midpoints between adjacent centers. Boundary edges are extrapolated using the nearest interior spacing. Latitude edges are clamped to [-90, 90]. Longitude edges are kept monotonically increasing (not wrapped) so that west < east is guaranteed for every cell.

Returns:

lon_corners, lat_corners – CCW order: SW, SE, NE, NW.

Return type:

np.ndarray, shape (nlat*nlon, 4)

credit.grid._corners_curvilinear(lons_2d, lats_2d)#

Compute cell corners for a curvilinear grid from 2D center arrays.

Interior corners are the average of the four surrounding cell centers. Boundary corners are extrapolated using a one-cell ghost layer.

Returns:

lon_corners, lat_corners – CCW order: SW, SE, NE, NW.

Return type:

np.ndarray, shape (ny*nx, 4)

credit.grid._COORD_CANDIDATES = [('longitude', 'latitude'), ('lon', 'lat')]#
credit.grid._find_coord_pair(ds)#

Find a lon/lat coordinate pair in an xr.Dataset.

Searches _COORD_CANDIDATES in order across both ds.coords and ds.data_vars.

Return type:

(lon_array, lat_array, lon_name, lat_name)

Raises:

ValueError if no recognised pair is found.

credit.grid.scrip_from_rectilinear(lon_1d, lat_1d, grid_name, grid_file, mask=None)#

Generate a SCRIP file for a rectilinear (regular lat/lon) grid.

Parameters:
  • lon_1d (array-like, shape (nlon,))

  • lat_1d (array-like, shape (nlat,))

  • grid_name (str)

  • grid_file (str)

  • mask (array-like, shape (nlat, nlon), optional 1=valid, 0=masked.)

Return type:

xr.Dataset

credit.grid.scrip_from_curvilinear(lon_2d, lat_2d, grid_name, grid_file, mask=None)#

Generate a SCRIP file for a curvilinear grid.

Parameters:
  • lon_2d (array-like, shape (ny, nx))

  • lat_2d (array-like, shape (ny, nx))

  • grid_name (str)

  • grid_file (str)

  • mask (array-like, shape (ny, nx), optional 1=valid, 0=masked.)

Return type:

xr.Dataset

credit.grid.scrip_from_netcdf(nc_file, scrip_file, grid_name=None, mask_var=None)#

Auto-detect grid type from a NetCDF file and write a SCRIP file.

Coordinate names supported: lon/lat, longitude/latitude. 1D coordinates → rectilinear. 2D coordinates → curvilinear.

Parameters:
  • nc_file (str Path to input NetCDF file.)

  • scrip_file (str Path for output SCRIP file.)

  • grid_name (str, optional Defaults to the input filename stem.)

  • mask_var (str, optional Variable to use as mask (1=valid, 0=masked).)

Return type:

xr.Dataset

credit.grid.parser#