--- ray/src/gen/gensky.c 1994/01/31 12:53:49 2.14 +++ ray/src/gen/gensky.c 2014/07/30 17:30:27 2.26 @@ -1,9 +1,6 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: gensky.c,v 2.26 2014/07/30 17:30:27 greg Exp $"; #endif - /* * gensky.c - program to generate sky functions. * Our zenith is along the Z-axis, the X-axis @@ -14,16 +11,17 @@ static char SCCSid[] = "$SunId$ LBL"; */ #include - +#include +#include #include - +#include +#include "sun.h" #include "color.h" -extern char *strcpy(), *strcat(), *malloc(); -extern double stadj(), sdec(), sazi(), salt(); +#ifndef PI +#define PI 3.14159265358979323846 +#endif -#define PI 3.141592654 - #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]) #define S_CLEAR 1 @@ -31,13 +29,35 @@ extern double stadj(), sdec(), sazi(), salt(); #define S_UNIF 3 #define S_INTER 4 -#define overcast (skytype==S_OVER|skytype==S_UNIF) +#define overcast ((skytype==S_OVER)|(skytype==S_UNIF)) double normsc(); - /* sun calculation constants */ -extern double s_latitude; -extern double s_longitude; -extern double s_meridian; + +#undef toupper +#define toupper(c) ((c) & ~0x20) /* ASCII trick to convert case */ + + /* European and North American zones */ +struct { + char zname[8]; /* time zone name (all caps) */ + float zmer; /* standard meridian */ +} tzone[] = { + {"YST", 135}, {"YDT", 120}, + {"PST", 120}, {"PDT", 105}, + {"MST", 105}, {"MDT", 90}, + {"CST", 90}, {"CDT", 75}, + {"EST", 75}, {"EDT", 60}, + {"AST", 60}, {"ADT", 45}, + {"NST", 52.5}, {"NDT", 37.5}, + {"GMT", 0}, {"BST", -15}, + {"CET", -15}, {"CEST", -30}, + {"EET", -30}, {"EEST", -45}, + {"AST", -45}, {"ADT", -60}, + {"GST", -60}, {"GDT", -75}, + {"IST", -82.5}, {"IDT", -97.5}, + {"JST", -135}, {"NDT", -150}, + {"NZST", -180}, {"NZDT", -195}, + {"", 0} +}; /* required values */ int month, day; /* date */ double hour; /* time */ @@ -48,7 +68,7 @@ int skytype = S_CLEAR; /* sky type */ int dosun = 1; double zenithbr = 0.0; int u_zenith = 0; /* -1=irradiance, 1=radiance */ -double turbidity = 2.75; +double turbidity = 2.45; double gprefl = 0.2; /* computed values */ double sundir[3]; @@ -60,11 +80,22 @@ int u_solar = 0; /* -1=irradiance, 1=radiance */ char *progname; char errmsg[128]; +void computesky(void); +void printsky(void); +void printdefaults(void); +void userror(char *msg); +double normsc(void); +int cvthour(char *hs); +void printhead(int ac, char **av); -main(argc, argv) -int argc; -char *argv[]; + +int +main( + int argc, + char *argv[] +) { + int got_meridian = 0; int i; progname = argv[0]; @@ -85,10 +116,7 @@ char *argv[]; day = atoi(argv[2]); if (day < 1 || day > 31) userror("bad day"); - hour = atof(argv[3]); - if (hour < 0 || hour >= 24) - userror("bad hour"); - tsolar = argv[3][0] == '+'; + got_meridian = cvthour(argv[3]); } for (i = 4; i < argc; i++) if (argv[i][0] == '-' || argv[i][0] == '+') @@ -130,6 +158,10 @@ char *argv[]; s_longitude = atof(argv[++i]) * (PI/180); break; case 'm': + if (got_meridian) { + ++i; + break; /* time overrides */ + } s_meridian = atof(argv[++i]) * (PI/180); break; default: @@ -139,19 +171,22 @@ char *argv[]; else userror("bad option"); - if (fabs(s_meridian-s_longitude) > 30*PI/180) + if (month && !tsolar && fabs(s_meridian-s_longitude) > 45*PI/180) fprintf(stderr, - "%s: warning: %.1f hours btwn. standard meridian and longitude\n", + "%s: warning - %.1f hours btwn. standard meridian and longitude\n", progname, (s_longitude-s_meridian)*12/PI); printhead(argc, argv); computesky(); printsky(); + + exit(0); } -computesky() /* compute sky parameters */ +void +computesky(void) /* compute sky parameters */ { double normfactor; /* compute solar direction */ @@ -167,6 +202,7 @@ computesky() /* compute sky parameters */ st = hour + stadj(jd); altitude = salt(sd, st); azimuth = sazi(sd, st); + printf("# Local solar time: %.2f\n", st); printf("# Solar altitude and azimuth: %.1f %.1f\n", 180./PI*altitude, 180./PI*azimuth); } @@ -235,7 +271,8 @@ computesky() /* compute sky parameters */ } -printsky() /* print out sky */ +void +printsky(void) /* print out sky */ { if (dosun) { printf("\nvoid light solar\n"); @@ -258,7 +295,8 @@ printsky() /* print out sky */ } -printdefaults() /* print default values */ +void +printdefaults(void) /* print default values */ { switch (skytype) { case S_OVER: @@ -291,8 +329,10 @@ printdefaults() /* print default values */ } -userror(msg) /* print usage error and quit */ -char *msg; +void +userror( /* print usage error and quit */ + char *msg +) { if (msg != NULL) fprintf(stderr, "%s: Use error - %s\n", progname, msg); @@ -304,7 +344,7 @@ char *msg; double -normsc() /* compute normalization factor (E0*F2/L0) */ +normsc(void) /* compute normalization factor (E0*F2/L0) */ { static double nfc[2][5] = { /* clear sky approx. */ @@ -312,9 +352,9 @@ normsc() /* compute normalization factor (E0*F2/L0) /* intermediate sky approx. */ {3.5556, -2.7152, -1.3081, 1.0660, 0.60227}, }; - register double *nf; + double *nf; double x, nsc; - register int i; + int i; /* polynomial approximation */ nf = nfc[skytype==S_INTER]; x = (altitude - PI/4.0)/(PI/4.0); @@ -326,9 +366,54 @@ normsc() /* compute normalization factor (E0*F2/L0) } -printhead(ac, av) /* print command header */ -register int ac; -register char **av; +int +cvthour( /* convert hour string */ + char *hs +) +{ + char *cp = hs; + int i, j; + + if ( (tsolar = *cp == '+') ) cp++; /* solar time? */ + while (isdigit(*cp)) cp++; + if (*cp == ':') + hour = atoi(hs) + atoi(++cp)/60.0; + else { + hour = atof(hs); + if (*cp == '.') cp++; + } + while (isdigit(*cp)) cp++; + if (!*cp) + return(0); + if (tsolar || !isalpha(*cp)) { + fprintf(stderr, "%s: bad time format: %s\n", progname, hs); + exit(1); + } + i = 0; + do { + for (j = 0; cp[j]; j++) + if (toupper(cp[j]) != tzone[i].zname[j]) + break; + if (!cp[j] && !tzone[i].zname[j]) { + s_meridian = tzone[i].zmer * (PI/180); + return(1); + } + } while (tzone[i++].zname[0]); + + fprintf(stderr, "%s: unknown time zone: %s\n", progname, cp); + fprintf(stderr, "Known time zones:\n\t%s", tzone[0].zname); + for (i = 1; tzone[i].zname[0]; i++) + fprintf(stderr, " %s", tzone[i].zname); + putc('\n', stderr); + exit(1); +} + + +void +printhead( /* print command header */ + int ac, + char **av +) { putchar('#'); while (ac--) {