Practical Course 16

Soil Moisture from Space

Estimate soil moisture using Sentinel-1 SAR data and AI-powered code generation.
North Morocco ~2 hours Master - Climate Change and GeoAI
Dataset: COPERNICUS/S1_GRD Sentinel-1 Ground Range Detected (GRD)

📖 Key Terms for Beginners

Before starting, make sure you understand these terms. They appear throughout all code blocks in this course.

TermFull NameWhat it means in simple words
SARSynthetic Aperture RadarA type of radar that creates two-dimensional images or three-dimensional reconstructions of objects, such as landscapes.
GRDGround Range DetectedSentinel-1 data that has been processed to represent the radar signal strength on the ground.
VVVertical-Vertical PolarizationThe radar signal is transmitted and received vertically.
VHVertical-Horizontal PolarizationThe radar signal is transmitted vertically and received horizontally.
Soil MoistureSoil MoistureThe amount of water present in the soil.
BackscatterRadar BackscatterThe amount of the radar signal that is reflected back to the sensor.
Sentinel-1Sentinel-1A European radar imaging satellite.
GEEGoogle Earth EngineA cloud-based platform for geospatial analysis.
SpeckleSpeckle NoiseGranular noise inherent in SAR images.
DeepSeekDeepSeekA large language model used to generate code.
FilteringImage FilteringProcess of removing noise or unwanted features from an image.
Incidence AngleIncidence AngleThe angle at which the radar beam hits the Earth's surface.

Contents

  1. Introduction to Soil Moisture and SAR
  2. Sentinel-1 Data in Google Earth Engine
  3. Data Filtering and Preprocessing
  4. Visualizing SAR Data
  5. Introduction to DeepSeek
  6. Generating Code with DeepSeek
  7. Analyzing Soil Moisture Trends
  8. Limitations and Considerations
  9. Advanced Techniques
  10. Conclusion
1
Introduction
10 min

Soil moisture is a critical component of the Earth's climate system, influencing weather patterns, agriculture, and ecosystem health. This course will guide you through using Sentinel-1 SAR data in Google Earth Engine (GEE) to estimate soil moisture in North Morocco, leveraging the power of DeepSeek for code generation.

GEE Asset ID
COPERNICUS/S1_GRD
Dataset Name
Sentinel-1 Ground Range Detected (GRD)
Study Area
North Morocco
AI Tool
DeepSeek (free, no API key needed)
🌍
What you will build: By the end of this course you will have a working GEE script that analyzes Soil Moisture from Space, computes statistics, visualizes results on an interactive map, and exports data to Google Drive.

2
Setup: GEE + DeepSeek
5 min

You need two browser tabs open side by side throughout this course.

1
Open chat.deepseek.com -- free account, no API key needed
2
Open code.earthengine.google.com in a second tab
3
Create a new GEE script named Course16_Soil Moisture from Space
💡
Workflow: Write a prompt in DeepSeek describing your task with exact dataset IDs and requirements. Copy the generated GEE JavaScript. Paste into GEE code editor. Click Run. Inspect results. Iterate.

3
Crafting the DeepSeek Prompt
15 min

The prompt is the core skill of this course. A good prompt includes the exact dataset ID, band names, study area coordinates, dates, preprocessing steps, and desired outputs.

📋 Main Prompt -- copy and paste this into DeepSeek
Write a Google Earth Engine script to calculate soil moisture from Sentinel-1 GRD data in the Rif Mountains and Middle Atlas region of North Morocco between January 1, 2023, and December 31, 2023. Use the VV and VH bands. Apply a speckle filter. Calculate the Normalized Difference between VV and VH. Display the resulting soil moisture map. Include comments explaining each step. Use the 'COPERNICUS/S1_GRD' dataset.
✨
After pasting the prompt: Wait 20-30 seconds for DeepSeek to generate the full GEE script. Copy the entire code block it produces. If the output is cut short, type "Continue the code" to get the rest.

4
Data Acquisition and Filtering
10 min

Paste the following code into your GEE code editor and click Run. Read the beginner notes below to understand what each line does.

Data Acquisition and Filtering
// Define the region of interest (North Morocco)
var roi = ee.Geometry.Polygon([[[-5.5, 35.0], [-2.0, 35.0], [-2.0, 32.0], [-5.5, 32.0]]]);

// Define the date range
var startDate = '2023-01-01';
var endDate = '2023-12-31';

// Load Sentinel-1 GRD data
var sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD')
  .filter(ee.Filter.eq('instrumentMode', 'IW'))
  .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VV'))
  .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VH'))
  .filter(ee.Filter.eq('orbitProperties_pass', 'DESCENDING'))
  .filter(ee.Filter.date(startDate, endDate))
  .filterBounds(roi);

//Print the number of images
print('Number of Sentinel-1 images:', sentinel1.size());
🔑
Beginner note: This code block first defines the region of interest (ROI) and the date range for the analysis. Then, it loads the Sentinel-1 GRD data, filtering it by instrument mode, polarization (VV and VH), orbit properties, date, and region of interest. Finally, prints the number of images.

5
Speckle Filtering and Soil Moisture Calculation
10 min

Add this block after the previous code. It continues the analysis with additional processing and visualization.

Speckle Filtering and Soil Moisture Calculation
// Function to apply a simple speckle filter (e.g., median filter)
var speckleFilter = function(image) {
  var vv = image.select('VV');
  var vvFiltered = vv.focal_median();
  return image.addBands(vvFiltered.rename('VV_filtered')).copyProperties(image, ['system:time_start']);
};

// Apply speckle filtering to the Sentinel-1 data
var sentinel1Filtered = sentinel1.map(speckleFilter);

// Function to calculate Normalized Difference Soil Moisture Index (NDMI)
var calculateNDMI = function(image) {
  var vv = image.select('VV_filtered');
  var vh = image.select('VH');
  var ndmi = vv.subtract(vh).divide(vv.add(vh)).rename('NDMI');
  return image.addBands(ndmi).copyProperties(image, ['system:time_start']);
};

// Calculate NDMI
var sentinel1NDMI = sentinel1Filtered.map(calculateNDMI);

//Composite the images into one image
var composite = sentinel1NDMI.median();
🔑
Beginner note: This code block defines a speckle filter function (using a median filter) and applies it to the Sentinel-1 data. Then, it defines a function to calculate the Normalized Difference Soil Moisture Index (NDMI) using the filtered VV and VH bands. Finally, the composite image is created by using median reducer.

6
Export Results to Google Drive
5 min

Add this block at the end of your script to export the final map as a GeoTIFF to your Google Drive. After clicking Run, go to the Tasks tab in GEE and click Run next to each export task.

Export to Google Drive
// Export the NDMI image to Google Drive
Export.image.toDrive({
  image: composite.select('NDMI'),
  description: 'Sentinel1_NDMI_Morocco',
  folder: 'GEE_Exports',
  scale: 10,
  region: roi,
  maxPixels: 1e10
});
🔑
Beginner note -- Export.image.toDrive(): This function does not export immediately. It creates a task in the GEE Tasks tab that you must manually start by clicking Run. The file will appear in your Google Drive folder (GEE_Courses) as a GeoTIFF that you can open in QGIS or ArcGIS.

7
Exercises Using DeepSeek Prompts
Homework

Use DeepSeek to extend the script for each exercise below. Write a follow-up prompt describing what you want to add, then copy the generated code and paste it into your GEE script.

Exploring Different Speckle Filters

  1. Implement a different speckle filter, such as the Lee filter.
  2. Compare the results with the median filter visually and statistically.

Analyzing Temporal Trends

  1. Create a time series chart of the NDMI values over the year.
  2. Identify periods of high and low soil moisture.

Impact of Incidence Angle

  1. Investigate the relationship between incidence angle and backscatter.
  2. Correct the backscatter values for incidence angle effects (optional, more advanced).

Comparison with other Soil Moisture products

  1. Find a soil moisture product in GEE (e.g. from SMAP) and compare it with the results from Sentinel-1.
  2. Discuss the differences between the products.

8
Prompt Engineering Tips
Reference

To get the best results from DeepSeek for GEE tasks, always include these elements in your prompts:

ElementExample
Exact dataset IDCOPERNICUS/S1_GRD
Band names"select band 'B4' for red, 'B8' for NIR"
Study area"rectangle [-6.5, 33.0, -1.5, 36.0] North Morocco"
Date range"filter from 2020-01-01 to 2023-12-31"
Output format"print area in hectares, add a legend, export to Drive"
Error fixing"The code gives error X. Please fix it."
Iteration"Add a time series chart for each year 2018-2023"
🧠
Best practice: Start with the main prompt, run the code, then send follow-up prompts to add features one at a time. This gives you cleaner, more reliable code than asking for everything at once.
Course 16 - Soil Moisture from Space | GeoAI for Climate Change | Dr. Ayoub Lahlouh | UM6P/CUS | 2025/2026