diff --git a/ncl/ncl_entries/dewtemp.ipynb b/ncl/ncl_entries/dewtemp.ipynb new file mode 100644 index 00000000..3f3dc98c --- /dev/null +++ b/ncl/ncl_entries/dewtemp.ipynb @@ -0,0 +1,101 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# dewtemp_trh" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Overview\n", + "NCL's [`dewtemp_trh`](https://www.ncl.ucar.edu/Document/Functions/Built-in/dewtemp_trh.shtml) calculates the dew point temperature given temperature and relative humidity using the equations from John Dutton's _\"Ceaseless Wind\"_ (pg. 273-274){footcite}`dutton_1986` and returns a temperature in Kelvin.\n", + "\n", + "
\n", + "

Important Note

\n", + " To convert from Kelvin to Celsius -273.15 and to convert from Celsius to Kelvin +273.15\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Grab and Go" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Input: Single Value\n", + "from geocat.comp import dewtemp\n", + "\n", + "temp_c = 18 # Celsius\n", + "relative_humidity = 46.5 # %\n", + "\n", + "dewtemp(temp_c + 273.15, relative_humidity) - 273.15 # Returns in Celsius" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Input: List/Array\n", + "from geocat.comp import dewtemp\n", + "\n", + "temp_kelvin = [291.15, 274.14, 360.3, 314] # Kelvin\n", + "relative_humidity = [46.5, 5, 96.5, 1] # %\n", + "\n", + "dewtemp(temp_kelvin, relative_humidity) - 273.15 # Returns in Celsius" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Python Resources\n", + "- [GeoCAT-comp Documentation](https://geocat-comp.readthedocs.io/en/latest/user_api/generated/geocat.comp.meteorology.dewtemp.html)\n", + "- [Convert between different temperature scales in SciPy](https://docs.scipy.org/doc/scipy/reference/generated/scipy.constants.convert_temperature.html)\n", + "\n", + "## Additional Reading\n", + "- [NOAA: Dew Point vs. Humidity](https://www.weather.gov/arx/why_dewpoint_vs_humidity)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.8" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/ncl/ncl_entries/ncl_entries.rst b/ncl/ncl_entries/ncl_entries.rst index 0f3500b7..c3f3bc1d 100644 --- a/ncl/ncl_entries/ncl_entries.rst +++ b/ncl/ncl_entries/ncl_entries.rst @@ -11,6 +11,7 @@ Data Analysis climatology_functions.ipynb trigonometric_functions.ipynb general_applied_math.ipynb + dewtemp.ipynb specx_specxy_anal.ipynb Dates and Times diff --git a/ncl/ncl_index/ncl-index-table.csv b/ncl/ncl_index/ncl-index-table.csv index 16887ba6..3ae2467d 100644 --- a/ncl/ncl_index/ncl-index-table.csv +++ b/ncl/ncl_index/ncl-index-table.csv @@ -43,3 +43,4 @@ NCL Function,Description,Python Equivalent,Notes `sqrt `__,"Computes the square root of its input","``math.sqrt()`` or ``numpy.sqrt()``",`example notebook <../ncl_entries/general_applied_math.ipynb#sqrt>`__ `sign_matlab `__,"Mimic the behavior of Matlab's sign function","``numpy.sign()``",`example notebook <../ncl_entries/general_applied_math.ipynb#sign-matlab>`__ `round `__,"Rounds a float or double variable to the nearest whole number","``round()`` or ``numpy.round()``",`example notebook <../ncl_entries/general_applied_math.ipynb#decimalplaces-round>`__ +`dewtemp_trh `__,"Calculates the dew point temperature given temperature and relative humidity","``geocat.comp.dewtemp()``",`example notebook <../ncl_entries/dewtemp.ipynb>`__ diff --git a/ncl/ncl_raw/dewtemp_trh.ncl b/ncl/ncl_raw/dewtemp_trh.ncl new file mode 100644 index 00000000..8fa6c2e8 --- /dev/null +++ b/ncl/ncl_raw/dewtemp_trh.ncl @@ -0,0 +1,14 @@ +; dewtemp_trh +; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/dewtemp_trh.shtml + +; ncl -n dewtemp_trh.ncl >> dewtemp_trh_output.txt + +print("Temperature (K), Relative Humidity (%), Dew Temperature (C)") +do tk=273,374 + do rh=1,100 + begin + dewtemp = dewtemp_trh(tk,rh)-273.15 + print (tk +","+ rh +","+ dewtemp) + end + end do +end do diff --git a/ncl/receipts/dewtemp_trh.ipynb b/ncl/receipts/dewtemp_trh.ipynb new file mode 100644 index 00000000..1f47bb72 --- /dev/null +++ b/ncl/receipts/dewtemp_trh.ipynb @@ -0,0 +1,161 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "e6eddaf181eacfd3", + "metadata": {}, + "source": [ + "# dewtemp_trh" + ] + }, + { + "cell_type": "markdown", + "id": "e6be6389ef38b00b", + "metadata": {}, + "source": [ + "```{warning} This is not meant to be a standalone notebook.\n", + "This notebook is part of the process we have for adding entries to the NCL Index and is not meant to be used as tutorial or example code.\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "536ffded4355d9c6", + "metadata": {}, + "source": [ + "## Functions covered\n", + "- [dewtemp_trh](https://www.ncl.ucar.edu/Document/Functions/Built-in/dewtemp_trh.shtml)" + ] + }, + { + "cell_type": "markdown", + "id": "4a129c971c083695", + "metadata": {}, + "source": [ + "## NCL code" + ] + }, + { + "cell_type": "markdown", + "id": "3d70616a8934f0fb", + "metadata": {}, + "source": [ + "```{literalinclude} ../ncl_raw/dewtemp_trh.ncl\n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "d918dec004b6456b", + "metadata": {}, + "source": [ + "## Python Functionality" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7af6f81c-2498-4aea-b250-579ec3fd31c8", + "metadata": {}, + "outputs": [], + "source": [ + "#### Collect NCL values for dewtemp_trh from geocat-datafiles\n", + "import geocat.datafiles as gdf\n", + "import numpy as np\n", + "\n", + "dewtemp_data = gdf.get('applications_files/ncl_outputs/dewtemp_trh_output.txt')\n", + "dewtemp_data = np.loadtxt(dewtemp_data, delimiter=',', skiprows=6)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3166c583-1290-4300-b96b-beab81cd033d", + "metadata": {}, + "outputs": [], + "source": [ + "### Collect NCL `dewtemp` value and associated (temperature_kelvin, relative humidity) values\n", + "ncl_dewtemp = {}\n", + "tk_rh = tuple(map(tuple, dewtemp_data[::, 0:2]))\n", + "dewtemp_values = dewtemp_data[::, 2]\n", + "ncl_dewtemp = dict(zip(tk_rh, dewtemp_values))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39fd494c-609b-4dd0-83fd-b08d0daf6aaa", + "metadata": {}, + "outputs": [], + "source": [ + "### Collect Temperature (Kelvin) and Relative Humidity Pairs\n", + "tk_rh = []\n", + "for tk in range(273, 374 + 1):\n", + " for rh in range(1, 100 + 1):\n", + " tk_rh.append((tk, rh))\n", + "\n", + "### Calculate GeoCAT-Comp `dewtemp` value and tk/rh\n", + "from geocat.comp import dewtemp\n", + "\n", + "geocat_dewtemp = {}\n", + "\n", + "for i, pair in enumerate(tk_rh):\n", + " tk, rh = pair\n", + " geocat_dewtemp[pair] = dewtemp(tk, rh) - 273.15" + ] + }, + { + "cell_type": "markdown", + "id": "3237a0bffc6827fc", + "metadata": {}, + "source": [ + "## Comparison" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "74362fd9-0e9f-4cf9-91da-08cd81be625c", + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "\n", + "for pair in ncl_dewtemp.keys():\n", + " try:\n", + " assert math.isclose(\n", + " ncl_dewtemp[pair], geocat_dewtemp[pair], rel_tol=1e-04\n", + " ) # within 4 decimal points\n", + " except Exception:\n", + " assert math.isclose(\n", + " ncl_dewtemp[pair], geocat_dewtemp[pair], rel_tol=1e-02\n", + " ) # within 2 decimal points\n", + " print(f\"{pair}:\")\n", + " print(f\"\\t{ncl_dewtemp[pair]}, {geocat_dewtemp[pair]}\")\n", + " print(f\"\\tDifference: {ncl_dewtemp[pair] - geocat_dewtemp[pair]}\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/references.bib b/references.bib index 864bdba1..eae8df66 100644 --- a/references.bib +++ b/references.bib @@ -107,3 +107,11 @@ @book{monteith_2008 year = {2008}, publisher = {Academic Press} } + +@book{dutton_1986, + author = {John A. Dutton}, + title = {The Ceaseless Wind: An Introduction to the Theory of Atmospheric Motion}, + year = {1986}, + pages={273-274}, + publisher = {Dover Publications} +}