File size: 11,936 Bytes
613de59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import subprocess
import traceback
from datetime import datetime, timedelta

import gradio as gr
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import pytz

from config import STATION_NAMES
from supabase_utils import (
    get_harmonic_predictions, save_predictions_to_supabase, get_supabase_client
)

def get_common_args(station_id):
    return [
        "--model", "TimeXer", "--features", "MS", "--seq_len", "144", "--pred_len", "72",
        "--label_len", "96", "--enc_in", "5", "--dec_in", "5", "--c_out", "1",
        "--d_model", "256", "--d_ff", "512", "--n_heads", "8", "--e_layers", "1",
        "--d_layers", "1", "--factor", "3", "--patch_len", "16", "--expand", "2", "--d_conv", "4"
    ]

def validate_csv_file(file_path, required_rows=144):
    """CSV 파일 유효성 검사"""
    try:
        df = pd.read_csv(file_path)
        required_columns = ['date', 'air_pres', 'wind_dir', 'wind_speed', 'air_temp', 'residual']
        missing_columns = [col for col in required_columns if col not in df.columns]
        
        if missing_columns:
            return False, f"필수 컬럼이 누락되었습니다: {missing_columns}"
        
        if len(df) < required_rows:
            return False, f"데이터가 부족합니다. 최소 {required_rows}행 필요, 현재 {len(df)}행"
            
        return True, "파일이 유효합니다."
    except Exception as e:
        return False, f"파일 읽기 오류: {str(e)}"

def execute_inference_and_get_results(command):
    """inference 실행하고 결과 파일을 읽어서 반환"""
    try:
        print(f"실행 명령어: {' '.join(command)}")
        result = subprocess.run(command, capture_output=True, text=True, timeout=300)

        if result.returncode != 0:
            error_message = (
                f"실행 실패 (Exit Code: {result.returncode}):\n\n"
                f"--- 에러 로그 ---\n{result.stderr}\n\n"
                f"--- 일반 출력 ---\n{result.stdout}"
            )
            raise gr.Error(error_message)
        
        return True, result.stdout
    except subprocess.TimeoutExpired:
        raise gr.Error("실행 시간이 초과되었습니다. (5분 제한)")
    except Exception as e:
        raise gr.Error(f"내부 오류: {str(e)}")

def calculate_final_tide(residual_predictions, station_id, last_time):
    """잔차 예측 + 조화 예측 = 최종 조위 계산"""
    if isinstance(last_time, pd.Timestamp):
        last_time = last_time.to_pydatetime()
    
    kst = pytz.timezone('Asia/Seoul')
    if last_time.tzinfo is None:
        last_time = kst.localize(last_time)
    
    start_time = last_time + timedelta(minutes=5)
    end_time = last_time + timedelta(minutes=72*5)
    
    harmonic_data = get_harmonic_predictions(station_id, start_time, end_time)
    
    residual_flat = residual_predictions.flatten()
    num_points = len(residual_flat)
    
    if not harmonic_data:
        print("조화 예측 데이터를 찾을 수 없습니다. 잔차 예측만 반환합니다.")
        return {
            'times': [last_time + timedelta(minutes=(i+1)*5) for i in range(num_points)],
            'residual': residual_flat.tolist(),
            'harmonic': [0.0] * num_points,
            'final_tide': residual_flat.tolist()
        }
    
    final_results = {
        'times': [],
        'residual': [],
        'harmonic': [],
        'final_tide': []
    }
    
    harmonic_dict = {}
    for h_data in harmonic_data:
        h_time_str = h_data['predicted_at']
        
        try:
            if 'T' in h_time_str:
                if h_time_str.endswith('Z'):
                    h_time = datetime.fromisoformat(h_time_str[:-1] + '+00:00')
                elif '+' in h_time_str or '-' in h_time_str[-6:]:
                    h_time = datetime.fromisoformat(h_time_str)
                else:
                    h_time = datetime.fromisoformat(h_time_str + '+00:00')
            else:
                from dateutil import parser
                h_time = parser.parse(h_time_str)
            
            if h_time.tzinfo is None:
                h_time = pytz.UTC.localize(h_time)
            h_time = h_time.astimezone(kst)
            
        except Exception as e:
            print(f"시간 파싱 오류: {h_time_str}, {e}")
            continue
        
        minutes = (h_time.minute // 5) * 5
        h_time = h_time.replace(minute=minutes, second=0, microsecond=0)
        harmonic_value = float(h_data['harmonic_level'])
        harmonic_dict[h_time] = harmonic_value
    
    for i, residual in enumerate(residual_flat):
        pred_time = last_time + timedelta(minutes=(i+1)*5)
        pred_time = pred_time.replace(second=0, microsecond=0)
        
        harmonic_value = harmonic_dict.get(pred_time, 0.0)
        
        if harmonic_value == 0.0 and harmonic_dict:
            min_diff = float('inf')
            for h_time, h_val in harmonic_dict.items():
                diff = abs((h_time - pred_time).total_seconds())
                if diff < min_diff and diff < 300:
                    min_diff = diff
                    harmonic_value = h_val
        
        final_tide = float(residual) + harmonic_value
        
        final_results['times'].append(pred_time)
        final_results['residual'].append(float(residual))
        final_results['harmonic'].append(harmonic_value)
        final_results['final_tide'].append(final_tide)
    
    return final_results

def create_enhanced_prediction_plot(prediction_results, input_data, station_name):
    """잔차 + 조화 + 최종 조위를 모두 표시하는 향상된 플롯"""
    try:
        input_df = pd.read_csv(input_data.name)
        input_df['date'] = pd.to_datetime(input_df['date'])
        
        recent_data = input_df.tail(24)
        future_times = pd.to_datetime(prediction_results['times'])
        
        fig = go.Figure()
        
        fig.add_trace(go.Scatter(
            x=recent_data['date'],
            y=recent_data['residual'],
            mode='lines+markers',
            name='실제 잔차조위',
            line=dict(color='blue', width=2),
            marker=dict(size=4)
        ))
        
        fig.add_trace(go.Scatter(
            x=future_times,
            y=prediction_results['residual'],
            mode='lines+markers',
            name='잔차 예측',
            line=dict(color='red', width=2, dash='dash'),
            marker=dict(size=3)
        ))
        
        if any(h != 0 for h in prediction_results['harmonic']):
            fig.add_trace(go.Scatter(
                x=future_times,
                y=prediction_results['harmonic'],
                mode='lines',
                name='조화 예측',
                line=dict(color='orange', width=2)
            ))
            
            fig.add_trace(go.Scatter(
                x=future_times,
                y=prediction_results['final_tide'],
                mode='lines+markers',
                name='최종 조위',
                line=dict(color='green', width=3),
                marker=dict(size=4)
            ))
        
        last_time = recent_data['date'].iloc[-1]
        
        fig.add_annotation(
            x=last_time,
            y=0,
            text="← 과거 | 미래 →",
            showarrow=False,
            yref="paper",
            yshift=10,
            font=dict(size=12, color="gray")
        )
        
        fig.update_layout(
            title=f'{station_name} 통합 조위 예측 결과',
            xaxis_title='시간',
            yaxis_title='수위 (cm)',
            hovermode='x unified',
            height=600,
            showlegend=True,
            xaxis=dict(tickformat='%H:%M<br>%m/%d', gridcolor='lightgray', showgrid=True),
            yaxis=dict(gridcolor='lightgray', showgrid=True),
            plot_bgcolor='white'
        )
        
        return fig
    except Exception as e:
        print(f"Enhanced plot creation error: {e}")
        traceback.print_exc()
        fig = go.Figure()
        fig.add_annotation(
            text=f"시각화 생성 중 오류: {str(e)}",
            xref="paper", yref="paper",
            x=0.5, y=0.5, showarrow=False
        )
        return fig

def single_prediction(station_id, input_csv_file):
    if input_csv_file is None: 
        raise gr.Error("예측을 위한 입력 파일을 업로드해주세요.")
    
    is_valid, message = validate_csv_file(input_csv_file.name)
    if not is_valid:
        raise gr.Error(f"파일 오류: {message}")
    
    station_name = STATION_NAMES.get(station_id, station_id)
    
    common_args = get_common_args(station_id)
    setting_name = f"long_term_forecast_{station_id}_144_72_TimeXer_TIDE_ftMS_sl144_ll96_pl72_dm256_nh8_el1_dl1_df512_expand2_dc4_fc3_ebtimeF_dtTrue_Exp_0"
    checkpoint_path = f"./checkpoints/{setting_name}/checkpoint.pth"
    scaler_path = f"./checkpoints/{setting_name}/scaler.gz"
    
    if not os.path.exists(checkpoint_path):
        raise gr.Error(f"모델 파일을 찾을 수 없습니다: {checkpoint_path}")
    if not os.path.exists(scaler_path):
        raise gr.Error(f"스케일러 파일을 찾을 수 없습니다: {scaler_path}")
    
    command = ["python", "inference.py", 
               "--checkpoint_path", checkpoint_path, 
               "--scaler_path", scaler_path, 
               "--predict_input_file", input_csv_file.name] + common_args
    
    gr.Info(f"{station_name}({station_id}) 통합 조위 예측을 실행중입니다...")
    
    success, output = execute_inference_and_get_results(command)
    
    try:
        prediction_file = "pred_results/prediction_future.npy"
        if os.path.exists(prediction_file):
            residual_predictions = np.load(prediction_file)
            
            input_df = pd.read_csv(input_csv_file.name)
            input_df['date'] = pd.to_datetime(input_df['date'])
            last_time = input_df['date'].iloc[-1]
            
            prediction_results = calculate_final_tide(residual_predictions, station_id, last_time)
            plot = create_enhanced_prediction_plot(prediction_results, input_csv_file, station_name)
            
            has_harmonic = any(h != 0 for h in prediction_results['harmonic'])
            
            if has_harmonic:
                result_df = pd.DataFrame({
                    '예측 시간': [t.strftime('%Y-%m-%d %H:%M') for t in prediction_results['times']],
                    '잔차 예측 (cm)': [f"{val:.2f}" for val in prediction_results['residual']],
                    '조화 예측 (cm)': [f"{val:.2f}" for val in prediction_results['harmonic']],
                    '최종 조위 (cm)': [f"{val:.2f}" for val in prediction_results['final_tide']]
                })
            else:
                result_df = pd.DataFrame({
                    '예측 시간': [t.strftime('%Y-%m-%d %H:%M') for t in prediction_results['times']],
                    '잔차 예측 (cm)': [f"{val:.2f}" for val in prediction_results['residual']]
                })
            
            saved_count = save_predictions_to_supabase(station_id, prediction_results)
            if saved_count > 0:
                save_message = f"\n💾 Supabase에 {saved_count}개 예측 결과 저장 완료!"
            elif get_supabase_client() is None:
                save_message = "\n⚠️ Supabase 연결 실패 (환경변수 확인 필요)"
            else:
                save_message = "\n⚠️ Supabase 저장 실패"
            
            return plot, result_df, f"✅ 예측 완료!{save_message}\n\n{output}"
        else:
            return None, None, f"❌ 결과 파일을 찾을 수 없습니다.\n\n{output}"
    except Exception as e:
        print(f"Result processing error: {e}")
        traceback.print_exc()
        return None, None, f"❌ 결과 처리 중 오류: {str(e)}\n\n{output}"