# python提取时间序列白天和黑夜的时间段

Atmospheric Boundary Layer Stability一文我们知道地表边界层在昼夜存在不同；白天由于地表被加热，边界层较薄，反之亦然；为此需要对雷达的测量结果提取昼夜的分布信息

Astral库计算日出日落时间一文解决获取当天日出日落时间，进一步可参考Astral手册

## 原始数据格式

：第一列为时间序列，2,3列分别为雷达40m处的风向风速，依次类推

  import pandas as pd
import numpy as np
import datetime
import astral

##https://www.jianshu.com/p/3d1d648406f3
longititude=21.45 ## 城市经度信息
latitude=112.15 ## 城市纬度度信息
location_yangjiang = astral.Location(('Yangjiang', 'China',21.45 ,112.15 , 'Asia/Shanghai', 0))
#location_yangjiang = astral.Location(('YanDun', 'China',42.3369 ,94.382 , 'Asia/Shanghai', 0))
#location_yangjiang = astral.Location(('huailai', 'China',42.242 ,115.35 , 'Asia/Shanghai', 0))
#time_sunrise=location_yangjiang.sunrise(date=datetime.date.today(),local=True)
time_sunrise=location_yangjiang.sunrise(date=datetime.date(2019,6,15),local=True)
time_sunrise_update=time_sunrise + datetime.timedelta(hours=0) #提前/延后日出时间量
time_sunrise_new=str(time_sunrise_update)
print(f"日出时间:{time_sunrise_new}")

#time_sunset=location_yangjiang.sunset(date=datetime.date.today(),local=True)
time_sunset=location_yangjiang.sunset(date=datetime.date(2019,6,15),local=True)

time_sunset_update=time_sunset + datetime.timedelta(hours=0) #提前/延后日落时间量
time_sunset_new=str(time_sunset_update)
print(f"日落时间:{time_sunset_new}")

positions=[40,65,90,115,140,150,160,170,180,190,200,210]
windDirection=180
minusPlus=30
velMean=np.zeros(positions.__len__())

def getSunriseFromDay(today):
## 日出
year=today[0:4]
month=today[4:6]
day=today[6:8]
time_sunrise = location_yangjiang.sunrise(date=datetime.date(int(year), int(month), int(day)), local=True)
time_sunrise_update = time_sunrise + datetime.timedelta(hours=0)  # 提前/延后日出时间量
time_sunrise_new = str(time_sunrise_update)
return time_sunrise_new[11:19]

def getSunsetFromDay(today):
year=today[0:4]
month=today[4:6]
day=today[6:8]
## 日落
time_sunset = location_yangjiang.sunset(date=datetime.date(int(year), int(month), int(day)), local=True)
time_sunset_update = time_sunset + datetime.timedelta(hours=0)  # 提前/延后日落时间量
time_sunset_new = str(time_sunset_update)
return time_sunset_new[11:19]

def getDayArrayCursorByDay():
timeseries=dongpingLidar.iloc[:,0]

## timeSeries 9-19 为小时：分钟:秒      time_sunset_new 11:19为小时:分钟:秒
# for (idx, val) in enumerate(timeseries):
#     time_sunrise_new,time_sunset_new=getSunriseSunsetFromDay(val)
#     if(val[9:19] >= time_sunrise_new[11:19] and val[9:19] <= time_sunset_new[11:19]]):
#         dayIndex

# dayIndex = [idx for (idx, val) in enumerate(timeseries) if val[9:19] >= time_sunrise_new[11:19] and val[9:19] <=time_sunset_new[11:19]]
# nightIndex = [idx for (idx, val) in enumerate(timeseries) if val[9:19] < time_sunrise_new[11:19] or val[9:19] > time_sunset_new[11:19]]
dayIndex = [idx for (idx, val) in enumerate(timeseries) if val[9:19] >= getSunriseFromDay(val) and val[9:19] <=getSunsetFromDay(val)]
nightIndex = [idx for (idx, val) in enumerate(timeseries) if val[9:19] < getSunriseFromDay(val) or val[9:19] > getSunsetFromDay(val)]
return dayIndex,nightIndex

def getDayArrayCursor():
timeseries=dongpingLidar.iloc[:,0]

## timeSeries 9-19 为小时：分钟:秒      time_sunset_new 11:19为小时:分钟:秒
dayIndex = [idx for (idx, val) in enumerate(timeseries) if val[9:19] >= time_sunrise_new[11:19] and val[9:19] <=time_sunset_new[11:19]]
nightIndex = [idx for (idx, val) in enumerate(timeseries) if val[9:19] < time_sunrise_new[11:19] or val[9:19] > time_sunset_new[11:19]]
return dayIndex,nightIndex

##1. 逐日跟踪日落日出
dayIndex,nightIndex=getDayArrayCursorByDay()

##2. 测试期中间日期跟踪日落日出
# dayIndex,nightIndex=getDayArrayCursor()
for i in range(len(positions)):
print(f"{i}:")
dir1 = dongpingLidar.iloc[:, 2*i+1]
vel = dongpingLidar.iloc[:, 2*i+2]

dayDir1=dir1[dayIndex]
dayVel=vel[dayIndex]

# dayDir1=dir1[nightIndex]
# dayVel=vel[nightIndex]
idx = [idx for (idx, val) in enumerate(dayDir1) if val >= (windDirection-minusPlus) and val <=(windDirection+minusPlus)]
## dir1[idx]  vel[idx]
countVel=0
for cursor1 in range(len(idx)):
cursor=idx[cursor1]
countVel=countVel+dayVel.iloc[cursor]

velMean[i]=countVel/len(idx)

profiles=pd.DataFrame([velMean,positions]).T
profiles.to_csv("Lidar387Profiles14Day30.csv")

## 结论

1. 1-2个月的雷达用中间日期即可，差别不大；如果全年的话，最好是跟踪每日。
2. 白天的计算速度边界层较薄，夜晚较厚一些，原因是白天的地表收到加热效应。

1. 凡是有备注的都显示粉色
2. 凡是一周之内有变动的文件和文件夹都显示蓝色
3. 凡是一天之内有变动的文件和文件夹都显示红色
4. 凡是office文件都显示灰色
##### 令狐冲
###### Engineer of offshore wind turbine technique research

My research interests include distributed energy, wind turbine power generation technique , Computational fluid dynamic and programmable matter.