ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gensdaymtx.c
Revision: 1.2
Committed: Fri Aug 2 19:01:13 2024 UTC (9 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +3 -0 lines
Log Message:
chore: Added missing RCSID and dependencies in Rmakefile

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 #include "atmos.h"
5 #include "copyright.h"
6 #include "data.h"
7 #include "platform.h"
8 #include "rtio.h"
9 #include <ctype.h>
10 #include <stdlib.h>
11 #ifdef _WIN32
12 #include <windows.h>
13 #else
14 #include <errno.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #endif
18
19 char *progname;
20
21 double altitude; /* Solar altitude (radians) */
22 double azimuth; /* Solar azimuth (radians) */
23 int julian_date; /* Julian date */
24 double sun_zenith; /* Sun zenith angle (radians) */
25 int input = 0; /* Input type */
26 int output = 0; /* Output type */
27 FVECT sundir;
28
29 const double ARCTIC_LAT = 67.;
30 const double TROPIC_LAT = 23.;
31 const int SUMMER_START = 4;
32 const int SUMMER_END = 9;
33 const double GNORM = 0.777778;
34
35 const double D65EFF = 203.; /* standard illuminant D65 */
36
37 /* Mean normalized relative daylight spectra where CCT = 6415K for overcast */
38 const double D6415[NSSAMP] = {0.63231, 1.06171, 1.00779, 1.36423, 1.34133,
39 1.27258, 1.26276, 1.26352, 1.22201, 1.13246,
40 1.0434, 1.05547, 0.98212, 0.94445, 0.9722,
41 0.82387, 0.87853, 0.82559, 0.75111, 0.78925};
42 /* Degrees into radians */
43 #define DegToRad(deg) ((deg) * (PI / 180.))
44
45 /* Radiuans into degrees */
46 #define RadToDeg(rad) ((rad) * (180. / PI))
47
48 #ifndef NSUNPATCH
49 #define NSUNPATCH 4 /* max. # patches to spread sun into */
50 #endif
51
52 #define SUN_ANG_DEG 0.533 /* sun full-angle in degrees */
53
54 int nsuns = NSUNPATCH; /* number of sun patches to use */
55 double fixed_sun_sa = -1; /* fixed solid angle per sun? */
56
57 int verbose = 0; /* progress reports to stderr? */
58
59 int outfmt = 'a'; /* output format */
60
61 int rhsubdiv = 1; /* Reinhart sky subdivisions */
62
63 COLOR skycolor = {.96, 1.004, 1.118}; /* sky coloration */
64 COLOR suncolor = {1., 1., 1.}; /* sun color */
65 double grefl = .2; /* ground reflectance */
66
67 int nskypatch; /* number of Reinhart patches */
68 float *rh_palt; /* sky patch altitudes (radians) */
69 float *rh_pazi; /* sky patch azimuths (radians) */
70 float *rh_dom; /* sky patch solid angle (sr) */
71
72 double sun_ct;
73
74 #define vector(v, alt, azi) \
75 ((v)[1] = cos(alt), (v)[0] = (v)[1] * sin(azi), (v)[1] *= cos(azi), \
76 (v)[2] = sin(alt))
77
78 #define rh_vector(v, i) vector(v, rh_palt[i], rh_pazi[i])
79
80 #define rh_cos(i) tsin(rh_palt[i])
81
82 #define solar_minute(jd, hr) ((24 * 60) * ((jd) - 1) + (int)((hr) * 60. + .5))
83
84 inline void vectorize(double altitude, double azimuth, FVECT v) {
85 v[1] = cos(altitude);
86 v[0] = (v)[1] * sin(azimuth);
87 v[1] *= cos(azimuth);
88 v[2] = sin(altitude);
89 }
90
91 static int make_directory(const char *path) {
92 #ifdef _WIN32
93 if (CreateDirectory(path, NULL) || GetLastError() == ERROR_ALREADY_EXISTS) {
94 return 1;
95 }
96 return 0;
97 #else
98 if (mkdir(path, 0777) == 0 || errno == EEXIST) {
99 return 1;
100 }
101 return 0;
102 #endif
103 }
104
105 static const char *getfmtname(int fmt) {
106 switch (fmt) {
107 case 'a':
108 return ("ascii");
109 case 'f':
110 return ("float");
111 case 'd':
112 return ("double");
113 }
114 return ("unknown");
115 }
116
117 static inline double wmean2(const double a, const double b, const double x) {
118 return a * (1 - x) + b * x;
119 }
120
121 static inline double wmean(const double a, const double x, const double b,
122 const double y) {
123 return (a * x + b * y) / (a + b);
124 }
125
126 static double get_zenith_brightness(const double sundir[3]) {
127 double zenithbr;
128 if (sundir[2] < 0) {
129 zenithbr = 0;
130 } else {
131 zenithbr = (8.6 * sundir[2] + .123) * 1000.0 / D65EFF;
132 }
133 return zenithbr;
134 }
135
136 /* from gensky.c */
137 static double get_overcast_brightness(const double dz, const double zenithbr) {
138 double groundbr = zenithbr * GNORM;
139 return wmean(pow(dz + 1.01, 10), zenithbr * (1 + 2 * dz) / 3,
140 pow(dz + 1.01, -10), groundbr);
141 }
142
143 int rh_init(void) {
144 #define NROW 7
145 static const int tnaz[NROW] = {30, 30, 24, 24, 18, 12, 6};
146 const double alpha = (PI / 2.) / (NROW * rhsubdiv + .5);
147 int p, i, j;
148 /* allocate patch angle arrays */
149 nskypatch = 0;
150 for (p = 0; p < NROW; p++)
151 nskypatch += tnaz[p];
152 nskypatch *= rhsubdiv * rhsubdiv;
153 nskypatch += 2;
154 rh_palt = (float *)malloc(sizeof(float) * nskypatch);
155 rh_pazi = (float *)malloc(sizeof(float) * nskypatch);
156 rh_dom = (float *)malloc(sizeof(float) * nskypatch);
157 if ((rh_palt == NULL) | (rh_pazi == NULL) | (rh_dom == NULL)) {
158 fprintf(stderr, "%s: out of memory in rh_init()\n", progname);
159 exit(1);
160 }
161 rh_palt[0] = -PI / 2.; /* ground & zenith patches */
162 rh_pazi[0] = 0.;
163 rh_dom[0] = 2. * PI;
164 rh_palt[nskypatch - 1] = PI / 2.;
165 rh_pazi[nskypatch - 1] = 0.;
166 rh_dom[nskypatch - 1] = 2. * PI * (1. - cos(alpha * .5));
167 p = 1; /* "normal" patches */
168 for (i = 0; i < NROW * rhsubdiv; i++) {
169 const float ralt = alpha * (i + .5);
170 const int ninrow = tnaz[i / rhsubdiv] * rhsubdiv;
171 const float dom =
172 2. * PI * (sin(alpha * (i + 1)) - sin(alpha * i)) / (double)ninrow;
173 for (j = 0; j < ninrow; j++) {
174 rh_palt[p] = ralt;
175 rh_pazi[p] = 2. * PI * j / (double)ninrow;
176 rh_dom[p++] = dom;
177 }
178 }
179 return nskypatch;
180 #undef NROW
181 }
182
183 /* Resize daylight matrix (GW) */
184 float *resize_dmatrix(float *mtx_data, int nsteps, int npatch) {
185 if (mtx_data == NULL)
186 mtx_data = (float *)malloc(sizeof(float) * NSSAMP * nsteps * npatch);
187 else
188 mtx_data =
189 (float *)realloc(mtx_data, sizeof(float) * NSSAMP * nsteps * npatch);
190 if (mtx_data == NULL) {
191 fprintf(stderr, "%s: out of memory in resize_dmatrix(%d,%d)\n", progname,
192 nsteps, npatch);
193 exit(1);
194 }
195 return (mtx_data);
196 }
197
198 static Atmosphere init_atmos(const double aod, const double grefl) {
199 Atmosphere atmos = {.ozone_density = {.layers =
200 {
201 {.width = 25000.0,
202 .exp_term = 0.0,
203 .exp_scale = 0.0,
204 .linear_term = 1.0 / 15000.0,
205 .constant_term = -2.0 / 3.0},
206 {.width = AH,
207 .exp_term = 0.0,
208 .exp_scale = 0.0,
209 .linear_term = -1.0 / 15000.0,
210 .constant_term = 8.0 / 3.0},
211 }},
212 .rayleigh_density = {.layers =
213 {
214 {.width = AH,
215 .exp_term = 1.0,
216 .exp_scale = -1.0 / HR_MS,
217 .linear_term = 0.0,
218 .constant_term = 0.0},
219 }},
220 .beta_r0 = BR0_MS,
221 .beta_scale = aod / AOD0_CA,
222 .beta_m = NULL,
223 .grefl = grefl};
224 return atmos;
225 }
226
227 static DpPaths get_dppaths(const char *dir, const double aod, const char *mname,
228 const char *tag) {
229 DpPaths paths;
230
231 snprintf(paths.tau, PATH_MAX, "%s%ctau_%s_%s_%.2f.dat", dir, DIRSEP, tag,
232 mname, aod);
233 snprintf(paths.scat, PATH_MAX, "%s%cscat_%s_%s_%.2f.dat", dir, DIRSEP, tag,
234 mname, aod);
235 snprintf(paths.scat1m, PATH_MAX, "%s%cscat1m_%s_%s_%.2f.dat", dir, DIRSEP,
236 tag, mname, aod);
237 snprintf(paths.irrad, PATH_MAX, "%s%cirrad_%s_%s_%.2f.dat", dir, DIRSEP, tag,
238 mname, aod);
239
240 return paths;
241 }
242 static void set_rayleigh_density_profile(Atmosphere *atmos, char *tag,
243 const int is_summer,
244 const double s_latitude) {
245 /* Set rayleigh density profile */
246 if (fabs(s_latitude * 180.0 / PI) > ARCTIC_LAT) {
247 tag[0] = 's';
248 if (is_summer) {
249 tag[1] = 's';
250 atmos->rayleigh_density.layers[0].exp_scale = -1.0 / HR_SS;
251 atmos->beta_r0 = BR0_SS;
252 } else {
253 tag[1] = 'w';
254 atmos->rayleigh_density.layers[0].exp_scale = -1.0 / HR_SW;
255 atmos->beta_r0 = BR0_SW;
256 }
257 } else if (fabs(s_latitude * 180.0 / PI) > TROPIC_LAT) {
258 tag[0] = 'm';
259 if (is_summer) {
260 tag[1] = 's';
261 atmos->rayleigh_density.layers[0].exp_scale = -1.0 / HR_MS;
262 atmos->beta_r0 = BR0_MS;
263 } else {
264 tag[1] = 'w';
265 atmos->rayleigh_density.layers[0].exp_scale = -1.0 / HR_MW;
266 atmos->beta_r0 = BR0_MW;
267 }
268 } else {
269 tag[0] = 't';
270 tag[1] = 'r';
271 atmos->rayleigh_density.layers[0].exp_scale = -1.0 / HR_T;
272 atmos->beta_r0 = BR0_T;
273 }
274 tag[2] = '\0';
275 }
276 /* Add in solar direct to nearest sky patches (GW) */
277 void add_direct(DATARRAY *tau, DATARRAY *scat, DATARRAY *scat1m,
278 DATARRAY *irrad, double ccover, float *parr) {
279 FVECT svec;
280 double near_dprod[NSUNPATCH];
281 int near_patch[NSUNPATCH];
282 double wta[NSUNPATCH], wtot;
283 int i, j, p;
284
285 /* identify nsuns closest patches */
286 for (i = nsuns; i--;)
287 near_dprod[i] = -1.;
288 vectorize(altitude, azimuth, svec);
289 for (p = 1; p < nskypatch; p++) {
290 FVECT pvec;
291 double dprod;
292 vectorize(rh_palt[p], rh_pazi[p], pvec);
293 dprod = DOT(pvec, svec);
294 for (i = 0; i < nsuns; i++)
295 if (dprod > near_dprod[i]) {
296 for (j = nsuns; --j > i;) {
297 near_dprod[j] = near_dprod[j - 1];
298 near_patch[j] = near_patch[j - 1];
299 }
300 near_dprod[i] = dprod;
301 near_patch[i] = p;
302 break;
303 }
304 }
305 /* Get solar radiance */
306 double sun_radiance[NSSAMP] = {0};
307 get_solar_radiance(tau, scat, scat1m, sundir, ER, sun_ct, sun_radiance);
308 if (ccover > 0) {
309 double zenithbr = get_zenith_brightness(sundir);
310 double skybr = get_overcast_brightness(sundir[2], zenithbr);
311 for (int l = 0; l < NSSAMP; ++l) {
312 sun_radiance[l] =
313 wmean2(sun_radiance[l], D6415[l] * skybr / WVLSPAN, ccover);
314 }
315 }
316 /* weight by proximity */
317 wtot = 0;
318 for (i = nsuns; i--;)
319 wtot += wta[i] = 1. / (1.002 - near_dprod[i]);
320 /* add to nearest patch radiances */
321 for (i = nsuns; i--;) {
322 float *pdest = parr + NSSAMP * near_patch[i];
323 for (int k = 0; k < NSSAMP; k++) {
324 *pdest++ = sun_radiance[k] * wta[i] / wtot;
325 }
326 }
327 }
328
329 void calc_sky_patch_radiance(DATARRAY *scat, DATARRAY *scat1m, double ccover,
330 float *parr) {
331 int i;
332 double mu_sky; /* Sun-sky point azimuthal angle */
333 double sspa; /* Sun-sky point angle */
334 double zsa; /* Zenithal sun angle */
335 FVECT view_point = {0, 0, ER};
336 for (i = 1; i < nskypatch; i++) {
337 FVECT rdir_sky;
338 vectorize(rh_palt[i], rh_pazi[i], rdir_sky);
339 mu_sky = fdot(view_point, rdir_sky) / ER;
340 sspa = fdot(rdir_sky, sundir);
341 SCOLOR sky_radiance = {0};
342
343 get_sky_radiance(scat, scat1m, ER, mu_sky, sun_ct, sspa, sky_radiance);
344 for (int k = 0; k < NSSAMP; ++k) {
345 sky_radiance[k] *= WVLSPAN;
346 }
347
348 if (ccover > 0) {
349 double zenithbr = get_zenith_brightness(sundir);
350 double grndbr = zenithbr * GNORM;
351 double skybr = get_overcast_brightness(rdir_sky[2], zenithbr);
352 for (int k = 0; k < NSSAMP; ++k) {
353 sky_radiance[k] = wmean2(sky_radiance[k], skybr * D6415[k], ccover);
354 }
355 }
356
357 for (int k = 0; k < NSSAMP; ++k) {
358 parr[NSSAMP * i + k] = sky_radiance[k];
359 }
360 }
361 }
362
363 /* Return maximum of two doubles */
364 static inline double dmax(double a, double b) { return (a > b) ? a : b; }
365
366 /* Compute sky patch radiance values (modified by GW) */
367 void compute_sky(DATARRAY *tau, DATARRAY *scat, DATARRAY *scat1m,
368 DATARRAY *irrad, double ccover, float *parr) {
369 int index; /* Category index */
370 int i;
371 float sun_zenith;
372 SCOLOR sky_radiance = {0};
373 SCOLOR ground_radiance = {0};
374 SCOLR sky_sclr = {0};
375 SCOLR ground_sclr = {0};
376 FVECT view_point = {0, 0, ER};
377 const double radius = VLEN(view_point);
378 const double sun_ct = fdot(view_point, sundir) / radius;
379 const FVECT rdir_grnd = {0, 0, -1};
380 const double mu_grnd = fdot(view_point, rdir_grnd) / radius;
381 const double nu_grnd = fdot(rdir_grnd, sundir);
382
383 /* Calculate sun zenith angle (don't let it dip below horizon) */
384 /* Also limit minimum angle to keep circumsolar off zenith */
385 if (altitude <= 0.0)
386 sun_zenith = DegToRad(90.0);
387 else if (altitude >= DegToRad(87.0))
388 sun_zenith = DegToRad(3.0);
389 else
390 sun_zenith = DegToRad(90.0) - altitude;
391
392 /* Compute ground radiance (include solar contribution if any) */
393 get_ground_radiance(tau, scat, scat1m, irrad, view_point, rdir_grnd, radius,
394 mu_grnd, sun_ct, nu_grnd, grefl, sundir, parr);
395 for (int j = 0; j < NSSAMP; j++) {
396 parr[j] *= WVLSPAN;
397 }
398 calc_sky_patch_radiance(scat, scat1m, ccover, parr);
399 }
400
401 int main(int argc, char *argv[]) {
402
403 char buf[256];
404 int doheader = 1; /* output header? */
405 double rotation = 0.0;
406 double elevation = 0;
407 int leap_day = 0; /* add leap day? */
408 int sun_hours_only = 0; /* only output sun hours? */
409 float *mtx_data = NULL;
410 int ntsteps = 0; /* number of time steps */
411 int tstorage = 0; /* number of allocated time steps */
412 int nstored = 0; /* number of time steps in matrix */
413 int last_monthly = 0; /* month of last report */
414 int mo, da;
415 double hr, aod, cc;
416 double dni, dhi;
417 int mtx_offset = 0;
418 int i, j;
419 char lstag[3];
420 char *mie_path = getpath("mie_ca.dat", getrlibpath(), R_OK);
421 char *ddir = ".";
422 char mie_name[20] = "mie_ca";
423 int num_threads = 1;
424 int sorder = 4;
425 int solar_only = 0;
426 int sky_only = 0;
427 FVECT view_point = {0, 0, ER};
428
429 progname = argv[0];
430
431 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
432 switch (argv[i][1]) {
433 case 'd': /* solar (direct) only */
434 solar_only = 1;
435 break;
436 case 's': /* sky only (no direct) */
437 sky_only = 1;
438 break;
439 case 'g':
440 grefl = atof(argv[++i]);
441 break;
442 case 'm':
443 rhsubdiv = atoi(argv[++i]);
444 break;
445 case 'n':
446 num_threads = atoi(argv[++i]);
447 break;
448 case 'r': /* rotate distribution */
449 if (argv[i][2] && argv[i][2] != 'z')
450 goto userr;
451 rotation = atof(argv[++i]);
452 break;
453 case 'u': /* solar hours only */
454 sun_hours_only = 1;
455 break;
456 case 'p':
457 ddir = argv[++i];
458 break;
459 case 'v': /* verbose progress reports */
460 verbose++;
461 break;
462 case 'h': /* turn off header */
463 doheader = 0;
464 break;
465 case '5': /* 5-phase calculation */
466 nsuns = 1;
467 fixed_sun_sa = PI / 360. * atof(argv[++i]);
468 if (fixed_sun_sa <= 0) {
469 fprintf(stderr,
470 "%s: missing solar disk size argument for '-5' option\n",
471 progname);
472 exit(1);
473 }
474 fixed_sun_sa *= fixed_sun_sa * PI;
475 break;
476 case 'o': /* output format */
477 switch (argv[i][2]) {
478 case 'f':
479 case 'd':
480 case 'a':
481 outfmt = argv[i][2];
482 break;
483 default:
484 goto userr;
485 }
486 break;
487 default:
488 goto userr;
489 }
490 }
491 if (i < argc - 1)
492 goto userr;
493 if (i == argc - 1 && freopen(argv[i], "r", stdin) == NULL) {
494 fprintf(stderr, "%s: cannot open '%s' for input\n", progname, argv[i]);
495 exit(1);
496 }
497 if (verbose) {
498 if (i == argc - 1)
499 fprintf(stderr, "%s: reading weather tape '%s'\n", progname, argv[i]);
500 else
501 fprintf(stderr, "%s: reading weather tape from <stdin>\n", progname);
502 }
503 /* read weather tape header */
504 if (scanf("place %[^\r\n] ", buf) != 1)
505 goto fmterr;
506 if (scanf("latitude %lf\n", &s_latitude) != 1)
507 goto fmterr;
508 if (scanf("longitude %lf\n", &s_longitude) != 1)
509 goto fmterr;
510 if (scanf("time_zone %lf\n", &s_meridian) != 1)
511 goto fmterr;
512 if (scanf("site_elevation %lf\n", &elevation) != 1)
513 goto fmterr;
514 if (scanf("weather_data_file_units %d\n", &input) != 1)
515 goto fmterr;
516
517 rh_init();
518 if (verbose) {
519 fprintf(stderr, "%s: location '%s'\n", progname, buf);
520 fprintf(stderr, "%s: (lat,long)=(%.1f,%.1f) degrees north, west\n",
521 progname, s_latitude, s_longitude);
522 if (rotation != 0)
523 fprintf(stderr, "%s: rotating output %.0f degrees\n", progname, rotation);
524 }
525
526 s_latitude = DegToRad(s_latitude);
527 s_longitude = DegToRad(s_longitude);
528 s_meridian = DegToRad(s_meridian);
529 /* initial allocation */
530 mtx_data = resize_dmatrix(mtx_data, tstorage = 2, nskypatch);
531
532 /* Load mie density data */
533 DATARRAY *mie_dp = getdata(mie_path);
534 if (mie_dp == NULL) {
535 fprintf(stderr, "Error reading mie data\n");
536 return 0;
537 }
538
539 while (scanf("%d %d %lf %lf %lf %lf %lf\n", &mo, &da, &hr, &dni, &dhi, &aod,
540 &cc) == 7) {
541 double sda, sta;
542 int sun_in_sky;
543 /* compute solar position */
544 if ((mo == 2) & (da == 29)) {
545 julian_date = 60;
546 leap_day = 1;
547 } else
548 julian_date = jdate(mo, da) + leap_day;
549 sda = sdec(julian_date);
550 sta = stadj(julian_date);
551 altitude = salt(sda, hr + sta);
552 sun_in_sky = (altitude > -DegToRad(SUN_ANG_DEG / 2.));
553
554 azimuth = sazi(sda, hr + sta) + PI - DegToRad(rotation);
555
556 vectorize(altitude, azimuth, sundir);
557 if (sun_hours_only && sundir[2] <= 0.) {
558 continue; /* skipping nighttime points */
559 }
560 sun_ct = fdot(view_point, sundir) / ER;
561
562 mtx_offset = NSSAMP * nskypatch * nstored;
563 nstored += 1;
564 /* make space for next row */
565 if (nstored > tstorage) {
566 tstorage += (tstorage >> 1) + nstored + 7;
567 mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
568 }
569 ntsteps++; /* keep count of time steps */
570 /* compute sky patch values */
571 Atmosphere clear_atmos = init_atmos(aod, grefl);
572 int is_summer = (mo >= SUMMER_START && mo <= SUMMER_END);
573 if (s_latitude < 0) {
574 is_summer = !is_summer;
575 }
576 set_rayleigh_density_profile(&clear_atmos, lstag, is_summer, s_latitude);
577
578 clear_atmos.beta_m = mie_dp;
579
580 char gsdir[PATH_MAX];
581 size_t siz = strlen(ddir);
582 if (ISDIRSEP(ddir[siz - 1]))
583 ddir[siz - 1] = '\0';
584 snprintf(gsdir, PATH_MAX, "%s%catmos_data", ddir, DIRSEP);
585 if (!make_directory(gsdir)) {
586 fprintf(stderr, "Failed creating atmos_data directory");
587 exit(1);
588 }
589 DpPaths clear_paths = get_dppaths(gsdir, aod, mie_name, lstag);
590
591 if (getpath(clear_paths.tau, ".", R_OK) == NULL ||
592 getpath(clear_paths.scat, ".", R_OK) == NULL ||
593 getpath(clear_paths.scat1m, ".", R_OK) == NULL ||
594 getpath(clear_paths.irrad, ".", R_OK) == NULL) {
595 printf("# Pre-computing...\n");
596 if (!precompute(sorder, clear_paths, &clear_atmos, num_threads)) {
597 fprintf(stderr, "Pre-compute failed\n");
598 return 0;
599 }
600 }
601
602 DATARRAY *tau_clear_dp = getdata(clear_paths.tau);
603 DATARRAY *irrad_clear_dp = getdata(clear_paths.irrad);
604 DATARRAY *scat_clear_dp = getdata(clear_paths.scat);
605 DATARRAY *scat1m_clear_dp = getdata(clear_paths.scat1m);
606
607 if (!solar_only)
608 compute_sky(tau_clear_dp, scat_clear_dp, scat1m_clear_dp, irrad_clear_dp,
609 cc, mtx_data + mtx_offset);
610 if (!sky_only)
611 add_direct(tau_clear_dp, scat_clear_dp, scat1m_clear_dp, irrad_clear_dp,
612 cc, mtx_data + mtx_offset);
613 /* update cumulative sky? */
614 for (i = NSSAMP * nskypatch * (ntsteps > 1); i--;)
615 mtx_data[i] += mtx_data[mtx_offset + i];
616 /* monthly reporting */
617 if (verbose && mo != last_monthly)
618 fprintf(stderr, "%s: stepping through month %d...\n", progname,
619 last_monthly = mo);
620 /* note whether leap-day was given */
621
622 freedata(tau_clear_dp);
623 freedata(irrad_clear_dp);
624 freedata(scat_clear_dp);
625 freedata(scat1m_clear_dp);
626 }
627 freedata(mie_dp);
628 if (!ntsteps) {
629 fprintf(stderr, "%s: no valid time steps on input\n", progname);
630 exit(1);
631 }
632 /* check for junk at end */
633 while ((i = fgetc(stdin)) != EOF)
634 if (!isspace(i)) {
635 fprintf(stderr, "%s: warning - unexpected data past EOT: ", progname);
636 buf[0] = i;
637 buf[1] = '\0';
638 fgets(buf + 1, sizeof(buf) - 1, stdin);
639 fputs(buf, stderr);
640 fputc('\n', stderr);
641 break;
642 }
643 /* write out matrix */
644 if (outfmt != 'a')
645 SET_FILE_BINARY(stdout);
646 #ifdef getc_unlocked
647 flockfile(stdout);
648 #endif
649 if (verbose)
650 fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", progname,
651 outfmt == 'a' ? "" : "binary ", nstored);
652 if (doheader) {
653 newheader("RADIANCE", stdout);
654 printargs(argc, argv, stdout);
655 printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
656 -RadToDeg(s_longitude));
657 printf("NROWS=%d\n", nskypatch);
658 printf("NCOLS=%d\n", nstored);
659 printf("NCOMP=%d\n", NSSAMP);
660 if ((outfmt == 'f') | (outfmt == 'd'))
661 fputendian(stdout);
662 fputformat((char *)getfmtname(outfmt), stdout);
663 putchar('\n');
664 }
665 /* patches are rows (outer sort) */
666 for (i = 0; i < nskypatch; i++) {
667 mtx_offset = NSSAMP * i;
668 switch (outfmt) {
669 case 'a':
670 for (j = 0; j < nstored; j++) {
671 for (int k = 0; k < NSSAMP; k++) {
672 printf("%.3g \n", mtx_data[mtx_offset + k]);
673 }
674 printf("\n");
675 mtx_offset += NSSAMP * nskypatch;
676 }
677 if (nstored > 1)
678 fputc('\n', stdout);
679 break;
680 case 'f':
681 for (j = 0; j < nstored; j++) {
682 putbinary(mtx_data + mtx_offset, sizeof(float), NSSAMP, stdout);
683 mtx_offset += NSSAMP * nskypatch;
684 }
685 break;
686 case 'd':
687 for (j = 0; j < nstored; j++) {
688 double ment[NSSAMP];
689 for (j = 0; j < NSSAMP; j++)
690 ment[j] = mtx_data[mtx_offset + j];
691 putbinary(ment, sizeof(double), NSSAMP, stdout);
692 mtx_offset += NSSAMP * nskypatch;
693 }
694 break;
695 }
696 if (ferror(stdout))
697 goto writerr;
698 }
699 alldone:
700 if (fflush(NULL) == EOF)
701 goto writerr;
702 if (verbose)
703 fprintf(stderr, "%s: done.\n", progname);
704 exit(0);
705 userr:
706 fprintf(stderr,
707 "Usage: %s [-v][-h][-A][-d|-s|-n][-u][-D file [-M modfile]][-r "
708 "deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n",
709 progname);
710 exit(1);
711 fmterr:
712 fprintf(stderr, "%s: weather tape format error in header\n", progname);
713 exit(1);
714 writerr:
715 fprintf(stderr, "%s: write error on output\n", progname);
716 exit(1);
717 }