uxarray
uxarray copied to clipboard
uxgrid.to_xarray('scrip') should add units attribute
trafficstars
Version
v2023.08.4
How did you install UXarray?
Conda
What happened?
When generating a SCRIP grid from a generic lat/lon mesh to use with TempestExtremes GenerateFileConnectivity, I originally got bad answers. I traced this down to to_xarray not attaching units to the variables (see here for SCRIP description)
My MCVE below shows how I converted these to radians for my purposes, but to_xarray should at the very least attach "degrees" units so that code that reads SCRIP grids can interpret the values internally.
What you get from an ncdump
netcdf mesh_C64_scrip {
dimensions:
grid_size = 24576 ;
grid_corners = 4 ;
grid_rank = 1 ;
variables:
double grid_corner_lat(grid_size, grid_corners) ;
grid_corner_lat:_FillValue = NaN ;
What did you expect to happen?
what you should get:
netcdf mesh_C64_scrip {
dimensions:
grid_size = 24576 ;
grid_corners = 4 ;
grid_rank = 1 ;
variables:
double grid_corner_lat(grid_size, grid_corners) ;
grid_corner_lat:_FillValue = NaN ;
grid_corner_lat:units = "degrees" ;
Can you provide a MCVE to repoduce the bug?
import uxarray as ux
import numpy as np
uxgrid = ux.open_grid('mesh_C64.nc')
scrip = uxgrid.to_xarray('scrip')
# Verify area in rad
total_area = scrip['grid_area'].sum()
print(total_area)
# Convert from deg -> rad
scrip['grid_corner_lat'] = np.deg2rad(scrip['grid_corner_lat'])
scrip['grid_corner_lon'] = np.deg2rad(scrip['grid_corner_lon'])
scrip['grid_center_lon'] = np.deg2rad(scrip['grid_center_lon'])
scrip['grid_center_lat'] = np.deg2rad(scrip['grid_center_lat'])
scrip.to_netcdf('mesh_C64_scrip.nc')