Creating a world map of where it's nighttime or daytime

Hello all,

I am fairly new to Processing, and I would like to create a world map of where it’s daytime and where it’s nighttime on the planet. Essentially, trying to emulate this: Day and Night World Map

I have code to bring in two images (one of the earth during the day, and one of the earth during the night) and I know how to stitch them together, depending if it’s daytime or nighttime at a specific location. However, what I don’t have is an algorithm to figure out if it’s daytime or nighttime at a specific latitude or longitude (which I would then convert to a specific pixel location).

Does anyone know of an algorithm to do this? I’m think the easiest way to do this would be to: for every latitude, calculate at what longitude it’s currently sunrise and what longitude it’s currently sunset, and then shade the latitudal line between these two locations accordingly. Alternatively, I could try to calculate for every latitude what time is sunrise and what time is sunset, and then along that latitude, I could go to every longitude and determine the local time at that location and see if it’s night or day.

Thoughts? This seems like a tough calculation to accomplish that would require a lot of trigonometry that is beyond me. Alternatively, is there any sort of Astronomy library that has this information that would be easily accessible?

Thanks for your help!

Hello @David1

That sounds like an interesting project!!

It might be worth looking into this Time API, where you can search for location via longitude and latitude.
It looks like a fee is involved though a free trial is available. At the very least it may provide some clues on how to proceed.

Good luck!
:nerd_face:

1 Like

time is UTC time going from 0 to 1 from midnight each day.
date is part of the year from 0 to 1 with 0 at winter solstice, 0.5 at summer solstice (so day number of the year plus 10 and divide by 365.25).

// https://www.solarsystemscope.com/textures/download/2k_earth_daymap.jpg
// https://www.solarsystemscope.com/textures/download/2k_earth_nightmap.jpg

PImage daymap;
PImage nightmap;
PShader shdr;

void setup() {
  size( 2048, 1024, P2D );
  shdr = new PShader( g.parent, vertSrc, fragSrc );
  daymap = loadImage("2k_earth_daymap.jpg");
  nightmap = loadImage("2k_earth_nightmap.jpg");
  shdr.set("daymap", daymap);
  shdr.set("nightmap", nightmap);
}

void draw() {
  shdr.set( "time", frameCount/600. % 1 );
  shdr.set( "date", frameCount/3600. % 1 );  // Nov 4 is 0.87
  shader( shdr );
  rect( 0, 0, width, height );
}


String[] vertSrc = { """
#version 330
uniform mat4 transformMatrix;
in vec4 position;
void main() {
  gl_Position = transformMatrix * position;
}
""" };

String[] fragSrc = { """
#version 330
precision highp float;
uniform vec2 resolution;
uniform sampler2D daymap;
uniform sampler2D nightmap;
uniform float time;
uniform float date;
#define TAU 6.28318530178

out vec4 fragColor;

vec2 rot( in vec2 p, float a ) {
   return cos(a)*p + sin(a)*vec2(p.y, -p.x);
}

void main() {
  vec2 sp = gl_FragCoord.xy/resolution-0.5;
  vec3 p = vec3( -1, 0, 0 );
  p.xz = rot( p.xz, sp.y*TAU*0.5 );   // latitude
  p.xy = rot( p.xy, sp.x*TAU );       // longitude
  
  p.xy = rot( p.xy, -TAU*date );      // counter-rotate the sun
  p.xy = rot( p.xy, TAU*time );       // time of day
  p.zx = rot( p.zx, TAU/360*23.5 );   // tilt of the Earth
  p.xy = rot( p.xy, TAU*date );       // date of year

  vec2 uv = gl_FragCoord.xy/resolution;
  uv.y = 1.0 - uv.y;
  vec3 day = texture( daymap, uv ).rgb;
  vec3 night = texture( nightmap, uv ).rgb;
  vec3 col = mix( night, day, smoothstep( -0.1, 0.05, p.x ) );

  fragColor = vec4( col, 1. );
}
""" };
1 Like

Oops, it looks right, but the rotation dealing with the Earth’s tilt isn’t quite right – it rotates the Earth along with rotating the axis, so midnight ends up during the day on the summer solstice. Gotta think it through a bit more…

Edit: fixed. The code posted above should be correct now.

I did this off the top of my head. It would be good if someone could verify that my math is correct.