-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_reader1998-2018.py
78 lines (63 loc) · 4.14 KB
/
csv_reader1998-2018.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import pandas as pd
DailyTempMean = {}
#讀取單一檔案
data = pd.read_csv('/Users/hsiehyuanlung/Desktop/氣象資料/1998-2018/Auto_2008-2018.csv')#讀取資料
#print(data)
data = data[data['# stno'].str.contains('C0A880')] #篩選測站id
data = data[data['yyyymmddhh'].astype(str).str.endswith(('24','06','12','18'))] #篩選四個時間
data = data[['# stno','yyyymmddhh','TX01']] #篩選出指定欄
data = data.drop(data[data['TEMP']<0].index) #刪除<0的溫度資料
data['yyyymmddhh'] = data['yyyymmddhh'] // 100
DailyTemp = []
DailyTempMean = {}
data = data.append({'# stno':999999,'yyyymmddhh':99999999,'TX01':-9999},ignore_index=True) #增加最後一行終止行,計算日均溫時出錯
for i in range(len(data)-1):
if data.iat[i,2] >0: #如果本日均溫大於十度(避免無數據(記錄為-9999)造成影響)
DailyTemp.append(data.iat[i,2]) #將此時段氣溫存進DailyTemp中
if (data.iat[i,1]) != (data.iat[i+1,1]): #如果日期和下一筆數據不同(為此日最後一筆數據)
if len(DailyTemp) >0: #若一天四時段皆無數據,則不計入日均溫
day_mean = sum(DailyTemp) / len(DailyTemp) #計算日均溫
DailyTempMean[str(data.iat[i,1])] = day_mean #將日均溫存進DailyTempMean中
DailyTemp.clear()
#print(DailyTempMean)
DailyTempMean = sorted(DailyTempMean.items(),key = lambda x:x[0]) #將資料由小到大(按日期)排序 items將DailyTempMeanionary轉為tuple型態
DailyTempMeanDataFrame = pd.DataFrame(DailyTempMean,columns=['Date', 'Mean TEMP']) #將DailyTempMean轉為DataFrame
#print(DailyTempMeanDataFrame)
#DailyTempMeanDataFrame.to_csv('2008-2018DailyTemp.csv') #輸出日均溫資料,可用 index = False 不輸出索引
WeeklyTemp = []
WeeklyTempMean = {}
week = 1 #從第一週開始
for i in range(len(DailyTempMean)):
datei = int(DailyTempMean[i][0]) % 100
if datei == 1 or datei == 8 or datei == 15 or datei == 22:
for j in range(i,i+10):
if j < len(DailyTempMean):
datej = int(DailyTempMean[j][0]) % 100
if (datej != datei) and (datej == 1 or datej == 8 or datej == 15 or datej == 22):
break
if DailyTempMean[j][1] >= 5:
WeeklyTemp.append(DailyTempMean[j][1])
if len(WeeklyTemp) > 0: #若一週皆無數據,則不計入週均溫
mean = sum(WeeklyTemp) / len(WeeklyTemp) #計算週均溫(每週以1,8,15,22號開始)
week = (int(str(DailyTempMean[i][0])[4:6])-1) * 4 + ((datei-1)//7)+1 #週數=(月份-1)x4+((日期-1)/7)+1
WeeklyTempMean[(str(DailyTempMean[i][0])[0:8]+'%02d' % week)] = mean #將週均溫儲存在WeeklyTempMean中
else: #若一週皆無數據,則該週記為0
week = (int(str(DailyTempMean[i][0])[4:6])-1) * 4 + ((datei-1)//7)+1 #週數=(月份-1)x4+((日期-1)/7)+1
WeeklyTempMean[(str(DailyTempMean[i][0])[0:8]+'%02d' % week)] = 0 #無資料則記為0
WeeklyTemp.clear() #清空此星期的氣溫數據
#print(WeeklyTempMean)
WeeklyTempMeanDataFrame = pd.DataFrame(WeeklyTempMean.items(),columns=['Week', 'Mean TEMP'])
print(WeeklyTempMeanDataFrame)
#WeeklyTempMeanDataFrame.to_csv('自動2008-2018WeeklyTemp.csv') #輸出週均溫資料,可用 index = False 不輸出索引
'''
for i in range(len(DailyTempMean)):
date = int(DailyTempMean[i][0]) % 100
if date == 1 or date == 8 or date == 15 or date == 22:
if len(WeeklyTemp) >= 1: #避免1/1多餘運算
mean = sum(WeeklyTemp) / len(WeeklyTemp) #計算週均溫(每週以1,8,15,22號開始)
week = (int(str(DailyTempMean[i][0])[4:6])-1) * 4 + ((date-1)//7) #週數=(月份-1)x4+((日期-1)/7)+1 -1 (要-1因為計算的是前一週的週均溫)
WeeklyTempMean[+str('%02d' % week)] = mean #將週均溫儲存在WeeklyTempMean中
WeeklyTemp.clear() #清空此星期的氣溫數據
if DailyTempMean[i][1] >= 5: #如果本日均溫大於十度(避免無數據(記錄為-9999)造成影響)
WeeklyTemp.append(DailyTempMean[i][1]) #將本日均溫儲存進WeeklyTemp中
'''