# Calculating Vorticity, Strain in regional MITgcm output

**URL:** <https://discourse.pangeo.io/t/calculating-vorticity-strain-in-regional-mitgcm-output/4510>\
**Category:** Science\
**Created:** [September 12, 2024, 9:57pm UTC](https://discourse.pangeo.io/t/calculating-vorticity-strain-in-regional-mitgcm-output/4510 "2024-09-12T21:57:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![manuelgv](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/manuelgv/32/2083_2.png) [@manuelgv](https://discourse.pangeo.io/u/manuelgv)\
**Post date:** [September 12, 2024, 9:57pm UTC](https://discourse.pangeo.io/t/calculating-vorticity-strain-in-regional-mitgcm-output/4510/1 "2024-09-12T21:57:27Z")

</div>

Hello all

I am currently using two years of hourly data from a regional MITgcm model output (configuration similar to llc4320) to estimate vorticity, strain and other kinematic quantities. I am using xmitgcm and xgcm to open the datasets stored in a server:

```python
uv = open_mdsdataset(data_dir, grid_dir, iters='all', delta_t=150, 
                     ignore_unknown_vars=True, prefix=['diags_3D'], 
                     ref_date="2015-01-01 2:0:0", geometry='sphericalpolar',
                     llc_method='smallchunks')
grid_llc = xgcm.Grid(uv, periodic=False)

```

I wonder if there is any way that I could just calculate for instance, relative vorticity in the first 10 Z-levels. I have tried using the following after the two lines above:

```python
zeta = (-grid_llc.diff(uv.UVEL.isel(Z=np.arange(0, 11)) * uv.dxC, 'Y') + 
            grid_llc.diff(uv.VVEL.isel(Z=np.arange(0, 11)) * uv.dyC, 'X'))/ds.rAz.

```

However, running the code yields the error

```python
ValueError: conflicting sizes for dimension 'Z': length 10 on <this-array> and length 90 on {'time': 'time', 'XG': 'XG', 'YG': 'YG', 'Z': 'drF'}

```

I would like to know if there is a way to first take a slice of the model output domain and then compute zeta, instead of computing zeta for the entire domain and Z levels, which takes up to 40 hrs.

Thanks

---

<div class="post-metadata">

**Author:** ![rabernat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/rabernat/32/22_2.png) [@rabernat](https://discourse.pangeo.io/u/rabernat)\
**Post date:** [September 13, 2024, 1:40pm UTC](https://discourse.pangeo.io/t/calculating-vorticity-strain-in-regional-mitgcm-output/4510/2 "2024-09-13T13:40:35Z")

</div>

Could you show the full xarray rep for uv (output of `print(uv)`)?

You might try slicing `uv` _before_ passing it to `Grid`.

---

<div class="post-metadata">

**Author:** ![TomNicholas](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/tomnicholas/32/3189_2.png) [@TomNicholas](https://discourse.pangeo.io/u/TomNicholas)\
**Post date:** [September 13, 2024, 5:12pm UTC](https://discourse.pangeo.io/t/calculating-vorticity-strain-in-regional-mitgcm-output/4510/3 "2024-09-13T17:12:17Z")

</div>

This xgcm example also might be useful for calculating vorticity efficiently

[https://xgcm.readthedocs.io/en/latest/ufunc\_examples.html#vorticity](https://xgcm.readthedocs.io/en/latest/ufunc_examples.html#vorticity)

---

<div class="post-metadata">

**Author:** ![manuelgv](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/manuelgv/32/2083_2.png) [@manuelgv](https://discourse.pangeo.io/u/manuelgv)\
**Post date:** [September 13, 2024, 8:17pm UTC](https://discourse.pangeo.io/t/calculating-vorticity-strain-in-regional-mitgcm-output/4510/4 "2024-09-13T20:17:17Z")

</div>

Heres the print of (uv):

\<xarray.Dataset\>  
Dimensions: (time: 17661, Z: 90, YC: 576, XC: 672, XG: 672, YG: 576, Zl: 90,  
Zp1: 91, Zu: 90)  
Coordinates: (12/33)  
iter (time) int64 dask.array\<chunksize=(1,), meta=np.ndarray\>

- time (time) datetime64[ns] 2015-12-27T03:00:00 … 2017-12-31T23:00:00
- XC (XC) \>f4 230.0 230.0 230.1 230.1 230.1 … 243.9 243.9 243.9 244.0
- YC (YC) \>f4 31.01 31.03 31.05 31.07 31.09 … 42.91 42.93 42.95 42.97
- XG (XG) \>f4 230.0 230.0 230.0 230.1 230.1 … 243.9 243.9 243.9 244.0
- YG (YG) \>f4 31.0 31.02 31.04 31.06 31.08 … 42.9 42.92 42.94 42.96  
… …  
maskW (Z, YC, XG) bool dask.array\<chunksize=(90, 576, 672), meta=np.ndarray\>  
maskS (Z, YG, XC) bool dask.array\<chunksize=(90, 576, 672), meta=np.ndarray\>  
maskInC (YC, XC) bool dask.array\<chunksize=(576, 672), meta=np.ndarray\>  
maskInS (YG, XC) bool dask.array\<chunksize=(576, 672), meta=np.ndarray\>  
maskInW (YC, XG) bool dask.array\<chunksize=(576, 672), meta=np.ndarray\>  
rhoRef (Z) \>f4 dask.array\<chunksize=(90,), meta=np.ndarray\>  
Data variables:  
THETA (time, Z, YC, XC) float32 dask.array\<chunksize=(1, 90, 576, 672), meta=np.ndarray\>  
SALT (time, Z, YC, XC) float32 dask.array\<chunksize=(1, 90, 576, 672), meta=np.ndarray\>  
UVEL (time, Z, YC, XG) float32 dask.array\<chunksize=(1, 90, 576, 672), meta=np.ndarray\>  
VVEL (time, Z, YG, XC) float32 dask.array\<chunksize=(1, 90, 576, 672), meta=np.ndarray\>  
WVEL (time, Zl, YC, XC) float32 dask.array\<chunksize=(1, 90, 576, 672), meta=np.ndarray\>  
DRHODR (time, Zl, YC, XC) float32 dask.array\<chunksize=(1, 90, 576, 672), meta=np.ndarray\>  
Attributes:  
Conventions: CF-1.6  
title: netCDF wrapper of MITgcm MDS binary data  
source: MITgcm  
history: Created by calling `open\_mdsdataset(data\_dir='/data/SO2/SWO…

---

<div class="post-metadata">

**Author:** ![manuelgv](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/manuelgv/32/2083_2.png) [@manuelgv](https://discourse.pangeo.io/u/manuelgv)\
**Post date:** [September 13, 2024, 8:17pm UTC](https://discourse.pangeo.io/t/calculating-vorticity-strain-in-regional-mitgcm-output/4510/5 "2024-09-13T20:17:41Z")

</div>

Thanks Tom. I will have a look at it.

---

<div class="post-metadata">

**Author:** ![manuelgv](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/manuelgv/32/2083_2.png) [@manuelgv](https://discourse.pangeo.io/u/manuelgv)\
**Post date:** [September 17, 2024, 8:20pm UTC](https://discourse.pangeo.io/t/calculating-vorticity-strain-in-regional-mitgcm-output/4510/6 "2024-09-17T20:20:01Z")

</div>

I first saved UVEL and VVEL into a new xarray dataset, then sliced the new array in XC, YC, XG, YG and Z and finally passed it to Grid. Those steps really worked for me. Thanks.
