Skip to content

Commit 41ee38d

Browse files
committed
A date related operation
1 parent 117718a commit 41ee38d

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

Date/sunPosition

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later // please change accordingly based on the license in "reference"
2+
var inputs=[{name: 'time', description: 'Time for sun position computation (ee.Date)', type: 'ee.Date', defaultValue: ee.Date(Date.now()), optional: false},
3+
{name:'Return',description:'Return', type:'ee.Image',defaultValue:null,optional:true} // change the return type accordingly OR remove this line if the function doesn’t have an output
4+
]
5+
6+
var reference = {
7+
name: 'sunPosition',
8+
license: 'LGPL-3.0-or-later',
9+
description: 'Computes sun position angles (zenith, azimuth, elevation) for a given time. The azimuth is in clockwise form from north (like Landsat).',
10+
contributors: ['Mathieu Gravey']
11+
};
12+
13+
exports.createDocumentation=function(){ return {inputs:inputs, reference:reference};}
14+
15+
exports.createFunction=function(internal,relPath,oeel){
16+
return function(){
17+
var inputDictionary=internal.readInputs(Array.prototype.slice.call(arguments,0),inputs,reference,relPath);
18+
internal.addRef(reference,relPath);
19+
20+
var time = ee.Date(inputDictionary.time);
21+
22+
// Compute equation of time and declination.
23+
var D = ee.Number.expression(
24+
"6.24004077+0.01720197*365.25*y_2000",
25+
{y_2000: time.getFraction("year").add(time.get("year").subtract(2000))}
26+
);
27+
var eqtime = ee.Number.expression(
28+
"-7.659*sin(D)+9.863*sin(2*D+3.5932)",
29+
{D: D}
30+
);
31+
var decl = ee.Number.expression(
32+
"-asin(0.39779*cos(2*PI/365.25*(N+10)+0.0167*sin(2*PI/365.25*(N-2))))",
33+
{N: time.getFraction("year").multiply(365.25), PI: Math.PI}
34+
);
35+
36+
var longLat = ee.Image.pixelLonLat().multiply(Math.PI / 180);
37+
var lambda_s = time.getFraction("day")
38+
.subtract(0.5)
39+
.add(eqtime.divide(60 * 24))
40+
.multiply(-Math.PI * 2);
41+
42+
var S_x = longLat.select("longitude")
43+
.subtract(lambda_s)
44+
.multiply(-1)
45+
.sin()
46+
.multiply(ee.Number(decl).cos());
47+
48+
var step1 = longLat.select("longitude")
49+
.subtract(lambda_s)
50+
.cos()
51+
.multiply(ee.Number(decl).cos());
52+
53+
var S_y = step1.multiply(longLat.select("latitude").sin())
54+
.multiply(-1)
55+
.add(longLat.select("latitude").cos().multiply(ee.Number(decl).cos()));
56+
57+
var S_z = step1.multiply(longLat.select("latitude").cos())
58+
.add(longLat.select("latitude").sin().multiply(ee.Number(decl).sin()));
59+
60+
var zenith = S_z.acos().rename("zenith").divide(Math.PI / 180);
61+
var azimuth = S_y.atan2(S_x).rename("azimuth").divide(Math.PI / 180);
62+
var elevation = zenith.subtract(90).multiply(-1).rename("elevation");
63+
64+
// Return an image with three bands: zenith, azimuth, elevation.
65+
return ee.Image.cat([zenith, azimuth, elevation]).set("system:time_start",time.millis(),"system:time_end",time.millis());
66+
}
67+
}

Date/sunRiseSet

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later // please change accordingly based on the license in "reference"
2+
var inputs=[{name: 'time', description: 'Time for sun position computation (ee.Date)', type: 'ee.Date', defaultValue: ee.Date(Date.now()), optional: false},
3+
{name: 'angle', description: 'Solar elevation angle image in degrees (default is ee.Image(90.833))', type: 'ee.Image', defaultValue: ee.Image(90.833), optional: false},
4+
{name:'Return',description:'Return', type:'ee.Image',defaultValue:null,optional:true} // change the return type accordingly OR remove this line if the function doesn’t have an output
5+
]
6+
7+
var reference = {
8+
name: 'sunRiseSet',
9+
license: 'LGPL-3.0-or-later',
10+
description: 'Computes sunrise and sunset times (in hours) for a given time. The angle input defaults to ee.Image(90.833) corresponding to the standard solar elevation for sunrise/sunset.',
11+
contributors: ['Mathieu Gravey']
12+
};
13+
14+
exports.createDocumentation=function(){ return {inputs:inputs, reference:reference};}
15+
16+
exports.createFunction=function(internal,relPath,oeel){
17+
return function(){
18+
var inputDictionary=internal.readInputs(Array.prototype.slice.call(arguments,0),inputs,reference,relPath);
19+
internal.addRef(reference,relPath);
20+
21+
var time = inputDictionary.time;
22+
var angleImage = inputDictionary.angle;
23+
24+
// Compute declination and equation of time from the provided time.
25+
var D = ee.Number.expression(
26+
"6.24004077 + 0.01720197 * 365.25 * y_2000",
27+
{ y_2000: time.getFraction("year").add(time.get("year").subtract(2000)) }
28+
);
29+
var eqtime = ee.Number.expression(
30+
"-7.659*sin(D) + 9.863*sin(2*D+3.5932)",
31+
{ D: D }
32+
);
33+
var decl = ee.Number.expression(
34+
"-asin(0.39779*cos(2*PI/365.25*(N+10) + 0.0167*sin(2*PI/365.25*(N-2))))",
35+
{ N: time.getFraction("year").multiply(365.25), PI: Math.PI }
36+
);
37+
38+
// Compute pixel longitude and latitude in radians.
39+
var longLat = ee.Image.pixelLonLat().multiply(Math.PI / 180);
40+
41+
// Compute lambda using the provided angle.
42+
var lambda = angleImage
43+
.multiply(Math.PI / 180)
44+
.cos()
45+
.subtract(longLat.select("latitude").sin().multiply(ee.Number(decl).sin()))
46+
.divide(longLat.select("latitude").cos())
47+
.divide(ee.Number(decl).cos())
48+
.acos();
49+
50+
// Compute sunrise (rise) and sunset (set) times in hours.
51+
var rise = lambda
52+
.subtract(longLat.select("longitude"))
53+
.divide(-Math.PI * 2)
54+
.subtract(eqtime.divide(60 * 24))
55+
.add(0.5)
56+
.multiply(24)
57+
.rename("rise");
58+
59+
var set = lambda.multiply(-1)
60+
.subtract(longLat.select("longitude"))
61+
.divide(-Math.PI * 2)
62+
.subtract(eqtime.divide(60 * 24))
63+
.add(0.5)
64+
.multiply(24)
65+
.rename("set");
66+
67+
// Return an image with two bands: "rise" and "set".
68+
return ee.Image.cat([rise, set]).set("system:time_start",time.millis(),"system:time_end",time.millis());
69+
}
70+
}

0 commit comments

Comments
 (0)