Google Earth Engine – Convert YYDOY Pixel Value to Milliseconds

google-earth-enginegoogle-earth-engine-javascript-api

I'm working with RADD alerts stored in the YYDOY (Year-Year-Day-Of-Year) format, and I'm having trouble converting this information to milliseconds.

I've tried the following:

var loadLatestRaddAlert = function() {
    var alerts = ee.ImageCollection('projects/radar-wur/raddalert/v1');
    var geography = 'sa';
    return ee.Image(
        alerts.filter(
            ee.Filter.and(
                ee.Filter.eq('layer', 'alerts'),
                ee.Filter.eq('geography', geography)
            )
        ).sort('system:time_end', false).first()
    ).select('Date');
};

var convertRaddDateToMillis = function(dateBand) {
    var year = dateBand.divide(1000).floor().add(2000);
    var doy = dateBand.mod(1000);
    var daysSinceEpoch = year.subtract(1970).multiply(365).add(doy);
    var millis = daysSinceEpoch.multiply(86400).multiply(1000);
    return millis;
};

The pixel with value 22121 (2022-05-01) should return me 1651363200000, but it returns 1650326400000.

I think it's because of leap years. Any suggestion?

Best Answer

Okay, so here is a way that should work. Probably not the fastest or cleanest, but the advantage is that you don't have to implement messy date logic with leap years or leap seconds yourself.

I'm basically building a lookup table for a desired time period and remapping the values in the image.

var test = ee.ImageCollection('projects/radar-wur/raddalert/v1').first().select("Date")

var start = ee.Date("2019-01-01")
var end = ee.Date("2023-01-01")
var days = end.difference(start, "days")

var yydoy = ee.List.sequence(0, days).map(function(advance){
  return ee.Number.parse(start.advance(advance, "days").format("yyDDD"))
})
var millis = ee.List.sequence(0, days).map(function(advance){
  return start.advance(advance, "days").millis()
})

var in_millis = test.remap(yydoy, millis, 0)
Map.addLayer(in_millis)
Related Question