{ "cells": [ { "cell_type": "markdown", "id": "e0e69847", "metadata": { "id": "e0e69847" }, "source": [ "# Exploring TC PRIMED, Chapter 1a: NetCDF Files\n", "- Creators: Naufal Razin1, Chris Slocum2, and Kathy Haynes1\n", "- Affiliation:\n", " - 1Cooperative Institute for Research in the Atmosphere, Colorado State University, Fort Collins, CO\n", " - 2NOAA/NESDIS Center for Satellite Applications and Research, Fort Collins, CO\n", "- History:\n", " - August 15th, 2024: Updated for TC PRIMED v01r01\n", " - November 16th, 2026: Updated code to use `tcprimedapi` and new package download routine\n", "\n", "---\n", "\n", "## Overview\n", "TC PRIMED uses NetCDF Groups to store data. Not all datasets stored in the NetCDF file format use NetCDF Groups. However, since TC PRIMED is a compilation of data from various sources, the NetCDF Groups functionality helps with data organization. In this notebook, you will learn how to read a TC PRIMED NetCDF file and retrieve the various information available in the different groups.\n", "\n", "## Prerequisites\n", "To successfully navigate and use this notebook, you should be familiar with:\n", "- the basics of Python programming such as loading modules, assigning variables, and list/array indexing\n", "\n", "## Learning Outcomes\n", "By working through this notebook, you should be able to:\n", "- understand the NetCDF file structure, particularly one that contains groups\n", "- interact with (e.g., load and plot) data from a NetCDF file\n", "\n", "## Time Estimate\n", "- Estimated text reading time: 6 to 11 min\n", "- Estimated code reading time: 3 to 5 min\n", "- Estimated total reading time: 9 to 16 min" ] }, { "cell_type": "markdown", "id": "e92a0ec1", "metadata": { "id": "e92a0ec1" }, "source": [ "## Background\n", "[NetCDF](https://www.unidata.ucar.edu/software/netcdf/), or Network Common Data Form, \"is a set of software libraries and machine-independent data formats that support the creation, access, and sharing of array-oriented scientific data\" (Unidata 2023). Scientific data stored in NetCDF files are meant to be self-describing in that the file should include information about the data it contains. This self-describing information is also known as attributes or metadata. NetCDF files are a type of multidimensional raster files, like GeoTIFF files.\n", "\n", "[NetCDF Groups](https://www.unidata.ucar.edu/software/netcdf/workshops/2011/groups-types/GroupsIntro.html) are like directories or folders on your computer, except that they are contained within the NetCDF file. For example, you may have a file within a folder, and that folder is stored within a parent folder. The path to that file on your computer would be `parent_folder/child_folder/file`.\n", "\n", "In this tutorial, you will learn how to access the attributes of the different groups in a TC PRIMED file, as well as learn how to load TC PRIMED variables within the groups." ] }, { "cell_type": "markdown", "id": "dWZRuWzGSbgg", "metadata": { "id": "dWZRuWzGSbgg" }, "source": [ "## Software\n", "This tutorial uses the Python programming language and packages. We will use:\n", "- `tcprimedapi` to query and retrieve TC PRIMED files from the Amazon Web Service S3 bucket\n", "- `netCDF4` to load the TC PRIMED file\n", "- `numpy` for simple array operations" ] }, { "cell_type": "markdown", "id": "414321af-57fa-4cfe-a7af-09d0841cf08f", "metadata": {}, "source": [ "
\n", " This Learning Journey assumes that the user is running the notebook in a clean conda environment. If the user runs this notebook in an existing environment, it may install a different version of an existing package, and the user would need to restart the kernel after the required package version is installed.\n", "
\n", "
\n", " Users should also note that support for particular packages may not be available for the newest Python versions. If the install code below produces the following error message:\n", "
\n",
    "        ERROR: Could not find a version that satisfies the requirement [package] (from versions: none)\n",
    "        ERROR: No matching distribution found for [package]\n",
    "    
\n", " users will want downgrade their Python version to a version supported by [package].\n", "
" ] }, { "cell_type": "markdown", "id": "fdd01316-f982-435e-9dec-4f09fe846321", "metadata": {}, "source": [ "### Install Packages\n", "Let's first check if we have the necessary Python packages to run this notebook. If we don't, let's install them." ] }, { "cell_type": "code", "execution_count": null, "id": "lgN28prkJNjC", "metadata": { "executionInfo": { "elapsed": 138, "status": "ok", "timestamp": 1694638572131, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "lgN28prkJNjC" }, "outputs": [], "source": [ "# sys provides access to system level information (i.e., the executable for the Python installation)\n", "import sys\n", "\n", "# subprocess makes system level calls\n", "import subprocess\n", "\n", "# json allows us to query text stored in json format\n", "import json\n", "\n", "# version allows us to check package version\n", "from importlib.metadata import version\n", "\n", "# Specify packages, their distribution name, and version needed for this notebook\n", "PACKAGES = {\"numpy\": {\"distribution_name\": None, \"version\": None},\n", " \"netCDF4\": {\"distribution_name\": None, \"version\": None},\n", " \"tcprimedapi\": {\"distribution_name\": None, \"version\": None}}\n", "\n", "restart_kernel_flag = 0\n", "\n", "# Loop through each package to import\n", "for package in PACKAGES:\n", "\n", " # Get distribution name to use for install\n", " dist_name = package if PACKAGES[package][\"distribution_name\"] is None else PACKAGES[package][\"distribution_name\"]\n", " \n", " try:\n", " \n", " # First, attempt to import the package\n", " __import__(package)\n", "\n", " # Then, obtain the installed package version, the required version, and the\n", " # latest version\n", " installed_version = version(dist_name)\n", " required_version = PACKAGES[package][\"version\"]\n", "\n", " output = subprocess.check_output([sys.executable, '-m', 'pip', 'index', 'versions', '--pre', dist_name, '--json'], text=True)\n", " latest_version = json.loads(output)['latest']\n", "\n", " # Specify the version we require based on required version or latest version\n", " required_version = latest_version if required_version is None else required_version\n", "\n", " # If the required version and doesn't match the installed version\n", " if installed_version != required_version:\n", "\n", " # Get distribution version to use for install\n", " dist_version = \"==\".join([dist_name, required_version])\n", "\n", " # Force reinstall the required distribution version using pip\n", " print('############################################################################')\n", " print(package)\n", " subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--no-cache-dir', '--force-reinstall', dist_version])\n", "\n", " restart_kernel_flag = 1\n", " \n", " except (ImportError, ModuleNotFoundError):\n", "\n", " # Obtain the required package version\n", " required_version = PACKAGES[package][\"version\"]\n", "\n", " # Obtain the latest version\n", " output = subprocess.check_output([sys.executable, '-m', 'pip', 'index', 'versions', '--pre', dist_name, '--json'], text=True)\n", " latest_version = json.loads(output)['latest']\n", "\n", " # Specify the version we require based on required version or latest version\n", " required_version = latest_version if required_version is None else required_version\n", "\n", " # Get distribution version to use for install\n", " dist_version = \"==\".join([dist_name, required_version])\n", "\n", " # Install the distribution version using pip\n", " print('############################################################################')\n", " print(package)\n", " subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--no-cache-dir', dist_version])\n", "\n", "if restart_kernel_flag == 1:\n", " \n", " print('###############################################################')\n", " print('NEW PACKAGE VERSION INSTALLED. RESTART JUPYTER NOTEBOOK KERNEL!')\n", " print('###############################################################')" ] }, { "cell_type": "markdown", "id": "5F3IZzQrTZ2c", "metadata": { "id": "5F3IZzQrTZ2c" }, "source": [ "Now, let's load the modules in the packages (e.g., `Dataset`) or load the packages and assign a shorter object name for the packages (e.g., `import numpy as np`) for a cleaner use throughout the notebook." ] }, { "cell_type": "code", "execution_count": null, "id": "4BtHkfoJThJU", "metadata": { "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1694638572291, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "4BtHkfoJThJU" }, "outputs": [], "source": [ "# Load the Python packages we will use in this notebook\n", "import tcprimedapi\n", "from netCDF4 import Dataset\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "5d279e98", "metadata": { "id": "5d279e98" }, "source": [ "## Read File Online\n", "Finally, let's retrieve information from the TC PRIMED file that we will use in this example. The TC PRIMED file will be from a Global Precipitation Measurement (GPM) satellite Microwave Imager (GMI) overpass of Hurricane Florence (2018). We will retrieve the TC PRIMED file from the [Amazon Web Service (AWS) S3 bucket](https://noaa-nesdis-tcprimed-pds.s3.amazonaws.com/index.html) as part of the [NOAA Open Data Dissemination program (NODD)](https://www.noaa.gov/information-technology/open-data-dissemination).\n", "\n", "The TC PRIMED team provides a Python package to query and retrieve TC PRIMED files from the AWS S3 bucket called [`tcprimedapi`](https://pypi.org/project/tcprimedapi/). To retrieve TC PRIMED files using `tcprimedapi`, we simply provide the API with the criteria for the file(s) of interest. The criteria that are relevant to this notebook are the:\n", "- `atcf_id`: the Automated Tropical Cyclone Forecast System (ATCF) 8 character storm identifier. The ATCF ID for Hurricane Florence (2018) is `AL062018`.\n", "- `file_type`: which can be the sensor or platform abbreviation or the TC PRIMED environmental file. Since we are interested in looking at a GMI overpass, this will be `GMI`.\n", "\n", "Let's query the TC PRIMED AWS S3 bucket using `tcprimedapi` and the criteria above." ] }, { "cell_type": "code", "execution_count": null, "id": "ec573e93-061e-4e17-ae87-93037fdc1339", "metadata": {}, "outputs": [], "source": [ "# Create an instance of a Client\n", "tcpc = tcprimedapi.Client()\n", "\n", "# List desired ATCF ID\n", "atcf_id = [\"AL062018\"]\n", "\n", "# List desired file type\n", "file_type = [\"GMI\"]\n", "\n", "# Setup a data request dictionary\n", "data_request = {\"atcf_id\": atcf_id,\n", " \"file_type\": file_type}\n", "\n", "# Make a data request query\n", "tcpc.query(data_request)" ] }, { "cell_type": "markdown", "id": "5fc10b73-8f4d-45fd-9229-f266f3202dfb", "metadata": {}, "source": [ "As you can see, the query returned 17 files that match our criteria. Let's list all of them." ] }, { "cell_type": "code", "execution_count": null, "id": "3a3ba55d-9c9d-4f54-b7ff-fc045e7179a1", "metadata": {}, "outputs": [], "source": [ "for obj_key in tcpc.object_keys:\n", " print(obj_key)" ] }, { "cell_type": "markdown", "id": "73df89d5-afd7-4f2b-8845-76f6435399da", "metadata": {}, "source": [ "Let's retrieve and explore data from `TCPRIMED_v01r01-final_AL062018_GMI_GPM_025677_20180905051440.nc`.\n", "\n", "Note that `tcprimedapi` is designed to safely handle a large volume of data. The conventional way of using `tcprimedapi` involves iterating over the available files from the query and individually downloading each file or reading into memory specific variables from each file while ensuring that each file is properly closed.\n", "\n", "However, for this notebook, we will be retrieving and exploring data from a single file, which requires the file to remain open until we are done with it. So, we will use the `iter()` and `next()` functions.\n", "\n", "Therefore, we will take the following steps:\n", "- Redefine `object_keys` to have just the file of interest (`TCPRIMED_v01r01-final_AL062018_GMI_GPM_025677_20180905051440.nc`).\n", "- Retrieve the datastream object associated with the file.\n", "- Load the datastream object into a NetCDF instance called \"DS\" without downloading the file. The data will exist only in your local system's random access memory (RAM)." ] }, { "cell_type": "code", "execution_count": null, "id": "020eccd7-2dd2-423e-aecc-acd695f1bc5e", "metadata": {}, "outputs": [], "source": [ "# Redefine object_keys with just the file of interest\n", "new_key = \"v01r01/final/2018/AL/06/TCPRIMED_v01r01-final_AL062018_GMI_GPM_025677_20180905051440.nc\"\n", "tcpc.object_keys = [new_key]\n", "\n", "# Retrieve the datastream associated with the file\n", "obj_key, obj_memory = next(iter(tcpc.inmemory()))\n", "\n", "# Load the datastream object into a NetCDF instance\n", "DS = Dataset(obj_key, memory=obj_memory)" ] }, { "cell_type": "markdown", "id": "57d7bd3b", "metadata": { "id": "57d7bd3b" }, "source": [ "Great! We've loaded the data into a NetCDF \"instance\". Let's first look at the instance of the file at the \"root\" group.\n", "\n", "The root group is the outer-most \"directory\" or \"folder\" in a NetCDF file. Its instance gets automatically loaded when you load a NetCDF file in Python. For datasets that do not use NetCDF Groups, all variables would be stored in the root group. However, in TC PRIMED, the root group stores only the \"global\" attributes — information about the whole file — and the different sub-groups.\n", "\n", "Let's print the root group instance." ] }, { "cell_type": "code", "execution_count": null, "id": "0060b3d4", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 14, "status": "ok", "timestamp": 1694638573634, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "0060b3d4", "outputId": "fb6859b0-9cce-405b-e596-8d07f1a73ba2", "scrolled": true }, "outputs": [], "source": [ "# DS would automatically contain information from the root group\n", "# Print the instance of the file from the root group\n", "print(DS)" ] }, { "cell_type": "markdown", "id": "85479255", "metadata": { "id": "85479255" }, "source": [ "The printout for the root group above shows all of the global attributes — e.g., dataset title, file ID, product version, etc. You can simply glean those information from the output above.\n", "\n", "Having gleaned the global attributes above, you can directly load a particular attribute from the root group using the `getncattr` function." ] }, { "cell_type": "code", "execution_count": null, "id": "3bd344c2", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 12, "status": "ok", "timestamp": 1694638573634, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "3bd344c2", "outputId": "85f07f08-8126-46c8-e7d3-b7694d9e3e08" }, "outputs": [], "source": [ "# Load the \"summary\" attribute from the root group as root_summary\n", "root_summary = DS.getncattr(\"summary\")\n", "\n", "# Print root_summary\n", "print(root_summary)" ] }, { "cell_type": "markdown", "id": "151968b4", "metadata": { "id": "151968b4" }, "source": [ "
\n", "

Exercise 1

\n", "Using the example above, uncomment the template below, and change the code as necessary to load the \"title\" attribute from the root group.\n", "
" ] }, { "cell_type": "markdown", "id": "571NTsqFcLnV", "metadata": { "id": "571NTsqFcLnV" }, "source": [ "
\n", "Hint: If you are unsure about what attributes are available, print out the root group instance again.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "ef5dd45c", "metadata": { "executionInfo": { "elapsed": 9, "status": "ok", "timestamp": 1694638573634, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "ef5dd45c" }, "outputs": [], "source": [ "# Load the \"title\" attribute from the root group as root_title\n", "#root_title = DS.getncattr(\"insert_attr_here\")\n", "\n", "# Print root_title\n", "#print(root_title)" ] }, { "cell_type": "markdown", "id": "d75a4662", "metadata": { "id": "d75a4662" }, "source": [ "Awesome! Now, let's step back and look at the root group again, printed out below" ] }, { "cell_type": "code", "execution_count": null, "id": "6f8e20f6", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 9, "status": "ok", "timestamp": 1694638573634, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "6f8e20f6", "outputId": "c0f9254d-5041-498a-cfa8-3f14cf5d6a33", "scrolled": true }, "outputs": [], "source": [ "# Print the instance of the file from the root group\n", "print(DS)" ] }, { "cell_type": "markdown", "id": "1d120b8b", "metadata": { "id": "1d120b8b" }, "source": [ "Notice at the end of the printout, the available \"dimensions,\" \"variables,\" and \"groups\" are listed for the root group. **These are not part of the TC PRIMED file metadata, but are part of the standard output of the Python netCDF4 package as additional information**. Nonetheless, we can use that information. Under groups, six groups are listed. They are:\n", "- `overpass_metadata`\n", "- `overpass_storm_metadata`\n", "- `passive_microwave`\n", "- `GPROF`\n", "- `radar_radiometer`\n", "- `infrared`" ] }, { "cell_type": "markdown", "id": "gNReQ7AwNkUX", "metadata": { "id": "gNReQ7AwNkUX" }, "source": [ "
\n", "Be careful. The radar_radiometer group is only available for satellites with precipitation radars, such as the TRMM and GPM. Other groups are available across the different sensors and satellites in TC PRIMED.\n", "
" ] }, { "cell_type": "markdown", "id": "tBc4ErjdN0HM", "metadata": { "id": "tBc4ErjdN0HM" }, "source": [ "Now, let's access the instance of the `passive_microwave` group." ] }, { "cell_type": "code", "execution_count": null, "id": "d262526e", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 9, "status": "ok", "timestamp": 1694638573635, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "d262526e", "outputId": "8410be6c-e824-4e3f-f49d-260817c71da4" }, "outputs": [], "source": [ "# Using the file instance, load the passive_microwave group as a\n", "# group instance called passive_microwave_group\n", "passive_microwave_group = DS[\"passive_microwave\"]\n", "\n", "# Print the passive_microwave group instance\n", "print(passive_microwave_group)" ] }, { "cell_type": "markdown", "id": "eb14f7ce", "metadata": { "id": "eb14f7ce" }, "source": [ "Notice, as in the root group, the printout above indicates the dimensions, variables, and groups that are available in the `passive_microwave` group. In this example, you have\n", "- a `time` dimension of length 1\n", "- a `time` variable with a `time` dimension; therefore, a `time` variable of length 1\n", "- two sub-groups: `S1` and `S2`\n", "\n", "Let's load and print out the `time` variable instance." ] }, { "cell_type": "code", "execution_count": null, "id": "35203b16", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 7, "status": "ok", "timestamp": 1694638573635, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "35203b16", "outputId": "981c52e6-b1b5-4d6f-8c93-41b05375f6be" }, "outputs": [], "source": [ "# Using the file instance, load the variable instance for time\n", "# in the passive_microwave group by specifying its \"path\" in the file\n", "pm_time_instance = DS[\"passive_microwave/time\"]\n", "\n", "# Print the variable instance of passive microwave time\n", "print(pm_time_instance)" ] }, { "cell_type": "markdown", "id": "4770206c", "metadata": { "id": "4770206c" }, "source": [ "From the printout of the time variable instance, you can see information on the `time` variable. As you have seen from printing out the passive microwave group instance above, `time` is a one-dimensional array with one element (`current shape = (1,)`). Let's first retrieve the `long_name` attribute of this time instance using a function we've already used above, `getncattr`." ] }, { "cell_type": "code", "execution_count": null, "id": "863edd22", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 6, "status": "ok", "timestamp": 1694638573635, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "863edd22", "outputId": "694dfdf8-34cf-4f4e-e691-173799a4d475" }, "outputs": [], "source": [ "# Using the file instance, load the variable instance for time\n", "# in the passive_microwave group by specifying its \"path\" in the file\n", "# Then, retrieve the long_name attribute using getncattr\n", "pm_time_long_name = DS[\"passive_microwave/time\"].getncattr(\"long_name\")\n", "\n", "# Print the time variable long_name\n", "print(pm_time_long_name)" ] }, { "cell_type": "markdown", "id": "37eaabaa", "metadata": { "id": "37eaabaa" }, "source": [ "Finally, let's retrieve the `time` variable itself from the `time` variable instance. To do so, load the variable from the `time` variable instance using `[:]`" ] }, { "cell_type": "code", "execution_count": null, "id": "ccec69ec", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 149, "status": "ok", "timestamp": 1694638573779, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "ccec69ec", "outputId": "cac25440-6755-4ed3-d4e3-b70a399805c8" }, "outputs": [], "source": [ "# Using the file instance, load the time variable from the\n", "# passive_microwave group by specifying its \"path\" in the file and\n", "# using [:] to load the variable\n", "pm_time = DS[\"passive_microwave/time\"][:]\n", "\n", "# Print the passive microwave time variable\n", "print(pm_time)" ] }, { "cell_type": "markdown", "id": "45717c1c", "metadata": { "id": "45717c1c" }, "source": [ "You've loaded a NetCDF variable! As you have seen in the cells above, it is a one-dimensional array with one element. The `time` variable in TC PRIMED files are in the units of seconds since 1970-01-01T00:00:00. We will briefly discuss this `time` unit in Chapter 1c.\n", "\n", "For now, let's go back to the `passive_microwave` group instance." ] }, { "cell_type": "code", "execution_count": null, "id": "a1712e3a", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 8, "status": "ok", "timestamp": 1694638573779, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "a1712e3a", "outputId": "e467a763-cf00-4d43-a288-7bf548889210" }, "outputs": [], "source": [ "# Using the file instance, load the passive_microwave group as a\n", "# group instance called passive_microwave_group\n", "passive_microwave_group = DS[\"passive_microwave\"]\n", "\n", "# Print the passive_microwave group instance\n", "print(passive_microwave_group)" ] }, { "cell_type": "markdown", "id": "ed15b31b", "metadata": { "id": "ed15b31b" }, "source": [ "
\n", "

Exercise 2

Notice that within the passive_microwave group, there are two additional groups called S1 and S2. We will discuss these groups in more detail in Chapter 1b. But for now, using the knowledge you've obtained about NetCDF groups, instances, and variables, uncomment the code below and change the code as necessary to print out the S1 group instance from the passive_microwave group." ] }, { "cell_type": "code", "execution_count": null, "id": "4a62565d", "metadata": { "executionInfo": { "elapsed": 7, "status": "ok", "timestamp": 1694638573779, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "4a62565d" }, "outputs": [], "source": [ "# Using the file instance, load the S1 group within the passive_microwave\n", "# group as an instance called S1_group\n", "#S1_group = DS[\"insert_S1_group_here\"]\n", "\n", "# Print the S1 group instance\n", "#print(S1_group)" ] }, { "cell_type": "markdown", "id": "00e6de95", "metadata": { "id": "00e6de95" }, "source": [ "From the printout of the `S1` group instance above, you should see two dimensions\n", "- `scan` with length 171\n", "- `pixel` with length 221\n", "\n", "You should also see a list of variables such as `latitude` and `longitude`, all of which should have a `scan` dimension or both `scan` and `pixel` dimension." ] }, { "cell_type": "markdown", "id": "cea58360", "metadata": { "id": "cea58360" }, "source": [ "
\n", "

Exercise 3

Using the knowledge you've obtained about NetCDF groups, instances, and variables, uncomment the template below and change the code as necessary to load and print out the latitude variable.\n", "
" ] }, { "cell_type": "markdown", "id": "CVQf9P_RjrkP", "metadata": { "id": "CVQf9P_RjrkP" }, "source": [ "
\n", "Hint: When loading variables, don't forget to use [:]" ] }, { "cell_type": "code", "execution_count": null, "id": "a8cdd320", "metadata": { "executionInfo": { "elapsed": 8, "status": "ok", "timestamp": 1694638573780, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "a8cdd320" }, "outputs": [], "source": [ "# Using the file instance, load the latitude variable from the\n", "# passive_microwave group and S1 sub-group by specifying its \"path\"\n", "# in the file, and using [:] to load the variable\n", "#S1_latitude = DS[\"insert_latitude_var_here\"]\n", "\n", "# Print the latitude variable from the passive_microwave group and\n", "# the S1 sub-group\n", "#print(S1_latitude)" ] }, { "cell_type": "markdown", "id": "7e2cfa7b", "metadata": { "id": "7e2cfa7b" }, "source": [ "When loading NetCDF variables, the `[:]` operator loads the full array of the variable. But, what if you only wanted the first 10 entries of the `scan` dimension? You can refer to the dimension information for each variable by first printing out the group or variable instance, like you've done in the cells above. Then, simply supply the read routine with the appropriate index. Let's look at an example by printing out the size of the `latitude` variable being loaded." ] }, { "cell_type": "code", "execution_count": null, "id": "2ecd9a16", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 7, "status": "ok", "timestamp": 1694638573780, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "2ecd9a16", "outputId": "1cc7e1f3-a944-4991-dfde-c6f5dc355bff" }, "outputs": [], "source": [ "# Load the full array of the latitude variable using only [:]\n", "S1_full_latitude = DS[\"passive_microwave/S1/latitude\"][:]\n", "\n", "# Print the shape of the full latitude array\n", "print(S1_full_latitude.shape)\n", "\n", "# Load a subset of the latitude variable for the first 10 entries of the\n", "# scan dimension and all of the pixel dimension\n", "S1_subset_latitude = DS[\"passive_microwave/S1/latitude\"][np.arange(0,10,1),:]\n", "\n", "# Print the shape of the latitude variable subset\n", "print(S1_subset_latitude.shape)" ] }, { "cell_type": "markdown", "id": "4c69894f", "metadata": { "id": "4c69894f" }, "source": [ "
\n", "

Exercise 4

Uncomment the template below and change the code as necessary to load the first 20 entries of the scan dimension and the first 10 entries of the pixel dimension for the latitude variable. Then, print out the shape of the variable array." ] }, { "cell_type": "code", "execution_count": null, "id": "76323471", "metadata": { "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1694638573780, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "76323471" }, "outputs": [], "source": [ "# Load a subset of the latitude variable for the first 20 entries of the\n", "# scan dimension and the first 10 entries of the pixel dimension\n", "#S1_subset_latitude = DS[\"insert_latitude_var_here\"]\n", "\n", "# Print the shape of the latitude variable subset\n", "#print(S1_subset_latitude.shape)" ] }, { "cell_type": "markdown", "id": "j10Qr5IHLDaX", "metadata": { "id": "j10Qr5IHLDaX" }, "source": [ "## Close the File\n", "When loading data from a NetCDF file, **always remember to close the file**. A best practice would be to close the file immediately after loading the variable or attribute of interest. However, since we're loading various variables and attributes throughout this notebook, we will close the file at the end of this notebook using the command below." ] }, { "cell_type": "code", "execution_count": null, "id": "SOisGtrjMUzg", "metadata": { "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1694638573781, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "SOisGtrjMUzg" }, "outputs": [], "source": [ "DS.close()" ] }, { "cell_type": "markdown", "id": "a389a4d3", "metadata": { "id": "a389a4d3" }, "source": [ "## Final Thoughts\n", "There are other ways to query NetCDF files, like using [Unidata's NetCDF Utilities](https://docs.unidata.ucar.edu/nug/current/netcdf_utilities_guide.html). However, in this tutorial, you learned how to use the Python `netCDF4` package to:\n", "- retrieve information from a remote NetCDF file\n", "- print out NetCDF instances\n", "- obtain attributes from NetCDF instances\n", "- retrieve a variable from NetCDF instances\n", "- navigate groups in a NetCDF file\n", "- load only a subset of data from a NetCDF variable\n", "\n", "These are crucial steps for you to get familiarized with the structure of TC PRIMED files before you move on to conducting analyses on TC PRIMED data." ] }, { "cell_type": "markdown", "id": "527025da", "metadata": { "id": "527025da" }, "source": [ "## Data Statement\n", "- Razin, Muhammad Naufal; Slocum, Christopher J.; Knaff, John A.; Brown, Paula J. 2023. Tropical Cyclone PRecipitation, Infrared, Microwave, and Environmental Dataset (TC PRIMED). v01r01. NOAA National Centers for Environmental Information. https://doi.org/10.25921/dmy1-0595." ] }, { "cell_type": "markdown", "id": "9cce1185", "metadata": {}, "source": [ "## References\n", "- Unidata, 2023: Network Common Data Form (NetCDF). Accessed 13 June 2023, https://www.unidata.ucar.edu/software/netcdf/." ] }, { "cell_type": "markdown", "id": "ffcc0ab1", "metadata": { "id": "ffcc0ab1" }, "source": [ "## Metadata\n", "- Language / package\n", " - Python\n", " - tcprimedapi\n", " - netCDF4\n", " - numpy\n", "- Application keywords\n", " - NetCDF\n", " - NetCDF Groups\n", "- Geophysical keywords\n", " - Tropical Cyclones" ] }, { "cell_type": "markdown", "id": "1b1d693a", "metadata": { "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1694638573781, "user": { "displayName": "Naufal Razin", "userId": "00945304052336708650" }, "user_tz": 360 }, "id": "1246c922" }, "source": [ "## Disclaimer\n", "\n", "This Jupyter notebook is a scientific product and is not official communication of the National Oceanic and Atmospheric Administration, or the United States Department of Commerce. All NOAA Jupyter notebooks are provided on an 'as is' basis and the user assumes responsibility for its use. Any claims against the Department of Commerce or Department of Commerce bureaus stemming from the use of this Jupyter notebook will be governed by all applicable Federal law. Any reference to specific commercial products, processes, or services by service mark, trademark, manufacturer, or otherwise does not constitute or imply their endorsement, recommendation or favoring by the Department of Commerce. The Department of Commerce seal and logo, or the seal and logo of a DOC bureau, shall not be used in any manner to imply endorsement of any commercial product or activity by DOC or the United States Government." ] }, { "cell_type": "code", "execution_count": null, "id": "0ae839d2", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "colab": { "provenance": [], "toc_visible": true }, "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.12.11" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "165px" }, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 5 }