Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add column relative humidity function #416

Merged

Conversation

lnellesen
Copy link
Contributor

Add column relative humidity function and water vapour pressure to specific humidity

@gerritholl
Copy link
Contributor

I wonder if typhon would benefit from leveraging the work in metpy https://unidata.github.io/MetPy/latest/index.html

@olemke
Copy link
Member

olemke commented Sep 11, 2023

@florian-roemer, @stefanbuehler: Here is the PR from Leonie. I'll merge it if you give it a thumbs-up.

@stefanbuehler
Copy link

Comments:

  1. I don't understand the division in "thermodynamics.py" and "atmosphere.py", looking at the functions inside it seems completely arbitrary. I propose two files: dry_atmosphere.py and moisture.py. The former would contain 'standard_atmosphere', 'pressure2height', 'density'. The latter would contain all other functions from these two files. (Perhaps this pure rearrangement could be done by Oliver in a separate step after merging the pull request, for better transparency.)

  2. column_relative_humidity should take input variable vmr, not q, for two reasons:
    a. vmr is the more basic, more physical variable.
    b. integrate_water_vapor, which is used internally by column_relative_humidity is a function of vmr (and internally converts to q). This creates a ring of conversions: q->vmr->q. Theoretically an alternative would be to make integrate_water_vapor also depend on q instead of VMR. But this would break old code and have no other advantages, as far as I can see.

  3. In column_relative_humidity, the conversion of q to vmr is obsolete with the change in input variable suggested above. Moreover, the calculation of the saturated q is also obsolete (already now), since what you need for integrate_water_vapor is the saturated vmr, and this is simply es(T)/p. With these two changes the function gets simpler and clearer.

  4. water_vapor_pressure2specific_humidity is not necessary and should be removed.

@florian-roemer
Copy link

florian-roemer commented Sep 14, 2023

Oliver and I came up with a more compact version of the function that implements Stefan's comments:

def column_relative_humidity(vmr, p, T, axis=0):
        r"""Convert VMR, pressure and temperature into column relative humidity.
    .. math::
        \mathrm{CRH} = \frac{IWV}{IWV_{saturated}}
    The integrated water vapour (IWV) is caluculated using ty.physics.integrate_water_vapor(vmr, p).
    To calculate the saturated intergrated water vapour (IWVS) the saturated mixing ratio (VMRS) is needed 
    which is calculated from the saturated water vapour pressure (es) which is derived via 
    ty.physics.e_eq_mixed_mk(T).
    Parameters:
        vmr (ndarray): Volume mixing ratio,
        p (ndarray): Pressure [Pa],
        T (ndarray): Temperature [K].
        
    Returns:
        float or ndarray: Column relative humidity.
    """

        # saturated vmr
        es = thermodynamics.e_eq_mixed_mk(T)
        a = [1] * len(vmr.shape)
        a[axis] = len(p)
        p.shape = a
        vmrs = es / p
        
        # vmr to integrated water vapour content
        iwv = integrate_water_vapor(vmr, p, axis=axis)

        # vmrs to integrated saturated water vapour content
        iwvs = integrate_water_vapor(vmrs, p, axis=axis)

        return iwv/iwvs

@olemke
Copy link
Member

olemke commented Sep 14, 2023

  1. I don't understand the division in "thermodynamics.py" and "atmosphere.py", looking at the functions inside it seems completely arbitrary. I propose two files: dry_atmosphere.py and moisture.py. The former would contain 'standard_atmosphere', 'pressure2height', 'density'. The latter would contain all other functions from these two files. (Perhaps this pure rearrangement could be done by Oliver in a separate step after merging the pull request, for better transparency.)

After this PR is done, I'll merge themodynamics.py into atmosphere.py since there are not that many functions and no reason to distribute them over two separate files. All functions are anyway exposed directly in the typhon.physics namespace.

@stefanbuehler
Copy link

Oliver and I came up with a more compact version of the function that implements Stefan's comments:

def column_relative_humidity(vmr, p, T, axis=0):
        r"""Convert VMR, pressure and temperature into column relative humidity.
    .. math::
        \mathrm{CRH} = \frac{IWV}{IWV_{saturated}}
    The integrated water vapour (IWV) is caluculated using ty.physics.integrate_water_vapor(vmr, p).
    To calculate the saturated intergrated water vapour (IWVS) the saturated mixing ratio (VMRS) is needed 
    which is calculated from the saturated water vapour pressure (es) which is derived via 
    ty.physics.e_eq_mixed_mk(T).
    Parameters:
        vmr (ndarray): Volume mixing ratio,
        p (ndarray): Pressure [Pa],
        T (ndarray): Temperature [K].
        
    Returns:
        float or ndarray: Column relative humidity.
    """

        # saturated vmr
        es = thermodynamics.e_eq_mixed_mk(T)
        a = [1] * len(vmr.shape)
        a[axis] = len(p)
        p.shape = a
        vmrs = es / p
        
        # vmr to integrated water vapour content
        iwv = integrate_water_vapor(vmr, p, axis=axis)

        # vmrs to integrated saturated water vapour content
        iwvs = integrate_water_vapor(vmrs, p, axis=axis)

        return iwv/iwvs

Looks good.

@olemke olemke merged commit a521620 into atmtools:master Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants