Google Earth Engine Tutorial: Analyze Seasonal Temperature with Harmonics
Credit: Youtube Channel “Terra Spatial, Learn how to perform seasonal temperature analysis using harmonic modeling techniques in GEE.”
You can see all the tutorials from here: Techgeo Academy.
Google Earth Engine Tutorial: Analyze Seasonal Temperature with Harmonics
Google Earth Engine (GEE) is a powerful platform for planetary-scale geospatial analysis. This tutorial demonstrates how to use harmonic analysis to model seasonal temperature variations using time-series data. Harmonic analysis involves decomposing periodic signals, like temperature cycles, into sinusoidal components to identify patterns and trends.
Data Preparation
Step 1: Load a temperature dataset. For this example, we use the MODIS Land Surface Temperature product (MODIS/006/MOD11A1).
var dataset = ee.ImageCollection("MODIS/006/MOD11A1")
.filterDate('2020-01-01', '2020-12-31')
.select('LST_Day_1km');
Step 2: Define time as a continuous variable (e.g., days since the start of the year).
var time = ee.ImageCollection(dataset.map(function(image) {
return image.set('time', image.date().getRelative('day', 'year'));
}));
Harmonic Analysis
Step 3: Apply harmonic functions (e.g., sine and cosine) for seasonality. The key is to compute regression coefficients for the sinusoidal model.
var harmonics = time.map(function(image) {
var t = image.get('time');
return image
.addBands(ee.Image(ee.Number(t).cos()).rename('cos'))
.addBands(ee.Image(ee.Number(t).sin()).rename('sin'));
});
Step 4: Use linear regression to derive the harmonic coefficients. The model assumes a general form: LST = A + B*cos(θ) + C*sin(θ), where θ = 2π*time/365.
var regression = harmonics.select(['cos', 'sin', 'LST_Day_1km'])
.reduce(ee.Reducer.linearRegression(2, 1));
Visualization and Interpretation
Step 5: Extract and visualize coefficients. The regression object contains the regression matrix, which you can process to retrieve amplitude and phase.
var coefficients = regression.select('coefficients');
var amplitude = coefficients.arraySlice(0, 0, 1).arrayTranspose();
var phase = coefficients.arraySlice(0, 1, 2).arrayTranspose();
Step 6: Display the results, such as the maximum temperature amplitude and the time of the seasonal peak.
Map.addLayer(amplitude, {min: 0, max: 10}, 'Amplitude');
Map.addLayer(phase, {min: 0, max: 365}, 'Phase');
Exporting Results
You can export the processed data using Export.image.toDrive() for further analysis in GIS software or spreadsheets.
Export.image.toDrive({
image: amplitude,
description: 'temperature_harmonics',
folder: 'GEE_Exports',
fileNamePrefix: 'seasonal_amplitude',
region: geometry,
crs: 'EPSG:4326',
scale: 1000,
maxPixels: 1e10
});
FAQ
What is harmonic analysis in the context of temperature data?
Harmonic analysis models periodic climate patterns (like seasons) using sine and cosine functions. It helps extract dominant cycles in temperature time series.
Which datasets are suitable for harmonic analysis in GEE?
Any time-series dataset with daily, monthly, or annual resolution is suitable, such as MODIS, Landsat, or ERA5 weather data.
How do I handle missing values or gaps in the dataset?
Use the qualityMasks() function or fill gaps with interpolation before applying harmonics. GEE offers tools like imageCollection.toImage() for gap removal.
Can I compute multiple harmonics (e.g., annual and biannual cycles)?
Yes! Add more terms (e.g., cos(2*θ), sin(2*θ)) to the model to capture multiple cycles, enhancing accuracy for complex patterns.
How do I interpret the output coefficients?
Coefficients represent the model’s parameters. Amplitude reflects the strength of the seasonal cycle, while phase indicates the timing (e.g., peak temperature date).







