Aggregate GPM hourly data to daily in GEE - google-app-engine

I need to aggregate the sum of 48 half-hourly images per day of a GPM collection getting an imageCollection with the band "precipitationCal" and daily images
I've tried to fill and iterate an empty featureCollection but I get an empty collection without images
var dataset = ee.ImageCollection('NASA/GPM_L3/IMERG_V05')
var startdate = ee.Date.fromYMD(2014,3,1)
var enddate = ee.Date.fromYMD(2014,4,1)
var precipitation = dataset.filter(ee.Filter.date(startdate,enddate)).select('precipitationCal')
print(precipitation)
var difdate = enddate.difference(startdate, 'day')
// Time lapse
var lapse = ee.List.sequence(0, difdate.subtract(1))
var startdate = ee.Date('2014-01-01')
var listdates = lapse.map(function(day){
return startdate.advance(day, 'day')
})
var pts = ee.FeatureCollection(ee.List([]))
var newft = ee.FeatureCollection(listdates.iterate(function(img, ft) {
// Cast
ft = ee.FeatureCollection(ft)
var day = ee.Date(img)
// Filter the collection in one day
var day_collection = precipitation.filterDate(day, day.advance(1, 'day'))
// Get the sum of all 24 images into one Image
var sum = ee.Image(day_collection.sum())
// Return the FeatureCollection with the new properties set
return sum
}, listdates))

Please have a try about my package pkg_trend. aggregate_prob function in it, works just like aggregate in R language.
var imgcol_all = ee.ImageCollection('NASA/GPM_L3/IMERG_V06');
function add_date(img){
var date = ee.Date(img.get('system:time_start'));
var date_daily = date.format('YYYY-MM-dd');
return img.set('date_daily', date_daily);
}
var startdate = ee.Date.fromYMD(2014,3,1);
var enddate = ee.Date.fromYMD(2014,4,1);
var imgcol = imgcol_all
.filter(ee.Filter.date(startdate,enddate)).select('precipitationCal')
.map(add_date);
// imgcol = pkg_trend.imgcol_addSeasonProb(imgcol);
print(imgcol.limit(3), imgcol.size());
var pkgs = require('users/kongdd/pkgs:pkgs.js');
var imgcol_daily = pkgs.aggregate_prop(imgcol, "date_daily", 'sum');
print(imgcol_daily);
Map.addLayer(imgcol_daily, {}, 'precp daily');
The GEE link is https://code.earthengine.google.com/3d8c7e68e0a7a16554ff8081880bfdad

According to GEE GPM page, This product is a half-hour precipitation rate. So, to get daily sum with the code above you should divide every image in collection by 2.

Related

Multiple Issues with Google Sheets Scripts (JavaScript)

NEEDLES TO SAY, I am very new to this, but trying my hardest to figure all this out. I have a script right now which actively removes all empty rows from an entire workbook. But I'm hoping someone can assist is narrowing this down to a single sheet within that workbook.
function removeEmptyRows() {
SpreadsheetApp.getActive()
.getSheets()
.forEach(function (s) {
c = 0;
s.getRange(1, 1, s.getMaxRows(), s.getMaxColumns())
.getValues()
.forEach(function (r, j) {
if (r.toString()
.replace(/,/g, "")
.length == 0) {
s.deleteRow((j += 1) - c)
c += 1;
}
})
})
}
Ideally I would like for this to remove only blank rows on one sheet within my workbook called 'Racing Results'. The reason I'm need this, is due to how the spreadsheet is setup and having multiple rows merged together. So when I copy these results over to a different sheet, there are gaps between them and I'd like them removed. Here is the script I'm using to copy the data to another sheet.
function Copy() {
var sss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ss = sss.getSheetByName('Score Card');
var range = ss.getRange('A32:E36');
var data = range.getValues();
var tss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ts = tss.getSheetByName('Race Results');
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
If not to throw one final monkey wrench into this colossal mess, I have been attempting to copy two different cell ranges within the same script and I feel like this isn't possible because only the latter one gets copied and the initial one is discarded. Here is the other copy script I am using which runs before the one above this.
function Copy() {
var sss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ss = sss.getSheetByName('Score Card');
var range = ss.getRange('A1:A1');
var data = range.getValues();
var tss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ts = tss.getSheetByName('Race Results');
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
Score Card Screenshot
In a perfect world, what I'm trying to get setup is the following. Above is a screenshot of the scorecard we are using. I am wanting to copy the Current Date (A1) to the 'Racing Results' sheet, then I want to copy the final score with the team names (A32:E36) and move them to the 'Racing Results' sheet right under it. Once that has been done, I want to remove the empty rows between the results because as of right now this is how it looks when copying over. (see image below)
Race Results Screenshot
Thanks in advance to anyone who is able to assist with any of this in any way shape or form.
Edit:
Removing Empty Rows from a spreadsheet is functioning as intended. Still having issues with Copying multiple times in the same action even when giving them different names. Here is my updated script.
function CopyDate() {
var sss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ss = sss.getSheetByName('Score Card');
var range = ss.getRange('A1:A1');//This range is only one cell
var data = range.getValues();
var tss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ts = tss.getSheetByName('Race Results');
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
function CopyScore() {
var sss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ss = sss.getSheetByName('Score Card');
var range = ss.getRange('A32:E36');
var data = range.getValues();
var tss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ts = tss.getSheetByName('Race Results');
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
function delBlankRows(shtname){
var shtname=shtname || 'Race Results';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName(shtname);
var rg=sh.getRange(1,1,sh.getMaxRows(),sh.getLastColumn());
var vA=rg.getValues();
var n=0;
for(var i=0;i<vA.length;i++){
if(!vA[i].join("")){
sh.deleteRow(i-n+1);
n++;
}
}
}
function Sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
function SaveRaceResults() {
CopyDate();
Sleep(5000);
CopyScore();
Sleep(5000);
delBlankRows();
}
Deleting All Blank Rows on a Sheet
function delBlankRows(shtname){
var shtname=shtname || 'Sheet1';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName(shtname);
var rg=sh.getRange(1,1,sh.getMaxRows(),sh.getLastColumn());
var vA=rg.getValues();
var n=0;
for(var i=0;i<vA.length;i++){
if(!vA[i].join("")){
sh.deleteRow(i-n+1);
n++;
}
}
}
Copy function:
function Copy() {
var sss = SpreadsheetApp.getActive();
var ss1 = sss.getSheetByName('Score Card');
var range1 = ss1.getRange('A1');//This range is only one cell
var data1 = range1.getValues();
var ts1 = sss.getSheetByName('Race Results');
ts1.getRange(ts1.getLastRow()+1, 1, data1.length, data1[0].length).setValues(data1);
var ss2 = sss.getSheetByName('Score Card');
var range2 = ss2.getRange('A32:E36');
var data2 = range2.getValues();
var ts2 = sss.getSheetByName('Race Results');
ts2.getRange(ts2.getLastRow()+1, 1, data2.length, data2[0].length).setValues(data2);
}

Increase my script performance Google Sheets Script

I created a function that whenever I run the AppendRow script every row that does not have a dot (".") at the AY column, an array with every information/column that I want from that sheet will be transfered to my main sheet that has around 13k rows atm.
Usually about 20-40 rows get pasted into the first sheet everyday and this script automatically re-arranges the columns to my main sheet.
PROBLEM:
The problem is that each row that gets appended to my main sheet takes around 8-15sec to get transfered and sometimes I get this "exceeded-maximum-execution-time" error thats its really annoying.
Is there any way to make my code run faster or maybe just a way to make the script run more than 5 minutes?
function AppendRow() {
var app = SpreadsheetApp;
var Sheet = app.getActiveSpreadsheet().getSheetByName("Sheet1");
var ss2017 = SpreadsheetApp.openById("");
var sheet2017 = ss2017.getSheetByName("2017");
var lastSourceRow = Sheet.getLastRow();
var lastSourceCol = Sheet.getLastColumn();
var sourceRange = Sheet.getRange(1, 1, lastSourceRow, lastSourceCol);
var sourceData = sourceRange.getValues();
var lenght = sourceData.length;
//Logger.log(lenght);
var time = new Date();
time = Utilities.formatDate(time, "GMT+01:00", "dd/MM/yy, HH:mm:ss");
for(i=5326;i<=lenght;i++)
//my i=5326 is just cuz I had already
// information on my sheet that was transfered before this script existed
{
var columnAY = Sheet.getRange(i,51).getValue();
var checkReservation = Sheet.getRange(i,2).getValue();
if(columnAY == ".")
{
}
else
{
var B = Sheet.getRange(i,2).getValue();
var F = Sheet.getRange(i,6).getValue();
var E = Sheet.getRange(i,5).getValue();
var K = Sheet.getRange(i,11).getValue();
var L = Sheet.getRange(i,12).getValue();
var V = Sheet.getRange(i,22).getValue();
var O = Sheet.getRange(i,15).getValue();
var P = Sheet.getRange(i,16).getValue();
var Q = Sheet.getRange(i,17).getValue();
var AF = Sheet.getRange(i,32).getValue();
var AG = Sheet.getRange(i,33).getValue();
var N = Sheet.getRange(i,14).getValue();
var AI = Sheet.getRange(i,35).getValue();
var AB = Sheet.getRange(i,28).getValue();
var AC = Sheet.getRange(i,29).getValue();
var array = ["",B,F,E,K,L,"",V,O,P,Q,AF,AG,"","",N,N,AI,"","",AB,AC,"","","","",time];
sheet2017.appendRow(array);
Sheet.getRange(i,51).setValue(".");
}
}
}
p.s.: I also have a menu in my spreadsheet to run the script every time i want to not sure if that is any relevant to the question :P
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('My Menu')
.addItem('Copy to 2017', 'AppendRow')
.addToUi();
Every comment is appreciated !! :)
I must admit to having a bit of a problem figure out how my i's coordinated with your's but I think my columns are correct. So check it out but this should run a bit faster.
function AppendRow()
{
var ss0=SpreadsheetApp.getActive();
var sh0=ss.getSheetByName('Sheet1');
var rg0=sh0.getDataRange();
var vA0=rg0.getValues();
var ss1=SpreadsheetApp.openById(id);
var sh1=ss1.getSheetByName('2017');
var TimeStamp=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd/MM/yy HH:mm:ss");
for(i=5326;i<vA0.length;i++)
{
var columnAY=vA0[i][50];
var checkReservation=vA0[i][1];
if(vA0[i][50]!='.')
{
var B = vA0[i][1];//Sheet.getRange(i,2).getValue();
var F = vA0[i][5];//Sheet.getRange(i,6).getValue();
var E = vA0[i][4];//Sheet.getRange(i,5).getValue();
var K = vA0[i][10];//Sheet.getRange(i,11).getValue();
var L = vA0[i][11];//Sheet.getRange(i,12).getValue();
var V = vA0[i][21];//Sheet.getRange(i,22).getValue();
var O = vA0[i][14];//Sheet.getRange(i,15).getValue();
var P = vA0[i][15];//Sheet.getRange(i,16).getValue();
var Q = vA0[i][16];//Sheet.getRange(i,17).getValue();
var AF = vA0[i][31];//Sheet.getRange(i,32).getValue();
var AG = vA0[i][32];//Sheet.getRange(i,33).getValue();
var N = vA0[i][13];//Sheet.getRange(i,14).getValue();
var AI = vA0[i][34];//Sheet.getRange(i,35).getValue();
var AB = vA0[i][27];//Sheet.getRange(i,28).getValue();
var AC = vA0[i][28];//Sheet.getRange(i,29).getValue();
var array = ["",B,F,E,K,L,"",V,O,P,Q,AF,AG,"","",N,N,AI,"","",AB,AC,"","","","",TimeStamp];
sh1.appendRow(array);
sh0.getRange(i+1,51).setValue(".");
}
}
}

Google Scripts For Loop

I'm trying to insert some data from a spreadsheet into a different spreadsheet, the problem is that the loop is not behaving as expected, it only gives me one entry in the target spreadsheet. I've tried using while and no function but it didn't work.
Here is the code:
function move(){
var homeBook = SpreadsheetApp.getActiveSpreadsheet();
var sheet = homeBook.getSheets()[0];
var limit = sheet.getLastRow(); //number of rows in the sheet
var evento = sheet.getRange(2, 1, limit-1).getValues(); //event list
var descript = sheet.getRange(2,2,limit-1).getValues(); //description list
var tags = sheet.getRange(2,3,limit-1).getValues(); //tag list
var sheetsIDHome = sheet.getRange(2,4,limit-1).getValues(); //ID list
var targetBook = SpreadsheetApp.openById("1t3qMTu2opYffLmFfTuIbV6BrwsDe9iLHZJ_ZT89kHr8"); //target workbook
var target = targetBook.getSheets()[0]; //Sheet1
var targetLimit =target.getLastRow(); //Rows with content
var sheetsIDTarget = target.getRange(targetLimit, 4); // ID list
var targetRow = targetLimit+1; //row where content is going to be inserted
for(i = 2;i <= limit;i++){//loop for each value to be inserted in each row of the target sheet
(function(x){
target.getRange(targetRow,1).setValue(x);
target.getRange(targetRow,2).setValue(descript[2]);
target.getRange(targetRow,3).setValue(tags[3]);
target.getRange(targetRow,4).setValue(sheetsIDHome[4]);
targetRow = targetRow++;
})(i);
};
You're trying to access the four arrays created from the values for columns 1-4.
Your for statement needs to match their structure, starting with the first array instance of 0. You can use any of the arrays for the iteration, I've chosen the first.
In addition, I've removed the function and replaced x with the instance from evento.
++ increments the value of the variable, no need for assignment there.
for (var i = 0; i < evento.length; i++) {
target.getRange(targetRow,1).setValue(evento[i]);
target.getRange(targetRow,2).setValue(descript[i]);
target.getRange(targetRow,3).setValue(tags[i]);
target.getRange(targetRow,4).setValue(sheetsIDHome[i]);
targetRow++;
}
there is a code that has been mis-placed, kindly check my code below:
function UpdateBSRSpecial()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var testForm = ss.getSheetByName("ENTRY FORM");
var testTable = ss.getSheetByName("BSR DATA");
var testFormValue = testForm.getRange("G6").getValue();
var rangeData = testTable.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
for(var i=2;i<=lastRow;i++){
var dataID = testTable.getRange(i,1).getValue();
if(testFormValue == dataID)
{
testTable.getRange(i,6).setValue("new value");
};
};
};
hope this helps

Angularjs - Time difference between my data time and CURRENT TIME

I am trying to display the time difference between my {{trade.timer}} and the current time but coudn't succeed after many tries.
I am looking to make the $scope.gains[i].timer = vtime - "CURRENTTIME"
Here is my code:
$scope.updatetimerValue = function (timerValue){
$.each(timerValue, function(k, v) {
for (var i =0; i < $scope.gains.length ; i ++) {
if($scope.gains[i].orderid == v.orderid){
$scope.gains[i].timer = v.time;
}
}
});
}
<td>{{gain.timer | date: 'HH:mm'}}</td>
Any idea?
Note: v.time time format is yyyy-MM-dd HH:mm:ss
You can get date difference between two days with basic javascript.
var date = new Date('10/27/2014');
var currentDate = new Date();
var milisecondsDiff = date-currentDate;
var secondsDiff = miliseconds/1000;
var minutesDiff = seconds/60;
var hoursDiff = minutes/60;
var daysDiff = hours/24;
Also I suggest don't mix up AngularJS and JQuery.
And instead $.each use the angular.forEach
angular.forEach(values, function(value, key) {
///
});
or even better to use simple for loop, because it works faster.

How to loop range of cells and set value in adjacent column

I'm learning Google Apps Scripts for use with Google Spreadsheets.
I have a list of URLs in one column and I want to write a script to get the title element from each URL and write it in the adjacent cell. I have accomplished this for one specific cell as per the following script:
function getTitles() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("url_list");
var range = sheet.getRange("G3");
var url = range.getValue();
var response = UrlFetchApp.fetch(url);
var doc = Xml.parse(response.getContentText(),true);
var title = doc.html.head.title.getText();
var output = sheet.getRange("H3").setValue(title);
Logger.log(title);
return title;
}
This gets the URL in G3, parses it, pulls the element and writes the output in H3.
Now that I have this basic building block I want to loop the entire G column and write the output to the adjacent cell but I'm stuck. Can anyone point me in the right direction?
May look something like this:
function getTitles() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("url_list");
var urls = sheet.getRange("G3:G").getValues();
var titleList = [], newValues = [],
response, doc, title;
for (var row = 0, var len = urls.length; row < len; row++) {
if (urls[row] != '') {
response = UrlFetchApp.fetch(urls[row]);
doc = Xml.parse(response.getContentText(),true);
title = doc.html.head.title.getText();
newValues.push([title]);
titleList.push(title);
Logger.log(title);
} else newValues.push([]);
}
Logger.log('newValues ' + newValues);
Logger.log('titleList ' + titleList);
// SET NEW COLUMN VALUES ALL AT ONCE!
sheet.getRange("H3").offset(0, 0, newValues.length).setValues(newValues);
return titleList;
}

Resources