chrisvwn / Rnightlights

R package to extract data from satellite nightlights.
GNU General Public License v3.0
47 stars 14 forks source link

Specifying a threshold value while calling the getCtryNlData() function. #4

Closed bidur closed 6 years ago

bidur commented 6 years ago

A way to specify the threshold value while calling the getCtryNlData() function seems helpful.

The VIIRS DNB monthly data that we get from ngdc.noaa.gov still contain errors ( e.g. lights from fire, boats or aurora). Applying thresholding can filter such errors. Users will be able to ignore some radiance values whenever necessary e.g. for example sometimes very high radiance values may occur in the Nightlight Image due to error.

Thank You.

chrisvwn commented 6 years ago

Hi Bidur,

Thanks for posting this. It makes sense to have the option of specifying a threshold. I need to find out what the best practice is for dealing with high radiance values. If you do find any literature on this please post it here.

First, I believe there are 2 types of errors with the nightlight rasters:

  1. Instrument errors which are most visible as negative values and NaN values : We currently handle instrument errors by converting all negative values to NA and excluding them from all calculations
  2. Radiances from known light sources as you have mentioned such as gas flares, aurora, wildfires, etc which there is a procedure of removal as shown here. We currently do not handle these errors but should and this will be included in an upcoming release.

Concerning adding a threshold parameter to getCtryNlData. As mentioned in our online discussion earlier getCtryNlData() has the parameter nlStats which takes a list of functions to perform on the radiances. It is possible to write your own function that will be calculated on the raster. Any function that accepts a vector as input and returns a scalar will work. In this case, thresholded sums of radiances could be obtained by writing a wrapper around the sum function for example:

mySum <- function(x)
{
  #OLS valid range = 0 .. 254
  #VIIRS valid range = 0 .. 65535
  thresh <- 10000

  threshX <- x[x < thresh]

  #data may contain NAs, handle accordingly
  sumThreshX <- sum(threshX, na.rm=TRUE)

  return(sumThreshX)
}

Then you can call getCtryNlData() with this function like so:

ctryNlData <- getCtryNlData(other parameters, nlStats=mySum)

The output data.frame ctryNlData will have a column with the uppercase name of your function appended e.g. VIIRS_SLK_201401_MYSUM

Still, if required we will add this option to the function.