interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / tools / ck_sky_gen.c

1427 lines · 59120 bytes Raw Blame History
1/* ck_sky_gen.c -- makes native/kernel/ui/sky_data.c: the real sky Allen
2 * opens on, as tables the kernel draws from with integer arithmetic only.
3 *
4 * Inputs (scripts/ck_sky_gen.sh downloads them, checks each against its
5 * sha256 and unpacks the two .gz files; nothing is fetched while the kernel
6 * runs), all in the --cache directory:
7 * bsc5.dat Yale Bright Star Catalogue, 5th revised edition
8 * (Hoffleit and Warren 1991; CDS V/50): every star to
9 * about magnitude 6.5, the naked-eye limit, with J2000
10 * position, proper motion, V magnitude and B-V colour;
11 * gaia_hpx6.csv Gaia DR3 source counts and integrated G-band flux per HEALPix
12
13
14 * NGC.csv, addendum.csv
15 * OpenNGC (Mattia Verga, CC BY-SA 4.0): galaxy
16 * positions, sizes and orientations;
17 * GW150914_skymap.fits
18 * the GWTC-1 sky map of GW150914 (LIGO/Virgo,
19 * LIGO-P1800381): where the first black-hole merger
20 * ever heard came from.
21 *
22 * What it works out (floating point is fine here; the kernel gets integers):
23 * - the view: a gnomonic (pinhole camera) projection centred on GW150914's
24 * most probable position, north up, a set width of sky across the screen;
25 * - the stars in that view as unit vectors (the kernel projects them), with
26 * a brightness code (flux = 10^(-0.4 m)) and a colour from B-V through a
27 * real temperature (Ballesteros 2012) to the colour of a black body of
28 * that temperature (CIE 1931 observer, sRGB);
29
30
31 * - the galaxies that are in the view and bright enough to see with the eye,
32 * at their true size and angle.
33 *
34 * Usage: ck_sky_gen --cache DIR --out sky_data.c [--report FILE]
35 * [--inputs FILE] [--ra DEG --dec DEG] [--fov DEG] [--roll DEG]
36 * [--mag LIMIT] [--grid N] [--gain X] [--skyglow MAG]
37 * [--exposure X]
38 * Host tool only; plain C11 and libm.
39 */
40#include <fcntl.h>
41#include <ctype.h>
42#include <math.h>
43#include <stdint.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <time.h>
48
49#define EPOCH 2026.8 /* the stars are moved along their proper motion to this year */
50/* The HEALPix level of the Gaia aggregates: 6 is 49152 cells (nside 64), 7 is 196608 (nside 128).
51 * scripts/ck_sky_gen.sh sets it (SKY_HPX_LEVEL); 6 is the committed default. */
52#ifndef HPX_LEVEL
53#define HPX_LEVEL 6
54#endif
55#define NSIDE (1 << HPX_LEVEL)
56#define NCELLS (12 * NSIDE * NSIDE)
57/* Gaia DR3 Vega zero points: a source of 1 e-/s has G 25.6874, BP 25.3385, RP 24.7479 (Gaia DR3 documentation,
58 * section 5.4.1, Table 5.4; Riello et al. 2021, A&A 649, A3). */
59#define GAIA_ZP_G 25.6874
60#define GAIA_ZP_BP 25.3385
61#define GAIA_ZP_RP 24.7479
62
63/* The screen shape the grid is made for: centre of the view at half the width
64 * and 40% of the height (where allen_ui.c puts the merger); rows cover a 16:10
65 * screen so a 1280x800 one is covered as well as 3840x2160. */
66#define CX_FRAC 0.5
67#define CY_FRAC 0.40
68#define H_OVER_W_MAX (10.0 / 16.0)
69
70static const double PI = 3.14159265358979323846;
71
72static void die(const char *fmt, ...) __attribute__((noreturn, format(printf, 1, 2)));
73#include <stdarg.h>
74static void die(const char *fmt, ...)
75{
76 va_list ap;
77 va_start(ap, fmt);
78 fprintf(stderr, "ck_sky_gen: ");
79 vfprintf(stderr, fmt, ap);
80 fprintf(stderr, "\n");
81 va_end(ap);
82 exit(1);
83}
84
85static void *xmalloc(size_t n)
86{
87 void *p = malloc(n ? n : 1);
88 if (!p)
89 die("out of memory (%zu bytes)", n);
90 return p;
91}
92
93static void *xrealloc(void *p, size_t n)
94{
95 p = realloc(p, n ? n : 1);
96 if (!p)
97 die("out of memory (%zu bytes)", n);
98 return p;
99}
100
101static double rad(double d) { return d * (PI / 180.0); }
102static double deg(double r) { return r * (180.0 / PI); }
103static double pmod(double x, double m)
104{
105 double r = fmod(x, m);
106 return r < 0 ? r + m : r;
107}
108static long rnd(double x) { return (long)nearbyint(x); } /* half to even, like the rest of the tables' maths */
109static double clampd(double x, double lo, double hi) { return x < lo ? lo : x > hi ? hi : x; }
110
111/* ---- a text report, kept in memory and printed at the end ---- */
112
113static char *rep;
114static size_t replen;
115static void report(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
116static void report(const char *fmt, ...)
117{
118 char buf[4096];
119 va_list ap;
120 va_start(ap, fmt);
121 int n = vsnprintf(buf, sizeof buf, fmt, ap);
122 va_end(ap);
123 if (n < 0)
124 die("report format");
125 if ((size_t)n >= sizeof buf)
126 n = (int)sizeof buf - 1;
127 rep = xrealloc(rep, replen + (size_t)n + 2);
128 memcpy(rep + replen, buf, (size_t)n);
129 replen += (size_t)n;
130 rep[replen++] = '\n';
131 rep[replen] = 0;
132}
133
134static uint8_t *slurp(const char *path, size_t *len)
135{
136 FILE *f = fopen(path, "rb");
137 if (!f)
138 die("cannot open %s (run scripts/ck_sky_gen.sh, it fetches the inputs)", path);
139 fseek(f, 0, SEEK_END);
140 long n = ftell(f);
141 fseek(f, 0, SEEK_SET);
142 uint8_t *b = xmalloc((size_t)n + 1);
143 if (fread(b, 1, (size_t)n, f) != (size_t)n)
144 die("short read of %s", path);
145 fclose(f);
146 b[n] = 0;
147 *len = (size_t)n;
148 return b;
149}
150
151/* ---- small vector helpers ---- */
152
153typedef struct {
154 double x, y, z;
155} vec3;
156
157static vec3 vec(double ra, double dec)
158{
159 double r = rad(ra), d = rad(dec);
160 return (vec3){cos(d) * cos(r), cos(d) * sin(r), sin(d)};
161}
162static double dot(vec3 a, vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
163static double sep_deg(vec3 a, vec3 b) { return deg(acos(clampd(dot(a, b), -1.0, 1.0))); }
164
165/* ---- the GW150914 sky map (HEALPix, nested order) ---- */
166
167static void healpix_nest_ang(int nside, long ipix, double *ra, double *dec)
168{
169 static const int jrll[12] = {2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
170 static const int jpll[12] = {1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};
171 long npface = (long)nside * nside;
172 int face = (int)(ipix / npface);
173 long ipf = ipix % npface;
174 long ix = 0, iy = 0;
175 for (int b = 0; b < 16; b++) {
176 ix |= ((ipf >> (2 * b)) & 1) << b;
177 iy |= (((ipf >> 1) >> (2 * b)) & 1) << b;
178 }
179 long nl4 = 4L * nside;
180 long jr = (long)jrll[face] * nside - ix - iy - 1;
181 long nr, kshift;
182 double z;
183 if (jr < nside) {
184 nr = jr;
185 z = 1 - (double)(jr * jr) / (3.0 * (double)npface);
186 kshift = 0;
187 } else if (jr > 3L * nside) {
188 nr = nl4 - jr;
189 z = (double)(nr * nr) / (3.0 * (double)npface) - 1;
190 kshift = 0;
191 } else {
192 nr = nside;
193 z = (double)(2L * nside - jr) * 2.0 / (3.0 * nside);
194 kshift = (jr - nside) & 1;
195 }
196 long t = (long)jpll[face] * nr + ix - iy + 1 + kshift;
197 long jp = t >= 0 ? t / 2 : -((-t + 1) / 2); /* floor division */
198 if (jp > nl4)
199 jp -= nl4;
200 if (jp < 1)
201 jp += nl4;
202 double phi = ((double)jp - (double)(kshift + 1) * 0.5) * (PI / 2 / (double)nr);
203 *ra = pmod(deg(phi), 360.0);
204 *dec = 90.0 - deg(acos(z));
205}
206
207static const double *g_prob;
208static int by_prob(const void *a, const void *b)
209{
210 long i = *(const long *)a, j = *(const long *)b;
211 if (g_prob[i] != g_prob[j])
212 return g_prob[i] > g_prob[j] ? -1 : 1;
213 return i < j ? -1 : i > j; /* ties by pixel number, like a stable sort */
214}
215
216typedef struct {
217 int nside;
218 double max_ra, max_dec, area90;
219 long n90;
220 double *region_ra, *region_dec;
221} gwmap;
222
223/* FITS: the header is a run of 2880-byte blocks of 80-character cards; the
224 * binary table's first column is PROB, big-endian doubles, NAXIS1 bytes a row. */
225static int fits_card_int(const char *blk, const char *key, long *out)
226{
227 for (int i = 0; i < 36; i++) {
228 const char *c = blk + i * 80;
229 size_t kl = strlen(key);
230 if (strncmp(c, key, kl) == 0 && (c[kl] == ' ' || kl == 8) && c[8] == '=') {
231 *out = strtol(c + 10, NULL, 10);
232 return 1;
233 }
234 }
235 return 0;
236}
237
238static int fits_has_end(const char *blk)
239{
240 for (int i = 0; i < 36; i++)
241 if (strncmp(blk + i * 80, "END ", 8) == 0)
242 return 1;
243 return 0;
244}
245
246static gwmap gw_load(const char *path)
247{
248 size_t len;
249 uint8_t *raw = slurp(path, &len);
250 size_t off = 0;
251 int hdr = 0;
252 long nside = 0, rows = 0, width = 0;
253 int nested = 1;
254 for (;;) {
255 if (off + 2880 > len)
256 die("%s: header runs off the end", path);
257 const char *blk = (const char *)raw + off;
258 off += 2880;
259 if (hdr == 1) {
260 fits_card_int(blk, "NSIDE", &nside);
261 fits_card_int(blk, "NAXIS1", &width);
262 fits_card_int(blk, "NAXIS2", &rows);
263 for (int i = 0; i < 36; i++) {
264 const char *c = blk + i * 80;
265 if (strncmp(c, "ORDERING", 8) == 0 && c[8] == '=')
266 nested = strstr(c, "NEST") != NULL;
267 }
268 }
269 if (fits_has_end(blk)) {
270 if (++hdr == 2)
271 break;
272 }
273 }
274 if (!nested)
275 die("%s: not NESTED ordering", path);
276 if (!nside || !rows || width < 8 || off + (size_t)rows * (size_t)width > len)
277 die("%s: bad table (nside %ld rows %ld width %ld)", path, nside, rows, width);
278 double *prob = xmalloc((size_t)rows * sizeof(double));
279 for (long i = 0; i < rows; i++) {
280 const uint8_t *p = raw + off + (size_t)i * (size_t)width;
281 uint64_t u = 0;
282 for (int k = 0; k < 8; k++)
283 u = (u << 8) | p[k];
284 memcpy(&prob[i], &u, 8);
285 }
286 long *order = xmalloc((size_t)rows * sizeof(long));
287 for (long i = 0; i < rows; i++)
288 order[i] = i;
289 g_prob = prob;
290 qsort(order, (size_t)rows, sizeof(long), by_prob);
291 double tot = 0;
292 for (long i = 0; i < rows; i++)
293 tot += prob[i];
294 double cum = 0;
295 long n90 = 0;
296 for (long k = 0; k < rows; k++) {
297 cum += prob[order[k]];
298 if (cum >= 0.9 * tot) {
299 n90 = k + 1;
300 break;
301 }
302 }
303 gwmap g;
304 g.nside = (int)nside;
305 healpix_nest_ang(g.nside, order[0], &g.max_ra, &g.max_dec);
306 double pixarea = 4 * PI / (12.0 * (double)nside * (double)nside) * (180 / PI) * (180 / PI);
307 g.area90 = (double)n90 * pixarea;
308 g.n90 = n90;
309 g.region_ra = xmalloc((size_t)n90 * sizeof(double));
310 g.region_dec = xmalloc((size_t)n90 * sizeof(double));
311 for (long k = 0; k < n90; k++)
312 healpix_nest_ang(g.nside, order[k], &g.region_ra[k], &g.region_dec[k]);
313 free(order);
314 free(prob);
315 free(raw);
316 return g;
317}
318
319/* ---- the view ---- */
320
321typedef struct {
322 double ra, dec, fov, roll;
323 vec3 rows[3]; /* right, up, forward */
324 double t; /* tangent-plane units across half the width */
325} view_t;
326
327static view_t view_make(double ra, double dec, double fov, double roll)
328{
329 view_t v;
330 v.ra = ra, v.dec = dec, v.fov = fov, v.roll = roll;
331 double a = rad(ra), d = rad(dec);
332 vec3 d0 = vec(ra, dec);
333 vec3 east = {-sin(a), cos(a), 0.0};
334 vec3 north = {-sin(d) * cos(a), -sin(d) * sin(a), cos(d)};
335 vec3 west = {-east.x, -east.y, -east.z};
336 double cr = cos(rad(roll)), sr = sin(rad(roll));
337 /* screen right and screen up; roll turns the view clockwise on screen */
338 v.rows[0] = (vec3){cr * west.x + sr * north.x, cr * west.y + sr * north.y, cr * west.z + sr * north.z};
339 v.rows[1] = (vec3){-sr * west.x + cr * north.x, -sr * west.y + cr * north.y, -sr * west.z + cr * north.z};
340 v.rows[2] = d0;
341 v.t = tan(rad(fov) / 2);
342 return v;
343}
344
345/* sky direction -> screen px (x right, y down); 0 if behind the camera */
346static int to_screen(const view_t *v, vec3 s, double w, double h, double *px, double *py)
347{
348 double x = dot(v->rows[0], s), y = dot(v->rows[1], s), z = dot(v->rows[2], s);
349 if (z <= 1e-6)
350 return 0;
351 double p = (w / 2) / v->t;
352 *px = w * CX_FRAC + p * x / z;
353 *py = h * CY_FRAC - p * y / z;
354 return 1;
355}
356
357/* screen offset from the view centre, in widths -> sky direction */
358static vec3 from_frac(const view_t *v, double fx, double fy)
359{
360 double u = fx * 2 * v->t, w = -fy * 2 * v->t;
361 vec3 d;
362 d.x = v->rows[2].x + u * v->rows[0].x + w * v->rows[1].x;
363 d.y = v->rows[2].y + u * v->rows[0].y + w * v->rows[1].y;
364 d.z = v->rows[2].z + u * v->rows[0].z + w * v->rows[1].z;
365 double n = sqrt(dot(d, d));
366 return (vec3){d.x / n, d.y / n, d.z / n};
367}
368
369/* ---- equatorial (J2000) to galactic (Hipparcos Vol. 1, sect. 1.5.3) ---- */
370
371static const vec3 GAL[3] = {{-0.0548755604, -0.8734370902, -0.4838350155},
372 {0.4941094279, -0.4448296300, 0.7469822445},
373 {-0.8676661490, -0.1980763734, 0.4559837762}};
374
375static void galactic(vec3 v, double *l, double *b)
376{
377 double g0 = dot(GAL[0], v), g1 = dot(GAL[1], v), g2 = dot(GAL[2], v);
378 *l = pmod(deg(atan2(g1, g0)), 360.0);
379 *b = deg(asin(clampd(g2, -1, 1)));
380}
381
382/* ---- colour: B-V -> temperature -> black body -> sRGB ---- */
383
384/* B-V -> effective temperature, K (Ballesteros 2012, EPL 97, 34008) */
385static double ballesteros(double bv) { return 4600.0 * (1.0 / (0.92 * bv + 1.7) + 1.0 / (0.92 * bv + 0.62)); }
386
387static double lobe(double x, double mu, double s1, double s2)
388{
389 double s = x < mu ? s1 : s2, t = (x - mu) / s;
390 return exp(-0.5 * t * t);
391}
392
393/* CIE 1931 2-degree observer, the multi-lobe fit of Wyman, Sloan and Shirley
394 * 2013 (JCGT 2(2)); lam in nm */
395static void cie1931(double lam, double *xb, double *yb, double *zb)
396{
397 *xb = 1.056 * lobe(lam, 599.8, 37.9, 31.0) + 0.362 * lobe(lam, 442.0, 16.0, 26.7) - 0.065 * lobe(lam, 501.1, 20.4, 26.2);
398 *yb = 0.821 * lobe(lam, 568.8, 46.9, 40.5) + 0.286 * lobe(lam, 530.9, 16.3, 31.1);
399 *zb = 1.217 * lobe(lam, 437.0, 11.8, 36.0) + 0.681 * lobe(lam, 459.0, 26.0, 13.8);
400}
401
402/* linear sRGB of a black body at T kelvin, brightest channel 1 */
403static void blackbody_rgb(double T, double out[3])
404{
405 double X = 0, Y = 0, Z = 0;
406 for (int lam = 380; lam <= 780; lam += 5) {
407 double l = lam * 1e-9;
408 double b = 1.0 / (pow(l, 5) * (exp(0.014387769 / (l * T)) - 1.0));
409 double xb, yb, zb;
410 cie1931(lam, &xb, &yb, &zb);
411 X += b * xb, Y += b * yb, Z += b * zb;
412 }
413 double r = 3.2404542 * X - 1.5371385 * Y - 0.4985314 * Z;
414 double g = -0.9692660 * X + 1.8760108 * Y + 0.0415560 * Z;
415 double bl = 0.0556434 * X - 0.2040259 * Y + 1.0572252 * Z;
416 r = r > 0 ? r : 0, g = g > 0 ? g : 0, bl = bl > 0 ? bl : 0;
417 double m = r > g ? r : g;
418 m = m > bl ? m : bl;
419 out[0] = r / m, out[1] = g / m, out[2] = bl / m;
420}
421
422static double srgb_decode(double v)
423{
424 v /= 255.0;
425 return v <= 0.04045 ? v / 12.92 : pow((v + 0.055) / 1.055, 2.4);
426}
427
428static double srgb_encode(double l)
429{
430 l = clampd(l, 0.0, 1.0);
431 double v = l <= 0.0031308 ? l * 12.92 : 1.055 * pow(l, 1 / 2.4) - 0.055;
432 return v * 255.0;
433}
434
435/* ---- the Yale Bright Star Catalogue ---- */
436
437typedef struct {
438 int hr;
439 char name[16];
440 double ra, dec, v, bv;
441 int has_bv;
442} star_t;
443
444static void field(const char *line, int a, int b, char *out)
445{
446 int n = b - a;
447 memcpy(out, line + a, (size_t)n);
448 out[n] = 0;
449}
450
451static int blank(const char *s)
452{
453 for (; *s; s++)
454 if (!isspace((unsigned char)*s))
455 return 0;
456 return 1;
457}
458
459static void trim(char *s)
460{
461 size_t n = strlen(s);
462 while (n && isspace((unsigned char)s[n - 1]))
463 s[--n] = 0;
464 char *p = s;
465 while (isspace((unsigned char)*p))
466 p++;
467 memmove(s, p, strlen(p) + 1);
468}
469
470static star_t *bsc5_load(const char *path, size_t *count, int *nobv)
471{
472 size_t len;
473 char *txt = (char *)slurp(path, &len);
474 size_t cap = 10000, n = 0;
475 star_t *st = xmalloc(cap * sizeof *st);
476 *nobv = 0;
477 char *p = txt;
478 while (*p) {
479 char *e = strchr(p, '\n');
480 size_t ll = e ? (size_t)(e - p) : strlen(p);
481 char line[256];
482 memset(line, ' ', sizeof line);
483 memcpy(line, p, ll < 197 ? ll : 197);
484 line[197] = 0;
485 p = e ? e + 1 : p + ll;
486 char f[32];
487 field(line, 75, 77, f);
488 int nopos = blank(f);
489 field(line, 102, 107, f);
490 if (nopos || blank(f))
491 continue; /* the 14 entries with no position (novae, clusters taken out) */
492 star_t s;
493 memset(&s, 0, sizeof s);
494 field(line, 0, 4, f);
495 s.hr = atoi(f);
496 double rah, ram, ras;
497 field(line, 75, 77, f), rah = atof(f);
498 field(line, 77, 79, f), ram = atof(f);
499 field(line, 79, 83, f), ras = atof(f);
500 double ra = (rah + ram / 60 + ras / 3600) * 15;
501 double dd, dm, ds;
502 field(line, 84, 86, f), dd = atof(f);
503 field(line, 86, 88, f), dm = atof(f);
504 field(line, 88, 90, f), ds = atof(f);
505 double dec = dd + dm / 60 + ds / 3600;
506 if (line[83] == '-')
507 dec = -dec;
508 field(line, 102, 107, f);
509 s.v = atof(f);
510 field(line, 109, 114, f);
511 if (!blank(f))
512 s.bv = atof(f), s.has_bv = 1;
513 else
514 (*nobv)++;
515 double pmra = 0, pmde = 0;
516 field(line, 148, 154, f);
517 if (!blank(f))
518 pmra = atof(f); /* arcsec/yr, along RA (times cos dec) */
519 field(line, 154, 160, f);
520 if (!blank(f))
521 pmde = atof(f);
522 double dt = EPOCH - 2000.0;
523 double dec2 = dec + pmde * dt / 3600;
524 double cd = cos(rad(dec));
525 double ra2 = ra + pmra * dt / 3600 / (cd > 1e-6 ? cd : 1e-6);
526 field(line, 4, 14, s.name);
527 trim(s.name);
528 s.ra = pmod(ra2, 360.0);
529 s.dec = dec2;
530 if (n == cap)
531 st = xrealloc(st, (cap *= 2) * sizeof *st);
532 st[n++] = s;
533 }
534 free(txt);
535 *count = n;
536 return st;
537}
538
539/* ---- OpenNGC ---- */
540
541typedef struct {
542 char name[32], common[160];
543 double ra, dec, vmag, bv, maj, min, pa;
544 int has_bv;
545} galaxy_t;
546
547/* splits one ';' separated line in place; fields may be "quoted" */
548static int csv_split(char *line, char **f, int max)
549{
550 int n = 0;
551 char *p = line;
552 for (;;) {
553 if (n == max)
554 return n;
555 if (*p == '"') {
556 char *w = ++p;
557 f[n++] = w;
558 for (;;) {
559 if (!*p)
560 return n;
561 if (*p == '"') {
562 if (p[1] == '"') {
563 *w++ = '"', p += 2;
564 continue;
565 }
566 p++;
567 break;
568 }
569 *w++ = *p++;
570 }
571 *w = 0;
572 if (*p == ';') {
573 p++;
574 continue;
575 }
576 return n;
577 }
578 f[n++] = p;
579 while (*p && *p != ';')
580 p++;
581 if (*p == ';') {
582 *p++ = 0;
583 continue;
584 }
585 return n;
586 }
587}
588
589static int col_of(char **h, int nh, const char *name)
590{
591 for (int i = 0; i < nh; i++)
592 if (strcmp(h[i], name) == 0)
593 return i;
594 die("OpenNGC: no column %s", name);
595}
596
597static double fnum(const char *s) { return *s ? atof(s) : 0.0; }
598
599static void openngc_load(const char *path, galaxy_t **out, size_t *n, size_t *cap)
600{
601 size_t len;
602 char *txt = (char *)slurp(path, &len);
603 char *p = txt;
604 char *hf[64], *f[64];
605 int nh = 0, cName = 0, cType = 0, cRA = 0, cDec = 0, cMaj = 0, cMin = 0, cPA = 0, cB = 0, cV = 0, cCommon = 0;
606 int first = 1;
607 while (*p) {
608 char *e = strchr(p, '\n');
609 char *line = p;
610 if (e)
611 *e = 0, p = e + 1;
612 else
613 p += strlen(p);
614 size_t ll = strlen(line);
615 if (ll && line[ll - 1] == '\r')
616 line[ll - 1] = 0;
617 if (first) {
618 nh = csv_split(line, hf, 64);
619 cName = col_of(hf, nh, "Name"), cType = col_of(hf, nh, "Type"), cRA = col_of(hf, nh, "RA");
620 cDec = col_of(hf, nh, "Dec"), cMaj = col_of(hf, nh, "MajAx"), cMin = col_of(hf, nh, "MinAx");
621 cPA = col_of(hf, nh, "PosAng"), cB = col_of(hf, nh, "B-Mag"), cV = col_of(hf, nh, "V-Mag");
622 cCommon = col_of(hf, nh, "Common names");
623 first = 0;
624 continue;
625 }
626 if (!*line)
627 continue;
628 int nf = csv_split(line, f, 64);
629 int need = cCommon > cV ? cCommon : cV;
630 if (nf <= need)
631 continue;
632 if (strcmp(f[cType], "G") != 0 || !*f[cRA])
633 continue;
634 const char *mag = *f[cV] ? f[cV] : f[cB];
635 if (!*mag)
636 continue;
637 galaxy_t g;
638 memset(&g, 0, sizeof g);
639 snprintf(g.name, sizeof g.name, "%s", f[cName]);
640 snprintf(g.common, sizeof g.common, "%s", f[cCommon]);
641 double h, m, s;
642 if (sscanf(f[cRA], "%lf:%lf:%lf", &h, &m, &s) != 3)
643 continue;
644 g.ra = (h + m / 60 + s / 3600) * 15;
645 int sd = f[cDec][0] == '-' ? -1 : 1;
646 double d, dm, ds;
647 if (sscanf(f[cDec], "%lf:%lf:%lf", &d, &dm, &ds) != 3)
648 continue;
649 g.dec = sd * (fabs(d) + fabs(dm) / 60 + fabs(ds) / 3600);
650 g.vmag = atof(mag);
651 if (*f[cB] && *f[cV])
652 g.bv = atof(f[cB]) - atof(f[cV]), g.has_bv = 1;
653 g.maj = fnum(f[cMaj]);
654 g.min = *f[cMin] ? atof(f[cMin]) : g.maj;
655 g.pa = fnum(f[cPA]);
656 if (*n == *cap)
657 *out = xrealloc(*out, (*cap *= 2) * sizeof **out);
658 (*out)[(*n)++] = g;
659 }
660 free(txt);
661}
662
663
664
665/* ---- the Milky Way from Gaia DR3: source counts and integrated G-band
666 * flux per HEALPix level-6 cell (nside=64, 49152 cells), reddened by
667 * G-band extinction from gaiadr3.astrophysical_parameters.
668 * ESA/Gaia/DPAC, CC BY-NC 3.0 IGO. Credit: ESA/Gaia/DPAC. */
669
670typedef struct {
671 long nsrc;
672 double g_flux, bp_flux, rp_flux;
673 double mean_ag; /* mean G-band extinction from GSP-Phot */
674 double v_s10; /* its light as V surface brightness, S10 (gaia_v_light) */
675 double ra, dec; /* cell centre, degrees */
676} gaia_cell_t;
677
678/* Parse a CSV with header line; column order: hpx6, nsrc, g_flux, bp_flux, rp_flux (or hpx6, mean_ag, n).
679 * Returns array of NCELLS (49152) cells indexed by hpx6; unvisited cells are zero. */
680static gaia_cell_t *parse_gaia_csv(const char *flux_path, const char *dust_path)
681{
682
683 gaia_cell_t *cells = xmalloc(NCELLS * sizeof *cells);
684 for (int i = 0; i < NCELLS; i++) cells[i] = (gaia_cell_t){0};
685
686 /* Flux CSV: hpx6, nsrc, g_flux, bp_flux, rp_flux */
687 size_t len; uint8_t *raw = slurp(flux_path, &len);
688 char *p = (char *)raw, *e = p + len;
689 /* skip header */
690 while (p < e && *p != '\n') p++;
691 if (p < e) p++;
692 while (p < e) {
693 long hpx = -1; long nsrc = 0; double gf = 0, bpf = 0, rpf = 0;
694 int r = sscanf(p, "%lf,%ld,%lf,%lf,%lf", (double*)&hpx, &nsrc, &gf, &bpf, &rpf);
695 (void)r;
696 hpx = (long)strtod(p, NULL); /* hpx6 may have .0 decimal */
697 if (hpx >= 0 && hpx < NCELLS) {
698 cells[hpx].nsrc = nsrc; cells[hpx].g_flux = gf;
699 cells[hpx].bp_flux = bpf; cells[hpx].rp_flux = rpf;
700 }
701 while (p < e && *p != '\n') p++;
702 if (p < e) p++;
703 }
704 free(raw);
705
706 /* Dust CSV: hpx6, mean_ag, n */
707 raw = slurp(dust_path, &len);
708 p = (char *)raw; e = p + len;
709 while (p < e && *p != '\n') p++;
710 if (p < e) p++; /* skip header */
711 while (p < e) {
712 long hpx = (long)strtod(p, NULL);
713 if (hpx >= 0 && hpx < NCELLS) {
714 char *q = p; while (q < e && *q != ',') q++; if (q < e) q++;
715 cells[hpx].mean_ag = strtod(q, NULL);
716 }
717 while (p < e && *p != '\n') p++;
718 if (p < e) p++;
719 }
720 free(raw);
721 return cells;
722}
723
724/* Build 360 x 180 equirectangular grid (1 deg/cell) from Gaia cells.
725 * Each grid point averages the cells within SPLAT_DEG of it: 1.5 deg for level 6 (cells ~0.9 deg apart),
726 * 0.75 deg for level 7 (~0.46 deg apart, about 8 cells per point, so no point is empty and the Small
727 * Magellanic Cloud, 5 x 3 deg, is not smeared over a 3 deg disk). Returns a flattened (rows*cols)*3 double array: [luma, co, cg]
728 * in the same YCbCr-like space as the old photograph, in 16ths-of-level units.
729 * flux_gain: linear display light per (g_flux / nsrc) unit. */
730#define SPLAT_DEG (HPX_LEVEL == 7 ? 0.75 : 1.5)
731static double *gaia_to_grid(const gaia_cell_t *cells)
732{
733#define GCOLS 360
734#define GROWS 180
735 double *grid = xmalloc((size_t)GROWS * (size_t)(GCOLS + 1) * 3 * sizeof *grid);
736 double *count = xmalloc((size_t)GROWS * (size_t)(GCOLS + 1) * sizeof *count);
737 for (size_t i = 0; i < (size_t)GROWS * (size_t)(GCOLS + 1) * 3; i++) grid[i] = 0;
738 for (size_t i = 0; i < (size_t)GROWS * (size_t)(GCOLS + 1); i++) count[i] = 0;
739
740 for (int k = 0; k < NCELLS; k++) {
741 const gaia_cell_t *c = &cells[k];
742 if (c->nsrc == 0) continue;
743 double ra = c->ra, dec = c->dec;
744 /* E(B-V) ~ A_G / 2.285 (Cardelli 1989); applied as reddening of the colour */
745 double ebv = c->mean_ag / 2.285;
746 /* the cell's light: V surface brightness in S10 (gaia_v_light), physical; emit() scales by a->g_phys */
747 double luma_lin = c->v_s10;
748 /* Color from BP/RP ratio (log scale). Higher = redder (dust or cool stars).
749 * Map to the chrominance offsets used by the photograph approach (linear, 0..1 range). */
750 double bprp = (c->bp_flux > 1.0) ? log(c->rp_flux / c->bp_flux) : 0.5;
751 bprp += ebv * 0.3; /* add dust reddening */
752 /* Milky Way colour: warm orange-yellow in the galactic plane, cooler at the poles.
753 * co: orange-red offset; cg: green offset (in the same linear units as luma_lin). */
754 /* Colour at fixed luminance: the three channel weights have Y = 0.2126 R + 0.7152 G + 0.0722 B
755 * equal to 1, so the cell's luminance (its light) is exactly luma_lin and the colour only
756 * moves the light between the channels. An orange tint from BP-RP (t), a small green one. */
757 double t = 0.2 * (bprp - 0.3);
758 double wR = 1.0 + t, wB = 1.0 - t, wG = 1.0 + 0.08 * (1.0 - fabs(bprp - 0.8));
759 double Yw = 0.2126 * wR + 0.7152 * wG + 0.0722 * wB;
760 double r_lin = luma_lin * wR / Yw, g_lin = luma_lin * wG / Yw, b_lin = luma_lin * wB / Yw;
761
762 /* Splat onto the grid points within SPLAT_DEG (row j is centred at Dec 89.5 - j) */
763 const double R = SPLAT_DEG;
764 int j0 = (int)floor(89.5 - dec - R), j1 = (int)ceil(89.5 - dec + R);
765 j0 = j0 < 0 ? 0 : j0 >= GROWS ? GROWS - 1 : j0;
766 j1 = j1 < 0 ? 0 : j1 >= GROWS ? GROWS - 1 : j1;
767 for (int j = j0; j <= j1; j++) {
768 double dec_j = 90.0 - j - 0.5;
769 double ddec = dec_j - dec;
770 if (ddec * ddec > R * R) continue;
771 double cos_dec = cos(rad(dec_j));
772 double dra_max = cos_dec > 0.01 ? R / cos_dec : 360.0;
773 int i0 = (int)floor(ra - dra_max - 0.5), i1 = (int)ceil(ra + dra_max - 0.5);
774 if (dra_max >= 180) i0 = 0, i1 = GCOLS - 1; /* near a pole: every column once */
775 for (int ii = i0; ii <= i1; ii++) {
776 int i = ((ii % GCOLS) + GCOLS) % GCOLS;
777 double ra_i = i + 0.5;
778 double dra = ra_i - ra;
779 if (dra > 180) dra -= 360;
780 if (dra < -180) dra += 360;
781 double dra_deg = dra * cos_dec;
782 if (dra_deg * dra_deg + ddec * ddec > R * R) continue;
783 size_t off = (size_t)j * (size_t)(GCOLS + 1) + (size_t)i;
784 grid[off * 3] += r_lin;
785 grid[off * 3 + 1] += g_lin;
786 grid[off * 3 + 2] += b_lin;
787 count[off]++;
788 }
789 }
790 }
791
792 /* Average and add glow floor */
793 /* The glow floor (dark sky brightness) is handled by the emit() function
794 * which adds glow[k] from the blackbody colour. Our grid stores SIGNAL only.
795 * Average the accumulated values. */
796 for (int j = 0; j < GROWS; j++) {
797 for (int i = 0; i <= GCOLS; i++) {
798 size_t off = (size_t)j * (size_t)(GCOLS + 1) + (size_t)i;
799 if (count[off] > 0) {
800 grid[off * 3] /= count[off];
801 grid[off * 3 + 1] /= count[off];
802 grid[off * 3 + 2] /= count[off];
803 }
804 /* no separate glow: emit() adds the dark-sky blackbody glow */
805
806 }
807 }
808
809 /* Copy col 0 to col GCOLS for wraparound bilinear */
810 for (int j = 0; j < GROWS; j++) {
811 size_t src = (size_t)j * (size_t)(GCOLS + 1);
812 size_t dst = src + GCOLS;
813 grid[dst * 3] = grid[src * 3];
814 grid[dst * 3 + 1] = grid[src * 3 + 1];
815 grid[dst * 3 + 2] = grid[src * 3 + 2];
816 }
817
818 free(count);
819 return grid;
820}
821
822/* Each cell's light as V surface brightness in S10. The cell's G flux F (e-/s) over its area A (arcsec^2) is
823 * G = ZP_G - 2.5 log10(F / A) per arcsec^2; 1 S10 is V 27.78 mag/arcsec^2. G is moved to Johnson V with the
824 * cell's own integrated colour c = BP - RP (from its summed BP and RP fluxes and the DR3 zero points):
825 * G - V = -0.02704 + 0.01424 c - 0.2156 c^2 + 0.01426 c^3, valid -0.5 < c < 5, scatter 0.03 mag (Gaia DR3
826 * documentation, chapter 5, "relationships with other photometric systems", Table 5.9). At c = 2 (the
827 * reddened bulge) G - V is -0.75: V is that much fainter than G. Returns the number of cells with no BP/RP flux (those keep G as V). */
828static long gaia_v_light(gaia_cell_t *cells, double cell_arcsec2)
829{
830 long nocol = 0;
831 double g_s10 = pow(10, 0.4 * (27.78 - GAIA_ZP_G)) / cell_arcsec2;
832 for (int k = 0; k < NCELLS; k++) {
833 gaia_cell_t *c = &cells[k];
834 double gv = 0;
835 if (c->bp_flux > 0 && c->rp_flux > 0) {
836 double x = clampd(-2.5 * log10(c->bp_flux / c->rp_flux) + GAIA_ZP_BP - GAIA_ZP_RP, -0.5, 5.0);
837 gv = -0.02704 + x * (0.01424 + x * (-0.2156 + x * 0.01426));
838 } else if (c->nsrc > 0) {
839 nocol++;
840 }
841 c->v_s10 = c->g_flux * g_s10 * pow(10, 0.4 * gv);
842 }
843 return nocol;
844}
845
846static int cmpd(const void *a, const void *b)
847{
848 double x = *(const double *)a, y = *(const double *)b;
849 return x < y ? -1 : x > y;
850}
851
852/* The Magellanic Clouds: Gaia resolves their brightest stars (the LMC is 50 kpc away, distance modulus 18.5,
853 * so Gaia's G 20.7 limit reaches absolute G +2) but not the faint, crowded rest of their light. For each
854 * catalogue galaxy at least CLOUD_MIN_ARCMIN long (the grid's 3 degree splat sees its shape), the Gaia light
855 * inside its catalogue ellipse (OpenNGC major and minor axes and position angle, the D25 isophote) above the
856 * local sky (the median cell between 1.2 and 1.6 ellipse radii: Milky Way foreground and background) is the
857 * resolved part. The catalogue's total V says how much light the galaxy has; the missing part is added in the
858 * pattern of the resolved stars themselves (each cell's excess times total / resolved), so the total inside the
859 * ellipse is the catalogue's and the shape is Gaia's. Only where Gaia resolves at least CLOUD_MIN_SHARE of the
860 * light: a farther galaxy (M31) shows Gaia only a few supergiants, too few to carry its shape. */
861#define CLOUD_MIN_ARCMIN 120.0
862#define CLOUD_MIN_SHARE 0.25
863static void gaia_clouds(gaia_cell_t *cells, const galaxy_t *gx, size_t ngx, double cell_deg2)
864{
865 double *rho2 = xmalloc(NCELLS * sizeof *rho2), *ann = xmalloc(NCELLS * sizeof *ann);
866 for (size_t gi = 0; gi < ngx; gi++) {
867 const galaxy_t *g = &gx[gi];
868 if (g->maj < CLOUD_MIN_ARCMIN || g->min <= 0 || g->vmag > 20)
869 continue;
870 vec3 gc = vec(g->ra, g->dec);
871 double a_ = rad(g->ra), d_ = rad(g->dec), cp = cos(rad(g->pa)), sp = sin(rad(g->pa));
872 vec3 east = {-sin(a_), cos(a_), 0.0};
873 vec3 north = {-sin(d_) * cos(a_), -sin(d_) * sin(a_), cos(d_)};
874 double ha = g->maj / 120.0, hb = g->min / 120.0; /* half axes, degrees */
875 size_t nann = 0, nin = 0, nctr = 0;
876 double ctr = 0;
877 for (int k = 0; k < NCELLS; k++) {
878 vec3 c = vec(cells[k].ra, cells[k].dec);
879 double cg = dot(c, gc);
880 rho2[k] = 1e9;
881 if (cg < 0.5)
882 continue;
883 double x = deg(dot(c, east) / cg), y = deg(dot(c, north) / cg); /* tangent plane, degrees */
884 double u = x * sp + y * cp, w = x * cp - y * sp; /* along the long and short axes */
885 rho2[k] = (u / ha) * (u / ha) + (w / hb) * (w / hb);
886 if (rho2[k] >= 1.44 && rho2[k] <= 2.56)
887 ann[nann++] = cells[k].v_s10;
888 }
889 if (nann < 8)
890 continue;
891 qsort(ann, nann, sizeof *ann, cmpd);
892 double bg = ann[nann / 2], ex = 0;
893 for (int k = 0; k < NCELLS; k++)
894 if (rho2[k] <= 1.0) {
895 nin++;
896 ex += fmax(0, cells[k].v_s10 - bg);
897 if (rho2[k] <= 0.04)
898 ctr += cells[k].v_s10, nctr++;
899 }
900 double f_gaia = ex * cell_deg2, f_cat = pow(10, 0.4 * (10 - g->vmag)); /* light in V 10 stars */
901 double share = f_cat > 0 ? f_gaia / f_cat : 0;
902 double v_gaia = f_gaia > 0 ? 10 - 2.5 * log10(f_gaia) : 99;
903 char common[160];
904 snprintf(common, sizeof common, "%s", g->common);
905 char *cm = strchr(common, ',');
906 if (cm)
907 *cm = 0;
908 if (share < CLOUD_MIN_SHARE) {
909 report("cloud light %s %s: Gaia resolves V %.2f of the catalogue's V %.2f (%.1f%%), under %.0f%%: not added",
910 g->name, common, v_gaia, g->vmag, 100 * share, 100 * CLOUD_MIN_SHARE);
911 continue;
912 }
913 double k_ = share < 1 ? 1 / share : 1, ctr_after = 0;
914 for (int k = 0; k < NCELLS; k++)
915 if (rho2[k] <= 1.0) {
916 cells[k].v_s10 += (k_ - 1) * fmax(0, cells[k].v_s10 - bg);
917 if (rho2[k] <= 0.04)
918 ctr_after += cells[k].v_s10;
919 }
920 ctr = nctr ? ctr / (double)nctr : 0, ctr_after = nctr ? ctr_after / (double)nctr : 0;
921 report("cloud light %s %s: %zu Gaia cells in the %.0fx%.0f arcmin ellipse, local sky %.1f S10 (median of %zu "
922 "cells at 1.2-1.6 radii); Gaia resolves V %.2f of the catalogue's V %.2f (%.0f%%); the rest added in the "
923 "Gaia stars' pattern (excess x %.2f); centre (inner fifth) V %.2f -> %.2f mag/arcsec2, %.0f -> %.0f S10",
924 g->name, common, nin, g->maj, g->min, bg, nann, v_gaia, g->vmag, 100 * share, k_,
925 27.78 - 2.5 * log10(ctr), 27.78 - 2.5 * log10(ctr_after), ctr, ctr_after);
926 }
927 free(rho2);
928 free(ann);
929}
930
931/* ---- the run ---- */
932
933typedef struct {
934 const char *cache, *out, *report, *inputs;
935 double ra, dec, fov, roll, mag, gain, skyglow, exposure;
936 int have_ra, have_dec, grid;
937 double glow_lin, g_phys;
938} opts_t;
939
940typedef struct {
941 star_t *s;
942 vec3 v;
943} sel_t;
944
945static int by_v(const void *a, const void *b)
946{
947 const sel_t *x = a, *y = b;
948 if (x->s->v != y->s->v)
949 return x->s->v < y->s->v ? -1 : 1;
950 return x->s < y->s ? -1 : x->s > y->s;
951}
952
953typedef struct {
954 galaxy_t *g;
955 vec3 v;
956} gsel_t;
957
958static int by_vmag(const void *a, const void *b)
959{
960 const gsel_t *x = a, *y = b;
961 if (x->g->vmag != y->g->vmag)
962 return x->g->vmag < y->g->vmag ? -1 : 1;
963 return x->g < y->g ? -1 : x->g > y->g;
964}
965
966static const double W = 3840, H = 2160;
967
968static int on_169(const view_t *view, vec3 v)
969{
970 double x, y;
971 return to_screen(view, v, W, H, &x, &y) && 0 <= x && x < W && 0 <= y && y < H;
972}
973
974static char *path_join(const char *dir, const char *name)
975{
976 size_t n = strlen(dir) + strlen(name) + 2;
977 char *s = xmalloc(n);
978 snprintf(s, n, "%s/%s", dir, name);
979 return s;
980}
981
982static void emit(const opts_t *a, const view_t *view, const sel_t *sel, size_t nsel, double pal[128][3], const double *grid,
983 int gw_cols, int gw_rows, double gy0, double step, double floor_, const gsel_t *gals, size_t ngal, double ra,
984 double dec);
985
986int main(int argc, char **argv)
987{
988 opts_t a = {0};
989 a.fov = 80.0, a.mag = 6.5, a.grid = 240, a.gain = 1.0, a.skyglow = 22.0, a.exposure = 0.06;
990 for (int i = 1; i < argc; i++) {
991 const char *k = argv[i];
992 const char *v = i + 1 < argc ? argv[i + 1] : NULL;
993 if (!v)
994 die("%s needs a value", k);
995 i++;
996 if (!strcmp(k, "--cache")) a.cache = v;
997 else if (!strcmp(k, "--out")) a.out = v;
998 else if (!strcmp(k, "--report")) a.report = v;
999 else if (!strcmp(k, "--inputs")) a.inputs = v;
1000 else if (!strcmp(k, "--ra")) a.ra = atof(v), a.have_ra = 1;
1001 else if (!strcmp(k, "--dec")) a.dec = atof(v), a.have_dec = 1;
1002 else if (!strcmp(k, "--fov")) a.fov = atof(v);
1003 else if (!strcmp(k, "--roll")) a.roll = atof(v);
1004 else if (!strcmp(k, "--mag")) a.mag = atof(v);
1005 else if (!strcmp(k, "--grid")) a.grid = atoi(v);
1006 else if (!strcmp(k, "--gain")) a.gain = atof(v);
1007 else if (!strcmp(k, "--skyglow")) a.skyglow = atof(v);
1008 else if (!strcmp(k, "--exposure")) a.exposure = atof(v);
1009 else die("unknown option %s", k);
1010 }
1011 if (!a.cache || !a.out)
1012 die("--cache and --out are needed");
1013 time_t t0 = time(NULL);
1014
1015 if (a.inputs) { /* "input name: url sha256 ..." lines made by scripts/ck_sky_gen.sh */
1016 size_t len;
1017 char *t = (char *)slurp(a.inputs, &len);
1018 char *p = t;
1019 while (*p) {
1020 char *e = strchr(p, '\n');
1021 if (e)
1022 *e = 0;
1023 if (*p)
1024 report("%s", p);
1025 p = e ? e + 1 : p + strlen(p);
1026 }
1027 free(t);
1028 }
1029
1030 gwmap gw = gw_load(path_join(a.cache, "GW150914_skymap.fits"));
1031 double ra = a.have_ra ? a.ra : rnd(gw.max_ra * 1000) / 1000.0;
1032 double dec = a.have_dec ? a.dec : rnd(gw.max_dec * 1000) / 1000.0;
1033 report("GW150914 sky map: HEALPix nside %d; most probable point RA %.3f Dec %.3f deg; 90%% region %.1f sq deg", gw.nside,
1034 gw.max_ra, gw.max_dec, gw.area90);
1035 view_t view = view_make(ra, dec, a.fov, a.roll);
1036 report("view: gnomonic, centre RA %.3f Dec %.3f deg, %.1f deg across the width, roll %.1f deg, "
1037 "centre at %.0f%% across and %.0f%% down",
1038 ra, dec, a.fov, a.roll, CX_FRAC * 100, CY_FRAC * 100);
1039 vec3 tl = from_frac(&view, -0.5, -CY_FRAC * H / W), br = from_frac(&view, 0.5, (1 - CY_FRAC) * H / W);
1040 report("3840x2160: the top-left corner is %.1f deg from the centre, the bottom-right %.1f deg", sep_deg(tl, view.rows[2]),
1041 sep_deg(br, view.rows[2]));
1042 double lc, bc;
1043 galactic(view.rows[2], &lc, &bc);
1044 report("the centre in galactic coordinates: l %.2f b %.2f deg", lc, bc);
1045 long inside = 0;
1046 for (long i = 0; i < gw.n90; i++) {
1047 double x, y;
1048 if (to_screen(&view, vec(gw.region_ra[i], gw.region_dec[i]), W, H, &x, &y) && 0 <= x && x < W && 0 <= y && y < H)
1049 inside++;
1050 }
1051 report("of the GW150914 90%% region, %.0f%% of its pixels are on the 3840x2160 screen", 100.0 * (double)inside / (double)gw.n90);
1052
1053 /* ---- stars ---- */
1054 size_t nstars;
1055 int nobv;
1056 star_t *stars = bsc5_load(path_join(a.cache, "bsc5.dat"), &nstars, &nobv);
1057 report("catalogue: %zu stars with positions; %d have no B-V (drawn at B-V 0.6, close to the Sun's)", nstars, nobv);
1058 const double margin = 64, hmax = W * H_OVER_W_MAX;
1059 sel_t *sel = xmalloc(nstars * sizeof *sel);
1060 size_t nsel = 0, allsky = 0;
1061 static const double lims[7] = {4.0, 5.0, 5.5, 6.0, 6.5, 7.0, 99};
1062 long counts[7] = {0};
1063 for (size_t i = 0; i < nstars; i++) {
1064 vec3 v = vec(stars[i].ra, stars[i].dec);
1065 int s169 = on_169(&view, v);
1066 for (int k = 0; k < 7; k++)
1067 if (stars[i].v <= lims[k] && s169)
1068 counts[k]++;
1069 if (stars[i].v <= a.mag) {
1070 allsky++;
1071 double x, y;
1072 if (to_screen(&view, v, W, hmax, &x, &y) && -margin <= x && x < W + margin && -margin <= y && y < hmax + margin)
1073 sel[nsel++] = (sel_t){&stars[i], v};
1074 }
1075 }
1076 {
1077 char buf[512];
1078 int n = 0;
1079 buf[0] = 0;
1080 for (int k = 0; k < 7; k++) {
1081 if (!counts[k])
1082 continue;
1083 char lim[16];
1084 if (k == 6)
1085 snprintf(lim, sizeof lim, "all");
1086 else
1087 snprintf(lim, sizeof lim, "%.1f", lims[k]);
1088 int w = snprintf(buf + n, sizeof buf - (size_t)n, "%sV<=%s: %ld", n ? ", " : "", lim, counts[k]);
1089 if (w < 0 || (size_t)w >= sizeof buf - (size_t)n) {
1090 memcpy(buf + sizeof buf - 4, "...", 4);
1091 break;
1092 }
1093 n += w;
1094 }
1095 report("stars on the 3840x2160 screen by limit: %s", buf);
1096 }
1097 report("stars to V %.1f in the whole sky: %zu (8 bytes each: %zu bytes); in this view with its margin: %zu", a.mag, allsky,
1098 allsky * 8, nsel);
1099 qsort(sel, nsel, sizeof *sel, by_v);
1100
1101 /* colour palette: B-V from -0.40 to 2.14 in steps of 0.02 */
1102 static double pal[128][3];
1103 for (int i = 0; i < 128; i++)
1104 blackbody_rgb(ballesteros(-0.40 + 0.02 * i), pal[i]);
1105 static const double bvs[4] = {-0.3, 0.0, 0.65, 1.5};
1106 for (int i = 0; i < 4; i++) {
1107 double rgb[3];
1108 double T = ballesteros(bvs[i]);
1109 blackbody_rgb(T, rgb);
1110 report("colour: B-V %.2f -> %.0f K -> sRGB (%.0f, %.0f, %.0f)", bvs[i], T, srgb_encode(rgb[0]), srgb_encode(rgb[1]),
1111 srgb_encode(rgb[2]));
1112 }
1113
1114 /* ---- the Milky Way from Gaia DR3 ---- */
1115 /* CSVs downloaded by scripts/ck_sky_gen.sh and passed via --cache */
1116 double r0 = 1.2; /* design px: the core of a star */
1117 double star0 = a.exposure * (PI * r0 * r0 / 4) / 0.8 * pow(10, 0.4 * 6.5);
1118 double px_deg2 = (a.fov / 1920.0) * (a.fov / 1920.0);
1119 double s10 = 1e-4 * star0 * px_deg2; /* linear light per design px of 1 S10 */
1120 /* The dark sky's own glow is physical: no gain on it. The gain (--gain) is on the Milky Way only. */
1121 a.glow_lin = pow(10, 0.4 * (27.78 - a.skyglow)) * s10;
1122 report("display: a magnitude 6.5 star peaks at %.3f linear (sRGB level %.0f); "
1123 "a magnitude 0 star holds %.1f linear x design px; 1 S10 is %.3g linear per design px",
1124 a.exposure, srgb_encode(a.exposure), star0, s10);
1125 gaia_cell_t *cells = parse_gaia_csv(path_join(a.cache, HPX_LEVEL == 7 ? "gaia_hpx7.csv" : "gaia_hpx6.csv"),
1126 path_join(a.cache, HPX_LEVEL == 7 ? "gaia_dust7.csv" : "gaia_dust.csv"));
1127 /* Compute direction (ra, dec) of each HEALPix nested cell (nside=64) */
1128 for (int k = 0; k < NCELLS; k++)
1129 healpix_nest_ang(NSIDE, k, &cells[k].ra, &cells[k].dec);
1130 /* The physical scale, with no anchor: each cell's light is its own Gaia flux, in V (gaia_v_light). */
1131 double cell_arcsec2 = (4 * PI / (12.0 * NSIDE * NSIDE)) * (180.0 * 3600.0 / PI) * (180.0 * 3600.0 / PI);
1132 long nocol = gaia_v_light(cells, cell_arcsec2);
1133 long nnonempty = 0;
1134 for (int k = 0; k < NCELLS; k++) if (cells[k].nsrc > 0) nnonempty++;
1135 report("Gaia DR3 Milky Way: %d HEALPix level-%d cells (nside %d), %ld non-empty, %ld with no BP/RP colour (G kept as V); "
1136 "sky glow %.5f linear/design-px (V %.1f, physical, no gain)",
1137 NCELLS, HPX_LEVEL, NSIDE, nnonempty, nocol, a.glow_lin, a.skyglow);
1138 /* the catalogue galaxies (OpenNGC): the Clouds' light here, the galaxies in the view below */
1139 size_t gcap = 4096, ngx = 0;
1140 galaxy_t *gx = xmalloc(gcap * sizeof *gx);
1141 openngc_load(path_join(a.cache, "NGC.csv"), &gx, &ngx, &gcap);
1142 openngc_load(path_join(a.cache, "addendum.csv"), &gx, &ngx, &gcap);
1143 gaia_clouds(cells, gx, ngx, cell_arcsec2 / (3600.0 * 3600.0));
1144 double *grid = gaia_to_grid(cells);
1145 free(cells);
1146 int gw_cols = GCOLS, gw_rows = GROWS;
1147 double gy0 = 0.0, step = 1.0 / GCOLS;
1148 double floor_ = 0.0; /* the glow is handled in emit() */
1149 double top = 0;
1150 for (int j = 0; j < gw_rows; j++)
1151 for (int i = 0; i <= gw_cols; i++) {
1152 double v = grid[((size_t)j * (size_t)(gw_cols + 1) + (size_t)i) * 3];
1153 if (v > top) top = v;
1154 }
1155 /* The grid is in S10 already (gaia_to_grid), so the light is physical: g_phys is one S10 of linear light
1156 * per design pixel. The gain (--gain, 1.0) is only for a deliberate test; the physical value is 1.0. */
1157 if (top <= 0)
1158 die("the Gaia Milky Way grid is empty");
1159 a.g_phys = s10;
1160 report("Milky Way: brightest band %.1f S10 = V %.2f mag/arcsec2 (Gaia flux in V, physical scale, no anchor; "
1161 "the published Bochum value is V 20.7, Leinert et al. 1998); %.2f times the dark sky; gain %.2f",
1162 top, 27.78 - 2.5 * log10(top), top / (pow(10, 0.4 * (27.78 - a.skyglow))), a.gain);
1163
1164 /* galaxies in the view, bright enough for the eye */
1165 gsel_t *gals = xmalloc((ngx + 1) * sizeof *gals);
1166 size_t ngal = 0;
1167 for (size_t i = 0; i < ngx; i++) {
1168 vec3 v = vec(gx[i].ra, gx[i].dec);
1169 if (gx[i].vmag > a.mag || !on_169(&view, v))
1170 continue;
1171 gals[ngal++] = (gsel_t){&gx[i], v};
1172 }
1173 qsort(gals, ngal, sizeof *gals, by_vmag);
1174 {
1175 char buf[2048];
1176 int n = 0;
1177 buf[0] = 0;
1178 for (size_t i = 0; i < ngal; i++) {
1179 char common[160];
1180 snprintf(common, sizeof common, "%s", gals[i].g->common);
1181 char *c = strchr(common, ',');
1182 if (c)
1183 *c = 0;
1184 int w = snprintf(buf + n, sizeof buf - (size_t)n, "%s%s %s V %.2f %.0fx%.0f arcmin PA %.0f", n ? ", " : "",
1185 gals[i].g->name, common, gals[i].g->vmag, gals[i].g->maj, gals[i].g->min, gals[i].g->pa);
1186 if (w < 0 || (size_t)w >= sizeof buf - (size_t)n) {
1187 memcpy(buf + sizeof buf - 4, "...", 4);
1188 break;
1189 }
1190 n += w;
1191 }
1192 report("galaxies on screen brighter than V %.1f (OpenNGC): %s", a.mag, ngal ? buf : "none");
1193 }
1194
1195 emit(&a, &view, sel, nsel, pal, grid, gw_cols, gw_rows, gy0, step, floor_, gals, ngal, ra, dec);
1196 report("made in %ld s", (long)(time(NULL) - t0));
1197 fputs(rep, stdout);
1198 if (a.report) {
1199 int f_fd = open(a.report, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
1200 FILE *f = f_fd < 0 ? NULL : fdopen(f_fd, "w");
1201 if (!f)
1202 die("cannot write %s", a.report);
1203 fputs(rep, f);
1204 fclose(f);
1205 }
1206 return 0;
1207}
1208
1209static void emit(const opts_t *a, const view_t *view, const sel_t *sel, size_t nsel, double pal[128][3], const double *grid,
1210 int gw_cols, int gw_rows, double gy0, double step, double floor_, const gsel_t *gals, size_t ngal, double ra,
1211 double dec)
1212{
1213 const double Q30 = (double)(1 << 30);
1214 int o_fd = open(a->out, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
1215 FILE *o = o_fd < 0 ? NULL : fdopen(o_fd, "w");
1216 if (!o)
1217 die("cannot write %s", a->out);
1218 fprintf(o, "/* sky_data.c -- GENERATED by native/kernel/tools/ck_sky_gen.c; do not edit.\n");
1219 fprintf(o, " * Regenerate: scripts/ck_sky_gen.sh (downloads the inputs, checks their sha256).\n *\n");
1220 fprintf(o, " * Stars: Yale Bright Star Catalogue, 5th revised ed. (Hoffleit & Warren 1991),\n");
1221 fprintf(o, " * CDS catalogue V/50, positions moved by proper motion to %.1f.\n", EPOCH);
1222 fprintf(o, " * Milky Way: Gaia DR3 source counts and integrated G-band flux per HEALPix level-%d cell\n", HPX_LEVEL);
1223 fprintf(o, " * (nside=%d); G-band extinction from gaiadr3.astrophysical_parameters.\n", NSIDE);
1224 fprintf(o, " * ESA/Gaia/DPAC, CC BY-NC 3.0 IGO. Credit: ESA/Gaia/DPAC.\n");
1225 fprintf(o, " * Galaxies: OpenNGC (Mattia Verga), CC BY-SA 4.0.\n");
1226 fprintf(o, " * View centre: the most probable point of the GW150914 sky map, GWTC-1,\n");
1227 fprintf(o, " * LIGO/Virgo (LIGO-P1800381-v7).\n");
1228 fprintf(o, " * Settings: centre RA %.3f Dec %.3f deg, %.1f deg across, roll %.1f deg, faintest V %.1f,\n", ra, dec, a->fov,
1229 a->roll, a->mag);
1230 fprintf(o, " * grid %d, exposure %.4f, sky glow %.1f mag/arcsec2, Milky Way and Clouds gain %.2f (1.00 = physical). */\n", gw_cols, a->exposure,
1231 a->skyglow, a->gain);
1232 fprintf(o, "#include \"sky_data.h\"\n\n");
1233 fprintf(o, "const ck_sky_view ck_sky_the_view = {\n");
1234 fprintf(o, " .ra_mdeg = %ld, .dec_mdeg = %ld, .fov_mdeg = %ld, .roll_mdeg = %ld,\n", rnd(ra * 1000), rnd(dec * 1000),
1235 rnd(a->fov * 1000), rnd(a->roll * 1000));
1236 fprintf(o, " .tan_half_q30 = %ld,\n", rnd(view->t * Q30));
1237 fprintf(o, " .rot_q30 = {\n");
1238 for (int r = 0; r < 3; r++)
1239 fprintf(o, " {%ld, %ld, %ld},\n", rnd(view->rows[r].x * Q30), rnd(view->rows[r].y * Q30), rnd(view->rows[r].z * Q30));
1240 fprintf(o, " },\n};\n\n");
1241
1242 /* stars */
1243 fprintf(o, "/* x, y, z: the direction, Q15 (J2000 axes); mag: (V + 2) * 20; bv: palette index */\n");
1244 fprintf(o, "const ck_sky_star ck_sky_stars[%zu] = {\n", nsel);
1245 for (size_t i = 0; i < nsel; i++) {
1246 const star_t *s = sel[i].s;
1247 double bv = s->has_bv ? s->bv : 0.6;
1248 long bvi = rnd((bv + 0.40) / 0.02);
1249 bvi = bvi < 0 ? 0 : bvi > 127 ? 127 : bvi;
1250 long mc = rnd((s->v + 2.0) * 20);
1251 mc = mc < 0 ? 0 : mc > 255 ? 255 : mc;
1252 char bvs[16];
1253 if (s->has_bv)
1254 snprintf(bvs, sizeof bvs, "%.2f", s->bv);
1255 else
1256 snprintf(bvs, sizeof bvs, "none");
1257 fprintf(o, " {%ld, %ld, %ld, %ld, %ld}, /* HR %d %s V %.2f B-V %s */\n", rnd(sel[i].v.x * 32767), rnd(sel[i].v.y * 32767),
1258 rnd(sel[i].v.z * 32767), mc, bvi, s->hr, s->name, s->v, bvs);
1259 }
1260 fprintf(o, "};\nconst unsigned ck_sky_nstars = %zu;\n\n", nsel);
1261
1262 /* brightness of a star at each magnitude code: linear light at the centre of its core at the reference scale
1263 * (k = 256, 1920 px wide), Q16 */
1264 fprintf(o, "/* light at the middle of a star of magnitude code m, linear, Q16: exposure x 10^(-0.4 (V - 6.5)) */\n");
1265 fprintf(o, "const uint32_t ck_sky_flux[256] = {\n");
1266 for (int i = 0; i < 256; i += 8) {
1267 fprintf(o, " ");
1268 for (int k = 0; k < 8; k++) {
1269 double V = (i + k) / 20.0 - 2.0;
1270 long v = rnd(a->exposure * pow(10, -0.4 * (V - 6.5)) * 65536);
1271 if (v > 0x7fffffff)
1272 v = 0x7fffffff;
1273 fprintf(o, "%ld%s", v, k < 7 ? ", " : ",\n");
1274 }
1275 }
1276 fprintf(o, "};\n\n");
1277 fprintf(o, "/* the colour of a black body at the temperature of B-V = -0.40 + 0.02 i (Ballesteros 2012),\n");
1278 fprintf(o, " * linear sRGB, the brightest channel 4096 */\n");
1279 fprintf(o, "const uint16_t ck_sky_palette[128][3] = {\n");
1280 for (int i = 0; i < 128; i++)
1281 fprintf(o, " {%ld, %ld, %ld}, /* B-V %.2f, %.0f K */\n", rnd(pal[i][0] * 4096), rnd(pal[i][1] * 4096), rnd(pal[i][2] * 4096),
1282 (2 * i - 40) / 100.0, ballesteros(-0.40 + 0.02 * i));
1283 fprintf(o, "};\n\n");
1284
1285 /* sRGB tables */
1286 fprintf(o, "/* sRGB level -> linear light, Q15 */\n");
1287 fprintf(o, "const uint16_t ck_sky_lin[256] = {\n");
1288 for (int i = 0; i < 256; i += 16) {
1289 fprintf(o, " ");
1290 for (int k = 0; k < 16; k++)
1291 fprintf(o, "%ld%s", rnd(srgb_decode(i + k) * 32768), k < 15 ? ", " : ",\n");
1292 }
1293 fprintf(o, "};\n");
1294 fprintf(o, "/* linear light (Q15 >> 3, 0..4095) -> sRGB level x 16 (sixteenths, for dithering) */\n");
1295 fprintf(o, "const uint16_t ck_sky_srgb16[4096] = {\n");
1296 for (int i = 0; i < 4096; i += 16) {
1297 fprintf(o, " ");
1298 for (int k = 0; k < 16; k++) {
1299 long v = rnd(srgb_encode((i + k) / 4095.0) * 16);
1300 fprintf(o, "%ld%s", v > 4080 ? 4080 : v, k < 15 ? ", " : ",\n");
1301 }
1302 }
1303 fprintf(o, "};\n\n");
1304
1305 /* the Milky Way grid: luma full, chroma at half, all in sixteenths-of-level space */
1306 double br[3], bgl[3];
1307 (void)bgl;
1308 blackbody_rgb(ballesteros(0.8), br); /* the dark sky's colour, B-V about 0.8 (Benn and Ellison 1998) */
1309 double gl = 0.2126 * br[0] + 0.7152 * br[1] + 0.0722 * br[2];
1310 double glow[3] = {a->glow_lin * br[0] / gl, a->glow_lin * br[1] / gl, a->glow_lin * br[2] / gl};
1311 int cols = gw_cols + 1;
1312 size_t ng = (size_t)cols * (size_t)gw_rows;
1313 double *lv = xmalloc(ng * 3 * sizeof(double));
1314 for (size_t i = 0; i < ng; i++)
1315 for (int k = 0; k < 3; k++) {
1316 /* surface brightness is the same wherever it lands on the screen (radiance is conserved) */
1317 double c = grid[i * 3 + (size_t)k] - floor_;
1318 double cc = glow[k] + (c > 0 ? c : 0) * a->g_phys * a->gain;
1319 lv[i * 3 + (size_t)k] = srgb_encode(cc); /* sRGB levels 0..255 (float) */
1320 }
1321 uint8_t *Y = xmalloc(ng);
1322 for (size_t i = 0; i < ng; i++) {
1323 long y = rnd((lv[i * 3] + 2 * lv[i * 3 + 1] + lv[i * 3 + 2]) / 4);
1324 Y[i] = (uint8_t)(y < 0 ? 0 : y > 255 ? 255 : y);
1325 }
1326 int hc = (cols + 1) / 2, hr = (gw_rows + 1) / 2;
1327 size_t nco = (size_t)hc * (size_t)hr;
1328 int8_t *CO = xmalloc(nco), *CG = xmalloc(nco);
1329 for (int j = 0; j < hr; j++)
1330 for (int i = 0; i < hc; i++) {
1331 double ao = 0, ag = 0;
1332 int n = 0;
1333 for (int dj = 0; dj < 2; dj++)
1334 for (int di = 0; di < 2; di++) {
1335 int ii = 2 * i + di < cols - 1 ? 2 * i + di : cols - 1;
1336 int jj = 2 * j + dj < gw_rows - 1 ? 2 * j + dj : gw_rows - 1;
1337 const double *p = lv + ((size_t)jj * (size_t)cols + (size_t)ii) * 3;
1338 ao += (p[0] - p[2]) / 2;
1339 ag += (-p[0] + 2 * p[1] - p[2]) / 4;
1340 n++;
1341 }
1342 long co = rnd(ao / n * 2), cg = rnd(ag / n * 2); /* half-levels */
1343 CO[(size_t)j * (size_t)hc + (size_t)i] = (int8_t)(co < -127 ? -127 : co > 127 ? 127 : co);
1344 CG[(size_t)j * (size_t)hc + (size_t)i] = (int8_t)(cg < -127 ? -127 : cg > 127 ? 127 : cg);
1345 }
1346 fprintf(o, "/* the Milky Way in the view: %d x %d samples, %.15g widths apart, the first row %.6f widths\n", cols, gw_rows, step,
1347 gy0);
1348 fprintf(o, " * above the view centre and the first column half a width left of it; luma (R + 2G + B) / 4 in sRGB\n");
1349 fprintf(o, " * levels; colour (Co, Cg) at half the spacing, in half levels */\n");
1350 fprintf(o, "const ck_sky_map ck_sky_milkyway = {\n");
1351 fprintf(o, " .cols = %d, .rows = %d, .per_width = %d, .row0_q16 = 0,\n};\n", cols, gw_rows, gw_cols);
1352 fprintf(o, "const uint8_t ck_sky_luma[%zu] = {\n", ng);
1353 for (size_t i = 0; i < ng; i += 24) {
1354 fprintf(o, " ");
1355 for (size_t k = i; k < i + 24 && k < ng; k++)
1356 fprintf(o, "%u%s", Y[k], k + 1 < i + 24 && k + 1 < ng ? ", " : ",\n");
1357 }
1358 fprintf(o, "};\nconst int8_t ck_sky_chroma[%zu][2] = {\n", nco);
1359 for (size_t i = 0; i < nco; i += 12) {
1360 fprintf(o, " ");
1361 for (size_t k = i; k < i + 12 && k < nco; k++)
1362 fprintf(o, "{%d, %d}%s", CO[k], CG[k], k + 1 < i + 12 && k + 1 < nco ? ", " : ",\n");
1363 }
1364 fprintf(o, "};\n\n");
1365
1366 /* galaxies: centre direction, axes and angle on screen worked out here */
1367 fprintf(o, "/* galaxies in the view bright enough to see: centre (Q15 direction), half axes in widths Q16,\n");
1368 fprintf(o, " * long axis angle on screen (turns / 65536, from screen right towards down), tint (sRGB) */\n");
1369 fprintf(o, "const ck_sky_galaxy ck_sky_galaxies[%zu] = {\n", ngal ? ngal : 1);
1370 for (size_t gi = 0; gi < ngal; gi++) {
1371 const galaxy_t *g = gals[gi].g;
1372 vec3 v = gals[gi].v;
1373 double p0x, p0y, p1x, p1y, p2x, p2y;
1374 to_screen(view, v, W, W, &p0x, &p0y);
1375 double pa = rad(g->pa), a_ = rad(g->ra), d_ = rad(g->dec);
1376 vec3 east = {-sin(a_), cos(a_), 0.0};
1377 vec3 north = {-sin(d_) * cos(a_), -sin(d_) * sin(a_), cos(d_)};
1378 double half = rad(g->maj / 120.0);
1379 double cp = cos(pa), sp = sin(pa);
1380 vec3 dirv = {cos(half) * v.x + sin(half) * (cp * north.x + sp * east.x), cos(half) * v.y + sin(half) * (cp * north.y + sp * east.y),
1381 cos(half) * v.z + sin(half) * (cp * north.z + sp * east.z)};
1382 double halfm = rad(g->min / 120.0);
1383 vec3 dirm = {cos(halfm) * v.x + sin(halfm) * (-sp * north.x + cp * east.x),
1384 cos(halfm) * v.y + sin(halfm) * (-sp * north.y + cp * east.y),
1385 cos(halfm) * v.z + sin(halfm) * (-sp * north.z + cp * east.z)};
1386 to_screen(view, dirv, W, W, &p1x, &p1y);
1387 to_screen(view, dirm, W, W, &p2x, &p2y);
1388 double ax = p1x - p0x, ay = p1y - p0y;
1389 double amaj = hypot(ax, ay) / W, amin = hypot(p2x - p0x, p2y - p0y) / W;
1390 double ang = pmod(atan2(ay, ax) / (2 * PI), 1.0);
1391 double rgb[3];
1392 blackbody_rgb(ballesteros(g->has_bv ? g->bv : 0.6), rgb);
1393 long t0 = rnd(srgb_encode(rgb[0])), t1 = rnd(srgb_encode(rgb[1])), t2 = rnd(srgb_encode(rgb[2]));
1394 char common[160];
1395 snprintf(common, sizeof common, "%s", g->common);
1396 char *c = strchr(common, ',');
1397 if (c)
1398 *c = 0;
1399 fprintf(o, " {{%ld, %ld, %ld}, %ld, %ld, %ld, 0x%02lx%02lx%02lxu}, /* %s, %s: V %.2f, %.1f x %.1f arcmin, PA %.0f deg */\n",
1400 rnd(v.x * 32767), rnd(v.y * 32767), rnd(v.z * 32767), rnd(amaj * 65536), rnd(amin * 65536), rnd(ang * 65536) % 65536,
1401 t0, t1, t2, g->name, common, g->vmag, g->maj, g->min, g->pa);
1402 report("galaxy %s: on screen half axes %.1f x %.1f px at 3840 wide, long axis at %.1f deg from right", g->name, amaj * W,
1403 amin * W, ang * 360);
1404 }
1405 if (!ngal)
1406 fprintf(o, " {{0, 0, 0}, 0, 0, 0, 0u},\n");
1407 fprintf(o, "};\nconst unsigned ck_sky_ngalaxies = %zu;\n\n", ngal);
1408
1409 /* a check list for the host test: named stars and where the generator puts them at 3840x2160 */
1410 fprintf(o, "/* for the host test: where these stars land on a 3840 x 2160 screen, 16ths of a pixel */\n");
1411 size_t chk_i[8], chk_n = 0;
1412 double chk_x[8], chk_y[8];
1413 for (size_t i = 0; i < nsel && i < 40 && chk_n < 8; i++) {
1414 double x, y;
1415 if (to_screen(view, sel[i].v, 3840, 2160, &x, &y) && 0 <= x && x < 3840 && 0 <= y && y < 2160)
1416 chk_i[chk_n] = i, chk_x[chk_n] = x, chk_y[chk_n] = y, chk_n++;
1417 }
1418 fprintf(o, "const ck_sky_check ck_sky_checks[%zu] = {\n", chk_n);
1419 for (size_t k = 0; k < chk_n; k++)
1420 fprintf(o, " {%zu, %ld, %ld}, /* HR %d %s */\n", chk_i[k], rnd(chk_x[k] * 16), rnd(chk_y[k] * 16), sel[chk_i[k]].s->hr,
1421 sel[chk_i[k]].s->name);
1422 fprintf(o, "};\nconst unsigned ck_sky_nchecks = %zu;\n", chk_n);
1423 fclose(o);
1424 report("tables: stars %zu B, Milky Way luma %zu B + colour %zu B, other tables about %d B", nsel * 8, ng, 2 * nco,
1425 256 * 4 + 128 * 6 + 512 + 8192);
1426 free(Y), free(CO), free(CG), free(lv);
1427}