0

I am trying to save 6 years daily MODIS data in a single image with multi bands.I specifically want the dates as band names.I have been tring to run this script but the page crashes or becomes unresponsive. I want the export a single multi-band image with names such as this: 2021_01_01_LST_Day_1km..

This is my script.

// use a geometry to select the study area
var geom = ee.FeatureCollection('users/payeldastidar/STATE_BUFF_WGS')
var output_name = 'LST_2002_2006'
var crs = 'EPSG:4326'


//print(geom)

// filter image collection to time period of interest 
var startDate = '2002-01-01'
var stopDate  = '2006-01-01'

// resulting image collection - same set of images as in the python GEE atmospheric correction code
var LST_MOD = ee.ImageCollection('MODIS/061/MOD11A1')
    .filterBounds(geom)
    .filterDate(startDate,stopDate)
    
    //.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 1))
    .sort('system:time_start');


var LST_Assam = LST_MOD.select('LST_Day_1km')



// convert image collection to multi-band image
var LST_Assam_band = ee.ImageCollection(LST_Assam).iterate(function(img, all){
  return ee.Image(all).addBands(img)}, ee.Image().select())
LST_Assam_band = ee.Image(LST_Assam_band).multiply(0.02).float()
print(LST_Assam_band,'LST_Assam_band')

// get band names for each collection
var LST_Assam_band_names = ee.Image(LST_Assam_band).bandNames() // s1 or s2_vi1 or pl_vi1
print(LST_Assam_band_names, 'LST_Assam_band_names')

// get a list of image dates
var date_list = ee.ImageCollection(LST_Assam).toList(LST_Assam_band_names.length()) 
var date_list2 = date_list.map(function(img) {
  return ee.Image(img).get('system:index')
  //return ee.Image(img).date()
})
print(date_list, 'date_list')
print(date_list2, 'date_list2')

var bnames = ee.List([])
for (var i = 0; i < date_list2.length().getInfo(); i++) {
  for (var j = 0; j < LST_Assam.length().getInfo(); j++) {
    var bname = ee.String(LST_Assam.get(j)).cat(ee.String(date_list2.get(i)))
    var bnames = ee.List(bnames).add(bname)
  } 
}

var LST_Assam_export = ee.Image(LST_Assam_band).rename(date_list2)
print(LST_Assam_export, 'LST_Assam_export')
 // Export image to drive
Export.image.toDrive({
  image: LST_Assam_t, 
  description: output_name, 
  scale: 1000,
  maxPixels: 1e13,
  crs: crs, 
  region: geom
});
3
  • Please share the error message or at least indicate where it fails "in the middle" Commented Jun 21, 2022 at 10:53
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Jun 21, 2022 at 10:54
  • The page becomes unresponsive and crashes. I want something like this: "2002_01_01_LST_Day_1km" ......."2022_01_01_LST_Day_1km"as the name of the bands after exporting it. Commented Jun 21, 2022 at 11:01

1 Answer 1

0

In general if you have to make a for loop in Earth Engine it is worth to consider an alternative. There is a build-in function available for earth engine: .toBands()

Depending on what it is you exactly want this is probably your best option. see for example: https://developers.google.com/earth-engine/apidocs/ee-imagecollection-tobands

https://code.earthengine.google.com/b8ab7b0a98b87c6e53ebb944d9b0fe35

Also, for future questions it is helpfull if you share a working example and give some detail on your aim.

7
  • I tried this and downloaded the data. However I wanted the dates as the name of each band. When I opened the data, it showed the names as this: [961] "LST_2002_2022.961" "LST_2002_2022.962" "LST_2002_2022.963" "LST_2002_2022.964" "LST_2002_2022.965" "LST_2002_2022.966" [967] "LST_2002_2022.967" "LST_2002_2022.968" "LST_2002_2022.969" "LST_2002_2022.970" "LST_2002_2022.971" "LST_2002_2022.972" Commented Jun 21, 2022 at 10:21
  • When I export the 'imageCollection' object I create in the script, the output of the example script is an 10-band image with the following names: 2002_01_01_LST_Day_1km, 2002_01_02_LST_Day_1km, 2002_01_03_LST_Day_1km, 2002_01_04_LST_Day_1km, 2002_01_05_LST_Day_1km etc.. Which is the expected outcome if I understand you correctly. So you'll have to specify what you are doing more clearly for me to be able to help you any further.
    – Jobbo90
    Commented Jun 21, 2022 at 11:59
  • I did exactly what you suggested. Here is the link to the code: code.earthengine.google.com/b74f5e43ae8e11ba07a6d960b317f379 Commented Jun 21, 2022 at 12:36
  • your assets are not shared so I can't reproduce it sorry
    – Jobbo90
    Commented Jun 21, 2022 at 14:09
  • I changed the setting to "anyone can read". I think you can have access to it now.Sorry i am new to this Commented Jun 21, 2022 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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