1

I wrote a code with different function to bring me different output values e.g. the values at specific dates, statistics, cloud cover, etc. I printed them all individually in the console but now I would like to have one table or list with all the information so that I can export it easily. Some of the values are in lists and some are in a feature collection.

I would like a table where for each date where there is an image within the given time, it shows the image name, the absolut value of point pt (B8 list), the standard deviation and the mean from statistics and the cloud cover score. The null values should also be included and just have a null values or empty cells. It should conclude in a table/list with 36 dates.

How could I go about this?

var myB8 = l8.select("B8");

var getB8 = function(image) {

  // Reducing region and getting value
  var value_B8 = ee.Image(image)
    .reduceRegion(ee.Reducer.first(), pt)
    .get('B8');

  return value_B8;
};

var count = myB8.size();

var listOfImages = l8.toList(count);

var B8_list = listOfImages.map(getB8);

print("B8 list", B8_list);

var allDates = l8.aggregate_array('system:time_start');

var allDatesSimple = allDates.map(function procDates (ele) {
  
  return ee.Date(ele).format().slice(0,10);
  
});

var paired = allDatesSimple.zip(B8_list);

print (paired);

// RATIO
var RatioALL = idxList.map(function  calculateRatio (ele) {
  
  var idx = ele;

  var value = ee.Number(B8_list.get(idx));
  
  var image = listOfImages.get(idx);
  
  var ratio1 = ee.Image(image).select('B8')
                              .clip(poly)
                              .divide(value);

  var time = ee.Image(image).get('system:time_start');

  return ratio1.set('system:time_start', time);
  
});

print("Original Image Collection", myB8);

print ("Ratio All for Non Null Values", RatioALL);

//statistics
var getStats = function(image) {
  
  var reducers = ee.Reducer.mean().combine({
  reducer2: ee.Reducer.stdDev(),
  sharedInputs: true
  });

  var stats1 = ee.Image(image).reduceRegion({
    reducer: reducers,
    geometry: poly,
    scale: 15,
    bestEffort: true
  });
  
  return ee.Image(image).set(stats1);

};

var statistics = RatioALL.map(getStats);

print("statistics", statistics);

//Cloud cover
var getCloudScores = function(img){
    //Get the cloud cover
    var value = ee.Image(img).get('CLOUD_COVER');
    return ee.Feature(null, {'score': value})
};


var input = l8.map(function(image) { return image.clip(poly); });
var results = input.map(getCloudScores);
print ('Cloud cover', results);
7
  • Place the code explicitly in your question but it can be closed.
    – xunilk
    Mar 31, 2021 at 16:29
  • Do you mean to put the code itself in the question because it gets closed otherwise?
    – xdsccc
    Mar 31, 2021 at 17:50
  • Yes, because some administrators ignores links to code and close the question. It is a possibility.
    – xunilk
    Mar 31, 2021 at 18:05
  • Ok changed it, thanks!
    – xdsccc
    Mar 31, 2021 at 18:06
  • You can also include a shorter portion of your code (where issue is probably located) and your link to the full code in GEE.
    – xunilk
    Mar 31, 2021 at 18:29

1 Answer 1

0

I followed the evolution of this question since your former questions and, some of previous codes are producing results that should be changed in this one. I did such changes for producing a list showing, in following order, image date, the absolute value of point pt (B8 list), the image name, the cloud cover score and the mean and standard deviation from statistics.

As there are lists with different elements number due to nulls presence, I had to make compatible them including nulls in correct order, in lists with the least number of element, by using an adequate function. Afterward, I produced a feature collection for MODISofstation2 geometry associating list values to it for finally exporting it to Google Drive. Complete code (without involved geometries) looks as follows:

// Modis Cloud Masking example.
// Calculate how frequently a location is labeled as clear (i.e. non-cloudy)
// according to the "internal cloud algorithm flag" of the MODIS "state 1km"
// QA band.

// A function to mask out pixels that did not have observations.
var maskEmptyPixels = function(image) {
  // Find pixels that had observations.
  var withObs = image.select('num_observations_1km').gt(0);
  return image.updateMask(withObs);
};

// A function to mask out cloudy pixels.
var maskClouds = function(image) {
  // Select the QA band.
  var QA = image.select('state_1km');
  // Make a mask to get bit 10, the internal_cloud_algorithm_flag bit.
  var bitMask = 1 << 10;
  // Return an image masking out cloudy areas.
  return image.updateMask(QA.bitwiseAnd(bitMask).eq(0));
};

// Start with an image collection for a 1 month period.
// and mask out areas that were not observed.
var collection = ee.ImageCollection('MODIS/006/MOD09GA')
        .filterDate('2019-06-01', '2019-06-30')
        .map(maskEmptyPixels);

// Get the total number of potential observations for the time interval.
var totalObsCount = collection
        .select('num_observations_1km')
        .count();

// Map the cloud masking function over the collection.
var collectionCloudMasked = collection.map(maskClouds);

// Get the total number of observations for non-cloudy pixels for the time
// interval.  The result is unmasked to set to unity so that all locations
// have counts, and the ratios later computed have values everywhere.
var clearObsCount = collectionCloudMasked
        .select('num_observations_1km')
        .count()
        .unmask(0);

Map.addLayer(
    collectionCloudMasked.median(),
    {bands: ['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03'],
     gain: 0.07,
     gamma: 1.4
    },
    'median of masked collection'
  );

Map.addLayer(
    totalObsCount,
    {min: 84, max: 92},
    'count of total observations',
    false
  );

Map.addLayer(
    clearObsCount,
    {min: 0, max: 90},
    'count of clear observations',
    false
  );

Map.addLayer(
    clearObsCount.toFloat().divide(totalObsCount),
    {min: 0, max: 1},
    'ratio of clear to total observations'
  );

// LANDSAT 

// This example demonstrates the use of the Landsat 8 QA band to mask clouds.

// Function to mask clouds using the quality band of Landsat 8.
var maskL8 = function(image) {
  var qa = image.select('BQA');
  /// Check that the cloud bit is off.
  // See https://www.usgs.gov/land-resources/nli/landsat/landsat-collection-1-level-1-quality-assessment-band
  var mask = qa.bitwiseAnd(1 << 4).eq(0);
  return image.updateMask(mask);
};

var start = '2020-01-01';
var finish = '2020-12-30';
var pt = ee.Geometry.Point([-49.31582,69.56833]);
var pt2 = ee.Geometry.Point([-61.1127,77.1826]);
Map.addLayer(pt2);
// Map the function over one year of Landsat 8 TOA data and take the median.
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate (start,finish)
.filterBounds(pt)
.filter("WRS_ROW <= 122")
.map(maskL8);

//Map.addLayer(B8);
//Map. addLayer(pt)
Map.centerObject(pt, 16);

//print(l8);

// B8 VALUE OF PT 

var myB8 = l8.select("B8");
//print("myB8",myB8); 

var getB8 = function(image) {

  // Reducing region and getting value
  var value_B8 = ee.Image(image)
    .reduceRegion(ee.Reducer.first(), pt)
    .get('B8');

  return value_B8;
};

var count = myB8.size();

var listOfImages = l8.toList(count);

var B8_list = listOfImages.map(getB8);

//print("B8 list", B8_list);

//null values 

var nonNulls = B8_list.filter(ee.Filter.neq('item', null));

//print("Non Null Values", nonNulls);

var idxList = nonNulls.map(function extract (ele) {
  
  var idx1 = nonNulls.indexOf(ele);
  
  var idx2 = B8_list.indexOf(nonNulls.get(idx1)); 
  
  return idx2;
  
});

//print("Indices of Non Null Values", idxList);

var mean_B8_list = B8_list.reduce(ee.Reducer.mean());

print("monthly mean of station B8", mean_B8_list);

var img = l8.max();

var imageVisParam1 = {"opacity":1,
                     "bands":["B1","B2","B3"],
                     "min":0.44147244095802307,
                     "max":1.1667611598968506,"gamma":1};

Map.addLayer(img, imageVisParam1, 'image');


var allDates = l8.aggregate_array('system:time_start');

var allDatesSimple = allDates.map(function procDates (ele) {
  
  return ee.Date(ele).format().slice(0,10);
  
});

var paired = allDatesSimple.zip(B8_list);

print ("dates, B8_list", paired);

// RATIO

var RatioALL = idxList.map(function  calculateRatio (ele) {
  
  var idx = ele;

  var value = ee.Number(B8_list.get(idx));
  
  var image = listOfImages.get(idx);
  
  var ratio1 = ee.Image(image).select('B8')
                              .clip(MODISofstation)
                              .divide(value);

  var time = ee.Image(image).get('system:time_start');

  return ratio1.set('system:time_start', time);
  
});

//print("Original Image Collection", myB8);

//print ("Ratio All for Non Null Values", RatioALL);

//statistics

var getStats = function(image) {
  
  var reducers = ee.Reducer.mean().combine({
  reducer2: ee.Reducer.stdDev(),
  sharedInputs: true
  });

  var stats1 = ee.Image(image).reduceRegion({
    reducer: reducers,
    geometry: MODISofstation,
    scale: 15,
    bestEffort: true
  });
  
  return [stats1.get('B8_mean'), stats1.get('B8_stdDev')];

};

var statistics = RatioALL.map(getStats);

//print("statistics", statistics);

var l8_list = l8.toList(l8.size());

var original = ee.List.sequence(0, l8.size().subtract(1));

//print(original);

var nullIdxList = original.map(function verifyingNulls (ele){

  return ee.Algorithms.If(idxList.contains(ele), -1, original.indexOf(ele));
  
}).removeAll([-1]);

//print("nullIdxList", nullIdxList);

var statsWithNulls = original.map(function includeNulls (ele) {
  
  var idx = idxList.indexOf(ele);
  
  return ee.Algorithms.If(idxList.contains(ele), statistics.get(idx), [null, null]);
  
});

print("Stats With Nulls", statsWithNulls);

//Cloud cover

var cloudCover = l8_list.map(function(img){
    //Get the cloud cover
    var id_image = ee.Image(img).id();
    var cloud_cover = ee.Image(img).get('CLOUD_COVER');

    return [id_image, cloud_cover];

});

//print("l8_list", l8_list);

print('Cloud cover', cloudCover);

var values = paired.zip(cloudCover).zip(statsWithNulls);

print(ee.List(values.get(0)).flatten());

var values = values.map(function flatten(ele){
  
  var idx = values.indexOf(ele);
  
  return ee.List(values.get(idx)).flatten();
  
});

print(values);

var myFeatures = ee.FeatureCollection(values.map(function(el){
  el = ee.List(el); // cast every element of the list
  var geom = MODISofstation2;
  return ee.Feature(geom, {
    'values':ee.List(el)
  });
}));

//print(myFeatures);

// Export features, specifying corresponding names.
Export.table.toDrive(myFeatures,
"export_list", //my task
"GEE_Folder", //my export folder
"values",  //file name
"CSV"); 

After running above code in GEE code editor and its associated task, I got in my Google Drive corresponding file with expected values. First, I edited it with a plain text editor before a final edition in a spreadsheet. It looks as follows.

system:index,date,B8_list,id_image,cloud_cover,mean,stddev
0,2020-03-06,0.639874398708344, LC08_008011_20200306,39.71,1.00997863758254,0.017781627522422
1,2020-03-22,0.796732664108276, LC08_008011_20200322,13.16,1.00737864871002,0.013068306049945
2,2020-04-07,0.735870778560638, LC08_008011_20200407,15.63,0.992330483074776,0.042033338343638
3,2020-04-23, null, LC08_008011_20200423,94.53, null, null
4,2020-06-10, null, LC08_008011_20200610,99.92, null, null
5,2020-07-12, null, LC08_008011_20200712,62.09, null, null
6,2020-08-29, null, LC08_008011_20200829,90.03, null, null
7,2020-09-14,0.783640444278717, LC08_008011_20200914,0.03,0.998489330491308,0.016857253357973
8,2020-09-30,0.782324194908142, LC08_008011_20200930,4.01,0.967718460714632,0.053932025456122
9,2020-02-10,0.496771335601807, LC08_009011_20200210,17.3,1.00798689666383,0.009221828314282
10,2020-02-26,0.640491127967835, LC08_009011_20200226,18.02,0.98532426436693,0.023731996703519
11,2020-03-13,0.72007143497467, LC08_009011_20200313,20.18,1.00011408979469,0.016300333388445
12,2020-04-14, null, LC08_009011_20200414,71.22, null, null
13,2020-04-30,0.856668829917908, LC08_009011_20200430,0,1.00296221243306,0.007834591409268
14,2020-05-16,0.855289399623871, LC08_009011_20200516,4.09,1.00159122828168,0.006579231494166
15,2020-06-17,0.852076232433319, LC08_009011_20200617,10.13,1.0500538899289,0.044271723989404
16,2020-07-03,0.79505980014801, LC08_009011_20200703,0.01,1.01705400599466,0.028674789817918
17,2020-07-19,0.737222611904144, LC08_009011_20200719,9.49,1.03501972120189,0.046183171698705
18,2020-08-20,0.749646365642548, LC08_009011_20200820,0.02,1.02083943422949,0.050779927859205
19,2020-09-05, null, LC08_009011_20200905,73.89, null, null
20,2020-09-21,0.746667981147766, LC08_009011_20200921,52.02,1.06454007235275,0.067902722027463
21,2020-10-07,0.927238523960114, LC08_009011_20201007,26.77,0.991586331087041,0.011503595453838
22,2020-10-23,0.661428928375244, LC08_009011_20201023,12.61,0.996714238453283,0.024315489499786
23,2020-02-17,0.674653470516205, LC08_010011_20200217,7.74,0.994263529138675,0.025845383893706
24,2020-03-04,0.68971574306488, LC08_010011_20200304,1.11,1.00472328218035,0.019180234417171
25,2020-03-20,0.741112053394318, LC08_010011_20200320,56.76,1.02239233365454,0.02426697934282
26,2020-04-05,0.826380848884583, LC08_010011_20200405,0.01,1.00363258765545,0.014827015256439
27,2020-04-21,0.86036342382431, LC08_010011_20200421,0.83,1.00779936473466,0.011055551449508
28,2020-05-07,0.866955101490021, LC08_010011_20200507,6.57,1.00132263821736,0.007307573870067
29,2020-05-23,0.856261253356934, LC08_010011_20200523,4.18,1.00366558742435,0.006242927892178
30,2020-06-24,0.853023648262024, LC08_010011_20200624,47.54,1.00081619994516,0.017336464304607
31,2020-08-11,0.840693593025208, LC08_010011_20200811,3.28,1.00221290703835,0.014082893637168
32,2020-08-27, null, LC08_010011_20200827,82.7, null, null
33,2020-09-12, null, LC08_010011_20200912,40.78, null, null
34,2020-09-28,0.715035974979401, LC08_010011_20200928,45.12,0.922666144767519,0.064023702775958
35,2020-10-14,0.718924283981323, LC08_010011_20201014,16.19,0.997433605258763,0.021170365794862
5
  • Wow thank you so much! I really appreciate it! One question: the code is all calculated for MODISofstation and only the last step with myFeatures is MODISofstation2. Is there a reason for that? Because MODISofstation2 was originally just another polygon to test previous codes but I never actually used it.
    – xdsccc
    Apr 2, 2021 at 7:55
  • You're welcome. By your question, you can change for that geometry (MODISofstation). You only need a geometry (or a collection of them as in a feature collection) as reference for exporting this table to Drive. On the other hand, you can widely simplify your code because there are variables (statistics and cloud cover) where you got unnecessarily images or feature collection for storing properties that they can directly obtained as lists.
    – xunilk
    Apr 2, 2021 at 13:29
  • Ok perfect, will do that :) Thanks again for your help. Have a great weekend!
    – xdsccc
    Apr 3, 2021 at 6:08
  • I was wondering what the reason is that the dates are not in order? It no problem since I can easily sort it but I was just wondering about the background reason why that is because I would imagine the function do go through image by image in date order
    – xdsccc
    Apr 16, 2021 at 11:34
  • Because they are exporttrd as dictionaries. This kind of objects don't preserve order as in lists.
    – xunilk
    Apr 16, 2021 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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