Google Earth Engine: Extract second largest value from annual observations? - google-earth-engine

In Google Earth Engine, is it possible to extract the annual second largest and second smallest value and construct an imagecollection?
Apparently, there is no build-in reducer for this purpose.
Here is my code for getting the min, please guide me on how to get the second max and min.
Thank you!
Here is the code:
var startDate = ee.Date('2001-01-01'); // set start time for analysis
var endDate = ee.Date('2021-12-31'); // set end time for analysis
// calculate the number of year to process
var nyears = ee.Number(endDate.difference(startDate,'year'));
//init a time band
var createTimeBand= function(image) {
return image.addBands(image.metadata('system:time_start')
.divide(1e18))
// .divide(1000*60*60*24*365))
}
var sst = ee.ImageCollection('MODIS/006/MOD11A1').select('LST_Day_1km')
.filterDate(startDate, endDate)
.map(createTimeBand)
var byyearMin = ee.ImageCollection(
// map over each month
ee.List.sequence(0,nyears).map(function (n) {
// calculate the offset from startDate
var ini = startDate.advance(n,'year');
// advance just one month
var end = ini.advance(1,'year');
// filter and reduce
return sst.filterDate(ini,end)
.select(0).min()
// .sort('LST_Day_1km').reverse().first()
.multiply(0.02)
.subtract(273.15)
.set('system:time_start', ini.millis());//convert time to number
}));

var startDate = ee.Date('2001-01-01'); // set start time for analysis
var endDate = ee.Date('2021-12-31'); // set end time for analysis
// calculate the number of year to process
var nyears = ee.Number(endDate.difference(startDate,'year'));
//init a time band
var createTimeBand= function(image) {
return image.addBands(image.metadata('system:time_start')
.divide(1e18))
// .divide(1000*60*60*24*365))
}
var sst = ee.ImageCollection('MODIS/006/MOD11A1').select('LST_Day_1km')
.filterDate(startDate, endDate)
//.map(createTimeBand)
var byyearMin = ee.ImageCollection(
// map over each month
ee.List.sequence(0,nyears).map(function (n) {
// calculate the offset from startDate
var ini = startDate.advance(n,'year');
// advance just one month
var end = ini.advance(1,'year');
var sortedDays = sst.filterDate(ini,end)
.sort('LST_Day_1km')
.toList(sst.size())
var secondLargest = ee.List(sortedDays.get(1))
var secondSmallest = ee.List(sortedDays.get(-1))
var collection = ee.Image(secondLargest)
.addBands(secondSmallest).rename(['secondLargest', 'secondSmallest'])
.multiply(0.02)
.subtract(273.15)
return collection
.set('system:time_start', ini.millis()) //convert time to number
.set('Date', ee.Date(ini))
}));
print(byyearMin)

Related

How do I solve the 0 element problem in Google Earth Engine?

I used the .combine command to convert two image collections into a two-band image collection (in the last line) to use in a function in the next step. This command is executed but writes 0 elements in the console. Where does this problem come from?
code link: https://code.earthengine.google.com/ed0992093ff830d926c7dd14403477c6
Code:
var ndvi = function(img){
var bands = img.select(['B2','B3','B4','B8']).multiply(0.0001)
.clip(geometry);
var index = bands.normalizedDifference(['B8','B4']).rename('NDVI_S2');
return index
.copyProperties(img,['system:time_start','system:time_end','system:index']);
};
var S2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(geometry)
.filterDate('2018-10-24','2019-06-30')
//.filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE',20))
.map(ndvi);
print(S2);
var START = '2018-10-24';
var END = '2019-06-30';
var DATES = [ '2018-12-19', '2018-12-29', '2019-01-23', '2019-02-12', '2019-03-04',
'2019-03-19', '2019-04-08', '2019-04-13', '2019-05-13', '2019-05-18', '2019-05-23',
'2019-05-28', '2019-06-02', '2019-06-07', '2019-06-12', '2019-06-17', '2019-06-22',
'2019-06-27'];
var addTime = function(x) {
return x.set('Date', ee.Date(x.get('system:time_start')).format("YYYY-MM-dd"))};
var Sentinel = ee.ImageCollection(S2)
.filter(ee.Filter.date(START, END))
.map(addTime)
.filter(ee.Filter.inList('Date',ee.List(DATES)));
print(Sentinel);
var PMODIS =
ee.Image('MODIS/006/MCD43A4/2018_12_19').select('Nadir_Reflectance_Band4');
var MODProjection = PMODIS.projection();
print('MODIS projection:', MODProjection);
var Viz = {min: 0, max: 1, palette: ['be6c44','ca3a3a','e4ae0c','565c04','819536']};
var S2_resampled = Sentinel.map(function(img){
var S2Mean = img
// Force the next reprojection to aggregate instead of resampling.
.reduceResolution({
reducer: ee.Reducer.mean(),
maxPixels: 2146
})
// Request the data at the scale and projection of the Sentinel image.
.reproject({
crs: MODProjection
});
return S2Mean
.copyProperties(img,['system:time_start','system:time_end','system:index']);
});
Map.addLayer(S2_resampled)
var M_ndvi = function(img){
var bands =
img.select(['Nadir_Reflectance_Band1','Nadir_Reflectance_Band5']).multiply(0.0001)
.clip(geometry);
var index=bands
.normalizedDifference(['Nadir_Reflectance_Band1','Nadir_Reflectance_Band5'])
.rename(
'NDVI_MOD');
return index
.copyProperties(img,['system:time_start','system:time_end','system:index']);
};
var MOD = ee.ImageCollection('MODIS/006/MCD43A4')
.filterBounds(geometry)
.filterDate('2018-10-24','2019-06-30')
.map(M_ndvi);
var MODIS = ee.ImageCollection(MOD)
.filter(ee.Filter.date(START, END))
.map(addTime)
.filter(ee.Filter.inList('Date',ee.List(DATES)));
print(MODIS);
var S2_and_MOD = S2_resampled.combine(MODIS, false);
print(S2_and_MOD);
var Diff = S2_and_MOD.map(function(img){
var clip = img.clip(geometry);
var Diffe = clip.expression('NDVI_S2 - NDVI_MOD',
{'NDVI_S2':clip.select('NDVI_S2') ,
'NDVI_MOD':clip.select('NDVI_MOD')}).rename('Diff');
return Diffe
.copyProperties(img,['system:time_start','system:time_end']); });
print(Diff);
ee.Image.combine() uses the system:ID property to join the 2 images. See the documentation here. Since your images do not match, the resulting collection has no images.
A solution that should fit your needs utilizes the ee.Join.inner() to take advantage of the Date property that you have created to join the 2 image collections. A similar question was answered here.
Using inner join, I was able to accomplish what appeared to be your goal of finding the difference in NDVI between the S2 and MODIS collections. The full working script can be found here: https://code.earthengine.google.com/dc45df1b7cf83723d53e9f7917975e2d
Code:
// Function - Calculate S2 NDVI
var ndvi = function(img){
var bands = img.select(['B2','B3','B4','B8']).multiply(0.0001)
.clip(geometry);
var index = bands.normalizedDifference(['B8','B4']).rename('NDVI_S2');
return index
.copyProperties(img,['system:time_start','system:time_end','system:index']);
};
// Get S2 NDVI images
var S2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(geometry)
.filterDate('2018-10-24','2019-06-30')
//.filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE',20))
.map(ndvi);
print('S2 NDVI ImageCollection',S2);
// Set Date Parameters
var START = '2018-10-24';
var END = '2019-06-30';
// Create Date List
var DATES = [ '2018-12-19', '2018-12-29', '2019-01-23', '2019-02-12', '2019-03-04',
'2019-03-19', '2019-04-08', '2019-04-13', '2019-05-13', '2019-05-18', '2019-05-23',
'2019-05-28', '2019-06-02', '2019-06-07', '2019-06-12', '2019-06-17', '2019-06-22',
'2019-06-27'];
// Function - Add 'Date' field to image
var addTime = function(x) {
return x.set('Date', ee.Date(x.get('system:time_start')).format("YYYY-MM-dd"))};
// Run addTime on S2 ImageCollection
var Sentinel = ee.ImageCollection(S2)
.filter(ee.Filter.date(START, END))
.map(addTime)
.filter(ee.Filter.inList('Date',ee.List(DATES)));
print('Date Added S2', Sentinel);
// Get MODIS Projection
var PMODIS = ee.Image('MODIS/006/MCD43A4/2018_12_19').select('Nadir_Reflectance_Band4');
var MODProjection = PMODIS.projection();
print('MODIS projection:', MODProjection);
// Set Visualization Parameters
var Viz = {min: 0, max: 1, palette: ['be6c44','ca3a3a','e4ae0c','565c04','819536']};
// Reproject S2 images to MODIS projection
var S2_resampled = Sentinel.map(function(img){
var S2Mean = img
// Force the next reprojection to aggregate instead of resampling.
.reduceResolution({
reducer: ee.Reducer.mean(),
maxPixels: 2146
})
// Request the data at the scale and projection of the Sentinel image.
.reproject({
crs: MODProjection
});
return S2Mean
.copyProperties(img,['system:time_start','system:time_end','system:index']);
});
print('S2_resampled',S2_resampled);
Map.addLayer(S2_resampled);
// Function - Calculate MODIS NDVI
var M_ndvi = function(img){
var bands = img.select(['Nadir_Reflectance_Band1','Nadir_Reflectance_Band5']).multiply(0.0001)
.clip(geometry);
var index = bands.normalizedDifference(['Nadir_Reflectance_Band1','Nadir_Reflectance_Band5']).rename('NDVI_MOD');
return index
.copyProperties(img,['system:time_start','system:time_end','system:index']);
};
// Get MODIS NDVI Images
var MOD = ee.ImageCollection('MODIS/006/MCD43A4')
.filterBounds(geometry)
.filterDate('2018-10-24','2019-06-30')
.map(M_ndvi);
// Run addTime on MODIS ImageCollection
var MODIS = ee.ImageCollection(MOD)
.filter(ee.Filter.date(START, END))
.map(addTime)
.filter(ee.Filter.inList('Date',ee.List(DATES)));
print('MODIS',MODIS);
// Combine MODIS and S2 Image Collections using Date
// Specify the join type
var join_type = ee.Join.inner();
// Set the join parameter
var filter = ee.Filter.equals({
leftField: 'Date',
rightField: 'Date'
});
// Execute the join
var inner_join = ee.ImageCollection(join_type.apply(MODIS,S2_resampled,filter));
// Flatten joined images into a single image with 2 bands
var S2_and_MOD = inner_join.map(function(feature) {
return ee.Image.cat(feature.get('primary'), feature.get('secondary'));
});
print('Combined S2 and MODIS Collection:',S2_and_MOD);
// Calculate the difference between S2 and MODIS NDVI values
var Diff = S2_and_MOD.map(function(img){
var clip = img.clip(geometry);
var Diffe = clip.expression('NDVI_S2 - NDVI_MOD',
{'NDVI_S2':clip.select('NDVI_S2') , 'NDVI_MOD':clip.select('NDVI_MOD')}).rename('Diff');
return Diffe
.copyProperties(img,['system:time_start','system:time_end']); });
print('NDVI Difference Collection',Diff);

Working with Date, Time in Google Apps Script

I need help with some quick coding with google apps script, linking to my googlesheets spreadsheet.
In the googlespreadsheets, I have a cell with the value “26-Jun-2020”. It is a date.
I want to use google apps script to calculate the number of days difference between that date (“26-Jun-2020”) and today’s day, but it won’t do the calculation for me somehow.
If I print only “expiry_date[i]” using Logger.log(expiry_date[i]), it will provide the output “Fri Dec 17 2021 01:00:00 GMT-0500 (Eastern Standard Time) “
function Put_Options_Expiry_Alert() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Long equity (sell puts)");
//var timeZone = AdsApp.currentAccount().getTimeZone(); //Get timezone of current spreadsheet
var status = sheet.getRange("F:F").getValues();
var expiry_date = sheet.getRange("M:M").getValues();
var potential_capitaloutlay_USD = sheet.getRange("Z:Z").getValues();
Logger.log("Length of status = " + status.length);
Logger.log("Length of expiry_date = " + expiry_date.length);
Logger.log("Length of potential_capitaloutlay_USD = " + potential_capitaloutlay_USD.length);
for (var i = 0; i < status.length; i++) {
if (status[i] == "Entered") { //Evaluate if this is a live Put position
//Calculate the time difference of two dates using date2. getTime() – date1. getTime();
//Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24)
Logger.log("expiry date is = "+expiry_date[i]);
Logger.log("today's date is = "+Date());
var days_to_expiry = dateDiffInDays(expiry_date[i],Date());
Logger.log(days_to_expiry);
}
}
}
// Function that returns the number of days difference between DateA and DateB
// DateA and DateB are javascript Date objects
function dateDiffInDays(DateA, DateB) {
var milliseconds_per_day = 1000 * 24 * 60; // number of milliseconds in a day
const utcA = Date.UTC(2021, DateA.getMonth(), DateA.getDate());
const utcB = Date.UTC(2020, DateB.getMonth(), DateB.getDate());
return Math.floor((utc2 - utc1) / milliseconds_per_day);
}
function timeDiffDays(Start, End) {
var day = 86400000;
var t1 = new Date(Start).valueOf();
var t2 = new Date(End).valueOf();
var d = t2 - t1;
return Math.floor(d / day);
}

Extract Index vegetation by points over collection in GEE

How I can extract index vegetation points over collections by adapting this beautiful code by #Rodrigo E. Principe:
Extract pixel values by points and convert to a table in Google Earth Engine
I try extract all values mas GEE is crashing, so only NDVI or EVI can works fine.
I did it with this tutorial https://developers.google.com/earth-engine/tutorial_api_06
// Dataset do sensor LS8
var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2018-04-01', '2019-03-31')
.select('B5', 'B4')
.filterBounds(aoi6010)
.filter(ee.Filter.lt('CLOUD_COVER', 20));
var addNDVI = function(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
return image.addBands(ndvi);
};
var withNDVI = dataset.map(addNDVI);
print(withNDVI);
// Empty Collection to fill
var ft = ee.FeatureCollection(ee.List([]))
var fill = function(img, ini) {
// type cast
var inift = ee.FeatureCollection(ini)
// gets the values for the points in the current img
var ft2 = img.reduceRegions(p601018, ee.Reducer.first(),30)
// gets the date of the img
var date = img.date().format()
// writes the date in each feature
var ft3 = ft2.map(function(f){return f.set("date", date)})
// merges the FeatureCollections
return inift.merge(ft3)
}
// Iterates over the ImageCollection
var newft = ee.FeatureCollection(withNDVI.iterate(fill, ft))

Google Earth Engine: Flatten a one-band ImageCollection into a multi-band single Image

I want to use supervised classification to classify a pattern that has a clear temporal pattern. For example, identifying stands of deciduous trees in a coniferous forest. NDVI would change overtime in the deciduous stands in a regular pattern that should be easily detectable. I assume there's an easy method to flatten the temporal dataset into a single image so that the bands in that image can be used in a classification algorithm. Maybe using .map(....)?
Here's some code to build the answer from:
var startDate = '2016-05-01';
var endDate = '2016-09-01';
var lng = -122.3424; var lat = 37.9344; //SF
var region = ee.Geometry.Point(lng, lat);
//Image Import
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(region)
.filterDate(startDate,endDate);
// NDVI temporal
var ndvi = l8.map(function(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename("NDVI");
return ndvi;
});
Map.addLayer(ndvi,{},"NDVI Temporal"); // 8 images with 1 band
//NDVI FLATTENED??????? I want 1 image with 8 bands. The below code doesn't work...
var ndviFlat = ee.Image().addBands(ndvi.map(function(image){
var temp = image.select("NDVI");
return temp;
}));
From there, I will pass ndviFlat to .sampleRegions, which only works with Images not ImageCollections:
//Classification Model:
var points = ee.FeatureCollection([trainingPointsPos,trainingPointsNeg]).flatten();
var training = ndviFlat.sampleRegions({
collection: points,
properties: ['class'],
scale: 30
});
var trained = ee.Classifier.randomForest(20).train(training, 'class', bands);
classified = regLayers.select(bands).classify(trained);
Here's one way:
var startDate = '2016-05-01';
var endDate = '2016-09-01';
var lng = -122.3424;
var lat = 37.9344; //SF
var region = ee.Geometry.Point(lng, lat);
//Image Import
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(region)
.filterDate(startDate, endDate);
var empty = ee.Image();
// NDVI temporal
var ndvi = ee.Image(l8.iterate(function(image, previous) {
var name = ee.String('NDVI_').cat(image.id());
var ndvi = image.normalizedDifference(['B5', 'B4']).rename(name);
return ee.Image(previous).addBands(ndvi);
}, empty));
// Remove the annoying non-band
ndvi = ndvi.select(ndvi.bandNames().remove('constant'));
Map.centerObject(region, 13);
Map.addLayer(ndvi, {}, 'ndvi');

add duration in timestamp data do not give right time moment js

I want to create an array of 10 minutes span for 1 hour using moment.js but it fails on first step.
var startTime = moment().unix();
var endTime = moment().add(1,'h').unix();
getTimeSheet(startTime,endTime);
var getTimeSheet = function(st, et) {
console.log(arguments);
var timeSheet = [];
var duration = moment.duration({'minutes' : 10});
var ct = st;
console.log(ct);
while (ct <= et ){
var n10 = moment.unix(st).add(duration).unix();
timeSheet.push(n10);
console.log(n10);
ct = n10;
}
console.log(timeSheet);
};
But this is Unable to create array
In each iteration of while loop you are adding 10 minutes to st variable. So n10 and ct still contain the same value. That's why this loop never ends.
You need to change:
var n10 = moment.unix(st).add(duration).unix();
to
var n10 = moment.unix(ct).add(duration).unix();
You can also check working example with some minor improvements here:
https://jsfiddle.net/65gL9tgr/2/

Resources