Neutral Surfaces Calculation in python

Hello. I would like to calculate neutral density (Jackett & McDougall, 1997) from ACCESS-OM2 (0.1 deg version in this case) output data. For example, calculation of depth-latitude sections of neutral density. I found that the neutralocean module for omega-surfaces is available on gadi, but what I would like is neutral surface labels identical to the JM97 definition, for comparison with other datasets rather than for optimal neutrality. While one can label omega surfaces with neutral density-like labels, they don’t necessarily remain very similar away from a defined point of reference, and the referencing still requires one JM97 calculation at the reference point.
I have the legacy matlab package for neutral surface computation, but are there python modules which can do that currently and are readily available on gadi?

1 Like

Good question Aviv! This is a very common question. Maybe @geoffstanley has some thoughts?

Hi Aviv,

Sounds like you’re pretty familiar with the neutral landscape already. :slight_smile:

I believe the best way to calculate 1997 neutral density values in Python is to use Eric Firing’s pygamma code, which is a Python wrapper for the original Jackett and McDougall (1997) FORTRAN code.

There should be two public functions: one to calculate the gamma^n value of a given bottle (ie given S, T, P, lon, lat values), and one to calculate the pressure of a certain gamma^n = const. isosurface (using a slightly more advanced method compared to a simple linear interpolation of gamma^n values, say).
The second function requires first pre-calculating gamma^n values for all the bottles in a cast. It sounds like the second one is what you want.

Beware that 1997 gamma^n performs fairly well when your ocean data is similar to the Levitus (1982) atlas upon which it was built, but the further your ocean data is from Levitus, the worse gamma^n will perform in terms of keeping its isosurfaces close to neutral.

Cheers,
Geoff

1 Like

Thanks Geoff, good to know! Sounds like that can be a great solution.
I looked at the instructions for requesting new packages, and it is stated that ``as a general rule we will only install packages from the ‘conda-forge’ channel’'. mmm. pygamma is not on conda-forge as far as I can see (I tried “conda search conda-forge::pygamma” and variations of that). Do you know if there a way around this @aidanheerdegen?

You can always load a conda environment and then do pip install <packagename> --user to install a package into your ~/.local directory. This works well for python-only packages. In this case it is a python front end to some fortran code, so it might work, but I’m not confident, and it may break in the future with updates to the conda environment. However, it is worth giving it a crack and see how you go.

Otherwise I would contact CLEX CMS Help if you’re affiliated with CLEX. In some cases CMS have created a conda package in their channel.

Thanks Aidan! The pip install did not work for me, but I’ll contact CLEX helpdesk at this point and write the results here later.

1 Like

I got help from @Paola-CMS, who recommended a possible solution is to install pygamma in the home folder of a user (e.g. me) via the following approach, which has worked for me:
module unload conda/analysis3-22.10
module load conda/analysis3. #or whichever environment you prefer
module load gcc
module load netcdf
download pygamma (press “zip” in the pygamma webpage), scp to your home folder, unzip, change its folder name (pygamma-2172748e338d) to, e.g., pygamma, cd into the folder, and then,
pip install . --user
Then you can import the pygamma module in your jupyter notebook as well as in a python terminal session (via “import pygamma”). There are various README files in the package, and the test_all.py file there is also a good demonstration of the interface.
Btw, there is a completely different package called pygamma (has to do with magnetic resonance) which can be installed automatically with pip. This unrelated package might be installed if you do something like “pip install pygamma”, so beware.

2 Likes