24
24
25
25
from pvlib import atmosphere
26
26
from pvlib .tools import datetime_to_djd , djd_to_datetime
27
+ from pvlib ._deprecation import deprecated
28
+
27
29
28
30
NS_PER_HR = 1.e9 * 3600. # nanoseconds per hour
29
31
@@ -40,7 +42,6 @@ def get_solarposition(time, latitude, longitude,
40
42
time : pandas.DatetimeIndex
41
43
42
44
latitude : float
43
-
44
45
longitude : float
45
46
46
47
altitude : None or float, default None
@@ -359,9 +360,8 @@ def spa_python(time, latitude, longitude,
359
360
return result
360
361
361
362
362
- def get_sun_rise_set_transit (time , latitude , longitude , how = 'numpy' ,
363
- delta_t = 67.0 ,
364
- numthreads = 4 ):
363
+ def sun_rise_set_transit_spa (times , latitude , longitude , how = 'numpy' ,
364
+ delta_t = 67.0 , numthreads = 4 ):
365
365
"""
366
366
Calculate the sunrise, sunset, and sun transit times using the
367
367
NREL SPA algorithm described in [1].
@@ -373,13 +373,15 @@ def get_sun_rise_set_transit(time, latitude, longitude, how='numpy',
373
373
374
374
Parameters
375
375
----------
376
- time : pandas.DatetimeIndex
377
- Only the date part is used
376
+ times : pandas.DatetimeIndex
377
+ Must be localized to the timezone for ``latitude`` and ``longitude``.
378
378
latitude : float
379
+ Latitude in degrees, positive north of equator, negative to south
379
380
longitude : float
381
+ Longitude in degrees, positive east of prime meridian, negative to west
380
382
delta_t : float, optional
381
383
If delta_t is None, uses spa.calculate_deltat
382
- using time .year and time .month from pandas.DatetimeIndex.
384
+ using times .year and times .month from pandas.DatetimeIndex.
383
385
For most simulations specifing delta_t is sufficient.
384
386
Difference between terrestrial time and UT1.
385
387
*Note: delta_t = None will break code using nrel_numba,
@@ -394,9 +396,9 @@ def get_sun_rise_set_transit(time, latitude, longitude, how='numpy',
394
396
395
397
Returns
396
398
-------
397
- DataFrame
398
- The DataFrame will have the following columns:
399
- sunrise, sunset, transit
399
+ pandas. DataFrame
400
+ index is the same as input `times` argument
401
+ columns are ' sunrise', ' sunset', and ' transit'
400
402
401
403
References
402
404
----------
@@ -409,36 +411,40 @@ def get_sun_rise_set_transit(time, latitude, longitude, how='numpy',
409
411
lat = latitude
410
412
lon = longitude
411
413
412
- if not isinstance ( time , pd . DatetimeIndex ):
413
- try :
414
- time = pd . DatetimeIndex ( time )
415
- except ( TypeError , ValueError ) :
416
- time = pd . DatetimeIndex ([ time , ] )
414
+ # times must be localized
415
+ if times . tz :
416
+ tzinfo = times . tz
417
+ else :
418
+ raise ValueError ( 'times must be localized' )
417
419
418
420
# must convert to midnight UTC on day of interest
419
- utcday = pd .DatetimeIndex (time .date ).tz_localize ('UTC' )
421
+ utcday = pd .DatetimeIndex (times .date ).tz_localize ('UTC' )
420
422
unixtime = np .array (utcday .astype (np .int64 )/ 10 ** 9 )
421
423
422
424
spa = _spa_python_import (how )
423
425
424
- delta_t = delta_t or spa .calculate_deltat (time .year , time .month )
426
+ delta_t = delta_t or spa .calculate_deltat (times .year , times .month )
425
427
426
428
transit , sunrise , sunset = spa .transit_sunrise_sunset (
427
429
unixtime , lat , lon , delta_t , numthreads )
428
430
429
431
# arrays are in seconds since epoch format, need to conver to timestamps
430
432
transit = pd .to_datetime (transit * 1e9 , unit = 'ns' , utc = True ).tz_convert (
431
- time . tz ).tolist ()
433
+ tzinfo ).tolist ()
432
434
sunrise = pd .to_datetime (sunrise * 1e9 , unit = 'ns' , utc = True ).tz_convert (
433
- time . tz ).tolist ()
435
+ tzinfo ).tolist ()
434
436
sunset = pd .to_datetime (sunset * 1e9 , unit = 'ns' , utc = True ).tz_convert (
435
- time . tz ).tolist ()
437
+ tzinfo ).tolist ()
436
438
437
- result = pd .DataFrame ({ 'transit ' : transit ,
438
- 'sunrise ' : sunrise ,
439
- 'sunset ' : sunset }, index = time )
439
+ return pd .DataFrame (index = times , data = { 'sunrise ' : sunrise ,
440
+ 'sunset ' : sunset ,
441
+ 'transit ' : transit } )
440
442
441
- return result
443
+
444
+ get_sun_rise_set_transit = deprecated ('0.6.1' ,
445
+ alternative = 'sun_rise_set_transit_spa' ,
446
+ name = 'get_sun_rise_set_transit' ,
447
+ removal = '0.7' )(sun_rise_set_transit_spa )
442
448
443
449
444
450
def _ephem_convert_to_seconds_and_microseconds (date ):
@@ -476,10 +482,11 @@ def _ephem_setup(latitude, longitude, altitude, pressure, temperature,
476
482
return obs , sun
477
483
478
484
479
- def rise_set_transit_ephem (time , latitude , longitude ,
480
- next_or_previous = 'next' ,
481
- altitude = 0 ,
482
- pressure = 101325 , temperature = 12 , horizon = '0:00' ):
485
+ def sun_rise_set_transit_ephem (times , latitude , longitude ,
486
+ next_or_previous = 'next' ,
487
+ altitude = 0 ,
488
+ pressure = 101325 ,
489
+ temperature = 12 , horizon = '0:00' ):
483
490
"""
484
491
Calculate the next sunrise and sunset times using the PyEphem package.
485
492
@@ -488,9 +495,9 @@ def rise_set_transit_ephem(time, latitude, longitude,
488
495
time : pandas.DatetimeIndex
489
496
Must be localized
490
497
latitude : float
491
- positive is north of 0
498
+ Latitude in degrees, positive north of equator, negative to south
492
499
longitude : float
493
- positive is east of 0
500
+ Longitude in degrees, positive east of prime meridian, negative to west
494
501
next_or_previous : str
495
502
'next' or 'previous' sunrise and sunset relative to time
496
503
altitude : float, default 0
@@ -523,8 +530,10 @@ def rise_set_transit_ephem(time, latitude, longitude,
523
530
raise ImportError ('PyEphem must be installed' )
524
531
525
532
# times must be localized
526
- if not time .tz :
527
- raise ValueError ('rise_set_ephem: times must be localized' )
533
+ if times .tz :
534
+ tzinfo = times .tz
535
+ else :
536
+ raise ValueError ('times must be localized' )
528
537
529
538
obs , sun = _ephem_setup (latitude , longitude , altitude ,
530
539
pressure , temperature , horizon )
@@ -544,18 +553,18 @@ def rise_set_transit_ephem(time, latitude, longitude,
544
553
sunrise = []
545
554
sunset = []
546
555
trans = []
547
- for thetime in time :
556
+ for thetime in times :
548
557
thetime = thetime .to_pydatetime ()
549
558
# pyephem drops timezone when converting to its internal datetime
550
559
# format, so handle timezone explicitly here
551
560
obs .date = ephem .Date (thetime - thetime .utcoffset ())
552
- sunrise .append (_ephem_to_timezone (rising (sun ), time . tz ))
553
- sunset .append (_ephem_to_timezone (setting (sun ), time . tz ))
554
- trans .append (_ephem_to_timezone (transit (sun ), time . tz ))
561
+ sunrise .append (_ephem_to_timezone (rising (sun ), tzinfo ))
562
+ sunset .append (_ephem_to_timezone (setting (sun ), tzinfo ))
563
+ trans .append (_ephem_to_timezone (transit (sun ), tzinfo ))
555
564
556
- return pd .DataFrame (index = time , data = {'sunrise' : sunrise ,
557
- 'sunset' : sunset ,
558
- 'transit' : trans })
565
+ return pd .DataFrame (index = times , data = {'sunrise' : sunrise ,
566
+ 'sunset' : sunset ,
567
+ 'transit' : trans })
559
568
560
569
561
570
def pyephem (time , latitude , longitude , altitude = 0 , pressure = 101325 ,
@@ -1371,8 +1380,8 @@ def _times_to_hours_after_local_midnight(times):
1371
1380
return np .array (hrs )
1372
1381
1373
1382
1374
- def sunrise_sunset_transit_geometric (times , latitude , longitude , declination ,
1375
- equation_of_time ):
1383
+ def sun_rise_set_transit_geometric (times , latitude , longitude , declination ,
1384
+ equation_of_time ):
1376
1385
"""
1377
1386
Geometric calculation of solar sunrise, sunset, and transit.
1378
1387
0 commit comments