加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pkg_modis.js 5.30 KB
一键复制 编辑 原始数据 按行查看 历史
Dongdong Kong 提交于 2019-01-08 19:22 . modify pkg_export and fix pkg_trend
/**** Start of imports. If edited, may not auto-convert in the playground. ****/
var imgcol_gpp = ee.ImageCollection("MODIS/006/MOD17A2H"),
imgcol_PMLV1 = ee.ImageCollection("projects/pml_evapotranspiration/PML/OUTPUT/PML_V1_8day");
/***** End of imports. If edited, may not auto-convert in the playground. *****/
var pkg_export = require('users/kongdd/public:pkg_export.js');
// var pkg_modis = require('users/kongdd/public:pkg_modis.js');
/**
* This function is used to convert 8-day product into monthly
*/
var pkg_modis = {};
pkg_modis.duration = function(dateRange, unit){
unit = unit || 'day';
dateRange = ee.DateRange(dateRange);
return dateRange.end().difference(dateRange.start(), unit);
};
/**
* Sum 8-day images into monthly by weights, which are generated by overlapping
* days with current month.
*/
pkg_modis.d8_to_monthly_img = function(imgcol, date_month){
var bandnames = imgcol.first().bandNames();
// 1. filter images
var monthDate_start = ee.Date(date_month);
var monthDate_end = monthDate_start.advance(1, 'month')
.advance(-1, 'day');
var monthDate_range = ee.DateRange(monthDate_start, monthDate_end);
var monthdays = monthDate_end.difference(monthDate_start, 'day').add(1);
var filter = ee.Filter.or(
ee.Filter.dateRangeContains({
leftValue: monthDate_range,
rightField:'system:time_start'
}),
ee.Filter.dateRangeContains({
leftValue: monthDate_range,
rightField:'date_end'
})
);
var imgcol_i = imgcol.filter(filter)
// .aside(print)
.map(function(img){
var date_start = ee.Date(img.get('system:time_start'));
var date_end = ee.Date(img.get('system:time_end'));
var range_sect = monthDate_range.intersection(ee.DateRange(date_start, date_end));
var days = pkg_modis.duration(range_sect);
// print(range_sect);
return img
.set('range_sect', range_sect)
.set('duration_day', days)
.set('duration_month', days.divide(monthdays))
.set('date_start', date_start)
.set('date_end' , date_end);
});
// 2. get weights
// print(imgcol_i);
var mask = imgcol_i.first().mask();
imgcol_i = imgcol_i.map(function(img){
return img.unmask(-999.0);
});
var array = imgcol_i.toArray(); // [ntime, 1]
var weight = imgcol_i.aggregate_array('duration_month');
weight = ee.Image(ee.Array([weight])); // [1, ntime]
// weight = weight.updateMask(mask.select(0));
// x_month = x_daily * monthDays
var res = ee.Image(weight).matrixMultiply(array).multiply(monthdays)
.arrayProject([1]).arrayFlatten([bandnames]) // 2d to bands
.set('system:time_start', monthDate_start.millis())
.set('system:index', monthDate_start.format('yyyy_MM_dd'))
.updateMask(mask);
res = res.set('images', imgcol_i.toList(100));
// pkg_export.ExportImg(res, 'test2', range, cellsize, type, folder);
// Map.addLayer(array, {}, 'array');
// Map.addLayer(weight, {}, 'weight');
// Map.addLayer(imgcol_i, {}, 'raw x');
// Map.addLayer(res, {}, 'x');
return ee.Image(res);
};
pkg_modis.d8_to_monthly = function(imgcol){
imgcol = ee.ImageCollection(imgcol);
// First, subtract 1sec for date_end to fix DateRange
imgcol = imgcol.map(function(img){
var date_start = ee.Date(img.get('system:time_start'));
var date_end = ee.Date(img.get('system:time_end')).advance(-1, 'second');
var date_month = ee.Date.fromYMD(date_start.get('year'), date_start.get('month'), 1);
return img
.set('date_start', date_start)
.set('date_end' , date_end)
.set('date_month', date_month);
});
// print(imgcol.limit(3));
var months = ee.Dictionary(imgcol.aggregate_histogram('date_month')).keys();
var res = months.map(function(date_month){
return pkg_modis.d8_to_monthly_img(imgcol, date_month);
});
// var x = pkg_modis.d8_to_monthly_img(imgcol, months.get(0));
res = ee.ImageCollection(res);
return res;
};
var debug = true;
if (debug){
var filter = ee.Filter.calendarRange(2016, 2016, 'year');
var imgcol = imgcol_PMLV1.select([0, 1, 2, 3]) //(['Gpp', 'PsnNet'])
.filter(filter)
.map(function(img){
var date = ee.Date(img.get('system:time_start'));
return img.set('system:time_end', date.advance(8, 'day'));
});
// print(imgcol);
var res = pkg_modis.d8_to_monthly(imgcol);
var img = res.first().expression('b(0) + b(1) + b(2)');
var pkg_vis = require('users/kongdd/public:pkg_vis.js');
var vis_vi = {min: 0, max: 1500, palette:pkg_vis.colors.RdYlGn[11]};
var lg_vi = pkg_vis.grad_legend(vis_vi , 'ET');
Map.addLayer(img, vis_vi, 'ET');
var range = [-180, -60, 180, 90],
cellsize = 1/4,
type = 'drive',
folder = 'PML',
crs = 'EPSG:4326';
// pkg_export.ExportImgCol(res, undefined, range, cellsize, type, folder, crs, undefined, 'PMLV1_monthly_');
// print(res);
Map.addLayer(imgcol, {}, 'imgcol raw');
Map.addLayer(res , {}, 'imgcol monthly');
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化