1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonによる気象・気候データ解析I 2章章末問題

Last updated at Posted at 2024-06-01

Pythonによる気象・気候データ解析I: Pythonの基礎・気候値と偏差・回帰相関分析

注:しばらくAmazonでは品切れなので楽天等で
Pythonによる気象・気候データ解析1 Pythonの基礎・気候値と偏差・回帰相関分析 [ 神山 翼 ]

の章末問題を一つずつ解いていきます。

A

A-1 1997−08

  • 本文と同じ年で夏の8月を
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
#!pip install japanize_matplotlib > /dev/null
import japanize_matplotlib

draw_year = 1997
draw_month = 8

vim = -5
vmax = 35
vint = 5

cm = plt.get_cmap('seismic')
cs = plt.contourf(lon2, lat2,
                  np.squeeze(sst[:, :, (y==draw_year)*(m==draw_month)]),
                  cmap=cm, norm=Normalize(vmin=vim, vmax=vmax),
                  levels=np.arange(vim, vmax+vint, vint), extend='both')
plt.colorbar(cs)
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.xlim(0, 360)
plt.ylim(-90, 90)
plt.title(str(draw_year)+'/'+str(draw_month))
plt.show()
  • 赤い領域が広がっている

image.png

A-2 日本近海

draw_year = 1997
draw_month = 8

vim = -5
vmax = 35
vint = 5

cm = plt.get_cmap('seismic')
cs = plt.contourf(lon2, lat2,
                  np.squeeze(sst[:, :, (y==draw_year)*(m==draw_month)]),
                  cmap=cm, norm=Normalize(vmin=vim, vmax=vmax),
                  levels=np.arange(vim, vmax+vint, vint), extend='both')
plt.colorbar(cs)
plt.xlabel('Longitude')
plt.ylabel('Latitude')

# 日本近海
plt.xlim(120, 160)
plt.ylim(30, 50)
plt.title(str(draw_year)+'/'+str(draw_month) + ' 日本近海')

plt.show()

image.png

  • 同じく draw_month = 12 にしてみると海水温の変化がわかりやすい

image.png

A-3

  • matplotlibのColorMapのマニュアルを見て寒色暖色系のグラデーションを試してみた
# c.f. https://matplotlib.org/stable/users/explain/colors/colormaps.html

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
#!pip install japanize_matplotlib > /dev/null
import japanize_matplotlib

draw_year = 1997
draw_month = 8

vim = -5
vmax = 35
vint = 5

def draw_sea_temp(cm_name):
  cm = plt.get_cmap(cm_name)
  cs = plt.contourf(lon2, lat2,
                    np.squeeze(sst[:, :, (y==draw_year)*(m==draw_month)]),
                    cmap=cm, norm=Normalize(vmin=vim, vmax=vmax),
                    levels=np.arange(vim, vmax+vint, vint), extend='both')
  plt.colorbar(cs)
  plt.xlabel('Longitude')
  plt.ylabel('Latitude')
  plt.xlim(0, 360)
  plt.ylim(-90, 90)
  plt.title(str(draw_year)+'/'+str(draw_month)+' '+cm_name)
  plt.show()

draw_sea_temp('seismic')
draw_sea_temp('bwr')
draw_sea_temp('coolwarm')
draw_sea_temp('rainbow')
draw_sea_temp('jet')
draw_sea_temp('turbo') 

image.png

image.png

image.png

image.png

image.png

image.png

B

B-1

import numpy as np

A = np.array([[1, 2, 4], 
              [4, 3, 5],
              [6, 2, 9]])
B = np.array([5, 5, 4])

print(B==4)

A[:, B==4]
[False False  True]
array([[4],
       [5],
       [9]])

B-2

(draw_year年)かつ(draw_month月)つまり draw_year年draw_month月 のsstの値を取得する。

C

  • 気圧は950hPaより小さい値もあるがグラデーションがうまく出ないので高めに950hPaとした。
  • 3時間毎にプロットして台風の動きを見えるようにした
vim = 950
vmax = 1030
vint = 5

def draw_typhoon(t):
  cm = plt.get_cmap('seismic')
  cs = plt.contourf(lon2, lat2,
                    surface_pressure[:, :, t] / 100,
                    cmap=cm, norm=Normalize(vmin=vim, vmax=vmax),
                    levels=np.arange(vim, vmax+vint, vint), extend='both')
  plt.colorbar(cs)
  plt.xlabel('Longitude')
  plt.ylabel('Latitude')
  plt.xlim(120, 150)
  plt.ylim(23, 45)
  plt.title(str(t) + '')
  plt.show()

for t in range(0, 24, 3):
  draw_typhoon(t)

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

  • 雲量
cloud = msm_dataset['cloud']

plt.hist(cloud.flatten(), bins=100)
plt.show()

image.png

vim = 0
vmax = 100
vint = 5

def draw_cloud(t):
  cm = plt.get_cmap('seismic')
  cs = plt.contourf(lon2, lat2,
                    cloud[:, :, t],
                    cmap=cm, norm=Normalize(vmin=vim, vmax=vmax),
                    levels=np.arange(vim, vmax+vint, vint), extend='both')
  plt.colorbar(cs)
  plt.xlabel('Longitude')
  plt.ylabel('Latitude')
  plt.xlim(120, 150)
  plt.ylim(23, 45)
  plt.title(str(t) + '')
  plt.show()

for t in range(0, 24, 3):
  draw_cloud(t)

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

D

  • 本書発売日 2024-05-01 14:00 の気温を描いてみた
    • 「なんらかの現象」までは思いつかず
    • 27-30.5 とちょっと高めの気がするがこういうものか

NetCDF化した数値予報GPVデータ

!pip install netCDF4 > /dev/null

import numpy as np
import netCDF4 as nc4
import datetime
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
#!pip install japanize_matplotlib > /dev/null
import japanize_matplotlib

loadfile = 'drive/MyDrive/0501.nc'

msm_data = nc4.Dataset(loadfile)

temperature = msm_data.variables['temp'][:]
temperature = temperature.transpose(2, 1, 0)
temperature = temperature.astype(float)

[imt, jmt, tmt] = temperature.shape

lon = msm_data.variables['lon'][:]
lat = msm_data.variables['lat'][:]

[xgrid2, ygrid2] = np.meshgrid(lon, lat)
lon2 = (xgrid2.T).astype(float)
lat2 = (ygrid2.T).astype(float)
del lon, lat, xgrid2, ygrid2

y = np.zeros(tmt)
m = np.zeros(tmt)
d = np.zeros(tmt)
h = np.zeros(tmt)

date_offset = datetime.datetime(2019, 10, 12)
time = msm_data.variables['time'][:]
for tt in range(0, tmt):
  time_data = date_offset + datetime.timedelta(hours=int(time[tt]))
  y[tt] = time_data.year
  m[tt] = time_data.month
  d[tt] = time_data.day
  h[tt] = time_data.hour

plt.hist(temperature.flatten())
plt.show()

image.png

vim = 27
vmax = 31
vint = 0.5

def draw_temp(t):
  cm = plt.get_cmap('seismic')
  cs = plt.contourf(lon2, lat2,
                    temperature[:, :, t] / 10,
                    cmap=cm, norm=Normalize(vmin=vim, vmax=vmax),
                    levels=np.arange(vim, vmax+vint, vint), extend='both')
  plt.colorbar(cs)
  plt.xlabel('Longitude')
  plt.ylabel('Latitude')
  plt.xlim(120, 150)
  plt.ylim(23, 45)
  plt.title(str(t) + '')
  plt.show()

draw_temp(14)

image.png


1
0
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?