0

I used the .combine command to convert two image collections into a two-band image collection (in the last line) to use in a function in the next step. This command is executed but writes 0 elements in the console. Where does this problem come from?

code link : https://code.earthengine.google.com/5ebed42b397e764e3229e3f224c8b643

code :

   var Rad1 = ee.ImageCollection('ECMWF/ERA5_LAND/HOURLY')
            .filter(ee.Filter.date('2018-10-24','2019-05-20'))
            .select('surface_solar_radiation_downwards');

   var Rad2 = ee.ImageCollection('ECMWF/ERA5_LAND/HOURLY')
            .filter(ee.Filter.date('2019-05-20','2019-06-30'))
            .select('surface_solar_radiation_downwards');


   var Rad1_Mj = Rad1.map(function(img){
   var bands = img.multiply(0.000001);
   var clip = bands.clip(geometry);
   return clip
   .copyProperties(img,['system:time_start','system:time_end']); });
   //print(Rad1_Mj);

   var Rad2_Mj = Rad2.map(function(img){
   var bands = img.multiply(0.000001);
   var clip = bands.clip(geometry);
   return clip
   .copyProperties(img,['system:time_start','system:time_end']); });
   //print(Rad2_Mj);

   // time frame change function for median collection
   var daily_product_median = function(collection, start, count, interval, units){
   var sequence = ee.List.sequence(0, ee.Number(count).subtract(1));
   var origin_date = ee.Date(start);

   return ee.ImageCollection(sequence.map(function(i){

    var start_date = origin_date.advance(ee.Number(interval).multiply(i),units);
    var end_date = 
    origin_date.advance(ee.Number(interval).multiply(ee.Number(i).add(1)),units);

    return collection.filterDate(start_date, end_date).median()
     .set('system:time_start',start_date.millis())
     .set('system:time_end',end_date.millis());
    }))};

   // daily radiation product
   var daily_Rad_1 = daily_product_median(Rad1_Mj,'2018-10-24', 208 , 24 , 'hour');
   // print(daily_Rad_1);
   //Map.addLayer(daily_Rad_1, {min: 17.38, max: 26.07, palette : 
   ['white','yellow','orange']}, 
   'Daily solar shortwave radiation 1' ); 

  var daily_Rad_2 = daily_product_median(Rad2_Mj,'2019-05-20', 41 , 24 , 'hour');
  // print(daily_Rad_2);
  // Map.addLayer(daily_Rad_2, {min: 23.77, max: 26.64, palette : 
  ['white','yellow','orange']}, 
  'Daily solar shortwave radiation 2');

  var daily_Rad_total = daily_Rad_1.merge(daily_Rad_2);
  //print(daily_Rad_total);


  var START = '2018-10-24';
  var END = '2019-06-30';

  var DATES = [ '2018-12-19', '2018-12-29', '2019-01-23', '2019-02-12', '2019-03-04',
 '2019-03-19', '2019-04-08', '2019-04-13', '2019-05-13', '2019-05-18', '2019-05-23',
 '2019-05-28', '2019-06-02', '2019-06-07', '2019-06-12', '2019-06-17', '2019-06-22',
 '2019-06-27'];

  var addTime = function(x) {
    return x.set('Date', ee.Date(x.get('system:time_start')).format("YYYY-MM-dd"))};

  var final_Rad = ee.ImageCollection(daily_Rad_total)
          .filter(ee.Filter.date(START, END))
          .map(addTime)
          .filter(ee.Filter.inList('Date',ee.List(DATES)));
  print(final_Rad);



  var ndvi = function(img){
  var bands = img.select(['B2','B3','B4','B8']).multiply(0.0001)
  .clip(geometry);
  var index = bands.normalizedDifference(['B8','B4']);
  return index
 .copyProperties(img,['system:time_start','system:time_end','system:index']);
  };

 var S2 = ee.ImageCollection('COPERNICUS/S2_SR')
 .filterBounds(geometry)
 .filterDate('2018-10-24','2019-06-30')
 .filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE',20))
 .map(ndvi);

 print(S2);

 var NDVI_and_Rad = S2.combine(final_Rad, false);
 print(NDVI_and_Rad);
Daniel T.
  • 32,821
  • 6
  • 50
  • 72

1 Answers1

0

Try it here

You may use merge instead of combine to get a new image collection


var Rad1 = ee.ImageCollection('ECMWF/ERA5_LAND/HOURLY')
                .filter(ee.Filter.date('2018-10-24','2019-05-20'))
                .select('surface_solar_radiation_downwards');

var Rad2 = ee.ImageCollection('ECMWF/ERA5_LAND/HOURLY')
                .filter(ee.Filter.date('2019-05-20','2019-06-30'))
                .select('surface_solar_radiation_downwards');


var Rad1_Mj = Rad1.map(function(img){
var bands = img.multiply(0.000001);
var clip = bands.clip(geometry);
return clip
.copyProperties(img,['system:time_start','system:time_end']); });

//print(Rad1_Mj);

var Rad2_Mj = Rad2.map(function(img){
var bands = img.multiply(0.000001);
var clip = bands.clip(geometry);
return clip
.copyProperties(img,['system:time_start','system:time_end']); });
//print(Rad2_Mj);

// time frame change function for median collection
var daily_product_median = function(collection, start, count, interval, units){
  var sequence = ee.List.sequence(0, ee.Number(count).subtract(1));
  var origin_date = ee.Date(start);
  
  return ee.ImageCollection(sequence.map(function(i){
    
    var start_date = origin_date.advance(ee.Number(interval).multiply(i),units);
    var end_date = origin_date.advance(ee.Number(interval).multiply(ee.Number(i).add(1)),units);
    
    return collection.filterDate(start_date, end_date).median()
    .set('system:time_start',start_date.millis())
    .set('system:time_end',end_date.millis());
  }))};
  
  // daily radiation product
  var daily_Rad_1 = daily_product_median(Rad1_Mj,'2018-10-24', 208 , 24 , 'hour');
 // print(daily_Rad_1);
 //Map.addLayer(daily_Rad_1, {min: 17.38, max: 26.07, palette : ['white','yellow','orange']}, 'Daily solar shortwave radiation 1' ); 
  
  var daily_Rad_2 = daily_product_median(Rad2_Mj,'2019-05-20', 41 , 24 , 'hour');
 // print(daily_Rad_2);
 // Map.addLayer(daily_Rad_2, {min: 23.77, max: 26.64, palette : ['white','yellow','orange']}, 'Daily solar shortwave radiation 2');

var daily_Rad_total = daily_Rad_1.merge(daily_Rad_2);
//print(daily_Rad_total);


var START = '2018-10-24';
var END = '2019-06-30';

var DATES = [ '2018-12-19', '2018-12-29', '2019-01-23', '2019-02-12', '2019-03-04',
'2019-03-19', '2019-04-08', '2019-04-13', '2019-05-13', '2019-05-18', '2019-05-23',
'2019-05-28', '2019-06-02', '2019-06-07', '2019-06-12', '2019-06-17', '2019-06-22',
'2019-06-27'];

var addTime = function(x) {
  return x.set('Date', ee.Date(x.get('system:time_start')).format("YYYY-MM-dd"))};
  
var final_Rad = ee.ImageCollection(daily_Rad_total)
            .filter(ee.Filter.date(START, END))
            .map(addTime)
            .filter(ee.Filter.inList('Date',ee.List(DATES)));
            
print("final_Rad",final_Rad);



var ndvi = function(img){
  var bands = img.select(['B2','B3','B4','B8']).multiply(0.0001)
  .clip(geometry);
  var index = bands.normalizedDifference(['B8','B4']);
  return index
  .copyProperties(img,['system:time_start','system:time_end','system:index']);
};

var S2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(geometry)
.filterDate('2018-10-24','2019-06-30')
.filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE',20))
.map(ndvi);

print("S2", S2);

var NDVI_and_Rad = S2.merge(final_Rad);
print('Both image collection', NDVI_and_Rad);


  • I want to multiply these two image collections, so I use the .combine code to make an image collection with two-band and then multiply two bands of NDVI and Radiation in a function code. but I don't understand why this code couldn't combine these two image collections – Amir Mohamad Abasi Jan 16 '22 at 19:39
  • So merge didn’t worked for you ? – Abhilash Singh Chauhan Jan 18 '22 at 19:37
  • No, merge just make a collection and for the next step, I can't multiply these two parameters. I need the right combine code or another code that I can multiply NDVI and Radiation – Amir Mohamad Abasi Jan 19 '22 at 21:48