src/coecms/um/um2oasis.py
def latlon_scrip_grid(mask, planet_radius=6371229.0):
    """
    Create a SCRIP description of a lat-lon grid from a mask

    Coordinate information is read from the mask's coordinates. Regular spacing of coordinates is assumed.

    Args:
        mask (xarray.DataArray): Source grid mask
        planet_radius (float): Planet radius for cell area calculation

    Returns:
        xarray.DataArray containing the SCRIP description
    """
    dims = ['ny', 'nx']

    dlat = (mask.lat[1] - mask.lat[0]).data
    dlon = (mask.lon[1] - mask.lon[0]).data

    lo, la = np.meshgrid(mask.lon, mask.lat)

    mask = xarray.DataArray(numpy.logical_not(mask.data), dims=dims)

    grid_dims = xarray.DataArray(list(reversed(mask.shape)), dims=['grid_rank'])

    lo = xarray.DataArray(lo, dims=dims)
    lo.attrs['units'] = 'degrees'
    la = xarray.DataArray(la, dims=dims)
    la.attrs['units'] = 'degrees'

    # Find the cell corners (starting at the bottom left, working anticlockwise)
    clo = xarray.DataArray(np.stack([lo-dlon/2, lo+dlon/2, lo+dlon/2, lo-dlon/2]),
            dims=['grid_corners', dims[0], dims[1]])
    clo.attrs['units'] = 'degrees'
    cla = xarray.DataArray(np.clip(np.stack([la-dlat/2, la-dlat/2, la+dlat/2, la+dlat/2]), -90, 90),
            dims=['grid_corners', dims[0], dims[1]])
    cla.attrs['units'] = 'degrees'

    # Calculate the cell area
    dlonr = dlon / 180.0 * np.pi
    la_low = np.clip((la - dlat/2) / 180 * np.pi, -np.pi/2, np.pi/2)
    la_high = np.clip((la + dlat/2) / 180 * np.pi, -np.pi/2, np.pi/2)

    area = xarray.DataArray(planet_radius**2 * dlonr * (np.sin(la_high) - np.sin(la_low)), dims=dims)
    area.attrs['units'] = 'm^2'
    area.attrs['planet_radius'] = planet_radius

    ds = xarray.Dataset({'grid_center_lat': la, 'grid_center_lon': lo, 'grid_corner_lon': clo, 'grid_corner_lat': cla, 'grid_imask': mask, 'grid_area': area, 'grid_dims': grid_dims})
    ds = ds.stack(grid_size=('ny','nx')).reset_index('grid_size')

    ds['grid_corner_lon'] = ds['grid_corner_lon'].transpose()
    ds['grid_corner_lat'] = ds['grid_corner_lat'].transpose()

    return ds

    Create a SCRIP description of a lat-lon grid from a mask

    Coordinate information is read from the mask's coordinates. Regular spacing of coordinates is assumed.

    Args:
        mask (xarray.DataArray): Source grid mask
        planet_radius (float): Planet radius for cell area calculation

    Returns:
        xarray.DataArray containing the SCRIP description
    
Create a SCRIP description of a lat-lon grid from a mask
Coordinate information is read from the mask's coordinates. Regular spacing of coordinates is assumed.
Create a SCRIP description of a lat - lon grid from a mask Coordinate information is read from the mask ' s coordinates . Regular spacing of coordinates is assumed .
def latlon_scrip_grid(mask, planet_radius=6371229.0):
    dims = ['ny', 'nx']
    dlat = (mask.lat[1] - mask.lat[0]).data
    dlon = (mask.lon[1] - mask.lon[0]).data
    lo, la = np.meshgrid(mask.lon, mask.lat)
    mask = xarray.DataArray(numpy.logical_not(mask.data), dims=dims)
    grid_dims = xarray.DataArray(list(reversed(mask.shape)), dims=['grid_rank'])
    lo = xarray.DataArray(lo, dims=dims)
    lo.attrs['units'] = 'degrees'
    la = xarray.DataArray(la, dims=dims)
    la.attrs['units'] = 'degrees'
    clo = xarray.DataArray(np.stack([lo-dlon/2, lo+dlon/2, lo+dlon/2, lo-dlon/2]),
            dims=['grid_corners', dims[0], dims[1]])
    clo.attrs['units'] = 'degrees'
    cla = xarray.DataArray(np.clip(np.stack([la-dlat/2, la-dlat/2, la+dlat/2, la+dlat/2]), -90, 90),
            dims=['grid_corners', dims[0], dims[1]])
    cla.attrs['units'] = 'degrees'
    dlonr = dlon / 180.0 * np.pi
    la_low = np.clip((la - dlat/2) / 180 * np.pi, -np.pi/2, np.pi/2)
    la_high = np.clip((la + dlat/2) / 180 * np.pi, -np.pi/2, np.pi/2)
    area = xarray.DataArray(planet_radius**2 * dlonr * (np.sin(la_high) - np.sin(la_low)), dims=dims)
    area.attrs['units'] = 'm^2'
    area.attrs['planet_radius'] = planet_radius
    ds = xarray.Dataset({'grid_center_lat': la, 'grid_center_lon': lo, 'grid_corner_lon': clo, 'grid_corner_lat': cla, 'grid_imask': mask, 'grid_area': area, 'grid_dims': grid_dims})
    ds = ds.stack(grid_size=('ny','nx')).reset_index('grid_size')
    ds['grid_corner_lon'] = ds['grid_corner_lon'].transpose()
    ds['grid_corner_lat'] = ds['grid_corner_lat'].transpose()
    return ds
def latlon_scrip_grid ( mask , planet_radius = 6371229.0 ) : dims = [ 'ny' , 'nx' ] dlat = ( mask . lat [ 1 ] - mask . lat [ 0 ] ) . data dlon = ( mask . lon [ 1 ] - mask . lon [ 0 ] ) . data lo , la = np . meshgrid ( mask . lon , mask . lat ) mask = xarray . DataArray ( numpy . logical_not ( mask . data ) , dims = dims ) grid_dims = xarray . DataArray ( list ( reversed ( mask . shape ) ) , dims = [ 'grid_rank' ] ) lo = xarray . DataArray ( lo , dims = dims ) lo . attrs [ 'units' ] = 'degrees' la = xarray . DataArray ( la , dims = dims ) la . attrs [ 'units' ] = 'degrees' clo = xarray . DataArray ( np . stack ( [ lo - dlon / 2 , lo + dlon / 2 , lo + dlon / 2 , lo - dlon / 2 ] ) , dims = [ 'grid_corners' , dims [ 0 ] , dims [ 1 ] ] ) clo . attrs [ 'units' ] = 'degrees' cla = xarray . DataArray ( np . clip ( np . stack ( [ la - dlat / 2 , la - dlat / 2 , la + dlat / 2 , la + dlat / 2 ] ) , - 90 , 90 ) , dims = [ 'grid_corners' , dims [ 0 ] , dims [ 1 ] ] ) cla . attrs [ 'units' ] = 'degrees' dlonr = dlon / 180.0 * np . pi la_low = np . clip ( ( la - dlat / 2 ) / 180 * np . pi , - np . pi / 2 , np . pi / 2 ) la_high = np . clip ( ( la + dlat / 2 ) / 180 * np . pi , - np . pi / 2 , np . pi / 2 ) area = xarray . DataArray ( planet_radius ** 2 * dlonr * ( np . sin ( la_high ) - np . sin ( la_low ) ) , dims = dims ) area . attrs [ 'units' ] = 'm^2' area . attrs [ 'planet_radius' ] = planet_radius ds = xarray . Dataset ( { 'grid_center_lat' : la , 'grid_center_lon' : lo , 'grid_corner_lon' : clo , 'grid_corner_lat' : cla , 'grid_imask' : mask , 'grid_area' : area , 'grid_dims' : grid_dims } ) ds = ds . stack ( grid_size = ( 'ny' , 'nx' ) ) . reset_index ( 'grid_size' ) ds [ 'grid_corner_lon' ] = ds [ 'grid_corner_lon' ] . transpose ( ) ds [ 'grid_corner_lat' ] = ds [ 'grid_corner_lat' ] . transpose ( ) return ds
Create a SCRIP description of a lat-lon grid from a mask
Coordinate information is read from the mask's coordinates.
Create a SCRIP description of a lat - lon grid from a mask Coordinate information is read from the mask ' s coordinates .
"""
    Create a SCRIP description of a lat-lon grid from a mask

    Coordinate information is read from the mask's coordinates. Regular spacing of coordinates is assumed.

    Args:
        mask (xarray.DataArray): Source grid mask
        planet_radius (float): Planet radius for cell area calculation

    Returns:
        xarray.DataArray containing the SCRIP description
    """ # Find the cell corners (starting at the bottom left, working anticlockwise) # Calculate the cell area
xarray.DataArray containing the SCRIP description
xarray . DataArray containing the SCRIP description
mask
Source grid mask
Source grid mask
planet_radius
Planet radius for cell area calculation
Planet radius for cell area calculation