From 796b2947fcab325f345f50842f3f165954607dee Mon Sep 17 00:00:00 2001 From: Cora Schneck <22159116+cyschneck@users.noreply.github.com> Date: Fri, 6 Dec 2024 14:29:39 -0700 Subject: [PATCH] saturation_vapor_pressure (#154) --- ncl/ncl_entries/meteorology.ipynb | 50 ++++++++++++- ncl/ncl_index/ncl-index-table.csv | 1 + ncl/ncl_raw/daylight_fao56.ncl | 14 ---- ncl/ncl_raw/dewtemp_trh.ncl | 14 ---- ncl/ncl_raw/meteorology.ncl | 42 +++++++++++ ncl/receipts/meteorology.ipynb | 112 ++++++++++++++++++++++++++---- 6 files changed, 190 insertions(+), 43 deletions(-) delete mode 100644 ncl/ncl_raw/daylight_fao56.ncl delete mode 100644 ncl/ncl_raw/dewtemp_trh.ncl create mode 100644 ncl/ncl_raw/meteorology.ncl diff --git a/ncl/ncl_entries/meteorology.ipynb b/ncl/ncl_entries/meteorology.ipynb index 64efce4f..b381c4b4 100644 --- a/ncl/ncl_entries/meteorology.ipynb +++ b/ncl/ncl_entries/meteorology.ipynb @@ -16,7 +16,8 @@ "This section covers meteorology functions from NCL:\n", "\n", "- [dewtemp_trh](https://www.ncl.ucar.edu/Document/Functions/Built-in/dewtemp_trh.shtml)\n", - "- [daylight_fao56](https://www.ncl.ucar.edu/Document/Functions/Crop/daylight_fao56.shtml)" + "- [daylight_fao56](https://www.ncl.ucar.edu/Document/Functions/Crop/daylight_fao56.shtml)\n", + "- [satvpr_temp_fao56](https://www.ncl.ucar.edu/Document/Functions/Crop/satvpr_temp_fao56.shtml)" ] }, { @@ -116,6 +117,50 @@ "max_daylight(days_of_year, latitudes)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## satvpr_temp_fao56\n", + "\n", + "NCL's `satvpr_temp_fao56` calculates saturation vapor pressure using temperature as described in the Food and Agriculture Organization (FAO) Irrigation and Drainage Paper 56 [(Chapter 3, Equation 11)](https://www.fao.org/4/x0490e/x0490e07.htm) {footcite}`allan_fao_1998`" + ] + }, + { + "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 saturation_vapor_pressure\n", + "\n", + "temp = 50 # Fahrenheit\n", + "\n", + "saturation_vapor_pressure(temp)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Input: List/Array\n", + "from geocat.comp import saturation_vapor_pressure\n", + "\n", + "temp = [33, 50, 100, 212] # Fahrenheit\n", + "\n", + "saturation_vapor_pressure(temp)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -131,6 +176,7 @@ "- [GeoCAT-comp `dewtemp` 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", "- [GeoCAT-comp `max_daylight` Documentation](https://geocat-comp.readthedocs.io/en/latest/user_api/generated/geocat.comp.meteorology.max_daylight.html)\n", + "- [GeoCAT-comp `saturation_vapor_pressure` Documentation](https://geocat-comp.readthedocs.io/en/latest/user_api/generated/geocat.comp.meteorology.saturation_vapor_pressure.html)\n", "\n", "## Additional Reading\n", "- [NOAA: Dew Point vs. Humidity](https://www.weather.gov/arx/why_dewpoint_vs_humidity)" @@ -163,7 +209,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.12.7" } }, "nbformat": 4, diff --git a/ncl/ncl_index/ncl-index-table.csv b/ncl/ncl_index/ncl-index-table.csv index 52491b38..c339c5e7 100644 --- a/ncl/ncl_index/ncl-index-table.csv +++ b/ncl/ncl_index/ncl-index-table.csv @@ -46,3 +46,4 @@ NCL Function,Description,Python Equivalent,Notes `daylight_fao56 `__," Compute maximum number of daylight hours as described in FAO 56","``geocat.comp.meteorology.max_daylight()``",`example notebook <../ncl_entries/meteorology.ipynb#daylight-fao56>`__ `dewtemp_trh `__,"Calculates the dew point temperature given temperature and relative humidity","``geocat.comp.dewtemp()``",`example notebook <../ncl_entries/meteorology.ipynb#dewtemp-trh>`__ `area_poly_sphere `__,"Calculates the area enclosed by an arbitrary polygon on the sphere","``pyproj.Geod()`` and ``shapely.geometry.polygon.Polygon()``",`example notebook <../ncl_entries/great_circle.ipynb#area-poly-sphere>`__ +`satvpr_temp_fao56 `__," Compute saturation vapor pressure using temperature as described in FAO 56","``geocat.comp.saturation_vapor_pressure()``",`example notebook <../ncl_entries/meteorology.ipynb#satvpr-temp-fao56>`__ diff --git a/ncl/ncl_raw/daylight_fao56.ncl b/ncl/ncl_raw/daylight_fao56.ncl deleted file mode 100644 index d126ce7c..00000000 --- a/ncl/ncl_raw/daylight_fao56.ncl +++ /dev/null @@ -1,14 +0,0 @@ -; daylight_fao56 -; Adapted from https://www.ncl.ucar.edu/Document/Functions/Crop/daylight_fao56.shtml - -; ncl -n daylight_fao56.ncl >> daylight_fao56_output.txt - -print("DOY, Latitude (Degrees), Daylight Hours") -do doy=0,365 - do lat=-66,66 - begin - daylight_hours = daylight_fao56(doy, lat) - print (doy +","+ lat +","+ daylight_hours) - end - end do -end do diff --git a/ncl/ncl_raw/dewtemp_trh.ncl b/ncl/ncl_raw/dewtemp_trh.ncl deleted file mode 100644 index 8fa6c2e8..00000000 --- a/ncl/ncl_raw/dewtemp_trh.ncl +++ /dev/null @@ -1,14 +0,0 @@ -; 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/ncl_raw/meteorology.ncl b/ncl/ncl_raw/meteorology.ncl new file mode 100644 index 00000000..e055c49b --- /dev/null +++ b/ncl/ncl_raw/meteorology.ncl @@ -0,0 +1,42 @@ +; daylight_fao56 +; Adapted from https://www.ncl.ucar.edu/Document/Functions/Crop/daylight_fao56.shtml + +; ncl -n daylight_fao56.ncl >> daylight_fao56_output.txt + +print("DOY, Latitude (Degrees), Daylight Hours") +do doy=0,365 + do lat=-66,66 + begin + daylight_hours = daylight_fao56(doy, lat) + print (doy +","+ lat +","+ daylight_hours) + end + end do +end do + +; 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 + +; satvpr_temp_fao56 +; Adapted from https://www.ncl.ucar.edu/Document/Functions/Crop/satvpr_temp_fao56.shtml + +; ncl -n satvpr_temp_fao56.ncl >> satvpr_temp_fao56_output.txt + +print("Temperature (F), Saturation Vapor Pressure (kPa)") +do temp=33,212 + begin + sat_vpr_pressure = satvpr_temp_fao56(temp, (/2, 2/)) + print (temp + "," + sat_vpr_pressure) + end +end do diff --git a/ncl/receipts/meteorology.ipynb b/ncl/receipts/meteorology.ipynb index 053569ef..c51b2fb2 100644 --- a/ncl/receipts/meteorology.ipynb +++ b/ncl/receipts/meteorology.ipynb @@ -25,7 +25,8 @@ "source": [ "## Functions covered\n", "- [dewtemp_trh](https://www.ncl.ucar.edu/Document/Functions/Built-in/dewtemp_trh.shtml)\n", - "- [daylight_fao56](https://www.ncl.ucar.edu/Document/Functions/Crop/daylight_fao56.shtml)" + "- [daylight_fao56](https://www.ncl.ucar.edu/Document/Functions/Crop/daylight_fao56.shtml)\n", + "- [satvpr_temp_fao56](https://www.ncl.ucar.edu/Document/Functions/Crop/satvpr_temp_fao56.shtml)" ] }, { @@ -41,17 +42,7 @@ "id": "3d70616a8934f0fb", "metadata": {}, "source": [ - "```{literalinclude} ../ncl_raw/dewtemp_trh.ncl\n", - "\n", - "```" - ] - }, - { - "cell_type": "markdown", - "id": "3b32a066-055f-454b-a843-e57dca954dad", - "metadata": {}, - "source": [ - "```{literalinclude} ../ncl_raw/daylight_fao56.ncl\n", + "```{literalinclude} ../ncl_raw/meteorology.ncl\n", "\n", "```" ] @@ -184,6 +175,60 @@ " geocat_daylight[pair] = max_daylight(doy, lat)" ] }, + { + "cell_type": "markdown", + "id": "a1401358-81a8-4477-9bc9-bbdaf4224c76", + "metadata": {}, + "source": [ + "### satvpr_temp_fao56" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39c75093-80bc-44c8-880e-ad7f3cfc605c", + "metadata": {}, + "outputs": [], + "source": [ + "#### Collect NCL values for satvpr_temp_fao56 from geocat-datafiles\n", + "import geocat.datafiles as gdf\n", + "import numpy as np\n", + "\n", + "satvpr_temp_fao56_data = gdf.get(\n", + " 'applications_files/ncl_outputs/satvpr_temp_fao56_output.txt'\n", + ")\n", + "satvpr_temp_fao56_data = np.loadtxt(satvpr_temp_fao56_data, delimiter=',', skiprows=6)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "00f0ba20-176f-4139-bc65-a14e01695494", + "metadata": {}, + "outputs": [], + "source": [ + "### Collect NCL `satvpr_temp_fao56` value and associated (temp, satvpr_temp) values\n", + "ncl_satvpr_temp_fao56 = dict(\n", + " zip(satvpr_temp_fao56_data[::, 0], satvpr_temp_fao56_data[::, 1])\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "128843eb-c4ad-425a-a1ec-de89545bcb15", + "metadata": {}, + "outputs": [], + "source": [ + "### Calculate GeoCAT-Comp `satvpr_temp_fao56`\n", + "from geocat.comp import saturation_vapor_pressure\n", + "\n", + "geocat_satvpr_temp_fao56 = {}\n", + "\n", + "for temp in range(33, 212 + 1):\n", + " geocat_satvpr_temp_fao56[temp] = saturation_vapor_pressure(temp)" + ] + }, { "cell_type": "markdown", "id": "3237a0bffc6827fc", @@ -192,6 +237,14 @@ "## Comparison" ] }, + { + "cell_type": "markdown", + "id": "38089ce5-395d-4956-8297-cde6ef5aa99e", + "metadata": {}, + "source": [ + "### dewtemp_trh" + ] + }, { "cell_type": "code", "execution_count": null, @@ -215,6 +268,14 @@ " print(f\"\\tDifference: {ncl_dewtemp[pair] - geocat_dewtemp[pair]}\")" ] }, + { + "cell_type": "markdown", + "id": "b9cd9ad4-f898-460c-b79b-eef6f7a8e89d", + "metadata": {}, + "source": [ + "### daylight_fao56" + ] + }, { "cell_type": "code", "execution_count": null, @@ -222,11 +283,36 @@ "metadata": {}, "outputs": [], "source": [ + "import math\n", + "\n", "for pair in ncl_daylight.keys():\n", " assert math.isclose(\n", " ncl_daylight[pair], geocat_daylight[pair].flatten()[0], rel_tol=1e-05\n", " ) # within 5 decimal points" ] + }, + { + "cell_type": "markdown", + "id": "db0d8bbd-4899-4932-a28d-107c08e12a15", + "metadata": {}, + "source": [ + "### satvpr_temp_fao56" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "134f8583-0e96-43fe-8b0b-94a5e58566be", + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "\n", + "for key in ncl_satvpr_temp_fao56.keys():\n", + " assert math.isclose(\n", + " ncl_satvpr_temp_fao56[key], geocat_satvpr_temp_fao56[key], rel_tol=1e-05\n", + " ) # within 5 decimal points" + ] } ], "metadata": { @@ -245,7 +331,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.12.7" } }, "nbformat": 4,