気象庁データダウンロード:指定気温の高度

Last updated: 2024/02/04

雷雲と雷の関連性を解析するとき、-10^\circ10-10^\circC や-20^\circ20-20^\circC の高度が非常に大事です。気象庁のホームページからこのデータを見ることができます。例えば、2022年11月22日9時のデータは以下のリンクにあります:
https://www.data.jma.go.jp/stats/etrn/upper/view/daily_uth.php?year=2022&month=11&day=22&hour=9&atm=&point=47600&view=

このページを見ると、高度4032mの温度は-8.8^\circ8.8-8.8^\circC、高度4379mの温度は-11.3^\circ11.3-11.3^\circC、ということが分かります。これで-10^\circ10-10^\circCの高度が推定できます。同じように他の温度の高度も推定できます。

でも、一つ一つのデータを計算するのが非常に手間がかかります。以下のプログラムは、任意の時刻における任意の温度の高度を自動的に計算することができます。

import requests

strDate = '20221122' # YYYYMMDD
strHour = '9' # '9' or '21' (JST)
siteCode = '47600' #Wajima
tarTempVal = -10 #Temperature

response = requests.get('https://www.data.jma.go.jp/stats/etrn/upper/view/daily_uth.php?year='+
                        strDate[0:4]+'&month='+str(int(strDate[4:6]))+'&day='+
                        str(int(strDate[6:8]))+'&hour='+strHour+'&atm=&point='+siteCode+'&view=')
data = response.text
strLineMat = data.split('\n')

lowHeight = 0
lowTemp = 100
for strLine in strLineMat:
    if 'data_0_0' in strLine:
        index1 = strLine.find('</td><td class=')
        if index1<0:
            continue
        begIndex1 = index1+26
        endIndex1 = begIndex1+5
        
        begIndex2 = endIndex1+26
        index2 = strLine.find('<', begIndex2)
        if index2<0:
            continue
        endIndex2 = index2
        
        try:
            heightVal = int(strLine[begIndex1:endIndex1])
            tempVal = float(strLine[begIndex2:endIndex2])
        except:
            continue
        
        upHeight = lowHeight
        upTemp = lowTemp
        lowHeight = heightVal
        lowTemp = tempVal
        if lowTemp<tarTempVal:
            break
        
if lowTemp==100:
    print('No data.')
else:
    tarHeight = (lowHeight-upHeight)/(upTemp-lowTemp)*(upTemp-tarTempVal)+upHeight
    print(tarHeight)
import requests

strDate = '20221122' # YYYYMMDD
strHour = '9' # '9' or '21' (JST)
siteCode = '47600' #Wajima
tarTempVal = -10 #Temperature

response = requests.get('https://www.data.jma.go.jp/stats/etrn/upper/view/daily_uth.php?year='+
                        strDate[0:4]+'&month='+str(int(strDate[4:6]))+'&day='+
                        str(int(strDate[6:8]))+'&hour='+strHour+'&atm=&point='+siteCode+'&view=')
data = response.text
strLineMat = data.split('\n')

lowHeight = 0
lowTemp = 100
for strLine in strLineMat:
    if 'data_0_0' in strLine:
        index1 = strLine.find('</td><td class=')
        if index1<0:
            continue
        begIndex1 = index1+26
        endIndex1 = begIndex1+5
        
        begIndex2 = endIndex1+26
        index2 = strLine.find('<', begIndex2)
        if index2<0:
            continue
        endIndex2 = index2
        
        try:
            heightVal = int(strLine[begIndex1:endIndex1])
            tempVal = float(strLine[begIndex2:endIndex2])
        except:
            continue
        
        upHeight = lowHeight
        upTemp = lowTemp
        lowHeight = heightVal
        lowTemp = tempVal
        if lowTemp<tarTempVal:
            break
        
if lowTemp==100:
    print('No data.')
else:
    tarHeight = (lowHeight-upHeight)/(upTemp-lowTemp)*(upTemp-tarTempVal)+upHeight
    print(tarHeight)

実行すると、-10^\circ10-10^\circCの高度が約4199mという結果が分かります。

複数の日にちのデータを一括ダウンロードする

以上のプログラムを利用して、複数の日にちのデータを一括でダウンロードすることが簡単にできます。以下のプログラムは2021年11月から2022年12月のすべての-10^\circ10-10^\circCの高度結果をダウンロードして、行8に指定されたフォルダーに保存します(一ヶ月の結果は一つのファイルに保存されます)。

import os
import requests
import datetime
import time

startMonth = '202111'
endMonth = '202212'
downloadDir = 'E:/Data/'

tarTempVal = -10 #Temperature
strHourMat = ['9', '21'] #Do not change
siteCode = '47600' #Wajima

if os.path.exists(downloadDir)==False:
    os.mkdir(downloadDir)

startDate = startMonth+'01'
endMonthPlus = int(endMonth[4:6])+1
if endMonthPlus>12:
    endDate = str(int(endMonth[0:4])+1)+'0101'
else:
    endDate = endMonth[0:4]+str(endMonthPlus).zfill(2)+'01'

startSec = int(time.mktime(datetime.datetime.strptime(startDate, '%Y%m%d').timetuple()))
endSec = int(time.mktime(datetime.datetime.strptime(endDate, '%Y%m%d').timetuple()))

for curSec in range(startSec, endSec, 86400):
    curDate = datetime.datetime.fromtimestamp(curSec).strftime('%Y%m%d')
    print('Downloading '+curDate)
    for strHour in strHourMat:
        response = requests.get('https://www.data.jma.go.jp/stats/etrn/upper/view/daily_uth.php?year='+
                                curDate[0:4]+'&month='+str(int(curDate[4:6]))+'&day='+
                                str(int(curDate[6:8]))+'&hour='+strHour+'&atm=&point='+siteCode+'&view=')
        data = response.text
        strLineMat = data.split('\n')

        lowHeight = 0
        lowTemp = 100
        for strLine in strLineMat:
            if 'data_0_0' in strLine:
                index1 = strLine.find('</td><td class=')
                if index1<0:
                    continue
                begIndex1 = index1+26
                endIndex1 = begIndex1+5
                
                begIndex2 = endIndex1+26
                index2 = strLine.find('<', begIndex2)
                if index2<0:
                    continue
                endIndex2 = index2
                
                try:
                    heightVal = int(strLine[begIndex1:endIndex1])
                    tempVal = float(strLine[begIndex2:endIndex2])
                except:
                    continue
                
                upHeight = lowHeight
                upTemp = lowTemp
                lowHeight = heightVal
                lowTemp = tempVal
                if lowTemp<tarTempVal:
                    break
                
        if lowTemp==100:
            print(strHour+': No data.')
            continue
        else:
            tarHeight = (lowHeight-upHeight)/(upTemp-lowTemp)*(upTemp-tarTempVal)+upHeight
        
        resFile = downloadDir+curDate[0:6]+'.txt'
        if os.path.exists(resFile)==False:
            fidRes = open(resFile, 'wt')
            fidRes.write('date,hour,height\n')
            fidRes.close()
        fidRes = open(resFile, 'at')
        fidRes.write(curDate[6:8]+','+strHour.zfill(2)+',%6.1f\n' % tarHeight)
        fidRes.close()
import os
import requests
import datetime
import time

startMonth = '202111'
endMonth = '202212'
downloadDir = 'E:/Data/'

tarTempVal = -10 #Temperature
strHourMat = ['9', '21'] #Do not change
siteCode = '47600' #Wajima

if os.path.exists(downloadDir)==False:
    os.mkdir(downloadDir)

startDate = startMonth+'01'
endMonthPlus = int(endMonth[4:6])+1
if endMonthPlus>12:
    endDate = str(int(endMonth[0:4])+1)+'0101'
else:
    endDate = endMonth[0:4]+str(endMonthPlus).zfill(2)+'01'

startSec = int(time.mktime(datetime.datetime.strptime(startDate, '%Y%m%d').timetuple()))
endSec = int(time.mktime(datetime.datetime.strptime(endDate, '%Y%m%d').timetuple()))

for curSec in range(startSec, endSec, 86400):
    curDate = datetime.datetime.fromtimestamp(curSec).strftime('%Y%m%d')
    print('Downloading '+curDate)
    for strHour in strHourMat:
        response = requests.get('https://www.data.jma.go.jp/stats/etrn/upper/view/daily_uth.php?year='+
                                curDate[0:4]+'&month='+str(int(curDate[4:6]))+'&day='+
                                str(int(curDate[6:8]))+'&hour='+strHour+'&atm=&point='+siteCode+'&view=')
        data = response.text
        strLineMat = data.split('\n')

        lowHeight = 0
        lowTemp = 100
        for strLine in strLineMat:
            if 'data_0_0' in strLine:
                index1 = strLine.find('</td><td class=')
                if index1<0:
                    continue
                begIndex1 = index1+26
                endIndex1 = begIndex1+5
                
                begIndex2 = endIndex1+26
                index2 = strLine.find('<', begIndex2)
                if index2<0:
                    continue
                endIndex2 = index2
                
                try:
                    heightVal = int(strLine[begIndex1:endIndex1])
                    tempVal = float(strLine[begIndex2:endIndex2])
                except:
                    continue
                
                upHeight = lowHeight
                upTemp = lowTemp
                lowHeight = heightVal
                lowTemp = tempVal
                if lowTemp<tarTempVal:
                    break
                
        if lowTemp==100:
            print(strHour+': No data.')
            continue
        else:
            tarHeight = (lowHeight-upHeight)/(upTemp-lowTemp)*(upTemp-tarTempVal)+upHeight
        
        resFile = downloadDir+curDate[0:6]+'.txt'
        if os.path.exists(resFile)==False:
            fidRes = open(resFile, 'wt')
            fidRes.write('date,hour,height\n')
            fidRes.close()
        fidRes = open(resFile, 'at')
        fidRes.write(curDate[6:8]+','+strHour.zfill(2)+',%6.1f\n' % tarHeight)
        fidRes.close()

以下のような結果がダウンロードされます:
202111.txt (2021年11月のデータ)
一列目は日にち、2列目は時間(9時か21時)、3列目は指定温度の高度(m)

date,hour,height
01,09,4316.8
01,21,4355.4
02,09,4231.2
02,21,3795.7
03,09,4241.0
03,21,3987.6
04,09,3639.4
04,21,3521.7
05,09,3673.0
05,21,4224.4
06,09,4080.1
06,21,4025.1
07,09,4513.1
07,21,5011.9
08,09,5235.5
08,21,5243.0
09,09,4972.0
09,21,4226.7
10,09,3249.5
10,21,3033.3
11,09,3392.7
11,21,3208.0
12,09,2735.0
12,21,3021.2
13,09,3977.0
13,21,4235.5
14,09,4009.4
14,21,3906.2
15,09,3948.4
15,21,4135.9
16,09,3517.1
16,21,3397.8
17,09,3571.0
17,21,3962.3
18,09,3623.0
18,21,4159.3
19,09,4385.2
19,21,4123.2
20,09,4149.5
20,21,4030.2
21,09,4380.7
21,21,4014.2
22,09,4732.1
22,21,2986.0
23,09,2544.1
23,21,2274.5
24,09,2171.4
24,21,2596.2
25,09,3049.3
25,21,2759.6
26,09,2436.6
26,21,2287.5
27,09,1966.5
27,21,2353.5
28,09,2626.8
28,21,4001.4
29,09,3896.1
29,21,3965.7
30,09,3904.6
30,21,4413.4

同じような方法で、気象庁の他のデータをダウンロードすることも簡単にできると思います。


Back to Python関連資料