|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "32af63de", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# net2cog regression tests\n", |
| 9 | + "\n", |
| 10 | + "This Jupyter notebook runs a suite of regression tests against the net2cog Harmony Service.\n", |
| 11 | + "\n", |
| 12 | + "These tests use [SMAP_RSS_L3_SSS_SMI_8DAY-RUNNINGMEAN_V4](https://cmr.uat.earthdata.nasa.gov/search/concepts/C1234410736-POCLOUD) as netcdf input data to test the net2cog service for the smap_sss variable.\n", |
| 13 | + "\n", |
| 14 | + "## Set the Harmony environment:\n", |
| 15 | + "\n", |
| 16 | + "The cell below sets the `harmony_host_url` to one of the following valid values:\n", |
| 17 | + "\n", |
| 18 | + "* Production: <https://harmony.earthdata.nasa.gov>\n", |
| 19 | + "* UAT: <https://harmony.uat.earthdata.nasa.gov>\n", |
| 20 | + "* SIT: <https://harmony.sit.earthdata.nasa.gov>\n", |
| 21 | + "* Local: <http://localhost:3000>\n", |
| 22 | + "\n", |
| 23 | + "The default value is for the UAT environment. When using this notebook there are two ways to use the non-default environment:\n", |
| 24 | + "\n", |
| 25 | + "* Run this notebook in a local Jupyter notebook server and change the value of `harmony_host_url` in the cell below to the value for the environment you require from the above list.\n", |
| 26 | + "\n", |
| 27 | + "* Use the `run_notebooks.sh` script, which requires you to declare an environment variable `HARMONY_HOST_URL`. Set that environment variable to the value above that corresponds to the environment you want to test. That environment variable will take precedence over the default value in the cell below." |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": null, |
| 33 | + "id": "dec3bc66", |
| 34 | + "metadata": { |
| 35 | + "tags": [ |
| 36 | + "parameters" |
| 37 | + ] |
| 38 | + }, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "harmony_host_url = 'https://harmony.uat.earthdata.nasa.gov'" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "id": "7e969d81", |
| 47 | + "metadata": {}, |
| 48 | + "source": [ |
| 49 | + "## Prerequisites\n", |
| 50 | + "\n", |
| 51 | + "The dependencies for this notebook are listed in the [environment.yaml](./environment.yaml). To test or install locally, create the papermill environment used in the automated regression testing suite:\n", |
| 52 | + "\n", |
| 53 | + "`conda env create -f ./environment.yaml && conda activate papermill-net2cog`\n", |
| 54 | + "\n", |
| 55 | + "A `.netrc` file must also be located in the `test` directory of this repository." |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "markdown", |
| 60 | + "id": "802241b5", |
| 61 | + "metadata": {}, |
| 62 | + "source": [ |
| 63 | + "### Import required packages:" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": null, |
| 69 | + "id": "295b8341", |
| 70 | + "metadata": {}, |
| 71 | + "outputs": [], |
| 72 | + "source": [ |
| 73 | + "from pathlib import Path\n", |
| 74 | + "from tempfile import TemporaryDirectory\n", |
| 75 | + "\n", |
| 76 | + "from harmony import BBox, Collection, Environment, Client, Request\n", |
| 77 | + "from harmony.harmony import ProcessingFailedException\n", |
| 78 | + "from numpy.testing import assert_array_almost_equal\n", |
| 79 | + "import rasterio\n", |
| 80 | + "from rasterio.transform import Affine\n", |
| 81 | + "from rasterio.crs import CRS\n", |
| 82 | + "\n", |
| 83 | + "import utility\n", |
| 84 | + "\n", |
| 85 | + "\n", |
| 86 | + "reference_dir = Path('./reference_data')" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "id": "9b644811", |
| 92 | + "metadata": {}, |
| 93 | + "source": [ |
| 94 | + "### Set up environment dependent variables:\n", |
| 95 | + "\n", |
| 96 | + "This includes the Harmony `Client` object and `Collection` objects for each of the collections for which there are regression tests. The local, SIT and UAT Harmony instances all utilise resources from CMR UAT, meaning any non-production environment will use the same resources.\n", |
| 97 | + "\n", |
| 98 | + "When adding a production entry to the dictionary below, the collection instances can be included directly in the production dictionary entry, as they do not need to be shared." |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "id": "437af5f8", |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "non_production_collection = {\n", |
| 109 | + " 'smap_collection': Collection(id='C1234410736-POCLOUD'),\n", |
| 110 | + "}\n", |
| 111 | + "\n", |
| 112 | + "non_prod_granule_data = {\n", |
| 113 | + " 'smap_granules': ['G1234601650-POCLOUD'],\n", |
| 114 | + "}\n", |
| 115 | + "\n", |
| 116 | + "collection_data = {\n", |
| 117 | + " 'https://harmony.uat.earthdata.nasa.gov': {\n", |
| 118 | + " **non_production_collection,\n", |
| 119 | + " **non_prod_granule_data,\n", |
| 120 | + " 'env': Environment.UAT,\n", |
| 121 | + " },\n", |
| 122 | + " 'https://harmony.sit.earthdata.nasa.gov': {\n", |
| 123 | + " **non_production_collection,\n", |
| 124 | + " **non_prod_granule_data,\n", |
| 125 | + " 'env': Environment.SIT,\n", |
| 126 | + " },\n", |
| 127 | + " 'http://localhost:3000': {\n", |
| 128 | + " **non_production_collection,\n", |
| 129 | + " **non_prod_granule_data,\n", |
| 130 | + " 'env': Environment.LOCAL,\n", |
| 131 | + " },\n", |
| 132 | + "}\n", |
| 133 | + "\n", |
| 134 | + "environment_information = collection_data.get(harmony_host_url)\n", |
| 135 | + "\n", |
| 136 | + "if environment_information is not None:\n", |
| 137 | + " harmony_client = Client(env=environment_information['env'])\n", |
| 138 | + " endpoint_url = environment_information.get('endpoint_url', None)" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "markdown", |
| 143 | + "id": "fd8d6cb3", |
| 144 | + "metadata": {}, |
| 145 | + "source": [ |
| 146 | + "## Test conversion of sss_smap variable\n", |
| 147 | + "\n", |
| 148 | + "Use SMAP data." |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "code", |
| 153 | + "execution_count": null, |
| 154 | + "id": "cc7d75c5", |
| 155 | + "metadata": {}, |
| 156 | + "outputs": [], |
| 157 | + "source": [ |
| 158 | + "if environment_information is not None:\n", |
| 159 | + "\n", |
| 160 | + " smap_request = Request(\n", |
| 161 | + " collection=environment_information['smap_collection'],\n", |
| 162 | + " granule_id=environment_information['smap_granules'][0],\n", |
| 163 | + " variables=['sss_smap'],\n", |
| 164 | + " max_results=1,\n", |
| 165 | + " format='image/tiff',\n", |
| 166 | + " )\n", |
| 167 | + " print(harmony_client.request_as_curl(smap_request))\n", |
| 168 | + "\n", |
| 169 | + " smap_job_id = harmony_client.submit(smap_request)\n", |
| 170 | + " harmony_client.wait_for_processing(smap_job_id, show_progress=True)" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "code", |
| 175 | + "execution_count": null, |
| 176 | + "id": "22348be6", |
| 177 | + "metadata": {}, |
| 178 | + "outputs": [], |
| 179 | + "source": [ |
| 180 | + "with TemporaryDirectory() as temp_dir:\n", |
| 181 | + " downloaded_cog_outputs = [\n", |
| 182 | + " file_future.result()\n", |
| 183 | + " for file_future in harmony_client.download_all(\n", |
| 184 | + " smap_job_id, overwrite=True, directory=temp_dir\n", |
| 185 | + " )\n", |
| 186 | + " ]\n", |
| 187 | + "\n", |
| 188 | + " for cog_file in downloaded_cog_outputs:\n", |
| 189 | + " utility.validate_cog(cog_file)\n", |
| 190 | + "\n", |
| 191 | + " expected_metadata = {\n", |
| 192 | + " 'driver': 'GTiff',\n", |
| 193 | + " 'dtype': 'float32',\n", |
| 194 | + " 'nodata': -9999.0,\n", |
| 195 | + " 'width': 1440,\n", |
| 196 | + " 'height': 720,\n", |
| 197 | + " 'count': 1,\n", |
| 198 | + " 'crs': CRS.from_epsg(4326),\n", |
| 199 | + " 'transform': Affine(0.25, 0.0, 0.0, 0.0, 0.25, -90.0),\n", |
| 200 | + " }\n", |
| 201 | + " reference_file = Path(\n", |
| 202 | + " './reference_data',\n", |
| 203 | + " 'RSS_smap_SSS_L3_8day_running_2020_005_FNL_v04.0_converted_sss_smap.tiff',\n", |
| 204 | + " )\n", |
| 205 | + "\n", |
| 206 | + " utility.assert_dataset_produced_correct_results(\n", |
| 207 | + " cog_file, expected_metadata, reference_file\n", |
| 208 | + " )" |
| 209 | + ] |
| 210 | + }, |
| 211 | + { |
| 212 | + "cell_type": "markdown", |
| 213 | + "id": "1edc6d53", |
| 214 | + "metadata": {}, |
| 215 | + "source": [ |
| 216 | + "## Test conversion of ALL variables FAILS\n", |
| 217 | + "\n", |
| 218 | + "net2cog only supports conversion of a single variable within a netcdf file. This tests that an appropriate error message is shown if more than one variable is specified as input." |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "code", |
| 223 | + "execution_count": null, |
| 224 | + "id": "aa3ec02c", |
| 225 | + "metadata": {}, |
| 226 | + "outputs": [], |
| 227 | + "source": [ |
| 228 | + "if environment_information is not None:\n", |
| 229 | + "\n", |
| 230 | + " smap_request = Request(\n", |
| 231 | + " collection=environment_information['smap_collection'],\n", |
| 232 | + " granule_id=environment_information['smap_granules'][0],\n", |
| 233 | + " variables=['all'],\n", |
| 234 | + " max_results=1,\n", |
| 235 | + " format='image/tiff',\n", |
| 236 | + " )\n", |
| 237 | + " print(harmony_client.request_as_curl(smap_request))\n", |
| 238 | + "\n", |
| 239 | + " smap_job_id = harmony_client.submit(smap_request)\n", |
| 240 | + " raised_expected_error = False\n", |
| 241 | + " try:\n", |
| 242 | + " harmony_client.wait_for_processing(smap_job_id, show_progress=True)\n", |
| 243 | + " except ProcessingFailedException as error:\n", |
| 244 | + " assert (\n", |
| 245 | + " \"net2cog harmony adapter currently only supports processing one variable at a time\"\n", |
| 246 | + " in str(error)\n", |
| 247 | + " )\n", |
| 248 | + " raised_expected_error = True\n", |
| 249 | + " print(error)\n", |
| 250 | + "\n", |
| 251 | + " assert (\n", |
| 252 | + " raised_expected_error\n", |
| 253 | + " ), 'Expected request to raise an exception but it did not.'\n", |
| 254 | + " utility.print_success('All variables raised expected error')" |
| 255 | + ] |
| 256 | + }, |
| 257 | + { |
| 258 | + "cell_type": "code", |
| 259 | + "execution_count": null, |
| 260 | + "id": "a0caf714", |
| 261 | + "metadata": {}, |
| 262 | + "outputs": [], |
| 263 | + "source": [] |
| 264 | + } |
| 265 | + ], |
| 266 | + "metadata": { |
| 267 | + "celltoolbar": "Tags", |
| 268 | + "kernelspec": { |
| 269 | + "display_name": "Python 3 (ipykernel)", |
| 270 | + "language": "python", |
| 271 | + "name": "python3" |
| 272 | + }, |
| 273 | + "language_info": { |
| 274 | + "codemirror_mode": { |
| 275 | + "name": "ipython", |
| 276 | + "version": 3 |
| 277 | + }, |
| 278 | + "file_extension": ".py", |
| 279 | + "mimetype": "text/x-python", |
| 280 | + "name": "python", |
| 281 | + "nbconvert_exporter": "python", |
| 282 | + "pygments_lexer": "ipython3", |
| 283 | + "version": "3.10.14" |
| 284 | + } |
| 285 | + }, |
| 286 | + "nbformat": 4, |
| 287 | + "nbformat_minor": 5 |
| 288 | +} |
0 commit comments