NCAR / geocat-comp

GeoCAT-comp provides implementations of computational functions for operating on geosciences data. Many of these functions originated in NCL and were translated into Python.
https://geocat-comp.readthedocs.io
Apache License 2.0
126 stars 56 forks source link

pres_hybrid_ccm #26

Open clyne opened 5 years ago

clyne commented 5 years ago

Port the NCL pres_hybrid_ccm family of functions, including: pres_hybrid_ccm, dpres_hybrid_ccm. pres_hybrid_ccm Interpolates data on constant pressure levels to hybrid levels.

katiedagon commented 1 year ago

Hi @NCAR/geocat, I'm reviving this issue as I am actively working with @teaganking and @jtruesdal on a workflow that is trying to replicate the behavior of NCL's dpres_hybrid_ccm in python.

I think what we're looking for is a version of geocat comp's dpres_plevel but for a hybrid coordinate system. This is similar to the interpolation functionality that allows the user to input hybrid coordinates (e.g., interp_hybrid_to_pressure) which we are also using as part of our project.

In the longer term, it might be useful to have a function that calculates total vertically integrated quantities from atmospheric model output with a hybrid coordinate system. In this specific case, we are trying to calculate total vertically integrated water vapor (TMQ) based on specific humidity (Q) and pressure (PS) on hybrid coordinates. Teagan has provided some sample code that I'm including below, and John and Teagan can fill in more details here on the specific use cases and what would be most helpful.

Thanks!

# open/read in files and vars and coefficients:
qfile1 = xr.open_dataset(qfilenames[0], chunks={"time": 50, "ncol":50})
psfile1 = xr.open_dataset(psfilenames[0], chunks={"time": 50})
file_1_Q = qfile1['Q']
file_1_PS = psfile1['PS']
ps = psfile1.PS
plev = psfile1.lev
hyai = qfile1['hyai']
hybi = qfile1['hybi']
p0 = 100000 # Pa

# calculate dp at each of 30 levels:
dp[k]=(hyai[k+1]*p0 + hybi[k+1]*ps) - (hyai[k]*p0 + hybi[k]*ps)

# determine mass of water in each level and sum to get TMQ
gravity=9.80616
rga=1/gravity
TMQ = sum(Q * dp *rga)
anissa111 commented 1 year ago

@katiedagon, thank you for popping on here to let us know of your work! We can definitely look into providing the functionality that you're describing.

TeaganKing commented 1 year ago

Hi @NCAR/geocat, it's great to see the updates that were made with the new extrapolate parameter in interp_hybrid_to_pressure()! Thanks for all of your work on this! I think this will be very useful for a project that I'm working on with @katiedagon and @jtruesdal .

As we're moving forward with some implementation, a question came up regarding some of the logic used here. In interp_hybrid_to_pressure(), it seems that if other is selected as a variable, t_bot and phi_sfc are still required. Line 393 of interpolation.py raises a value error if extrapolate and ((variable is None) or (t_bot is None) or (phi_sfc is None)). However, I believe t_bot and phi_sfc are used in _vertical_remap_extrap only in the cases where variable is temperature or geopotential. So, if variable is set to other, interp_hybrid_to_pressure() currently still requires the user to set t_bot and phi_sfc despite these variables not actually being used. Am I overlooking some other reason why these variables are needed, or is it possible to update this such that the value error is raised only if (extrapolate and (variable is None)) or (extrapolate and ((variable is not None) and (variable is not 'other')) and ((t_bot is None) or (phi_sfc is None)))?

For the time being, I should be able to use dummy variables for t_bot and phi_sfc in order to bypass the value error, so I don't think that this is an urgent concern, but I wanted to make sure that this was not used elsewhere and ideally update this if it is a bug.

Thanks!

hCraker commented 1 year ago

Hi @TeaganKing! You are correct that this is a bug. Mathematically, you don't need to provide t_bot and phi_sfc to extrapolate for the other values. I can take a look at this and make a bug fix. I appreciate you catching this!