Before starting, make sure you understand these terms. They appear throughout all code blocks in this course.
| Term | Full Name | What it means in simple words |
|---|---|---|
| NDVI | Normalized Difference Vegetation Index | A measure of vegetation greenness using red and near-infrared light. |
| MODIS | Moderate Resolution Imaging Spectroradiometer | A satellite sensor used to collect data about Earth's land, oceans, and atmosphere. |
| Drought | Drought | A prolonged period of abnormally low rainfall, leading to a shortage of water. |
| Remote Sensing | Remote Sensing | Acquiring information about an object or area from a distance, typically using satellites or aircraft. |
| GEE | Google Earth Engine | A cloud-based platform for geospatial analysis. |
| Pixel | Pixel | The smallest unit of an image. |
| Band | Spectral Band | A specific range of wavelengths in the electromagnetic spectrum. |
| Time Series | Time Series | A sequence of data points indexed in time order. |
| Threshold | Threshold | A value or level at which something starts or changes. |
| Vegetation Index | Vegetation Index | A numerical indicator derived from satellite data that reflects vegetation characteristics. |
| Rif Mountains | Rif Mountains | A mountain range in northern Morocco. |
| Middle Atlas | Middle Atlas | A mountain range in Morocco, part of the Atlas Mountains. |
| DeepSeek | DeepSeek | AI model for assisting with code generation. |
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.
MODIS/061/MOD13A1You need two browser tabs open side by side throughout this course.
Course6_Drought Monitoring with MODIS NDVI and DeepSeek
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.
Paste the following code into your GEE code editor and click Run. Read the beginner notes below to understand what each line does.
// 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.
};
Add this block after the previous code. It continues the analysis with additional processing and visualization.
// 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);
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.
// 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'
});
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.
To get the best results from DeepSeek for GEE tasks, always include these elements in your prompts:
| Element | Example |
|---|---|
| Exact dataset ID | MODIS/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" |