Module crepyscule
[hide private]
[frames] | no frames]

Source Code for Module crepyscule

  1  #!/usr/bin/env python 
  2  # -*- coding: iso-8859-1 -*- 
  3   
  4  # Copyright  2005,2008  Miguel Tremblay 
  5   
  6  # This program is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 3 of the License, or 
  9  # (at your option) any later version. 
 10   
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15   
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not see  <http://www.gnu.org/licenses/>. 
 18  ############################################################################ 
 19   
 20  """ 
 21  Package to get different information related to the sun 
 22  going around the Earth. It is based on one or two locations 
 23  on the Earth, based on the lat/lon and, optional, the different 
 24  time zone and summer time codes.  
 25  Currently the following information is given by 
 26  this code: sunrise, sunset, sun altitude, daylight variation, 
 27  solar flux and twilight. 
 28   
 29  crepyscule could be use in two modes: for graphics and as a module. 
 30   1. The U{python module numpy<http://numpy.scipy.org/>} is required. 
 31   2. The graphic mode required other modules and is more tedious to 
 32      install. U{rpy<http://rpy.sourceforge.net/>} for python and U{GDD in R<http://www.rosuda.org/R/GDD/>} are required. 
 33   3. Starting from version 1.0.0, it is possible to use crepyscule 
 34      as a module, one only needs to uncompress 
 35      crepyscule and could import it and use it directly. It only 
 36      uses standard python package and numpy. 
 37   
 38  Project home page is U{http://ptaff.ca/crepyscule/} 
 39   
 40  Note: 
 41    - The file /usr/share/apps/kstars/Cities.dat from software kdeedu 
 42      was used as a base for informations about time zones.  The list of 
 43      available time zone can be found in crepyscule_summer_time.py 
 44   
 45   - Name:        crepyscule.py 
 46   - Author:      U{Miguel Tremblay<http://ptaff.ca/miguel/>} 
 47   - Date:        February 12th 2008        
 48  """ 
 49   
 50  import os 
 51  import sys 
 52  if 'LANGUAGE' not in os.environ: 
 53     os.environ.setdefault("LANGUAGE","en") 
 54  elif "en" not in os.environ["LANGUAGE"] and "fr" not in os.environ["LANGUAGE"] : 
 55     os.environ["LANGUAGE"] = "en" 
 56   
 57  import crepyscule_graphic 
 58  import crepyscule_tools 
 59   
 60  # Set the place where to fetch languages dependant messages 
 61  import gettext 
 62  t = gettext.translation('crepyscule', sys.path[0] + '/locale') 
 63  _ = t.gettext 
 64   
 65   
 66   
 67  VERSION = "1.1.1" 
 68   
 69   
70 -def crepyscule(dDataRequest, fLat1, fLon1, ctime, \ 71 fUTC1=0, sFilename='', \ 72 sSummerTime1='', fLat2= None, \ 73 fLon2=None, sSummerTime2='', fUTC2=0, \ 74 sBackgroundColor='white'):
75 """ 76 This is the main function for using crepyscule in command line. 77 The result is png images and/or text files containing the requested 78 information about the Sun. 79 80 @type dDataRequest: dictionnary 81 @param dDataRequest: Contains what file(s) or image(s) 82 should be created. See the command line options. Keys are 83 options with '--' and values are booleans. 84 Keys are: 'GRAPH-SUNRISE','GRAPH-ALTITUDE' 85 'GRAPH-DAYLIGHT_VARIATION','GRAPH-SOLAR_FLUX', 'GRAPH-TWILIGHT_LENGTH', 86 'TEXT-SUNRISE' 87 @type fLat1: float 88 @param fLat1: latitude of the first place. Latitude is in decimal 89 degrees. Example: 30°30' should be 30.5 90 @type fLon1: float 91 @param fLon1: longitude of the first place. Longitude is in decimal 92 degrees. Example: 30°30' should be 30.5. West longitude are negative 93 East longitude are positive. 94 @type ctime: float 95 @param ctime: Any ctime in the day for the wanted value. If a graphic is 96 created and there is only one place, a line is drawn with the values 97 written at the intersection for this specific day. 98 @type fUTC1: float 99 @param fUTC1: Time to add/substract for each time values for the first place. 100 Usually used for UTC value of place. 101 @type sFilename: string 102 @param sFilename: Filename for the images or text. Filename must ends 103 with 'png' extension. The sunrise/sunset image is written with this 104 filename. This terminology is used:: 105 sunrise/sunset: filename.png 106 altitude: filename_alt.png 107 daylight variation: filename_delta_t.png 108 solar flux: filename_sf.png 109 sunrise/sunset text file: filename.txt 110 @type sSummerTime1: string 111 @param sSummerTime1: This variable is a 2 letters code indicating 112 when is the daylight saving time (DST) starting and ending in the year 113 for the first place. 114 Default is no DST. For a list of DST code, 115 see L{crepyscule_summer_time.lTimezone}. 116 @type fLat2: float 117 @param fLat2: latitude of the second place. See fLat1. 118 @type fLon2: float 119 @param fLon2: longitude of the second place. See fLon1. 120 @type sSummerTime2: string 121 @param sSummerTime2: This variable is a 2 letters code indicating 122 when is the daylight saving time (DST) starting and ending in the year 123 for the seconde place. 124 Default is no DST. For a list of DST code, 125 see L{crepyscule_summer_time.lTimezone}. 126 @type fUTC2: float 127 @param fUTC2: Time to add/substract for each time values for the second place. 128 @type sBackgroundColor: string 129 @param sBackgroundColor: Color that will be used for the background 130 color for the generated graphics. See U{Chart of R colors<http://research.stowers-institute.org/efg/R/Color/Chart/>} 131 """ 132 133 # If no filename given, use lat and lon 134 if sFilename == '': 135 sFilename = 'Lat' +str(fLat1) + '_Lon' + str(fLon1) + '.png' 136 137 (lSunrise, lSunset, lSunAltitude, tToday) = \ 138 crepyscule_tools.get_one_year_data(fLat1, fLon1, \ 139 ctime, fUTC1, \ 140 sSummerTime1) 141 142 lDaylength = crepyscule_tools.get_daylength(ctime, fLat1, fLon1) 143 lDateISO8601 = crepyscule_tools.get_one_year_of_iso8601(ctime) 144 145 # Check if there is a 2nd place to compute lat and lon 146 if fLon2 != None and fLat2 != None: 147 (lSunrise2, lSunset2, lSunAltitude2, tTmp) = \ 148 crepyscule_tools.get_one_year_data(fLat2, fLon2, \ 149 ctime, fUTC2, sSummerTime2) 150 lDaylength2 = crepyscule_tools.get_daylength(ctime, fLat2, fLon2) 151 else: 152 lSunrise2 = lSunset2 = lSunAltitude2 = lDaylength2 = None 153 154 # Graphics 155 if dDataRequest["GRAPH-SUNRISE"]: 156 print 'filename', sFilename 157 crepyscule_graphic.plot_sunset_sunrise(lSunrise, lSunset, lDaylength, \ 158 lDateISO8601,\ 159 sFilename, tToday, \ 160 sBackgroundColor, lSunrise2,\ 161 lSunset2, lDaylength2) 162 if dDataRequest["GRAPH-ALTITUDE"]: 163 crepyscule_graphic.plot_altitude(tToday, lSunAltitude,lDateISO8601,\ 164 sFilename, sBackgroundColor, \ 165 lSunAltitude2) 166 if dDataRequest["GRAPH-DAYLIGHT_VARIATION"]: 167 crepyscule_graphic.plot_daylight_differences(lSunrise, lSunset,\ 168 lDaylength,\ 169 lDateISO8601,\ 170 sFilename, \ 171 tToday, sBackgroundColor, \ 172 lSunrise2, lSunset2, \ 173 lDaylength2) 174 if dDataRequest["GRAPH-SOLAR_FLUX"]: 175 crepyscule_graphic.plot_max_solar_flux(lDateISO8601, sFilename, tToday,\ 176 sBackgroundColor, fLat1, fLat2) 177 178 if dDataRequest["GRAPH-TWILIGHT_LENGTH"]: 179 dTwilightLength1 = get_twilight_length_as_dict(ctime, fLat1) 180 if fLat2 is not None: 181 dTwilightLength2 = get_twilight_length_as_dict(ctime, fLat2) 182 else: 183 dTwilightLength2 = None 184 crepyscule_graphic.plot_twilight_length(sFilename, tToday, sBackgroundColor, \ 185 fLat1, dTwilightLength1,\ 186 fLat2, dTwilightLength2) 187 188 if dDataRequest["TEXT-SUNRISE"]: 189 crepyscule_tools.save_info_flat_file(sFilename, lDateISO8601,\ 190 lSunrise, lSunset, lDaylength,\ 191 fLat1, fLon1, fUTC1, sSummerTime1, \ 192 lSunrise2, lSunset2, lDaylength2, \ 193 fLat2, fLon2, fUTC2, sSummerTime2)
194 195 196 ############################################################################## 197 # Sunrise/sunset 198
199 -def get_sunrise_day(ctime, fLat, fLon, fUTC=0, sSummerTime=''):
200 """ 201 Get the sunrise value for this day. 202 203 @type fLat: float 204 @param fLat: Latitude in decimal. 205 @type fLon: float 206 @param fLon: Longitude in decimal. West longitude are negative 207 East longitude are positive. 208 @type ctime: float 209 @param ctime: Any ctime in the day for the wanted value. 210 @type fUTC: float 211 @param fUTC: Time to add/substract for each time values. Usually used 212 for UTC value of place. 213 @type sSummerTime: string 214 @param sSummerTime: This variable is a 2 letters code indicating 215 when is the daylight saving time (DST) starting and ending in the year 216 for the first place. 217 Default is no DST. For a list of DST code, see L{crepyscule_summer_time.lTimezone}. 218 219 @rtype: float 220 @return: Time of the day in float. 221 """ 222 fSunrise = crepyscule_tools.get_one_value("sunrise", fLat, fLon,\ 223 ctime, fUTC, sSummerTime) 224 225 return fSunrise
226
227 -def get_sunset_day(ctime, fLat, fLon, fUTC=0, sSummerTime=''):
228 """ 229 Get the sunset value for this day. 230 231 @type fLat: float 232 @param fLat: Latitude in decimal. 233 @type fLon: float 234 @param fLon: Longitude in decimal. West longitude are negative 235 East longitude are positive. 236 @type ctime: float 237 @param ctime: Any ctime in the day for the wanted value. 238 @type fUTC: float 239 @param fUTC: Time to add/substract for each time values. Usually used 240 for UTC value of place. 241 @type sSummerTime: string 242 @param sSummerTime: This variable is a 2 letters code indicating 243 when is the daylight saving time (DST) starting and ending in the year 244 for the first place. 245 Default is no DST. For a list of DST code, see L{crepyscule_summer_time.lTimezone}. 246 247 @rtype: float 248 @return: Time of the day in float. 249 """ 250 fSunset = crepyscule_tools.get_one_value("sunset", fLat, fLon,\ 251 ctime, fUTC, sSummerTime) 252 253 return fSunset
254
255 -def get_sunrise_as_dict(ctime, fLat, fLon, fUTC=0, sSummerTime=''):
256 """ 257 Get a dictionary containing the sunrise time for the year of the ctime. 258 Keys of dictionary are date in iso8601 format. 259 260 @type fLat: float 261 @param fLat: Latitude in decimal. 262 @type fLon: float 263 @param fLon: Longitude in decimal. West longitude are negative 264 East longitude are positive. 265 @type ctime: float 266 @param ctime: Any ctime in the year of the result. 267 @type fUTC: float 268 @param fUTC: Time to add/substract for each time values. Usually used 269 for UTC value of place. 270 @type sSummerTime: string 271 @param sSummerTime: This variable is a 2 letters code indicating 272 when is the daylight saving time (DST) starting and ending in the year 273 for the first place. 274 Default is no DST. For a list of DST code, see L{crepyscule_summer_time.lTimezone}. 275 276 @rtype: dictionnary 277 @return: Dictionnary containing sunrise time for the year of the ctime. 278 Keys of dictionary are date in iso8601 format. 279 """ 280 281 dSunrise = crepyscule_tools.get_dictionary( "sunrise", fLat, fLon, ctime,\ 282 fUTC, sSummerTime) 283 284 return dSunrise
285
286 -def get_sunset_as_dict(ctime, fLat, fLon, fUTC=0, sSummerTime=''):
287 """ 288 Return a list containing the sunset time for this ctime. 289 290 @type fLat: float 291 @param fLat: Latitude in decimal. 292 @type fLon: float 293 @param fLon: Longitude in decimal. West longitude are negative 294 East longitude are positive. 295 @type ctime: float 296 @param ctime: Any ctime in the year of the result. 297 @type fUTC: float 298 @param fUTC: Time to add/substract for each time values. Usually used 299 for UTC value of place. 300 @type sSummerTime: string 301 @param sSummerTime: This variable is a 2 letters code indicating 302 when is the daylight saving time (DST) starting and ending in the year 303 for the first place. 304 Default is no DST. For a list of DST code, see L{crepyscule_summer_time.lTimezone}. 305 306 @rtype: dictionnary 307 @return: Dictionnary containing sunset time for the year of the ctime. 308 Keys of dictionary is a string in iso8601 format like 'YYYY-MM-DD'. 309 """ 310 dSunset = crepyscule_tools.get_dictionary("sunset", fLat, fLon, ctime, \ 311 fUTC, sSummerTime) 312 313 return dSunset
314 315
316 -def get_sunrise_sunset_as_csv(ctime, fLat1, fLon1, fUTC1=0, sSummerTime1='',\ 317 fLat2= None, fLon2=None, fUTC2=0, sSummerTime2=''):
318 """ 319 Returns a list containing the date,sunrise,sunset in a CSV format. 320 See function L{crepyscule_tools.get_sunrise_sunset_as_csv} for 321 detailed information. 322 323 @type fLat1: float 324 @param fLat1: Latitude in decimal for the first place. 325 @type fLon1: float 326 @param fLon1: Longitude in decimal for the first place. 327 West longitude are negative. 328 East longitude are positive. 329 @type ctime: float 330 @param ctime: Any ctime in the year of the result. 331 @type fUTC1: float 332 @param fUTC1: Time to add/substract for each time values for the first place. 333 Usually used for UTC value of place. 334 @type sSummerTime1: string 335 @param sSummerTime1: This variable is a 2 letters code indicating 336 when is the daylight saving time (DST) starting and ending in the year 337 for the first place. 338 Default is no DST. For a list of DST code, see L{crepyscule_summer_time.lTimezone}. 339 @type fLat2: float 340 @param fLat2: latitude of the second place. See fLat1. 341 @type fLon2: float 342 @param fLon2: longitude of the second place. See fLon1. 343 @type sSummerTime2: string 344 @param sSummerTime2: This variable is a 2 letters code indicating 345 when is the daylight saving time (DST) starting and ending in the year 346 for the seconde place. 347 Default is no DST. For a list of DST code, see L{crepyscule_summer_time.lTimezone}. 348 @type fUTC2: float 349 @param fUTC2: Time to add/substract for each time values for the second place. 350 351 @rtype: list 352 @return: list containing the date,sunrise,sunset in CSV format. One line per 353 item in the list. 354 """ 355 356 (lSunrise1, lSunset1, lSunAltitude, tToday) = \ 357 crepyscule_tools.get_one_year_data(fLat1, fLon1, \ 358 ctime, fUTC1, sSummerTime1) 359 lDaylength1 = crepyscule_tools.get_daylength(ctime, fLat1, fLon1) 360 # Check if there is a 2nd place to compute lat and lon 361 if fLon2 != None and fLat2 != None: 362 (lSunrise2, lSunset2, lSunAltitude2, tTmp) = \ 363 crepyscule_tools.get_one_year_data\ 364 (fLat2, fLon2, ctime, fUTC2, sSummerTime2) 365 lDaylength2 = crepyscule_tools.get_daylength(ctime, fLat2, fLon2) 366 else: 367 lSunrise2 = lSunset2 = lDaylength2 = None 368 369 lDateISO8601 = crepyscule_tools.get_one_year_of_iso8601(ctime) 370 lSunriseSunset = crepyscule_tools.\ 371 get_sunrise_sunset_as_csv(lDateISO8601,\ 372 lSunrise1, lSunset1, lDaylength1,\ 373 fLat1, fLon1, fUTC1, sSummerTime1,\ 374 lSunrise2, lSunset2, lDaylength2,\ 375 fLat2, fLon2, fUTC2,\ 376 sSummerTime2) 377 378 return lSunriseSunset
379 380 381 382 ############################################################################## 383 # Sun altitude
384 -def get_sun_altitude_as_csv(ctime, fLat1, fLat2=None):
385 """ 386 Returns a list containing the date,sun_altitude 387 388 No use for longitude, UTC or summer time. Use default values. 389 390 See function L{crepyscule.get_sun_altitude_as_csv} for 391 detailed information. 392 393 @type ctime: float 394 @param ctime: Any ctime in the year of the result. 395 @type fLat1: float 396 @param fLat1: Latitude in decimal for the first place. 397 @type fLat2: float 398 @param fLat2: latitude in decimal for the second place. 399 400 @rtype: list 401 @return: list containing the date,sun_altitude in CSV format. One line per 402 item in the list. 403 404 """ 405 # Use dummy value since it is the same for all lon, crepyscule_summer_time and utc 406 sSummerTime1 = sSummerTime2 = '' 407 fLon1 = fLon2 = 0.0 408 fUTC1 = fUTC2 = 0 409 410 (lSunrise, lSunset, lSunAltitude1, tToday) = \ 411 crepyscule_tools.get_one_year_data(fLat1, fLon1, ctime, fUTC1, \ 412 sSummerTime1) 413 414 # Check if there is a 2nd place to compute lat and lon 415 if fLat2 != None: 416 (lSunrise2, lSunset2, lSunAltitude2, tTmp) = \ 417 crepyscule_tools.get_one_year_data\ 418 (fLat2, fLon2, ctime, fUTC2, sSummerTime2) 419 else: 420 lSunAltitude2 = None 421 422 423 lDateISO8601 = crepyscule_tools.get_one_year_of_iso8601(ctime) 424 lSunAltitude = crepyscule_tools.\ 425 get_sun_altitude_as_csv(lDateISO8601, fLat1, lSunAltitude1, \ 426 fLat2, lSunAltitude2) 427 428 return lSunAltitude
429 430
431 -def get_sun_altitude_as_dict(ctime, fLat):
432 """ 433 Return a list containing the sun altitude for this ctime. 434 435 No use for longitude, UTC or summer time. Use default values. 436 437 @type ctime: float 438 @param ctime: Any ctime in the year of the result. 439 @type fLat: float 440 @param fLat: Latitude in decimal for the place. 441 442 @rtype: dictionnary 443 @return: Dictionnary containing sun altitude for the year of the ctime. 444 Keys of dictionary is a string in iso8601 format like 'YYYY-MM-DD'. 445 """ 446 # Use dummy value since it is the same for all lon, crepyscule_summer_time and utc 447 sSummerTime = "" 448 fLon = 0.0 449 fUTC = 0.0 450 451 dSunAltitude = crepyscule_tools.get_dictionary("altitude", fLat, fLon,\ 452 ctime, fUTC, sSummerTime) 453 454 return dSunAltitude
455
456 -def get_sun_altitude_day(ctime, fLat):
457 """ 458 Get the sun altitude value for this day. 459 No use for longitude, UTC or summer time. Use default values. 460 461 @type ctime: float 462 @param ctime: Any ctime in the year of the result. 463 @type fLat: float 464 @param fLat: Latitude in decimal for the place. 465 466 @rtype: float 467 @return: Sun altitude for this day in float. 468 """ 469 # Use dummy value since it is the same for all lon, crepyscule_summer_time and utc 470 sSummerTime = "" 471 fLon = 0.0 472 fUTC = 0.0 473 474 fAltitude = crepyscule_tools.get_one_value("altitude", fLat, fLon,\ 475 ctime, fUTC, sSummerTime) 476 477 return fAltitude
478 479 ############################################################################## 480 # Daylight variation 481 482
483 -def get_daylight_variation_as_csv(ctime, fLat1, fLat2=None):
484 """ 485 Returns a list containing the date, variation in daylight. 486 487 See function L{crepyscule_tools.get_daylight_variation_as_csv} for 488 detailed information. 489 490 @type ctime: float 491 @param ctime: Any ctime in the year of the result. 492 @type fLat1: float 493 @param fLat1: Latitude in decimal for the first place. 494 @type fLat2: float 495 @param fLat2: latitude in decimal for the second place. 496 497 @rtype: list 498 @return: list containing the date,variation in daylight in CSV format. 499 One line per item in the list. 500 """ 501 # Use dummy value since it is the same for all lon, 502 # crepyscule_summer_time and utc 503 sSummerTime1 = sSummerTime2 = '' 504 fLon1 = fLon2 = 0.0 505 fUTC1 = fUTC2 = 0.0 506 507 lSunTime1 = crepyscule_tools.get_daylength(ctime, fLat1, fLon1) 508 509 # Check if there is a 2nd place to compute lat and lon 510 if fLon2 != None and fLat2 != None: 511 lSunTime2 = crepyscule_tools.get_daylength(ctime, fLat2, fLon2) 512 else: 513 lSunTime2 = None 514 515 lDateISO8601 = crepyscule_tools.get_one_year_of_iso8601(ctime) 516 lDaylightVariation = crepyscule_tools.\ 517 get_daylight_variation_as_csv(lDateISO8601,\ 518 lSunTime1, fLat1, \ 519 lSunTime2, fLat2) 520 521 return lDaylightVariation
522
523 -def get_daylight_variation_as_dict(ctime, fLat):
524 """ 525 Return a list containing the daylight variation for the year 526 correspoding to this ctime in a dictionnary. 527 528 @type ctime: float 529 @param ctime: Any ctime in the year of the result. 530 @type fLat: float 531 @param fLat: Latitude in decimal for the place. 532 533 @rtype: dictionnary 534 @return: Dictionnary containing daylight variation for the year of the ctime. 535 Keys of dictionary is a string in iso8601 format like 'YYYY-MM-DD'. 536 """ 537 # Use dummy value since it is the same for all lon, 538 # crepyscule_summer_time and utc 539 sSummerTime = "" 540 fLon = 0.0 541 fUTC = 0.0 542 543 dDayligthVariation = crepyscule_tools.get_dictionary("variation", \ 544 fLat, fLon, ctime, \ 545 fUTC, sSummerTime ) 546 547 return dDayligthVariation
548 549
550 -def get_daylight_variation_day(ctime, fLat):
551 """ 552 Get the sun dayligth variation value for this day. 553 554 @type ctime: float 555 @param ctime: Any ctime in the year of the result. 556 @type fLat: float 557 @param fLat: Latitude in decimal for the place. 558 559 @rtype: float 560 @return: Daylight variation for this day in float. 561 """ 562 # Use dummy value since it is the same for all lon, 563 # crepyscule_summer_time and utc 564 sSummerTime = "" 565 fLon = 0.0 566 fUTC = 0.0 567 568 fVariation = crepyscule_tools.get_one_value("variation", fLat, fLon,\ 569 ctime, fUTC, sSummerTime) 570 571 return fVariation
572 573 ############################################################################## 574 # Solar flux 575
576 -def get_max_sf_as_csv(ctime, fLat1, fLat2=None):
577 """ 578 Returns a list containing the date, maximum solar flux 579 580 See function L{crepyscule_tools.get_solar_flux_as_csv} for 581 detailed information. 582 583 @type ctime: float 584 @param ctime: Any ctime in the year of the result. 585 @type fLat1: float 586 @param fLat1: Latitude in decimal for the first place. 587 @type fLat2: float 588 @param fLat2: Latitude in decimal for the second place. 589 590 @rtype: list 591 @return: list containing the date,max_solar_flux in CSV format. 592 One line per item in the list. Solar flux is in watt per meter square. 593 """ 594 595 nYear = int(crepyscule_tools.ctime_to_iso8601(ctime)[0:4]) 596 lFlux1 = crepyscule_tools.get_one_year_max_sf(nYear, fLat1) 597 lFlux2 = [0] # Initialization to a value to avoid an error for nYMax 598 if fLat2 != None: # Draw the other line 599 lFlux2 = crepyscule_tools.get_one_year_max_sf(nYear, fLat2) 600 else: 601 lFlux2 = None 602 603 lDateISO8601 = crepyscule_tools.get_one_year_of_iso8601(ctime) 604 lSolarFlux = crepyscule_tools.\ 605 get_solar_flux_as_csv(lDateISO8601, fLat1, lFlux1, \ 606 fLat2, lFlux2) 607 608 return lSolarFlux
609 610
611 -def get_max_sf_as_dict(ctime, fLat):
612 """ 613 Return a list containing the maximum solar flux for each day in the 614 year where this ctime belongs. 615 616 @type ctime: float 617 @param ctime: Any ctime in the year of the result. 618 @type fLat: float 619 @param fLat: Latitude in decimal for the place. 620 621 @rtype: dictionnary 622 @return: Dictionnary containing maximum solar flux of each day 623 for the year of the ctime. Keys of dictionary is a string in iso8601 format 624 like 'YYYY-MM-DD'. Solar flux is in watt per meter square. 625 """ 626 # Use dummy value since it is the same for all lon, crepyscule_summer_time and utc 627 sSummerTime = "" 628 fLon = 0.0 629 fUTC = 0.0 630 631 dSF = crepyscule_tools.get_dictionary("sf", fLat, fLon, ctime, \ 632 fUTC, sSummerTime) 633 634 return dSF
635 636
637 -def get_max_sf_day(ctime, fLat):
638 """ 639 Get the maximum solar flux value for this day. 640 641 @type ctime: float 642 @param ctime: Any ctime in the year of the result. 643 @type fLat: float 644 @param fLat: Latitude in decimal for the place. 645 646 @rtype: float 647 @return: Maximum solar flux for this day in float. Solar flux is in 648 watt per meter square. 649 """ 650 651 fFlux = crepyscule_tools.get_max_solar_flux(ctime, fLat) 652 653 return fFlux
654 655 656 ############################################################################## 657 # Twilight length. 658
659 -def get_twilight_length_as_csv(ctime, fLat1, fLat2=None):
660 """ 661 Returns a list containing the date and the twilight length in minute. 662 Note: the twilight length is defined as the duration while the sun is below 663 the horizon and less than 12° below (nautical twilight). 664 665 See function L{crepyscule_tools.get_twilight_length_as_csv} for 666 detailed information. 667 668 @type ctime: float 669 @param ctime: Any ctime in the year of the result. 670 @type fLat1: float 671 @param fLat1: Latitude in decimal for the first place. 672 @type fLat2: float 673 @param fLat2: Latitude in decimal for the second place. 674 675 @rtype: list 676 @return: list containing the date, twilight_length in CSV format. 677 One line per item in the list. Twilight length is minutes 678 """ 679 dLength1 = get_twilight_length_as_dict(ctime, fLat1) 680 lDate = dLength1.keys() 681 lDate.sort() 682 683 lLength1 = [] 684 for i in range(len(lDate)): 685 lLength1.append(dLength1[lDate[i]]) 686 687 lLength2 = [] 688 if fLat2 is not None: 689 dLength2 = get_twilight_length_as_dict(ctime, fLat2) 690 for i in range(len(lDate)): 691 lLength2.append(dLength2[lDate[i]]) 692 else: 693 lLength2 = None 694 695 lDateISO8601 = crepyscule_tools.get_one_year_of_iso8601(ctime) 696 lTwilightLength = crepyscule_tools.\ 697 get_twilight_length_as_csv(lDateISO8601, fLat1, lLength1, \ 698 fLat2, lLength2) 699 700 return lTwilightLength
701
702 -def get_twilight_length_as_dict(ctime, fLat):
703 """ 704 Return a list containing the twilight length in minute for each day in the 705 year where this ctime belongs. 706 707 @type ctime: float 708 @param ctime: Any ctime in the year of the result. 709 @type fLat: float 710 @param fLat: Latitude in decimal for the place. 711 712 @rtype: dictionnary 713 @return: Dictionnary containing the twilight length of each day 714 for the year of the ctime. Keys of dictionary is a string in iso8601 format 715 like 'YYYY-MM-DD'. Solar flux is in watt per meter square. 716 """ 717 # Use dummy value since it is the same for all lon, crepyscule_summer_time 718 # and utc 719 sSummerTime = "" 720 fLon = 0.0 721 fUTC = 0.0 722 723 dTwilightLength = crepyscule_tools.get_dictionary("twilight", \ 724 fLat, fLon, ctime, \ 725 fUTC, sSummerTime ) 726 727 return dTwilightLength
728 729
730 -def get_twilight_length_day(ctime, fLat):
731 """ 732 Get the twilight length in minute value for this day. 733 734 @type ctime: float 735 @param ctime: Any ctime in the year of the result. 736 @type fLat: float 737 @param fLat: Latitude in decimal for the place. 738 739 @rtype: float 740 @return: Twilight length for this day in float. Length is in degree/hour 741 """ 742 743 fTwilight_length = crepyscule_tools.get_twilight_length_day(ctime, fLat) 744 745 return fTwilight_length
746 747 748 ############################################################ 749 # Crepyscule in Command line 750 # 751 # 752 CFG_LONG_OPTIONS = ["help","version", \ 753 "filename", "ctime", "background-color", \ 754 "lat1", "lon1", "utc1", "tz1", \ 755 "lat2", "lon2", "utc2", "tz2"] 756 757 import optparse 758
759 -def check_required (parser, opt):
760 """ 761 Extention of class OptionParser for mandatory argument. 762 Comes from 763 http://www.python.org/doc/2.3.4/lib/optparse-extending-examples.html 764 765 @type parser: OptionParser 766 @param parser: Parser containing the values in the command line 767 @type opt: string 768 @param opt: option to check if it is required. 769 """ 770 option = parser.get_option(opt) 771 # Assumes the option's 'default' is set to None! 772 if getattr(parser.values, option.dest) is None: 773 sError = _("ERROR: ") + opt +\ 774 _(" parameter is missing\n Please try ") + \ 775 sys.argv[0] + _(" --help for all the options") 776 sys.stderr.write(sError) 777 sys.exit(1)
778 779 780
781 -def get_command_line():
782 """ 783 Parse the command line and perform all the checks. 784 """ 785 # Parse the command line 786 parser = optparse.OptionParser(usage="%prog --filename filename --ctime ctime --background-color bg_color --lat1 latitude1 --lon1 longitude1 --utc1 utc1 --tz1 timezone1",\ 787 version="crepyscule " + str(VERSION)) 788 789 parser.add_option("--filename", "-f", dest="filename", \ 790 help=_("Filename of the graphic. If not present, the output is forced to text and given in stdout"),\ 791 action="store", type="string", default=None) 792 parser.add_option("--ctime", dest="fTime", \ 793 help=_("ctime for the current day"),\ 794 action="store", type="float") 795 parser.add_option("--background-color", dest="sBackgroundColor", \ 796 help=_("Background color of the graphics"),\ 797 action="store", type="string") 798 parser.add_option("--lat1", dest="fLat1", \ 799 help=_("Latitude of the first place"), \ 800 action="store", type="float") 801 parser.add_option("--lon1", dest="fLon1", \ 802 help=_("Longitude of the first place. West longitude are negative. East longitude are positive."), \ 803 action="store", type="float") 804 parser.add_option("--utc1", dest="fUTC1", \ 805 help=_("Difference of the time zone compared to the UTC time\n for the first place"),\ 806 action="store", type="float") 807 parser.add_option("--tz1", dest="sTZ1", \ 808 help=_("Time zone parameter used for summer time. Possible value are:\n IR, JD, EU, IQ, US, CH, EG, MX, SK, HK, NB, FK, PY, NZ, TS, BZ, CL, AU, TG, ZN, LB, SY, RU"),\ 809 action="store", type="string") 810 parser.add_option("--lat2", dest="fLat2", \ 811 help=_("Latitude of the second place"),\ 812 action="store", type="float") 813 parser.add_option("--lon2", dest="fLon2", \ 814 help=_("Longitude of the second place. West longitude are negative. East longitude are positive."),\ 815 action="store", type="float") 816 parser.add_option("--utc2", dest="fUTC2", \ 817 help=_("Difference of the time zone compared to the UTC time\n for the second place"), \ 818 action="store", type="float") 819 parser.add_option("--tz2", dest="sTZ2", \ 820 help=_("Time zone parameter for 2nd place. See --tz1 for possible value"), \ 821 action="store", type="string") 822 # Type of data wanted by the user 823 parser.add_option("--graph-sunrise-sunset", dest="bGraphSunriseSunset", \ 824 help=_("Create the graphic for sunrise/sunset"),\ 825 action="store_true", default=False) 826 parser.add_option("--graph-sun-altitude", dest="bGraphSunAltitude", \ 827 help=_("Create the graphic for sun altitude"),\ 828 action="store_true", default=False) 829 parser.add_option("--graph-daylight-variation", dest="bGraphDaylightVariation", \ 830 help=_("Create the graphic for daylight variation"),\ 831 action="store_true", default=False) 832 parser.add_option("--graph-solar-flux", dest="bGraphSolarFlux", \ 833 help=_("Create the graphic for the solar flux"),\ 834 action="store_true", default=False) 835 parser.add_option("--graph-twilight-length", dest="bGraphTwilightLength", \ 836 help=_("Create the graphic for the twilight length"), \ 837 action="store_true", default=False) 838 parser.add_option("--text-sunrise-sunset", dest="bTextSunriseSunset", \ 839 help=_("Create a text file containing the sunrise/sunset value. Omit --filename argument to have the output in stdout."),\ 840 action="store_true", default=False) 841 parser.add_option("--graph-all", dest="bGraphAll", \ 842 help=_("Create all the graphics for these lat/lon"),\ 843 action="store_true", default=False) 844 845 # Parse the args 846 (options, args) = parser.parse_args() 847 848 849 # Check if all mandatory argument are here: 850 for sOption in ["ctime", "lat1", "lon1", "utc1", "tz1"]: 851 sLongOption = "--" + sOption 852 check_required(parser, sLongOption) 853 854 # Everything is there, check which graphic and text 855 dDataRequest = { "GRAPH-SUNRISE" : options.bGraphSunriseSunset, 856 "GRAPH-ALTITUDE" : options.bGraphSunAltitude, 857 "GRAPH-DAYLIGHT_VARIATION" : options.bGraphDaylightVariation, 858 "GRAPH-SOLAR_FLUX" : options.bGraphSolarFlux, 859 "GRAPH-TWILIGHT_LENGTH" : options.bGraphTwilightLength, 860 "TEXT-SUNRISE" : options.bTextSunriseSunset} 861 862 # Check if the filename option is there or not. 863 if options.filename is None: 864 if dDataRequest["TEXT-SUNRISE"] is False: 865 sys.stderr.write( _("ERROR: --filename argument not given. You must ask for a text output (--text-sunrise-sunset) to use stdout. If you want a graphic, please provide a filename.\n")) 866 sys.exit(1) 867 else: # Do not display any graphs. 868 sys.stderr.write(_("INFO: Option --filename not provided. Turning all graphic output to off and using stdout for text output.\n")) 869 options.bGraphAll = False 870 for sKey in dDataRequest.keys(): 871 if "GRAPH" in sKey: 872 dDataRequest[sKey] = False 873 # If all the graphs are asked, put the corresponding boolean to True 874 elif options.bGraphAll: 875 for sKey in dDataRequest.keys(): 876 if "GRAPH" in sKey: 877 dDataRequest[sKey] = True 878 879 # Check if at least one graph or text is asked 880 i = 0 881 for sKey in dDataRequest.keys(): 882 if dDataRequest[sKey] == False: 883 i = i + 1 884 if i == len(dDataRequest): 885 print _("WARNING: No data specified, using default (--graph-sunrise-sunset)") 886 print _("Try --help for more information") 887 dDataRequest["GRAPH-SUNRISE"] = True 888 889 return (dDataRequest, options.fLat1, options.fLon1, options.fTime, options.fUTC1, \ 890 options.filename, options.sTZ1, options.fLat2, options.fLon2, options.sTZ2, \ 891 options.fUTC2, options.sBackgroundColor)
892 893 if __name__ == "__main__": 894 895 (bDataRequest, fLat, fLon, fTime, fUTC, sFilename, sTimeZone, fLat2, fLon2, \ 896 sTimeZone2, fUTC2, sBackgroundColor) = \ 897 get_command_line() 898 899 900 901 crepyscule(bDataRequest, fLat, fLon, fTime,\ 902 fUTC, sFilename, sTimeZone,\ 903 fLat2, fLon2, sTimeZone2, fUTC2, sBackgroundColor) 904