Sentinel & Landsat Time Series to NetCDF using python and xarray

EmailTwitterLinkedInFacebookWhatsAppShare

Introduction

Satellite imagery, especially from platforms like Sentinel and Landsat, provides valuable data for monitoring Earth’s surface. In this tutorial, we’ll walk through the process of creating a time series of cloud-free imagery using the HLS (Harmonized Landsat Sentinel) dataset. We’ll then export this time series to NetCDF format for further analysis.

Setting up Configuration in Sentinel Hub

  1. Login to Sentinel Hub: Go to Sentinel Hub and log in with your credentials. If you don’t have credential then you have to sign up.
  2. Configuration Utility: Navigate to the Configuration Utility to set up the layers. Once you navigate to Configuration Utility, following screen will popup. Now click on New Layer to create one
  3. Once you click new layer, following screen will open. Give ID as HLS and Select Source as Harmonized Landsat Sentinel. Setting ID as HLS and selecting Source as Harmonized Landsat Sentinel is very important because our python code is configured to use HLS layer ID. Make sure you get it right in the configuration.
    After that, click on edit button in Data Processing which can be used for setting custom script.
  4. Once you click on edit button, below screen will open. Paste below script and click on Set Custom Script. The script can be used to filter and process the data on the fly from sentinel

  5. Get Instance ID: After configuring the layers, obtain the instance ID from the configuration utility. This ID will be used in the code to access the data.

Setting up the Code

Now, let’s understand and set up the Python code provided earlier.

  1. Install Required Packages:
    conda install -c conda-forge requests pyproj base64 rasterio xarray rioxarray numpy matplotlib json netCDF4 shapely
  2. Understanding the Code: The code uses the Sentinel Hub Web Feature Service (WFS) to query and download HLS data. It converts the geographical coordinates of the area of interest from WGS84 to UTM, retrieves cloud-free imagery, and exports the data to NetCDF format.
  3. Replace Configuration Values:
    • Replace instance_id with the instance ID obtained from Sentinel Hub.
    • Adjust the bboxWgs coordinates to define the bounding box of your area of interest.
  4. Run the Code: Execute the Python script, and it will download HLS time series data, filter out cloudy images, and export the results to NetCDF format.

Complete Code:

Let’s break down the provided code step by step:

This section sets up the geometry, bounding box, and converts coordinates from WGS84 to UTM.

1. Geometry and Bounding Box Setup

2. Setting Up Sentinel Hub Configuration

This section initializes the Sentinel Hub configuration, including the instance ID, feature offset, time range, and WFS (Web Feature Service) URL.

3. Retrieve HLS Data and Process

This section retrieves HLS (Heterogeneous Land Surface) data features by making requests to the Sentinel Hub WFS and collects information such as date, CRS (Coordinate Reference System), cloud cover percentage, and feature ID.

4. Download and Process Data for Each Date

This section downloads HLS data for each date, including checking the QA (Quality Assurance) band for cloud cover, evaluating cloud mask, and fetching specific bands.

5. NetCDF Export

This section defines a function to export the processed data to NetCDF format, specifying encoding for each variable and exporting the data to NetCDF files.

This code fetches HLS data from Sentinel Hub, applies cloud masking, and downloads specific bands for a defined region and time period. The resulting data is then stored in NetCDF format for further analysis. Make sure to replace 'your_instance_id' with your actual Sentinel Hub instance ID before running the code.

Conclusion

Congratulations! You’ve successfully created a time series of cloud-free HLS imagery and exported it to NetCDF. This workflow is valuable for various applications, including environmental monitoring and land cover analysis.

Feel free to customize the code further based on your specific needs and explore additional functionalities offered by Sentinel Hub.

I hope this tutorial will create a good foundation for you. If you want tutorials on another GIS topic or you have any queries, please send an email at contact@spatial-dev.guru.

We also offer freelancing services. Please email us at contact@spatial-dev.guru for any query.

Leave a ReplyCancel reply

Discover more from Spatial Dev Guru

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Spatial Dev Guru

Subscribe now to keep reading and get access to the full archive.

Continue reading