1

I am a beginner with GEE and I am trying to calculate the incoming net radiation for that I need the square inverse of relative earth sun distance using the expression dr = 1 + 0.033*cos(J*2*pi/365) where J is the day of year.

Can someone help me with this code?

var sequence = ee.List.sequence(1, 365);
var dr2 =  sequence.divide(365).multiply(Math.PI).cos().add(1);
and alternatively I used 
var addDate = function(image){
  var doy = image.date().getRelative('day', 'year');
  var doyBand = ee.Image.constant(doy).uint16().rename('doy');
  doyBand = doyBand.updateMask(image.select('B2').mask());

  return image.addBands(doyBand);
};
var image = ee.ImageCollection('LANDSAT/LT05/C01/T2')
  .filterDate('1987-01-01', '1990-05-01')
  .filterBounds(geometry);
var withDate = image.map(addDate);
print(withDate);
Map.addLayer(withdate);
var dr = withDate.divide(365).multiply(Math.PI).cos().add(1);
2

You prabably want to calculate the dr on a per image basis, similar as you did for calculating the doy. That's why you will have to map over the image collection. You could add the dr function inside the function were you calculate the doy.

As your function in the question differs from the function you typed in the code below it, I assumed you want to use this: dr = 1 + 0.033*cos(J*2*pi/365):

// and alternatively I used 
var addDate = function(image){

  // calculate the doy band
  var doy = image.date().getRelative('day', 'year');
  var doyBand = ee.Image.constant(doy).uint16().rename('doy');
  doyBand = doyBand.updateMask(image.select('B2').mask());

  // calculate the dr using: dr = 1 + 0.033*cos(J*2*pi/365)
  var temp = doyBand.multiply(2).multiply(Math.PI).divide(365);
  var temp2 = temp.cos();
  var dr = temp2.multiply(0.033).add(1).rename('dr');
  // add the bands to the image
  return image.addBands([doyBand, dr]);
};

I wrote it down in multiple lines so you are less likely to mix up the order of the functions. Link code.

For the list you calculated (possibly as check), you will need to cast the list to an ee.Array() to be able to do element-wise operations:

var sequence = ee.Array(ee.List.sequence(1, 365));
var dr2 =  sequence.multiply(2).divide(365).multiply(Math.PI).cos().multiply(0.033).add(1);
print(dr2)

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.