uxarray icon indicating copy to clipboard operation
uxarray copied to clipboard

uxgrid.to_xarray('scrip') should add units attribute

Open zarzycki opened this issue 1 year ago • 4 comments

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')

zarzycki avatar Sep 12 '24 15:09 zarzycki

Not sure how to attach the *.nc file. Perhaps that could be added to supported file types in the repo (within reason, so no one tries to dump a 60GB file :)

The mesh (mesh_C64.nc) is ~2.4MB.

zarzycki avatar Sep 12 '24 15:09 zarzycki

Not sure how to attach the *.nc file. Perhaps that could be added to supported file types in the repo (within reason, so no one tries to dump a 60GB file :)

The mesh (mesh_C64.nc) is ~2.4MB.

Feel free to share a google drive link to the mesh.

philipc2 avatar Sep 12 '24 15:09 philipc2

Not sure how to attach the *.nc file. Perhaps that could be added to supported file types in the repo (within reason, so no one tries to dump a 60GB file :) The mesh (mesh_C64.nc) is ~2.4MB.

Feel free to share a google drive link to the mesh.

https://drive.google.com/file/d/18KoLILLT_6VeSePN_XtTWVkodNzIuDOc/view?usp=sharing

zarzycki avatar Sep 12 '24 16:09 zarzycki

Thanks!

I can get a fix started for this, thanks for catching this!

philipc2 avatar Sep 12 '24 16:09 philipc2

@rajeeja please check

erogluorhan avatar Jul 08 '25 21:07 erogluorhan

@rajeeja

Was this fixed in #1282

philipc2 avatar Aug 12 '25 16:08 philipc2

Yes, they are set to degrees now

rajeeja avatar Aug 12 '25 17:08 rajeeja