I have a script in python that computes sea ice mass budgets from our CMIP6 output. I had hoped to do this in Xarray/DASC, but the calendar axis was messed up for me. So, I went back to just the netcdf4 options. @Elizabeth Maroon helped me prototype this originally. I will start a github project here.
Dave
Here is the repository:
https://github.com/dabail10/massbalance
I've forgotten the code syntax, but I have an issue here with trying to do a NumPy computation with the Xarray object:
budgetVars = [
'sidmassgrowthbot',
'sidmassgrowthwat',
'sidmassmelttop',
'sidmassmeltbot',
'sidmasslat',
'sidmasssi',
'sidmassevapsubl',
'sidmassdyn',
'total'
]
fh = xr.open_dataset('arctic_region_mask_gx1v7.nc')
mask = fh.variables['mask'][:,:]
fh.close()
maskFile = '/glade/p/cesm/omwg/grids/gx1v7_grid.nc'
fh = xr.open_dataset(maskFile)
tarea = fh.variables['TAREA'][:,:]
tlat = fh.variables['TLAT'][:,:]
fh.close()
tarea = tarea*1.0e-4
files = sorted(glob.glob(path+case+'/ice/proc/tseries/month_1/sidmassgrowthbot.nc'))
fh1 = xr.open_mfdataset(files)
thisBudget = np.ma.masked_all([len(budgetVars)],dtype=float)
thisVar1 = fh1.variables[budgetVars[0]][n,:,:]
thisBudget[0] = np.sum(thisVar1tareamask,dtype=float)
This is just for the first one. The error I get is:
Traceback (most recent call last):
File "SIMIP_ice_mass_budget_calculate_CESM.py", line 143, in <module>
thisBudget[0] = np.sum(thisVar1tareamask,dtype=float)
File "/gpfs/u/apps/dav/opt/python/3.6.8/gnu/7.3.0/pkg-library/20190723/lib/python3.6/site-packages/numpy/ma/core.py", line 3330, in __setitem__
_data[indx] = dval
ValueError: setting an array element with a sequence.
@David Bailey , it looks like zulip is applying some text transformation to the line generating the error. So it's not clear what your python code actually is. Could you repost that line? Perhaps quoting it will avoid the text transformation. You can click on the eye icon before posting to see what it will look like to us.
Backquotes like this
will treat text verbatim
That should be:
thisBudget[0] = np.sum(thisVar1*tarea*mask,dtype=float)
Thanks for catching that Keith.
The error is that you're trying to assign a vector to a single element [0].
You could do the whole reduction using something like
(fh1[budgetVars] * TAREA * mask).sum(dim = appropriate_dimension_name)
assuming that TAREA and mask are appropriate for every variable
@David Bailey , I'm not able to generate this error message with separate code,
and there's not enough info to run your code, so I'm not sure why you're getting it.
I suggest adding some print statements to see what python thinks the intermediate terms are
print(thisVar1*tarea*mask) print(np.sum(thisVar1*tarea*mask,dtype=float))
Thanks Deepak. What is the syntax to sum across two dimensions?
.sum(["dim1", "dim2"])
i.e. a list of dimension names?
(possibly dim=["dim1", "dim2"]
, not 100% sure.)
Ok. I think I know the issue. So mask and TAREA have named dimensions of nlat and nlon, but the CESM variable (thisVar1) has dimensions nj and ni.
Aha! So, I had to consistently rename my dimensions and now it is working. I will commit this code to my repo.
The code is now doing exactly what I want, but using Xarray instead of numpy. There are some further tweaks I would like to make for efficiency.
https://github.com/dabail10/massbalance
I meant using Xarray instead of netcdf4.
Last updated: May 16 2025 at 17:14 UTC