0

I am a new student and have been trying to export images from GEE. The following code generates Mean Surface Soil Moisture for each month in Afghanistan over the course of 6 years. I export the data as a table. How can I generate the images and export the images?

// SMAP soil moisture data, Extract & Visualize Monthly Time Series Analysis of SMAP soil moisture and export to your Google Drive as a CSV file

// 1. Define countries boundary
var Countries = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');
var roi = Countries.filter(ee.Filter.eq('country_na', 'Afghanistan'));
Map.addLayer(roi, {}, "roi");
Map.centerObject(roi);

// 2. List of years
var years = ee.List.sequence(2016, 2021);
var months = ee.List.sequence(1, 12);

// 3. Load SMAP Data
var coll = ee.ImageCollection('NASA_USDA/HSL/SMAP10KM_soil_moisture').select('ssm');
print(coll.first());

// 4. Set visualization parameter
var soilVis = {
min: 0.0,
max: 28.0,
palette: ['0300ff', '418504', 'efff07', 'efff07', 'ff0303'],
};

// 5. Center and add SMAP layer
Map.centerObject(roi); // Zoom level ranges from 1 to 16
Map.addLayer(coll.mean().clip(roi), soilVis, 'Soil Moisture');

// 6. Summaries our SMAP (Soil Moisture Active Passive) soil moisture data by month and year
var smap = coll.select('ssm')
.map(function(img){
var d = ee.Date(ee.Number(img.get('system:time_start')));
var m = ee.Number(d.get('month'));
var y = ee.Number(d.get('year'));
return img.set({'month':m, 'year':y});
});
print(smap.first());

// 7. Function generate monthly soil moisture data for each year
var byYearMonth = ee.ImageCollection.fromImages(
years.map(function(y){
return months.map(function(m) {
return smap.filterMetadata('year', 'equals', y)
.filterMetadata('month', 'equals', m)
.select('ssm').mean()
.set('year', y)
.set('month', m)
.set('date', ee.Date.fromYMD(y,m,1));
});
}).flatten()
);
print("monthlyCol", byYearMonth.first());

// 8. Zonal statistics to sumarries SMAP soil moisture to specific study area (eg Afghanistan)
var smapAfghanistan = byYearMonth.map(function(img) {
var features = roi.map(function(f) {return f.set('date', img.get('date'), 'month', img.get('month'), 'year', img.get('year'))})
var proj = ee.Image(byYearMonth.first()).projection();
return img.reduceRegions(features, ee.Reducer.mean(), 1000, proj);
}).flatten();
print("SMAP Summary Mean", smapAfghanistan.limit(10)); 

// 9. Export the resulting mean soil moisture as a table to Google Drive
var selectors = "year, month, country_na, mean";
Export.table.toDrive({
collection: smapAfghanistan,
description: 'SMAP_Timeseries',
folder: 'earth_engine_data',
fileNamePrefix: 'SMAP_Timeseries',
fileFormat: 'CSV',
selectors: selectors
});

From here, I would like to export the images but am unsure how to. This images were generated, is it the variable 'smapAfghanistan'?

I tried the following but it did not produce any tasks.

var batch = require('users/fitoprincipe/geetools:batch');

// Set export folder (relative to Google Drive root folder)
var output_folder = 'gee-export';

// Export collection image to Drive
batch.Download.ImageCollection.toDrive(
smapAfghanistan,
output_folder,
{
name: 'name'
scale: 30,
maxPixels: 1e13,
region: roi,
type: 'int16'
}
);

1 Answer 1

0

I am guessing that the image collection that you actually want to export is byYearMonth, which gives you the (temporal) mean soil moisture value for each pixel within a given year/month over your roi (Afghanistan).

smapAfghanistan is not an image collection, it is a FeatureCollection (table) that is giving you the (spatial) mean for each image in byYearMonth.

Here's how you can export the images in byYearMonth with some minimal modification of your code:

batch.Download.ImageCollection.toDrive(
byYearMonth,
output_folder,
{
  name:'{yearmon}',
  scale: 30,
  maxPixels: 1e8,
  region: roi,
  type: 'int16'
}
);

I changed your parameter name from 'name' to a property called 'yearmon'. If you leave 'name' then all your images will be exported with the name 'name', so my guess is either they will be overwritten, or GoogleDrive might append brackets to distinguish them. Instead of guessing what will happen, let's just define a better naming convention. According to the documentation of geetools, there is some flexibility on how you can form the names for each image, e.g. by using properties from your images enclosed in curly brackets {}.

I decided to simply add a yearmon property to your images, and this just requires one extra line in your step 7:

// 7. Function generate monthly soil moisture data for each year
var byYearMonth = ee.ImageCollection.fromImages(
years.map(function(y){
return months.map(function(m) {
return smap.filterMetadata('year', 'equals', y)
.filterMetadata('month', 'equals', m)
.select('ssm').mean()
.set('year', y)
.set('month', m)
.set('date', ee.Date.fromYMD(y,m,1))
.set('yearmon', ee.Number(y).format("%04d").cat(ee.Number(m).format("%02d"))); // This is the extra line. 
});
}).flatten()
);
print("monthlyCol", byYearMonth.first());

There are other potential improvements to your code for obtaining the monthly averages, but I limited my answer to do what you asked with minimal modification to your code.

https://code.earthengine.google.com/7fbee6dc8345d34a21785a3ad150fa35

2
  • Thank you Oliver Lopez. That is also helpful to have them named with the yearmon property. I had an issue with downloading the images. The task would fail to submit because the pixels were higher than my max pixel value. When I changed it to 1e10, it was very slow and I could not get it to complete the task and stopped it at 10 minutes. So I changed the scale to 500, which completed the task and allowed me to download the photo. However it was a .tif file with just a complete black image. Do you know if this is because of the file type? Or if you have another way of downloading images. Thank u
    – Mas
    Commented Apr 5, 2023 at 18:58
  • The native resolution of the data you are using is 10000 meters (see developers.google.com/earth-engine/datasets/catalog/…). There is no point in using 30m for your analysis. Commented Apr 6, 2023 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.