[GIS] Earth Engine: Evaluating ee.Number in file path

file pathgoogle-earth-engine

In the Earth Engine code editor, I'm trying to load an image using a variable in the file path. In my full code I'm passing a variable (call it "id") into a function, and am trying to use "id" in a file path.

The below code gives me a similar error as to what I get in my full code. In short, when I define "id" as an "ee.Number()", the code fails.

Any ideas on how to evaluate "id" in a file path after I set "id" as an "ee.Number()"?

// THIS WORKS
var id = 20120401;
var viirs_rad = 
ee.Image('NOAA/VIIRS/DNB/MONTHLY_V1/VCMCFG/'.concat(id)).select('avg_rad');
print(viirs_rad);

// THIS DOES NOT WORK
var id = 20120401;
id = ee.Number(id);
var viirs_rad = 
ee.Image('NOAA/VIIRS/DNB/MONTHLY_V1/VCMCFG/'.concat(id)).select('avg_rad');
print(viirs_rad);

Here's the error I'm getting:

Image (Error)
 Image.load: Image asset 'NOAA/VIIRS/DNB/MONTHLY_V1/VCMCFG/ee.Number(20120401)' not found.

Here's my full code. I'm trying to collect (1) average VIIRS radiance values (masked by cloud cover) and (2) sum of cloud-free observations for each feature in a featurecollection (in this case, for each county in Maine) for each VIIRS image (ie, every month). The result is a new featurecollection with variables noting avg & sum values for each month.

I'm putting **** near the portion of the code that I'm having problems with.

//=====================================================================
//Load and Prep Data
var maineCounties = 
    ee.FeatureCollection('ft:1S4EB6319wWW2sWQDPhDvmSBIVrD3iEmCLYB7nMM'). 
    filter(ee.Filter.eq('StateName', 'Maine'));

//=====================================================================
//Functions

var summarize_viirs = function(feature){

  // Define list of VIIRS data
  var viirs_years_list = [20120401,20120501,20120601];
  viirs_years_list = ee.List(viirs_years_list);                        

  // Collects VIIRS data within a feature for an image                   
  var summarize_viirs_year = function(id, feat){
    id = ee.Number(id);
    feat = ee.Feature(feat);

    // Import VIIRS radiance and cloud cover data

    // *******************************************************
    // CODE DOESN'T FAIL, BUT ONLY COLLECTS DATA ON ONE IMAGE 
    // *******************************************************
    //var viirs_rad = 
  ee.Image('NOAA/VIIRS/DNB/MONTHLY_V1/VCMCFG/20120401').select('avg_rad');
    //var viirs_cld = 
  ee.Image('NOAA/VIIRS/DNB/MONTHLY_V1/VCMCFG/20120401').select('cf_cvg');

    // *******************************************************
    // DOESN'T WORK 
    // *******************************************************
    var viirs_rad = 
 ee.Image('NOAA/VIIRS/DNB/MONTHLY_V1/VCMCFG/'.concat(id)).select('avg_rad');
    var viirs_cld = 
 ee.Image('NOAA/VIIRS/DNB/MONTHLY_V1/VCMCFG/'.concat(id)).select('avg_rad');

    // Mask radiance values withcloud cover
    viirs_rad = viirs_rad.mask(viirs_cld);

    // Average VIIRS Radiance
    var feature_rad = viirs_rad.reduceRegion({
      geometry: feature.geometry(),
      reducer: ee.Reducer.mean(),
      scale: 150
      });

    // Sum VIIRS Cloud-Free Observations
    var feature_cld = viirs_cld.reduceRegion({
      geometry: feature.geometry(),
      reducer: ee.Reducer.sum(),
      scale: 150
      });  

    // Collect Values    
    var mean_rad = feature_rad.get('avg_rad');
    var sum_cld = feature_cld.get('cf_cvg');

    // Add "mean" and "sum" variables to feature  
    var year_month = ee.Number(id);
    mean_rad = ee.Number(mean_rad);
    sum_cld = ee.Number(sum_cld);
    var mean_rad_name = ee.String("viirs_rad_mean_").cat(year_month);
    var sum_cld_name = ee.String("viirs_cld_sum_").cat(year_month);
    return feat.set(mean_rad_name, mean_rad, sum_cld_name, sum_cld);

  };  

  var new_feature = 
      ee.Feature(viirs_years_list.iterate(summarize_viirs_year, feature));

  return new_feature;

};

//=====================================================================
//Apply Function
var maineCounties_output = maineCounties.map(summarize_viirs);
print(maineCounties_output);

NOTE: I'm very new to Earth Engine & Javascript. A lot of my code has been adapted from: Iterating over years for features in feature collection using Google Earth Engine?

Best Answer

In the first (working) example, the entire analysis is done client-side.

In the second (non-working) example, you are mixing a client-side Javascript function .concat() with a server-side Earth Engine ee.Number() object. That is not allowed. See https://developers.google.com/earth-engine/client_server for more details.

From the code supplied, it is not clear what your larger objective is. Could you provide more details on what you are trying to do, and potentially include the function that you mentioned as well?