Stream: hack-projects

Topic: Sea Ice mass balance


view this post on Zulip David Bailey (Apr 10 2020 at 20:11):

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

view this post on Zulip David Bailey (Apr 10 2020 at 20:17):

Here is the repository:

https://github.com/dabail10/massbalance

view this post on Zulip David Bailey (Apr 10 2020 at 20:49):

I've forgotten the code syntax, but I have an issue here with trying to do a NumPy computation with the Xarray object:

define sea ice budget variables

budgetVars = [
'sidmassgrowthbot',
'sidmassgrowthwat',
'sidmassmelttop',
'sidmassmeltbot',
'sidmasslat',
'sidmasssi',
'sidmassevapsubl',
'sidmassdyn',
'total'
]

read in static fields: mask and grid-cell-areas

# mask

fh = xr.open_dataset('arctic_region_mask_gx1v7.nc')
mask = fh.variables['mask'][:,:]
fh.close()

areas

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)

# reset/zero the budget for this month

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.

view this post on Zulip Keith Lindsay (Apr 10 2020 at 21:09):

@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.

view this post on Zulip Brian Bonnlander (Apr 10 2020 at 21:10):

Backquotes like this will treat text verbatim

view this post on Zulip David Bailey (Apr 10 2020 at 21:12):

That should be:

thisBudget[0] = np.sum(thisVar1*tarea*mask,dtype=float)

Thanks for catching that Keith.

view this post on Zulip Deepak Cherian (Apr 10 2020 at 21:27):

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

view this post on Zulip Keith Lindsay (Apr 10 2020 at 21:33):

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

view this post on Zulip David Bailey (Apr 10 2020 at 21:35):

Thanks Deepak. What is the syntax to sum across two dimensions?

view this post on Zulip Deepak Cherian (Apr 10 2020 at 21:36):

.sum(["dim1", "dim2"]) i.e. a list of dimension names?

view this post on Zulip Brian Bonnlander (Apr 10 2020 at 21:38):

(possibly dim=["dim1", "dim2"], not 100% sure.)

view this post on Zulip David Bailey (Apr 10 2020 at 21:41):

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.

view this post on Zulip David Bailey (Apr 24 2020 at 19:05):

Aha! So, I had to consistently rename my dimensions and now it is working. I will commit this code to my repo.

view this post on Zulip David Bailey (Apr 24 2020 at 19:21):

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

view this post on Zulip David Bailey (Apr 24 2020 at 20:18):

I meant using Xarray instead of netcdf4.


Last updated: May 16 2025 at 17:14 UTC