【发布时间】:2018-04-26 00:17:58
【问题描述】:
我正在尝试在一天中的某个时间计算一个给定条件的值。矢量/系列是给定小时的净太阳辐射和收集数据的时间。当在白天获得该值时,我必须将该值乘以 0.1,而当在夜间获得该值时,我将该值乘以 0.5。问题是日光时间按月(甚至按周)显着变化,如下面的日光字典所示:
我正在尝试创建一个布尔掩码(在日光下),这将帮助我应用计算/乘法,而无需在“时间”向量中的每个值上使用 for 循环,并根据日光字典检查它,这就是我正在做:
def Ghr(time, Rn):
#soil heatflux cal
#time is a single vale of the time vector
mon = time.strftime('%b') #strips the month
#sunrise -sunset hours from 1st of the month (sr,ss) to end of the month
#Jan-1st SR 8:17, Jan-31st SR 07:47
#Jan-1st SS 16:03, Jan-31st SS 16:52
daylight = {'Jan':('08:00', '16:25'),
'Feb':('07:20', '17:20'),
'Mar':('06:45', '18:40'),
'Apr':('06:05', '20:05'),
'May':('05:10', '20:55'),
'Jun':('04:50', '21:25'),
'Jul':('05:10', '21:15'),
'Aug':('05:50', '20:30'),
'Sep':('06:45', '19:25'),
'Oct':('07:00', '17:30'),
'Nov':('07:25', '16:15'),
'Dec':('08:05', '16:00')}
#strips the hour and minute from the daylight dictionary
#then withindaylight is the boolean after checking the
#time the data was collected against the these stripped values
daybegin = dt.strptime(daylight[mon][0], '%H:%M').time()
dayend= dt.strptime(daylight[mon][1], '%H:%M').time()
withindaylight = daybegin <= time.time() <= dayend
#I want to apply the boolean mask such that it produces the following,
#but returns a vector and not just a single value
if withindaylight:
return .1*Rn #I want to return a series and not just a single value
else:
return .5*Rn
【问题讨论】:
标签: python dictionary boolean time-series