Select Night Lights in Google Earth Engine by year - select

I am able to select an image by year from the OLS dataset with the code examples from the web:
// Load a Japan boundary from a Fusion Table.
var japan = ee.FeatureCollection('ft:1tdSwUL7MVpOauSgRzqVTOwdfy17KDbw-1d9omPw')
.filter(ee.Filter.eq('Country', 'Japan'));
// Load a 2012 nightlights image, clipped to the Japan border.
var nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182009')
.select('stable_lights')
.clipToCollection(japan);
However, when I try to use the ImageCollection I am unable to select by date like with other datasets (such as Landsat):
var collection = ee.ImageCollection('LANDSAT/LE07/C01/T1')
.filterDate('2000-01-01', '2001-01-01');
I would like to be able to apply the same filters on the OLS dataset:
var ols = ee.ImageCollection("NOAA/DMSP-OLS/CALIBRATED_LIGHTS_V4")
.filterDate('2000-01-01', '2001-01-01')
.select('stable_lights')
.clipToCollection(japan);

You're just using the wrong ImageCollection ID.
If you use NOAA/DMSP-OLS/NIGHTTIME_LIGHTS, the filter work:
var ols = ee.ImageCollection("NOAA/DMSP-OLS/NIGHTTIME_LIGHTS")
.filterDate('2000-01-01', '2001-01-01')
.select('stable_lights')
print(ols)
// ImageCollection NOAA/DMSP-OLS/NIGHTTIME_LIGHTS (2 elements)
// type: ImageCollection
// id: NOAA/DMSP-OLS/NIGHTTIME_LIGHTS
// version: 1509484869949711
// bands: []
// features: List (2 elements)
// properties: Object (17 properties)

Related

Counting cloudless days using Google Earth Enigne - Sentinel-5p

I perform some analysis on Sentinel-5P data using Google Earth Engine. I would like to know how many cludless days are during month for each pixel. I am using the code below and it works. The problem is that Sentinel-5P images are captured several times a day so my result is number of cloudless images instead of cloudless days.
//Poland's border
var polska = ee.FeatureCollection('users/patrykgrzybowski1991/POL_adm1');
//upload collection
var collection_january_19 = ee.ImageCollection('COPERNICUS/S5P/NRTI/L3_NO2')//('COPERNICUS/S5P/NRTI/L3_NO2')
//filters
.filterBounds(ee.FeatureCollection('users/patrykgrzybowski1991/POL_adm0'))
.filterDate('2019-01-01', '2019-02-01')
.map(function(img){return ee.Image(img.select('tropospheric_NO2_column_number_density')).updateMask(img.select('cloud_fraction').lt(0.4))})
//cludless images - count
var count_january_19 = collection_january_19.count();
Number of cloudless days is slightly ill-defined when there are multiple images, but if you mean "none of the images on a day had clouds in them", then you probably need to composite the images by day before counting:
var start = ee.Date('2019-01-01')
var daysAsList = ee.List.sequence(0, 31).map(function(n) {
n = ee.Number(n)
var begin = start.advance(n, 'day')
var end = begin.advance(1, 'day')
return collection_january_19.filterDate(begin, end).mosaic()
})
var days = ee.ImageCollection.fromImages(daysAsList).count()

Problem with understanding date formats in googleScripts

I made a few functions with GoogleSheets using AppsScripts for simple task a few times in previous years. I always had problems when taking dates from cells/ranges and processing them, but somehow alwaays found a workaround, so that I did not have to deal with it. Well, this time I can not find a workaround, so I will try to explain my problems with the following code:
function getDates(){
var s = SpreadsheetApp.getActiveSpreadsheet();
var sht = s.getSheetByName('Dates');
var date = sht.getRange(2,1).getValues();
Logger.log(date[0][0]); //output is Tue Jun 08 18:00:00 GMT-04:00 2021
var datumFilter= Utilities.formatDate(date[0][0], "GMT+1", "dd/mm/yy");
Logger.log(datumFilter); //output is 08/00/21
var outrng = sht.getRange(25,1);
outrng.setValue(date);
}
The first targeted cell ('var date') has a value of "9.6.21" in the spreadsheet. The cell is formatted as a date and it opens a calendar when double-clicked. When I set the new cells values (with 'outrng.setValue(date);'), the result is OK, with the same date as in the original cell.
But I do not need to simply transfer the values, I want to implement them in some loops and I have no idea how to simply get the date in the same format or at least the same date in the script as it is in the cell. As you can see from the logger, the values there are different. A simple d/m/yy format would be sufficient.
My spreadsheet settings are set to my local time (Slovenia, GMT+1).
I am guessing that I am missing some basics here. I have spent many hours trying to understand it, so any help is highly appreciated!
Cooper already answered all your questions in the comment. I'd like to add on and show you an example on what it would like and add some modifications.
Code:
function getDates() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var sht = s.getSheetByName('Dates');
// get last row of the sheet
var lastRow = sht.getLastRow();
// get your sheet's timezone
var timezone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var output = [];
// getValues is mostly used for multiple cells returning a 2D array
// use getValue for single cells to return its actual value
// but since function name is getDates, I assume column A is all dates
// so we fetch the whole column (A2:A[lastRow]) except the header
var dates = sht.getRange("A2:A" + lastRow).getValues();
// for each date on that column, we format the date to d/M/yy
// m/mm = minute
// M/MM = month
dates.forEach(function ([date]){
Logger.log(date);
var datumFilter= Utilities.formatDate(new Date(date), timezone, "d/M/yy");
Logger.log(datumFilter);
// collect all dates in an array
output.push([datumFilter]);
});
// assign all the dates in the array onto range B2:B
sht.getRange(2, 2, output.length, 1).setValues(output);
}
Sample data:
Logs:
Output:
Note:
The output on sheets is not equal to logs due to the formatting of my sheet.

Filtering Sentinel 1 Image Collection to my area of interest produced no feature (or image) in google-earth-engine

My objective it to load and filter 2015 to 2016 sentinel images for my project area (AOI); The code produced image collection without image. Can you help me
The following is the code originally from https://krstn.eu/analyze-Sentinel-1-time-series-in-Google-Earth-Engine it does not work for my project area. It results in o elements in the image collection
Load the Sentinel-1 ImageCollection.
var sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD');
Filter VH, IW
var vh = sentinel1
// Filter to get images with VV and VH dual polarization.
.filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VH'))
// Filter to get images collected in interferometric wide swath mode.
.filter(ee.Filter.eq('instrumentMode', 'IW'))
// reduce to VH polarization
.select('VH')
// filter 10m resolution
.filter(ee.Filter.eq('resolution_meters', 10));
// Filter to orbitdirection Descending
var vhDescending = vh.filter(ee.Filter.eq('orbitProperties_pass', 'DESCENDING'));
// Filter time 2015
var vhDesc2015 = vhDescending.filterDate(ee.Date('2015-01-01'), ee.Date('2015-12-31'));
Filter to MKD AOI
var AOI = ee.Geometry.Polygon(
[[[40.5548410121811,6.969011579129182],
[39.0332345668686,7.241560288027144],
[37.5226144496811,7.050793118016936],
[37.9675607387436,6.521691240305265],
[39.6539621059311,6.390691419627786],
[40.5548410121811,6.969011579129182]]]);
var s1_mkd = vhDesc2015.filterBounds(AOI);
print(s1_mkd,'s1_mkd');
The following is the output of the print
ImageCollection COPERNICUS/S1_GRD (0 elements)
type: ImageCollection
id: COPERNICUS/S1_GRD
version: 1533921047325895
bands: []
features: []
properties: Object (15 properties)
There are simply no images matching your criteria. For some parts of the world, Sentinel-1 imagery weren't acquired at full operational capacity until mid-late 2016. Change your date range in filterDate() to include more recent imagery (e.g., all of 2017 or 2018) and you will start seeing something closer to the Sentinel-1 12-day revisit.

Having issues with pulling a date from the spreadsheet

I am working with Google Scripts and here is my problem.
I am attempting to compare today's date with a date entered into column C in the spreadsheet. The code I have seems like it should work but when I use the logger to see the date it gives me "Wed Dec 31 19:00:00 GMT-05:00 1969" instead of 2018-07-29 1:00 PM.
I know I need to use the Utilities.formatDate in order to compare them but I can't seem to understand why it is not pulling the date in column C.
Here is my code below:
function sendEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3; // First row of data to process
var numRows = sheet.getLastRow()-1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues();
//Logger.log(data); // give you the data you are looking at comparing
for (var i in data) {
var row = data[i];
var date = new Date();
//Logger.log(date); // sets to todays date and time.
var sheetDate = new Date(row[3]);
Logger.log(sheetDate); // date in the row you are comparing
You're not looking at the value in column C. The method getValues() returns a two-dimensional array, and arrays are 0-indexed. This means that you should be using a 2 (not a 3), when defining sheetDate.
var sheetDate = new Date(row[2]);
As mentioned in the comments, you don't need to use Utilities.formatDate(), but that's a separate issue. I would also try logging the value before converting it to a date (e.g. Logger.log(row[2])).

.getMonth and .getYear etc. won't extract correctly from dates in Google Sheets

I have dates stored in a column of a Google Sheet and I want to extract parts of the dates such as year and month. I tried to follow examples I have found but they do not work. Here is the code:
var analysisSS = SpreadsheetApp.getActiveSpreadsheet();
var analysisSheet = analysisSS.getSheetName();
var monthsListRange = analysisSS.getRangeByName(MonthDateRange);
var monthsList = monthsListRange.getValues();
var monthPointer = 1;
var monthCode = monthsList[monthPointer].getMonth();
Which produces the error message:
TypeError: Cannot find function getMonth in object Sun Jan 01 2017 00:00:00 GMT-0800 (PST).DetailsDismiss
This is the correct date stored in the first cell of the spreadsheet column.