-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1673 lines (1405 loc) · 65.9 KB
/
Copy pathapp.py
File metadata and controls
1673 lines (1405 loc) · 65.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import pandas as pd
import numpy as np
from scipy.interpolate import RBFInterpolator
from PySide6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QLabel, QSlider, QPushButton, QCheckBox,
QGroupBox, QFileDialog, QSplitter, QDoubleSpinBox, QFormLayout, QMessageBox, QProgressDialog, QComboBox)
from PySide6.QtCore import Qt, QTimer
from PySide6.QtGui import QVector3D, QFont
import pyqtgraph.opengl as gl
import pyqtgraph as pg
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from mpl_toolkits.mplot3d import Axes3D
from PySide6.QtWidgets import QStackedWidget, QDialog, QSizePolicy
# ==========================================
# 0. Localization
# ==========================================
TRANSLATIONS = {
'zh': {
'WINDOW_TITLE': "时空数据可视化与剖面分析工具 v1.0",
'CONTROL_PANEL': "控制面板",
'BTN_LOAD': "加载数据 (CSV)",
'BTN_EXPORT': "导出剖面图 (SVG/PNG)",
'CHK_PROJECTION': "地球投影模式",
'CHK_NORMALIZE': "数据归一化 (0-1)",
'LBL_VAR_SELECT': "选择变量:",
'BTN_PLAY': "播放",
'BTN_PAUSE': "暂停",
'LBL_TIME': "时间",
'LBL_INFO_WAIT': "请加载数据...",
'LBL_INFO_READY': "就绪: {t}T x {lat}Lat x {lon}Lon",
'LBL_INFO_NO_VAR': "无变量被选中",
'PLOT_TITLE': "切面剖面图",
'PLOT_X': "归一化距离 (0-1)",
'PLOT_Y_NORM': "归一化数值 (0-1)",
'PLOT_Y_RAW': "原始数值",
'GRP_SLICE': "切面设置",
'LBL_START_LAT': "起点 纬度:",
'LBL_START_LON': "起点 经度:",
'LBL_END_LAT': "终点 纬度:",
'LBL_END_LON': "终点 经度:",
'MSG_SPARSE_TITLE': "稀疏数据检测",
'MSG_SPARSE_BODY': "检测到非规则网格数据,是否进行插值处理?\n(这可能需要几秒钟)",
'DIALOG_INTERP': "正在插值...",
'BTN_CANCEL': "取消"
},
'en': {
'WINDOW_TITLE': "Spatio-Temporal Data Visualization & Profile Analysis Tool v1.0",
'CONTROL_PANEL': "Control Panel",
'BTN_LOAD': "Load Data (CSV)",
'BTN_EXPORT': "Export Profile (SVG/PNG)",
'CHK_PROJECTION': "Earth Projection Mode",
'CHK_NORMALIZE': "Normalize Data (0-1)",
'LBL_VAR_SELECT': "Select Variables:",
'BTN_PLAY': "Play",
'BTN_PAUSE': "Pause",
'LBL_TIME': "Time",
'LBL_INFO_WAIT': "Please load data...",
'LBL_INFO_READY': "Ready: {t}T x {lat}Lat x {lon}Lon",
'LBL_INFO_NO_VAR': "No variables selected",
'PLOT_TITLE': "Slice Profile Plot",
'PLOT_X': "Normalized Distance (0-1)",
'PLOT_Y_NORM': "Normalized Value (0-1)",
'PLOT_Y_RAW': "Raw Value",
'GRP_SLICE': "Slice Settings",
'LBL_START_LAT': "Start Lat:",
'LBL_START_LON': "Start Lon:",
'LBL_END_LAT': "End Lat:",
'LBL_END_LON': "End Lon:",
'MSG_SPARSE_TITLE': "Sparse Data Detected",
'MSG_SPARSE_BODY': "Irregular grid detected. Perform interpolation?\n(This may take a few seconds)",
'DIALOG_INTERP': "Interpolating...",
'BTN_CANCEL': "Cancel"
},
'ja': {
'WINDOW_TITLE': "時空間データ可視化・断面分析ツール v1.0",
'CONTROL_PANEL': "コントロールパネル",
'BTN_LOAD': "データ読み込み (CSV)",
'CHK_PROJECTION': "地球投影モード",
'CHK_NORMALIZE': "データ正規化 (0-1)",
'LBL_VAR_SELECT': "変数を選択:",
'BTN_PLAY': "再生",
'BTN_PAUSE': "一時停止",
'LBL_TIME': "時間",
'LBL_INFO_WAIT': "データを読み込んでください...",
'LBL_INFO_READY': "準備完了: {t}T x {lat}Lat x {lon}Lon",
'LBL_INFO_NO_VAR': "変数が選択されていません",
'PLOT_TITLE': "断面プロファイル図",
'PLOT_X': "正規化距離 (0-1)",
'PLOT_Y_NORM': "正規化値 (0-1)",
'PLOT_Y_RAW': "生の値",
'GRP_SLICE': "断面設定",
'LBL_START_LAT': "開始 緯度:",
'LBL_START_LON': "開始 経度:",
'LBL_END_LAT': "終了 緯度:",
'LBL_END_LON': "終了 経度:",
'MSG_SPARSE_TITLE': "スパースデータ検出",
'MSG_SPARSE_BODY': "不規則なグリッドが検出されました。補間処理を行いますか?\n(数秒かかる場合があります)",
'DIALOG_INTERP': "補間中...",
'BTN_CANCEL': "キャンセル"
},
'fr': {
'WINDOW_TITLE': "Outil de Visualisation et d'Analyse de Profil Spatio-Temporel v1.0",
'CONTROL_PANEL': "Panneau de configuration",
'BTN_LOAD': "Charger données (CSV)",
'CHK_PROJECTION': "Mode Projection Terrestre",
'CHK_NORMALIZE': "Normaliser (0-1)",
'LBL_VAR_SELECT': "Sélectionner variables:",
'BTN_PLAY': "Lire",
'BTN_PAUSE': "Pause",
'LBL_TIME': "Temps",
'LBL_INFO_WAIT': "Veuillez charger les données...",
'LBL_INFO_READY': "Prêt: {t}T x {lat}Lat x {lon}Lon",
'LBL_INFO_NO_VAR': "Aucune variable sélectionnée",
'PLOT_TITLE': "Profil de Coupe",
'PLOT_X': "Distance Normalisée (0-1)",
'PLOT_Y_NORM': "Valeur Normalisée (0-1)",
'PLOT_Y_RAW': "Valeur Brute",
'GRP_SLICE': "Paramètres de Coupe",
'LBL_START_LAT': "Lat Début:",
'LBL_START_LON': "Lon Début:",
'LBL_END_LAT': "Lat Fin:",
'LBL_END_LON': "Lon Fin:",
'MSG_SPARSE_TITLE': "Données Éparses Détectées",
'MSG_SPARSE_BODY': "Grille irrégulière détectée. Effectuer l'interpolation ?\n(Cela peut prendre quelques secondes)",
'DIALOG_INTERP': "Interpolation en cours...",
'BTN_CANCEL': "Annuler"
},
'ru': {
'WINDOW_TITLE': "Инструмент пространственно-временной визуализации и анализа профилей v1.0",
'CONTROL_PANEL': "Панель управления",
'BTN_LOAD': "Загрузить данные (CSV)",
'CHK_PROJECTION': "Проекция Земли",
'CHK_NORMALIZE': "Нормализация (0-1)",
'LBL_VAR_SELECT': "Выбрать переменные:",
'BTN_PLAY': "Воспр.",
'BTN_PAUSE': "Пауза",
'LBL_TIME': "Время",
'LBL_INFO_WAIT': "Загрузите данные...",
'LBL_INFO_READY': "Готово: {t}T x {lat}Lat x {lon}Lon",
'LBL_INFO_NO_VAR': "Переменные не выбраны",
'PLOT_TITLE': "График профиля среза",
'PLOT_X': "Норм. расстояние (0-1)",
'PLOT_Y_NORM': "Норм. значение (0-1)",
'PLOT_Y_RAW': "Исходное значение",
'GRP_SLICE': "Настройки среза",
'LBL_START_LAT': "Нач. Широта:",
'LBL_START_LON': "Нач. Долгота:",
'LBL_END_LAT': "Кон. Широта:",
'LBL_END_LON': "Кон. Долгота:",
'MSG_SPARSE_TITLE': "Обнаружены разреженные данные",
'MSG_SPARSE_BODY': "Обнаружена нерегулярная сетка. Выполнить интерполяцию?\n(Это может занять несколько секунд)",
'DIALOG_INTERP': "Интерполяция...",
'BTN_CANCEL': "Отмена"
},
'de': {
'WINDOW_TITLE': "Raum-Zeit-Datenvisualisierung & Profilanalyse-Tool v1.0",
'CONTROL_PANEL': "Bedienfeld",
'BTN_LOAD': "Daten laden (CSV)",
'CHK_PROJECTION': "Erdprojektionsmodus",
'CHK_NORMALIZE': "Daten normalisieren (0-1)",
'LBL_VAR_SELECT': "Variablen auswählen:",
'BTN_PLAY': "Abspielen",
'BTN_PAUSE': "Pause",
'LBL_TIME': "Zeit",
'LBL_INFO_WAIT': "Bitte Daten laden...",
'LBL_INFO_READY': "Bereit: {t}T x {lat}Lat x {lon}Lon",
'LBL_INFO_NO_VAR': "Keine Variablen ausgewählt",
'PLOT_TITLE': "Schnittprofil-Diagramm",
'PLOT_X': "Norm. Abstand (0-1)",
'PLOT_Y_NORM': "Norm. Wert (0-1)",
'PLOT_Y_RAW': "Rohwert",
'GRP_SLICE': "Schnitteinstellungen",
'LBL_START_LAT': "Start Lat:",
'LBL_START_LON': "Start Lon:",
'LBL_END_LAT': "Ende Lat:",
'LBL_END_LON': "Ende Lon:",
'MSG_SPARSE_TITLE': "Dünne Daten erkannt",
'MSG_SPARSE_BODY': "Unregelmäßiges Raster erkannt. Interpolation durchführen?\n(Dies kann einige Sekunden dauern)",
'DIALOG_INTERP': "Interpoliere...",
'BTN_CANCEL': "Abbrechen"
},
'it': {
'WINDOW_TITLE': "Strumento di Visualizzazione e Analisi Spazio-Temporale v1.0",
'CONTROL_PANEL': "Pannello di Controllo",
'BTN_LOAD': "Carica Dati (CSV)",
'CHK_PROJECTION': "Modalità Proiezione Terra",
'CHK_NORMALIZE': "Normalizza Dati (0-1)",
'LBL_VAR_SELECT': "Seleziona Variabili:",
'BTN_PLAY': "Riproduci",
'BTN_PAUSE': "Pausa",
'LBL_TIME': "Tempo",
'LBL_INFO_WAIT': "Carica i dati...",
'LBL_INFO_READY': "Pronto: {t}T x {lat}Lat x {lon}Lon",
'LBL_INFO_NO_VAR': "Nessuna variabile selezionata",
'PLOT_TITLE': "Profilo della Sezione",
'PLOT_X': "Distanza Norm. (0-1)",
'PLOT_Y_NORM': "Valore Norm. (0-1)",
'PLOT_Y_RAW': "Valore Grezzo",
'GRP_SLICE': "Impostazioni Sezione",
'LBL_START_LAT': "Lat Inizio:",
'LBL_START_LON': "Lon Inizio:",
'LBL_END_LAT': "Lat Fine:",
'LBL_END_LON': "Lon Fine:",
'MSG_SPARSE_TITLE': "Dati Sparsi Rilevati",
'MSG_SPARSE_BODY': "Griglia irregolare rilevata. Eseguire interpolazione?\n(Potrebbe richiedere alcuni secondi)",
'DIALOG_INTERP': "Interpolazione...",
'BTN_CANCEL': "Annulla"
},
'es': {
'WINDOW_TITLE': "Herramienta de Visualización y Análisis Espacio-Temporal v1.0",
'CONTROL_PANEL': "Panel de Control",
'BTN_LOAD': "Cargar Datos (CSV)",
'CHK_PROJECTION': "Modo Proyección Terrestre",
'CHK_NORMALIZE': "Normalizar Datos (0-1)",
'LBL_VAR_SELECT': "Seleccionar Variables:",
'BTN_PLAY': "Reproducir",
'BTN_PAUSE': "Pausa",
'LBL_TIME': "Tiempo",
'LBL_INFO_WAIT': "Por favor cargue datos...",
'LBL_INFO_READY': "Listo: {t}T x {lat}Lat x {lon}Lon",
'LBL_INFO_NO_VAR': "Sin variables seleccionadas",
'PLOT_TITLE': "Perfil de Corte",
'PLOT_X': "Distancia Norm. (0-1)",
'PLOT_Y_NORM': "Valor Norm. (0-1)",
'PLOT_Y_RAW': "Valor Bruto",
'GRP_SLICE': "Ajustes de Corte",
'LBL_START_LAT': "Lat Inicio:",
'LBL_START_LON': "Lon Inicio:",
'LBL_END_LAT': "Lat Fin:",
'LBL_END_LON': "Lon Fin:",
'MSG_SPARSE_TITLE': "Datos Dispersos Detectados",
'MSG_SPARSE_BODY': "Rejilla irregular detectada. ¿Realizar interpolación?\n(Puede tardar unos segundos)",
'DIALOG_INTERP': "Interpolando...",
'BTN_CANCEL': "Cancelar"
},
'pt': {
'WINDOW_TITLE': "Ferramenta de Visualização e Análise Espaço-Temporal v1.0",
'CONTROL_PANEL': "Painel de Controle",
'BTN_LOAD': "Carregar Dados (CSV)",
'CHK_PROJECTION': "Modo Projeção Terra",
'CHK_NORMALIZE': "Normalizar Dados (0-1)",
'LBL_VAR_SELECT': "Selecionar Variáveis:",
'BTN_PLAY': "Reproduzir",
'BTN_PAUSE': "Pausa",
'LBL_TIME': "Tempo",
'LBL_INFO_WAIT': "Por favor carregue dados...",
'LBL_INFO_READY': "Pronto: {t}T x {lat}Lat x {lon}Lon",
'LBL_INFO_NO_VAR': "Nenhuma variável selecionada",
'PLOT_TITLE': "Perfil de Corte",
'PLOT_X': "Distância Norm. (0-1)",
'PLOT_Y_NORM': "Valor Norm. (0-1)",
'PLOT_Y_RAW': "Valor Bruto",
'GRP_SLICE': "Configurações de Corte",
'LBL_START_LAT': "Lat Início:",
'LBL_START_LON': "Lon Início:",
'LBL_END_LAT': "Lat Fim:",
'LBL_END_LON': "Lon Fim:",
'MSG_SPARSE_TITLE': "Dados Esparsos Detectados",
'MSG_SPARSE_BODY': "Grade irregular detectada. Realizar interpolação?\n(Isso pode levar alguns segundos)",
'DIALOG_INTERP': "Interpolando...",
'BTN_CANCEL': "Cancelar"
},
'ko': {
'WINDOW_TITLE': "시공간 데이터 시각화 및 프로파일 분석 도구 v1.0",
'CONTROL_PANEL': "제어 패널",
'BTN_LOAD': "데이터 로드 (CSV)",
'CHK_PROJECTION': "지구 투영 모드",
'CHK_NORMALIZE': "데이터 정규화 (0-1)",
'LBL_VAR_SELECT': "변수 선택:",
'BTN_PLAY': "재생",
'BTN_PAUSE': "일시 중지",
'LBL_TIME': "시간",
'LBL_INFO_WAIT': "데이터를 로드하십시오...",
'LBL_INFO_READY': "준비: {t}T x {lat}Lat x {lon}Lon",
'LBL_INFO_NO_VAR': "선택된 변수 없음",
'PLOT_TITLE': "단면 프로파일 플롯",
'PLOT_X': "정규화 거리 (0-1)",
'PLOT_Y_NORM': "정규화 값 (0-1)",
'PLOT_Y_RAW': "원시 값",
'GRP_SLICE': "단면 설정",
'LBL_START_LAT': "시작 위도:",
'LBL_START_LON': "시작 경도:",
'LBL_END_LAT': "종료 위도:",
'LBL_END_LON': "종료 경도:",
'MSG_SPARSE_TITLE': "희소 데이터 감지됨",
'MSG_SPARSE_BODY': "불규칙 그리드가 감지되었습니다. 보간을 수행하시겠습니까?\n(몇 초 정도 걸릴 수 있습니다)",
'DIALOG_INTERP': "보간 중...",
'BTN_CANCEL': "취소"
}
}
# ==========================================
# 1. Data Processing Module (Interpolation)
# ==========================================
class DataProcessor:
@staticmethod
def process_sparse_data(df, progress_callback=None):
"""
Interpolate sparse dataframe to dense grid.
df: DataFrame with 'Latitude', 'Longitude', 'Age' (or 'Time') and variables.
"""
# 1. Identify Columns
coords_cols = ['Latitude', 'Longitude', 'Time', 'Age']
# Map Age to Time if needed
if 'Age' in df.columns and 'Time' not in df.columns:
df['Time'] = df['Age']
var_cols = [c for c in df.columns if c not in coords_cols and c != 'SiteID']
# 2. Define Target Grid
grid_lat_res = 50
grid_lon_res = 100
grid_time_res = 20
lats = np.linspace(-90, 90, grid_lat_res)
lons = np.linspace(-180, 180, grid_lon_res)
times = np.linspace(df['Time'].min(), df['Time'].max(), grid_time_res)
# Create meshgrid (3D)
grid_lat_mesh, grid_lon_mesh, grid_time_mesh = np.meshgrid(lats, lons, times, indexing='ij')
# Flatten target grid
target_points = np.column_stack([
grid_lat_mesh.ravel(),
grid_lon_mesh.ravel(),
grid_time_mesh.ravel()
])
# 3. Normalization Helpers
def normalize(v, v_min, v_max):
return (v - v_min) / (v_max - v_min) if v_max > v_min else np.zeros_like(v)
min_lat, max_lat = -90, 90
min_lon, max_lon = -180, 180
min_time, max_time = df['Time'].min(), df['Time'].max()
# 4. Interpolation
dense_data_dict = {
'Latitude': grid_lat_mesh.ravel(),
'Longitude': grid_lon_mesh.ravel(),
'Time': grid_time_mesh.ravel()
}
# Normalize Target once
tgt_lat_n = normalize(target_points[:,0], min_lat, max_lat)
tgt_lon_n = normalize(target_points[:,1], min_lon, max_lon)
tgt_time_n = normalize(target_points[:,2], min_time, max_time)
tgt_points_n = np.column_stack([tgt_lat_n, tgt_lon_n, tgt_time_n])
total_vars = len(var_cols)
for i, var in enumerate(var_cols):
if progress_callback:
progress_callback(int((i / total_vars) * 100), f"Interpolating {var}...")
# Filter NaN
sub_df = df.dropna(subset=[var, 'Latitude', 'Longitude', 'Time'])
if len(sub_df) < 10:
dense_data_dict[var] = np.nan
continue
src_lat = sub_df['Latitude'].values
src_lon = sub_df['Longitude'].values
src_time = sub_df['Time'].values
src_vals = sub_df[var].values
# Normalize Source
src_lat_n = normalize(src_lat, min_lat, max_lat)
src_lon_n = normalize(src_lon, min_lon, max_lon)
src_time_n = normalize(src_time, min_time, max_time)
src_points_n = np.column_stack([src_lat_n, src_lon_n, src_time_n])
try:
# RBF Interpolation
rbf = RBFInterpolator(src_points_n, src_vals, kernel='thin_plate_spline', smoothing=0.1)
interpolated_vals = rbf(tgt_points_n)
dense_data_dict[var] = interpolated_vals
except Exception as e:
print(f"Interpolation error for {var}: {e}")
dense_data_dict[var] = np.nan
dense_df = pd.DataFrame(dense_data_dict)
dense_df.sort_values(by=['Time', 'Latitude', 'Longitude'], inplace=True)
return dense_df
# ==========================================
# 2. Interactive 3D View Widget
# ==========================================
class InteractiveGLViewWidget(gl.GLViewWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.dragging_idx = -1 # -1: none, 0: Start, 1: End
self.parent_viz = None
self.z_plane = 12
def wheelEvent(self, ev):
delta = ev.angleDelta().y()
# Adjust zoom factor for better responsiveness
zoom_factor = 0.9 if delta > 0 else 1.1
new_dist = self.opts['distance'] * zoom_factor
# Limit distance to prevent getting stuck or going too far
new_dist = max(1.0, min(new_dist, 500.0))
self.opts['distance'] = new_dist
self.update()
def get_ray(self, pos):
"""Maps 2D screen position to a 3D world-space ray."""
x = pos.x()
y = pos.y()
w = self.width()
h = self.height()
# NDC
x_ndc = (2.0 * x / w) - 1.0
y_ndc = 1.0 - (2.0 * y / h)
view_matrix = self.viewMatrix()
proj_matrix = self.projectionMatrix()
mvp = proj_matrix * view_matrix
inv_mvp, success = mvp.inverted()
if not success: return None, None
p_near = inv_mvp * QVector3D(x_ndc, y_ndc, -1.0)
p_far = inv_mvp * QVector3D(x_ndc, y_ndc, 1.0)
direction = (p_far - p_near).normalized()
return p_near, direction
def intersect_z_plane(self, start, direction):
if start is None or direction is None: return None
# Check projection mode
if self.parent_viz and self.parent_viz.chk_projection.isChecked():
# Spherical Intersection |P| = R
R = 20.5
a = 1
b = 2 * QVector3D.dotProduct(start, direction)
c = QVector3D.dotProduct(start, start) - R*R
discriminant = b*b - 4*a*c
if discriminant < 0: return None
t1 = (-b - np.sqrt(discriminant)) / (2*a)
t2 = (-b + np.sqrt(discriminant)) / (2*a)
t = None
if t1 > 0: t = t1
elif t2 > 0: t = t2
if t is None: return None
hit = start + direction * t
return hit
else:
# Planar Intersection Z = z_plane
if abs(direction.z()) < 1e-6: return None
t = (self.z_plane - start.z()) / direction.z()
hit = start + direction * t
return hit
def mousePressEvent(self, ev):
if ev.button() == Qt.LeftButton:
start, direction = self.get_ray(ev.position())
hit = self.intersect_z_plane(start, direction)
if hit and self.parent_viz:
lat1 = self.parent_viz.spin_lat1.value()
lon1 = self.parent_viz.spin_lon1.value()
lat2 = self.parent_viz.spin_lat2.value()
lon2 = self.parent_viz.spin_lon2.value()
p1_x, p1_y, p1_z = self.parent_viz.latlon_to_local(lat1, lon1)
p2_x, p2_y, p2_z = self.parent_viz.latlon_to_local(lat2, lon2)
# Check proximity
if self.parent_viz.chk_projection.isChecked():
p1 = np.array([p1_x, p1_y, p1_z])
p1 = p1 / np.linalg.norm(p1) * 20.5
p2 = np.array([p2_x, p2_y, p2_z])
p2 = p2 / np.linalg.norm(p2) * 20.5
hit_arr = np.array([hit.x(), hit.y(), hit.z()])
dist1 = np.linalg.norm(hit_arr - p1)
dist2 = np.linalg.norm(hit_arr - p2)
else:
dist1 = np.hypot(hit.x() - p1_x, hit.y() - p1_y)
dist2 = np.hypot(hit.x() - p2_x, hit.y() - p2_y)
threshold = 2.0
if dist1 < threshold:
self.dragging_idx = 0
return
elif dist2 < threshold:
self.dragging_idx = 1
return
super().mousePressEvent(ev)
def mouseMoveEvent(self, ev):
if self.dragging_idx != -1 and self.parent_viz:
start, direction = self.get_ray(ev.position())
hit = self.intersect_z_plane(start, direction)
if hit:
lat, lon = self.parent_viz.local_to_latlon(hit.x(), hit.y(), hit.z())
if self.dragging_idx == 0:
self.parent_viz.spin_lat1.setValue(lat)
self.parent_viz.spin_lon1.setValue(lon)
else:
self.parent_viz.spin_lat2.setValue(lat)
self.parent_viz.spin_lon2.setValue(lon)
return
super().mouseMoveEvent(ev)
def mouseReleaseEvent(self, ev):
if self.dragging_idx != -1:
self.dragging_idx = -1
return
super().mouseReleaseEvent(ev)
# ==========================================
# 3. Main Application Window
# ==========================================
class SpatioTemporalViz(QMainWindow):
def __init__(self):
super().__init__()
self.resize(1400, 900)
# Localization
self.current_lang = 'zh' # Default
self.tr = TRANSLATIONS[self.current_lang]
# Data storage
self.df = None
self.grid_data = {}
self.unique_times = []
self.unique_lats = []
self.unique_lons = []
self.current_time_idx = 0
self.active_variables = set()
self.is_playing = False
self.var_order = []
self.var_colors = {}
# Rendering objects
self.surfaces = {}
self.surface_labels = {}
self.axis_labels = []
self.init_ui()
self.timer = QTimer()
self.timer.timeout.connect(self.next_step)
def init_ui(self):
splitter = QSplitter(Qt.Horizontal)
self.setCentralWidget(splitter)
# --- LEFT PANEL: 3D View ---
left_widget = QWidget()
left_layout = QVBoxLayout(left_widget)
self.view = InteractiveGLViewWidget()
self.view.parent_viz = self
self.view.setCameraPosition(distance=80, elevation=30, azimuth=45)
self.view.opts['distance'] = 80
left_layout.addWidget(self.view, stretch=1)
# Grid
self.grid = gl.GLGridItem()
self.grid.setSize(x=20, y=20, z=0)
self.grid.setSpacing(x=1, y=1, z=1)
self.view.addItem(self.grid)
# Slice Indicators
self.slice_line = gl.GLLinePlotItem(pos=np.array([[0,0,0], [0,0,0]]), color=(1,1,1,1), width=2, antialias=True)
self.view.addItem(self.slice_line)
self.slice_points = gl.GLScatterPlotItem(pos=np.array([[0,0,0], [0,0,0]]), color=(1,1,1,1), size=10, pxMode=True)
self.view.addItem(self.slice_points)
# Earth Sphere
md = gl.MeshData.sphere(rows=40, cols=80)
self.earth_sphere = gl.GLMeshItem(
meshdata=md, smooth=True, color=(0.1, 0.1, 0.3, 0.3),
shader='shaded', glOptions='translucent', drawEdges=True, edgeColor=(0.3, 0.3, 0.5, 0.2)
)
self.earth_sphere.scale(19.9, 19.9, 19.9)
self.earth_sphere.setVisible(False)
self.view.addItem(self.earth_sphere)
# Controls Group
self.controls_group = QGroupBox()
controls_layout = QHBoxLayout(self.controls_group)
left_layout.addWidget(self.controls_group)
# File Loading
f_layout = QVBoxLayout()
# Language Selector
self.combo_lang = QComboBox()
self.combo_lang.addItems([
"中文 (Chinese)", "English", "日本語 (Japanese)", "Français (French)",
"Русский (Russian)", "Deutsch (German)", "Italiano (Italian)",
"Español (Spanish)", "Português (Portuguese)", "한국어 (Korean)"
])
# Map index to code
self.lang_codes = ['zh', 'en', 'ja', 'fr', 'ru', 'de', 'it', 'es', 'pt', 'ko']
self.combo_lang.currentIndexChanged.connect(self.on_language_changed)
f_layout.addWidget(self.combo_lang)
self.btn_load = QPushButton()
self.btn_load.clicked.connect(self.open_file_dialog)
f_layout.addWidget(self.btn_load)
self.chk_projection = QCheckBox()
self.chk_projection.stateChanged.connect(self.on_projection_changed)
f_layout.addWidget(self.chk_projection)
# Normalization Mode
self.chk_normalize = QCheckBox()
self.chk_normalize.stateChanged.connect(self.on_normalization_changed)
f_layout.addWidget(self.chk_normalize)
f_layout.addStretch()
controls_layout.addLayout(f_layout)
# Variable Selection
self.v_layout = QVBoxLayout()
self.lbl_var_select = QLabel()
self.v_layout.addWidget(self.lbl_var_select)
self.check_boxes = {}
controls_layout.addLayout(self.v_layout)
# Play Controls
p_layout = QVBoxLayout()
self.btn_play = QPushButton()
self.btn_play.clicked.connect(self.toggle_play)
p_layout.addWidget(self.btn_play)
controls_layout.addLayout(p_layout)
# Time Slider
t_layout = QVBoxLayout()
self.lbl_time = QLabel()
t_layout.addWidget(self.lbl_time)
self.slider = QSlider(Qt.Horizontal)
self.slider.setMinimum(0)
self.slider.valueChanged.connect(self.on_slider_change)
t_layout.addWidget(self.slider)
controls_layout.addLayout(t_layout, stretch=1)
self.lbl_info = QLabel()
controls_layout.addWidget(self.lbl_info)
# --- RIGHT PANEL: Slice Analysis ---
right_widget = QWidget()
right_layout = QVBoxLayout(right_widget)
self.plot_widget = pg.PlotWidget()
# Academic Style: White background, Black axes
self.plot_widget.setBackground('w')
self.plot_widget.getAxis('bottom').setPen('k')
self.plot_widget.getAxis('left').setPen('k')
self.plot_widget.getAxis('bottom').setTextPen('k')
self.plot_widget.getAxis('left').setTextPen('k')
self.plot_widget.addLegend()
right_layout.addWidget(self.plot_widget, stretch=1)
self.slice_group = QGroupBox()
slice_layout = QFormLayout(self.slice_group)
self.spin_lat1 = QDoubleSpinBox()
self.spin_lon1 = QDoubleSpinBox()
self.spin_lat2 = QDoubleSpinBox()
self.spin_lon2 = QDoubleSpinBox()
for spin in [self.spin_lat1, self.spin_lon1, self.spin_lat2, self.spin_lon2]:
spin.setDecimals(2)
spin.setRange(-999, 999)
spin.valueChanged.connect(self.update_slice_view)
self.lbl_start_lat = QLabel()
self.lbl_start_lon = QLabel()
self.lbl_end_lat = QLabel()
self.lbl_end_lon = QLabel()
slice_layout.addRow(self.lbl_start_lat, self.spin_lat1)
slice_layout.addRow(self.lbl_start_lon, self.spin_lon1)
slice_layout.addRow(self.lbl_end_lat, self.spin_lat2)
slice_layout.addRow(self.lbl_end_lon, self.spin_lon2)
self.btn_export = QPushButton()
self.btn_export.clicked.connect(self.export_slice_plot)
slice_layout.addRow(self.btn_export)
# New Button for Multi-Site Analysis
self.btn_multi_analysis = QPushButton("时空演化分析 (Spatio-Temporal)")
self.btn_multi_analysis.clicked.connect(self.open_multi_analysis)
slice_layout.addRow(self.btn_multi_analysis)
right_layout.addWidget(self.slice_group)
splitter.addWidget(left_widget)
splitter.addWidget(right_widget)
splitter.setSizes([900, 400])
self.update_ui_text()
def on_language_changed(self, index):
code = self.lang_codes[index]
self.current_lang = code
self.tr = TRANSLATIONS[code]
self.update_ui_text()
def update_ui_text(self):
tr = self.tr
self.setWindowTitle(tr['WINDOW_TITLE'])
self.controls_group.setTitle(tr['CONTROL_PANEL'])
self.btn_load.setText(tr['BTN_LOAD'])
self.chk_projection.setText(tr['CHK_PROJECTION'])
self.chk_normalize.setText(tr['CHK_NORMALIZE'])
self.lbl_var_select.setText(tr['LBL_VAR_SELECT'])
self.btn_play.setText(tr['BTN_PAUSE'] if self.is_playing else tr['BTN_PLAY'])
# Time Label update
current_time_val = 0
if self.unique_times is not None and len(self.unique_times) > self.current_time_idx:
current_time_val = self.unique_times[self.current_time_idx]
self.lbl_time.setText(f"{tr['LBL_TIME']}: {current_time_val:.2f}")
# Info Label
if self.df is None:
self.lbl_info.setText(tr['LBL_INFO_WAIT'])
else:
# Refresh info text logic if needed, or just keep current status
pass
self.plot_widget.setTitle(tr['PLOT_TITLE'])
self.plot_widget.setLabel('bottom', tr['PLOT_X'])
is_norm = self.chk_normalize.isChecked()
self.plot_widget.setLabel('left', tr['PLOT_Y_NORM'] if is_norm else tr['PLOT_Y_RAW'])
self.slice_group.setTitle(tr['GRP_SLICE'])
self.lbl_start_lat.setText(tr['LBL_START_LAT'])
self.lbl_start_lon.setText(tr['LBL_START_LON'])
self.lbl_end_lat.setText(tr['LBL_END_LAT'])
self.lbl_end_lon.setText(tr['LBL_END_LON'])
self.btn_export.setText(tr.get('BTN_EXPORT', "Export"))
# Update Plot Labels if data loaded
if self.df is not None:
self.update_plot()
self.update_slice_view()
def open_file_dialog(self):
filename, _ = QFileDialog.getOpenFileName(self, self.tr['BTN_LOAD'], "", "CSV Files (*.csv)")
if filename:
self.load_data(filename)
def normalize_data(self, df):
# Rename standard columns
mapping = {
'lat': 'Latitude', 'latitude': 'Latitude', 'y': 'Latitude',
'lon': 'Longitude', 'longitude': 'Longitude', 'long': 'Longitude', 'x': 'Longitude',
'time': 'Time', 'date': 'Time', 't': 'Time', 'age': 'Time', 'Age': 'Time'
}
df = df.rename(columns=lambda x: mapping.get(x.lower(), mapping.get(x, x)))
return df
def load_data(self, filepath):
try:
print(f"Loading {filepath}...")
df_raw = pd.read_csv(filepath)
df_norm = self.normalize_data(df_raw)
required = ['Latitude', 'Longitude', 'Time']
if not all(col in df_norm.columns for col in required):
# Try heuristics
raise ValueError("Could not find Latitude, Longitude, or Time columns.")
# Check if sparse / needs interpolation
# Heuristic: irregular unique counts or explicit SiteID
unique_lats = df_norm['Latitude'].unique()
unique_lons = df_norm['Longitude'].unique()
unique_times = df_norm['Time'].unique()
expected_rows = len(unique_lats) * len(unique_lons) * len(unique_times)
is_sparse = len(df_norm) != expected_rows
if 'SiteID' in df_norm.columns or is_sparse:
reply = QMessageBox.question(self, self.tr['MSG_SPARSE_TITLE'],
self.tr['MSG_SPARSE_BODY'],
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if reply == QMessageBox.Yes:
# Show progress
progress = QProgressDialog(self.tr['DIALOG_INTERP'], self.tr['BTN_CANCEL'], 0, 100, self)
progress.setWindowModality(Qt.WindowModal)
progress.show()
def cb(val, msg):
progress.setValue(val)
progress.setLabelText(msg)
QApplication.processEvents()
self.df = DataProcessor.process_sparse_data(df_norm, progress_callback=cb)
progress.setValue(100)
else:
self.df = df_norm
else:
self.df = df_norm
# --- Common Load Logic ---
self.setup_visualization()
except Exception as e:
self.lbl_info.setText(f"错误: {str(e)}")
import traceback
traceback.print_exc()
def setup_visualization(self):
# Reset
self.grid_data = {}
self.active_variables = set()
self.var_colors = {}
while self.v_layout.count() > 1:
item = self.v_layout.takeAt(1)
if item.widget(): item.widget().deleteLater()
for item in self.view.items[:]:
if isinstance(item, (gl.GLMeshItem, gl.GLTextItem)) and item != self.earth_sphere:
self.view.removeItem(item)
self.surfaces = {}
self.surface_labels = {}
self.axis_labels = []
# Dimensions
self.unique_times = np.sort(self.df['Time'].unique())
self.unique_lats = np.sort(self.df['Latitude'].unique())
self.unique_lons = np.sort(self.df['Longitude'].unique())
n_t, n_lat, n_lon = len(self.unique_times), len(self.unique_lats), len(self.unique_lons)
self.slider.setMaximum(n_t - 1)
self.slider.setValue(0)
min_lat, max_lat = self.unique_lats[0], self.unique_lats[-1]
min_lon, max_lon = self.unique_lons[0], self.unique_lons[-1]
for spin in [self.spin_lat1, self.spin_lat2]:
spin.setRange(min_lat, max_lat)
spin.setSingleStep((max_lat - min_lat) / 20)
for spin in [self.spin_lon1, self.spin_lon2]:
spin.setRange(min_lon, max_lon)
spin.setSingleStep((max_lon - min_lon) / 20)
self.spin_lat1.setValue(min_lat)
self.spin_lon1.setValue(min_lon)
self.spin_lat2.setValue(max_lat)
self.spin_lon2.setValue(max_lon)
# Process Variables
reserved = ['Time', 'Latitude', 'Longitude', 'SiteID']
self.var_order = [c for c in self.df.columns if c not in reserved]
palette = [(255, 50, 50), (50, 255, 50), (50, 100, 255), (255, 255, 0),
(0, 255, 255), (255, 0, 255), (255, 128, 0)]
for i, var in enumerate(self.var_order):
# Safe reshape
try:
data_array = self.df[var].values.reshape(n_t, n_lat, n_lon)
except:
print(f"Skipping {var} due to shape mismatch")
continue
self.grid_data[var] = data_array
self.var_colors[var] = palette[i % len(palette)]
cb = QCheckBox(var)
if i == 0:
cb.setChecked(True)
self.active_variables.add(var)
cb.stateChanged.connect(self.on_var_check_changed)
self.check_boxes[var] = cb
self.v_layout.addWidget(cb)
self.lbl_info.setText(self.tr['LBL_INFO_READY'].format(t=n_t, lat=n_lat, lon=n_lon))
self.init_surfaces()
self.update_axis_labels()
self.update_plot()
self.update_slice_view()
def generate_grid_faces(self, rows, cols):
r_idx = np.arange(rows - 1)
c_idx = np.arange(cols - 1)
r_grid, c_grid = np.meshgrid(r_idx, c_idx, indexing='ij')
p00 = r_grid * cols + c_grid
p01 = r_grid * cols + (c_grid + 1)
p10 = (r_grid + 1) * cols + c_grid
p11 = (r_grid + 1) * cols + (c_grid + 1)
t1 = np.column_stack((p00.flatten(), p01.flatten(), p11.flatten()))
t2 = np.column_stack((p00.flatten(), p11.flatten(), p10.flatten()))
return np.vstack((t1, t2)).astype(np.uint32)
def init_surfaces(self):
for var in self.var_order:
if var not in self.grid_data: continue
surface = gl.GLMeshItem(meshdata=None, smooth=True, shader='shaded', glOptions='translucent')
surface.setVisible(False)
self.view.addItem(surface)
self.surfaces[var] = surface
label = gl.GLTextItem(text=var, color=(1, 1, 1, 1))
label.setVisible(False)
self.view.addItem(label)
self.surface_labels[var] = label
def update_axis_labels(self):
for item in self.axis_labels:
try: self.view.removeItem(item)
except: pass
self.axis_labels = []
if len(self.unique_lons) == 0: return
n_labels = 5
is_spherical = self.chk_projection.isChecked()
min_lat, max_lat = self.unique_lats[0], self.unique_lats[-1]
min_lon, max_lon = self.unique_lons[0], self.unique_lons[-1]
# Lon
lon_indices = np.linspace(0, len(self.unique_lons)-1, n_labels, dtype=int)
for idx in lon_indices:
lon_val = self.unique_lons[idx]
if is_spherical:
R = 24.0
phi = np.radians(90 - max(-90, min_lat - 10))
theta = np.radians(lon_val)
x = R * np.sin(phi) * np.cos(theta)
y = R * np.sin(phi) * np.sin(theta)
z = R * np.cos(phi)
label = gl.GLTextItem(pos=(x,y,z), text=f"{lon_val:.0f}", font=QFont('Arial', 8))
else:
x = -10 + (idx / (len(self.unique_lons)-1)) * 20
label = gl.GLTextItem(pos=(x, -12, 0), text=f"{lon_val:.0f}", font=QFont('Arial', 8))
self.view.addItem(label)
self.axis_labels.append(label)
# Lat
lat_indices = np.linspace(0, len(self.unique_lats)-1, n_labels, dtype=int)
for idx in lat_indices:
lat_val = self.unique_lats[idx]
if is_spherical:
R = 24.0
phi = np.radians(90 - lat_val)
theta = np.radians(min_lon - 10)
x = R * np.sin(phi) * np.cos(theta)
y = R * np.sin(phi) * np.sin(theta)
z = R * np.cos(phi)
label = gl.GLTextItem(pos=(x,y,z), text=f"{lat_val:.0f}", font=QFont('Arial', 8))
else:
y = -10 + (idx / (len(self.unique_lats)-1)) * 20
label = gl.GLTextItem(pos=(-13, y, 0), text=f"{lat_val:.0f}", font=QFont('Arial', 8))
self.view.addItem(label)
self.axis_labels.append(label)
def generate_gradient_colors(self, data, base_color_rgb):
d_min = np.nanmin(data)
d_max = np.nanmax(data)
if d_max == d_min: norm = np.zeros_like(data)
else: norm = (data - d_min) / (d_max - d_min)
norm = np.nan_to_num(norm, nan=0.0)
norm_flat = norm.flatten()
colors = np.zeros((len(norm_flat), 4), dtype=float)
r, g, b = [c/255.0 for c in base_color_rgb]
colors[:, 0] = r; colors[:, 1] = g; colors[:, 2] = b
colors[:, 3] = 0.05 + 0.8 * (norm_flat ** 1.5)
return colors
def update_plot(self):
if not self.grid_data: return
info_texts = []
is_spherical = self.chk_projection.isChecked()
is_norm = self.chk_normalize.isChecked()
n_lat = len(self.unique_lats)
n_lon = len(self.unique_lons)
if not hasattr(self, '_cached_faces') or self._cached_faces_shape != (n_lat, n_lon):
self._cached_faces = self.generate_grid_faces(n_lat, n_lon)
self._cached_faces_shape = (n_lat, n_lon)