main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /****************************************************************************
  2. *
  3. * MODULE: r.sunhours
  4. * AUTHOR(S): Markus Metz
  5. * PURPOSE: Calculates solar azimuth and angle, and
  6. * sunshine hours (also called daytime period)
  7. * COPYRIGHT: (C) 2010-2013 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. *****************************************************************************/
  14. /* TODO: always use solpos if time is Greenwich standard time */
  15. #define MAIN
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include <grass/gis.h>
  21. #include <grass/raster.h>
  22. #include <grass/gprojects.h>
  23. #include <grass/glocale.h>
  24. #include "solpos00.h"
  25. void set_solpos_time(struct posdata *, int, int, int, int, int, int);
  26. void set_solpos_longitude(struct posdata *, double );
  27. int roundoff(double *);
  28. int main(int argc, char *argv[])
  29. {
  30. struct GModule *module;
  31. struct
  32. {
  33. struct Option *elev, *azimuth, *sunhours, *year,
  34. *month, *day, *hour, *minutes, *seconds;
  35. struct Flag *lst_time, *no_solpos;
  36. } parm;
  37. struct Cell_head window;
  38. FCELL *elevbuf, *azimuthbuf, *sunhourbuf;
  39. struct History hist;
  40. /* projection information of input map */
  41. struct Key_Value *in_proj_info, *in_unit_info;
  42. struct pj_info iproj; /* input map proj parameters */
  43. struct pj_info oproj; /* output map proj parameters */
  44. char *elev_name, *azimuth_name, *sunhour_name;
  45. int elev_fd, azimuth_fd, sunhour_fd;
  46. double ha, ha_cos, s_gamma, s_elevation, s_azimuth;
  47. double s_declination, sd_sin, sd_cos;
  48. double se_sin, sa_cos;
  49. double east, east_ll, north, north_ll;
  50. double north_gc, north_gc_sin, north_gc_cos; /* geocentric latitude */
  51. double ba2;
  52. int year, month, day, hour, minutes, seconds;
  53. int doy; /* day of year */
  54. int row, col, nrows, ncols;
  55. int do_reproj = 0;
  56. int lst_time = 1;
  57. int use_solpos = 0;
  58. struct posdata pd;
  59. G_gisinit(argv[0]);
  60. module = G_define_module();
  61. G_add_keyword(_("raster"));
  62. G_add_keyword(_("solar"));
  63. module->label = _("Calculates solar elevation, solar azimuth, and sun hours.");
  64. module->description = _("Solar elevation: the angle between the direction of the geometric center "
  65. "of the sun's apparent disk and the (idealized) horizon. "
  66. "Solar azimuth: the angle from due north in clockwise direction.");
  67. parm.elev = G_define_standard_option(G_OPT_R_OUTPUT);
  68. parm.elev->key = "elevation";
  69. parm.elev->label = _("Output raster map with solar elevation angle");
  70. parm.elev->required = NO;
  71. parm.azimuth = G_define_standard_option(G_OPT_R_OUTPUT);
  72. parm.azimuth->key = "azimuth";
  73. parm.azimuth->label = _("Output raster map with solar azimuth angle");
  74. parm.azimuth->required = NO;
  75. parm.sunhours = G_define_standard_option(G_OPT_R_OUTPUT);
  76. parm.sunhours->key = "sunhour";
  77. parm.sunhours->label = _("Output raster map with sunshine hours");
  78. parm.sunhours->description = _("Sunshine hours require solpos and Greenwich standard time");
  79. parm.sunhours->required = NO;
  80. parm.year = G_define_option();
  81. parm.year->key = "year";
  82. parm.year->type = TYPE_INTEGER;
  83. parm.year->required = YES;
  84. parm.year->description = _("Year");
  85. parm.year->options = "1950-2050";
  86. parm.year->guisection = _("Time");
  87. parm.month = G_define_option();
  88. parm.month->key = "month";
  89. parm.month->type = TYPE_INTEGER;
  90. parm.month->required = NO;
  91. parm.month->label = _("Month");
  92. parm.month->description = _("If not given, day is interpreted as day of the year");
  93. parm.month->options = "1-12";
  94. parm.month->guisection = _("Time");
  95. parm.day = G_define_option();
  96. parm.day->key = "day";
  97. parm.day->type = TYPE_INTEGER;
  98. parm.day->required = YES;
  99. parm.day->description = _("Day");
  100. parm.day->options = "1-366";
  101. parm.day->guisection = _("Time");
  102. parm.hour = G_define_option();
  103. parm.hour->key = "hour";
  104. parm.hour->type = TYPE_INTEGER;
  105. parm.hour->required = NO;
  106. parm.hour->description = _("Hour");
  107. parm.hour->options = "0-24";
  108. parm.hour->answer = "12";
  109. parm.hour->guisection = _("Time");
  110. parm.minutes = G_define_option();
  111. parm.minutes->key = "minute";
  112. parm.minutes->type = TYPE_INTEGER;
  113. parm.minutes->required = NO;
  114. parm.minutes->description = _("Minutes");
  115. parm.minutes->options = "0-60";
  116. parm.minutes->answer = "0";
  117. parm.minutes->guisection = _("Time");
  118. parm.seconds = G_define_option();
  119. parm.seconds->key = "second";
  120. parm.seconds->type = TYPE_INTEGER;
  121. parm.seconds->required = NO;
  122. parm.seconds->description = _("Seconds");
  123. parm.seconds->options = "0-60";
  124. parm.seconds->answer = "0";
  125. parm.seconds->guisection = _("Time");
  126. parm.lst_time = G_define_flag();
  127. parm.lst_time->key = 't';
  128. parm.lst_time->description = _("Time is local sidereal time, not Greenwich standard time");
  129. parm.no_solpos = G_define_flag();
  130. parm.no_solpos->key = 's';
  131. parm.no_solpos->description = _("Do not use solpos algorithm of NREL");
  132. if (G_parser(argc, argv))
  133. exit(EXIT_FAILURE);
  134. G_get_window(&window);
  135. /* require at least one output */
  136. elev_name = parm.elev->answer;
  137. azimuth_name = parm.azimuth->answer;
  138. sunhour_name = parm.sunhours->answer;
  139. if (!elev_name && !azimuth_name && !sunhour_name)
  140. G_fatal_error(_("No output requested, exiting."));
  141. year = atoi(parm.year->answer);
  142. if (parm.month->answer)
  143. month = atoi(parm.month->answer);
  144. else
  145. month = -1;
  146. day = atoi(parm.day->answer);
  147. hour = atoi(parm.hour->answer);
  148. minutes = atoi(parm.minutes->answer);
  149. seconds = atoi(parm.seconds->answer);
  150. lst_time = (parm.lst_time->answer != 0);
  151. use_solpos = (parm.no_solpos->answer == 0);
  152. /* init variables */
  153. ha = 180;
  154. ha_cos = 0;
  155. sd_cos = 0;
  156. sd_sin = 1;
  157. north_gc_cos = 0;
  158. north_gc_sin = 1;
  159. se_sin = 0;
  160. if (use_solpos && lst_time) {
  161. G_warning(_("NREL solpos algorithm uses Greenwich standard time."));
  162. G_warning(_("Time will be interpreted as Greenwich standard time."));
  163. lst_time = 0;
  164. }
  165. if (!use_solpos) {
  166. if (lst_time)
  167. G_message(_("Time will be interpreted as local sidereal time."));
  168. else
  169. G_message(_("Time will be interpreted as Greenwich standard time."));
  170. if (sunhour_name)
  171. G_fatal_error(_("Sunshine hours require NREL solpos."));
  172. }
  173. if ((G_projection() != PROJECTION_LL)) {
  174. if (window.proj == 0)
  175. G_fatal_error(_("Current projection is x,y (undefined)."));
  176. do_reproj = 1;
  177. /* read current projection info */
  178. if ((in_proj_info = G_get_projinfo()) == NULL)
  179. G_fatal_error(_("Cannot get projection info of current location"));
  180. if ((in_unit_info = G_get_projunits()) == NULL)
  181. G_fatal_error(_("Cannot get projection units of current location"));
  182. if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
  183. G_fatal_error(_("Cannot get projection key values of current location"));
  184. G_free_key_value(in_proj_info);
  185. G_free_key_value(in_unit_info);
  186. /* output projection to lat/long w/ same ellipsoid as input */
  187. oproj.zone = 0;
  188. oproj.meters = 1.;
  189. sprintf(oproj.proj, "ll");
  190. if ((oproj.pj = pj_latlong_from_proj(iproj.pj)) == NULL)
  191. G_fatal_error(_("Unable to update lat/long projection parameters"));
  192. }
  193. /* always init pd */
  194. S_init(&pd);
  195. pd.function = S_GEOM;
  196. if (use_solpos) {
  197. pd.function = S_ZENETR;
  198. if (azimuth_name)
  199. pd.function = S_SOLAZM;
  200. if (sunhour_name)
  201. pd.function |= S_SRSS;
  202. }
  203. if (month == -1)
  204. doy = day;
  205. else
  206. doy = dom2doy2(year, month, day);
  207. set_solpos_time(&pd, year, 1, doy, hour, minutes, seconds);
  208. set_solpos_longitude(&pd, 0);
  209. pd.latitude = 0;
  210. S_solpos(&pd);
  211. if (lst_time) {
  212. /* hour angle */
  213. /***************************************************************
  214. * The hour angle of a point on the Earth's surface is the angle
  215. * through which the earth would turn to bring the meridian of
  216. * the point directly under the sun. This angular displacement
  217. * represents time (1 hour = 15 degrees).
  218. * The hour angle is negative in the morning, zero at 12:00,
  219. * and positive in the afternoon
  220. ***************************************************************/
  221. ha = 15.0 * (hour + minutes / 60.0 + seconds / 3600.0) - 180.;
  222. G_debug(1, "Solar hour angle, degrees: %.2f", ha);
  223. ha *= DEG2RAD;
  224. ha_cos = cos(ha);
  225. roundoff(&ha_cos);
  226. }
  227. if (!use_solpos) {
  228. /* sun declination */
  229. /***************************************************************
  230. * The declination of the sun is the angle between
  231. * the rays of the sun and the plane of the Earth's equator.
  232. ***************************************************************/
  233. s_gamma = (2 * M_PI * (doy - 1)) / 365;
  234. G_debug(1, "fractional year in radians: %.2f", s_gamma);
  235. /* sun declination for day of year with Fourier series representation
  236. * NOTE: based on 1950, override with solpos */
  237. s_declination = (0.006918 - 0.399912 * cos(s_gamma) + 0.070257 * sin(s_gamma) -
  238. 0.006758 * cos(2 * s_gamma) + 0.000907 * sin(2 * s_gamma) -
  239. 0.002697 * cos(3 * s_gamma) + 0.00148 * sin(3 * s_gamma));
  240. G_debug(1, "sun declination: %.5f", s_declination * RAD2DEG);
  241. G_debug(1, "sun declination (solpos): %.5f", pd.declin);
  242. if (lst_time) {
  243. north_ll = (window.north + window.south) / 2;
  244. east_ll = (window.east + window.west) / 2;
  245. if (do_reproj) {
  246. if (pj_do_proj(&east_ll, &north_ll, &iproj, &oproj) < 0)
  247. G_fatal_error(_("Error in pj_do_proj (projection of input coordinate pair)"));
  248. }
  249. pd.timezone = east_ll / 15.;
  250. pd.time_updated = 1;
  251. set_solpos_longitude(&pd, east_ll);
  252. G_debug(1, "fake timezone: %.2f", pd.timezone);
  253. S_solpos(&pd);
  254. G_debug(1, "Solar hour angle (solpos), degrees: %.2f", pd.hrang);
  255. }
  256. /* always use solpos sun declination */
  257. s_declination = pd.declin * DEG2RAD;
  258. sd_sin = sin(s_declination);
  259. roundoff(&sd_sin);
  260. sd_cos = cos(s_declination);
  261. roundoff(&sd_cos);
  262. G_debug(1, "sun declination (solpos): %.5f", s_declination * RAD2DEG);
  263. if (0 && lst_time) {
  264. pd.timezone = 0;
  265. pd.time_updated = pd.longitude_updated = 1;
  266. S_solpos(&pd);
  267. }
  268. }
  269. if (elev_name) {
  270. if ((elev_fd = Rast_open_new(elev_name, FCELL_TYPE)) < 0)
  271. G_fatal_error(_("Unable to create raster map <%s>"), elev_name);
  272. elevbuf = Rast_allocate_f_buf();
  273. }
  274. else {
  275. elevbuf = NULL;
  276. elev_fd = -1;
  277. }
  278. if (azimuth_name) {
  279. if ((azimuth_fd = Rast_open_new(azimuth_name, FCELL_TYPE)) < 0)
  280. G_fatal_error(_("Unable to create raster map <%s>"), azimuth_name);
  281. azimuthbuf = Rast_allocate_f_buf();
  282. }
  283. else {
  284. azimuthbuf = NULL;
  285. azimuth_fd = -1;
  286. }
  287. if (sunhour_name) {
  288. if ((sunhour_fd = Rast_open_new(sunhour_name, FCELL_TYPE)) < 0)
  289. G_fatal_error(_("Unable to create raster map <%s>"), sunhour_name);
  290. sunhourbuf = Rast_allocate_f_buf();
  291. }
  292. else {
  293. sunhourbuf = NULL;
  294. sunhour_fd = -1;
  295. }
  296. if (elev_name && azimuth_name) {
  297. G_message(_("Calculating solar elevation and azimuth..."));
  298. }
  299. else if (elev_name) {
  300. G_message(_("Calculating solar elevation..."));
  301. }
  302. else if (azimuth_name) {
  303. G_message(_("Calculating solar azimuth..."));
  304. }
  305. nrows = Rast_window_rows();
  306. ncols = Rast_window_cols();
  307. ba2 = 6356752.3142 / 6378137.0;
  308. ba2 = ba2 * ba2;
  309. for (row = 0; row < nrows; row++) {
  310. G_percent(row, nrows, 2);
  311. /* get cell center northing */
  312. north = window.north - (row + 0.5) * window.ns_res;
  313. north_ll = north;
  314. for (col = 0; col < ncols; col++) {
  315. long int retval;
  316. s_elevation = s_azimuth = -1.;
  317. /* get cell center easting */
  318. east = window.west + (col + 0.5) * window.ew_res;
  319. east_ll = east;
  320. if (do_reproj) {
  321. north_ll = north;
  322. if (pj_do_proj(&east_ll, &north_ll, &iproj, &oproj) < 0)
  323. G_fatal_error(_("Error in pj_do_proj (projection of input coordinate pair)"));
  324. }
  325. /* geocentric latitude */
  326. north_gc = atan(ba2 * tan(DEG2RAD * north_ll));
  327. north_gc_sin = sin(north_gc);
  328. roundoff(&north_gc_sin);
  329. north_gc_cos = cos(north_gc);
  330. roundoff(&north_gc_cos);
  331. if (!lst_time) {
  332. set_solpos_longitude(&pd, east_ll);
  333. pd.latitude = north_gc * RAD2DEG;
  334. retval = S_solpos(&pd);
  335. S_decode(retval, &pd);
  336. G_debug(3, "solpos hour angle: %.5f", pd.hrang);
  337. }
  338. /* solar elevation angle */
  339. if (!use_solpos) {
  340. if (!lst_time) {
  341. ha = pd.hrang;
  342. ha_cos = cos(ha * DEG2RAD);
  343. roundoff(&ha_cos);
  344. }
  345. se_sin = ha_cos * sd_cos * north_gc_cos + sd_sin * north_gc_sin;
  346. roundoff(&se_sin);
  347. s_elevation = RAD2DEG * asin(se_sin);
  348. }
  349. else /* use_solpos && !lst_time */
  350. s_elevation = pd.elevetr;
  351. if (elev_name)
  352. elevbuf[col] = s_elevation;
  353. if (azimuth_name) {
  354. /* solar azimuth angle */
  355. if (!use_solpos) {
  356. sa_cos = (se_sin * north_gc_sin - sd_sin) /
  357. (cos(DEG2RAD * s_elevation) * north_gc_cos);
  358. roundoff(&sa_cos);
  359. s_azimuth = RAD2DEG * acos(sa_cos);
  360. /* morning */
  361. s_azimuth = 180. - RAD2DEG * acos(sa_cos);
  362. if (ha > 0) /* afternoon */
  363. s_azimuth = 360.0 - s_azimuth;
  364. }
  365. else
  366. s_azimuth = pd.azim;
  367. azimuthbuf[col] = s_azimuth;
  368. }
  369. if (sunhour_name) {
  370. sunhourbuf[col] = (pd.ssetr - pd.sretr) / 60.;
  371. if (sunhourbuf[col] > 24.)
  372. sunhourbuf[col] = 24.;
  373. if (sunhourbuf[col] < 0.)
  374. sunhourbuf[col] = 0.;
  375. }
  376. }
  377. if (elev_name)
  378. Rast_put_f_row(elev_fd, elevbuf);
  379. if (azimuth_name)
  380. Rast_put_f_row(azimuth_fd, azimuthbuf);
  381. if (sunhour_name)
  382. Rast_put_f_row(sunhour_fd, sunhourbuf);
  383. }
  384. G_percent(1, 1, 2);
  385. if (elev_name) {
  386. Rast_close(elev_fd);
  387. /* writing history file */
  388. Rast_short_history(elev_name, "raster", &hist);
  389. Rast_command_history(&hist);
  390. Rast_write_history(elev_name, &hist);
  391. }
  392. if (azimuth_name) {
  393. Rast_close(azimuth_fd);
  394. /* writing history file */
  395. Rast_short_history(azimuth_name, "raster", &hist);
  396. Rast_command_history(&hist);
  397. Rast_write_history(azimuth_name, &hist);
  398. }
  399. if (sunhour_name) {
  400. Rast_close(sunhour_fd);
  401. /* writing history file */
  402. Rast_short_history(sunhour_name, "raster", &hist);
  403. Rast_command_history(&hist);
  404. Rast_write_history(sunhour_name, &hist);
  405. }
  406. G_done_msg(" ");
  407. exit(EXIT_SUCCESS);
  408. }
  409. void set_solpos_time(struct posdata *pdat, int year, int month, int day,
  410. int hour, int minute, int second)
  411. {
  412. pdat->year = year;
  413. pdat->month = month;
  414. pdat->day = day;
  415. pdat->daynum = day;
  416. pdat->hour = hour;
  417. pdat->minute = minute;
  418. pdat->second = second;
  419. pdat->timezone = 0;
  420. pdat->time_updated = 1;
  421. pdat->longitude_updated = 1;
  422. }
  423. void set_solpos_longitude(struct posdata *pdat, double longitude)
  424. {
  425. pdat->longitude = longitude;
  426. pdat->longitude_updated = 1;
  427. }
  428. int roundoff(double *x)
  429. {
  430. /* watch out for the roundoff errors */
  431. if (fabs(*x) > 1.0) {
  432. if (*x > 0.0)
  433. *x = 1.0;
  434. else
  435. *x = -1.0;
  436. return 1;
  437. }
  438. return 0;
  439. }