Skip to content

Instantly share code, notes, and snippets.

@mehori
Created April 5, 2024 13:40
Show Gist options
  • Save mehori/5f4782ec4ad1e9f324da625f2270a710 to your computer and use it in GitHub Desktop.
Save mehori/5f4782ec4ad1e9f324da625f2270a710 to your computer and use it in GitHub Desktop.
生物季節観測累年値ファイル(CSV) をパースする Python スクリプト
# 生物季節観測累年値ファイル(CSV) をパースして、「年 1月1日からの日数」に変換する
# https://www.data.jma.go.jp/sakura/data/download_ruinenchi.html
# replace には全角スペースを入れておくようにする
import codecs
def readData(file, station):
with codecs.open(file, "r", encoding="shift_jis") as ifile:
for line in ifile:
line = line.strip()
line = line.replace(" ", "")
data = line.split(",")
print(data[1],"a",station)
if data[1].encode("shift_jis") == station.encode("shift_jis"):
print("found",station)
sakura = data[2::2]
return sakura
def parseDate(date,year):
doy = 59
if is_leap_year(year):
doy += 1
mon = date[0]
day = date[1:3]
if int(mon) == 4:
doy += 31 + int(day)
else:
doy += int(day)
return doy
def is_leap_year(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
def main():
tokyo = readData("004.csv","東京")
i = 0
for y in range(1953,2023):
doy = parseDate(tokyo[i],y)
print(y,doy)
i += 1
if __name__ == "__main__":
main()
@mehori
Copy link
Author

mehori commented Apr 5, 2024

Shift JIS で、全角スペースも含むファイルで提供されているので、codecs.open で文字コードを指定して開き、replace で全角スペースを消してから station と比較するようにしています。

日付は 3 月14 日が 314 の形式になっているので、閏年かを考慮しつつ 1月1日からの day of year にして出力するようになっています。
データは3列目から2列おきに入っているので取っています。

そういえば5月に咲く場合と、欠測処理をしていないのでそこだけ要注意だ(笑)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment