solpos00.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #define RAD2DEG 57.295779513 /* converts from radians to degrees */
  2. #define DEG2RAD 0.0174532925 /* converts from degrees to radians */
  3. int dom2doy2(int year, int month, int day);
  4. /*============================================================================
  5. *
  6. * NAME: solpos.h
  7. *
  8. * Contains:
  9. * S_solpos (computes the solar position and intensity
  10. * from time and place)
  11. * INPUTS: (from posdata)
  12. * year, month, day, hour, minute, second,
  13. * latitude, longitude, timezone, interval
  14. * OPTIONAL: (from posdata; defaults from S_init function)
  15. * press DEFAULT 1013.0 (standard pressure)
  16. * temp DEFAULT 10.0 (standard temperature)
  17. * tilt DEFAULT 0.0 (horizontal panel)
  18. * aspect DEFAULT 180.0 (South-facing panel)
  19. * sbwid DEFAULT 7.6 (shadowband width)
  20. * sbrad DEFAULT 31.7 (shadowband radius)
  21. * sbsky DEFAULT 0.04 (shadowband sky factor)
  22. *
  23. * OUTPUTS: (posdata) daynum, amass, ampress, azim, cosinc,
  24. * elevref, etr, etrn, etrtilt, prime,
  25. * sbcf, sretr, ssetr, unprime, zenref
  26. *
  27. * RETURNS: Long int status code (defined in solpos.h)
  28. *
  29. * Usage:
  30. * In calling program, along with other 'includes', insert:
  31. *
  32. * #include "solpos.h"
  33. *
  34. * Martin Rymes
  35. * National Renewable Energy Laboratory
  36. * 25 March 1998
  37. *
  38. *----------------------------------------------------------------------------*/
  39. /*============================================================================
  40. *
  41. * Define the function codes
  42. *
  43. *----------------------------------------------------------------------------*/
  44. #define L_DOY 0x0001
  45. #define L_GEOM 0x0002
  46. #define L_ZENETR 0x0004
  47. #define L_SSHA 0x0008
  48. #define L_SBCF 0x0010
  49. #define L_TST 0x0020
  50. #define L_SRSS 0x0040
  51. #define L_SOLAZM 0x0080
  52. #define L_REFRAC 0x0100
  53. #define L_AMASS 0x0200
  54. #define L_PRIME 0x0400
  55. #define L_TILT 0x0800
  56. #define L_ETR 0x1000
  57. #define L_ALL 0xFFFF
  58. /*============================================================================
  59. *
  60. * Define the bit-wise masks for each function
  61. *
  62. *----------------------------------------------------------------------------*/
  63. #define S_DOY ( L_DOY )
  64. #define S_GEOM ( L_GEOM | S_DOY )
  65. #define S_ZENETR ( L_ZENETR | S_GEOM )
  66. #define S_SSHA ( L_SSHA | S_GEOM )
  67. #define S_SBCF ( L_SBCF | S_SSHA )
  68. #define S_TST ( L_TST | S_GEOM )
  69. #define S_SRSS ( L_SRSS | S_SSHA | S_TST )
  70. #define S_SOLAZM ( L_SOLAZM | S_ZENETR )
  71. #define S_REFRAC ( L_REFRAC | S_ZENETR )
  72. #define S_AMASS ( L_AMASS | S_REFRAC )
  73. #define S_PRIME ( L_PRIME | S_AMASS )
  74. #define S_TILT ( L_TILT | S_SOLAZM | S_REFRAC )
  75. #define S_ETR ( L_ETR | S_REFRAC )
  76. #define S_ALL ( L_ALL )
  77. /*============================================================================
  78. *
  79. * Enumerate the error codes
  80. * (Bit positions are from least significant to most significant)
  81. *
  82. *----------------------------------------------------------------------------*/
  83. /* Code Bit Parameter Range
  84. =============== === =================== ============= */
  85. enum
  86. { S_YEAR_ERROR, /* 0 year 1950 - 2050 */
  87. S_MONTH_ERROR, /* 1 month 1 - 12 */
  88. S_DAY_ERROR, /* 2 day-of-month 1 - 31 */
  89. S_DOY_ERROR, /* 3 day-of-year 1 - 366 */
  90. S_HOUR_ERROR, /* 4 hour 0 - 24 */
  91. S_MINUTE_ERROR, /* 5 minute 0 - 59 */
  92. S_SECOND_ERROR, /* 6 second 0 - 59 */
  93. S_TZONE_ERROR, /* 7 time zone -12 - 12 */
  94. S_INTRVL_ERROR, /* 8 interval (seconds) 0 - 28800 */
  95. S_LAT_ERROR, /* 9 latitude -90 - 90 */
  96. S_LON_ERROR, /* 10 longitude -180 - 180 */
  97. S_TEMP_ERROR, /* 11 temperature (deg. C) -100 - 100 */
  98. S_PRESS_ERROR, /* 12 pressure (millibars) 0 - 2000 */
  99. S_TILT_ERROR, /* 13 tilt -90 - 90 */
  100. S_ASPECT_ERROR, /* 14 aspect -360 - 360 */
  101. S_SBWID_ERROR, /* 15 shadow band width (cm) 1 - 100 */
  102. S_SBRAD_ERROR, /* 16 shadow band radius (cm) 1 - 100 */
  103. S_SBSKY_ERROR
  104. }; /* 17 shadow band sky factor -1 - 1 */
  105. struct posdata
  106. {
  107. /***** ALPHABETICAL LIST OF COMMON VARIABLES *****/
  108. /* Each comment begins with a 1-column letter code:
  109. I: INPUT variable
  110. O: OUTPUT variable
  111. T: TRANSITIONAL variable used in the algorithm,
  112. of interest only to the solar radiation
  113. modelers, and available to you because you
  114. may be one of them.
  115. The FUNCTION column indicates which sub-function
  116. within solpos must be switched on using the
  117. "function" parameter to calculate the desired
  118. output variable. All function codes are
  119. defined in the solpos.h file. The default
  120. S_ALL switch calculates all output variables.
  121. Multiple functions may be or'd to create a
  122. composite function switch. For example,
  123. (S_TST | S_SBCF). Specifying only the functions
  124. for required output variables may allow solpos
  125. to execute more quickly.
  126. The S_DOY mask works as a toggle between the
  127. input date represented as a day number (daynum)
  128. or as month and day. To set the switch (to
  129. use daynum input), the function is or'd; to
  130. clear the switch (to use month and day input),
  131. the function is inverted and and'd.
  132. For example:
  133. pdat->function |= S_DOY (sets daynum input)
  134. pdat->function &= ~S_DOY (sets month and day input)
  135. Whichever date form is used, S_solpos will
  136. calculate and return the variables(s) of the
  137. other form. See the soltest.c program for
  138. other examples. */
  139. /* VARIABLE I/O Function Description */
  140. /* ------------- ---- ---------- --------------------------------------- */
  141. int day; /* I/O: S_DOY Day of month (May 27 = 27, etc.)
  142. solpos will CALCULATE this by default,
  143. or will optionally require it as input
  144. depending on the setting of the S_DOY
  145. function switch. */
  146. int daynum; /* I/O: S_DOY Day number (day of year; Feb 1 = 32 )
  147. solpos REQUIRES this by default, but
  148. will optionally calculate it from
  149. month and day depending on the setting
  150. of the S_DOY function switch. */
  151. int function; /* I: Switch to choose functions for desired
  152. output. */
  153. int hour; /* I: Hour of day, 0 - 23, DEFAULT = 12 */
  154. int interval; /* I: Interval of a measurement period in
  155. seconds. Forces solpos to use the
  156. time and date from the interval
  157. midpoint. The INPUT time (hour,
  158. minute, and second) is assumed to
  159. be the END of the measurement
  160. interval. */
  161. int minute; /* I: Minute of hour, 0 - 59, DEFAULT = 0 */
  162. int month; /* I/O: S_DOY Month number (Jan = 1, Feb = 2, etc.)
  163. solpos will CALCULATE this by default,
  164. or will optionally require it as input
  165. depending on the setting of the S_DOY
  166. function switch. */
  167. int second; /* I: Second of minute, 0 - 59, DEFAULT = 0 */
  168. int year; /* I: 4-digit year (2-digit year is NOT
  169. allowed */
  170. int time_updated; /* recalculate time-dependent variables */
  171. int longitude_updated; /* recalculate longitude-dependent variables */
  172. /***** FLOATS *****/
  173. float amass; /* O: S_AMASS Relative optical airmass */
  174. float ampress; /* O: S_AMASS Pressure-corrected airmass */
  175. float aspect; /* I: Azimuth of panel surface (direction it
  176. faces) N=0, E=90, S=180, W=270,
  177. DEFAULT = 180 */
  178. float azim; /* O: S_SOLAZM Solar azimuth angle: N=0, E=90, S=180,
  179. W=270 */
  180. float cosinc; /* O: S_TILT Cosine of solar incidence angle on
  181. panel */
  182. float coszen; /* O: S_REFRAC Cosine of refraction corrected solar
  183. zenith angle */
  184. float dayang; /* T: S_GEOM Day angle (daynum*360/year-length)
  185. degrees */
  186. float declin; /* T: S_GEOM Declination--zenith angle of solar noon
  187. at equator, degrees NORTH */
  188. float eclong; /* T: S_GEOM Ecliptic longitude, degrees */
  189. float ecobli; /* T: S_GEOM Obliquity of ecliptic */
  190. float ectime; /* T: S_GEOM Time of ecliptic calculations */
  191. float elevetr; /* O: S_ZENETR Solar elevation, no atmospheric
  192. correction (= ETR) */
  193. float elevref; /* O: S_REFRAC Solar elevation angle,
  194. deg. from horizon, refracted */
  195. float eqntim; /* T: S_TST Equation of time (TST - LMT), minutes */
  196. float erv; /* T: S_GEOM Earth radius vector
  197. (multiplied to solar constant) */
  198. float etr; /* O: S_ETR Extraterrestrial (top-of-atmosphere)
  199. W/sq m global horizontal solar
  200. irradiance */
  201. float etrn; /* O: S_ETR Extraterrestrial (top-of-atmosphere)
  202. W/sq m direct normal solar
  203. irradiance */
  204. float etrtilt; /* O: S_TILT Extraterrestrial (top-of-atmosphere)
  205. W/sq m global irradiance on a tilted
  206. surface */
  207. float gmst; /* T: S_GEOM Greenwich mean sidereal time, hours */
  208. float hrang; /* T: S_GEOM Hour angle--hour of sun from solar noon,
  209. degrees WEST */
  210. float julday; /* T: S_GEOM Julian Day of 1 JAN 2000 minus
  211. 2,400,000 days (in order to regain
  212. single precision) */
  213. float latitude; /* I: Latitude, degrees north (south negative) */
  214. float longitude; /* I: Longitude, degrees east (west negative) */
  215. float lmst; /* T: S_GEOM Local mean sidereal time, degrees */
  216. float mnanom; /* T: S_GEOM Mean anomaly, degrees */
  217. float mnlong; /* T: S_GEOM Mean longitude, degrees */
  218. float rascen; /* T: S_GEOM Right ascension, degrees */
  219. float press; /* I: Surface pressure, millibars, used for
  220. refraction correction and ampress */
  221. float prime; /* O: S_PRIME Factor that normalizes Kt, Kn, etc. */
  222. float sbcf; /* O: S_SBCF Shadow-band correction factor */
  223. float sbwid; /* I: Shadow-band width (cm) */
  224. float sbrad; /* I: Shadow-band radius (cm) */
  225. float sbsky; /* I: Shadow-band sky factor */
  226. float solcon; /* I: Solar constant (NREL uses 1367 W/sq m) */
  227. float ssha; /* T: S_SRHA Sunset(/rise) hour angle, degrees */
  228. float sretr; /* O: S_SRSS Sunrise time, minutes from midnight,
  229. local, WITHOUT refraction */
  230. float ssetr; /* O: S_SRSS Sunset time, minutes from midnight,
  231. local, WITHOUT refraction */
  232. float temp; /* I: Ambient dry-bulb temperature, degrees C,
  233. used for refraction correction */
  234. float tilt; /* I: Degrees tilt from horizontal of panel */
  235. float timezone; /* I: Time zone, east (west negative).
  236. USA: Mountain = -7, Central = -6, etc. */
  237. float tst; /* T: S_TST True solar time, minutes from midnight */
  238. float tstfix; /* T: S_TST True solar time - local standard time */
  239. float unprime; /* O: S_PRIME Factor that denormalizes Kt', Kn', etc. */
  240. float utime; /* T: S_GEOM Universal (Greenwich) standard time */
  241. float zenetr; /* T: S_ZENETR Solar zenith angle, no atmospheric
  242. correction (= ETR) */
  243. float zenref; /* O: S_REFRAC Solar zenith angle, deg. from zenith,
  244. refracted */
  245. };
  246. /* For users that wish to access individual functions, the following table
  247. lists all output and transition variables, the L_ mask for the function
  248. that calculates them, and all the input variables required by that function.
  249. The function variable is set to the L_ mask, which will force S_solpos to
  250. only call the required function. L_ masks may be ORed as desired.
  251. VARIABLE Mask Required Variables
  252. --------- ---------- ---------------------------------------
  253. amass L_AMASS zenref, press
  254. ampress L_AMASS zenref, press
  255. azim L_SOLAZM elevetr, declin, latitude, hrang
  256. cosinc L_TILT azim, aspect, tilt, zenref, coszen,etrn
  257. coszen L_REFRAC elevetr, press, temp
  258. dayang L_GEOM All date, time, and location inputs
  259. declin L_GEOM All date, time, and location inputs
  260. eclong L_GEOM All date, time, and location inputs
  261. ecobli L_GEOM All date, time, and location inputs
  262. ectime L_GEOM All date, time, and location inputs
  263. elevetr L_ZENETR declin, latitude, hrang
  264. elevref L_REFRAC elevetr, press, temp
  265. eqntim L_TST hrang, hour, minute, second, interval
  266. erv L_GEOM All date, time, and location inputs
  267. etr L_ETR coszen, solcon, erv
  268. etrn L_ETR coszen, solcon, erv
  269. etrtilt L_TILT azim, aspect, tilt, zenref, coszen, etrn
  270. gmst L_GEOM All date, time, and location inputs
  271. hrang L_GEOM All date, time, and location inputs
  272. julday L_GEOM All date, time, and location inputs
  273. lmst L_GEOM All date, time, and location inputs
  274. mnanom L_GEOM All date, time, and location inputs
  275. mnlong L_GEOM All date, time, and location inputs
  276. rascen L_GEOM All date, time, and location inputs
  277. prime L_PRIME amass
  278. sbcf L_SBCF latitude, declin, ssha, sbwid, sbrad, sbsky
  279. ssha L_SRHA latitude, declin
  280. sretr L_SRSS ssha, tstfix
  281. ssetr L_SRSS ssha, tstfix
  282. tst L_TST hrang, hour, minute, second, interval
  283. tstfix L_TST hrang, hour, minute, second, interval
  284. unprime L_PRIME amass
  285. utime L_GEOM All date, time, and location inputs
  286. zenetr L_ZENETR declination, latitude, hrang
  287. zenref L_REFRAC elevetr, press, temp
  288. */
  289. /*============================================================================
  290. * Long int function S_solpos, adapted from the NREL VAX solar libraries
  291. *
  292. * This function calculates the apparent solar position and intensity
  293. * (theoretical maximum solar energy) based on the date, time, and
  294. * location on Earth. (DEFAULT values are from the optional S_posinit
  295. * function.)
  296. *
  297. * Requires:
  298. * Date and time:
  299. * year
  300. * month (optional without daynum)
  301. * day (optional without daynum)
  302. * daynum
  303. * hour
  304. * minute
  305. * second
  306. * Location:
  307. * latitude
  308. * longitude
  309. * Location/time adjuster:
  310. * timezone
  311. * Atmospheric pressure and temperature:
  312. * press DEFAULT 1013.0 mb
  313. * temp DEFAULT 10.0 degrees C
  314. * Tilt of flat surface that receives solar energy:
  315. * aspect DEFAULT 180 (South)
  316. * tilt DEFAULT 0 (Horizontal)
  317. * Shadow band parameters:
  318. * sbwid DEFAULT 7.6 cm
  319. * sbrad DEFAULT 31.7 cm
  320. * sbsky DEFAULT 0.04
  321. * Functionality
  322. * function DEFAULT S_ALL (all output parameters computed)
  323. *
  324. * Returns:
  325. * everything defined at the top of this listing.
  326. *----------------------------------------------------------------------------*/
  327. long S_solpos(struct posdata *pdat);
  328. /*============================================================================
  329. * Void function S_init
  330. *
  331. * This function initiates all of the input functions to S_Solpos().
  332. * NOTE: This function is optional if you initialize all input parameters
  333. * in your calling code.
  334. *
  335. * Requires: Pointer to a posdata structure, members of which are
  336. * initialized.
  337. *
  338. * Returns: Void
  339. *
  340. *----------------------------------------------------------------------------*/
  341. void S_init(struct posdata *pdat);
  342. /*============================================================================
  343. * Void function S_decode
  344. *
  345. * This function decodes the error codes from S_solpos return value
  346. *
  347. * INPUTS: Long integer S_solpos return value, struct posdata*
  348. *
  349. * OUTPUTS: Descriptive text of errors to stderr
  350. *----------------------------------------------------------------------------*/
  351. void S_decode(long code, struct posdata *pdat);