Upload 24 files
Browse files- app.py +856 -114
- ash_animator/__init__.py +12 -0
- ash_animator/__pycache__/__init__.cpython-312.pyc +0 -0
- ash_animator/__pycache__/animation_all.cpython-312.pyc +0 -0
- ash_animator/__pycache__/animation_single.cpython-312.pyc +0 -0
- ash_animator/__pycache__/animation_vertical.cpython-312.pyc +0 -0
- ash_animator/__pycache__/basemaps.cpython-312.pyc +0 -0
- ash_animator/__pycache__/converter.cpython-312.pyc +0 -0
- ash_animator/__pycache__/export.cpython-312.pyc +0 -0
- ash_animator/__pycache__/interpolation.cpython-312.pyc +0 -0
- ash_animator/__pycache__/plot_3dfield_data.cpython-312.pyc +0 -0
- ash_animator/__pycache__/plot_horizontal_data.cpython-312.pyc +0 -0
- ash_animator/__pycache__/utils.cpython-312.pyc +0 -0
- ash_animator/animation_all.py +516 -0
- ash_animator/animation_single.py +147 -0
- ash_animator/animation_vertical.py +360 -0
- ash_animator/basemaps.py +103 -0
- ash_animator/converter.py +279 -0
- ash_animator/export.py +119 -0
- ash_animator/interpolation.py +14 -0
- ash_animator/plot_3dfield_data.py +472 -0
- ash_animator/plot_horizontal_data.py +288 -0
- ash_animator/utils.py +23 -0
- default_model.zip +3 -0
app.py
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import io
|
| 2 |
import random
|
| 3 |
from typing import List, Tuple
|
|
@@ -7,141 +23,867 @@ import panel as pn
|
|
| 7 |
from PIL import Image
|
| 8 |
from transformers import CLIPModel, CLIPProcessor
|
| 9 |
|
| 10 |
-
pn.extension(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
async with session.get(api_url) as resp:
|
| 26 |
-
return (await resp.json())[0]["url"]
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
processor_name: str, model_name: str
|
| 32 |
-
) -> Tuple[CLIPProcessor, CLIPModel]:
|
| 33 |
-
processor = CLIPProcessor.from_pretrained(processor_name)
|
| 34 |
-
model = CLIPModel.from_pretrained(model_name)
|
| 35 |
-
return processor, model
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
-
outputs = model(**inputs)
|
| 54 |
-
logits_per_image = outputs.logits_per_image
|
| 55 |
-
class_likelihoods = logits_per_image.softmax(dim=1).detach().numpy()
|
| 56 |
-
return class_likelihoods[0]
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
"""
|
| 61 |
-
High level function that takes in the user inputs and returns the
|
| 62 |
-
classification results as panel objects.
|
| 63 |
-
"""
|
| 64 |
try:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
return
|
| 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 |
-
value=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
)
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
pn.
|
| 117 |
-
|
|
|
|
| 118 |
)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
# create dashboard
|
| 135 |
-
main = pn.WidgetBox(
|
| 136 |
-
input_widgets,
|
| 137 |
-
interactive_result,
|
| 138 |
-
footer_row,
|
| 139 |
)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
).servable(
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import glob
|
| 3 |
+
import shutil
|
| 4 |
+
import io
|
| 5 |
+
import logging
|
| 6 |
+
import panel as pn
|
| 7 |
+
import xarray as xr
|
| 8 |
+
import numpy as np
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
from types import SimpleNamespace
|
| 11 |
+
from collections import defaultdict
|
| 12 |
+
from ash_animator.converter import NAMEDataProcessor
|
| 13 |
+
from ash_animator.plot_3dfield_data import Plot_3DField_Data
|
| 14 |
+
from ash_animator.plot_horizontal_data import Plot_Horizontal_Data
|
| 15 |
+
from ash_animator import create_grid
|
| 16 |
+
|
| 17 |
import io
|
| 18 |
import random
|
| 19 |
from typing import List, Tuple
|
|
|
|
| 23 |
from PIL import Image
|
| 24 |
from transformers import CLIPModel, CLIPProcessor
|
| 25 |
|
| 26 |
+
pn.extension()
|
| 27 |
+
|
| 28 |
+
import tempfile
|
| 29 |
+
|
| 30 |
+
MEDIA_DIR = os.environ.get("NAME_MEDIA_DIR", os.path.join(tempfile.gettempdir(), "name_media"))
|
| 31 |
+
os.makedirs(MEDIA_DIR, exist_ok=True)
|
| 32 |
+
|
| 33 |
+
# Logging setup
|
| 34 |
+
LOG_FILE = os.path.join(MEDIA_DIR, "app_errors.log")
|
| 35 |
+
logging.basicConfig(filename=LOG_FILE, level=logging.ERROR,
|
| 36 |
+
format="%(asctime)s - %(levelname)s - %(message)s")
|
| 37 |
+
|
| 38 |
+
animator_obj = {}
|
| 39 |
+
|
| 40 |
+
# ---------------- Widgets ----------------
|
| 41 |
+
file_input = pn.widgets.FileInput(accept=".zip")
|
| 42 |
+
process_button = pn.widgets.Button(name="📦 Process ZIP", button_type="primary")
|
| 43 |
+
reset_button = pn.widgets.Button(name="🔄 Reset App", button_type="danger")
|
| 44 |
+
status = pn.pane.Markdown("### Upload a NAME Model ZIP to begin")
|
| 45 |
+
|
| 46 |
+
download_button = pn.widgets.FileDownload(
|
| 47 |
+
label="⬇️ Download All Exports",
|
| 48 |
+
filename="all_exports.zip",
|
| 49 |
+
button_type="success",
|
| 50 |
+
callback=lambda: io.BytesIO(
|
| 51 |
+
open(shutil.make_archive(
|
| 52 |
+
os.path.join(MEDIA_DIR, "all_exports").replace(".zip", ""),
|
| 53 |
+
"zip", MEDIA_DIR
|
| 54 |
+
), 'rb').read()
|
| 55 |
+
)
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
log_link = pn.widgets.FileDownload(
|
| 59 |
+
label="🪵 View Error Log", file=LOG_FILE,
|
| 60 |
+
filename="app_errors.log", button_type="warning"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
threshold_slider_3d = pn.widgets.FloatSlider(name='3D Threshold', start=0.0, end=1.0, step=0.05, value=0.1)
|
| 64 |
+
zoom_slider_3d = pn.widgets.IntSlider(name='3D Zoom Level', start=1, end=20, value=19)
|
| 65 |
+
cmap_select_3d = pn.widgets.Select(name='3D Colormap', options=["rainbow", "viridis", "plasma"])
|
| 66 |
+
fps_slider_3d = pn.widgets.IntSlider(name='3D FPS', start=1, end=10, value=2)
|
| 67 |
+
Altitude_slider = pn.widgets.IntSlider(name='Define Ash Altitude', start=1, end=15, value=1)
|
| 68 |
+
|
| 69 |
+
threshold_slider_2d = pn.widgets.FloatSlider(name='2D Threshold', start=0.0, end=1.0, step=0.01, value=0.005)
|
| 70 |
+
zoom_slider_2d = pn.widgets.IntSlider(name='2D Zoom Level', start=1, end=20, value=19)
|
| 71 |
+
fps_slider_2d = pn.widgets.IntSlider(name='2D FPS', start=1, end=10, value=2)
|
| 72 |
+
cmap_select_2d = pn.widgets.Select(name='2D Colormap', options=["rainbow", "viridis", "plasma"])
|
| 73 |
+
|
| 74 |
+
# ---------------- Core Functions ----------------
|
| 75 |
+
def process_zip(event=None):
|
| 76 |
+
if file_input.value:
|
| 77 |
+
zip_path = os.path.join(MEDIA_DIR, file_input.filename)
|
| 78 |
+
with open(zip_path, "wb") as f:
|
| 79 |
+
f.write(file_input.value)
|
| 80 |
+
status.object = "✅ ZIP uploaded and saved."
|
| 81 |
+
else:
|
| 82 |
+
zip_path = os.path.join(MEDIA_DIR, "default_model.zip")
|
| 83 |
+
if not os.path.exists(zip_path):
|
| 84 |
+
zip_path = "default_model.zip" # fallback to local directory
|
| 85 |
+
if not os.path.exists(zip_path):
|
| 86 |
+
status.object = "❌ No ZIP uploaded and default_model.zip not found."
|
| 87 |
+
return
|
| 88 |
+
status.object = "📦 Using default_model.zip"
|
| 89 |
+
|
| 90 |
|
| 91 |
+
try:
|
| 92 |
+
output_dir = os.path.join("./", "ash_output")
|
| 93 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 94 |
+
except PermissionError:
|
| 95 |
+
output_dir = os.path.join(tempfile.gettempdir(), "name_output")
|
| 96 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 97 |
+
shutil.rmtree(output_dir, ignore_errors=True)
|
| 98 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
processor = NAMEDataProcessor(output_root=output_dir)
|
| 102 |
+
processor.batch_process_zip(zip_path)
|
| 103 |
|
| 104 |
+
# animator_obj["3d"] = [xr.open_dataset(fp).load()
|
| 105 |
+
# for fp in sorted(glob.glob(os.path.join(output_dir, "3D", "*.nc")))]
|
| 106 |
+
|
| 107 |
+
# animator_obj["3d"] = []
|
| 108 |
+
# for fp in sorted(glob.glob(os.path.join(output_dir, "3D", "*.nc"))):
|
| 109 |
+
# with xr.open_dataset(fp) as ds:
|
| 110 |
+
# animator_obj["3d"].append(ds.load())
|
| 111 |
+
animator_obj["3d"] = []
|
| 112 |
+
for fp in sorted(glob.glob(os.path.join(output_dir, "3D", "*.nc"))):
|
| 113 |
+
with xr.open_dataset(fp) as ds:
|
| 114 |
+
animator_obj["3d"].append(ds.load())
|
| 115 |
|
| 116 |
+
animator_obj["2d"] = []
|
| 117 |
+
for fp in sorted(glob.glob(os.path.join(output_dir, "horizontal", "*.nc"))):
|
| 118 |
+
with xr.open_dataset(fp) as ds:
|
| 119 |
+
animator_obj["2d"].append(ds.load())
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
|
| 122 |
+
# animator_obj["2d"] = [xr.open_dataset(fp).load()
|
| 123 |
+
# for fp in sorted(glob.glob(os.path.join(output_dir, "horizontal", "*.nc")))]
|
| 124 |
|
| 125 |
+
with open(os.path.join(MEDIA_DIR, "last_run.txt"), "w") as f:
|
| 126 |
+
f.write(zip_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
status.object += f" | ✅ Loaded 3D: {len(animator_obj['3d'])} & 2D: {len(animator_obj['2d'])}"
|
| 129 |
+
update_media_tabs()
|
| 130 |
+
except Exception as e:
|
| 131 |
+
logging.exception("Error during ZIP processing")
|
| 132 |
+
status.object = f"❌ Processing failed: {e}"
|
| 133 |
|
| 134 |
+
def reset_app(event=None):
|
| 135 |
+
animator_obj.clear()
|
| 136 |
+
file_input.value = None
|
| 137 |
+
status.object = "🔄 App has been reset."
|
| 138 |
+
for folder in ["ash_output", "2D", "3D"]:
|
| 139 |
+
shutil.rmtree(os.path.join(MEDIA_DIR, folder), ignore_errors=True)
|
| 140 |
+
if os.path.exists(os.path.join(MEDIA_DIR, "last_run.txt")):
|
| 141 |
+
os.remove(os.path.join(MEDIA_DIR, "last_run.txt"))
|
| 142 |
+
update_media_tabs()
|
| 143 |
+
|
| 144 |
+
def restore_previous_session():
|
| 145 |
+
try:
|
| 146 |
+
state_file = os.path.join(MEDIA_DIR, "last_run.txt")
|
| 147 |
+
if os.path.exists(state_file):
|
| 148 |
+
with open(state_file) as f:
|
| 149 |
+
zip_path = f.read().strip()
|
| 150 |
+
if os.path.exists(zip_path):
|
| 151 |
+
try:
|
| 152 |
+
output_dir = os.path.join("./", "ash_output")
|
| 153 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 154 |
+
except PermissionError:
|
| 155 |
+
output_dir = os.path.join(tempfile.gettempdir(), "name_output")
|
| 156 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 157 |
|
| 158 |
+
animator_obj["3d"] = []
|
| 159 |
+
for fp in sorted(glob.glob(os.path.join(output_dir, "3D", "*.nc"))):
|
| 160 |
+
with xr.open_dataset(fp) as ds:
|
| 161 |
+
animator_obj["3d"].append(ds.load())
|
| 162 |
|
| 163 |
+
animator_obj["2d"] = []
|
| 164 |
+
for fp in sorted(glob.glob(os.path.join(output_dir, "horizontal", "*.nc"))):
|
| 165 |
+
with xr.open_dataset(fp) as ds:
|
| 166 |
+
animator_obj["2d"].append(ds.load())
|
| 167 |
+
|
| 168 |
+
status.object = f"🔁 Restored previous session from {os.path.basename(zip_path)}"
|
| 169 |
+
update_media_tabs()
|
| 170 |
+
except Exception as e:
|
| 171 |
+
logging.exception("Error restoring previous session")
|
| 172 |
+
status.object = f"⚠️ Could not restore previous session: {e}"
|
| 173 |
+
|
| 174 |
+
process_button.on_click(process_zip)
|
| 175 |
+
reset_button.on_click(reset_app)
|
| 176 |
+
|
| 177 |
+
# ---------------- Animator Builders ----------------
|
| 178 |
+
def build_animator_3d():
|
| 179 |
+
ds = animator_obj["3d"]
|
| 180 |
+
attrs = ds[0].attrs
|
| 181 |
+
lons, lats, grid = create_grid(attrs)
|
| 182 |
+
return SimpleNamespace(
|
| 183 |
+
datasets=ds,
|
| 184 |
+
levels=ds[0].altitude.values,
|
| 185 |
+
lons=lons,
|
| 186 |
+
lats=lats,
|
| 187 |
+
lon_grid=grid[0],
|
| 188 |
+
lat_grid=grid[1],
|
| 189 |
)
|
| 190 |
+
|
| 191 |
+
def build_animator_2d():
|
| 192 |
+
ds = animator_obj["2d"]
|
| 193 |
+
lat_grid, lon_grid = xr.broadcast(ds[0]["latitude"], ds[0]["longitude"])
|
| 194 |
+
return SimpleNamespace(
|
| 195 |
+
datasets=ds,
|
| 196 |
+
lats=ds[0]["latitude"].values,
|
| 197 |
+
lons=ds[0]["longitude"].values,
|
| 198 |
+
lat_grid=lat_grid.values,
|
| 199 |
+
lon_grid=lon_grid.values,
|
| 200 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
+
# ---------------- Plot Functions ----------------
|
| 203 |
+
def plot_z_level():
|
| 204 |
+
try:
|
| 205 |
+
animator = build_animator_3d()
|
| 206 |
+
out = os.path.join(MEDIA_DIR, "3D")
|
| 207 |
+
os.makedirs(out, exist_ok=True)
|
| 208 |
+
Plot_3DField_Data(animator, out, cmap_select_3d.value,
|
| 209 |
+
threshold_slider_3d.value, zoom_slider_3d.value,
|
| 210 |
+
fps_slider_3d.value).plot_single_z_level(
|
| 211 |
+
Altitude_slider.value, f"ash_altitude{Altitude_slider.value}km_runTimes.gif")
|
| 212 |
+
update_media_tabs()
|
| 213 |
+
status.object = "✅ Z-Level animation created."
|
| 214 |
+
except Exception as e:
|
| 215 |
+
logging.exception("Error in plot_z_level")
|
| 216 |
+
status.object = f"❌ Error in Z-Level animation: {e}"
|
| 217 |
|
| 218 |
+
def plot_vertical_profile():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
try:
|
| 220 |
+
animator = build_animator_3d()
|
| 221 |
+
out = os.path.join(MEDIA_DIR, "3D")
|
| 222 |
+
os.makedirs(out, exist_ok=True)
|
| 223 |
+
plotter = Plot_3DField_Data(animator, out, cmap_select_3d.value, fps_slider_3d.value,
|
| 224 |
+
threshold_slider_3d.value, zoom_level=zoom_slider_3d.value,
|
| 225 |
+
basemap_type='basemap')
|
| 226 |
+
plotter.plot_vertical_profile_at_time(Altitude_slider.value - 1,
|
| 227 |
+
filename=f"T{Altitude_slider.value - 1}_profile.gif")
|
| 228 |
+
update_media_tabs()
|
| 229 |
+
status.object = "✅ Vertical profile animation created."
|
| 230 |
+
except Exception as e:
|
| 231 |
+
logging.exception("Error in plot_vertical_profile")
|
| 232 |
+
status.object = f"❌ Error in vertical profile animation: {e}"
|
| 233 |
+
|
| 234 |
+
def animate_all_altitude_profiles():
|
| 235 |
+
try:
|
| 236 |
+
animator = build_animator_3d()
|
| 237 |
+
out = os.path.join(MEDIA_DIR, "3D")
|
| 238 |
+
Plot_3DField_Data(animator, out, cmap_select_3d.value,
|
| 239 |
+
threshold_slider_3d.value, zoom_slider_3d.value).animate_all_altitude_profiles()
|
| 240 |
+
update_media_tabs()
|
| 241 |
+
status.object = "✅ All altitude profile animations created."
|
| 242 |
+
except Exception as e:
|
| 243 |
+
logging.exception("Error in animate_all_altitude_profiles")
|
| 244 |
+
status.object = f"❌ Error animating all altitude profiles: {e}"
|
| 245 |
+
|
| 246 |
+
def export_jpg_frames():
|
| 247 |
+
try:
|
| 248 |
+
animator = build_animator_3d()
|
| 249 |
+
out = os.path.join(MEDIA_DIR, "3D")
|
| 250 |
+
Plot_3DField_Data(animator, out, cmap_select_3d.value,
|
| 251 |
+
threshold_slider_3d.value, zoom_slider_3d.value).export_frames_as_jpgs(include_metadata=True)
|
| 252 |
+
update_media_tabs()
|
| 253 |
+
status.object = "✅ JPG frames exported."
|
| 254 |
+
except Exception as e:
|
| 255 |
+
logging.exception("Error exporting JPG frames")
|
| 256 |
+
status.object = f"❌ Error exporting JPG frames: {e}"
|
| 257 |
+
|
| 258 |
+
def plot_2d_field(field):
|
| 259 |
+
try:
|
| 260 |
+
animator = build_animator_2d()
|
| 261 |
+
out = os.path.join(MEDIA_DIR, "2D")
|
| 262 |
+
Plot_Horizontal_Data(animator, out, cmap_select_2d.value, fps_slider_2d.value,
|
| 263 |
+
include_metadata=True, threshold=threshold_slider_2d.value,
|
| 264 |
+
zoom_width_deg=6.0, zoom_height_deg=6.0,
|
| 265 |
+
zoom_level=zoom_slider_2d.value,
|
| 266 |
+
static_frame_export=True).plot_single_field_over_time(field, f"{field}.gif")
|
| 267 |
+
update_media_tabs()
|
| 268 |
+
status.object = f"✅ 2D field `{field}` animation created."
|
| 269 |
+
except Exception as e:
|
| 270 |
+
logging.exception(f"Error in plot_2d_field: {field}")
|
| 271 |
+
status.object = f"❌ Error in 2D field `{field}` animation: {e}"
|
| 272 |
+
|
| 273 |
+
# ---------------- Layout ----------------
|
| 274 |
+
def human_readable_size(size):
|
| 275 |
+
for unit in ['B', 'KB', 'MB', 'GB']:
|
| 276 |
+
if size < 1024: return f"{size:.1f} {unit}"
|
| 277 |
+
size /= 1024
|
| 278 |
+
return f"{size:.1f} TB"
|
| 279 |
+
|
| 280 |
+
# def generate_output_gallery(base_folder):
|
| 281 |
+
# grouped = defaultdict(lambda: defaultdict(list))
|
| 282 |
+
# for root, _, files in os.walk(os.path.join(MEDIA_DIR, base_folder)):
|
| 283 |
+
# for file in files:
|
| 284 |
+
# ext = os.path.splitext(file)[1].lower()
|
| 285 |
+
# subfolder = os.path.relpath(root, MEDIA_DIR)
|
| 286 |
+
# grouped[subfolder][ext].append(os.path.join(root, file))
|
| 287 |
+
|
| 288 |
+
# folder_tabs = []
|
| 289 |
+
# for subfolder, ext_files in sorted(grouped.items()):
|
| 290 |
+
# type_tabs = []
|
| 291 |
+
# for ext, paths in sorted(ext_files.items()):
|
| 292 |
+
# previews = []
|
| 293 |
+
# for path in sorted(paths, key=os.path.getmtime, reverse=True):
|
| 294 |
+
# size = human_readable_size(os.path.getsize(path))
|
| 295 |
+
# mod = datetime.fromtimestamp(os.path.getmtime(path)).strftime("%Y-%m-%d %H:%M")
|
| 296 |
+
# title = f"**{os.path.basename(path)}**\\n_{size}, {mod}_"
|
| 297 |
+
# download = pn.widgets.FileDownload(label="⬇", file=path, filename=os.path.basename(path), width=60)
|
| 298 |
+
# if ext in [".gif", ".png", ".jpg", ".jpeg"]:
|
| 299 |
+
# preview = pn.pane.Image(path, width=320)
|
| 300 |
+
# else:
|
| 301 |
+
# with open(path, "r", errors="ignore") as f:
|
| 302 |
+
# content = f.read(2048)
|
| 303 |
+
# preview = pn.pane.PreText(content, width=320)
|
| 304 |
+
# card = pn.Card(pn.pane.Markdown(title), preview, pn.Row(download), width=360)
|
| 305 |
+
# previews.append(card)
|
| 306 |
+
# type_tabs.append((ext.upper(), pn.GridBox(*previews, ncols=2)))
|
| 307 |
+
# folder_tabs.append((subfolder, pn.Tabs(*type_tabs)))
|
| 308 |
+
# return pn.Tabs(*folder_tabs)
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
def generate_output_gallery(base_folder):
|
| 312 |
+
preview_container = pn.Column(width=640, height=550)
|
| 313 |
+
preview_container.append(pn.pane.Markdown("👈 Click a thumbnail to preview"))
|
| 314 |
+
folder_cards = []
|
| 315 |
+
|
| 316 |
+
def make_preview(file_path):
|
| 317 |
+
ext = os.path.splitext(file_path)[1].lower()
|
| 318 |
+
title = pn.pane.Markdown(f"### {os.path.basename(file_path)}")
|
| 319 |
+
download_button = pn.widgets.FileDownload(file=file_path, filename=os.path.basename(file_path),
|
| 320 |
+
label="⬇ Download", button_type="success", width=150)
|
| 321 |
+
|
| 322 |
+
if ext in [".gif", ".png", ".jpg", ".jpeg"]:
|
| 323 |
+
content = pn.pane.Image(file_path, width=640, height=450, sizing_mode="fixed")
|
| 324 |
+
else:
|
| 325 |
+
try:
|
| 326 |
+
with open(file_path, 'r', errors="ignore") as f:
|
| 327 |
+
text = f.read(2048)
|
| 328 |
+
content = pn.pane.PreText(text, width=640, height=450)
|
| 329 |
+
except:
|
| 330 |
+
content = pn.pane.Markdown("*Unable to preview this file.*")
|
| 331 |
+
|
| 332 |
+
return pn.Column(title, content, download_button)
|
| 333 |
+
|
| 334 |
+
grouped = defaultdict(list)
|
| 335 |
+
for root, _, files in os.walk(os.path.join(MEDIA_DIR, base_folder)):
|
| 336 |
+
for file in sorted(files):
|
| 337 |
+
full_path = os.path.join(root, file)
|
| 338 |
+
if not os.path.exists(full_path):
|
| 339 |
+
continue
|
| 340 |
+
rel_folder = os.path.relpath(root, os.path.join(MEDIA_DIR, base_folder))
|
| 341 |
+
grouped[rel_folder].append(full_path)
|
| 342 |
+
|
| 343 |
+
for folder, file_paths in sorted(grouped.items()):
|
| 344 |
+
thumbnails = []
|
| 345 |
+
for full_path in file_paths:
|
| 346 |
+
filename = os.path.basename(full_path)
|
| 347 |
+
ext = os.path.splitext(full_path)[1].lower()
|
| 348 |
+
|
| 349 |
+
if ext in [".gif", ".png", ".jpg", ".jpeg"]:
|
| 350 |
+
img = pn.pane.Image(full_path, width=140, height=100)
|
| 351 |
+
else:
|
| 352 |
+
img = pn.pane.Markdown("📄", width=140, height=100)
|
| 353 |
+
|
| 354 |
+
view_button = pn.widgets.Button(name="👁", width=40, height=30, button_type="primary")
|
| 355 |
+
|
| 356 |
+
def click_handler(path=full_path):
|
| 357 |
+
def inner_click(event):
|
| 358 |
+
preview_container[:] = [make_preview(path)]
|
| 359 |
+
return inner_click
|
| 360 |
+
|
| 361 |
+
view_button.on_click(click_handler())
|
| 362 |
+
|
| 363 |
+
overlay = pn.Column(pn.Row(pn.Spacer(width=90), view_button), img, width=160)
|
| 364 |
+
label_md = pn.pane.Markdown(f"**{filename}**", width=140, height=35)
|
| 365 |
+
thumb_card = pn.Column(overlay, label_md, width=160)
|
| 366 |
+
thumbnails.append(thumb_card)
|
| 367 |
+
|
| 368 |
+
folder_card = pn.Card(pn.GridBox(*thumbnails, ncols=2), title=f"📁 {folder}", width=400, collapsible=True)
|
| 369 |
+
folder_cards.append(folder_card)
|
| 370 |
+
|
| 371 |
+
folder_scroll = pn.Column(*folder_cards, scroll=True, height=600, width=420)
|
| 372 |
+
return pn.Row(preview_container, pn.Spacer(width=20), folder_scroll)
|
| 373 |
+
|
| 374 |
+
def update_media_tabs():
|
| 375 |
+
media_tab_2d.objects[:] = [generate_output_gallery("2D")]
|
| 376 |
+
media_tab_3d.objects[:] = [generate_output_gallery("3D")]
|
| 377 |
+
|
| 378 |
+
media_tab_2d = pn.Column(generate_output_gallery("2D"))
|
| 379 |
+
media_tab_3d = pn.Column(generate_output_gallery("3D"))
|
| 380 |
+
|
| 381 |
+
media_tab = pn.Tabs(
|
| 382 |
+
("2D Outputs", media_tab_2d),
|
| 383 |
+
("3D Outputs", media_tab_3d)
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
|
| 387 |
+
tab3d = pn.Column(
|
| 388 |
+
threshold_slider_3d, zoom_slider_3d, fps_slider_3d, Altitude_slider, cmap_select_3d,
|
| 389 |
+
pn.widgets.Button(name="🎞 Generate animation at selected altitude level", button_type="primary", on_click=lambda e: tab3d.append(plot_z_level())),
|
| 390 |
+
pn.widgets.Button(name="📈 Generate vertical profile animation at time index", button_type="primary", on_click=lambda e: tab3d.append(plot_vertical_profile())),
|
| 391 |
+
pn.widgets.Button(name="📊 Generate all altitude level animations", button_type="primary", on_click=lambda e: tab3d.append(animate_all_altitude_profiles())),
|
| 392 |
+
pn.widgets.Button(name="🖼 Export all animation frames as JPG", button_type="primary", on_click=lambda e: tab3d.append(export_jpg_frames())),
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
tab2d = pn.Column(
|
| 396 |
+
threshold_slider_2d, zoom_slider_2d, fps_slider_2d, cmap_select_2d,
|
| 397 |
+
pn.widgets.Button(name="🌫 Animate Air Concentration", button_type="primary", on_click=lambda e: tab2d.append(plot_2d_field("air_concentration"))),
|
| 398 |
+
pn.widgets.Button(name="🌧 Animate Dry Deposition Rate", button_type="primary", on_click=lambda e: tab2d.append(plot_2d_field("dry_deposition_rate"))),
|
| 399 |
+
pn.widgets.Button(name="💧 Animate Wet Deposition Rate", button_type="primary", on_click=lambda e: tab2d.append(plot_2d_field("wet_deposition_rate"))),
|
| 400 |
+
)
|
| 401 |
+
|
| 402 |
+
help_tab = pn.Column(pn.pane.Markdown("""
|
| 403 |
+
## ❓ How to Use the NAME Ash Visualizer
|
| 404 |
+
|
| 405 |
+
This dashboard allows users to upload and visualize outputs from the NAME ash dispersion model.
|
| 406 |
+
|
| 407 |
+
### 🧭 Workflow
|
| 408 |
+
1. **Upload ZIP** containing NetCDF files from the NAME model.
|
| 409 |
+
2. Use **3D and 2D tabs** to configure and generate animations.
|
| 410 |
+
3. Use **Media Viewer** to preview and download results.
|
| 411 |
+
|
| 412 |
+
### 🧳 ZIP Structure
|
| 413 |
+
```
|
| 414 |
+
## 🗂 How Uploaded ZIP is Processed
|
| 415 |
+
|
| 416 |
+
```text
|
| 417 |
+
┌────────────────────────────────────────────┐
|
| 418 |
+
│ Uploaded ZIP (.zip) │
|
| 419 |
+
│ (e.g. Taal_273070_20200112_scenario_*.zip)│
|
| 420 |
+
└────────────────────────────────────────────┘
|
| 421 |
+
│
|
| 422 |
+
▼
|
| 423 |
+
┌───────────────────────────────┐
|
| 424 |
+
│ Contains: raw .txt outputs │
|
| 425 |
+
│ - AQOutput_3DField_*.txt │
|
| 426 |
+
│ - AQOutput_horizontal_*.txt │
|
| 427 |
+
└───────────────────────────────┘
|
| 428 |
+
│
|
| 429 |
+
▼
|
| 430 |
+
┌────────────────────────────────────────┐
|
| 431 |
+
│ NAMEDataProcessor.batch_process_zip()│
|
| 432 |
+
└────────────────────────────────────────┘
|
| 433 |
+
│
|
| 434 |
+
▼
|
| 435 |
+
┌─────────────────────────────┐
|
| 436 |
+
│ Converts to NetCDF files │
|
| 437 |
+
│ - ash_output/3D/*.nc │
|
| 438 |
+
│ - ash_output/horizontal/*.nc │
|
| 439 |
+
└─────────────────────────────┘
|
| 440 |
+
│
|
| 441 |
+
▼
|
| 442 |
+
┌─────────────────────────────────────┐
|
| 443 |
+
│ View & animate in 3D/2D tabs │
|
| 444 |
+
│ Download results in Media Viewer │
|
| 445 |
+
└─────────────────────────────────────┘
|
| 446 |
+
|
| 447 |
+
```
|
| 448 |
+
|
| 449 |
+
### 📢 Tips
|
| 450 |
+
- Reset the app with 🔄 if needed.
|
| 451 |
+
- View logs if an error occurs.
|
| 452 |
+
- Outputs are temporary per session.
|
| 453 |
+
""", sizing_mode="stretch_width"))
|
| 454 |
+
|
| 455 |
+
tabs = pn.Tabs(
|
| 456 |
+
("🧱 3D Field", tab3d),
|
| 457 |
+
("🌍 2D Field", tab2d),
|
| 458 |
+
("📁 Media Viewer", media_tab),
|
| 459 |
+
("❓ Help", help_tab)
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
+
sidebar = pn.Column(
|
| 463 |
+
pn.pane.Markdown("## 🌋 NAME Ash Visualizer", sizing_mode="stretch_width"),
|
| 464 |
+
pn.Card(pn.Column(file_input, process_button, reset_button, sizing_mode="stretch_width"),
|
| 465 |
+
title="📂 File Upload & Processing", collapsible=True, sizing_mode="stretch_width"),
|
| 466 |
+
pn.Card(pn.Column(download_button, log_link, sizing_mode="stretch_width"),
|
| 467 |
+
title="📁 Downloads & Logs", collapsible=True, sizing_mode="stretch_width"),
|
| 468 |
+
pn.Card(status, title="📢 Status", collapsible=True, sizing_mode="stretch_width"),
|
| 469 |
+
sizing_mode="stretch_width")
|
| 470 |
+
|
| 471 |
+
restore_previous_session()
|
| 472 |
+
|
| 473 |
+
pn.template.FastListTemplate(
|
| 474 |
+
title="NAME Visualizer Dashboard",
|
| 475 |
+
sidebar=sidebar,
|
| 476 |
+
main=[tabs],
|
| 477 |
+
).servable()
|
| 478 |
+
|
| 479 |
+
'''
|
| 480 |
+
import os
|
| 481 |
+
import glob
|
| 482 |
+
import shutil
|
| 483 |
+
import io
|
| 484 |
+
import logging
|
| 485 |
+
import panel as pn
|
| 486 |
+
import xarray as xr
|
| 487 |
+
import numpy as np
|
| 488 |
+
from datetime import datetime
|
| 489 |
+
from types import SimpleNamespace
|
| 490 |
+
from collections import defaultdict
|
| 491 |
+
from ash_animator.converter import NAMEDataProcessor
|
| 492 |
+
from ash_animator.plot_3dfield_data import Plot_3DField_Data
|
| 493 |
+
from ash_animator.plot_horizontal_data import Plot_Horizontal_Data
|
| 494 |
+
from ash_animator import create_grid
|
| 495 |
+
import tempfile
|
| 496 |
+
|
| 497 |
+
pn.extension()
|
| 498 |
+
|
| 499 |
+
MEDIA_DIR = os.environ.get("NAME_MEDIA_DIR", os.path.join(tempfile.gettempdir(), "name_media"))
|
| 500 |
+
os.makedirs(MEDIA_DIR, exist_ok=True)
|
| 501 |
+
|
| 502 |
+
LOG_FILE = os.path.join(MEDIA_DIR, "app_errors.log")
|
| 503 |
+
logging.basicConfig(filename=LOG_FILE, level=logging.ERROR, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 504 |
+
|
| 505 |
+
animator_obj = {}
|
| 506 |
+
|
| 507 |
+
file_input = pn.widgets.FileInput(accept=".zip")
|
| 508 |
+
process_button = pn.widgets.Button(name="📦 Process ZIP", button_type="primary")
|
| 509 |
+
reset_button = pn.widgets.Button(name="🔄 Reset App", button_type="danger")
|
| 510 |
+
status = pn.pane.Markdown("### Upload a NAME Model ZIP to begin")
|
| 511 |
+
|
| 512 |
+
download_button = pn.widgets.FileDownload(
|
| 513 |
+
label="⬇️ Download All Exports", filename="all_exports.zip", button_type="success",
|
| 514 |
+
callback=lambda: io.BytesIO(open(shutil.make_archive(os.path.join(MEDIA_DIR, "all_exports").replace(".zip", ""), "zip", MEDIA_DIR), 'rb').read())
|
| 515 |
+
)
|
| 516 |
+
|
| 517 |
+
log_link = pn.widgets.FileDownload(label="🪵 View Error Log", file=LOG_FILE, filename="app_errors.log", button_type="warning")
|
| 518 |
+
|
| 519 |
+
threshold_slider_3d = pn.widgets.FloatSlider(name='3D Threshold', start=0.0, end=1.0, step=0.05, value=0.1)
|
| 520 |
+
zoom_slider_3d = pn.widgets.IntSlider(name='3D Zoom Level', start=1, end=20, value=19)
|
| 521 |
+
cmap_select_3d = pn.widgets.Select(name='3D Colormap', options=["rainbow", "viridis", "plasma"])
|
| 522 |
+
fps_slider_3d = pn.widgets.IntSlider(name='3D FPS', start=1, end=10, value=2)
|
| 523 |
+
Altitude_slider = pn.widgets.IntSlider(name='Define Ash Altitude', start=1, end=15, value=1)
|
| 524 |
+
|
| 525 |
+
threshold_slider_2d = pn.widgets.FloatSlider(name='2D Threshold', start=0.0, end=1.0, step=0.01, value=0.005)
|
| 526 |
+
zoom_slider_2d = pn.widgets.IntSlider(name='2D Zoom Level', start=1, end=20, value=19)
|
| 527 |
+
fps_slider_2d = pn.widgets.IntSlider(name='2D FPS', start=1, end=10, value=2)
|
| 528 |
+
cmap_select_2d = pn.widgets.Select(name='2D Colormap', options=["rainbow", "viridis", "plasma"])
|
| 529 |
+
|
| 530 |
+
def process_zip(event=None):
|
| 531 |
+
if file_input.value:
|
| 532 |
+
zip_path = os.path.join(MEDIA_DIR, file_input.filename)
|
| 533 |
+
with open(zip_path, "wb") as f:
|
| 534 |
+
f.write(file_input.value)
|
| 535 |
+
status.object = "✅ ZIP uploaded and saved."
|
| 536 |
+
else:
|
| 537 |
+
zip_path = os.path.join(MEDIA_DIR, "default_model.zip")
|
| 538 |
+
if not os.path.exists(zip_path):
|
| 539 |
+
zip_path = "default_model.zip"
|
| 540 |
+
if not os.path.exists(zip_path):
|
| 541 |
+
status.object = "❌ No ZIP uploaded and default_model.zip not found."
|
| 542 |
return
|
| 543 |
+
status.object = "📦 Using default_model.zip"
|
| 544 |
+
|
| 545 |
+
try:
|
| 546 |
+
output_dir = os.path.join("./", "ash_output")
|
| 547 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 548 |
+
except PermissionError:
|
| 549 |
+
output_dir = os.path.join(tempfile.gettempdir(), "name_output")
|
| 550 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 551 |
+
shutil.rmtree(output_dir, ignore_errors=True)
|
| 552 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 553 |
+
|
| 554 |
+
try:
|
| 555 |
+
processor = NAMEDataProcessor(output_root=output_dir)
|
| 556 |
+
processor.batch_process_zip(zip_path)
|
| 557 |
+
|
| 558 |
+
animator_obj["3d"] = [xr.open_dataset(fp).load() for fp in sorted(glob.glob(os.path.join(output_dir, "3D", "*.nc")))]
|
| 559 |
+
animator_obj["2d"] = [xr.open_dataset(fp).load() for fp in sorted(glob.glob(os.path.join(output_dir, "horizontal", "*.nc")))]
|
| 560 |
+
|
| 561 |
+
with open(os.path.join(MEDIA_DIR, "last_run.txt"), "w") as f:
|
| 562 |
+
f.write(zip_path)
|
| 563 |
+
|
| 564 |
+
status.object += f" | ✅ Loaded 3D: {len(animator_obj['3d'])} & 2D: {len(animator_obj['2d'])}"
|
| 565 |
+
update_media_tabs()
|
| 566 |
+
except Exception as e:
|
| 567 |
+
logging.exception("Error during ZIP processing")
|
| 568 |
+
status.object = f"❌ Processing failed: {e}"
|
| 569 |
+
|
| 570 |
+
def reset_app(event=None):
|
| 571 |
+
animator_obj.clear()
|
| 572 |
+
file_input.value = None
|
| 573 |
+
status.object = "🔄 App has been reset."
|
| 574 |
+
for folder in ["ash_output", "2D", "3D"]:
|
| 575 |
+
shutil.rmtree(os.path.join(MEDIA_DIR, folder), ignore_errors=True)
|
| 576 |
+
if os.path.exists(os.path.join(MEDIA_DIR, "last_run.txt")):
|
| 577 |
+
os.remove(os.path.join(MEDIA_DIR, "last_run.txt"))
|
| 578 |
+
update_media_tabs()
|
| 579 |
+
|
| 580 |
+
def restore_previous_session():
|
| 581 |
+
try:
|
| 582 |
+
state_file = os.path.join(MEDIA_DIR, "last_run.txt")
|
| 583 |
+
if os.path.exists(state_file):
|
| 584 |
+
with open(state_file) as f:
|
| 585 |
+
zip_path = f.read().strip()
|
| 586 |
+
if os.path.exists(zip_path):
|
| 587 |
+
output_dir = os.path.join("./", "ash_output")
|
| 588 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 589 |
+
animator_obj["3d"] = [xr.open_dataset(fp).load() for fp in sorted(glob.glob(os.path.join(output_dir, "3D", "*.nc")))]
|
| 590 |
+
animator_obj["2d"] = [xr.open_dataset(fp).load() for fp in sorted(glob.glob(os.path.join(output_dir, "horizontal", "*.nc")))]
|
| 591 |
+
status.object = f"🔁 Restored previous session from {os.path.basename(zip_path)}"
|
| 592 |
+
update_media_tabs()
|
| 593 |
+
except Exception as e:
|
| 594 |
+
logging.exception("Error restoring previous session")
|
| 595 |
+
status.object = f"⚠️ Could not restore previous session: {e}"
|
| 596 |
+
|
| 597 |
+
process_button.on_click(process_zip)
|
| 598 |
+
reset_button.on_click(reset_app)
|
| 599 |
+
|
| 600 |
+
def build_animator_3d():
|
| 601 |
+
ds = animator_obj["3d"]
|
| 602 |
+
attrs = ds[0].attrs
|
| 603 |
+
lons, lats, grid = create_grid(attrs)
|
| 604 |
+
return SimpleNamespace(datasets=ds, levels=ds[0].altitude.values, lons=lons, lats=lats,
|
| 605 |
+
lon_grid=grid[0], lat_grid=grid[1])
|
| 606 |
+
|
| 607 |
+
def build_animator_2d():
|
| 608 |
+
ds = animator_obj["2d"]
|
| 609 |
+
lat_grid, lon_grid = xr.broadcast(ds[0]["latitude"], ds[0]["longitude"])
|
| 610 |
+
return SimpleNamespace(datasets=ds, lats=ds[0]["latitude"].values,
|
| 611 |
+
lons=ds[0]["longitude"].values,
|
| 612 |
+
lat_grid=lat_grid.values, lon_grid=lon_grid.values)
|
| 613 |
+
|
| 614 |
+
def plot_z_level():
|
| 615 |
+
try:
|
| 616 |
+
animator = build_animator_3d()
|
| 617 |
+
out = os.path.join(MEDIA_DIR, "3D")
|
| 618 |
+
os.makedirs(out, exist_ok=True)
|
| 619 |
+
Plot_3DField_Data(animator, out, cmap_select_3d.value, threshold_slider_3d.value,
|
| 620 |
+
zoom_slider_3d.value, fps_slider_3d.value).plot_single_z_level(
|
| 621 |
+
Altitude_slider.value, f"ash_altitude{Altitude_slider.value}km_runTimes.gif")
|
| 622 |
+
update_media_tabs()
|
| 623 |
+
status.object = "✅ Z-Level animation created."
|
| 624 |
+
except Exception as e:
|
| 625 |
+
logging.exception("Error in plot_z_level")
|
| 626 |
+
status.object = f"❌ Error in Z-Level animation: {e}"
|
| 627 |
+
|
| 628 |
+
def plot_vertical_profile():
|
| 629 |
+
try:
|
| 630 |
+
animator = build_animator_3d()
|
| 631 |
+
out = os.path.join(MEDIA_DIR, "3D")
|
| 632 |
+
os.makedirs(out, exist_ok=True)
|
| 633 |
+
Plot_3DField_Data(animator, out, cmap_select_3d.value, fps_slider_3d.value,
|
| 634 |
+
threshold_slider_3d.value, zoom_level=zoom_slider_3d.value).plot_vertical_profile_at_time(
|
| 635 |
+
Altitude_slider.value - 1, f"T{Altitude_slider.value - 1}_profile.gif")
|
| 636 |
+
update_media_tabs()
|
| 637 |
+
status.object = "✅ Vertical profile animation created."
|
| 638 |
+
except Exception as e:
|
| 639 |
+
logging.exception("Error in plot_vertical_profile")
|
| 640 |
+
status.object = f"❌ Error in vertical profile animation: {e}"
|
| 641 |
+
|
| 642 |
+
def animate_all_altitude_profiles():
|
| 643 |
+
try:
|
| 644 |
+
animator = build_animator_3d()
|
| 645 |
+
out = os.path.join(MEDIA_DIR, "3D")
|
| 646 |
+
Plot_3DField_Data(animator, out, cmap_select_3d.value, threshold_slider_3d.value,
|
| 647 |
+
zoom_slider_3d.value).animate_all_altitude_profiles()
|
| 648 |
+
update_media_tabs()
|
| 649 |
+
status.object = "✅ All altitude profile animations created."
|
| 650 |
+
except Exception as e:
|
| 651 |
+
logging.exception("Error in animate_all_altitude_profiles")
|
| 652 |
+
status.object = f"❌ Error animating all altitude profiles: {e}"
|
| 653 |
+
|
| 654 |
+
def export_jpg_frames():
|
| 655 |
+
try:
|
| 656 |
+
animator = build_animator_3d()
|
| 657 |
+
out = os.path.join(MEDIA_DIR, "3D")
|
| 658 |
+
Plot_3DField_Data(animator, out, cmap_select_3d.value, threshold_slider_3d.value,
|
| 659 |
+
zoom_slider_3d.value).export_frames_as_jpgs(include_metadata=True)
|
| 660 |
+
update_media_tabs()
|
| 661 |
+
status.object = "✅ JPG frames exported."
|
| 662 |
+
except Exception as e:
|
| 663 |
+
logging.exception("Error exporting JPG frames")
|
| 664 |
+
status.object = f"❌ Error exporting JPG frames: {e}"
|
| 665 |
+
|
| 666 |
+
def plot_2d_field(field):
|
| 667 |
+
try:
|
| 668 |
+
animator = build_animator_2d()
|
| 669 |
+
out = os.path.join(MEDIA_DIR, "2D")
|
| 670 |
+
Plot_Horizontal_Data(animator, out, cmap_select_2d.value, fps_slider_2d.value,
|
| 671 |
+
include_metadata=True, threshold=threshold_slider_2d.value,
|
| 672 |
+
zoom_width_deg=6.0, zoom_height_deg=6.0,
|
| 673 |
+
zoom_level=zoom_slider_2d.value,
|
| 674 |
+
static_frame_export=True).plot_single_field_over_time(field, f"{field}.gif")
|
| 675 |
+
update_media_tabs()
|
| 676 |
+
status.object = f"✅ 2D field `{field}` animation created."
|
| 677 |
+
except Exception as e:
|
| 678 |
+
logging.exception(f"Error in plot_2d_field: {field}")
|
| 679 |
+
status.object = f"❌ Error in 2D field `{field}` animation: {e}"
|
| 680 |
+
|
| 681 |
+
def human_readable_size(size):
|
| 682 |
+
for unit in ['B', 'KB', 'MB', 'GB']:
|
| 683 |
+
if size < 1024: return f"{size:.1f} {unit}"
|
| 684 |
+
size /= 1024
|
| 685 |
+
return f"{size:.1f} TB"
|
| 686 |
+
|
| 687 |
+
# def generate_output_gallery(base_folder):
|
| 688 |
+
# grouped = defaultdict(lambda: defaultdict(list))
|
| 689 |
+
# for root, _, files in os.walk(os.path.join(MEDIA_DIR, base_folder)):
|
| 690 |
+
# for file in files:
|
| 691 |
+
# ext = os.path.splitext(file)[1].lower()
|
| 692 |
+
# subfolder = os.path.relpath(root, MEDIA_DIR)
|
| 693 |
+
# grouped[subfolder][ext].append(os.path.join(root, file))
|
| 694 |
+
|
| 695 |
+
# folder_panels = []
|
| 696 |
+
# for subfolder, ext_files in sorted(grouped.items()):
|
| 697 |
+
# section = []
|
| 698 |
+
# for ext, paths in sorted(ext_files.items()):
|
| 699 |
+
# previews = []
|
| 700 |
+
# for path in sorted(paths, key=os.path.getmtime, reverse=True):
|
| 701 |
+
# size = human_readable_size(os.path.getsize(path))
|
| 702 |
+
# mod = datetime.fromtimestamp(os.path.getmtime(path)).strftime("%Y-%m-%d %H:%M")
|
| 703 |
+
# title = f"**{os.path.basename(path)}**\n_{size}, {mod}_"
|
| 704 |
+
# download = pn.widgets.FileDownload(label="⬇", file=path, filename=os.path.basename(path), width=60)
|
| 705 |
+
# if ext in [".gif", ".png", ".jpg", ".jpeg"]:
|
| 706 |
+
# preview = pn.pane.Image(path, width=320)
|
| 707 |
+
# else:
|
| 708 |
+
# with open(path, "r", errors="ignore") as f:
|
| 709 |
+
# content = f.read(2048)
|
| 710 |
+
# preview = pn.pane.PreText(content, width=320)
|
| 711 |
+
# card = pn.Card(pn.pane.Markdown(title), preview, pn.Row(download), width=360)
|
| 712 |
+
# previews.append(card)
|
| 713 |
+
# section.append(pn.Column(f"### {ext.upper()}", pn.GridBox(*previews, ncols=2)))
|
| 714 |
+
# folder_section = pn.Card(f"📁 {subfolder}", *section, collapsible=True, width_policy="max")
|
| 715 |
+
# folder_panels.append(folder_section)
|
| 716 |
+
|
| 717 |
+
# return pn.Column(*folder_panels, height=600, scroll=True, sizing_mode='stretch_width', styles={'overflow': 'auto'})
|
| 718 |
+
|
| 719 |
+
|
| 720 |
+
|
| 721 |
+
def generate_output_gallery(base_folder):
|
| 722 |
+
preview_container = pn.Column(width=640, height=550)
|
| 723 |
+
preview_container.append(pn.pane.Markdown("👈 Click a thumbnail to preview"))
|
| 724 |
+
folder_cards = []
|
| 725 |
+
|
| 726 |
+
def make_preview(file_path):
|
| 727 |
+
ext = os.path.splitext(file_path)[1].lower()
|
| 728 |
+
title = pn.pane.Markdown(f"### {os.path.basename(file_path)}", width=640)
|
| 729 |
+
download_button = pn.widgets.FileDownload(file=file_path, filename=os.path.basename(file_path),
|
| 730 |
+
label="⬇ Download", button_type="success", width=150)
|
| 731 |
+
|
| 732 |
+
if ext in [".gif", ".png", ".jpg", ".jpeg"]:
|
| 733 |
+
content = pn.pane.Image(file_path, width=640, height=450, sizing_mode="fixed")
|
| 734 |
+
else:
|
| 735 |
+
try:
|
| 736 |
+
with open(file_path, 'r', errors="ignore") as f:
|
| 737 |
+
text = f.read(2048)
|
| 738 |
+
content = pn.pane.PreText(text, width=640, height=450)
|
| 739 |
+
except:
|
| 740 |
+
content = pn.pane.Markdown("*Unable to preview this file.*")
|
| 741 |
+
|
| 742 |
+
return pn.Column(title, content, download_button)
|
| 743 |
+
|
| 744 |
+
grouped = defaultdict(list)
|
| 745 |
+
for root, _, files in os.walk(os.path.join(MEDIA_DIR, base_folder)):
|
| 746 |
+
for file in sorted(files):
|
| 747 |
+
full_path = os.path.join(root, file)
|
| 748 |
+
if not os.path.exists(full_path):
|
| 749 |
+
continue
|
| 750 |
+
rel_folder = os.path.relpath(root, os.path.join(MEDIA_DIR, base_folder))
|
| 751 |
+
grouped[rel_folder].append(full_path)
|
| 752 |
+
|
| 753 |
+
for folder, file_paths in sorted(grouped.items()):
|
| 754 |
+
thumbnails = []
|
| 755 |
+
for full_path in file_paths:
|
| 756 |
+
filename = os.path.basename(full_path)
|
| 757 |
+
ext = os.path.splitext(full_path)[1].lower()
|
| 758 |
+
|
| 759 |
+
if ext in [".gif", ".png", ".jpg", ".jpeg"]:
|
| 760 |
+
img = pn.pane.Image(full_path, width=140, height=100)
|
| 761 |
+
else:
|
| 762 |
+
img = pn.pane.Markdown("📄", width=140, height=100)
|
| 763 |
+
|
| 764 |
+
view_button = pn.widgets.Button(name="👁", width=40, height=30, button_type="primary")
|
| 765 |
+
|
| 766 |
+
def click_handler(path=full_path):
|
| 767 |
+
def inner_click(event):
|
| 768 |
+
preview_container[:] = [make_preview(path)]
|
| 769 |
+
return inner_click
|
| 770 |
+
|
| 771 |
+
view_button.on_click(click_handler())
|
| 772 |
+
|
| 773 |
+
overlay = pn.Column(pn.Row(pn.Spacer(width=90), view_button), img, width=160)
|
| 774 |
+
label_md = pn.pane.Markdown(f"**{filename}**", width=140, height=35)
|
| 775 |
+
thumb_card = pn.Column(overlay, label_md, width=160)
|
| 776 |
+
thumbnails.append(thumb_card)
|
| 777 |
+
|
| 778 |
+
folder_card = pn.Card(pn.GridBox(*thumbnails, ncols=2), title=f"📁 {folder}", width=400, collapsible=True)
|
| 779 |
+
folder_cards.append(folder_card)
|
| 780 |
+
|
| 781 |
+
folder_scroll = pn.Column(*folder_cards, scroll=True, height=600, width=420)
|
| 782 |
+
return pn.Row(preview_container, pn.Spacer(width=20), folder_scroll)
|
| 783 |
+
|
| 784 |
+
def update_media_tabs():
|
| 785 |
+
media_tab_2d.objects[:] = [generate_output_gallery("2D")]
|
| 786 |
+
media_tab_3d.objects[:] = [generate_output_gallery("3D")]
|
| 787 |
+
|
| 788 |
+
media_tab_2d = pn.Column(generate_output_gallery("2D"))
|
| 789 |
+
media_tab_3d = pn.Column(generate_output_gallery("3D"))
|
| 790 |
+
|
| 791 |
+
media_tab = pn.Tabs(
|
| 792 |
+
("🖼 2D Output Gallery", media_tab_2d),
|
| 793 |
+
("🖼 3D Output Gallery", media_tab_3d),
|
| 794 |
+
dynamic=True
|
| 795 |
)
|
| 796 |
+
|
| 797 |
+
tab3d = pn.Column(
|
| 798 |
+
threshold_slider_3d, zoom_slider_3d, fps_slider_3d, Altitude_slider, cmap_select_3d,
|
| 799 |
+
pn.widgets.Button(name="🎞 Generate animation at selected altitude level", button_type="primary", on_click=lambda e: tab3d.append(plot_z_level())),
|
| 800 |
+
pn.widgets.Button(name="📈 Generate vertical profile animation at time index", button_type="primary", on_click=lambda e: tab3d.append(plot_vertical_profile())),
|
| 801 |
+
pn.widgets.Button(name="📊 Generate all altitude level animations", button_type="primary", on_click=lambda e: tab3d.append(animate_all_altitude_profiles())),
|
| 802 |
+
pn.widgets.Button(name="🖼 Export all animation frames as JPG", button_type="primary", on_click=lambda e: tab3d.append(export_jpg_frames())),
|
| 803 |
)
|
| 804 |
|
| 805 |
+
tab2d = pn.Column(
|
| 806 |
+
threshold_slider_2d, zoom_slider_2d, fps_slider_2d, cmap_select_2d,
|
| 807 |
+
pn.widgets.Button(name="🌫 Animate Air Concentration", button_type="primary", on_click=lambda e: tab2d.append(plot_2d_field("air_concentration"))),
|
| 808 |
+
pn.widgets.Button(name="🌧 Animate Dry Deposition Rate", button_type="primary", on_click=lambda e: tab2d.append(plot_2d_field("dry_deposition_rate"))),
|
| 809 |
+
pn.widgets.Button(name="💧 Animate Wet Deposition Rate", button_type="primary", on_click=lambda e: tab2d.append(plot_2d_field("wet_deposition_rate"))),
|
| 810 |
)
|
| 811 |
|
| 812 |
+
help_tab = pn.Column(pn.pane.Markdown("""
|
| 813 |
+
## ❓ How to Use the NAME Ash Visualizer
|
| 814 |
+
|
| 815 |
+
This dashboard allows users to upload and visualize outputs from the NAME ash dispersion model.
|
| 816 |
+
|
| 817 |
+
### 🧭 Workflow
|
| 818 |
+
1. **Upload ZIP** containing NetCDF files from the NAME model.
|
| 819 |
+
2. Use **3D and 2D tabs** to configure and generate animations.
|
| 820 |
+
3. Use **Media Viewer** to preview and download results.
|
| 821 |
+
|
| 822 |
+
### 🧳 ZIP Structure
|
| 823 |
+
```
|
| 824 |
+
## 🗂 How Uploaded ZIP is Processed
|
| 825 |
+
|
| 826 |
+
```text
|
| 827 |
+
┌────────────────────────────────────────────┐
|
| 828 |
+
│ Uploaded ZIP (.zip) │
|
| 829 |
+
│ (e.g. Taal_273070_20200112_scenario_*.zip)│
|
| 830 |
+
└────────────────────────────────────────────┘
|
| 831 |
+
│
|
| 832 |
+
▼
|
| 833 |
+
┌───────────────────────────────┐
|
| 834 |
+
│ Contains: raw .txt outputs │
|
| 835 |
+
│ - AQOutput_3DField_*.txt │
|
| 836 |
+
│ - AQOutput_horizontal_*.txt │
|
| 837 |
+
└───────────────────────────────┘
|
| 838 |
+
│
|
| 839 |
+
▼
|
| 840 |
+
┌────────────────────────────────────────┐
|
| 841 |
+
│ NAMEDataProcessor.batch_process_zip()│
|
| 842 |
+
└────────────────────────────────────────┘
|
| 843 |
+
│
|
| 844 |
+
▼
|
| 845 |
+
┌─────────────────────────────┐
|
| 846 |
+
│ Converts to NetCDF files │
|
| 847 |
+
│ - ash_output/3D/*.nc │
|
| 848 |
+
│ - ash_output/horizontal/*.nc │
|
| 849 |
+
└─────────────────────────────┘
|
| 850 |
+
│
|
| 851 |
+
▼
|
| 852 |
+
┌─────────────────────────────────────┐
|
| 853 |
+
│ View & animate in 3D/2D tabs │
|
| 854 |
+
│ Download results in Media Viewer │
|
| 855 |
+
└─────────────────────────────────────┘
|
| 856 |
+
|
| 857 |
+
```
|
| 858 |
+
|
| 859 |
+
### 📢 Tips
|
| 860 |
+
- Reset the app with 🔄 if needed.
|
| 861 |
+
- View logs if an error occurs.
|
| 862 |
+
- Outputs are temporary per session.
|
| 863 |
+
""", sizing_mode="stretch_width", width=800))
|
| 864 |
+
|
| 865 |
+
tabs = pn.Tabs(
|
| 866 |
+
("🧱 3D Field", tab3d),
|
| 867 |
+
("🌍 2D Field", tab2d),
|
| 868 |
+
("📁 Media Viewer", media_tab),
|
| 869 |
+
("❓ Help", help_tab)
|
| 870 |
)
|
| 871 |
|
| 872 |
+
sidebar = pn.Column(
|
| 873 |
+
pn.pane.Markdown("## 🌋 NAME Ash Visualizer", sizing_mode="stretch_width"),
|
| 874 |
+
pn.Card(pn.Column(file_input, process_button, reset_button, sizing_mode="stretch_width"),
|
| 875 |
+
title="📂 File Upload & Processing", collapsible=True, sizing_mode="stretch_width"),
|
| 876 |
+
pn.Card(pn.Column(download_button, log_link, sizing_mode="stretch_width"),
|
| 877 |
+
title="📁 Downloads & Logs", collapsible=True, sizing_mode="stretch_width"),
|
| 878 |
+
pn.Card(status, title="📢 Status", collapsible=True, sizing_mode="stretch_width"),
|
| 879 |
+
sizing_mode="stretch_width", width=300
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 880 |
)
|
| 881 |
|
| 882 |
+
restore_previous_session()
|
| 883 |
+
|
| 884 |
+
pn.template.FastListTemplate(
|
| 885 |
+
title="NAME Visualizer Dashboard",
|
| 886 |
+
sidebar=sidebar,
|
| 887 |
+
main=[tabs],
|
| 888 |
+
).servable()
|
| 889 |
+
'''
|
ash_animator/__init__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Auto-generated __init__.py to import all modules
|
| 3 |
+
from .basemaps import *
|
| 4 |
+
from .converter import *
|
| 5 |
+
from .interpolation import *
|
| 6 |
+
from .plot_3dfield_data import *
|
| 7 |
+
from .plot_horizontal_data import *
|
| 8 |
+
from .utils import *
|
| 9 |
+
from .animation_all import *
|
| 10 |
+
from .animation_single import *
|
| 11 |
+
from .animation_vertical import *
|
| 12 |
+
from .export import *
|
ash_animator/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (522 Bytes). View file
|
|
|
ash_animator/__pycache__/animation_all.cpython-312.pyc
ADDED
|
Binary file (12.2 kB). View file
|
|
|
ash_animator/__pycache__/animation_single.cpython-312.pyc
ADDED
|
Binary file (11.4 kB). View file
|
|
|
ash_animator/__pycache__/animation_vertical.cpython-312.pyc
ADDED
|
Binary file (14.3 kB). View file
|
|
|
ash_animator/__pycache__/basemaps.cpython-312.pyc
ADDED
|
Binary file (5.97 kB). View file
|
|
|
ash_animator/__pycache__/converter.cpython-312.pyc
ADDED
|
Binary file (14.9 kB). View file
|
|
|
ash_animator/__pycache__/export.cpython-312.pyc
ADDED
|
Binary file (9.23 kB). View file
|
|
|
ash_animator/__pycache__/interpolation.cpython-312.pyc
ADDED
|
Binary file (1.02 kB). View file
|
|
|
ash_animator/__pycache__/plot_3dfield_data.cpython-312.pyc
ADDED
|
Binary file (33.7 kB). View file
|
|
|
ash_animator/__pycache__/plot_horizontal_data.cpython-312.pyc
ADDED
|
Binary file (16.8 kB). View file
|
|
|
ash_animator/__pycache__/utils.cpython-312.pyc
ADDED
|
Binary file (1.6 kB). View file
|
|
|
ash_animator/animation_all.py
ADDED
|
@@ -0,0 +1,516 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# import os
|
| 3 |
+
# import numpy as np
|
| 4 |
+
# import matplotlib.pyplot as plt
|
| 5 |
+
# import matplotlib.animation as animation
|
| 6 |
+
# import matplotlib.ticker as mticker
|
| 7 |
+
# import cartopy.crs as ccrs
|
| 8 |
+
# import cartopy.feature as cfeature
|
| 9 |
+
# from adjustText import adjust_text
|
| 10 |
+
# import cartopy.io.shapereader as shpreader
|
| 11 |
+
# from .interpolation import interpolate_grid
|
| 12 |
+
# from .basemaps import draw_etopo_basemap
|
| 13 |
+
|
| 14 |
+
# def animate_all_z_levels(animator, output_folder: str, fps: int = 2, threshold: float = 0.1):
|
| 15 |
+
# os.makedirs(output_folder, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
# countries_shp = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries')
|
| 18 |
+
# reader = shpreader.Reader(countries_shp)
|
| 19 |
+
# country_geoms = list(reader.records())
|
| 20 |
+
|
| 21 |
+
# for z_index, z_val in enumerate(animator.levels):
|
| 22 |
+
# fig = plt.figure(figsize=(16, 7))
|
| 23 |
+
# proj = ccrs.PlateCarree()
|
| 24 |
+
# ax1 = fig.add_subplot(1, 2, 1, projection=proj)
|
| 25 |
+
# ax2 = fig.add_subplot(1, 2, 2, projection=proj)
|
| 26 |
+
|
| 27 |
+
# valid_mask = np.stack([
|
| 28 |
+
# ds['ash_concentration'].values[z_index] for ds in animator.datasets
|
| 29 |
+
# ]).max(axis=0) > 0
|
| 30 |
+
# y_idx, x_idx = np.where(valid_mask)
|
| 31 |
+
|
| 32 |
+
# if y_idx.size == 0 or x_idx.size == 0:
|
| 33 |
+
# print(f"Z level {z_val} km has no valid data. Skipping...")
|
| 34 |
+
# plt.close()
|
| 35 |
+
# continue
|
| 36 |
+
|
| 37 |
+
# y_min, y_max = y_idx.min(), y_idx.max()
|
| 38 |
+
# x_min, x_max = x_idx.min(), x_idx.max()
|
| 39 |
+
|
| 40 |
+
# buffer_y = int((y_max - y_min) * 0.5)
|
| 41 |
+
# buffer_x = int((x_max - x_min) * 0.5)
|
| 42 |
+
|
| 43 |
+
# y_start = max(0, y_min - buffer_y)
|
| 44 |
+
# y_end = min(animator.lat_grid.shape[0], y_max + buffer_y + 1)
|
| 45 |
+
# x_start = max(0, x_min - buffer_x)
|
| 46 |
+
# x_end = min(animator.lon_grid.shape[1], x_max + buffer_x + 1)
|
| 47 |
+
|
| 48 |
+
# lat_zoom = animator.lats[y_start:y_end]
|
| 49 |
+
# lon_zoom = animator.lons[x_start:x_end]
|
| 50 |
+
# lon_zoom_grid, lat_zoom_grid = np.meshgrid(lon_zoom, lat_zoom)
|
| 51 |
+
|
| 52 |
+
# valid_frames = []
|
| 53 |
+
# for t in range(len(animator.datasets)):
|
| 54 |
+
# data = animator.datasets[t]['ash_concentration'].values[z_index]
|
| 55 |
+
# interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 56 |
+
# interp = np.where(interp < 0, np.nan, interp)
|
| 57 |
+
# if np.isfinite(interp).sum() > 0:
|
| 58 |
+
# valid_frames.append(t)
|
| 59 |
+
|
| 60 |
+
# if not valid_frames:
|
| 61 |
+
# print(f"No valid frames for Z={z_val} km. Skipping animation.")
|
| 62 |
+
# plt.close()
|
| 63 |
+
# continue
|
| 64 |
+
|
| 65 |
+
# def update(t):
|
| 66 |
+
# ax1.clear()
|
| 67 |
+
# ax2.clear()
|
| 68 |
+
|
| 69 |
+
# data = animator.datasets[t]['ash_concentration'].values[z_index]
|
| 70 |
+
# interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 71 |
+
# interp = np.where(interp < 0, np.nan, interp)
|
| 72 |
+
# zoom_plot = interp[y_start:y_end, x_start:x_end]
|
| 73 |
+
|
| 74 |
+
# valid_vals = interp[np.isfinite(interp)]
|
| 75 |
+
# if valid_vals.size == 0:
|
| 76 |
+
# return []
|
| 77 |
+
|
| 78 |
+
# min_val = np.nanmin(valid_vals)
|
| 79 |
+
# max_val = np.nanmax(valid_vals)
|
| 80 |
+
# log_cutoff = 1e-3
|
| 81 |
+
# log_ratio = max_val / (min_val + 1e-6)
|
| 82 |
+
# use_log = min_val > log_cutoff and log_ratio > 100
|
| 83 |
+
|
| 84 |
+
# if use_log:
|
| 85 |
+
# data_for_plot = np.where(interp > log_cutoff, interp, np.nan)
|
| 86 |
+
# levels = np.logspace(np.log10(log_cutoff), np.log10(max_val), 20)
|
| 87 |
+
# scale_label = "Hybrid Log"
|
| 88 |
+
# else:
|
| 89 |
+
# data_for_plot = interp
|
| 90 |
+
# levels = np.linspace(0, max_val, 20)
|
| 91 |
+
# scale_label = "Linear"
|
| 92 |
+
|
| 93 |
+
# draw_etopo_basemap(ax1, mode='stock')
|
| 94 |
+
# draw_etopo_basemap(ax2, mode='stock')
|
| 95 |
+
|
| 96 |
+
# c1 = ax1.contourf(animator.lons, animator.lats, data_for_plot, levels=levels,
|
| 97 |
+
# cmap="rainbow", alpha=0.6, transform=proj)
|
| 98 |
+
# ax1.contour(animator.lons, animator.lats, data_for_plot, levels=levels,
|
| 99 |
+
# colors='black', linewidths=0.5, transform=proj)
|
| 100 |
+
# ax1.set_title(f"T{t+1} | Alt: {z_val} km (Full - {scale_label})")
|
| 101 |
+
# ax1.set_extent([animator.lons.min(), animator.lons.max(), animator.lats.min(), animator.lats.max()])
|
| 102 |
+
# ax1.coastlines()
|
| 103 |
+
# ax1.add_feature(cfeature.BORDERS, linestyle=':')
|
| 104 |
+
# ax1.add_feature(cfeature.LAND)
|
| 105 |
+
# ax1.add_feature(cfeature.OCEAN)
|
| 106 |
+
|
| 107 |
+
# c2 = ax2.contourf(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=levels,
|
| 108 |
+
# cmap="rainbow", alpha=0.4, transform=proj)
|
| 109 |
+
# ax2.contour(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=levels,
|
| 110 |
+
# colors='black', linewidths=0.5, transform=proj)
|
| 111 |
+
# ax2.set_title(f"T{t+1} | Alt: {z_val} km (Zoom - {scale_label})")
|
| 112 |
+
# ax2.set_extent([lon_zoom.min(), lon_zoom.max(), lat_zoom.min(), lat_zoom.max()])
|
| 113 |
+
# ax2.coastlines()
|
| 114 |
+
# ax2.add_feature(cfeature.BORDERS, linestyle=':')
|
| 115 |
+
# ax2.add_feature(cfeature.LAND)
|
| 116 |
+
# ax2.add_feature(cfeature.OCEAN)
|
| 117 |
+
|
| 118 |
+
# ax2.text(animator.lons[0], animator.lats[0], animator.country_label, fontsize=9, color='white',
|
| 119 |
+
# transform=proj, bbox=dict(facecolor='black', alpha=0.5))
|
| 120 |
+
|
| 121 |
+
# texts_ax1, texts_ax2 = [], []
|
| 122 |
+
# for country in country_geoms:
|
| 123 |
+
# name = country.attributes['NAME_LONG']
|
| 124 |
+
# geom = country.geometry
|
| 125 |
+
# try:
|
| 126 |
+
# lon, lat = geom.centroid.x, geom.centroid.y
|
| 127 |
+
# if (lon_zoom.min() <= lon <= lon_zoom.max()) and (lat_zoom.min() <= lat <= lat_zoom.max()):
|
| 128 |
+
# text = ax2.text(lon, lat, name, fontsize=6, transform=proj,
|
| 129 |
+
# ha='center', va='center', color='white',
|
| 130 |
+
# bbox=dict(facecolor='black', alpha=0.5, linewidth=0))
|
| 131 |
+
# texts_ax2.append(text)
|
| 132 |
+
|
| 133 |
+
# if (animator.lons.min() <= lon <= animator.lons.max()) and (animator.lats.min() <= lat <= animator.lats.max()):
|
| 134 |
+
# text = ax1.text(lon, lat, name, fontsize=6, transform=proj,
|
| 135 |
+
# ha='center', va='center', color='white',
|
| 136 |
+
# bbox=dict(facecolor='black', alpha=0.5, linewidth=0))
|
| 137 |
+
# texts_ax1.append(text)
|
| 138 |
+
# except:
|
| 139 |
+
# continue
|
| 140 |
+
|
| 141 |
+
# adjust_text(texts_ax1, ax=ax1, only_move={'points': 'y', 'text': 'y'},
|
| 142 |
+
# arrowprops=dict(arrowstyle="->", color='white', lw=0.5))
|
| 143 |
+
# adjust_text(texts_ax2, ax=ax2, only_move={'points': 'y', 'text': 'y'},
|
| 144 |
+
# arrowprops=dict(arrowstyle="->", color='white', lw=0.5))
|
| 145 |
+
|
| 146 |
+
# if np.nanmax(valid_vals) > threshold:
|
| 147 |
+
# alert_text = f"⚠ Exceeds {threshold} g/m³!"
|
| 148 |
+
# for ax in [ax1, ax2]:
|
| 149 |
+
# ax.text(0.99, 0.01, alert_text, transform=ax.transAxes,
|
| 150 |
+
# ha='right', va='bottom', fontsize=10, color='red',
|
| 151 |
+
# bbox=dict(facecolor='white', alpha=0.8, edgecolor='red'))
|
| 152 |
+
# ax1.contour(animator.lons, animator.lats, interp, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 153 |
+
# ax2.contour(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 154 |
+
|
| 155 |
+
# if not hasattr(update, "colorbar"):
|
| 156 |
+
# update.colorbar = fig.colorbar(c1, ax=[ax1, ax2], orientation='vertical',
|
| 157 |
+
# label="Ash concentration (g/m³)")
|
| 158 |
+
# formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 159 |
+
# update.colorbar.ax.yaxis.set_major_formatter(formatter)
|
| 160 |
+
# if use_log:
|
| 161 |
+
# update.colorbar.ax.text(1.05, 1.02, "log scale", transform=update.colorbar.ax.transAxes,
|
| 162 |
+
# fontsize=9, color='gray', rotation=90, ha='left', va='bottom')
|
| 163 |
+
|
| 164 |
+
# return []
|
| 165 |
+
|
| 166 |
+
# ani = animation.FuncAnimation(fig, update, frames=valid_frames, blit=False)
|
| 167 |
+
# gif_path = os.path.join(output_folder, f"ash_T1-Tn_Z{z_index+1}.gif")
|
| 168 |
+
# ani.save(gif_path, writer='pillow', fps=fps)
|
| 169 |
+
# plt.close()
|
| 170 |
+
# print(f"✅ Saved animation for Z={z_val} km to {gif_path}")
|
| 171 |
+
###################################################################################################################
|
| 172 |
+
# import os
|
| 173 |
+
# import numpy as np
|
| 174 |
+
# import matplotlib.pyplot as plt
|
| 175 |
+
# import matplotlib.animation as animation
|
| 176 |
+
# import matplotlib.ticker as mticker
|
| 177 |
+
# import cartopy.crs as ccrs
|
| 178 |
+
# import cartopy.feature as cfeature
|
| 179 |
+
# from adjustText import adjust_text
|
| 180 |
+
# import cartopy.io.shapereader as shpreader
|
| 181 |
+
# from .interpolation import interpolate_grid
|
| 182 |
+
# from .basemaps import draw_etopo_basemap
|
| 183 |
+
|
| 184 |
+
# def animate_all_z_levels(animator, output_folder: str, fps: int = 2, threshold: float = 0.1):
|
| 185 |
+
# os.makedirs(output_folder, exist_ok=True)
|
| 186 |
+
|
| 187 |
+
# countries_shp = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries')
|
| 188 |
+
# reader = shpreader.Reader(countries_shp)
|
| 189 |
+
# country_geoms = list(reader.records())
|
| 190 |
+
|
| 191 |
+
# # Compute consistent zoom window across all z-levels and time frames
|
| 192 |
+
# valid_mask_all = np.zeros_like(animator.datasets[0]['ash_concentration'].values[0], dtype=bool)
|
| 193 |
+
# for ds in animator.datasets:
|
| 194 |
+
# for z in range(len(animator.levels)):
|
| 195 |
+
# valid_mask_all |= ds['ash_concentration'].values[z] > 0
|
| 196 |
+
|
| 197 |
+
# y_idx_all, x_idx_all = np.where(valid_mask_all)
|
| 198 |
+
# if y_idx_all.size == 0 or x_idx_all.size == 0:
|
| 199 |
+
# raise ValueError("No valid data found across any Z level or frame.")
|
| 200 |
+
|
| 201 |
+
# y_min, y_max = y_idx_all.min(), y_idx_all.max()
|
| 202 |
+
# x_min, x_max = x_idx_all.min(), x_idx_all.max()
|
| 203 |
+
# buffer_y = int((y_max - y_min) * 0.5)
|
| 204 |
+
# buffer_x = int((x_max - x_min) * 0.5)
|
| 205 |
+
|
| 206 |
+
# y_start = max(0, y_min - buffer_y)
|
| 207 |
+
# y_end = min(animator.lat_grid.shape[0], y_max + buffer_y + 1)
|
| 208 |
+
# x_start = max(0, x_min - buffer_x)
|
| 209 |
+
# x_end = min(animator.lon_grid.shape[1], x_max + buffer_x + 1)
|
| 210 |
+
|
| 211 |
+
# lat_zoom = animator.lats[y_start:y_end]
|
| 212 |
+
# lon_zoom = animator.lons[x_start:x_end]
|
| 213 |
+
# lon_zoom_grid, lat_zoom_grid = np.meshgrid(lon_zoom, lat_zoom)
|
| 214 |
+
|
| 215 |
+
# for z_index, z_val in enumerate(animator.levels):
|
| 216 |
+
# fig = plt.figure(figsize=(16, 7))
|
| 217 |
+
# proj = ccrs.PlateCarree()
|
| 218 |
+
# ax1 = fig.add_subplot(1, 2, 1, projection=proj)
|
| 219 |
+
# ax2 = fig.add_subplot(1, 2, 2, projection=proj)
|
| 220 |
+
|
| 221 |
+
# valid_frames = []
|
| 222 |
+
# for t in range(len(animator.datasets)):
|
| 223 |
+
# data = animator.datasets[t]['ash_concentration'].values[z_index]
|
| 224 |
+
# interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 225 |
+
# interp = np.where(interp < 0, np.nan, interp)
|
| 226 |
+
# if np.isfinite(interp).sum() > 0:
|
| 227 |
+
# valid_frames.append(t)
|
| 228 |
+
|
| 229 |
+
# if not valid_frames:
|
| 230 |
+
# print(f"No valid frames for Z={z_val} km. Skipping animation.")
|
| 231 |
+
# plt.close()
|
| 232 |
+
# continue
|
| 233 |
+
|
| 234 |
+
# def update(t):
|
| 235 |
+
# ax1.clear()
|
| 236 |
+
# ax2.clear()
|
| 237 |
+
|
| 238 |
+
# data = animator.datasets[t]['ash_concentration'].values[z_index]
|
| 239 |
+
# interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 240 |
+
# interp = np.where(interp < 0, np.nan, interp)
|
| 241 |
+
# zoom_plot = interp[y_start:y_end, x_start:x_end]
|
| 242 |
+
|
| 243 |
+
# valid_vals = interp[np.isfinite(interp)]
|
| 244 |
+
# if valid_vals.size == 0:
|
| 245 |
+
# return []
|
| 246 |
+
|
| 247 |
+
# min_val = np.nanmin(valid_vals)
|
| 248 |
+
# max_val = np.nanmax(valid_vals)
|
| 249 |
+
# log_cutoff = 1e-3
|
| 250 |
+
# log_ratio = max_val / (min_val + 1e-6)
|
| 251 |
+
# use_log = min_val > log_cutoff and log_ratio > 100
|
| 252 |
+
|
| 253 |
+
# if use_log:
|
| 254 |
+
# data_for_plot = np.where(interp > log_cutoff, interp, np.nan)
|
| 255 |
+
# levels = np.logspace(np.log10(log_cutoff), np.log10(max_val), 20)
|
| 256 |
+
# scale_label = "Hybrid Log"
|
| 257 |
+
# else:
|
| 258 |
+
# data_for_plot = interp
|
| 259 |
+
# levels = np.linspace(0, max_val, 20)
|
| 260 |
+
# scale_label = "Linear"
|
| 261 |
+
|
| 262 |
+
# draw_etopo_basemap(ax1, mode='stock')
|
| 263 |
+
# draw_etopo_basemap(ax2, mode='stock')
|
| 264 |
+
|
| 265 |
+
# c1 = ax1.contourf(animator.lons, animator.lats, data_for_plot, levels=levels,
|
| 266 |
+
# cmap="rainbow", alpha=0.6, transform=proj)
|
| 267 |
+
# ax1.contour(animator.lons, animator.lats, data_for_plot, levels=levels,
|
| 268 |
+
# colors='black', linewidths=0.5, transform=proj)
|
| 269 |
+
# ax1.set_title(f"T{t+1} | Alt: {z_val} km (Full - {scale_label})")
|
| 270 |
+
# ax1.set_extent([animator.lons.min(), animator.lons.max(), animator.lats.min(), animator.lats.max()])
|
| 271 |
+
# ax1.coastlines()
|
| 272 |
+
# ax1.add_feature(cfeature.BORDERS, linestyle=':')
|
| 273 |
+
# ax1.add_feature(cfeature.LAND)
|
| 274 |
+
# ax1.add_feature(cfeature.OCEAN)
|
| 275 |
+
|
| 276 |
+
# c2 = ax2.contourf(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=levels,
|
| 277 |
+
# cmap="rainbow", alpha=0.4, transform=proj)
|
| 278 |
+
# ax2.contour(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=levels,
|
| 279 |
+
# colors='black', linewidths=0.5, transform=proj)
|
| 280 |
+
# ax2.set_title(f"T{t+1} | Alt: {z_val} km (Zoom - {scale_label})")
|
| 281 |
+
# ax2.set_extent([lon_zoom.min(), lon_zoom.max(), lat_zoom.min(), lat_zoom.max()])
|
| 282 |
+
# ax2.coastlines()
|
| 283 |
+
# ax2.add_feature(cfeature.BORDERS, linestyle=':')
|
| 284 |
+
# ax2.add_feature(cfeature.LAND)
|
| 285 |
+
# ax2.add_feature(cfeature.OCEAN)
|
| 286 |
+
|
| 287 |
+
# ax2.text(animator.lons[0], animator.lats[0], animator.country_label, fontsize=9, color='white',
|
| 288 |
+
# transform=proj, bbox=dict(facecolor='black', alpha=0.5))
|
| 289 |
+
|
| 290 |
+
# texts_ax1, texts_ax2 = [], []
|
| 291 |
+
# for country in country_geoms:
|
| 292 |
+
# name = country.attributes['NAME_LONG']
|
| 293 |
+
# geom = country.geometry
|
| 294 |
+
# try:
|
| 295 |
+
# lon, lat = geom.centroid.x, geom.centroid.y
|
| 296 |
+
# if (lon_zoom.min() <= lon <= lon_zoom.max()) and (lat_zoom.min() <= lat <= lat_zoom.max()):
|
| 297 |
+
# text = ax2.text(lon, lat, name, fontsize=6, transform=proj,
|
| 298 |
+
# ha='center', va='center', color='white',
|
| 299 |
+
# bbox=dict(facecolor='black', alpha=0.5, linewidth=0))
|
| 300 |
+
# texts_ax2.append(text)
|
| 301 |
+
|
| 302 |
+
# if (animator.lons.min() <= lon <= animator.lons.max()) and (animator.lats.min() <= lat <= animator.lats.max()):
|
| 303 |
+
# text = ax1.text(lon, lat, name, fontsize=6, transform=proj,
|
| 304 |
+
# ha='center', va='center', color='white',
|
| 305 |
+
# bbox=dict(facecolor='black', alpha=0.5, linewidth=0))
|
| 306 |
+
# texts_ax1.append(text)
|
| 307 |
+
# except:
|
| 308 |
+
# continue
|
| 309 |
+
|
| 310 |
+
# adjust_text(texts_ax1, ax=ax1, only_move={'points': 'y', 'text': 'y'},
|
| 311 |
+
# arrowprops=dict(arrowstyle="->", color='white', lw=0.5))
|
| 312 |
+
# adjust_text(texts_ax2, ax=ax2, only_move={'points': 'y', 'text': 'y'},
|
| 313 |
+
# arrowprops=dict(arrowstyle="->", color='white', lw=0.5))
|
| 314 |
+
|
| 315 |
+
# if np.nanmax(valid_vals) > threshold:
|
| 316 |
+
# alert_text = f"⚠ Exceeds {threshold} g/m³!"
|
| 317 |
+
# for ax in [ax1, ax2]:
|
| 318 |
+
# ax.text(0.99, 0.01, alert_text, transform=ax.transAxes,
|
| 319 |
+
# ha='right', va='bottom', fontsize=10, color='red',
|
| 320 |
+
# bbox=dict(facecolor='white', alpha=0.8, edgecolor='red'))
|
| 321 |
+
# ax1.contour(animator.lons, animator.lats, interp, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 322 |
+
# ax2.contour(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 323 |
+
|
| 324 |
+
# if not hasattr(update, "colorbar"):
|
| 325 |
+
# update.colorbar = fig.colorbar(c1, ax=[ax1, ax2], orientation='vertical',
|
| 326 |
+
# label="Ash concentration (g/m³)")
|
| 327 |
+
# formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 328 |
+
# update.colorbar.ax.yaxis.set_major_formatter(formatter)
|
| 329 |
+
# if use_log:
|
| 330 |
+
# update.colorbar.ax.text(1.05, 1.02, "log scale", transform=update.colorbar.ax.transAxes,
|
| 331 |
+
# fontsize=9, color='gray', rotation=90, ha='left', va='bottom')
|
| 332 |
+
|
| 333 |
+
# return []
|
| 334 |
+
|
| 335 |
+
# ani = animation.FuncAnimation(fig, update, frames=valid_frames, blit=False)
|
| 336 |
+
# gif_path = os.path.join(output_folder, f"ash_T1-Tn_Z{z_index+1}.gif")
|
| 337 |
+
# ani.save(gif_path, writer='pillow', fps=fps)
|
| 338 |
+
# plt.close()
|
| 339 |
+
# print(f"✅ Saved animation for Z={z_val} km to {gif_path}")
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
import os
|
| 343 |
+
import numpy as np
|
| 344 |
+
import matplotlib.pyplot as plt
|
| 345 |
+
import matplotlib.animation as animation
|
| 346 |
+
import matplotlib.ticker as mticker
|
| 347 |
+
import cartopy.crs as ccrs
|
| 348 |
+
import cartopy.feature as cfeature
|
| 349 |
+
from adjustText import adjust_text
|
| 350 |
+
import cartopy.io.shapereader as shpreader
|
| 351 |
+
from .interpolation import interpolate_grid
|
| 352 |
+
from .basemaps import draw_etopo_basemap
|
| 353 |
+
|
| 354 |
+
def animate_all_z_levels(animator, output_folder: str, fps: int = 2, threshold: float = 0.1,
|
| 355 |
+
zoom_width_deg: float = 6.0, zoom_height_deg: float = 6.0):
|
| 356 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 357 |
+
|
| 358 |
+
countries_shp = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries')
|
| 359 |
+
reader = shpreader.Reader(countries_shp)
|
| 360 |
+
country_geoms = list(reader.records())
|
| 361 |
+
|
| 362 |
+
# Find the most active region (max concentration point)
|
| 363 |
+
max_conc = -np.inf
|
| 364 |
+
center_lat = center_lon = None
|
| 365 |
+
for ds in animator.datasets:
|
| 366 |
+
for z in range(len(animator.levels)):
|
| 367 |
+
data = ds['ash_concentration'].values[z]
|
| 368 |
+
if np.max(data) > max_conc:
|
| 369 |
+
max_conc = np.max(data)
|
| 370 |
+
max_idx = np.unravel_index(np.argmax(data), data.shape)
|
| 371 |
+
center_lat = animator.lat_grid[max_idx]
|
| 372 |
+
center_lon = animator.lon_grid[max_idx]
|
| 373 |
+
|
| 374 |
+
if center_lat is None or center_lon is None:
|
| 375 |
+
raise ValueError("No valid concentration found to determine zoom center.")
|
| 376 |
+
|
| 377 |
+
# Compute fixed zoom extents in lat/lon degrees
|
| 378 |
+
lon_zoom_min = center_lon - zoom_width_deg / 2
|
| 379 |
+
lon_zoom_max = center_lon + zoom_width_deg / 2
|
| 380 |
+
lat_zoom_min = center_lat - zoom_height_deg / 2
|
| 381 |
+
lat_zoom_max = center_lat + zoom_height_deg / 2
|
| 382 |
+
|
| 383 |
+
# Create zoom grids for plotting
|
| 384 |
+
lat_zoom = animator.lats[(animator.lats >= lat_zoom_min) & (animator.lats <= lat_zoom_max)]
|
| 385 |
+
lon_zoom = animator.lons[(animator.lons >= lon_zoom_min) & (animator.lons <= lon_zoom_max)]
|
| 386 |
+
lon_zoom_grid, lat_zoom_grid = np.meshgrid(lon_zoom, lat_zoom)
|
| 387 |
+
|
| 388 |
+
for z_index, z_val in enumerate(animator.levels):
|
| 389 |
+
fig = plt.figure(figsize=(16, 7))
|
| 390 |
+
proj = ccrs.PlateCarree()
|
| 391 |
+
ax1 = fig.add_subplot(1, 2, 1, projection=proj)
|
| 392 |
+
ax2 = fig.add_subplot(1, 2, 2, projection=proj)
|
| 393 |
+
|
| 394 |
+
valid_frames = []
|
| 395 |
+
for t in range(len(animator.datasets)):
|
| 396 |
+
data = animator.datasets[t]['ash_concentration'].values[z_index]
|
| 397 |
+
interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 398 |
+
interp = np.where(interp < 0, np.nan, interp)
|
| 399 |
+
if np.isfinite(interp).sum() > 0:
|
| 400 |
+
valid_frames.append(t)
|
| 401 |
+
|
| 402 |
+
if not valid_frames:
|
| 403 |
+
print(f"No valid frames for Z={z_val} km. Skipping animation.")
|
| 404 |
+
plt.close()
|
| 405 |
+
continue
|
| 406 |
+
|
| 407 |
+
def update(t):
|
| 408 |
+
ax1.clear()
|
| 409 |
+
ax2.clear()
|
| 410 |
+
|
| 411 |
+
data = animator.datasets[t]['ash_concentration'].values[z_index]
|
| 412 |
+
interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 413 |
+
interp = np.where(interp < 0, np.nan, interp)
|
| 414 |
+
|
| 415 |
+
# Extract zoom window from interpolated data
|
| 416 |
+
lat_idx = np.where((animator.lats >= lat_zoom_min) & (animator.lats <= lat_zoom_max))[0]
|
| 417 |
+
lon_idx = np.where((animator.lons >= lon_zoom_min) & (animator.lons <= lon_zoom_max))[0]
|
| 418 |
+
zoom_plot = interp[np.ix_(lat_idx, lon_idx)]
|
| 419 |
+
|
| 420 |
+
valid_vals = interp[np.isfinite(interp)]
|
| 421 |
+
if valid_vals.size == 0:
|
| 422 |
+
return []
|
| 423 |
+
|
| 424 |
+
min_val = np.nanmin(valid_vals)
|
| 425 |
+
max_val = np.nanmax(valid_vals)
|
| 426 |
+
log_cutoff = 1e-3
|
| 427 |
+
log_ratio = max_val / (min_val + 1e-6)
|
| 428 |
+
use_log = min_val > log_cutoff and log_ratio > 100
|
| 429 |
+
|
| 430 |
+
if use_log:
|
| 431 |
+
data_for_plot = np.where(interp > log_cutoff, interp, np.nan)
|
| 432 |
+
levels = np.logspace(np.log10(log_cutoff), np.log10(max_val), 20)
|
| 433 |
+
scale_label = "Hybrid Log"
|
| 434 |
+
else:
|
| 435 |
+
data_for_plot = interp
|
| 436 |
+
levels = np.linspace(0, max_val, 20)
|
| 437 |
+
scale_label = "Linear"
|
| 438 |
+
|
| 439 |
+
draw_etopo_basemap(ax1, mode='stock')
|
| 440 |
+
draw_etopo_basemap(ax2, mode='stock')
|
| 441 |
+
|
| 442 |
+
c1 = ax1.contourf(animator.lons, animator.lats, data_for_plot, levels=levels,
|
| 443 |
+
cmap="rainbow", alpha=0.6, transform=proj)
|
| 444 |
+
ax1.contour(animator.lons, animator.lats, data_for_plot, levels=levels,
|
| 445 |
+
colors='black', linewidths=0.5, transform=proj)
|
| 446 |
+
ax1.set_title(f"T{t+1} | Alt: {z_val} km (Full - {scale_label})")
|
| 447 |
+
ax1.set_extent([animator.lons.min(), animator.lons.max(), animator.lats.min(), animator.lats.max()])
|
| 448 |
+
ax1.coastlines()
|
| 449 |
+
ax1.add_feature(cfeature.BORDERS, linestyle=':')
|
| 450 |
+
ax1.add_feature(cfeature.LAND)
|
| 451 |
+
ax1.add_feature(cfeature.OCEAN)
|
| 452 |
+
|
| 453 |
+
c2 = ax2.contourf(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=levels,
|
| 454 |
+
cmap="rainbow", alpha=0.4, transform=proj)
|
| 455 |
+
ax2.contour(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=levels,
|
| 456 |
+
colors='black', linewidths=0.5, transform=proj)
|
| 457 |
+
ax2.set_title(f"T{t+1} | Alt: {z_val} km (Zoom - {scale_label})")
|
| 458 |
+
ax2.set_extent([lon_zoom_min, lon_zoom_max, lat_zoom_min, lat_zoom_max])
|
| 459 |
+
ax2.coastlines()
|
| 460 |
+
ax2.add_feature(cfeature.BORDERS, linestyle=':')
|
| 461 |
+
ax2.add_feature(cfeature.LAND)
|
| 462 |
+
ax2.add_feature(cfeature.OCEAN)
|
| 463 |
+
|
| 464 |
+
ax2.text(animator.lons[0], animator.lats[0], animator.country_label, fontsize=9, color='white',
|
| 465 |
+
transform=proj, bbox=dict(facecolor='black', alpha=0.5))
|
| 466 |
+
|
| 467 |
+
texts_ax1, texts_ax2 = [], []
|
| 468 |
+
for country in country_geoms:
|
| 469 |
+
name = country.attributes['NAME_LONG']
|
| 470 |
+
geom = country.geometry
|
| 471 |
+
try:
|
| 472 |
+
lon, lat = geom.centroid.x, geom.centroid.y
|
| 473 |
+
if (lon_zoom_min <= lon <= lon_zoom_max) and (lat_zoom_min <= lat <= lat_zoom_max):
|
| 474 |
+
text = ax2.text(lon, lat, name, fontsize=6, transform=proj,
|
| 475 |
+
ha='center', va='center', color='white',
|
| 476 |
+
bbox=dict(facecolor='black', alpha=0.5, linewidth=0))
|
| 477 |
+
texts_ax2.append(text)
|
| 478 |
+
|
| 479 |
+
if (animator.lons.min() <= lon <= animator.lons.max()) and (animator.lats.min() <= lat <= animator.lats.max()):
|
| 480 |
+
text = ax1.text(lon, lat, name, fontsize=6, transform=proj,
|
| 481 |
+
ha='center', va='center', color='white',
|
| 482 |
+
bbox=dict(facecolor='black', alpha=0.5, linewidth=0))
|
| 483 |
+
texts_ax1.append(text)
|
| 484 |
+
except:
|
| 485 |
+
continue
|
| 486 |
+
|
| 487 |
+
adjust_text(texts_ax1, ax=ax1, only_move={'points': 'y', 'text': 'y'},
|
| 488 |
+
arrowprops=dict(arrowstyle="->", color='white', lw=0.5))
|
| 489 |
+
adjust_text(texts_ax2, ax=ax2, only_move={'points': 'y', 'text': 'y'},
|
| 490 |
+
arrowprops=dict(arrowstyle="->", color='white', lw=0.5))
|
| 491 |
+
|
| 492 |
+
if np.nanmax(valid_vals) > threshold:
|
| 493 |
+
alert_text = f"⚠ Exceeds {threshold} g/m³!"
|
| 494 |
+
for ax in [ax1, ax2]:
|
| 495 |
+
ax.text(0.99, 0.01, alert_text, transform=ax.transAxes,
|
| 496 |
+
ha='right', va='bottom', fontsize=10, color='red',
|
| 497 |
+
bbox=dict(facecolor='white', alpha=0.8, edgecolor='red'))
|
| 498 |
+
ax1.contour(animator.lons, animator.lats, interp, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 499 |
+
ax2.contour(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 500 |
+
|
| 501 |
+
if not hasattr(update, "colorbar"):
|
| 502 |
+
update.colorbar = fig.colorbar(c1, ax=[ax1, ax2], orientation='vertical',
|
| 503 |
+
label="Ash concentration (g/m³)")
|
| 504 |
+
formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 505 |
+
update.colorbar.ax.yaxis.set_major_formatter(formatter)
|
| 506 |
+
if use_log:
|
| 507 |
+
update.colorbar.ax.text(1.05, 1.02, "log scale", transform=update.colorbar.ax.transAxes,
|
| 508 |
+
fontsize=9, color='gray', rotation=90, ha='left', va='bottom')
|
| 509 |
+
|
| 510 |
+
return []
|
| 511 |
+
|
| 512 |
+
ani = animation.FuncAnimation(fig, update, frames=valid_frames, blit=False)
|
| 513 |
+
gif_path = os.path.join(output_folder, f"ash_T1-Tn_Z{z_index+1}.gif")
|
| 514 |
+
ani.save(gif_path, writer='pillow', fps=fps)
|
| 515 |
+
plt.close()
|
| 516 |
+
print(f"✅ Saved animation for Z={z_val} km to {gif_path}")
|
ash_animator/animation_single.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import matplotlib.animation as animation
|
| 6 |
+
import matplotlib.ticker as mticker
|
| 7 |
+
import cartopy.crs as ccrs
|
| 8 |
+
import cartopy.feature as cfeature
|
| 9 |
+
from .interpolation import interpolate_grid
|
| 10 |
+
from .basemaps import draw_etopo_basemap
|
| 11 |
+
|
| 12 |
+
def animate_single_z_level(animator, z_km: float, output_path: str, fps: int = 2, include_metadata: bool = True, threshold: float = 0.1):
|
| 13 |
+
if z_km not in animator.levels:
|
| 14 |
+
print(f"Z level {z_km} km not found in dataset.")
|
| 15 |
+
return
|
| 16 |
+
|
| 17 |
+
z_index = np.where(animator.levels == z_km)[0][0]
|
| 18 |
+
fig = plt.figure(figsize=(16, 7))
|
| 19 |
+
proj = ccrs.PlateCarree()
|
| 20 |
+
ax1 = fig.add_subplot(1, 2, 1, projection=proj)
|
| 21 |
+
ax2 = fig.add_subplot(1, 2, 2, projection=proj)
|
| 22 |
+
|
| 23 |
+
meta = animator.datasets[0].attrs
|
| 24 |
+
legend_text = (
|
| 25 |
+
f"Run name: {meta.get('run_name', 'N/A')}\n"
|
| 26 |
+
f"Run time: {meta.get('run_time', 'N/A')}\n"
|
| 27 |
+
f"Met data: {meta.get('met_data', 'N/A')}\n"
|
| 28 |
+
f"Start release: {meta.get('start_of_release', 'N/A')}\n"
|
| 29 |
+
f"End release: {meta.get('end_of_release', 'N/A')}\n"
|
| 30 |
+
f"Source strength: {meta.get('source_strength', 'N/A')} g/s\n"
|
| 31 |
+
f"Release loc: {meta.get('release_location', 'N/A')}\n"
|
| 32 |
+
f"Release height: {meta.get('release_height', 'N/A')} m asl\n"
|
| 33 |
+
f"Run duration: {meta.get('run_duration', 'N/A')}"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
valid_mask = np.stack([
|
| 37 |
+
ds['ash_concentration'].values[z_index] for ds in animator.datasets
|
| 38 |
+
]).max(axis=0) > 0
|
| 39 |
+
y_idx, x_idx = np.where(valid_mask)
|
| 40 |
+
|
| 41 |
+
if y_idx.size == 0 or x_idx.size == 0:
|
| 42 |
+
print(f"Z level {z_km} km has no valid data. Skipping...")
|
| 43 |
+
plt.close()
|
| 44 |
+
return
|
| 45 |
+
|
| 46 |
+
y_min, y_max = y_idx.min(), y_idx.max()
|
| 47 |
+
x_min, x_max = x_idx.min(), x_idx.max()
|
| 48 |
+
buffer_y = int((y_max - y_min) * 0.5)
|
| 49 |
+
buffer_x = int((x_max - x_min) * 0.5)
|
| 50 |
+
y_start = max(0, y_min - buffer_y)
|
| 51 |
+
y_end = min(animator.lat_grid.shape[0], y_max + buffer_y + 1)
|
| 52 |
+
x_start = max(0, x_min - buffer_x)
|
| 53 |
+
x_end = min(animator.lon_grid.shape[1], x_max + buffer_x + 1)
|
| 54 |
+
|
| 55 |
+
lat_zoom = animator.lats[y_start:y_end]
|
| 56 |
+
lon_zoom = animator.lons[x_start:x_end]
|
| 57 |
+
lon_zoom_grid, lat_zoom_grid = np.meshgrid(lon_zoom, lat_zoom)
|
| 58 |
+
|
| 59 |
+
valid_frames = []
|
| 60 |
+
for t in range(len(animator.datasets)):
|
| 61 |
+
interp = interpolate_grid(animator.datasets[t]['ash_concentration'].values[z_index],
|
| 62 |
+
animator.lon_grid, animator.lat_grid)
|
| 63 |
+
if np.isfinite(interp).sum() > 0:
|
| 64 |
+
valid_frames.append(t)
|
| 65 |
+
|
| 66 |
+
if not valid_frames:
|
| 67 |
+
print(f"No valid frames for Z={z_km} km. Skipping animation.")
|
| 68 |
+
plt.close()
|
| 69 |
+
return
|
| 70 |
+
|
| 71 |
+
def update(t):
|
| 72 |
+
ax1.clear()
|
| 73 |
+
ax2.clear()
|
| 74 |
+
|
| 75 |
+
data = animator.datasets[t]['ash_concentration'].values[z_index]
|
| 76 |
+
interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 77 |
+
interp = np.where(interp < 0, np.nan, interp)
|
| 78 |
+
zoom_plot = interp[y_start:y_end, x_start:x_end]
|
| 79 |
+
|
| 80 |
+
valid_vals = interp[np.isfinite(interp)]
|
| 81 |
+
if valid_vals.size == 0:
|
| 82 |
+
return []
|
| 83 |
+
|
| 84 |
+
min_val = np.nanmin(valid_vals)
|
| 85 |
+
max_val = np.nanmax(valid_vals)
|
| 86 |
+
log_cutoff = 1e-3
|
| 87 |
+
use_log = min_val > log_cutoff and (max_val / (min_val + 1e-6)) > 100
|
| 88 |
+
|
| 89 |
+
levels = np.logspace(np.log10(log_cutoff), np.log10(max_val), 20) if use_log else np.linspace(0, max_val, 20)
|
| 90 |
+
data_for_plot = np.where(interp > log_cutoff, interp, 0) if use_log else interp
|
| 91 |
+
scale_label = "Log" if use_log else "Linear"
|
| 92 |
+
|
| 93 |
+
draw_etopo_basemap(ax1, mode='stock')
|
| 94 |
+
draw_etopo_basemap(ax2, mode='stock')
|
| 95 |
+
|
| 96 |
+
c1 = ax1.contourf(animator.lons, animator.lats, data_for_plot, levels=levels,
|
| 97 |
+
cmap="rainbow", alpha=0.6, transform=proj)
|
| 98 |
+
ax1.set_title(f"T{t+1} | Alt: {z_km} km (Full - {scale_label})")
|
| 99 |
+
ax1.set_extent([animator.lons.min(), animator.lons.max(), animator.lats.min(), animator.lats.max()])
|
| 100 |
+
ax1.coastlines(); ax1.add_feature(cfeature.BORDERS); ax1.add_feature(cfeature.LAND); ax1.add_feature(cfeature.OCEAN)
|
| 101 |
+
|
| 102 |
+
c2 = ax2.contourf(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=levels,
|
| 103 |
+
cmap="rainbow", alpha=0.6, transform=proj)
|
| 104 |
+
ax2.set_title(f"T{t+1} | Alt: {z_km} km (Zoom - {scale_label})")
|
| 105 |
+
ax2.set_extent([lon_zoom.min(), lon_zoom.max(), lat_zoom.min(), lat_zoom.max()])
|
| 106 |
+
ax2.coastlines(); ax2.add_feature(cfeature.BORDERS); ax2.add_feature(cfeature.LAND); ax2.add_feature(cfeature.OCEAN)
|
| 107 |
+
|
| 108 |
+
if not hasattr(update, "colorbar"):
|
| 109 |
+
update.colorbar = fig.colorbar(c1, ax=[ax1, ax2], orientation='vertical',
|
| 110 |
+
label="Ash concentration (g/m³)")
|
| 111 |
+
formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 112 |
+
update.colorbar.ax.yaxis.set_major_formatter(formatter)
|
| 113 |
+
if use_log:
|
| 114 |
+
update.colorbar.ax.text(1.05, 1.02, "log scale", transform=update.colorbar.ax.transAxes,
|
| 115 |
+
fontsize=9, color='gray', rotation=90, ha='left', va='bottom')
|
| 116 |
+
|
| 117 |
+
if include_metadata:
|
| 118 |
+
ax1.annotate(legend_text, xy=(0.75, 0.99), xycoords='axes fraction',
|
| 119 |
+
fontsize=8, ha='left', va='top',
|
| 120 |
+
bbox=dict(boxstyle="round", facecolor="white", edgecolor="gray"))
|
| 121 |
+
for ax in [ax1, ax2]:
|
| 122 |
+
ax.text(0.01, 0.01,
|
| 123 |
+
f"Source: NAME\nRes: {animator.x_res:.2f}°\n{meta.get('run_name', 'N/A')}",
|
| 124 |
+
transform=ax.transAxes, fontsize=8, color='white',
|
| 125 |
+
bbox=dict(facecolor='black', alpha=0.5))
|
| 126 |
+
|
| 127 |
+
for ax in [ax1, ax2]:
|
| 128 |
+
ax.text(0.01, 0.98, f"Time step T{t+1}", transform=ax.transAxes,
|
| 129 |
+
fontsize=9, color='white', va='top', ha='left',
|
| 130 |
+
bbox=dict(facecolor='black', alpha=0.4, boxstyle='round'))
|
| 131 |
+
|
| 132 |
+
if np.nanmax(valid_vals) > threshold:
|
| 133 |
+
alert_text = f"⚠ Exceeds {threshold} g/m³!"
|
| 134 |
+
for ax in [ax1, ax2]:
|
| 135 |
+
ax.text(0.99, 0.01, alert_text, transform=ax.transAxes,
|
| 136 |
+
ha='right', va='bottom', fontsize=10, color='red',
|
| 137 |
+
bbox=dict(facecolor='white', alpha=0.8, edgecolor='red'))
|
| 138 |
+
ax1.contour(animator.lons, animator.lats, interp, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 139 |
+
ax2.contour(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 140 |
+
|
| 141 |
+
return []
|
| 142 |
+
|
| 143 |
+
ani = animation.FuncAnimation(fig, update, frames=valid_frames, blit=False)
|
| 144 |
+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 145 |
+
ani.save(output_path, writer='pillow', fps=fps)
|
| 146 |
+
plt.close()
|
| 147 |
+
print(f"✅ Saved animation for Z={z_km} km to {output_path}")
|
ash_animator/animation_vertical.py
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import matplotlib.animation as animation
|
| 6 |
+
import matplotlib.ticker as mticker
|
| 7 |
+
import cartopy.crs as ccrs
|
| 8 |
+
import cartopy.feature as cfeature
|
| 9 |
+
import cartopy.io.shapereader as shpreader
|
| 10 |
+
from .interpolation import interpolate_grid
|
| 11 |
+
from .basemaps import draw_etopo_basemap
|
| 12 |
+
|
| 13 |
+
# def animate_vertical_profile(animator, t_index: int, output_path: str, fps: int = 2, include_metadata: bool = True, threshold: float = 0.1):
|
| 14 |
+
# if not (0 <= t_index < len(animator.datasets)):
|
| 15 |
+
# print(f"Invalid time index {t_index}. Must be between 0 and {len(animator.datasets) - 1}.")
|
| 16 |
+
# return
|
| 17 |
+
|
| 18 |
+
# ds = animator.datasets[t_index]
|
| 19 |
+
# fig = plt.figure(figsize=(16, 7))
|
| 20 |
+
# proj = ccrs.PlateCarree()
|
| 21 |
+
# ax1 = fig.add_subplot(1, 2, 1, projection=proj)
|
| 22 |
+
# ax2 = fig.add_subplot(1, 2, 2, projection=proj)
|
| 23 |
+
|
| 24 |
+
# meta = ds.attrs
|
| 25 |
+
# legend_text = (
|
| 26 |
+
# f"Run name: {meta.get('run_name', 'N/A')}\n"
|
| 27 |
+
# f"Run time: {meta.get('run_time', 'N/A')}\n"
|
| 28 |
+
# f"Met data: {meta.get('met_data', 'N/A')}\n"
|
| 29 |
+
# f"Start release: {meta.get('start_of_release', 'N/A')}\n"
|
| 30 |
+
# f"End release: {meta.get('end_of_release', 'N/A')}\n"
|
| 31 |
+
# f"Source strength: {meta.get('source_strength', 'N/A')} g/s\n"
|
| 32 |
+
# f"Release loc: {meta.get('release_location', 'N/A')}\n"
|
| 33 |
+
# f"Release height: {meta.get('release_height', 'N/A')} m asl\n"
|
| 34 |
+
# f"Run duration: {meta.get('run_duration', 'N/A')}"
|
| 35 |
+
# )
|
| 36 |
+
|
| 37 |
+
# valid_mask = np.stack([ds['ash_concentration'].values[z] for z in range(len(animator.levels))]).max(axis=0) > 0
|
| 38 |
+
# y_idx, x_idx = np.where(valid_mask)
|
| 39 |
+
|
| 40 |
+
# if y_idx.size == 0 or x_idx.size == 0:
|
| 41 |
+
# print(f"No valid data found for time T{t_index+1}. Skipping...")
|
| 42 |
+
# plt.close()
|
| 43 |
+
# return
|
| 44 |
+
|
| 45 |
+
# y_min, y_max = y_idx.min(), y_idx.max()
|
| 46 |
+
# x_min, x_max = x_idx.min(), x_idx.max()
|
| 47 |
+
# buffer_y = int((y_max - y_min) * 0.1)
|
| 48 |
+
# buffer_x = int((x_max - x_min) * 0.1)
|
| 49 |
+
# y_start = max(0, y_min - buffer_y)
|
| 50 |
+
# y_end = min(animator.lat_grid.shape[0], y_max + buffer_y + 1)
|
| 51 |
+
# x_start = max(0, x_min - buffer_x)
|
| 52 |
+
# x_end = min(animator.lon_grid.shape[1], x_max + buffer_x + 1)
|
| 53 |
+
|
| 54 |
+
# lat_zoom = animator.lats[y_start:y_end]
|
| 55 |
+
# lon_zoom = animator.lons[x_start:x_end]
|
| 56 |
+
# lon_zoom_grid, lat_zoom_grid = np.meshgrid(lon_zoom, lat_zoom)
|
| 57 |
+
|
| 58 |
+
# z_indices_with_data = []
|
| 59 |
+
# for z_index in range(len(animator.levels)):
|
| 60 |
+
# data = ds['ash_concentration'].values[z_index]
|
| 61 |
+
# interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 62 |
+
# if np.isfinite(interp).sum() > 0:
|
| 63 |
+
# z_indices_with_data.append(z_index)
|
| 64 |
+
|
| 65 |
+
# if not z_indices_with_data:
|
| 66 |
+
# print(f"No valid Z-levels at time T{t_index+1}.")
|
| 67 |
+
# plt.close()
|
| 68 |
+
# return
|
| 69 |
+
|
| 70 |
+
# def update(z_index):
|
| 71 |
+
# ax1.clear()
|
| 72 |
+
# ax2.clear()
|
| 73 |
+
|
| 74 |
+
# data = ds['ash_concentration'].values[z_index]
|
| 75 |
+
# interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 76 |
+
# interp = np.where(interp < 0, np.nan, interp)
|
| 77 |
+
# zoom_plot = interp[y_start:y_end, x_start:x_end]
|
| 78 |
+
|
| 79 |
+
# valid_vals = interp[np.isfinite(interp)]
|
| 80 |
+
# if valid_vals.size == 0:
|
| 81 |
+
# return []
|
| 82 |
+
|
| 83 |
+
# min_val = np.nanmin(valid_vals)
|
| 84 |
+
# max_val = np.nanmax(valid_vals)
|
| 85 |
+
# log_cutoff = 1e-3
|
| 86 |
+
# use_log = min_val > log_cutoff and (max_val / (min_val + 1e-6)) > 100
|
| 87 |
+
|
| 88 |
+
# levels = np.logspace(np.log10(log_cutoff), np.log10(max_val), 20) if use_log else np.linspace(0, max_val, 20)
|
| 89 |
+
# data_for_plot = np.where(interp > log_cutoff, interp, 0) if use_log else interp
|
| 90 |
+
# scale_label = "Log" if use_log else "Linear"
|
| 91 |
+
|
| 92 |
+
# draw_etopo_basemap(ax1, mode='stock')
|
| 93 |
+
# draw_etopo_basemap(ax2, mode='stock')
|
| 94 |
+
|
| 95 |
+
# c1 = ax1.contourf(animator.lons, animator.lats, data_for_plot, levels=levels,
|
| 96 |
+
# cmap="rainbow", alpha=0.6, transform=proj)
|
| 97 |
+
# ax1.set_title(f"T{t_index+1} | Alt: {animator.levels[z_index]} km (Full - {scale_label})")
|
| 98 |
+
# ax1.set_extent([animator.lons.min(), animator.lons.max(), animator.lats.min(), animator.lats.max()])
|
| 99 |
+
# ax1.coastlines(); ax1.add_feature(cfeature.BORDERS, linestyle=':')
|
| 100 |
+
# ax1.add_feature(cfeature.LAND); ax1.add_feature(cfeature.OCEAN)
|
| 101 |
+
|
| 102 |
+
# c2 = ax2.contourf(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=levels,
|
| 103 |
+
# cmap="rainbow", alpha=0.6, transform=proj)
|
| 104 |
+
# ax2.set_title(f"T{t_index+1} | Alt: {animator.levels[z_index]} km (Zoom - {scale_label})")
|
| 105 |
+
# ax2.set_extent([lon_zoom.min(), lon_zoom.max(), lat_zoom.min(), lat_zoom.max()])
|
| 106 |
+
# ax2.coastlines(); ax2.add_feature(cfeature.BORDERS, linestyle=':')
|
| 107 |
+
# ax2.add_feature(cfeature.LAND); ax2.add_feature(cfeature.OCEAN)
|
| 108 |
+
|
| 109 |
+
# for ax in [ax1, ax2]:
|
| 110 |
+
# ax.text(0.01, 0.98, f"Altitude: {animator.levels[z_index]:.2f} km", transform=ax.transAxes,
|
| 111 |
+
# fontsize=9, color='white', va='top', ha='left',
|
| 112 |
+
# bbox=dict(facecolor='black', alpha=0.4, boxstyle='round'))
|
| 113 |
+
|
| 114 |
+
# if include_metadata:
|
| 115 |
+
# ax.text(0.01, 0.01,
|
| 116 |
+
# f"Source: NAME\nRes: {animator.x_res:.2f}°\n{meta.get('run_name', 'N/A')}",
|
| 117 |
+
# transform=ax.transAxes, fontsize=8, color='white',
|
| 118 |
+
# bbox=dict(facecolor='black', alpha=0.5))
|
| 119 |
+
|
| 120 |
+
# if np.nanmax(valid_vals) > threshold:
|
| 121 |
+
# for ax in [ax1, ax2]:
|
| 122 |
+
# ax.text(0.99, 0.01, f"⚠ Exceeds {threshold} g/m³!", transform=ax.transAxes,
|
| 123 |
+
# ha='right', va='bottom', fontsize=10, color='red',
|
| 124 |
+
# bbox=dict(facecolor='white', alpha=0.8, edgecolor='red'))
|
| 125 |
+
# ax1.contour(animator.lons, animator.lats, interp, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 126 |
+
# ax2.contour(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 127 |
+
|
| 128 |
+
# if include_metadata and not hasattr(update, "legend_text"):
|
| 129 |
+
# ax1.annotate(legend_text, xy=(0.75, 0.99), xycoords='axes fraction',
|
| 130 |
+
# fontsize=8, ha='left', va='top',
|
| 131 |
+
# bbox=dict(boxstyle="round", facecolor="white", edgecolor="gray"))
|
| 132 |
+
|
| 133 |
+
# if not hasattr(update, "colorbar"):
|
| 134 |
+
# update.colorbar = fig.colorbar(c1, ax=[ax1, ax2], orientation='vertical',
|
| 135 |
+
# label="Ash concentration (g/m³)")
|
| 136 |
+
# formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 137 |
+
# update.colorbar.ax.yaxis.set_major_formatter(formatter)
|
| 138 |
+
|
| 139 |
+
# if use_log:
|
| 140 |
+
# update.colorbar.ax.text(1.05, 1.02, "log scale", transform=update.colorbar.ax.transAxes,
|
| 141 |
+
# fontsize=9, color='gray', rotation=90, ha='left', va='bottom')
|
| 142 |
+
|
| 143 |
+
# return []
|
| 144 |
+
|
| 145 |
+
# os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 146 |
+
# ani = animation.FuncAnimation(fig, update, frames=z_indices_with_data, blit=False)
|
| 147 |
+
# ani.save(output_path, writer='pillow', fps=fps)
|
| 148 |
+
# plt.close()
|
| 149 |
+
# print(f"✅ Saved vertical profile animation for T{t_index+1} to {output_path}")
|
| 150 |
+
|
| 151 |
+
# def animate_all_vertical_profiles(animator, output_folder: str, fps: int = 2, include_metadata: bool = True, threshold: float = 0.1):
|
| 152 |
+
# os.makedirs(output_folder, exist_ok=True)
|
| 153 |
+
# for t_index in range(len(animator.datasets)):
|
| 154 |
+
# output_path = os.path.join(output_folder, f"vertical_T{t_index+1:02d}.gif")
|
| 155 |
+
# print(f"🔄 Generating vertical profile animation for T{t_index+1}...")
|
| 156 |
+
# animate_vertical_profile(animator, t_index, output_path, fps, include_metadata, threshold)
|
| 157 |
+
|
| 158 |
+
import os
|
| 159 |
+
import numpy as np
|
| 160 |
+
import matplotlib.pyplot as plt
|
| 161 |
+
import matplotlib.animation as animation
|
| 162 |
+
import matplotlib.ticker as mticker
|
| 163 |
+
import cartopy.crs as ccrs
|
| 164 |
+
import cartopy.feature as cfeature
|
| 165 |
+
import cartopy.io.shapereader as shpreader
|
| 166 |
+
from .interpolation import interpolate_grid
|
| 167 |
+
from .basemaps import draw_etopo_basemap
|
| 168 |
+
from adjustText import adjust_text
|
| 169 |
+
|
| 170 |
+
def animate_vertical_profile(animator, t_index: int, output_path: str, fps: int = 2,
|
| 171 |
+
include_metadata: bool = True, threshold: float = 0.1,
|
| 172 |
+
zoom_width_deg: float = 6.0, zoom_height_deg: float = 6.0):
|
| 173 |
+
if not (0 <= t_index < len(animator.datasets)):
|
| 174 |
+
print(f"Invalid time index {t_index}. Must be between 0 and {len(animator.datasets) - 1}.")
|
| 175 |
+
return
|
| 176 |
+
|
| 177 |
+
countries_shp = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries')
|
| 178 |
+
reader = shpreader.Reader(countries_shp)
|
| 179 |
+
country_geoms = list(reader.records())
|
| 180 |
+
|
| 181 |
+
ds = animator.datasets[t_index]
|
| 182 |
+
fig = plt.figure(figsize=(18, 7)) # Wider for metadata outside
|
| 183 |
+
proj = ccrs.PlateCarree()
|
| 184 |
+
ax1 = fig.add_subplot(1, 2, 1, projection=proj)
|
| 185 |
+
ax2 = fig.add_subplot(1, 2, 2, projection=proj)
|
| 186 |
+
|
| 187 |
+
meta = ds.attrs
|
| 188 |
+
legend_text = (
|
| 189 |
+
f"Run name: {meta.get('run_name', 'N/A')}\n"
|
| 190 |
+
f"Run time: {meta.get('run_time', 'N/A')}\n"
|
| 191 |
+
f"Met data: {meta.get('met_data', 'N/A')}\n"
|
| 192 |
+
f"Start release: {meta.get('start_of_release', 'N/A')}\n"
|
| 193 |
+
f"End release: {meta.get('end_of_release', 'N/A')}\n"
|
| 194 |
+
f"Source strength: {meta.get('source_strength', 'N/A')} g/s\n"
|
| 195 |
+
f"Release loc: {meta.get('release_location', 'N/A')}\n"
|
| 196 |
+
f"Release height: {meta.get('release_height', 'N/A')} m asl\n"
|
| 197 |
+
f"Run duration: {meta.get('run_duration', 'N/A')}"
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
# 🔍 Find most active point at this time step
|
| 201 |
+
max_conc = -np.inf
|
| 202 |
+
center_lat = center_lon = None
|
| 203 |
+
for z in range(len(animator.levels)):
|
| 204 |
+
data = ds['ash_concentration'].values[z]
|
| 205 |
+
if np.max(data) > max_conc:
|
| 206 |
+
max_conc = np.max(data)
|
| 207 |
+
max_idx = np.unravel_index(np.argmax(data), data.shape)
|
| 208 |
+
center_lat = animator.lat_grid[max_idx]
|
| 209 |
+
center_lon = animator.lon_grid[max_idx]
|
| 210 |
+
|
| 211 |
+
if center_lat is None or center_lon is None:
|
| 212 |
+
print(f"No valid data found for time T{t_index+1}. Skipping...")
|
| 213 |
+
plt.close()
|
| 214 |
+
return
|
| 215 |
+
|
| 216 |
+
# 🌍 Define fixed zoom extents
|
| 217 |
+
lon_zoom_min = center_lon - zoom_width_deg / 2
|
| 218 |
+
lon_zoom_max = center_lon + zoom_width_deg / 2
|
| 219 |
+
lat_zoom_min = center_lat - zoom_height_deg / 2
|
| 220 |
+
lat_zoom_max = center_lat + zoom_height_deg / 2
|
| 221 |
+
|
| 222 |
+
lat_zoom = animator.lats[(animator.lats >= lat_zoom_min) & (animator.lats <= lat_zoom_max)]
|
| 223 |
+
lon_zoom = animator.lons[(animator.lons >= lon_zoom_min) & (animator.lons <= lon_zoom_max)]
|
| 224 |
+
lon_zoom_grid, lat_zoom_grid = np.meshgrid(lon_zoom, lat_zoom)
|
| 225 |
+
|
| 226 |
+
z_indices_with_data = []
|
| 227 |
+
for z_index in range(len(animator.levels)):
|
| 228 |
+
data = ds['ash_concentration'].values[z_index]
|
| 229 |
+
interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 230 |
+
if np.isfinite(interp).sum() > 0:
|
| 231 |
+
z_indices_with_data.append(z_index)
|
| 232 |
+
|
| 233 |
+
if not z_indices_with_data:
|
| 234 |
+
print(f"No valid Z-levels at time T{t_index+1}.")
|
| 235 |
+
plt.close()
|
| 236 |
+
return
|
| 237 |
+
|
| 238 |
+
def update(z_index):
|
| 239 |
+
ax1.clear()
|
| 240 |
+
ax2.clear()
|
| 241 |
+
|
| 242 |
+
data = ds['ash_concentration'].values[z_index]
|
| 243 |
+
interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 244 |
+
interp = np.where(interp < 0, np.nan, interp)
|
| 245 |
+
|
| 246 |
+
lat_idx = np.where((animator.lats >= lat_zoom_min) & (animator.lats <= lat_zoom_max))[0]
|
| 247 |
+
lon_idx = np.where((animator.lons >= lon_zoom_min) & (animator.lons <= lon_zoom_max))[0]
|
| 248 |
+
zoom_plot = interp[np.ix_(lat_idx, lon_idx)]
|
| 249 |
+
|
| 250 |
+
valid_vals = interp[np.isfinite(interp)]
|
| 251 |
+
if valid_vals.size == 0:
|
| 252 |
+
return []
|
| 253 |
+
|
| 254 |
+
min_val = np.nanmin(valid_vals)
|
| 255 |
+
max_val = np.nanmax(valid_vals)
|
| 256 |
+
log_cutoff = 1e-3
|
| 257 |
+
use_log = min_val > log_cutoff and (max_val / (min_val + 1e-6)) > 100
|
| 258 |
+
|
| 259 |
+
levels = np.logspace(np.log10(log_cutoff), np.log10(max_val), 20) if use_log else np.linspace(0, max_val, 20)
|
| 260 |
+
data_for_plot = np.where(interp > log_cutoff, interp, 0) if use_log else interp
|
| 261 |
+
scale_label = "Log" if use_log else "Linear"
|
| 262 |
+
|
| 263 |
+
draw_etopo_basemap(ax1, mode='stock')
|
| 264 |
+
draw_etopo_basemap(ax2, mode='stock')
|
| 265 |
+
|
| 266 |
+
c1 = ax1.contourf(animator.lons, animator.lats, data_for_plot, levels=levels,
|
| 267 |
+
cmap="rainbow", alpha=0.6, transform=proj)
|
| 268 |
+
ax1.set_title(f"T{t_index+1} | Alt: {animator.levels[z_index]} km (Full - {scale_label})")
|
| 269 |
+
ax1.set_extent([animator.lons.min(), animator.lons.max(), animator.lats.min(), animator.lats.max()])
|
| 270 |
+
ax1.coastlines(); ax1.add_feature(cfeature.BORDERS, linestyle=':')
|
| 271 |
+
ax1.add_feature(cfeature.LAND); ax1.add_feature(cfeature.OCEAN)
|
| 272 |
+
|
| 273 |
+
c2 = ax2.contourf(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=levels,
|
| 274 |
+
cmap="rainbow", alpha=0.6, transform=proj)
|
| 275 |
+
ax2.set_title(f"T{t_index+1} | Alt: {animator.levels[z_index]} km (Zoom - {scale_label})")
|
| 276 |
+
ax2.set_extent([lon_zoom_min, lon_zoom_max, lat_zoom_min, lat_zoom_max])
|
| 277 |
+
ax2.coastlines(); ax2.add_feature(cfeature.BORDERS, linestyle=':')
|
| 278 |
+
ax2.add_feature(cfeature.LAND); ax2.add_feature(cfeature.OCEAN)
|
| 279 |
+
|
| 280 |
+
for ax in [ax1, ax2]:
|
| 281 |
+
ax.text(0.01, 0.98, f"Altitude: {animator.levels[z_index]:.2f} km", transform=ax.transAxes,
|
| 282 |
+
fontsize=9, color='white', va='top', ha='left',
|
| 283 |
+
bbox=dict(facecolor='black', alpha=0.4, boxstyle='round'))
|
| 284 |
+
|
| 285 |
+
if include_metadata:
|
| 286 |
+
fig.text(0.50, 0.1, legend_text, va='center', ha='left', fontsize=8,
|
| 287 |
+
bbox=dict(facecolor='white', alpha=0.8, edgecolor='gray'),
|
| 288 |
+
transform=fig.transFigure)
|
| 289 |
+
|
| 290 |
+
if np.nanmax(valid_vals) > threshold:
|
| 291 |
+
for ax in [ax1, ax2]:
|
| 292 |
+
ax.text(0.99, 0.01, f"⚠ Exceeds {threshold} g/m³!", transform=ax.transAxes,
|
| 293 |
+
ha='right', va='bottom', fontsize=10, color='red',
|
| 294 |
+
bbox=dict(facecolor='white', alpha=0.8, edgecolor='red'))
|
| 295 |
+
ax1.contour(animator.lons, animator.lats, interp, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 296 |
+
ax2.contour(lon_zoom_grid, lat_zoom_grid, zoom_plot, levels=[threshold], colors='red', linewidths=2, transform=proj)
|
| 297 |
+
|
| 298 |
+
if not hasattr(update, "colorbar"):
|
| 299 |
+
update.colorbar = fig.colorbar(c1, ax=[ax1, ax2], orientation='vertical',
|
| 300 |
+
label="Ash concentration (g/m³)", shrink=0.75)
|
| 301 |
+
formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 302 |
+
update.colorbar.ax.yaxis.set_major_formatter(formatter)
|
| 303 |
+
|
| 304 |
+
if use_log:
|
| 305 |
+
update.colorbar.ax.text(1.05, 1.02, "log scale", transform=update.colorbar.ax.transAxes,
|
| 306 |
+
fontsize=9, color='gray', rotation=90, ha='left', va='bottom')
|
| 307 |
+
|
| 308 |
+
######################3
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
texts_ax1, texts_ax2 = [], []
|
| 312 |
+
for country in country_geoms:
|
| 313 |
+
name = country.attributes['NAME_LONG']
|
| 314 |
+
geom = country.geometry
|
| 315 |
+
try:
|
| 316 |
+
lon, lat = geom.centroid.x, geom.centroid.y
|
| 317 |
+
if (lon_zoom_min <= lon <= lon_zoom_max) and (lat_zoom_min <= lat <= lat_zoom_max):
|
| 318 |
+
text = ax2.text(lon, lat, name, fontsize=6, transform=proj,
|
| 319 |
+
ha='center', va='center', color='white',
|
| 320 |
+
bbox=dict(facecolor='black', alpha=0.5, linewidth=0))
|
| 321 |
+
texts_ax2.append(text)
|
| 322 |
+
|
| 323 |
+
if (animator.lons.min() <= lon <= animator.lons.max()) and (animator.lats.min() <= lat <= animator.lats.max()):
|
| 324 |
+
text = ax1.text(lon, lat, name, fontsize=6, transform=proj,
|
| 325 |
+
ha='center', va='center', color='white',
|
| 326 |
+
bbox=dict(facecolor='black', alpha=0.5, linewidth=0))
|
| 327 |
+
texts_ax1.append(text)
|
| 328 |
+
except:
|
| 329 |
+
continue
|
| 330 |
+
|
| 331 |
+
adjust_text(texts_ax1, ax=ax1, only_move={'points': 'y', 'text': 'y'},
|
| 332 |
+
arrowprops=dict(arrowstyle="->", color='white', lw=0.5))
|
| 333 |
+
adjust_text(texts_ax2, ax=ax2, only_move={'points': 'y', 'text': 'y'},
|
| 334 |
+
arrowprops=dict(arrowstyle="->", color='white', lw=0.5))
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
############################################
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
return []
|
| 343 |
+
|
| 344 |
+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 345 |
+
ani = animation.FuncAnimation(fig, update, frames=z_indices_with_data, blit=False)
|
| 346 |
+
ani.save(output_path, writer='pillow', fps=fps)
|
| 347 |
+
plt.close()
|
| 348 |
+
print(f"✅ Saved vertical profile animation for T{t_index+1} to {output_path}")
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
def animate_all_vertical_profiles(animator, output_folder: str, fps: int = 2,
|
| 352 |
+
include_metadata: bool = True, threshold: float = 0.1,
|
| 353 |
+
zoom_width_deg: float = 10.0, zoom_height_deg: float = 6.0):
|
| 354 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 355 |
+
for t_index in range(len(animator.datasets)):
|
| 356 |
+
output_path = os.path.join(output_folder, f"vertical_T{t_index+1:02d}.gif")
|
| 357 |
+
print(f"🔄 Generating vertical profile animation for T{t_index+1}...")
|
| 358 |
+
animate_vertical_profile(animator, t_index, output_path, fps,
|
| 359 |
+
include_metadata, threshold,
|
| 360 |
+
zoom_width_deg, zoom_height_deg)
|
ash_animator/basemaps.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import hashlib
|
| 3 |
+
import tempfile
|
| 4 |
+
import contextily as ctx
|
| 5 |
+
from mpl_toolkits.basemap import Basemap
|
| 6 |
+
import cartopy.crs as ccrs
|
| 7 |
+
import cartopy.feature as cfeature
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
|
| 11 |
+
# Determine platform and fallback cache path
|
| 12 |
+
def get_cache_dir(app_name):
|
| 13 |
+
if os.name == 'nt':
|
| 14 |
+
return os.path.join(os.getenv('LOCALAPPDATA', tempfile.gettempdir()), f"{app_name}_cache")
|
| 15 |
+
elif os.name == 'posix':
|
| 16 |
+
home_dir = os.path.expanduser("~")
|
| 17 |
+
if os.path.isdir(home_dir) and os.access(home_dir, os.W_OK):
|
| 18 |
+
return os.path.join(home_dir, f".{app_name}_cache")
|
| 19 |
+
else:
|
| 20 |
+
return os.path.join(tempfile.gettempdir(), f"{app_name}_cache")
|
| 21 |
+
else:
|
| 22 |
+
return os.path.join(tempfile.gettempdir(), f"{app_name}_cache")
|
| 23 |
+
|
| 24 |
+
# Define cache directories
|
| 25 |
+
CTX_TILE_CACHE_DIR = get_cache_dir("contextily")
|
| 26 |
+
BASEMAP_TILE_CACHE_DIR = get_cache_dir("basemap")
|
| 27 |
+
|
| 28 |
+
os.environ["XDG_CACHE_HOME"] = CTX_TILE_CACHE_DIR
|
| 29 |
+
os.makedirs(CTX_TILE_CACHE_DIR, exist_ok=True)
|
| 30 |
+
os.makedirs(BASEMAP_TILE_CACHE_DIR, exist_ok=True)
|
| 31 |
+
|
| 32 |
+
def draw_etopo_basemap(ax, mode="basemap", zoom=11):
|
| 33 |
+
"""
|
| 34 |
+
Draws a high-resolution basemap background on the provided Cartopy GeoAxes.
|
| 35 |
+
|
| 36 |
+
Parameters
|
| 37 |
+
----------
|
| 38 |
+
ax : matplotlib.axes._subplots.AxesSubplot
|
| 39 |
+
The matplotlib Axes object (with Cartopy projection) to draw the map background on.
|
| 40 |
+
|
| 41 |
+
mode : str, optional
|
| 42 |
+
The basemap mode to use:
|
| 43 |
+
- "stock": Default stock image from Cartopy.
|
| 44 |
+
- "contextily": Web tile background (CartoDB Voyager), with caching.
|
| 45 |
+
- "basemap": High-resolution shaded relief using Basemap, with caching.
|
| 46 |
+
Default is "basemap".
|
| 47 |
+
|
| 48 |
+
zoom : int, optional
|
| 49 |
+
Tile zoom level (only for "contextily"). Higher = more detail. Default is 7.
|
| 50 |
+
|
| 51 |
+
Notes
|
| 52 |
+
-----
|
| 53 |
+
- Uses high resolution for Basemap (resolution='h') and saves figure at 300 DPI.
|
| 54 |
+
- Cached images are reused using extent-based hashing to avoid re-rendering.
|
| 55 |
+
- Basemap is deprecated; Cartopy with web tiles is recommended for new projects.
|
| 56 |
+
"""
|
| 57 |
+
try:
|
| 58 |
+
if mode == "stock":
|
| 59 |
+
ax.stock_img()
|
| 60 |
+
|
| 61 |
+
elif mode == "contextily":
|
| 62 |
+
extent = ax.get_extent(crs=ccrs.PlateCarree())
|
| 63 |
+
ax.set_extent(extent, crs=ccrs.PlateCarree())
|
| 64 |
+
ctx.add_basemap(
|
| 65 |
+
ax,
|
| 66 |
+
crs=ccrs.PlateCarree(),
|
| 67 |
+
source=ctx.providers.CartoDB.Voyager,
|
| 68 |
+
zoom=zoom
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
elif mode == "basemap":
|
| 72 |
+
extent = ax.get_extent(crs=ccrs.PlateCarree())
|
| 73 |
+
extent_str = f"{extent[0]:.4f}_{extent[1]:.4f}_{extent[2]:.4f}_{extent[3]:.4f}"
|
| 74 |
+
cache_key = hashlib.md5(extent_str.encode()).hexdigest()
|
| 75 |
+
cache_file = os.path.join(BASEMAP_TILE_CACHE_DIR, f"{cache_key}_highres.png")
|
| 76 |
+
|
| 77 |
+
if os.path.exists(cache_file):
|
| 78 |
+
img = Image.open(cache_file)
|
| 79 |
+
ax.imshow(img, extent=extent, transform=ccrs.PlateCarree())
|
| 80 |
+
else:
|
| 81 |
+
temp_fig, temp_ax = plt.subplots(figsize=(12, 9),
|
| 82 |
+
subplot_kw={'projection': ccrs.PlateCarree()})
|
| 83 |
+
temp_ax.set_extent(extent, crs=ccrs.PlateCarree())
|
| 84 |
+
|
| 85 |
+
m = Basemap(projection='cyl',
|
| 86 |
+
llcrnrlon=extent[0], urcrnrlon=extent[1],
|
| 87 |
+
llcrnrlat=extent[2], urcrnrlat=extent[3],
|
| 88 |
+
resolution='f', ax=temp_ax)
|
| 89 |
+
m.shadedrelief()
|
| 90 |
+
|
| 91 |
+
temp_fig.savefig(cache_file, dpi=300, bbox_inches='tight', pad_inches=0)
|
| 92 |
+
plt.close(temp_fig)
|
| 93 |
+
|
| 94 |
+
img = Image.open(cache_file)
|
| 95 |
+
ax.imshow(img, extent=extent, transform=ccrs.PlateCarree())
|
| 96 |
+
|
| 97 |
+
else:
|
| 98 |
+
raise ValueError(f"Unsupported basemap mode: {mode}")
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"[Relief Error - {mode} mode]:", e)
|
| 102 |
+
ax.add_feature(cfeature.LAND)
|
| 103 |
+
ax.add_feature(cfeature.OCEAN)
|
ash_animator/converter.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
# Re-defining the integrated class first
|
| 4 |
+
import os
|
| 5 |
+
import re
|
| 6 |
+
import zipfile
|
| 7 |
+
import numpy as np
|
| 8 |
+
import xarray as xr
|
| 9 |
+
from typing import List, Tuple
|
| 10 |
+
import shutil
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
import tempfile # Added for safe temp directory usage
|
| 14 |
+
|
| 15 |
+
class NAMEDataProcessor:
|
| 16 |
+
def __init__(self, output_root: str = None):
|
| 17 |
+
if output_root is None:
|
| 18 |
+
output_root = os.path.join(tempfile.gettempdir(), "name_outputs")
|
| 19 |
+
self.output_root = output_root
|
| 20 |
+
self.output_3d = os.path.join(self.output_root, "3D")
|
| 21 |
+
self.output_horizontal = os.path.join(self.output_root, "horizontal")
|
| 22 |
+
os.makedirs(self.output_3d, exist_ok=True)
|
| 23 |
+
os.makedirs(self.output_horizontal, exist_ok=True)
|
| 24 |
+
self.output_root = output_root
|
| 25 |
+
self.output_3d = os.path.join(self.output_root, "3D")
|
| 26 |
+
self.output_horizontal = os.path.join(self.output_root, "horizontal")
|
| 27 |
+
os.makedirs(self.output_3d, exist_ok=True)
|
| 28 |
+
os.makedirs(self.output_horizontal, exist_ok=True)
|
| 29 |
+
|
| 30 |
+
def _sanitize_key(self, key: str) -> str:
|
| 31 |
+
key = re.sub(r'\W+', '_', key)
|
| 32 |
+
if not key[0].isalpha():
|
| 33 |
+
key = f"attr_{key}"
|
| 34 |
+
return key
|
| 35 |
+
|
| 36 |
+
def _parse_metadata(self, lines: List[str]) -> dict:
|
| 37 |
+
metadata = {}
|
| 38 |
+
for line in lines:
|
| 39 |
+
if ":" in line:
|
| 40 |
+
key, value = line.split(":", 1)
|
| 41 |
+
clean_key = self._sanitize_key(key.strip().lower())
|
| 42 |
+
metadata[clean_key] = value.strip()
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
metadata.update({
|
| 46 |
+
"x_origin": float(metadata["x_grid_origin"]),
|
| 47 |
+
"y_origin": float(metadata["y_grid_origin"]),
|
| 48 |
+
"x_size": int(metadata["x_grid_size"]),
|
| 49 |
+
"y_size": int(metadata["y_grid_size"]),
|
| 50 |
+
"x_res": float(metadata["x_grid_resolution"]),
|
| 51 |
+
"y_res": float(metadata["y_grid_resolution"]),
|
| 52 |
+
})
|
| 53 |
+
except KeyError as e:
|
| 54 |
+
raise ValueError(f"Missing required metadata field: {e}")
|
| 55 |
+
except ValueError as e:
|
| 56 |
+
raise ValueError(f"Invalid value in metadata: {e}")
|
| 57 |
+
|
| 58 |
+
if metadata["x_res"] == 0 or metadata["y_res"] == 0:
|
| 59 |
+
raise ZeroDivisionError("Grid resolution cannot be zero.")
|
| 60 |
+
|
| 61 |
+
return metadata
|
| 62 |
+
|
| 63 |
+
def _get_data_lines(self, lines: List[str]) -> List[str]:
|
| 64 |
+
idx = next(i for i, l in enumerate(lines) if l.strip() == "Fields:")
|
| 65 |
+
return lines[idx + 1:]
|
| 66 |
+
|
| 67 |
+
def _is_horizontal_file(self, filename: str) -> bool:
|
| 68 |
+
return "HorizontalField" in filename
|
| 69 |
+
|
| 70 |
+
def _convert_horizontal(self, filepath: str, output_filename: str) -> str:
|
| 71 |
+
with open(filepath, 'r') as f:
|
| 72 |
+
lines = f.readlines()
|
| 73 |
+
|
| 74 |
+
meta = self._parse_metadata(lines)
|
| 75 |
+
data_lines = self._get_data_lines(lines)
|
| 76 |
+
|
| 77 |
+
lons = np.round(np.arange(meta["x_origin"], meta["x_origin"] + meta["x_size"] * meta["x_res"], meta["x_res"]), 6)
|
| 78 |
+
lats = np.round(np.arange(meta["y_origin"], meta["y_origin"] + meta["y_size"] * meta["y_res"], meta["y_res"]), 6)
|
| 79 |
+
|
| 80 |
+
air_conc = np.zeros((meta["y_size"], meta["x_size"]), dtype=np.float32)
|
| 81 |
+
dry_depo = np.zeros((meta["y_size"], meta["x_size"]), dtype=np.float32)
|
| 82 |
+
wet_depo = np.zeros((meta["y_size"], meta["x_size"]), dtype=np.float32)
|
| 83 |
+
|
| 84 |
+
for line in data_lines:
|
| 85 |
+
parts = [p.strip().strip(',') for p in line.strip().split(',') if p.strip()]
|
| 86 |
+
if len(parts) >= 7 and parts[0].isdigit() and parts[1].isdigit():
|
| 87 |
+
try:
|
| 88 |
+
x = int(parts[0]) - 1
|
| 89 |
+
y = int(parts[1]) - 1
|
| 90 |
+
air_val = float(parts[4])
|
| 91 |
+
dry_val = float(parts[5])
|
| 92 |
+
wet_val = float(parts[6])
|
| 93 |
+
if 0 <= x < meta["x_size"] and 0 <= y < meta["y_size"]:
|
| 94 |
+
air_conc[y, x] = air_val
|
| 95 |
+
dry_depo[y, x] = dry_val
|
| 96 |
+
wet_depo[y, x] = wet_val
|
| 97 |
+
except Exception:
|
| 98 |
+
continue
|
| 99 |
+
|
| 100 |
+
ds = xr.Dataset(
|
| 101 |
+
{
|
| 102 |
+
"air_concentration": (['latitude', 'longitude'], air_conc),
|
| 103 |
+
"dry_deposition_rate": (['latitude', 'longitude'], dry_depo),
|
| 104 |
+
"wet_deposition_rate": (['latitude', 'longitude'], wet_depo)
|
| 105 |
+
},
|
| 106 |
+
coords={
|
| 107 |
+
"latitude": lats,
|
| 108 |
+
"longitude": lons
|
| 109 |
+
},
|
| 110 |
+
attrs={
|
| 111 |
+
"title": "Volcanic Ash Horizontal Output (Multiple Fields)",
|
| 112 |
+
"source": "NAME model output processed to NetCDF (horizontal multi-field)",
|
| 113 |
+
**{k: str(v) for k, v in meta.items()}
|
| 114 |
+
}
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
ds["air_concentration"].attrs.update({
|
| 118 |
+
"units": "g/m^3",
|
| 119 |
+
"long_name": "Boundary Layer Average Air Concentration"
|
| 120 |
+
})
|
| 121 |
+
ds["dry_deposition_rate"].attrs.update({
|
| 122 |
+
"units": "g/m^2/s",
|
| 123 |
+
"long_name": "Dry Deposition Rate"
|
| 124 |
+
})
|
| 125 |
+
ds["wet_deposition_rate"].attrs.update({
|
| 126 |
+
"units": "g/m^2/s",
|
| 127 |
+
"long_name": "Wet Deposition Rate"
|
| 128 |
+
})
|
| 129 |
+
ds["latitude"].attrs["units"] = "degrees_north"
|
| 130 |
+
ds["longitude"].attrs["units"] = "degrees_east"
|
| 131 |
+
|
| 132 |
+
out_path = os.path.join(self.output_horizontal, output_filename)
|
| 133 |
+
ds.to_netcdf(out_path, engine="netcdf4")
|
| 134 |
+
|
| 135 |
+
return out_path
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def _convert_3d_group(self, group: List[Tuple[int, str]], output_filename: str) -> str:
|
| 139 |
+
first_file_path = group[0][1]
|
| 140 |
+
with open(first_file_path, 'r') as f:
|
| 141 |
+
lines = f.readlines()
|
| 142 |
+
meta = self._parse_metadata(lines)
|
| 143 |
+
|
| 144 |
+
lons = np.round(np.arange(meta["x_origin"], meta["x_origin"] + meta["x_size"] * meta["x_res"], meta["x_res"]), 6)
|
| 145 |
+
lats = np.round(np.arange(meta["y_origin"], meta["y_origin"] + meta["y_size"] * meta["y_res"], meta["y_res"]), 6)
|
| 146 |
+
|
| 147 |
+
z_levels = []
|
| 148 |
+
z_coords = []
|
| 149 |
+
|
| 150 |
+
for z_idx, filepath in group:
|
| 151 |
+
with open(filepath, 'r') as f:
|
| 152 |
+
lines = f.readlines()
|
| 153 |
+
data_lines = self._get_data_lines(lines)
|
| 154 |
+
grid = np.zeros((meta["y_size"], meta["x_size"]), dtype=np.float32)
|
| 155 |
+
|
| 156 |
+
for line in data_lines:
|
| 157 |
+
parts = [p.strip().strip(',') for p in line.strip().split(',') if p.strip()]
|
| 158 |
+
if len(parts) >= 5 and parts[0].isdigit() and parts[1].isdigit():
|
| 159 |
+
try:
|
| 160 |
+
x = int(parts[0]) - 1
|
| 161 |
+
y = int(parts[1]) - 1
|
| 162 |
+
val = float(parts[4])
|
| 163 |
+
if 0 <= x < meta["x_size"] and 0 <= y < meta["y_size"]:
|
| 164 |
+
grid[y, x] = val
|
| 165 |
+
except Exception:
|
| 166 |
+
continue
|
| 167 |
+
z_levels.append(grid)
|
| 168 |
+
z_coords.append(z_idx)
|
| 169 |
+
|
| 170 |
+
z_cube = np.stack(z_levels, axis=0)
|
| 171 |
+
ds = xr.Dataset(
|
| 172 |
+
{
|
| 173 |
+
"ash_concentration": (['altitude', 'latitude', 'longitude'], z_cube)
|
| 174 |
+
},
|
| 175 |
+
coords={
|
| 176 |
+
"altitude": np.array(z_coords, dtype=np.float32),
|
| 177 |
+
"latitude": lats,
|
| 178 |
+
"longitude": lons
|
| 179 |
+
},
|
| 180 |
+
attrs={
|
| 181 |
+
"title": "Volcanic Ash Concentration (3D)",
|
| 182 |
+
"source": "NAME model output processed to NetCDF (3D fields)",
|
| 183 |
+
**{k: str(v) for k, v in meta.items()}
|
| 184 |
+
}
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
out_path = os.path.join(self.output_3d, output_filename)
|
| 188 |
+
|
| 189 |
+
# 🔥 Check if file exists, delete it first
|
| 190 |
+
# if os.path.exists(out_path):
|
| 191 |
+
# os.remove(out_path)
|
| 192 |
+
|
| 193 |
+
# 🔥 Save NetCDF safely using netCDF4
|
| 194 |
+
ds.to_netcdf(out_path, engine="netcdf4")
|
| 195 |
+
|
| 196 |
+
return out_path
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def batch_process_zip(self, zip_path: str) -> List[str]:
|
| 200 |
+
extract_dir = os.path.join(tempfile.gettempdir(), "unzipped_name_extract")
|
| 201 |
+
|
| 202 |
+
os.makedirs(extract_dir, exist_ok=True)
|
| 203 |
+
|
| 204 |
+
###
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
# Function to empty folder contents
|
| 208 |
+
def empty_folder(folder_path):
|
| 209 |
+
import os
|
| 210 |
+
import glob
|
| 211 |
+
files = glob.glob(os.path.join(folder_path, '*'))
|
| 212 |
+
for f in files:
|
| 213 |
+
try:
|
| 214 |
+
os.remove(f)
|
| 215 |
+
except IsADirectoryError:
|
| 216 |
+
shutil.rmtree(f)
|
| 217 |
+
|
| 218 |
+
# 🛠 Clear cached open files and garbage collect before deleting
|
| 219 |
+
|
| 220 |
+
# 🔥 Empty previous outputs, do not delete folders
|
| 221 |
+
if os.path.exists(self.output_3d):
|
| 222 |
+
empty_folder(self.output_3d)
|
| 223 |
+
else:
|
| 224 |
+
os.makedirs(self.output_3d, exist_ok=True)
|
| 225 |
+
|
| 226 |
+
# if os.path.exists(self.output_horizontal):
|
| 227 |
+
# empty_folder(self.output_horizontal)
|
| 228 |
+
# else:
|
| 229 |
+
# os.makedirs(self.output_horizontal, exist_ok=True)
|
| 230 |
+
|
| 231 |
+
# if os.path.exists(extract_dir):
|
| 232 |
+
# shutil.rmtree(extract_dir)
|
| 233 |
+
# os.makedirs(extract_dir, exist_ok=True)
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
#####
|
| 240 |
+
|
| 241 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 242 |
+
zip_ref.extractall(extract_dir)
|
| 243 |
+
|
| 244 |
+
txt_files = []
|
| 245 |
+
for root, _, files in os.walk(extract_dir):
|
| 246 |
+
for file in files:
|
| 247 |
+
if file.endswith(".txt"):
|
| 248 |
+
txt_files.append(os.path.join(root, file))
|
| 249 |
+
|
| 250 |
+
horizontal_files = []
|
| 251 |
+
grouped_3d = {}
|
| 252 |
+
|
| 253 |
+
pattern = re.compile(r"_T(\d+)_.*_Z(\d+)\.txt$")
|
| 254 |
+
|
| 255 |
+
for f in txt_files:
|
| 256 |
+
if self._is_horizontal_file(f):
|
| 257 |
+
horizontal_files.append(f)
|
| 258 |
+
else:
|
| 259 |
+
match = pattern.search(f)
|
| 260 |
+
if match:
|
| 261 |
+
t = int(match.group(1))
|
| 262 |
+
z = int(match.group(2))
|
| 263 |
+
grouped_3d.setdefault(t, []).append((z, f))
|
| 264 |
+
|
| 265 |
+
nc_files = []
|
| 266 |
+
|
| 267 |
+
# Process horizontal
|
| 268 |
+
for f in sorted(horizontal_files):
|
| 269 |
+
base_name = os.path.splitext(os.path.basename(f))[0]
|
| 270 |
+
out_nc = self._convert_horizontal(f, f"{base_name}.nc")
|
| 271 |
+
nc_files.append(out_nc)
|
| 272 |
+
|
| 273 |
+
# Process 3D
|
| 274 |
+
for t_key in sorted(grouped_3d):
|
| 275 |
+
group = sorted(grouped_3d[t_key])
|
| 276 |
+
out_nc = self._convert_3d_group(group, f"T{t_key}.nc")
|
| 277 |
+
nc_files.append(out_nc)
|
| 278 |
+
|
| 279 |
+
return nc_files
|
ash_animator/export.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import matplotlib.ticker as mticker
|
| 6 |
+
import cartopy.crs as ccrs
|
| 7 |
+
import cartopy.feature as cfeature
|
| 8 |
+
from .interpolation import interpolate_grid
|
| 9 |
+
from .basemaps import draw_etopo_basemap
|
| 10 |
+
|
| 11 |
+
def export_frames_as_jpgs(animator, output_folder: str, include_metadata: bool = True):
|
| 12 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
meta = animator.datasets[0].attrs
|
| 15 |
+
legend_text = (
|
| 16 |
+
f"Run name: {meta.get('run_name', 'N/A')}\n"
|
| 17 |
+
f"Run time: {meta.get('run_time', 'N/A')}\n"
|
| 18 |
+
f"Met data: {meta.get('met_data', 'N/A')}\n"
|
| 19 |
+
f"Start of release: {meta.get('start_of_release', 'N/A')}\n"
|
| 20 |
+
f"End of release: {meta.get('end_of_release', 'N/A')}\n"
|
| 21 |
+
f"Source strength: {meta.get('source_strength', 'N/A')} g / s\n"
|
| 22 |
+
f"Release location: {meta.get('release_location', 'N/A')}\n"
|
| 23 |
+
f"Release height: {meta.get('release_height', 'N/A')} m asl\n"
|
| 24 |
+
f"Run duration: {meta.get('run_duration', 'N/A')}"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
for z_index, z_val in enumerate(animator.levels):
|
| 28 |
+
z_dir = os.path.join(output_folder, f"ash_T1-Tn_Z{z_index+1}")
|
| 29 |
+
os.makedirs(z_dir, exist_ok=True)
|
| 30 |
+
|
| 31 |
+
valid_mask = np.stack([
|
| 32 |
+
ds['ash_concentration'].values[z_index] for ds in animator.datasets
|
| 33 |
+
]).max(axis=0) > 0
|
| 34 |
+
y_idx, x_idx = np.where(valid_mask)
|
| 35 |
+
|
| 36 |
+
if y_idx.size == 0 or x_idx.size == 0:
|
| 37 |
+
print(f"Z level {z_val} km has no valid data. Skipping...")
|
| 38 |
+
continue
|
| 39 |
+
|
| 40 |
+
y_min, y_max = y_idx.min(), y_idx.max()
|
| 41 |
+
x_min, x_max = x_idx.min(), x_idx.max()
|
| 42 |
+
|
| 43 |
+
for t in range(len(animator.datasets)):
|
| 44 |
+
data = animator.datasets[t]['ash_concentration'].values[z_index]
|
| 45 |
+
interp = interpolate_grid(data, animator.lon_grid, animator.lat_grid)
|
| 46 |
+
if np.isfinite(interp).sum() == 0:
|
| 47 |
+
continue
|
| 48 |
+
|
| 49 |
+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(18, 8), subplot_kw={'projection': ccrs.PlateCarree()})
|
| 50 |
+
valid_vals = interp[np.isfinite(interp)]
|
| 51 |
+
min_val = np.nanmin(valid_vals)
|
| 52 |
+
max_val = np.nanmax(valid_vals)
|
| 53 |
+
log_cutoff = 1e-3
|
| 54 |
+
log_ratio = max_val / (min_val + 1e-6)
|
| 55 |
+
use_log = min_val > log_cutoff and log_ratio > 100
|
| 56 |
+
|
| 57 |
+
levels = np.logspace(np.log10(log_cutoff), np.log10(max_val), 20) if use_log else np.linspace(0, max_val, 20)
|
| 58 |
+
data_for_plot = np.where(interp > log_cutoff, interp, np.nan) if use_log else interp
|
| 59 |
+
scale_label = "Hybrid Log" if use_log else "Linear"
|
| 60 |
+
|
| 61 |
+
# Plot full
|
| 62 |
+
c1 = ax1.contourf(animator.lons, animator.lats, data_for_plot, levels=levels,
|
| 63 |
+
cmap="rainbow", alpha=0.6, transform=ccrs.PlateCarree())
|
| 64 |
+
draw_etopo_basemap(ax1, mode='stock')
|
| 65 |
+
ax1.set_extent([animator.lons.min(), animator.lons.max(), animator.lats.min(), animator.lats.max()])
|
| 66 |
+
ax1.set_title(f"T{t+1} | Alt: {z_val} km (Full - {scale_label})")
|
| 67 |
+
ax1.coastlines(); ax1.add_feature(cfeature.BORDERS)
|
| 68 |
+
ax1.add_feature(cfeature.LAND); ax1.add_feature(cfeature.OCEAN)
|
| 69 |
+
|
| 70 |
+
# Zoom region
|
| 71 |
+
buffer_y = int((y_max - y_min) * 0.5)
|
| 72 |
+
buffer_x = int((x_max - x_min) * 0.5)
|
| 73 |
+
|
| 74 |
+
y_start = max(0, y_min - buffer_y)
|
| 75 |
+
y_end = min(data_for_plot.shape[0], y_max + buffer_y + 1)
|
| 76 |
+
x_start = max(0, x_min - buffer_x)
|
| 77 |
+
x_end = min(data_for_plot.shape[1], x_max + buffer_x + 1)
|
| 78 |
+
|
| 79 |
+
zoom = data_for_plot[y_start:y_end, x_start:x_end]
|
| 80 |
+
lon_zoom = animator.lons[x_start:x_end]
|
| 81 |
+
lat_zoom = animator.lats[y_start:y_end]
|
| 82 |
+
|
| 83 |
+
c2 = ax2.contourf(lon_zoom, lat_zoom, zoom, levels=levels,
|
| 84 |
+
cmap="rainbow", alpha=0.6, transform=ccrs.PlateCarree())
|
| 85 |
+
draw_etopo_basemap(ax2, mode='stock')
|
| 86 |
+
ax2.set_extent([lon_zoom.min(), lon_zoom.max(), lat_zoom.min(), lat_zoom.max()])
|
| 87 |
+
ax2.set_title(f"T{t+1} | Alt: {z_val} km (Zoom - {scale_label})")
|
| 88 |
+
ax2.coastlines(); ax2.add_feature(cfeature.BORDERS)
|
| 89 |
+
ax2.add_feature(cfeature.LAND); ax2.add_feature(cfeature.OCEAN)
|
| 90 |
+
|
| 91 |
+
for ax in [ax1, ax2]:
|
| 92 |
+
ax.text(0.01, 0.98, f"Time step T{t+1}", transform=ax.transAxes,
|
| 93 |
+
fontsize=9, color='white', va='top', ha='left',
|
| 94 |
+
bbox=dict(facecolor='black', alpha=0.4, boxstyle='round'))
|
| 95 |
+
|
| 96 |
+
if include_metadata:
|
| 97 |
+
for ax in [ax1, ax2]:
|
| 98 |
+
ax.text(0.01, 0.01,
|
| 99 |
+
f"Source: NAME model\nRes: {animator.x_res:.2f}°\n{meta.get('run_name', 'N/A')}",
|
| 100 |
+
transform=ax.transAxes, fontsize=8, color='white',
|
| 101 |
+
bbox=dict(facecolor='black', alpha=0.5))
|
| 102 |
+
ax1.annotate(legend_text, xy=(0.75, 0.99), xycoords='axes fraction',
|
| 103 |
+
fontsize=8, ha='left', va='top',
|
| 104 |
+
bbox=dict(boxstyle="round", facecolor="white", edgecolor="gray"),
|
| 105 |
+
annotation_clip=False)
|
| 106 |
+
|
| 107 |
+
cbar = fig.colorbar(c1, ax=[ax1, ax2], orientation='vertical', shrink=0.75, pad=0.03)
|
| 108 |
+
cbar.set_label("Ash concentration (g/m³)")
|
| 109 |
+
formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 110 |
+
cbar.ax.yaxis.set_major_formatter(formatter)
|
| 111 |
+
|
| 112 |
+
if use_log:
|
| 113 |
+
cbar.ax.text(1.1, 1.02, "log scale", transform=cbar.ax.transAxes,
|
| 114 |
+
fontsize=9, color='gray', rotation=90, ha='left', va='bottom')
|
| 115 |
+
|
| 116 |
+
frame_path = os.path.join(z_dir, f"frame_{t+1:04d}.jpg")
|
| 117 |
+
plt.savefig(frame_path, dpi=150, bbox_inches='tight')
|
| 118 |
+
plt.close(fig)
|
| 119 |
+
print(f"Saved {frame_path}")
|
ash_animator/interpolation.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import numpy as np
|
| 3 |
+
from scipy.interpolate import griddata
|
| 4 |
+
|
| 5 |
+
def interpolate_grid(data, lon_grid, lat_grid):
|
| 6 |
+
data = np.where(data < 0, np.nan, data)
|
| 7 |
+
mask = data > 0
|
| 8 |
+
if np.count_nonzero(mask) < 10:
|
| 9 |
+
return np.full_like(data, np.nan)
|
| 10 |
+
|
| 11 |
+
points = np.column_stack((lon_grid[mask], lat_grid[mask]))
|
| 12 |
+
values = data[mask]
|
| 13 |
+
grid_z = griddata(points, values, (lon_grid, lat_grid), method='cubic')
|
| 14 |
+
return np.where(grid_z < 0, 0, grid_z)
|
ash_animator/plot_3dfield_data.py
ADDED
|
@@ -0,0 +1,472 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import matplotlib.animation as animation
|
| 5 |
+
import matplotlib.ticker as mticker
|
| 6 |
+
import cartopy.crs as ccrs
|
| 7 |
+
import cartopy.feature as cfeature
|
| 8 |
+
import cartopy.io.shapereader as shpreader
|
| 9 |
+
from adjustText import adjust_text
|
| 10 |
+
from .interpolation import interpolate_grid
|
| 11 |
+
from .basemaps import draw_etopo_basemap
|
| 12 |
+
import imageio.v2 as imageio
|
| 13 |
+
import shutil
|
| 14 |
+
import tempfile
|
| 15 |
+
|
| 16 |
+
class Plot_3DField_Data:
|
| 17 |
+
|
| 18 |
+
"""
|
| 19 |
+
A class for visualizing 3D spatiotemporal field data (e.g., ash concentration) across time and altitude levels.
|
| 20 |
+
|
| 21 |
+
This class uses matplotlib and cartopy to create:
|
| 22 |
+
- Animated GIFs of spatial fields at given altitudes
|
| 23 |
+
- Vertical profile animations over time
|
| 24 |
+
- Exported static frames with metadata annotations and zoomed views
|
| 25 |
+
|
| 26 |
+
Parameters
|
| 27 |
+
----------
|
| 28 |
+
animator : object
|
| 29 |
+
A container holding the dataset, including:
|
| 30 |
+
- datasets: list of xarray-like DataArrays with 'ash_concentration'
|
| 31 |
+
- lons, lats: 1D longitude and latitude arrays
|
| 32 |
+
- lat_grid, lon_grid: 2D grid arrays for spatial mapping
|
| 33 |
+
- levels: 1D array of vertical altitude levels (e.g., in km)
|
| 34 |
+
output_dir : str
|
| 35 |
+
Base directory for saving all outputs. Defaults to "plots".
|
| 36 |
+
cmap : str
|
| 37 |
+
Matplotlib colormap name. Defaults to "rainbow".
|
| 38 |
+
fps : int
|
| 39 |
+
Frames per second for GIFs. Defaults to 2.
|
| 40 |
+
include_metadata : bool
|
| 41 |
+
Whether to annotate each figure with simulation metadata. Defaults to True.
|
| 42 |
+
threshold : float
|
| 43 |
+
Value threshold (e.g., in g/m³) to highlight exceedances. Defaults to 0.1.
|
| 44 |
+
zoom_width_deg : float
|
| 45 |
+
Width of the zoomed-in region in degrees. Defaults to 6.0.
|
| 46 |
+
zoom_height_deg : float
|
| 47 |
+
Height of the zoomed-in region in degrees. Defaults to 6.0.
|
| 48 |
+
zoom_level : int
|
| 49 |
+
Zoom level passed to basemap drawing. Defaults to 7.
|
| 50 |
+
basemap_type : str
|
| 51 |
+
Type of basemap to draw (passed to draw_etopo_basemap). Defaults to "basemap".
|
| 52 |
+
|
| 53 |
+
Methods
|
| 54 |
+
-------
|
| 55 |
+
plot_single_z_level(z_km, filename)
|
| 56 |
+
Generate animation over time at a specific altitude level.
|
| 57 |
+
|
| 58 |
+
plot_vertical_profile_at_time(t_index, filename=None)
|
| 59 |
+
Generate vertical profile GIF for a single timestep.
|
| 60 |
+
|
| 61 |
+
animate_altitude(t_index, output_path)
|
| 62 |
+
Animate altitude slices for one timestep.
|
| 63 |
+
|
| 64 |
+
animate_all_altitude_profiles(output_folder='altitude_profiles')
|
| 65 |
+
Generate vertical animations for all time steps.
|
| 66 |
+
|
| 67 |
+
export_frames_as_jpgs(include_metadata=True)
|
| 68 |
+
Export individual frames as static `.jpg` images with annotations.
|
| 69 |
+
"""
|
| 70 |
+
def __init__(self, animator, output_dir="plots", cmap="rainbow", fps=2,
|
| 71 |
+
include_metadata=True, threshold=0.1,
|
| 72 |
+
zoom_width_deg=6.0, zoom_height_deg=6.0, zoom_level=7, basemap_type="basemap"):
|
| 73 |
+
self.animator = animator
|
| 74 |
+
|
| 75 |
+
self.output_dir = os.path.abspath(
|
| 76 |
+
os.path.join(
|
| 77 |
+
os.environ.get("NAME_OUTPUT_DIR", tempfile.gettempdir()),
|
| 78 |
+
output_dir
|
| 79 |
+
)
|
| 80 |
+
)
|
| 81 |
+
os.makedirs(self.output_dir, exist_ok=True)
|
| 82 |
+
self.cmap = cmap
|
| 83 |
+
self.fps = fps
|
| 84 |
+
self.include_metadata = include_metadata
|
| 85 |
+
self.threshold = threshold
|
| 86 |
+
self.zoom_width = zoom_width_deg
|
| 87 |
+
self.zoom_height = zoom_height_deg
|
| 88 |
+
shp = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries')
|
| 89 |
+
self.country_geoms = list(shpreader.Reader(shp).records())
|
| 90 |
+
self.zoom_level=zoom_level
|
| 91 |
+
self.basemap_type=basemap_type
|
| 92 |
+
|
| 93 |
+
#############3
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# Load shapefile once
|
| 97 |
+
countries_shp = shpreader.natural_earth(
|
| 98 |
+
resolution='110m',
|
| 99 |
+
category='cultural',
|
| 100 |
+
name='admin_0_countries'
|
| 101 |
+
)
|
| 102 |
+
self.country_geoms = list(shpreader.Reader(countries_shp).records())
|
| 103 |
+
|
| 104 |
+
# Cache extent bounds
|
| 105 |
+
self.lon_min = np.min(self.animator.lons)
|
| 106 |
+
self.lon_max = np.max(self.animator.lons)
|
| 107 |
+
self.lat_min = np.min(self.animator.lats)
|
| 108 |
+
self.lat_max = np.max(self.animator.lats)
|
| 109 |
+
|
| 110 |
+
#####################3
|
| 111 |
+
|
| 112 |
+
def _make_dirs(self, path):
|
| 113 |
+
path = os.path.abspath(os.path.join(os.getcwd(), os.path.dirname(path)))
|
| 114 |
+
os.makedirs(path, exist_ok=True)
|
| 115 |
+
|
| 116 |
+
def _get_zoom_indices(self, center_lat, center_lon):
|
| 117 |
+
lon_min = center_lon - self.zoom_width / 2
|
| 118 |
+
lon_max = center_lon + self.zoom_width / 2
|
| 119 |
+
lat_min = center_lat - self.zoom_height / 2
|
| 120 |
+
lat_max = center_lat + self.zoom_height / 2
|
| 121 |
+
lat_idx = np.where((self.animator.lats >= lat_min) & (self.animator.lats <= lat_max))[0]
|
| 122 |
+
lon_idx = np.where((self.animator.lons >= lon_min) & (self.animator.lons <= lon_max))[0]
|
| 123 |
+
return lat_idx, lon_idx, lon_min, lon_max, lat_min, lat_max
|
| 124 |
+
|
| 125 |
+
def _get_max_concentration_location(self):
|
| 126 |
+
max_conc = -np.inf
|
| 127 |
+
center_lat = center_lon = None
|
| 128 |
+
for ds in self.animator.datasets:
|
| 129 |
+
for z in range(len(self.animator.levels)):
|
| 130 |
+
data = ds['ash_concentration'].values[z]
|
| 131 |
+
if np.max(data) > max_conc:
|
| 132 |
+
max_conc = np.max(data)
|
| 133 |
+
max_idx = np.unravel_index(np.argmax(data), data.shape)
|
| 134 |
+
center_lat = self.animator.lat_grid[max_idx]
|
| 135 |
+
center_lon = self.animator.lon_grid[max_idx]
|
| 136 |
+
return center_lat, center_lon
|
| 137 |
+
|
| 138 |
+
def _add_country_labels(self, ax, extent):
|
| 139 |
+
proj = ccrs.PlateCarree()
|
| 140 |
+
texts = []
|
| 141 |
+
for country in self.country_geoms:
|
| 142 |
+
name = country.attributes['NAME_LONG']
|
| 143 |
+
geom = country.geometry
|
| 144 |
+
try:
|
| 145 |
+
lon, lat = geom.centroid.x, geom.centroid.y
|
| 146 |
+
if extent[0] <= lon <= extent[1] and extent[2] <= lat <= extent[3]:
|
| 147 |
+
text = ax.text(lon, lat, name, fontsize=6, transform=proj,
|
| 148 |
+
ha='center', va='center', color='white',
|
| 149 |
+
bbox=dict(facecolor='black', alpha=0.5, linewidth=0))
|
| 150 |
+
texts.append(text)
|
| 151 |
+
except:
|
| 152 |
+
continue
|
| 153 |
+
adjust_text(texts, ax=ax, only_move={'points': 'y', 'text': 'y'},
|
| 154 |
+
arrowprops=dict(arrowstyle="->", color='white', lw=0.5))
|
| 155 |
+
|
| 156 |
+
def _plot_frame(self, ax, data, lons, lats, title, levels, scale_label, proj):
|
| 157 |
+
draw_etopo_basemap(ax, mode=self.basemap_type, zoom=self.zoom_level)
|
| 158 |
+
c = ax.contourf(lons, lats, data, levels=levels, cmap=self.cmap, alpha=0.6, transform=proj)
|
| 159 |
+
ax.contour(lons, lats, data, levels=levels, colors='black', linewidths=0.5, transform=proj)
|
| 160 |
+
ax.set_title(title)
|
| 161 |
+
ax.set_extent([lons.min(), lons.max(), lats.min(), lats.max()])
|
| 162 |
+
ax.coastlines()
|
| 163 |
+
ax.add_feature(cfeature.BORDERS, linestyle=':')
|
| 164 |
+
ax.add_feature(cfeature.LAND)
|
| 165 |
+
ax.add_feature(cfeature.OCEAN)
|
| 166 |
+
return c
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
# metadata placement function and usage
|
| 171 |
+
|
| 172 |
+
def _draw_metadata_sidebar(self, fig, meta_dict):
|
| 173 |
+
lines = [
|
| 174 |
+
f"Run name: {meta_dict.get('run_name', 'N/A')}",
|
| 175 |
+
f"Run time: {meta_dict.get('run_time', 'N/A')}",
|
| 176 |
+
f"Met data: {meta_dict.get('met_data', 'N/A')}",
|
| 177 |
+
f"Start release: {meta_dict.get('start_of_release', 'N/A')}",
|
| 178 |
+
f"End release: {meta_dict.get('end_of_release', 'N/A')}",
|
| 179 |
+
f"Source strength: {meta_dict.get('source_strength', 'N/A')} g/s",
|
| 180 |
+
f"Release loc: {meta_dict.get('release_location', 'N/A')}",
|
| 181 |
+
f"Release height: {meta_dict.get('release_height', 'N/A')} m asl",
|
| 182 |
+
f"Run duration: {meta_dict.get('run_duration', 'N/A')}"
|
| 183 |
+
]
|
| 184 |
+
full_text = "\n".join(lines) # ✅ actual newlines
|
| 185 |
+
fig.text(0.1, 0.095, full_text, va='center', ha='left',
|
| 186 |
+
fontsize=9, family='monospace', color='black',
|
| 187 |
+
bbox=dict(facecolor='white', alpha=0.8, edgecolor='gray'))
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
def plot_single_z_level(self, z_km, filename="z_level.gif"):
|
| 192 |
+
|
| 193 |
+
if z_km not in self.animator.levels:
|
| 194 |
+
print(f"Z level {z_km} km not found.")
|
| 195 |
+
return
|
| 196 |
+
z_index = np.where(self.animator.levels == z_km)[0][0]
|
| 197 |
+
output_path = os.path.join(self.output_dir, "z_levels", filename)
|
| 198 |
+
fig = plt.figure(figsize=(16, 8))
|
| 199 |
+
proj = ccrs.PlateCarree()
|
| 200 |
+
ax1 = fig.add_subplot(1, 2, 1, projection=proj)
|
| 201 |
+
ax2 = fig.add_subplot(1, 2, 2, projection=proj)
|
| 202 |
+
|
| 203 |
+
center_lat, center_lon = self._get_max_concentration_location()
|
| 204 |
+
lat_idx, lon_idx, lon_min, lon_max, lat_min, lat_max = self._get_zoom_indices(center_lat, center_lon)
|
| 205 |
+
lat_zoom = self.animator.lats[lat_idx]
|
| 206 |
+
lon_zoom = self.animator.lons[lon_idx]
|
| 207 |
+
lon_zoom_grid, lat_zoom_grid = np.meshgrid(lon_zoom, lat_zoom)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
meta = self.animator.datasets[0].attrs
|
| 212 |
+
valid_frames = []
|
| 213 |
+
for t in range(len(self.animator.datasets)):
|
| 214 |
+
interp = interpolate_grid(self.animator.datasets[t]['ash_concentration'].values[z_index],
|
| 215 |
+
self.animator.lon_grid, self.animator.lat_grid)
|
| 216 |
+
if np.isfinite(interp).sum() > 0:
|
| 217 |
+
valid_frames.append(t)
|
| 218 |
+
if not valid_frames:
|
| 219 |
+
print(f"No valid frames for Z={z_km} km.")
|
| 220 |
+
plt.close()
|
| 221 |
+
return
|
| 222 |
+
|
| 223 |
+
def update(t):
|
| 224 |
+
ax1.clear()
|
| 225 |
+
ax2.clear()
|
| 226 |
+
|
| 227 |
+
data = self.animator.datasets[t]['ash_concentration'].values[z_index]
|
| 228 |
+
interp = interpolate_grid(data, self.animator.lon_grid, self.animator.lat_grid)
|
| 229 |
+
interp = np.where(interp < 0, np.nan, interp)
|
| 230 |
+
zoom_plot = interp[np.ix_(lat_idx, lon_idx)]
|
| 231 |
+
|
| 232 |
+
valid_vals = interp[np.isfinite(interp)]
|
| 233 |
+
if valid_vals.size == 0:
|
| 234 |
+
return []
|
| 235 |
+
|
| 236 |
+
min_val, max_val = np.nanmin(valid_vals), np.nanmax(valid_vals)
|
| 237 |
+
log_cutoff = 1e-3
|
| 238 |
+
use_log = min_val > log_cutoff and (max_val / (min_val + 1e-6)) > 100
|
| 239 |
+
|
| 240 |
+
levels = (
|
| 241 |
+
np.logspace(np.log10(log_cutoff), np.log10(max_val), 20)
|
| 242 |
+
if use_log else
|
| 243 |
+
np.linspace(0, max_val, 20)
|
| 244 |
+
)
|
| 245 |
+
data_for_plot = np.where(interp > log_cutoff, interp, np.nan) if use_log else interp
|
| 246 |
+
scale_label = "Log" if use_log else "Linear"
|
| 247 |
+
|
| 248 |
+
c = self._plot_frame(ax1, data_for_plot, self.animator.lons, self.animator.lats,
|
| 249 |
+
f"T{t+1} | Alt: {z_km} km (Full - {scale_label})", levels, scale_label, proj)
|
| 250 |
+
self._plot_frame(ax2, zoom_plot, lon_zoom, lat_zoom,
|
| 251 |
+
f"T{t} | Alt: {z_km} km (Zoom - {scale_label})", levels, scale_label, proj)
|
| 252 |
+
|
| 253 |
+
self._add_country_labels(ax1, [self.animator.lons.min(), self.animator.lons.max(),
|
| 254 |
+
self.animator.lats.min(), self.animator.lats.max()])
|
| 255 |
+
self._add_country_labels(ax2, [lon_min, lon_max, lat_min, lat_max])
|
| 256 |
+
|
| 257 |
+
if not hasattr(update, "colorbar"):
|
| 258 |
+
update.colorbar = fig.colorbar(c, ax=[ax1, ax2], orientation='vertical',
|
| 259 |
+
label="Ash concentration (g/m³)")
|
| 260 |
+
formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 261 |
+
update.colorbar.ax.yaxis.set_major_formatter(formatter)
|
| 262 |
+
|
| 263 |
+
# ✅ Draw threshold outline and label only if exceeded
|
| 264 |
+
if np.nanmax(valid_vals) > self.threshold:
|
| 265 |
+
ax1.contour(self.animator.lons, self.animator.lats, interp, levels=[self.threshold],
|
| 266 |
+
colors='red', linewidths=2, transform=proj)
|
| 267 |
+
ax2.contour(lon_zoom, lat_zoom, zoom_plot, levels=[self.threshold],
|
| 268 |
+
colors='red', linewidths=2, transform=proj)
|
| 269 |
+
ax2.text(0.99, 0.01, f"⚠ Max Thresold Exceed: {np.nanmax(valid_vals):.2f} > {self.threshold} g/m³",
|
| 270 |
+
transform=ax2.transAxes, ha='right', va='bottom',
|
| 271 |
+
fontsize=9, color='red',
|
| 272 |
+
bbox=dict(facecolor='white', alpha=0.8, edgecolor='red'))
|
| 273 |
+
|
| 274 |
+
return []
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
self._draw_metadata_sidebar(fig, meta)
|
| 280 |
+
self._make_dirs(output_path)
|
| 281 |
+
fig.tight_layout()
|
| 282 |
+
ani = animation.FuncAnimation(fig, update, frames=valid_frames, blit=False, cache_frame_data =False)
|
| 283 |
+
ani.save(output_path, writer='pillow', fps=self.fps, dpi=300)
|
| 284 |
+
plt.close()
|
| 285 |
+
print(f"✅ Saved Z-level animation to {output_path}")
|
| 286 |
+
|
| 287 |
+
def plot_vertical_profile_at_time(self, t_index, filename=None):
|
| 288 |
+
time_label = f"T{t_index+1}"
|
| 289 |
+
for z_index, z_val in enumerate(self.animator.levels):
|
| 290 |
+
filename = f"TimeSlices_Z{z_val:.1f}km.gif"
|
| 291 |
+
self.plot_single_z_level(z_val, filename=os.path.join("vertical_profiles_timeSlice", filename))
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
################################################
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
def animate_altitude(self, t_index: int, output_path: str):
|
| 299 |
+
if not (0 <= t_index < len(self.animator.datasets)):
|
| 300 |
+
print(f"Invalid time index {t_index}. Must be between 0 and {len(self.animator.datasets) - 1}.")
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
ds = self.animator.datasets[t_index]
|
| 304 |
+
fig = plt.figure(figsize=(18, 7))
|
| 305 |
+
proj = ccrs.PlateCarree()
|
| 306 |
+
ax1 = fig.add_subplot(1, 2, 1, projection=proj)
|
| 307 |
+
ax2 = fig.add_subplot(1, 2, 2, projection=proj)
|
| 308 |
+
|
| 309 |
+
meta = ds.attrs
|
| 310 |
+
center_lat, center_lon = self._get_max_concentration_location()
|
| 311 |
+
if center_lat is None or center_lon is None:
|
| 312 |
+
print(f"No valid data found for time T{t_index + 1}. Skipping...")
|
| 313 |
+
plt.close()
|
| 314 |
+
return
|
| 315 |
+
|
| 316 |
+
lat_idx, lon_idx, lon_min, lon_max, lat_min, lat_max = self._get_zoom_indices(center_lat, center_lon)
|
| 317 |
+
lat_zoom = self.animator.lats[lat_idx]
|
| 318 |
+
lon_zoom = self.animator.lons[lon_idx]
|
| 319 |
+
lon_zoom_grid, lat_zoom_grid = np.meshgrid(lon_zoom, lat_zoom)
|
| 320 |
+
|
| 321 |
+
z_indices_with_data = []
|
| 322 |
+
for z_index in range(len(self.animator.levels)):
|
| 323 |
+
data = ds['ash_concentration'].values[z_index]
|
| 324 |
+
interp = interpolate_grid(data, self.animator.lon_grid, self.animator.lat_grid)
|
| 325 |
+
if np.isfinite(interp).sum() > 0:
|
| 326 |
+
z_indices_with_data.append(z_index)
|
| 327 |
+
|
| 328 |
+
if not z_indices_with_data:
|
| 329 |
+
print(f"No valid Z-levels at time T{t_index + 1}.")
|
| 330 |
+
plt.close()
|
| 331 |
+
return
|
| 332 |
+
|
| 333 |
+
def update(z_index):
|
| 334 |
+
ax1.clear()
|
| 335 |
+
ax2.clear()
|
| 336 |
+
|
| 337 |
+
data = ds['ash_concentration'].values[z_index]
|
| 338 |
+
interp = interpolate_grid(data, self.animator.lon_grid, self.animator.lat_grid)
|
| 339 |
+
interp = np.where(interp < 0, np.nan, interp)
|
| 340 |
+
zoom_plot = interp[np.ix_(lat_idx, lon_idx)]
|
| 341 |
+
|
| 342 |
+
valid_vals = interp[np.isfinite(interp)]
|
| 343 |
+
if valid_vals.size == 0:
|
| 344 |
+
return []
|
| 345 |
+
|
| 346 |
+
min_val, max_val = np.nanmin(valid_vals), np.nanmax(valid_vals)
|
| 347 |
+
log_cutoff = 1e-3
|
| 348 |
+
use_log = min_val > log_cutoff and (max_val / (min_val + 1e-6)) > 100
|
| 349 |
+
|
| 350 |
+
levels = np.logspace(np.log10(log_cutoff), np.log10(max_val), 20) if use_log else np.linspace(0, max_val, 20)
|
| 351 |
+
data_for_plot = np.where(interp > log_cutoff, interp, np.nan) if use_log else interp
|
| 352 |
+
scale_label = "Log" if use_log else "Linear"
|
| 353 |
+
|
| 354 |
+
title1 = f"T{t_index + 1} | Alt: {self.animator.levels[z_index]} km (Full - {scale_label})"
|
| 355 |
+
title2 = f"T{t_index + 1} | Alt: {self.animator.levels[z_index]} km (Zoom - {scale_label})"
|
| 356 |
+
|
| 357 |
+
c1 = self._plot_frame(ax1, data_for_plot, self.animator.lons, self.animator.lats, title1, levels, scale_label, proj)
|
| 358 |
+
self._plot_frame(ax2, zoom_plot, lon_zoom, lat_zoom, title2, levels, scale_label, proj)
|
| 359 |
+
|
| 360 |
+
self._add_country_labels(ax1, [self.lon_min, self.lon_max, self.lat_min, self.lat_max])
|
| 361 |
+
self._add_country_labels(ax2, [lon_min, lon_max, lat_min, lat_max])
|
| 362 |
+
|
| 363 |
+
if self.include_metadata:
|
| 364 |
+
self._draw_metadata_sidebar(fig, meta)
|
| 365 |
+
|
| 366 |
+
if not hasattr(update, "colorbar"):
|
| 367 |
+
update.colorbar = fig.colorbar(c1, ax=[ax1, ax2], orientation='vertical',
|
| 368 |
+
label="Ash concentration (g/m³)", shrink=0.75)
|
| 369 |
+
formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 370 |
+
update.colorbar.ax.yaxis.set_major_formatter(formatter)
|
| 371 |
+
|
| 372 |
+
if np.nanmax(valid_vals) > self.threshold:
|
| 373 |
+
ax1.contour(self.animator.lons, self.animator.lats, interp, levels=[self.threshold],
|
| 374 |
+
colors='red', linewidths=2, transform=proj)
|
| 375 |
+
ax2.contour(lon_zoom, lat_zoom, zoom_plot, levels=[self.threshold],
|
| 376 |
+
colors='red', linewidths=2, transform=proj)
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
ax2.text(0.99, 0.01, f"⚠ Max Thresold Exceed: {np.nanmax(valid_vals):.2f} > {self.threshold} g/m³",
|
| 380 |
+
transform=ax2.transAxes, ha='right', va='bottom',
|
| 381 |
+
fontsize=9, color='red',
|
| 382 |
+
bbox=dict(facecolor='white', alpha=0.8, edgecolor='red'))
|
| 383 |
+
return []
|
| 384 |
+
|
| 385 |
+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 386 |
+
#fig.set_size_inches(18, 7)
|
| 387 |
+
fig.tight_layout(rect=[0.02, 0.02, 0.98, 0.98])
|
| 388 |
+
ani = animation.FuncAnimation(fig, update, frames=z_indices_with_data, blit=False, cache_frame_data =False)
|
| 389 |
+
ani.save(output_path, writer='pillow', fps=self.fps, dpi=300)
|
| 390 |
+
plt.close()
|
| 391 |
+
print(f"✅ Saved vertical profile animation for T{t_index + 1} to {output_path}")
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def animate_all_altitude_profiles(self, output_folder='altitude_profiles'):
|
| 396 |
+
output_folder = os.path.join(self.output_dir, "altitude_profiles")
|
| 397 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 398 |
+
for t_index in range(len(self.animator.datasets)):
|
| 399 |
+
output_path = os.path.join(output_folder, f"vertical_T{t_index + 1:02d}.gif")
|
| 400 |
+
print(f"🔄 Generating vertical profile animation for T{t_index + 1}...")
|
| 401 |
+
self.animate_altitude(t_index, output_path)
|
| 402 |
+
|
| 403 |
+
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
def export_frames_as_jpgs(self, include_metadata: bool = True):
|
| 409 |
+
output_folder = os.path.join(self.output_dir, "frames")
|
| 410 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 411 |
+
meta = self.animator.datasets[0].attrs
|
| 412 |
+
legend_text = "\\n".join([
|
| 413 |
+
f"Run name: {meta.get('run_name', 'N/A')}",
|
| 414 |
+
f"Run time: {meta.get('run_time', 'N/A')}",
|
| 415 |
+
f"Met data: {meta.get('met_data', 'N/A')}",
|
| 416 |
+
f"Start release: {meta.get('start_of_release', 'N/A')}",
|
| 417 |
+
f"End release: {meta.get('end_of_release', 'N/A')}",
|
| 418 |
+
f"Strength: {meta.get('source_strength', 'N/A')} g/s",
|
| 419 |
+
f"Location: {meta.get('release_location', 'N/A')}",
|
| 420 |
+
f"Height: {meta.get('release_height', 'N/A')} m asl",
|
| 421 |
+
f"Duration: {meta.get('run_duration', 'N/A')}"
|
| 422 |
+
])
|
| 423 |
+
for z_index, z_val in enumerate(self.animator.levels):
|
| 424 |
+
z_dir = os.path.join(output_folder, f"Z{z_val:.1f}km")
|
| 425 |
+
os.makedirs(z_dir, exist_ok=True)
|
| 426 |
+
for t in range(len(self.animator.datasets)):
|
| 427 |
+
data = self.animator.datasets[t]['ash_concentration'].values[z_index]
|
| 428 |
+
interp = interpolate_grid(data, self.animator.lon_grid, self.animator.lat_grid)
|
| 429 |
+
if not np.isfinite(interp).any():
|
| 430 |
+
continue
|
| 431 |
+
fig = plt.figure(figsize=(16, 8))
|
| 432 |
+
proj = ccrs.PlateCarree()
|
| 433 |
+
ax1 = fig.add_subplot(1, 2, 1, projection=proj)
|
| 434 |
+
ax2 = fig.add_subplot(1, 2, 2, projection=proj)
|
| 435 |
+
valid_vals = interp[np.isfinite(interp)]
|
| 436 |
+
min_val, max_val = np.nanmin(valid_vals), np.nanmax(valid_vals)
|
| 437 |
+
log_cutoff = 1e-3
|
| 438 |
+
use_log = min_val > log_cutoff and (max_val / (min_val + 1e-6)) > 100
|
| 439 |
+
levels = np.logspace(np.log10(log_cutoff), np.log10(max_val), 20) if use_log else np.linspace(0, max_val, 20)
|
| 440 |
+
data_for_plot = np.where(interp > log_cutoff, interp, np.nan) if use_log else interp
|
| 441 |
+
scale_label = "Log" if use_log else "Linear"
|
| 442 |
+
center_lat, center_lon = self._get_max_concentration_location()
|
| 443 |
+
lat_idx, lon_idx, lon_min, lon_max, lat_min, lat_max = self._get_zoom_indices(center_lat, center_lon)
|
| 444 |
+
zoom_plot = interp[np.ix_(lat_idx, lon_idx)]
|
| 445 |
+
lon_zoom = self.animator.lons[lon_idx]
|
| 446 |
+
lat_zoom = self.animator.lats[lat_idx]
|
| 447 |
+
c1 = self._plot_frame(ax1, data_for_plot, self.animator.lons, self.animator.lats,
|
| 448 |
+
f"T{t+1} | Alt: {z_val} km (Full - {scale_label})", levels, scale_label, proj)
|
| 449 |
+
self._plot_frame(ax2, zoom_plot, lon_zoom, lat_zoom,
|
| 450 |
+
f"T{t+1} | Alt: {z_val} km (Zoom - {scale_label})", levels, scale_label, proj)
|
| 451 |
+
self._add_country_labels(ax1, [self.animator.lons.min(), self.animator.lons.max(),
|
| 452 |
+
self.animator.lats.min(), self.animator.lats.max()])
|
| 453 |
+
self._add_country_labels(ax2, [lon_min, lon_max, lat_min, lat_max])
|
| 454 |
+
if np.nanmax(valid_vals) > self.threshold:
|
| 455 |
+
ax1.contour(self.animator.lons, self.animator.lats, interp, levels=[self.threshold],
|
| 456 |
+
colors='red', linewidths=2, transform=proj)
|
| 457 |
+
ax2.contour(lon_zoom, lat_zoom, zoom_plot, levels=[self.threshold],
|
| 458 |
+
colors='red', linewidths=2, transform=proj)
|
| 459 |
+
ax2.text(0.99, 0.01, f"⚠ Max: {np.nanmax(valid_vals):.2f} > {self.threshold} g/m³",
|
| 460 |
+
transform=ax2.transAxes, ha='right', va='bottom',
|
| 461 |
+
fontsize=9, color='red',
|
| 462 |
+
bbox=dict(facecolor='white', alpha=0.8, edgecolor='red'))
|
| 463 |
+
if include_metadata:
|
| 464 |
+
self._draw_metadata_sidebar(fig, meta)
|
| 465 |
+
cbar = fig.colorbar(c1, ax=[ax1, ax2], orientation='vertical', shrink=0.75, pad=0.03)
|
| 466 |
+
cbar.set_label("Ash concentration (g/m³)")
|
| 467 |
+
formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 468 |
+
cbar.ax.yaxis.set_major_formatter(formatter)
|
| 469 |
+
frame_path = os.path.join(z_dir, f"frame_{t+1:04d}.jpg")
|
| 470 |
+
plt.savefig(frame_path, bbox_inches='tight')
|
| 471 |
+
plt.close(fig)
|
| 472 |
+
print(f"📸 Saved {frame_path}")
|
ash_animator/plot_horizontal_data.py
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import matplotlib.animation as animation
|
| 5 |
+
import matplotlib.ticker as mticker
|
| 6 |
+
import cartopy.crs as ccrs
|
| 7 |
+
import cartopy.feature as cfeature
|
| 8 |
+
import cartopy.io.shapereader as shpreader
|
| 9 |
+
from adjustText import adjust_text
|
| 10 |
+
from ash_animator.interpolation import interpolate_grid
|
| 11 |
+
from ash_animator.basemaps import draw_etopo_basemap
|
| 12 |
+
import tempfile
|
| 13 |
+
|
| 14 |
+
class Plot_Horizontal_Data:
|
| 15 |
+
def __init__(self, animator, output_dir="plots", cmap="rainbow", fps=2,
|
| 16 |
+
include_metadata=True, threshold=0.1,
|
| 17 |
+
zoom_width_deg=6.0, zoom_height_deg=6.0, zoom_level=7, static_frame_export=False):
|
| 18 |
+
self.animator = animator
|
| 19 |
+
|
| 20 |
+
self.output_dir = os.path.abspath(
|
| 21 |
+
os.path.join(
|
| 22 |
+
os.environ.get("NAME_OUTPUT_DIR", tempfile.gettempdir()),
|
| 23 |
+
output_dir
|
| 24 |
+
)
|
| 25 |
+
)
|
| 26 |
+
os.makedirs(self.output_dir, exist_ok=True)
|
| 27 |
+
self.cmap = cmap
|
| 28 |
+
self.fps = fps
|
| 29 |
+
self.include_metadata = include_metadata
|
| 30 |
+
self.threshold = threshold
|
| 31 |
+
self.zoom_width = zoom_width_deg
|
| 32 |
+
self.zoom_height = zoom_height_deg
|
| 33 |
+
shp = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries')
|
| 34 |
+
self.country_geoms = list(shpreader.Reader(shp).records())
|
| 35 |
+
self.interpolate_grid= interpolate_grid
|
| 36 |
+
self._draw_etopo_basemap=draw_etopo_basemap
|
| 37 |
+
self.zoom_level=zoom_level
|
| 38 |
+
self.static_frame_export=static_frame_export
|
| 39 |
+
|
| 40 |
+
def _make_dirs(self, path):
|
| 41 |
+
os.makedirs(os.path.abspath(os.path.join(os.getcwd(), os.path.dirname(path))), exist_ok=True)
|
| 42 |
+
|
| 43 |
+
def _get_max_concentration_location(self, field):
|
| 44 |
+
max_val = -np.inf
|
| 45 |
+
lat = lon = None
|
| 46 |
+
for ds in self.animator.datasets:
|
| 47 |
+
data = ds[field].values
|
| 48 |
+
if np.max(data) > max_val:
|
| 49 |
+
max_val = np.max(data)
|
| 50 |
+
idx = np.unravel_index(np.argmax(data), data.shape)
|
| 51 |
+
lat = self.animator.lat_grid[idx]
|
| 52 |
+
lon = self.animator.lon_grid[idx]
|
| 53 |
+
return lat, lon
|
| 54 |
+
|
| 55 |
+
def _get_zoom_indices(self, center_lat, center_lon):
|
| 56 |
+
lon_min = center_lon - self.zoom_width / 2
|
| 57 |
+
lon_max = center_lon + self.zoom_width / 2
|
| 58 |
+
lat_min = center_lat - self.zoom_height / 2
|
| 59 |
+
lat_max = center_lat + self.zoom_height / 2
|
| 60 |
+
lat_idx = np.where((self.animator.lats >= lat_min) & (self.animator.lats <= lat_max))[0]
|
| 61 |
+
lon_idx = np.where((self.animator.lons >= lon_min) & (self.animator.lons <= lon_max))[0]
|
| 62 |
+
return lat_idx, lon_idx, lon_min, lon_max, lat_min, lat_max
|
| 63 |
+
|
| 64 |
+
def _add_country_labels(self, ax, extent):
|
| 65 |
+
proj = ccrs.PlateCarree()
|
| 66 |
+
texts = []
|
| 67 |
+
for country in self.country_geoms:
|
| 68 |
+
name = country.attributes['NAME_LONG']
|
| 69 |
+
geom = country.geometry
|
| 70 |
+
try:
|
| 71 |
+
lon, lat = geom.centroid.x, geom.centroid.y
|
| 72 |
+
if extent[0] <= lon <= extent[1] and extent[2] <= lat <= extent[3]:
|
| 73 |
+
text = ax.text(lon, lat, name, fontsize=6, transform=proj,
|
| 74 |
+
ha='center', va='center', color='white',
|
| 75 |
+
bbox=dict(facecolor='black', alpha=0.5, linewidth=0))
|
| 76 |
+
texts.append(text)
|
| 77 |
+
except:
|
| 78 |
+
continue
|
| 79 |
+
adjust_text(texts, ax=ax, only_move={'points': 'y', 'text': 'y'},
|
| 80 |
+
arrowprops=dict(arrowstyle="->", color='white', lw=0.5))
|
| 81 |
+
|
| 82 |
+
def _draw_metadata_sidebar(self, fig, meta_dict):
|
| 83 |
+
lines = [
|
| 84 |
+
f"Run name: {meta_dict.get('run_name', 'N/A')}",
|
| 85 |
+
f"Run time: {meta_dict.get('run_time', 'N/A')}",
|
| 86 |
+
f"Met data: {meta_dict.get('met_data', 'N/A')}",
|
| 87 |
+
f"Start release: {meta_dict.get('start_of_release', 'N/A')}",
|
| 88 |
+
f"End release: {meta_dict.get('end_of_release', 'N/A')}",
|
| 89 |
+
f"Source strength: {meta_dict.get('source_strength', 'N/A')} g/s",
|
| 90 |
+
f"Release loc: {meta_dict.get('release_location', 'N/A')}",
|
| 91 |
+
f"Release height: {meta_dict.get('release_height', 'N/A')} m asl",
|
| 92 |
+
f"Run duration: {meta_dict.get('run_duration', 'N/A')}"
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
# Split into two columns
|
| 96 |
+
mid = len(lines) // 2 + len(lines) % 2
|
| 97 |
+
left_lines = lines[:mid]
|
| 98 |
+
right_lines = lines[mid:]
|
| 99 |
+
|
| 100 |
+
left_text = "\n".join(left_lines)
|
| 101 |
+
right_text = "\n".join(right_lines)
|
| 102 |
+
|
| 103 |
+
# right column
|
| 104 |
+
fig.text(0.05, 0.05, left_text, va='bottom', ha='left',
|
| 105 |
+
fontsize=9, family='monospace', color='black',
|
| 106 |
+
bbox=dict(facecolor='white', alpha=0.8, edgecolor='gray'))
|
| 107 |
+
|
| 108 |
+
# left column
|
| 109 |
+
fig.text(0.3, 0.05, right_text, va='bottom', ha='left',
|
| 110 |
+
fontsize=9, family='monospace', color='black',
|
| 111 |
+
bbox=dict(facecolor='white', alpha=0.8, edgecolor='gray'))
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def _plot_frame(self, ax, data, lons, lats, title, levels, scale_label, proj):
|
| 118 |
+
self._draw_etopo_basemap(ax, mode='basemap', zoom=self.zoom_level)
|
| 119 |
+
c = ax.contourf(lons, lats, data, levels=levels, cmap=self.cmap, alpha=0.6, transform=proj)
|
| 120 |
+
ax.set_title(title)
|
| 121 |
+
ax.set_extent([lons.min(), lons.max(), lats.min(), lats.max()])
|
| 122 |
+
ax.coastlines()
|
| 123 |
+
ax.add_feature(cfeature.BORDERS, linestyle=':')
|
| 124 |
+
ax.add_feature(cfeature.LAND)
|
| 125 |
+
ax.add_feature(cfeature.OCEAN)
|
| 126 |
+
return c
|
| 127 |
+
|
| 128 |
+
def get_available_2d_fields(self):
|
| 129 |
+
ds = self.animator.datasets[0]
|
| 130 |
+
return [v for v in ds.data_vars if ds[v].ndim == 2]
|
| 131 |
+
|
| 132 |
+
def plot_single_field_over_time(self, field, filename="field.gif"):
|
| 133 |
+
output_path = os.path.join(self.output_dir, "2d_fields", field, filename)
|
| 134 |
+
meta = self.animator.datasets[0].attrs
|
| 135 |
+
center_lat, center_lon = self._get_max_concentration_location(field)
|
| 136 |
+
lat_idx, lon_idx, lon_min, lon_max, lat_min, lat_max = self._get_zoom_indices(center_lat, center_lon)
|
| 137 |
+
lat_zoom = self.animator.lats[lat_idx]
|
| 138 |
+
lon_zoom = self.animator.lons[lon_idx]
|
| 139 |
+
|
| 140 |
+
valid_frames = []
|
| 141 |
+
for t in range(len(self.animator.datasets)):
|
| 142 |
+
data = self.animator.datasets[t][field].values
|
| 143 |
+
interp = self.interpolate_grid(data, self.animator.lon_grid, self.animator.lat_grid)
|
| 144 |
+
if np.isfinite(interp).sum() > 0:
|
| 145 |
+
valid_frames.append(t)
|
| 146 |
+
|
| 147 |
+
if not valid_frames:
|
| 148 |
+
print(f"No valid frames to plot for field '{field}'.")
|
| 149 |
+
return
|
| 150 |
+
|
| 151 |
+
fig = plt.figure(figsize=(16, 8))
|
| 152 |
+
proj = ccrs.PlateCarree()
|
| 153 |
+
ax1 = fig.add_subplot(1, 2, 1, projection=proj)
|
| 154 |
+
ax2 = fig.add_subplot(1, 2, 2, projection=proj)
|
| 155 |
+
|
| 156 |
+
def update(t):
|
| 157 |
+
ax1.clear()
|
| 158 |
+
ax2.clear()
|
| 159 |
+
data = self.animator.datasets[t][field].values
|
| 160 |
+
interp = self.interpolate_grid(data, self.animator.lon_grid, self.animator.lat_grid)
|
| 161 |
+
zoom = interp[np.ix_(lat_idx, lon_idx)]
|
| 162 |
+
valid = interp[np.isfinite(interp)]
|
| 163 |
+
if valid.size == 0:
|
| 164 |
+
return []
|
| 165 |
+
|
| 166 |
+
min_val, max_val = np.nanmin(valid), np.nanmax(valid)
|
| 167 |
+
log_cutoff = 1e-3
|
| 168 |
+
use_log = min_val > log_cutoff and (max_val / (min_val + 1e-6)) > 100
|
| 169 |
+
levels = np.logspace(np.log10(log_cutoff), np.log10(max_val), 20) if use_log else np.linspace(0, max_val, 20)
|
| 170 |
+
plot_data = np.where(interp > log_cutoff, interp, np.nan) if use_log else interp
|
| 171 |
+
scale_label = "Log" if use_log else "Linear"
|
| 172 |
+
|
| 173 |
+
c = self._plot_frame(ax1, plot_data, self.animator.lons, self.animator.lats,
|
| 174 |
+
f"T{t+1} | {field} (Full - {scale_label})", levels, scale_label, proj)
|
| 175 |
+
self._plot_frame(ax2, zoom, lon_zoom, lat_zoom,
|
| 176 |
+
f"T{t+1} | {field} (Zoom - {scale_label})", levels, scale_label, proj)
|
| 177 |
+
|
| 178 |
+
self._add_country_labels(ax1, [self.animator.lons.min(), self.animator.lons.max(),
|
| 179 |
+
self.animator.lats.min(), self.animator.lats.max()])
|
| 180 |
+
self._add_country_labels(ax2, [lon_min, lon_max, lat_min, lat_max])
|
| 181 |
+
|
| 182 |
+
# Inside update() function:
|
| 183 |
+
if not hasattr(update, "colorbar"):
|
| 184 |
+
unit_label = f"{field}:({self.animator.datasets[0][field].attrs.get('units', field)})" #self.animator.datasets[0][field].attrs.get('units', field)
|
| 185 |
+
update.colorbar = fig.colorbar(c, ax=[ax1, ax2], orientation='vertical', label=unit_label)
|
| 186 |
+
formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 187 |
+
update.colorbar.ax.yaxis.set_major_formatter(formatter)
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
if np.nanmax(valid) > self.threshold:
|
| 191 |
+
ax1.contour(self.animator.lons, self.animator.lats, interp, levels=[self.threshold],
|
| 192 |
+
colors='red', linewidths=2, transform=proj)
|
| 193 |
+
ax2.contour(lon_zoom, lat_zoom, zoom, levels=[self.threshold],
|
| 194 |
+
colors='red', linewidths=2, transform=proj)
|
| 195 |
+
ax2.text(0.99, 0.01, f"⚠ Max Thresold Exceed: {np.nanmax(valid):.2f} > {self.threshold}",
|
| 196 |
+
transform=ax2.transAxes, ha='right', va='bottom',
|
| 197 |
+
fontsize=9, color='red',
|
| 198 |
+
bbox=dict(facecolor='white', alpha=0.8, edgecolor='red'))
|
| 199 |
+
|
| 200 |
+
if self.static_frame_export:
|
| 201 |
+
frame_folder = os.path.join(self.output_dir, "frames", field)
|
| 202 |
+
os.makedirs(frame_folder, exist_ok=True)
|
| 203 |
+
frame_path = os.path.join(frame_folder, f"frame_{t+1:04d}.jpg")
|
| 204 |
+
plt.savefig(frame_path, bbox_inches='tight')
|
| 205 |
+
print(f"🖼️ Saved static frame: {frame_path}")
|
| 206 |
+
|
| 207 |
+
return []
|
| 208 |
+
|
| 209 |
+
if self.include_metadata:
|
| 210 |
+
self._draw_metadata_sidebar(fig, meta)
|
| 211 |
+
|
| 212 |
+
self._make_dirs(output_path)
|
| 213 |
+
fig.tight_layout()
|
| 214 |
+
ani = animation.FuncAnimation(fig, update, frames=valid_frames, blit=False, cache_frame_data =False)
|
| 215 |
+
ani.save(output_path, writer='pillow', fps=self.fps)
|
| 216 |
+
plt.close()
|
| 217 |
+
print(f"✅ Saved enhanced 2D animation for {field} to {output_path}")
|
| 218 |
+
|
| 219 |
+
# def export_frames_as_jpgs(self, fields=None, include_metadata=True):
|
| 220 |
+
# all_fields = self.get_available_2d_fields()
|
| 221 |
+
# if fields:
|
| 222 |
+
# fields = [f for f in fields if f in all_fields]
|
| 223 |
+
# else:
|
| 224 |
+
# fields = all_fields
|
| 225 |
+
|
| 226 |
+
# meta = self.animator.datasets[0].attrs
|
| 227 |
+
|
| 228 |
+
# for field in fields:
|
| 229 |
+
# print(f"📤 Exporting frames for field: {field}")
|
| 230 |
+
# output_folder = os.path.join(self.output_dir, "frames", field)
|
| 231 |
+
# os.makedirs(output_folder, exist_ok=True)
|
| 232 |
+
|
| 233 |
+
# center_lat, center_lon = self._get_max_concentration_location(field)
|
| 234 |
+
# lat_idx, lon_idx, lon_min, lon_max, lat_min, lat_max = self._get_zoom_indices(center_lat, center_lon)
|
| 235 |
+
# lat_zoom = self.animator.lats[lat_idx]
|
| 236 |
+
# lon_zoom = self.animator.lons[lon_idx]
|
| 237 |
+
|
| 238 |
+
# for t, ds in enumerate(self.animator.datasets):
|
| 239 |
+
# data = ds[field].values
|
| 240 |
+
# interp = self.interpolate_grid(data, self.animator.lon_grid, self.animator.lat_grid)
|
| 241 |
+
# if not np.isfinite(interp).any():
|
| 242 |
+
# continue
|
| 243 |
+
|
| 244 |
+
# fig = plt.figure(figsize=(16, 8))
|
| 245 |
+
# proj = ccrs.PlateCarree()
|
| 246 |
+
# ax1 = fig.add_subplot(1, 2, 1, projection=proj)
|
| 247 |
+
# ax2 = fig.add_subplot(1, 2, 2, projection=proj)
|
| 248 |
+
# zoom = interp[np.ix_(lat_idx, lon_idx)]
|
| 249 |
+
# valid = interp[np.isfinite(interp)]
|
| 250 |
+
# min_val, max_val = np.nanmin(valid), np.nanmax(valid)
|
| 251 |
+
# log_cutoff = 1e-3
|
| 252 |
+
# use_log = min_val > log_cutoff and (max_val / (min_val + 1e-6)) > 100
|
| 253 |
+
# levels = np.logspace(np.log10(log_cutoff), np.log10(max_val), 20) if use_log else np.linspace(0, max_val, 20)
|
| 254 |
+
# plot_data = np.where(interp > log_cutoff, interp, np.nan) if use_log else interp
|
| 255 |
+
# scale_label = "Log" if use_log else "Linear"
|
| 256 |
+
|
| 257 |
+
# c = self._plot_frame(ax1, plot_data, self.animator.lons, self.animator.lats,
|
| 258 |
+
# f"T{t+1} | {field} (Full - {scale_label})", levels, scale_label, proj)
|
| 259 |
+
# self._plot_frame(ax2, zoom, lon_zoom, lat_zoom,
|
| 260 |
+
# f"T{t+1} | {field} (Zoom - {scale_label})", levels, scale_label, proj)
|
| 261 |
+
|
| 262 |
+
# self._add_country_labels(ax1, [self.animator.lons.min(), self.animator.lons.max(),
|
| 263 |
+
# self.animator.lats.min(), self.animator.lats.max()])
|
| 264 |
+
# self._add_country_labels(ax2, [lon_min, lon_max, lat_min, lat_max])
|
| 265 |
+
|
| 266 |
+
# if include_metadata:
|
| 267 |
+
# self._draw_metadata_sidebar(fig, meta)
|
| 268 |
+
|
| 269 |
+
# cbar = fig.colorbar(c, ax=[ax1, ax2], orientation='vertical', shrink=0.75, pad=0.03)
|
| 270 |
+
# unit_label = f"{field}:({self.animator.datasets[0][field].attrs.get('units', field)})"
|
| 271 |
+
# cbar.set_label(unit_label)
|
| 272 |
+
# formatter = mticker.FuncFormatter(lambda x, _: f'{x:.2g}')
|
| 273 |
+
# cbar.ax.yaxis.set_major_formatter(formatter)
|
| 274 |
+
|
| 275 |
+
# if np.nanmax(valid) > self.threshold:
|
| 276 |
+
# ax1.contour(self.animator.lons, self.animator.lats, interp, levels=[self.threshold],
|
| 277 |
+
# colors='red', linewidths=2, transform=proj)
|
| 278 |
+
# ax2.contour(lon_zoom, lat_zoom, zoom, levels=[self.threshold],
|
| 279 |
+
# colors='red', linewidths=2, transform=proj)
|
| 280 |
+
# ax2.text(0.99, 0.01, f"⚠ Max: {np.nanmax(valid):.2f} > {self.threshold}",
|
| 281 |
+
# transform=ax2.transAxes, ha='right', va='bottom',
|
| 282 |
+
# fontsize=9, color='red',
|
| 283 |
+
# bbox=dict(facecolor='white', alpha=0.8, edgecolor='red'))
|
| 284 |
+
|
| 285 |
+
# frame_path = os.path.join(output_folder, f"frame_{t+1:04d}.jpg")
|
| 286 |
+
# plt.savefig(frame_path, dpi=150, bbox_inches='tight')
|
| 287 |
+
# plt.close(fig)
|
| 288 |
+
# print(f"📸 Saved {frame_path}")
|
ash_animator/utils.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from geopy.geocoders import Nominatim
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
def create_grid(attrs):
|
| 6 |
+
x_origin = float(attrs["x_origin"])
|
| 7 |
+
y_origin = float(attrs["y_origin"])
|
| 8 |
+
x_res = float(attrs["x_res"])
|
| 9 |
+
y_res = float(attrs["y_res"])
|
| 10 |
+
x_grid_size = int(attrs["x_grid_size"])
|
| 11 |
+
y_grid_size = int(attrs["y_grid_size"])
|
| 12 |
+
|
| 13 |
+
lons = np.round(np.linspace(x_origin, x_origin + (x_grid_size - 1) * x_res, x_grid_size), 6)
|
| 14 |
+
lats = np.round(np.linspace(y_origin, y_origin + (y_grid_size - 1) * y_res, y_grid_size), 6)
|
| 15 |
+
return lons, lats, np.meshgrid(lons, lats)
|
| 16 |
+
|
| 17 |
+
def get_country_label(lat, lon):
|
| 18 |
+
geolocator = Nominatim(user_agent="ash_animator")
|
| 19 |
+
try:
|
| 20 |
+
location = geolocator.reverse((lat, lon), language='en')
|
| 21 |
+
return location.raw['address'].get('country', 'Unknown')
|
| 22 |
+
except:
|
| 23 |
+
return "Unknown"
|
default_model.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9bb340a75132c3008a149557ff85f8bc05b4a46e70eee027503e30b9573fdd39
|
| 3 |
+
size 181349
|