{ "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",
" 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",
"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",
"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": [
"latitude variable.\n",
"[:]"
]
},
{
"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": [
"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
}