Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ee = require('@google/earthengine')
  2.  
  3.  
  4.  
  5. const ndvi = (inputs, callback) => {
  6.     let ndvi
  7.     let vis
  8.     const palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
  9.         '74A901', '66A000', '529400', '3E8601', '207401', '056201',
  10.         '004C00', '023B01', '012E01', '011D01', '011301']
  11.  
  12.     if (inputs.satellite == "SENTINEL") {
  13.         // Map the function over one year of data and take the median.
  14.         // Load Sentinel-2 TOA reflectance data.
  15.         let collection = ee.ImageCollection("COPERNICUS/S2")
  16.             .filterDate(inputs.date[0], inputs.date[1])
  17.             // Pre-filter to get less cloudy granules.
  18.             .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
  19.             //.map(maskS2clouds)
  20.             .filterBounds(inputs.reg)
  21.  
  22.         let composite = collection.median()
  23.  
  24.         ndvi = composite.normalizedDifference(['B8', 'B4'])
  25.         vis = { min: 0, max: 1, palette: palette }
  26.  
  27. ///////////////////////////////CHART_TEST/////////////////////////////////////////////////////
  28.  
  29.   var geometry = ee.Geometry.Polygon(
  30.         [[[-7.780510647532537, 33.12380141439545],
  31.           [-7.780510647532537, 33.06210458015715],
  32.           [-7.7106444671126155, 33.06210458015715],
  33.           [-7.7106444671126155, 33.12380141439545]]], null, false);
  34.  
  35. // var mask_agricole = ee.FeatureCollection("users/mahyouh/mask_agricole");
  36.  
  37.  
  38. async function to_fr(n,str,str2){
  39.  
  40.     var ni = n.reduce(ee.Reducer.mean()).reduceRegion({reducer:'mean',geometry: geometry,scale:10,maxPixels: 1e7}).getNumber(str2)
  41.     ni.evaluate(async function(value) {
  42.        
  43.        console.log(await value)
  44.        fs.appendFile("log_chart.txt",str+","+ await value+",\n", function(err) {
  45.         if(err) {
  46.             return console.log(err);
  47.         }
  48.        
  49.     });
  50.  
  51.     });
  52.  
  53.     }
  54.  
  55. function to_nd(n){
  56.  
  57.     var ni = n.reduce(ee.Reducer.mean()).reduceRegion({reducer:'mean',geometry: geometry,scale:10,maxPixels: 1e7}).getNumber("NDVI_median_mean");
  58.  
  59.     return ni;
  60.  
  61. }
  62.  
  63.  
  64. var years = ee.List.sequence(2003, 2019);
  65. var months = ee.List.sequence(1, 12);
  66.  
  67. ////////////////////////////////////////////////
  68.  
  69.  
  70.  
  71.  ////////// Long term average can be generated directly from the pre-calculated NDVI
  72.  
  73.  
  74. var col = ee.ImageCollection("MODIS/006/MYD13Q1")
  75. .filterBounds(geometry);
  76.  
  77. // select NDVI
  78. var nd = col.select("NDVI");
  79.  
  80. // scaling factor
  81. var multiply = function(image){
  82.   // multply image
  83.   var img = image.multiply(0.0001);
  84.   return img.set('system:time_start',image.get('system:time_start')) ;
  85. };
  86.  
  87. // use map function to apply function to all maps
  88. nd = nd.map(multiply);
  89.  
  90. var monthlymedianNDVI =  ee.ImageCollection.fromImages(
  91.   years.map(function (y) {
  92.     return months.map(function(m) {
  93.     return nd.filter(ee.Filter.calendarRange(y, y, 'year')).filter(ee.Filter.calendarRange(m, m, 'month'))
  94.     .median().set('year', y).set('month', m).set('system:time_start', ee.Date.fromYMD(y, m, 1));
  95.     });
  96.   }).flatten()
  97. );
  98.  
  99.  /////////////////////////////////////////////////////Median///////////////////////////////////////////////////////////////////  
  100. var months1 = ee.List.sequence(9, 12);
  101.  
  102. var medianMonthlyNDVI =  ee.ImageCollection.fromImages(
  103.   months1.map(function (m) {
  104.     var w = monthlymedianNDVI.filter(ee.Filter.eq('month', m)).reduce(ee.Reducer.median());
  105.     return w.set('month', m)
  106.     .set('system:time_start',ee.Date.fromYMD(2019, m, 1));
  107.     }).flatten()
  108.     );
  109.    
  110.  
  111.  
  112.  var months2 = ee.List.sequence(1, 8);
  113.  
  114.  var medianMonthlyNDVI1 =  ee.ImageCollection.fromImages(
  115.   months2.map(function (m) {
  116.     var w = monthlymedianNDVI.filter(ee.Filter.eq('month', m)).reduce(ee.Reducer.median());
  117.     return w.set('month', m)
  118.     .set('system:time_start',ee.Date.fromYMD(2020, m, 1));
  119.     }).flatten()
  120.     );
  121.  
  122.  
  123.   var Med_sep = medianMonthlyNDVI.filter(ee.Filter.eq('month', 9));
  124.   var Med_Oct = medianMonthlyNDVI.filter(ee.Filter.eq('month', 10));
  125.  var Med_Nov = medianMonthlyNDVI.filter(ee.Filter.eq('month', 11));
  126. var Med_Dec = medianMonthlyNDVI.filter(ee.Filter.eq('month', 12));
  127.  var Med_jan = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 1));
  128. var Med_Fev = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 2));
  129. var Med_Mars = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 3));
  130.  var Med_Avr = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 4));
  131. var Med_Mai = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 5));
  132. var Med_Jun = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 6));
  133.  var Med_jui = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 7));
  134. var Med_Aout = medianMonthlyNDVI1.filter(ee.Filter.eq('month', 8));
  135.  
  136. var arr = [];
  137.  
  138. var n_jan = to_nd(Med_jan);
  139.  
  140. n_jan.evaluate(function(value){
  141.     arr.push(value);
  142.  
  143.     var n_fev = to_nd(Med_Fev);
  144.     n_fev.evaluate(function(value){
  145.         arr.push(value);
  146.    
  147.     var n_mars = to_nd(Med_Mars);
  148.     n_mars.evaluate(function(value){
  149.  
  150.         arr.push(value);
  151.     var n_avril = to_nd(Med_Avr);
  152.     n_avril.evaluate(function(value){
  153.  
  154.         arr.push(value)
  155.    
  156.     var n_mai = to_nd(Med_Mai);
  157.     n_mai.evaluate(function(value){
  158.  
  159.         arr.push(value)
  160.  
  161.     var n_jun = to_nd(Med_Jun);
  162.     n_jun.evaluate(function(value){
  163.  
  164.         arr.push(value)
  165.  
  166.     var n_jui = to_nd(Med_jui);
  167.     n_jui.evaluate(function(value){
  168.  
  169.         arr.push(value);
  170.  
  171.         var n_aout = to_nd(Med_Aout);
  172.  
  173.         n_aout.evaluate(function(value){
  174.  
  175.             arr.push(value);
  176.             var n_sep = to_nd(Med_sep);
  177.             n_sep.evaluate(function(value){
  178.  
  179.                 arr.push(value);
  180.  
  181.             var n_oct = to_nd(Med_Oct);
  182.             n_oct.evaluate(function(value){
  183.  
  184.                 arr.push(value);
  185.  
  186.                 var n_nov = to_nd(Med_Nov);
  187.  
  188.                 n_nov.evaluate(function(value){
  189.  
  190.                     arr.push(value);
  191.                 var n_dec = to_nd(Med_Dec);
  192.                 n_dec.evaluate(function(value){
  193.  
  194.                     arr.push(value);
  195.  
  196.                     console.log(arr)
  197.  
  198.  
  199.                                });
  200.                              });
  201.                             });
  202.                            });
  203.                           });
  204.                         });
  205.                        });
  206.                       });
  207.                     });
  208.                    });
  209.                   });
  210.                 });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement