Search code examples
chartstime-seriesgoogle-earth-engine

Change date format in download CSV from Time series Chart


I use this script to generate a chart of NDVi vs time. The problem is when I download the series at CSV the date format is hard to work with and is not the same in all the observation.

The original CSV is like this:

system:time_start,NDVI
Mar 26, 2013,0.132

What I need is change the date format to 2013-03-26 (yyyy-mm-dd)

The script

var roi = /* color: #98ff00 */ee.Geometry.Point([-72.18245324628742, -13.260203147923505]);
var l8toa = ee.ImageCollection("LANDSAT/LC08/C01/T1_RT");
// This field contains UNIX time in milliseconds.
var timeField = 'system:time_start';
// Use this function to mask clouds in Landsat 8 imagery. (See https://landsat.usgs.gov/collectionqualityband)
var maskClouds = function(image) {var quality = image.select('BQA');
var cloud01 = quality.eq(61440);
var cloud02 = quality.eq(53248);
var cloud03 = quality.eq(28672);
var mask = cloud01.or(cloud02).or(cloud03).not();
return image.updateMask(mask);};
// Use this function to add variables for NDVI, time and a constant
// to Landsat 8 imagery.
var addVariables = function(image) {
  // Compute time in fractional years since the epoch.
  var date = ee.Date(image.get(timeField));
  var years = date.difference(ee.Date('1970-01-01'), 'year');
  // Return the image with the added bands.
  return image
  // Add an NDVI band.
  .addBands(image.normalizedDifference(['B5', 'B4']).rename('NDVI')).float()
  // Add a time band.
  .addBands(ee.Image(years).rename('t').float())
  // Add a constant band.
  .addBands(ee.Image.constant(1));};
  // Remove clouds, add variables and filter to the area of interest.
  var filteredLandsat = l8toa  .filterBounds(roi)  .map(maskClouds)  .map(addVariables);
  // Plot a time series of NDVI at a single location.
  var l8Chart = ui.Chart.image.series(filteredLandsat.select('NDVI'), roi)
  .setChartType('ScatterChart')
  .setOptions({title: 'Landsat 8 NDVI time series at ROI',trendlines: {0: {color: 'CC0000'
  }},lineWidth: 1,pointSize: 3,
  });
print(l8Chart);

Solution

  • Use DATE_ACQUIRED as the xProperty argument:

      var l8Chart = ui.Chart.image.series({ imageCollection:filteredLandsat.select('NDVI'), 
                           region:roi, xProperty:'DATE_ACQUIRED'})
      .setChartType('ScatterChart')
      .setOptions({title: 'Landsat 8 NDVI time series at ROI',trendlines: {0: {color: 'CC0000'
      }},lineWidth: 1,pointSize: 3,
      });