uxarray icon indicating copy to clipboard operation
uxarray copied to clipboard

xarray features not working on datasets

Open wwieder opened this issue 7 months ago • 5 comments
trafficstars

Version

v2025.3.0

How did you install UXarray?

Conda

What happened?

A few examples using groupby and resample don't seem to be working as I'd expect from experience with xarray. Briefly, I'm trying to create a climatology of monthly means from a decade of monthly CESM history files (h0)

First the win: After reading in a dataset with three variables dimensioned time (120) x n_face, this works, returning a month (12) x n_face array:

  • ds0['TSA'].groupby("time.month").mean('time')

The following three examples all fail as a:

  • data array using resample ds0['TSA'].resample(time="1ME").mean() the time dimension isn't reduced (still 120 x n_faces; or as a
  • dataset using both resample and groupby (e.g., ds0.groupby("time.month").mean('time')) throws a bunch of errors. As what the data array, groupby seems to reduce time dimensions as expected, but resample does not. The end of the error message is below
/glade/work/wwieder/conda-envs/ldf_v0.0/lib/python3.11/site-packages/uxarray/formatting_html.py:24, in <listcomp>(.0)
     22 def _grid_sections(grid, max_items_collapse=15):
     23     cartesian_coordinates = list(
---> 24         [coord for coord in ugrid.CARTESIAN_COORDS if coord in grid._ds]
     25     )
     26     spherical_coordinates = list(
     27         [coord for coord in ugrid.SPHERICAL_COORDS if coord in grid._ds]
     28     )
     29     descriptor = list(
     30         [desc for desc in descriptors.DESCRIPTOR_NAMES if desc in grid._ds]
     31     )

AttributeError: 'NoneType' object has no attribute '_ds'

I'm not sure this is in scope for efforts to enable xarray functionality in uxarray, but I thought I'd create an issue here

What did you expect to happen?

I though resample and groupy by would both produce climatological means of monthly history output that would work for a data array or a dataset.d

Can you provide a MCVE to repoduce the bug?

import os,sys
from glob import glob
from os.path import join
import numpy as np
import pandas as pd
from netCDF4 import num2date
import xarray as xr
import uxarray as ux 
import matplotlib
import matplotlib.pyplot as plt

path = '/glade/derecho/scratch/hannay/archive/'
suff = '/lnd/hist/'
cases = ['b.e30_alpha06e.B1850C_LTso.ne30_t232_wgx3.156']
vars  = ['TSA','QRUNOFF_TO_COUPLER', 'QRUNOFF_ICE_TO_COUPLER']
nyears = 10
iyears = -12 * nyears

fin0 = []
fin0.extend(sorted(glob(join(path+cases[0]+suff+'*.nc'))))


def preprocess(ds):
    return ds[vars]

mesh = '/glade/campaign/cesm/cesmdata/inputdata/share/meshes/ne30pg3_ESMFmesh_cdf5_c20211018.nc'
ds0 = ux.open_mfdataset(mesh, fin0[iyears:], preprocess = preprocess)


# Works on data array
ds0['TSA'].groupby("time.month").mean('time')

# Fails to reduce time dimensions
ds0['TSA'].resample(time="1ME").mean()


# Seems to work on a dataset, but throws a bunch of errors
ds0.groupby("time.month").mean('time')

# Fails to reduce time dimensions and throws a bunch of errors
ds0.resample(time="1ME").mean()

wwieder avatar Apr 22 '25 14:04 wwieder