Back to index

radiance  4R0+20100331
gensky.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: gensky.c,v 2.24 2006/07/26 17:15:11 greg Exp $";
00003 #endif
00004 /*
00005  *  gensky.c - program to generate sky functions.
00006  *            Our zenith is along the Z-axis, the X-axis
00007  *            points east, and the Y-axis points north.
00008  *            Radiance is in watts/steradian/sq. meter.
00009  *
00010  *     3/26/86
00011  */
00012 
00013 #include  <stdio.h>
00014 #include  <stdlib.h>
00015 #include  <string.h>
00016 #include  <math.h>
00017 #include  <ctype.h>
00018 
00019 #include  "color.h"
00020 
00021 extern int jdate(int month, int day);
00022 extern double stadj(int  jd);
00023 extern double sdec(int  jd);
00024 extern double salt(double sd, double st);
00025 extern double sazi(double sd, double st);
00026 
00027 #ifndef  PI
00028 #define  PI          3.14159265358979323846
00029 #endif
00030 
00031 #define  DOT(v1,v2)  (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
00032 
00033 #define  S_CLEAR     1
00034 #define  S_OVER             2
00035 #define  S_UNIF             3
00036 #define  S_INTER     4
00037 
00038 #define  overcast    ((skytype==S_OVER)|(skytype==S_UNIF))
00039 
00040 double  normsc();
00041                                    /* sun calculation constants */
00042 extern double  s_latitude;
00043 extern double  s_longitude;
00044 extern double  s_meridian;
00045 
00046 #undef  toupper
00047 #define  toupper(c)  ((c) & ~0x20) /* ASCII trick to convert case */
00048 
00049                                    /* European and North American zones */
00050 struct {
00051        char   zname[8];     /* time zone name (all caps) */
00052        float  zmer;         /* standard meridian */
00053 } tzone[] = {
00054        {"YST", 135}, {"YDT", 120},
00055        {"PST", 120}, {"PDT", 105},
00056        {"MST", 105}, {"MDT", 90},
00057        {"CST", 90}, {"CDT", 75},
00058        {"EST", 75}, {"EDT", 60},
00059        {"AST", 60}, {"ADT", 45},
00060        {"NST", 52.5}, {"NDT", 37.5},
00061        {"GMT", 0}, {"BST", -15},
00062        {"CET", -15}, {"CEST", -30},
00063        {"EET", -30}, {"EEST", -45},
00064        {"AST", -45}, {"ADT", -60},
00065        {"GST", -60}, {"GDT", -75},
00066        {"IST", -82.5}, {"IDT", -97.5},
00067        {"JST", -135}, {"NDT", -150},
00068        {"NZST", -180}, {"NZDT", -195},
00069        {"", 0}
00070 };
00071                                    /* required values */
00072 int  month, day;                          /* date */
00073 double  hour;                             /* time */
00074 int  tsolar;                              /* 0=standard, 1=solar */
00075 double  altitude, azimuth;                /* or solar angles */
00076                                    /* default values */
00077 int  skytype = S_CLEAR;                          /* sky type */
00078 int  dosun = 1;
00079 double  zenithbr = 0.0;
00080 int    u_zenith = 0;                      /* -1=irradiance, 1=radiance */
00081 double  turbidity = 2.45;
00082 double  gprefl = 0.2;
00083                                    /* computed values */
00084 double  sundir[3];
00085 double  groundbr;
00086 double  F2;
00087 double  solarbr = 0.0;
00088 int    u_solar = 0;                       /* -1=irradiance, 1=radiance */
00089 
00090 char  *progname;
00091 char  errmsg[128];
00092 
00093 void computesky(void);
00094 void printsky(void);
00095 void printdefaults(void);
00096 void userror(char  *msg);
00097 double normsc(void);
00098 int cvthour(char  *hs);
00099 void printhead(register int  ac, register char  **av);
00100 
00101 
00102 int
00103 main(argc, argv)
00104 int  argc;
00105 char  *argv[];
00106 {
00107        int  got_meridian = 0;
00108        int  i;
00109 
00110        progname = argv[0];
00111        if (argc == 2 && !strcmp(argv[1], "-defaults")) {
00112               printdefaults();
00113               exit(0);
00114        }
00115        if (argc < 4)
00116               userror("arg count");
00117        if (!strcmp(argv[1], "-ang")) {
00118               altitude = atof(argv[2]) * (PI/180);
00119               azimuth = atof(argv[3]) * (PI/180);
00120               month = 0;
00121        } else {
00122               month = atoi(argv[1]);
00123               if (month < 1 || month > 12)
00124                      userror("bad month");
00125               day = atoi(argv[2]);
00126               if (day < 1 || day > 31)
00127                      userror("bad day");
00128               got_meridian = cvthour(argv[3]);
00129        }
00130        for (i = 4; i < argc; i++)
00131               if (argv[i][0] == '-' || argv[i][0] == '+')
00132                      switch (argv[i][1]) {
00133                      case 's':
00134                             skytype = S_CLEAR;
00135                             dosun = argv[i][0] == '+';
00136                             break;
00137                      case 'r':
00138                      case 'R':
00139                             u_solar = argv[i][1]=='R' ? -1 : 1;
00140                             solarbr = atof(argv[++i]);
00141                             break;
00142                      case 'c':
00143                             skytype = S_OVER;
00144                             break;
00145                      case 'u':
00146                             skytype = S_UNIF;
00147                             break;
00148                      case 'i':
00149                             skytype = S_INTER;
00150                             dosun = argv[i][0] == '+';
00151                             break;
00152                      case 't':
00153                             turbidity = atof(argv[++i]);
00154                             break;
00155                      case 'b':
00156                      case 'B':
00157                             u_zenith = argv[i][1]=='B' ? -1 : 1;
00158                             zenithbr = atof(argv[++i]);
00159                             break;
00160                      case 'g':
00161                             gprefl = atof(argv[++i]);
00162                             break;
00163                      case 'a':
00164                             s_latitude = atof(argv[++i]) * (PI/180);
00165                             break;
00166                      case 'o':
00167                             s_longitude = atof(argv[++i]) * (PI/180);
00168                             break;
00169                      case 'm':
00170                             if (got_meridian) {
00171                                    ++i;
00172                                    break;        /* time overrides */
00173                             }
00174                             s_meridian = atof(argv[++i]) * (PI/180);
00175                             break;
00176                      default:
00177                             sprintf(errmsg, "unknown option: %s", argv[i]);
00178                             userror(errmsg);
00179                      }
00180               else
00181                      userror("bad option");
00182 
00183        if (fabs(s_meridian-s_longitude) > 45*PI/180)
00184               fprintf(stderr,
00185        "%s: warning: %.1f hours btwn. standard meridian and longitude\n",
00186                      progname, (s_longitude-s_meridian)*12/PI);
00187 
00188        printhead(argc, argv);
00189 
00190        computesky();
00191        printsky();
00192 
00193        exit(0);
00194 }
00195 
00196 
00197 void
00198 computesky(void)                   /* compute sky parameters */
00199 {
00200        double normfactor;
00201                                    /* compute solar direction */
00202        if (month) {                /* from date and time */
00203               int  jd;
00204               double  sd, st;
00205 
00206               jd = jdate(month, day);            /* Julian date */
00207               sd = sdec(jd);                     /* solar declination */
00208               if (tsolar)                 /* solar time */
00209                      st = hour;
00210               else
00211                      st = hour + stadj(jd);
00212               altitude = salt(sd, st);
00213               azimuth = sazi(sd, st);
00214               printf("# Local solar time: %.2f\n", st);
00215               printf("# Solar altitude and azimuth: %.1f %.1f\n",
00216                             180./PI*altitude, 180./PI*azimuth);
00217        }
00218        if (!overcast && altitude > 87.*PI/180.) {
00219               fprintf(stderr,
00220 "%s: warning - sun too close to zenith, reducing altitude to 87 degrees\n",
00221                             progname);
00222               printf(
00223 "# warning - sun too close to zenith, reducing altitude to 87 degrees\n");
00224               altitude = 87.*PI/180.;
00225        }
00226        sundir[0] = -sin(azimuth)*cos(altitude);
00227        sundir[1] = -cos(azimuth)*cos(altitude);
00228        sundir[2] = sin(altitude);
00229 
00230                                    /* Compute normalization factor */
00231        switch (skytype) {
00232        case S_UNIF:
00233               normfactor = 1.0;
00234               break;
00235        case S_OVER:
00236               normfactor = 0.777778;
00237               break;
00238        case S_CLEAR:
00239               F2 = 0.274*(0.91 + 10.0*exp(-3.0*(PI/2.0-altitude)) +
00240                             0.45*sundir[2]*sundir[2]);
00241               normfactor = normsc()/F2/PI;
00242               break;
00243        case S_INTER:
00244               F2 = (2.739 + .9891*sin(.3119+2.6*altitude)) *
00245                      exp(-(PI/2.0-altitude)*(.4441+1.48*altitude));
00246               normfactor = normsc()/F2/PI;
00247               break;
00248        }
00249                                    /* Compute zenith brightness */
00250        if (u_zenith == -1)
00251               zenithbr /= normfactor*PI;
00252        else if (u_zenith == 0) {
00253               if (overcast)
00254                      zenithbr = 8.6*sundir[2] + .123;
00255               else
00256                      zenithbr = (1.376*turbidity-1.81)*tan(altitude)+0.38;
00257               if (skytype == S_INTER)
00258                      zenithbr = (zenithbr + 8.6*sundir[2] + .123)/2.0;
00259               if (zenithbr < 0.0)
00260                      zenithbr = 0.0;
00261               else
00262                      zenithbr *= 1000.0/SKYEFFICACY;
00263        }
00264                                    /* Compute horizontal radiance */
00265        groundbr = zenithbr*normfactor;
00266        printf("# Ground ambient level: %.1f\n", groundbr);
00267        if (!overcast && sundir[2] > 0.0 && (!u_solar || solarbr > 0.0)) {
00268               if (u_solar == -1)
00269                      solarbr /= 6e-5*sundir[2];
00270               else if (u_solar == 0) {
00271                      solarbr = 1.5e9/SUNEFFICACY *
00272                      (1.147 - .147/(sundir[2]>.16?sundir[2]:.16));
00273                      if (skytype == S_INTER)
00274                             solarbr *= 0.15;     /* fudge factor! */
00275               }
00276               groundbr += 6e-5/PI*solarbr*sundir[2];
00277        } else
00278               dosun = 0;
00279        groundbr *= gprefl;
00280 }
00281 
00282 
00283 void
00284 printsky(void)                     /* print out sky */
00285 {
00286        if (dosun) {
00287               printf("\nvoid light solar\n");
00288               printf("0\n0\n");
00289               printf("3 %.2e %.2e %.2e\n", solarbr, solarbr, solarbr);
00290               printf("\nsolar source sun\n");
00291               printf("0\n0\n");
00292               printf("4 %f %f %f 0.5\n", sundir[0], sundir[1], sundir[2]);
00293        }
00294        
00295        printf("\nvoid brightfunc skyfunc\n");
00296        printf("2 skybr skybright.cal\n");
00297        printf("0\n");
00298        if (overcast)
00299               printf("3 %d %.2e %.2e\n", skytype, zenithbr, groundbr);
00300        else
00301               printf("7 %d %.2e %.2e %.2e %f %f %f\n",
00302                             skytype, zenithbr, groundbr, F2,
00303                             sundir[0], sundir[1], sundir[2]);
00304 }
00305 
00306 
00307 void
00308 printdefaults(void)                /* print default values */
00309 {
00310        switch (skytype) {
00311        case S_OVER:
00312               printf("-c\t\t\t\t# Cloudy sky\n");
00313               break;
00314        case S_UNIF:
00315               printf("-u\t\t\t\t# Uniform cloudy sky\n");
00316               break;
00317        case S_INTER:
00318               if (dosun)
00319                      printf("+i\t\t\t\t# Intermediate sky with sun\n");
00320               else
00321                      printf("-i\t\t\t\t# Intermediate sky without sun\n");
00322               break;
00323        case S_CLEAR:
00324               if (dosun)
00325                      printf("+s\t\t\t\t# Sunny sky with sun\n");
00326               else
00327                      printf("-s\t\t\t\t# Sunny sky without sun\n");
00328               break;
00329        }
00330        printf("-g %f\t\t\t# Ground plane reflectance\n", gprefl);
00331        if (zenithbr > 0.0)
00332               printf("-b %f\t\t\t# Zenith radiance (watts/ster/m2\n", zenithbr);
00333        else
00334               printf("-t %f\t\t\t# Atmospheric turbidity\n", turbidity);
00335        printf("-a %f\t\t\t# Site latitude (degrees)\n", s_latitude*(180/PI));
00336        printf("-o %f\t\t\t# Site longitude (degrees)\n", s_longitude*(180/PI));
00337        printf("-m %f\t\t\t# Standard meridian (degrees)\n", s_meridian*(180/PI));
00338 }
00339 
00340 
00341 void
00342 userror(                    /* print usage error and quit */
00343        char  *msg
00344 )
00345 {
00346        if (msg != NULL)
00347               fprintf(stderr, "%s: Use error - %s\n", progname, msg);
00348        fprintf(stderr, "Usage: %s month day hour [options]\n", progname);
00349        fprintf(stderr, "   Or: %s -ang altitude azimuth [options]\n", progname);
00350        fprintf(stderr, "   Or: %s -defaults\n", progname);
00351        exit(1);
00352 }
00353 
00354 
00355 double
00356 normsc(void)                /* compute normalization factor (E0*F2/L0) */
00357 {
00358        static double  nfc[2][5] = {
00359                             /* clear sky approx. */
00360               {2.766521, 0.547665, -0.369832, 0.009237, 0.059229},
00361                             /* intermediate sky approx. */
00362               {3.5556, -2.7152, -1.3081, 1.0660, 0.60227},
00363        };
00364        register double  *nf;
00365        double  x, nsc;
00366        register int  i;
00367                                    /* polynomial approximation */
00368        nf = nfc[skytype==S_INTER];
00369        x = (altitude - PI/4.0)/(PI/4.0);
00370        nsc = nf[i=4];
00371        while (i--)
00372               nsc = nsc*x + nf[i];
00373 
00374        return(nsc);
00375 }
00376 
00377 
00378 int
00379 cvthour(                    /* convert hour string */
00380        char  *hs
00381 )
00382 {
00383        register char  *cp = hs;
00384        register int  i, j;
00385 
00386        if ( (tsolar = *cp == '+') ) cp++;        /* solar time? */
00387        while (isdigit(*cp)) cp++;
00388        if (*cp == ':')
00389               hour = atoi(hs) + atoi(++cp)/60.0;
00390        else {
00391               hour = atof(hs);
00392               if (*cp == '.') cp++;
00393        }
00394        while (isdigit(*cp)) cp++;
00395        if (!*cp)
00396               return(0);
00397        if (tsolar || !isalpha(*cp)) {
00398               fprintf(stderr, "%s: bad time format: %s\n", progname, hs);
00399               exit(1);
00400        }
00401        i = 0;
00402        do {
00403               for (j = 0; cp[j]; j++)
00404                      if (toupper(cp[j]) != tzone[i].zname[j])
00405                             break;
00406               if (!cp[j] && !tzone[i].zname[j]) {
00407                      s_meridian = tzone[i].zmer * (PI/180);
00408                      return(1);
00409               }
00410        } while (tzone[i++].zname[0]);
00411 
00412        fprintf(stderr, "%s: unknown time zone: %s\n", progname, cp);
00413        fprintf(stderr, "Known time zones:\n\t%s", tzone[0].zname);
00414        for (i = 1; tzone[i].zname[0]; i++)
00415               fprintf(stderr, " %s", tzone[i].zname);
00416        putc('\n', stderr);
00417        exit(1);
00418 }
00419 
00420 
00421 void
00422 printhead(           /* print command header */
00423        register int  ac,
00424        register char  **av
00425 )
00426 {
00427        putchar('#');
00428        while (ac--) {
00429               putchar(' ');
00430               fputs(*av++, stdout);
00431        }
00432        putchar('\n');
00433 }