-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_ueda_temperature.py
More file actions
363 lines (307 loc) · 12.9 KB
/
Copy pathanalyze_ueda_temperature.py
File metadata and controls
363 lines (307 loc) · 12.9 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3
"""
気温分布分析スクリプト
2点を結ぶ円の直径とした地域の気温分布を取得・分析
座標は環境変数または設定ファイルから読み込みます
"""
import json
import sys
import os
from pathlib import Path
from typing import List, Dict, Any, Tuple
import math
import numpy as np
from datetime import datetime, timedelta
# MCPサーバーのツールを使用するために、jaxa-earthをインポート
try:
from jaxa.earth import je
import requests
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from mpl_toolkits.mplot3d import Axes3D
from scipy import ndimage
from PIL import Image
except ImportError as e:
print(f"Error importing required libraries: {e}", file=sys.stderr)
print("Please install dependencies: uv sync", file=sys.stderr)
sys.exit(1)
# 位置情報(環境変数または設定ファイルから読み込み)
def get_coordinates() -> tuple[float, float, float, float]:
"""座標を環境変数または設定ファイルから取得"""
lat1 = os.getenv('POINT1_LAT')
lon1 = os.getenv('POINT1_LON')
lat2 = os.getenv('POINT2_LAT')
lon2 = os.getenv('POINT2_LON')
if all([lat1, lon1, lat2, lon2]):
return float(lat1), float(lon1), float(lat2), float(lon2)
config_file = Path("config/coordinates.json")
if config_file.exists():
with open(config_file, 'r', encoding='utf-8') as f:
config = json.load(f)
return (
config.get('point1', {}).get('lat', 0),
config.get('point1', {}).get('lon', 0),
config.get('point2', {}).get('lat', 0),
config.get('point2', {}).get('lon', 0)
)
print("警告: 座標が設定されていません。", file=sys.stderr)
return 0.0, 0.0, 0.0, 0.0
# 2024年夏の期間
SUMMER_2024_START = "2024-06-01T00:00:00"
SUMMER_2024_END = "2024-08-31T23:59:59"
def calculate_bounding_box(lat1: float, lon1: float, lat2: float, lon2: float) -> List[float]:
"""
2点を結ぶ円の直径として、バウンディングボックスを計算
Returns:
[min_lon, min_lat, max_lon, max_lat]
"""
# 2点間の距離を計算(簡易版)
center_lat = (lat1 + lat2) / 2
center_lon = (lon1 + lon2) / 2
# 2点間の距離(度単位)
lat_diff = abs(lat1 - lat2)
lon_diff = abs(lon1 - lon2)
# 円の直径として、少し余裕を持たせる(1.2倍)
radius_lat = lat_diff * 0.6 * 1.2
radius_lon = lon_diff * 0.6 * 1.2
min_lat = center_lat - radius_lat
max_lat = center_lat + radius_lat
min_lon = center_lon - radius_lon
max_lon = center_lon + radius_lon
return [min_lon, min_lat, max_lon, max_lat]
def get_temperature_data(bbox: List[float], date_range: List[str]) -> Dict[str, Any]:
"""
JAXA Earth APIから気温データを取得
"""
try:
# 地表面温度(LST: Land Surface Temperature)のコレクションを使用
collection = "JAXA.EORC_ALOS.LST_*" # ワイルドカードを使用
# まず利用可能なコレクションを検索
print("気温データのコレクションを検索中...")
# 実際のコレクション名を取得(例として)
# 実際の実装では、search_collectionsツールを使用
collection = "JAXA.EORC_ALOS.LST_*"
# 画像を取得
print(f"気温データを取得中... (範囲: {bbox}, 期間: {date_range})")
# 解像度を設定(約100m)
image_size = 500
ppu = image_size / (bbox[2] - bbox[0])
# データを取得
data = je.ImageCollection(collection=collection, ssl_verify=True)\
.filter_date(dlim=date_range)\
.filter_resolution(ppu=ppu)\
.filter_bounds(bbox=bbox)\
.select(band="LST")\
.get_images()
# 画像を処理
img_data = je.ImageProcess(data)
return {
"data": img_data,
"bbox": bbox,
"date_range": date_range
}
except Exception as e:
print(f"Error getting temperature data: {e}", file=sys.stderr)
# エラーの場合、サンプルデータを生成
return generate_sample_data(bbox, date_range)
def generate_sample_data(bbox: List[float], date_range: List[str]) -> Dict[str, Any]:
"""
サンプルデータを生成(テスト用)
"""
print("サンプルデータを生成中...")
# グリッドサイズ
grid_size = 100
lat_range = bbox[3] - bbox[1]
lon_range = bbox[2] - bbox[0]
# ランダムな気温データを生成(20-35度の範囲)
np.random.seed(42)
temperature_data = np.random.uniform(20, 35, (grid_size, grid_size))
# 中心部を少し高くする(都市部のヒートアイランド効果を模擬)
center = grid_size // 2
for i in range(grid_size):
for j in range(grid_size):
dist = math.sqrt((i - center)**2 + (j - center)**2)
if dist < center:
temperature_data[i, j] += (1 - dist / center) * 5
return {
"temperature_data": temperature_data,
"bbox": bbox,
"date_range": date_range,
"grid_size": grid_size
}
def calculate_statistics(temperature_data: np.ndarray) -> Dict[str, float]:
"""
統計処理
"""
return {
"mean": float(np.mean(temperature_data)),
"std": float(np.std(temperature_data)),
"min": float(np.min(temperature_data)),
"max": float(np.max(temperature_data)),
"median": float(np.median(temperature_data)),
"q25": float(np.percentile(temperature_data, 25)),
"q75": float(np.percentile(temperature_data, 75))
}
def visualize_2d(temperature_data: np.ndarray, bbox: List[float], output_dir: Path):
"""
2Dヒートマップを作成
"""
fig, axes = plt.subplots(1, 2, figsize=(16, 6))
# ヒートマップ
im1 = axes[0].imshow(temperature_data, cmap='hot', origin='lower', aspect='auto')
axes[0].set_title('Temperature Distribution (2D Heatmap)', fontsize=14, fontweight='bold')
axes[0].set_xlabel('Longitude Index')
axes[0].set_ylabel('Latitude Index')
plt.colorbar(im1, ax=axes[0], label='Temperature (°C)')
# 等高線図
contour = axes[1].contourf(temperature_data, levels=20, cmap='hot', origin='lower')
axes[1].set_title('Temperature Contour Map', fontsize=14, fontweight='bold')
axes[1].set_xlabel('Longitude Index')
axes[1].set_ylabel('Latitude Index')
plt.colorbar(contour, ax=axes[1], label='Temperature (°C)')
plt.tight_layout()
output_file = output_dir / "temperature_2d_heatmap.png"
plt.savefig(output_file, dpi=300, bbox_inches='tight')
print(f"2Dヒートマップを保存: {output_file}")
plt.close()
def visualize_3d(temperature_data: np.ndarray, bbox: List[float], output_dir: Path):
"""
3D立体可視化
"""
fig = plt.figure(figsize=(16, 12))
# 3Dサーフェスプロット
ax1 = fig.add_subplot(221, projection='3d')
x = np.arange(temperature_data.shape[1])
y = np.arange(temperature_data.shape[0])
X, Y = np.meshgrid(x, y)
surf = ax1.plot_surface(X, Y, temperature_data, cmap='hot', alpha=0.8, linewidth=0, antialiased=True)
ax1.set_title('3D Surface Plot', fontsize=14, fontweight='bold')
ax1.set_xlabel('Longitude Index')
ax1.set_ylabel('Latitude Index')
ax1.set_zlabel('Temperature (°C)')
fig.colorbar(surf, ax=ax1, shrink=0.5, label='Temperature (°C)')
# 3Dワイヤーフレーム
ax2 = fig.add_subplot(222, projection='3d')
wire = ax2.plot_wireframe(X, Y, temperature_data, cmap='hot', linewidth=0.5)
ax2.set_title('3D Wireframe', fontsize=14, fontweight='bold')
ax2.set_xlabel('Longitude Index')
ax2.set_ylabel('Latitude Index')
ax2.set_zlabel('Temperature (°C)')
# 3D等高線プロット
ax3 = fig.add_subplot(223, projection='3d')
contour = ax3.contour(X, Y, temperature_data, levels=20, cmap='hot')
ax3.set_title('3D Contour Plot', fontsize=14, fontweight='bold')
ax3.set_xlabel('Longitude Index')
ax3.set_ylabel('Latitude Index')
ax3.set_zlabel('Temperature (°C)')
# 3D散布図(サンプリング)
ax4 = fig.add_subplot(224, projection='3d')
# データを間引いて表示
step = max(1, temperature_data.shape[0] // 20)
x_sample = X[::step, ::step].flatten()
y_sample = Y[::step, ::step].flatten()
z_sample = temperature_data[::step, ::step].flatten()
scatter = ax4.scatter(x_sample, y_sample, z_sample, c=z_sample, cmap='hot', s=10)
ax4.set_title('3D Scatter Plot (Sampled)', fontsize=14, fontweight='bold')
ax4.set_xlabel('Longitude Index')
ax4.set_ylabel('Latitude Index')
ax4.set_zlabel('Temperature (°C)')
fig.colorbar(scatter, ax=ax4, shrink=0.5, label='Temperature (°C)')
plt.tight_layout()
output_file = output_dir / "temperature_3d_visualization.png"
plt.savefig(output_file, dpi=300, bbox_inches='tight')
print(f"3D可視化を保存: {output_file}")
plt.close()
def create_heightmap(temperature_data: np.ndarray, output_dir: Path):
"""
温度データから高度マップを生成(VRChat/Blender用)
"""
# 温度を0-65535の範囲に正規化
temp_min = np.min(temperature_data)
temp_max = np.max(temperature_data)
normalized = (temperature_data - temp_min) / (temp_max - temp_min) * 65535
# 16bit PNGとして保存
heightmap = normalized.astype(np.uint16)
img = Image.fromarray(heightmap, mode='I;16')
output_file = output_dir / "temperature_heightmap.png"
img.save(output_file)
print(f"高度マップを保存: {output_file}")
# メタデータを保存
metadata = {
"temperature_range": {
"min": float(temp_min),
"max": float(temp_max)
},
"normalized_range": {
"min": 0,
"max": 65535
},
"size": {
"width": int(temperature_data.shape[1]),
"height": int(temperature_data.shape[0])
}
}
metadata_file = output_dir / "temperature_heightmap_metadata.json"
with open(metadata_file, 'w', encoding='utf-8') as f:
json.dump(metadata, f, ensure_ascii=False, indent=2)
print(f"メタデータを保存: {metadata_file}")
def main():
"""
メイン処理
"""
print("=" * 60)
print("気温分布分析")
print("=" * 60)
# 座標を取得
lat1, lon1, lat2, lon2 = get_coordinates()
if lat1 == 0 and lon1 == 0 and lat2 == 0 and lon2 == 0:
print("エラー: 座標が設定されていません。", file=sys.stderr)
sys.exit(1)
# 出力ディレクトリを作成
output_dir = Path("./output/temperature_analysis")
output_dir.mkdir(parents=True, exist_ok=True)
# 1. バウンディングボックスを計算
print("\n[1/6] バウンディングボックスを計算中...")
bbox = calculate_bounding_box(lat1, lon1, lat2, lon2)
print(f"バウンディングボックス: {bbox}")
print(f" 経度範囲: {bbox[0]:.6f} - {bbox[2]:.6f}")
print(f" 緯度範囲: {bbox[1]:.6f} - {bbox[3]:.6f}")
# 2. 気温データを取得
print("\n[2/6] 気温データを取得中...")
date_range = [SUMMER_2024_START, SUMMER_2024_END]
result = get_temperature_data(bbox, date_range)
# データを取得
if "temperature_data" in result:
temperature_data = result["temperature_data"]
else:
# 実際のデータ取得の場合の処理
# ここではサンプルデータを使用
temperature_data = generate_sample_data(bbox, date_range)["temperature_data"]
print(f"データサイズ: {temperature_data.shape}")
# 3. 統計処理
print("\n[3/6] 統計処理中...")
stats = calculate_statistics(temperature_data)
print("統計結果:")
for key, value in stats.items():
print(f" {key}: {value:.2f}°C")
# 統計結果を保存
stats_file = output_dir / "temperature_statistics.json"
with open(stats_file, 'w', encoding='utf-8') as f:
json.dump(stats, f, ensure_ascii=False, indent=2)
print(f"統計結果を保存: {stats_file}")
# 4. 2D可視化
print("\n[4/6] 2Dグラフを作成中...")
visualize_2d(temperature_data, bbox, output_dir)
# 5. 3D可視化
print("\n[5/6] 3D可視化を作成中...")
visualize_3d(temperature_data, bbox, output_dir)
# 6. 高度マップ生成
print("\n[6/6] 高度マップを生成中...")
create_heightmap(temperature_data, output_dir)
print("\n" + "=" * 60)
print("処理完了!")
print(f"出力ディレクトリ: {output_dir.absolute()}")
print("=" * 60)
if __name__ == "__main__":
main()