imported on: 2022-11-28

This notebook is from NASA's OpenScapes COG HTTPS access notebook

The original source for this document is

Accessing Cloud Optimized GeoTIFF (COG) - HTTPS Example#


In this notebook, we will access data for the Harmonized Landsat Sentinel-2 (HLS) Operational Land Imager Surface Reflectance and TOA Brightness Daily Global 30m v2.0 (L30) (10.5067/HLS/HLSL30.002) data product. These data are archived and distributed as Cloud Optimized GeoTIFF (COG) files, one file for each spectral band.

We will access a single COG file, L30 red band (0.64 – 0.67 μm), from inside the AWS cloud (us-west-2 region, specifically) and load it into Python as an xarray dataarray. This approach leverages S3 native protocols for efficient access to the data.


1. AWS instance running in us-west-2#

NASA Earthdata Cloud data in S3 can be directly accessed via temporary credentials; this access is limited to requests made within the US West (Oregon) (code: us-west-2) AWS region.

2. Earthdata Login#

An Earthdata Login account is required to access data, as well as discover restricted data, from the NASA Earthdata system. Thus, to access NASA data, you need Earthdata Login. Please visit to register and manage your Earthdata Login account. This account is free to create and only takes a moment to set up.

3. netrc File#

You will need a netrc file containing your NASA Earthdata Login credentials in order to execute the notebooks. A netrc file can be created manually within text editor and saved to your home directory. For additional information see: Authentication for NASA Earthdata.

Learning Objectives#

  • how to configure you Python work environment to access Cloud Optimized geoTIFF (COG) files

  • how to access HLS COG files

  • how to plot the data

Cloud Optimized GeoTIFF (COG)#

Using Harmonized Landsat Sentinel-2 (HLS) version 2.0

Import Packages#

import os
from osgeo import gdal
import rasterio as rio
import rioxarray
import hvplot.xarray
import holoviews as hv

Workspace Environment Setup#

For this exercise, we are going to open up a context manager for the notebook using the rasterio.env module to store the required GDAL configurations we need to access the data from Earthdata Cloud. While the context manager is open (rio_env.enter()) we will be able to run the open or get data commands that would typically be executed within a with statement, thus allowing us to more freely interact with the data. We’ll close the context (rio_env.exit()) at the end of the notebook.

GDAL environment variables must be configured to access COGs from Earthdata Cloud. Geospatial data access Python packages like rasterio and rioxarray depend on GDAL, leveraging GDAL’s “Virtual File Systems” to read remote files. GDAL has a lot of environment variables that control it’s behavior. Changing these settings can mean the difference being able to access a file or not. They can also have an impact on the performance.


In this example we’re interested in the HLS L30 data collection from NASA’s LP DAAC in Earthdata Cloud. Below we specify the s3 URL to the data asset in Earthdata Cloud. This URL can be found via Earthdata Search or programmatically through the CMR and CMR-STAC APIs.

https_url = ''

HTTPS Data Access#

Read in the HLS s3 URL for the L30 red band (0.64 – 0.67 μm) into our workspace using rioxarray, an extension of xarray used to read geospatial data.

da = rioxarray.open_rasterio(https_url)

The file is read into Python as an xarray dataarray with a band, x, and y dimension. In this example the band dimension is meaningless, so we’ll use the squeeze() function to remove band as a dimension.

da_red = da.squeeze('band', drop=True)

Plot the dataarray, representing the L30 red band, using hvplot.

da_red.hvplot.image(x='x', y='y', cmap='gray', aspect='equal')

Exit the context manager.