我已经编写了一个非常完美的代码(即使我确定它可以改进)。 这段代码为我提供了CHIRPS项目的每月降雨数据。我对GEE非常陌生,我想知道是否有任何方法可以获取特定投资回报率上的每周数据,而不是每月数据。
特别是,我正在使用该函数创建每月数据的模板,但不知道对其进行修改以获取每周数据(函数ee.Date.FromYMD
)
var startyear = 2008;
var endyear = 2017;
// Set date in ee date format
var startdate = ee.Date.fromYMD(startyear,1,1);
var enddate = ee.Date.fromYMD(endyear,12,31)
请参阅下面的完整代码。
var chirps = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY')
// import rainfall stations
// Define time range
var startyear = 2008;
var endyear = 2017;
// Set date in ee date format
var startdate = ee.Date.fromYMD(startyear,1,1);
var enddate = ee.Date.fromYMD(endyear,12,31)
// create list for years
var years = ee.List.sequence(startyear,endyear);
// make a list with months
var months = ee.List.sequence(1,12);
// Filter chirps
var Pchirps = chirps.filterDate(startdate, enddate)
// Sort chronologically in descending order.
.sort('system:time_start', false)
.filterBounds(Rainfallstations)
.select("precipitation");
// calculate the P for each month
var MonthlyChirps = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function(m){
var w = Pchirps.filter(ee.Filter.calendarRange(y, y, 'year'))
.filter(ee.Filter.calendarRange(m, m, 'month'))
.sum();
return w.set('year', y)
.set('month', m)
.set('system:time_start',ee.Date.fromYMD(y,m,1))
.set('date', ee.Date.fromYMD(y,m,1))
});
}).flatten());
// Collect region, image, value triplets.
var triplets = MonthlyChirps.map(function(image) {
return image.reduceRegions({
collection: Rainfallstations.select(['ADM2_NAME']),
reducer: ee.Reducer.mean(),
scale: 1000
}).filter(ee.Filter.neq('mean', null))
.map(function(f) {
return f.set('imageId', image.id()).setGeometry(null);
});
}).flatten();
print(triplets.first());
// Format a table of triplets into a 2D table of rowId x colId.
var format = function(table, rowId, colId) {
// Get a FeatureCollection with unique row IDs.
var rows = table.distinct(rowId);
// Join the table to the unique IDs to get a collection in which
// each feature stores a list of all features having a common row ID.
var joined = ee.Join.saveAll('matches').apply({
primary: rows,
secondary: table,
condition: ee.Filter.equals({
leftField: rowId,
rightField: rowId
})
});
return joined.map(function(row) {
// Get the list of all features with a unique row ID.
var values = ee.List(row.get('matches'))
// Map a function over the list of rows to return a list of
// column ID and value.
.map(function(feature) {
feature = ee.Feature(feature);
return [feature.get(colId), feature.get('mean')];
});
// Return the row with its ID property and properties for
// all matching columns IDs storing the output of the reducer.
// The Dictionary constructor is using a list of key, value pairs.
return row.select([rowId]).set(ee.Dictionary(values.flatten()));
});
};
var table1 = format(triplets, 'imageId', 'ADM2_NAME');
var desc1 = 'table_demo_';
Export.table.toDrive({
collection: table1,
description: desc1,
fileNamePrefix: desc1,
fileFormat: 'CSV'
});