Practical Course 6

Drought Monitoring with MODIS NDVI and DeepSeek

Learn how to use satellite imagery and AI to monitor drought conditions in North Morocco.
North Morocco ~2 hours Master - Climate Change and GeoAI
Dataset: MODIS/061/MOD13A1 MODIS Terra Vegetation Indices Monthly Global 250m

📖 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
NDVINormalized Difference Vegetation IndexA measure of vegetation greenness using red and near-infrared light.
MODISModerate Resolution Imaging SpectroradiometerA satellite sensor used to collect data about Earth's land, oceans, and atmosphere.
DroughtDroughtA prolonged period of abnormally low rainfall, leading to a shortage of water.
Remote SensingRemote SensingAcquiring information about an object or area from a distance, typically using satellites or aircraft.
GEEGoogle Earth EngineA cloud-based platform for geospatial analysis.
PixelPixelThe smallest unit of an image.
BandSpectral BandA specific range of wavelengths in the electromagnetic spectrum.
Time SeriesTime SeriesA sequence of data points indexed in time order.
ThresholdThresholdA value or level at which something starts or changes.
Vegetation IndexVegetation IndexA numerical indicator derived from satellite data that reflects vegetation characteristics.
Rif MountainsRif MountainsA mountain range in northern Morocco.
Middle AtlasMiddle AtlasA mountain range in Morocco, part of the Atlas Mountains.
DeepSeekDeepSeekAI model for assisting with code generation.

Contents

  1. Introduction to Drought Monitoring
  2. Understanding NDVI
  3. Accessing MODIS Data in GEE
  4. Calculating NDVI Time Series
  5. Visualizing NDVI Trends
  6. Drought Thresholds and Classification
  7. Using DeepSeek for Code Generation
  8. Case Study: North Morocco
  9. Exercises
  10. Conclusion
1
Introduction
10 min

Droughts pose a significant threat to agriculture and water resources, especially in regions like North Morocco. This course will guide you through using MODIS NDVI data in Google Earth Engine to monitor drought conditions. You will also learn how to leverage DeepSeek to automate parts of the coding process.

GEE Asset ID
MODIS/061/MOD13A1
Dataset Name
MODIS Terra Vegetation Indices Monthly Global 250m
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 Drought Monitoring with MODIS NDVI and DeepSeek, 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 Course6_Drought Monitoring with MODIS NDVI and DeepSeek
💡
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 the average NDVI from the MODIS/061/MOD13A1 dataset for the Rif Mountains and Middle Atlas region of North Morocco between January 1, 2020, and December 31, 2023. Use the 'NDVI' band. Calculate the average NDVI for each year. Display the results as a time series chart. Filter the MODIS image collection to remove cloudy pixels. The Rif Mountains and Middle Atlas boundary should be defined as a FeatureCollection. Export a table with the average NDVI values per year. Provide comments explaining each step.
✨
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
Loading and Filtering MODIS Data
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.

Loading and Filtering MODIS Data
// Define the region of interest (Rif Mountains and Middle Atlas)
var roi = ee.FeatureCollection('users/yourusername/Rif_MiddleAtlas'); // Replace with your asset ID

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

// Load the MODIS NDVI dataset
var modis = ee.ImageCollection('MODIS/061/MOD13A1')
  .filterDate(startDate, endDate)
  .filterBounds(roi);

// Function to mask clouds (example, requires adaptation to MODIS QA)
var maskClouds = function(image) {
  return image.updateMask(image.select('NDVI').gt(-10000)); //Simple mask assuming NDVI values below -10000 are invalid.
};
🔑
Beginner note: First, we define the region of interest (ROI) using a FeatureCollection. Then, we specify the start and end dates for our analysis. We load the MODIS NDVI dataset and filter it by date and region. Finally, we define a function `maskClouds` to remove cloudy pixels based on NDVI values. Remember to replace 'users/yourusername/Rif_MiddleAtlas' with the actual path to your FeatureCollection.

5
Calculating and Visualizing NDVI Time Series
10 min

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

Calculating and Visualizing NDVI Time Series
// Apply cloud masking
var modisMasked = modis.map(maskClouds);

// Function to calculate mean NDVI for each image and year
var calculateMeanNDVI = function(year) {
  var start = ee.Date.fromYMD(year, 1, 1);
  var end = ee.Date.fromYMD(year, 12, 31);
  var annualNDVI = modisMasked.filterDate(start, end).select('NDVI');
  var meanNDVI = annualNDVI.mean().multiply(0.0001); // Scale factor for MODIS NDVI
  return meanNDVI.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: roi,
    scale: 250,
    maxPixels: 1e9
  }).set('year', year);
};

// Generate a list of years
var years = ee.List.sequence(2020, 2023);

// Map the function over the years
var annualNDVIValues = years.map(calculateMeanNDVI);

// Print the results
print(annualNDVIValues);
🔑
Beginner note: This block applies the cloud masking function to the MODIS collection. It then defines a function `calculateMeanNDVI` to calculate the mean NDVI for each year within the specified ROI. This function filters the MODIS data by year, calculates the mean, and reduces the region to a single mean value. A list of years is created, and the `calculateMeanNDVI` function is mapped over this list. Finally, the results (annual NDVI values) are printed to the console.

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
// Convert the results to a FeatureCollection
var annualNDVICollection = ee.FeatureCollection(annualNDVIValues);

// Export the table to Google Drive
Export.table.toDrive({
  collection: annualNDVICollection,
  description: 'MODIS_NDVI_Annual_Means',
  fileFormat: 'CSV'
});
🔑
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.

Explore Different Date Ranges

  1. Change the start and end dates to analyze a different period.
  2. Observe how the NDVI values change over time.

Modify the Cloud Masking

  1. Adjust the threshold in the `maskClouds` function to be more or less aggressive.
  2. Examine the impact on the resulting NDVI values.

Analyze a Different Region

  1. Replace the `roi` with a different FeatureCollection representing another region in Morocco.
  2. Compare the NDVI trends in the new region with those in the Rif Mountains and Middle Atlas.

Explore other MODIS bands

  1. Use the EVI band instead of NDVI.
  2. Compare the results and see if the drought signal is clearer.

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 IDMODIS/061/MOD13A1
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 6 - Drought Monitoring with MODIS NDVI and DeepSeek | GeoAI for Climate Change | Dr. Ayoub Lahlouh | UM6P/CUS | 2025/2026