1

I have run the following code , based on :Extracting the time series of all pixel values in the study area for one band in an image collection in Google Earth Engine in a CSV file, to extract pixel values of precipitation from "NOAA/PERSIANN-CDR".

var scale = 27830

var bands = ['precipitation']
var collection = ee.ImageCollection('NOAA/PERSIANN-CDR')
  .filterDate('2010-01-01', '2015-01-01')
  .filterBounds(geometry)
  .map(extractPixels)
  .flatten()
  
//print('Collection: ', collection);

var bands = collection.select('precipitation');
print("reza")
Export.table.toDrive({
  collection: collection, 
  selectors: ['date', 'latitude', 'longitude','precipitation']
})

function extractPixels(image) {
  var mask = ee.Image(1) // Create some mask - remove clouds etc.
  var pixels = image
    .select(bands)
    .addBands(ee.Image.pixelLonLat())
    .updateMask(image.mask().reduce(ee.Reducer.min())) // Mask everything if something is masked
    .updateMask(mask)
    .reduceRegion({
      reducer: ee.Reducer.toList(), 
      geometry: geometry, 
      scale: scale,
      maxPixels: 1e30
    })
  
  return ee.FeatureCollection(
    ee.Array(pixels.values())
      .transpose()
      .toList()
      .map(function (values) {
        var properties = ee.Dictionary.fromLists(pixels.keys(), values)
        return ee.Feature(null, properties)
          .set('date', image.date().format('yyyy-MM-dd'))
      })
  )
}

print('reza finished')

When the it gets to the missing values announce the following error:

"Error: Error in map(ID=20100928): Array: No numbers in 'values', must provide a type. (Error code: 3)" .

How can I add some conditions on code to skip missing values and insert "non" in the CSV file?

1 Answer 1

1

You can split this into two mappings. Extract the list of pixel values for each image, returning a feature for each image, remove features without any values, then convert the lists of pixel values into separate features.

var geometry = Map.getBounds(true)
var scale = 27830

var bands = ['precipitation']
var collection = ee.ImageCollection('NOAA/PERSIANN-CDR')
  .filterDate('2010-01-01', '2015-01-01')
  .filterBounds(geometry)
  .map(extractPixels)
  .filter(ee.Filter.gt('count', 0))
  .map(pixelsToFeatures)
  .flatten()
  
Export.table.toDrive({
  collection: collection, 
  selectors: ['date', 'latitude', 'longitude', 'precipitation']
})

function extractPixels(image) {
  var mask = ee.Image(1) // Create some mask - remove clouds etc.
  var pixels = image
    .select(bands)
    .addBands(ee.Image.pixelLonLat())
    .updateMask(image.mask().reduce(ee.Reducer.min())) // Mask everything if something is masked
    .updateMask(mask)
    .reduceRegion({
      reducer: ee.Reducer.toList(), 
      geometry: geometry, 
      scale: scale,
      maxPixels: 1e30
    })
  return ee.Feature(null, pixels)
    .set('date', image.date().format('yyyy-MM-dd'))
    .set('count', ee.List(pixels.get('latitude')).size()) // Allow filtering away empty features
}

function pixelsToFeatures(feature) {
  var pixels = ee.Feature(feature).toDictionary(bands.concat(['longitude', 'latitude']))
  return ee.FeatureCollection(
    ee.Array(pixels.values())
      .transpose()
      .toList()
      .map(function (values) {
        var properties = ee.Dictionary.fromLists(pixels.keys(), values)
          .set('date', feature.get('date'))
        return ee.Feature(null, properties)
      })
  )  
}

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

5
  • Thanks for your new revision but when I run it, the output does not have lat and long info (the cells are empty) in CSV. I also compared it with the last code: gis.stackexchange.com/questions/439389/… . The last code extracts lat, long and precipitation values but the new one fails to extract lat and long. Also the revised code precipitation output values are not realistic (for example daily precipitation is 82 mm. Commented Sep 6, 2022 at 17:11
  • Sorry, I missed the lat/lng. I've updated the answer. Unrealistic precipitation - there certainly are pixels with 82 mm and more in this collection: code.earthengine.google.com/601d4f8f4071394a3cb1006aac595116 Commented Sep 7, 2022 at 10:23
  • Sorry bothering you. would you please upload the last updated code (for lat and long retrieval) here. I could not find updated parts. Commented Sep 8, 2022 at 6:17
  • It's been updated - I somehow forgot to save the edit before. Commented Sep 8, 2022 at 7:18
  • Thanks a lot. It worked. Commented Sep 11, 2022 at 2:51

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.