Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ee = require('@google/earthengine')
- const ndvi = (inputs, callback) => {
- let ndvi
- let vis
- const palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
- '74A901', '66A000', '529400', '3E8601', '207401', '056201',
- '004C00', '023B01', '012E01', '011D01', '011301']
- if (inputs.satellite == "SENTINEL") {
- // Map the function over one year of data and take the median.
- // Load Sentinel-2 TOA reflectance data.
- let collection = ee.ImageCollection("COPERNICUS/S2")
- .filterDate(inputs.date[0], inputs.date[1])
- // Pre-filter to get less cloudy granules.
- .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
- //.map(maskS2clouds)
- .filterBounds(inputs.reg)
- let composite = collection.median()
- ndvi = composite.normalizedDifference(['B8', 'B4'])
- vis = { min: 0, max: 1, palette: palette }
- ///////////////////////////////CHART_TEST/////////////////////////////////////////////////////
- var geometry = ee.Geometry.Polygon(
- [[[-7.780510647532537, 33.12380141439545],
- [-7.780510647532537, 33.06210458015715],
- [-7.7106444671126155, 33.06210458015715],
- [-7.7106444671126155, 33.12380141439545]]], null, false);
- // var mask_agricole = ee.FeatureCollection("users/mahyouh/mask_agricole");
- async function to_fr(n,str,str2){
- var ni = n.reduce(ee.Reducer.mean()).reduceRegion({reducer:'mean',geometry: geometry,scale:10,maxPixels: 1e7}).getNumber(str2)
- ni.evaluate(async function(value) {
- console.log(await value)
- fs.appendFile("log_chart.txt",str+","+ await value+",\n", function(err) {
- if(err) {
- return console.log(err);
- }
- });
- });
- }
- function to_nd(n){
- var ni = n.reduce(ee.Reducer.mean()).reduceRegion({reducer:'mean',geometry: geometry,scale:10,maxPixels: 1e7}).getNumber("NDVI_median_mean");
- return ni;
- }
- var years = ee.List.sequence(2003, 2019);
- var months = ee.List.sequence(1, 12);
- ////////////////////////////////////////////////
- ////////// Long term average can be generated directly from the pre-calculated NDVI
- var col = ee.ImageCollection("MODIS/006/MYD13Q1")
- .filterBounds(geometry);
- // select NDVI
- var nd = col.select("NDVI");
- // scaling factor
- var multiply = function(image){
- // multply image
- var img = image.multiply(0.0001);
- return img.set('system:time_start',image.get('system:time_start')) ;
- };
- // use map function to apply function to all maps
- nd = nd.map(multiply);
- var monthlymedianNDVI = ee.ImageCollection.fromImages(
- years.map(function (y) {
- return months.map(function(m) {
- return nd.filter(ee.Filter.calendarRange(y, y, 'year')).filter(ee.Filter.calendarRange(m, m, 'month'))
- .median().set('year', y).set('month', m).set('system:time_start', ee.Date.fromYMD(y, m, 1));
- });
- }).flatten()
- );
- /////////////////////////////////////////////////////Median///////////////////////////////////////////////////////////////////
- var months1 = ee.List.sequence(9, 12);
- var medianMonthlyNDVI = ee.ImageCollection.fromImages(
- months1.map(function (m) {
- var w = monthlymedianNDVI.filter(ee.Filter.eq('month', m)).reduce(ee.Reducer.median());
- return w.set('month', m)
- .set('system:time_start',ee.Date.fromYMD(2019, m, 1));
- }).flatten()
- );
- var months2 = ee.List.sequence(1, 8);
- var medianMonthlyNDVI1 = ee.ImageCollection.fromImages(
- months2.map(function (m) {
- var w = monthlymedianNDVI.filter(ee.Filter.eq('month', m)).reduce(ee.Reducer.median());
- return w.set('month', m)
- .set('system:time_start',ee.Date.fromYMD(2020, m, 1));
- }).flatten()
- );
- var Med_sep = medianMonthlyNDVI.filter(ee.Filter.eq('month', 9));
- var Med_Oct = medianMonthlyNDVI.filter(ee.Filter.eq('month', 10));
- var Med_Nov = medianMonthlyNDVI.filter(ee.Filter.eq('month', 11));
- var Med_Dec = medianMonthlyNDVI.filter(ee.Filter.eq('month', 12));
- var Med_jan = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 1));
- var Med_Fev = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 2));
- var Med_Mars = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 3));
- var Med_Avr = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 4));
- var Med_Mai = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 5));
- var Med_Jun = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 6));
- var Med_jui = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 7));
- var Med_Aout = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 8));
- var arr = [];
- var n_jan = to_nd(Med_jan);
- n_jan.evaluate(function(value){
- arr.push(value);
- var n_fev = to_nd(Med_Fev);
- n_fev.evaluate(function(value){
- arr.push(value);
- var n_mars = to_nd(Med_Mars);
- n_mars.evaluate(function(value){
- arr.push(value);
- var n_avril = to_nd(Med_Avr);
- n_avril.evaluate(function(value){
- arr.push(value)
- var n_mai = to_nd(Med_Mai);
- n_mai.evaluate(function(value){
- arr.push(value)
- var n_jun = to_nd(Med_Jun);
- n_jun.evaluate(function(value){
- arr.push(value)
- var n_jui = to_nd(Med_jui);
- n_jui.evaluate(function(value){
- arr.push(value);
- var n_aout = to_nd(Med_Aout);
- n_aout.evaluate(function(value){
- arr.push(value);
- var n_sep = to_nd(Med_sep);
- n_sep.evaluate(function(value){
- arr.push(value);
- var n_oct = to_nd(Med_Oct);
- n_oct.evaluate(function(value){
- arr.push(value);
- var n_nov = to_nd(Med_Nov);
- n_nov.evaluate(function(value){
- arr.push(value);
- var n_dec = to_nd(Med_Dec);
- n_dec.evaluate(function(value){
- arr.push(value);
- console.log(arr)
- });
- });
- });
- });
- });
- });
- });
- });
- });
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement