File size: 34,472 Bytes
1e44df5
6abcf96
 
 
 
 
 
 
 
 
 
 
 
 
1e44df5
 
 
 
 
 
9468667
6abcf96
9468667
 
 
1e44df5
 
 
6abcf96
1e44df5
6abcf96
1e44df5
6abcf96
1e44df5
6abcf96
1e44df5
 
9468667
 
1e44df5
 
6abcf96
1e44df5
6abcf96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e44df5
6abcf96
 
 
 
 
 
 
 
 
 
 
 
 
1e44df5
9468667
 
 
1e44df5
6abcf96
1e44df5
 
 
6abcf96
 
1e44df5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9468667
 
 
1e44df5
 
 
 
6abcf96
1e44df5
 
9468667
1e44df5
6abcf96
1e44df5
 
6abcf96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e44df5
 
 
9468667
1e44df5
 
 
 
 
 
 
 
 
 
9468667
1e44df5
 
 
 
 
 
 
 
 
 
 
 
 
 
6abcf96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9468667
 
6abcf96
 
9468667
6abcf96
 
 
9468667
 
 
 
6abcf96
 
 
 
 
 
 
 
 
 
 
 
 
9468667
 
 
6abcf96
9468667
6abcf96
9468667
 
6abcf96
9468667
6abcf96
 
 
 
 
 
 
 
 
 
 
 
9468667
 
 
6abcf96
9468667
6abcf96
 
 
 
 
 
 
 
9468667
6abcf96
 
9468667
6abcf96
 
 
 
9468667
 
 
6abcf96
9468667
6abcf96
9468667
 
 
6abcf96
 
9468667
 
6abcf96
 
9468667
6abcf96
 
9468667
 
6abcf96
9468667
 
6abcf96
9468667
 
 
 
6abcf96
 
 
 
9468667
6abcf96
9468667
 
 
 
 
6abcf96
 
 
 
 
9468667
6abcf96
 
 
 
9468667
 
 
6abcf96
9468667
6abcf96
9468667
6abcf96
 
9468667
 
6abcf96
 
9468667
6abcf96
 
9468667
 
6abcf96
9468667
 
6abcf96
9468667
 
 
 
6abcf96
 
 
 
9468667
6abcf96
9468667
 
 
 
 
6abcf96
 
 
 
 
9468667
6abcf96
 
 
 
9468667
 
 
6abcf96
9468667
6abcf96
9468667
6abcf96
 
 
 
 
9468667
6abcf96
9468667
6abcf96
 
 
9468667
 
 
 
6abcf96
 
 
 
 
9468667
6abcf96
 
 
 
9468667
 
 
 
6abcf96
 
 
9468667
6abcf96
9468667
 
6abcf96
9468667
 
 
 
 
 
 
 
 
 
1e44df5
 
 
 
 
6abcf96
1e44df5
 
6abcf96
9468667
6abcf96
 
 
 
 
 
 
 
 
 
 
 
1e44df5
 
 
 
6abcf96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e44df5
 
9468667
6abcf96
 
 
 
 
1e44df5
6abcf96
 
 
 
 
 
 
 
 
 
 
 
1e44df5
6abcf96
 
 
 
9468667
 
 
 
6abcf96
 
9468667
6abcf96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9468667
 
 
 
6abcf96
9468667
 
 
6abcf96
 
 
 
 
9468667
 
 
 
 
 
 
6abcf96
9468667
 
 
 
 
 
 
6abcf96
 
 
 
9468667
 
 
b48149a
 
 
 
 
 
 
 
9468667
 
6abcf96
9468667
1e44df5
9468667
 
1e44df5
9468667
 
 
 
6abcf96
 
9468667
 
6abcf96
 
9468667
 
 
 
 
 
b48149a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9468667
 
 
6abcf96
 
9468667
 
6abcf96
 
9468667
 
 
 
 
 
1e44df5
6abcf96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9468667
6abcf96
 
fdb0281
167386a
 
 
 
fdb0281
 
 
 
167386a
 
 
 
fdb0281
 
167386a
 
 
 
ecb5195
 
fdb0281
 
 
6abcf96
fdb0281
 
6abcf96
fdb0281
ecb5195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb0281
ecb5195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb0281
6abcf96
fdb0281
 
 
 
6abcf96
fdb0281
 
 
 
6abcf96
fdb0281
 
 
6abcf96
 
 
 
 
 
 
 
fdb0281
 
 
 
 
 
6abcf96
fdb0281
 
1e44df5
 
 
 
6abcf96
1e44df5
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
"""
AGROW Time Series Service v2.1
==============================
Production-ready prediction pipeline with:
- Persistent CSV storage per field
- Detailed structured logging
- CSV download endpoints
- Idempotent processing

CSVs Generated per field:
1. sar_data.csv - Historical SAR data
2. sentinel2_data.csv - Historical Sentinel-2 data
3. sar_predictions.csv - SAR forecasts
4. sentinel2_predictions.csv - S2 forecasts
"""

import os
import json
import logging
import traceback
import threading
import uuid
from datetime import datetime
from typing import List, Optional, Dict, Any, Tuple
from concurrent.futures import ThreadPoolExecutor

import numpy as np
import pandas as pd
from fastapi import FastAPI, HTTPException, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, StreamingResponse
from pydantic import BaseModel
import io

# Import modules
from satellite_pipeline import SatelliteFetcher
from auto_tuning_predictor import AutoTimeSeriesPredictor
from storage import FieldStorage, JobStatus
from index_calculator import IndexCalculator

# ============================================================================
# LOGGING CONFIGURATION (Industry Standard)
# ============================================================================
class StructuredFormatter(logging.Formatter):
    """Structured logging formatter for production."""
    def format(self, record):
        log_data = {
            "timestamp": datetime.utcnow().isoformat(),
            "level": record.levelname,
            "logger": record.name,
            "message": record.getMessage(),
        }
        if hasattr(record, 'field_hash'):
            log_data['field_hash'] = record.field_hash
        if hasattr(record, 'step'):
            log_data['step'] = record.step
        if hasattr(record, 'duration_ms'):
            log_data['duration_ms'] = record.duration_ms
        if record.exc_info:
            log_data['exception'] = self.formatException(record.exc_info)
        return json.dumps(log_data)

# Setup logging
logger = logging.getLogger("TimeSeriesService")
logger.setLevel(logging.INFO)

# Console handler with structured format
console_handler = logging.StreamHandler()
console_handler.setFormatter(StructuredFormatter())
logger.addHandler(console_handler)

# File handler for debugging
file_handler = logging.FileHandler('timeseries.log')
file_handler.setFormatter(logging.Formatter(
    '[%(asctime)s] %(levelname)s [%(name)s] %(message)s'
))
logger.addHandler(file_handler)

# Thread pool for background processing
executor = ThreadPoolExecutor(max_workers=2)

# ============================================================================
# FASTAPI APP
# ============================================================================
app = FastAPI(
    title="AGROW Time Series Service",
    description="Production-ready prediction pipeline with CSV storage",
    version="2.1.0"
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ============================================================================
# REQUEST/RESPONSE MODELS
# ============================================================================
class DataPoint(BaseModel):
    date: str
    value: float

class ForecastPoint(BaseModel):
    date: str
    value: float
    confidence_low: Optional[float] = None
    confidence_high: Optional[float] = None

class TimeSeriesRequest(BaseModel):
    center_lat: float
    center_lon: float
    field_size_hectares: float = 10.0
    metric: str = "VV"
    days_history: int = 365
    days_forecast: int = 30

class TimeSeriesResponse(BaseModel):
    success: bool
    metric: str
    field_hash: str
    historical: List[DataPoint]
    forecast: List[ForecastPoint]
    trend: str
    stats: Dict[str, float]
    csv_files: Dict[str, str]
    timestamp: str

class PredictRequest(BaseModel):
    polygon_coords: List[List[float]]
    field_name: Optional[str] = None

class PredictResponse(BaseModel):
    job_id: str
    field_hash: str
    status: str
    message: str
    csv_files: Optional[Dict[str, str]] = None
    created_at: Optional[str] = None

class StatusResponse(BaseModel):
    field_hash: str
    status: str
    progress: int
    step: str
    message: Optional[str] = None
    csv_files: Optional[Dict[str, str]] = None
    created_at: Optional[str] = None
    completed_at: Optional[str] = None

# ============================================================================
# HELPERS
# ============================================================================
def coords_to_polygon(center_lat: float, center_lon: float, size_ha: float) -> List[Tuple[float, float]]:
    """Convert center point and size to polygon coordinates."""
    radius_km = np.sqrt(size_ha / 100) / 2
    lat_off = radius_km / 111
    lon_off = radius_km / (111 * np.cos(np.radians(center_lat)))
    
    return [
        (center_lon - lon_off, center_lat - lat_off),
        (center_lon + lon_off, center_lat - lat_off),
        (center_lon + lon_off, center_lat + lat_off),
        (center_lon - lon_off, center_lat + lat_off),
        (center_lon - lon_off, center_lat - lat_off),
    ]

def calculate_trend(values: List[float]) -> str:
    """Determine trend from values."""
    if len(values) < 5:
        return "stable"
    x = np.arange(len(values))
    slope = np.polyfit(x, values, 1)[0]
    if slope > 0.01:
        return "improving"
    elif slope < -0.01:
        return "declining"
    return "stable"

def get_csv_urls(field_hash: str) -> Dict[str, str]:
    """Get download URLs for all 4 CSV files."""
    base = f"/download/{field_hash}"
    return {
        "sar_historical": f"{base}/sar_data.csv",
        "sentinel2_historical": f"{base}/sentinel2_data.csv",
        "sar_predictions": f"{base}/sar_predictions.csv",
        "sentinel2_predictions": f"{base}/sentinel2_predictions.csv",
        "indices": f"{base}/indices.csv"
    }

def log_step(field_hash: str, step: str, message: str, level: str = "INFO"):
    """Helper for structured step logging."""
    extra = {'field_hash': field_hash, 'step': step}
    if level == "ERROR":
        logger.error(f"[{field_hash}] {step}: {message}", extra=extra)
    else:
        logger.info(f"[{field_hash}] {step}: {message}", extra=extra)

# ============================================================================
# PREDICTION JOB
# ============================================================================
def run_prediction_job(field_hash: str, polygon_coords: List[Tuple[float, float]], field_name: str):
    """
    Production prediction job with detailed logging.
    Generates 4 named CSVs + indices.
    """
    start_time = datetime.now()
    log_step(field_hash, "START", f"Beginning prediction pipeline for {field_name}")
    
    try:
        field_dir = FieldStorage.get_field_dir(field_hash)
        os.makedirs(field_dir, exist_ok=True)
        
        # CSV file paths with descriptive names
        csv_files = {
            "sar_data": os.path.join(field_dir, "sar_data.csv"),
            "sentinel2_data": os.path.join(field_dir, "sentinel2_data.csv"),
            "sar_predictions": os.path.join(field_dir, "sar_predictions.csv"),
            "sentinel2_predictions": os.path.join(field_dir, "sentinel2_predictions.csv"),
            "indices": os.path.join(field_dir, "indices.csv")
        }
        
        # =====================
        # STEP 1: Fetch SAR Data
        # =====================
        step_start = datetime.now()
        FieldStorage.update_metadata(field_hash, 
            status=JobStatus.FETCHING_SAR,
            progress=10,
            step="Fetching SAR (Sentinel-1) data..."
        )
        log_step(field_hash, "FETCH_SAR", "Starting SAR data acquisition from Sentinel Hub")
        
        fetcher = SatelliteFetcher(polygon_coords)
        fetcher.fetch_sar_data(csv_files["sar_data"])
        
        sar_rows = 0
        if os.path.exists(csv_files["sar_data"]):
            sar_df = pd.read_csv(csv_files["sar_data"])
            sar_rows = len(sar_df)
        
        duration = (datetime.now() - step_start).total_seconds() * 1000
        log_step(field_hash, "FETCH_SAR", f"SAR data fetched: {sar_rows} rows in {duration:.0f}ms")
        
        # =====================
        # STEP 2: Fetch Sentinel-2 Data
        # =====================
        step_start = datetime.now()
        FieldStorage.update_metadata(field_hash,
            status=JobStatus.FETCHING_S2,
            progress=25,
            step="Fetching Sentinel-2 optical data..."
        )
        log_step(field_hash, "FETCH_S2", "Starting Sentinel-2 optical data acquisition")
        
        fetcher.fetch_sentinel2_data(csv_files["sentinel2_data"])
        
        s2_rows = 0
        if os.path.exists(csv_files["sentinel2_data"]):
            s2_df = pd.read_csv(csv_files["sentinel2_data"])
            s2_rows = len(s2_df)
        
        duration = (datetime.now() - step_start).total_seconds() * 1000
        log_step(field_hash, "FETCH_S2", f"Sentinel-2 data fetched: {s2_rows} rows in {duration:.0f}ms")
        
        # =====================
        # STEP 3: SAR Predictions
        # =====================
        step_start = datetime.now()
        FieldStorage.update_metadata(field_hash,
            status=JobStatus.PREDICTING_SAR,
            progress=40,
            step="Running AutoNHITS model on SAR bands..."
        )
        log_step(field_hash, "PREDICT_SAR", "Initializing AutoNHITS predictor for SAR")
        
        predictor = AutoTimeSeriesPredictor()
        
        if os.path.exists(csv_files["sar_data"]):
            sar_df = pd.read_csv(csv_files["sar_data"])
            if 'ds' in sar_df.columns:
                target_cols = [c for c in sar_df.columns if c != 'ds']
                log_step(field_hash, "PREDICT_SAR", f"Processing {len(target_cols)} SAR bands: {target_cols}")
                
                all_preds = []
                for idx, col in enumerate(target_cols):
                    log_step(field_hash, "PREDICT_SAR", f"Predicting band {idx+1}/{len(target_cols)}: {col}")
                    try:
                        pred_df = predictor.tune_and_predict(
                            csv_path=csv_files["sar_data"],
                            field_coords=polygon_coords,
                            target_col=col,
                            output_file=f"temp_sar_{col}.csv",
                            num_samples=3
                        )
                        pred_df = pred_df.rename(columns={'predicted_y': col})
                        all_preds.append(pred_df[['ds', col]])
                        
                        # Cleanup temp file
                        if os.path.exists(f"temp_sar_{col}.csv"):
                            os.remove(f"temp_sar_{col}.csv")
                    except Exception as e:
                        log_step(field_hash, "PREDICT_SAR", f"Failed to predict {col}: {e}", "ERROR")
                
                if all_preds:
                    final_df = all_preds[0]
                    for i in range(1, len(all_preds)):
                        final_df = final_df.merge(all_preds[i], on='ds', how='outer')
                    final_df.to_csv(csv_files["sar_predictions"], index=False)
                    log_step(field_hash, "PREDICT_SAR", f"SAR predictions saved: {len(final_df)} rows")
        
        duration = (datetime.now() - step_start).total_seconds() * 1000
        log_step(field_hash, "PREDICT_SAR", f"SAR prediction complete in {duration:.0f}ms")
        
        # =====================
        # STEP 4: Sentinel-2 Predictions
        # =====================
        step_start = datetime.now()
        FieldStorage.update_metadata(field_hash,
            status=JobStatus.PREDICTING_S2,
            progress=60,
            step="Running AutoNHITS model on optical bands..."
        )
        log_step(field_hash, "PREDICT_S2", "Initializing AutoNHITS predictor for Sentinel-2")
        
        if os.path.exists(csv_files["sentinel2_data"]):
            s2_df = pd.read_csv(csv_files["sentinel2_data"])
            if 'ds' in s2_df.columns:
                target_cols = [c for c in s2_df.columns if c != 'ds']
                log_step(field_hash, "PREDICT_S2", f"Processing {len(target_cols)} optical bands: {target_cols}")
                
                all_preds = []
                for idx, col in enumerate(target_cols):
                    log_step(field_hash, "PREDICT_S2", f"Predicting band {idx+1}/{len(target_cols)}: {col}")
                    try:
                        pred_df = predictor.tune_and_predict(
                            csv_path=csv_files["sentinel2_data"],
                            field_coords=polygon_coords,
                            target_col=col,
                            output_file=f"temp_s2_{col}.csv",
                            num_samples=3
                        )
                        pred_df = pred_df.rename(columns={'predicted_y': col})
                        all_preds.append(pred_df[['ds', col]])
                        
                        # Cleanup temp file
                        if os.path.exists(f"temp_s2_{col}.csv"):
                            os.remove(f"temp_s2_{col}.csv")
                    except Exception as e:
                        log_step(field_hash, "PREDICT_S2", f"Failed to predict {col}: {e}", "ERROR")
                
                if all_preds:
                    final_df = all_preds[0]
                    for i in range(1, len(all_preds)):
                        final_df = final_df.merge(all_preds[i], on='ds', how='outer')
                    final_df.to_csv(csv_files["sentinel2_predictions"], index=False)
                    log_step(field_hash, "PREDICT_S2", f"S2 predictions saved: {len(final_df)} rows")
        
        duration = (datetime.now() - step_start).total_seconds() * 1000
        log_step(field_hash, "PREDICT_S2", f"S2 prediction complete in {duration:.0f}ms")
        
        # =====================
        # STEP 5: Compute Indices
        # =====================
        step_start = datetime.now()
        FieldStorage.update_metadata(field_hash,
            status=JobStatus.COMPUTING_INDICES,
            progress=85,
            step="Computing vegetation indices (NDVI, NDWI, EVI, etc.)..."
        )
        log_step(field_hash, "INDICES", "Computing vegetation indices from bands")
        
        if os.path.exists(csv_files["sentinel2_data"]):
            s2_df = pd.read_csv(csv_files["sentinel2_data"])
            sar_df = pd.read_csv(csv_files["sar_data"]) if os.path.exists(csv_files["sar_data"]) else None
            
            # Historical indices
            indices_df = IndexCalculator.compute_all_indices(s2_df, sar_df)
            indices_df['type'] = 'historical'
            
            # Prediction indices
            if os.path.exists(csv_files["sentinel2_predictions"]):
                s2_pred_df = pd.read_csv(csv_files["sentinel2_predictions"])
                pred_indices = IndexCalculator.compute_all_indices(s2_pred_df, None)
                pred_indices['type'] = 'forecast'
                indices_df = pd.concat([indices_df, pred_indices], ignore_index=True)
            
            indices_df.to_csv(csv_files["indices"], index=False)
            log_step(field_hash, "INDICES", f"Indices computed: {len(indices_df)} rows, columns: {list(indices_df.columns)}")
        
        duration = (datetime.now() - step_start).total_seconds() * 1000
        log_step(field_hash, "INDICES", f"Index computation complete in {duration:.0f}ms")
        
        # =====================
        # COMPLETE
        # =====================
        total_duration = (datetime.now() - start_time).total_seconds()
        FieldStorage.update_metadata(field_hash,
            status=JobStatus.COMPLETE,
            progress=100,
            step="Complete",
            csv_files=get_csv_urls(field_hash),
            completed_at=datetime.now().isoformat(),
            duration_seconds=total_duration
        )
        log_step(field_hash, "COMPLETE", f"Pipeline finished successfully in {total_duration:.1f}s")
        
    except Exception as e:
        log_step(field_hash, "ERROR", f"Pipeline failed: {str(e)}", "ERROR")
        logger.error(traceback.format_exc())
        FieldStorage.update_metadata(field_hash,
            status=JobStatus.ERROR,
            progress=0,
            step="Error",
            error=str(e)
        )
    finally:
        FieldStorage.release_lock(field_hash)

# ============================================================================
# API ENDPOINTS
# ============================================================================
@app.get("/")
async def root():
    logger.info("Root endpoint accessed")
    return {
        "service": "AGROW Time Series Service",
        "version": "2.1.0",
        "endpoints": {
            "/timeseries": "POST - Get time series with predictions",
            "/predict": "POST - Start full prediction job",
            "/predict/status/{hash}": "GET - Check job status",
            "/download/{hash}/{file}": "GET - Download CSV file"
        },
        "csv_files": [
            "sar_data.csv - Historical Sentinel-1 SAR data",
            "sentinel2_data.csv - Historical Sentinel-2 optical data",
            "sar_predictions.csv - SAR band predictions",
            "sentinel2_predictions.csv - Optical band predictions",
            "indices.csv - Computed vegetation indices"
        ]
    }

@app.get("/health")
async def health():
    return {"status": "healthy", "version": "2.1.0"}


@app.get("/download/{field_hash}/{filename}")
async def download_csv(field_hash: str, filename: str):
    """Download a specific CSV file for a field."""
    logger.info(f"Download request: {field_hash}/{filename}")
    
    valid_files = ["sar_data.csv", "sentinel2_data.csv", "sar_predictions.csv", 
                   "sentinel2_predictions.csv", "indices.csv"]
    
    if filename not in valid_files:
        raise HTTPException(400, f"Invalid filename. Valid files: {valid_files}")
    
    file_path = os.path.join(FieldStorage.get_field_dir(field_hash), filename)
    
    if not os.path.exists(file_path):
        raise HTTPException(404, f"File not found: {filename}")
    
    return FileResponse(
        file_path,
        media_type="text/csv",
        filename=f"{field_hash}_{filename}"
    )


@app.post("/predict", response_model=PredictResponse)
async def start_prediction(request: PredictRequest):
    """Start prediction pipeline for coordinates."""
    polygon = [(coord[0], coord[1]) for coord in request.polygon_coords]
    field_hash = FieldStorage.get_field_hash(polygon)
    field_name = request.field_name or f"Field_{field_hash[:6]}"
    
    logger.info(f"Prediction request: {field_name} (hash: {field_hash})")
    
    # Check if already complete
    if FieldStorage.field_exists(field_hash):
        metadata = FieldStorage.get_metadata(field_hash)
        return PredictResponse(
            job_id=field_hash,
            field_hash=field_hash,
            status="complete",
            message="Data ready. Use download endpoints to get CSV files.",
            csv_files=get_csv_urls(field_hash),
            created_at=metadata.get("created_at")
        )
    
    # Check if job is running
    if FieldStorage.is_locked(field_hash):
        metadata = FieldStorage.get_metadata(field_hash) or {}
        return PredictResponse(
            job_id=field_hash,
            field_hash=field_hash,
            status="processing",
            message=f"Job running: {metadata.get('step', 'Processing...')}",
            created_at=metadata.get("created_at")
        )
    
    # Acquire lock and start job
    if not FieldStorage.acquire_lock(field_hash):
        return PredictResponse(
            job_id=field_hash,
            field_hash=field_hash,
            status="processing",
            message="Job starting..."
        )
    
    FieldStorage.update_metadata(field_hash,
        field_name=field_name,
        polygon_coords=polygon,
        status=JobStatus.PENDING,
        progress=0,
        step="Initializing...",
        created_at=datetime.now().isoformat()
    )
    
    executor.submit(run_prediction_job, field_hash, polygon, field_name)
    
    return PredictResponse(
        job_id=field_hash,
        field_hash=field_hash,
        status="processing",
        message="Prediction job started. Poll /predict/status/{hash} for progress.",
        created_at=datetime.now().isoformat()
    )


@app.get("/predict/status/{field_hash}", response_model=StatusResponse)
async def get_status(field_hash: str):
    """Get job status with CSV file links."""
    metadata = FieldStorage.get_metadata(field_hash)
    
    if not metadata:
        raise HTTPException(404, f"No job found: {field_hash}")
    
    csv_files = None
    if metadata.get("status") == JobStatus.COMPLETE:
        csv_files = get_csv_urls(field_hash)
    
    return StatusResponse(
        field_hash=field_hash,
        status=metadata.get("status", "unknown"),
        progress=metadata.get("progress", 0),
        step=metadata.get("step", "Unknown"),
        message=metadata.get("error"),
        csv_files=csv_files,
        created_at=metadata.get("created_at"),
        completed_at=metadata.get("completed_at")
    )


@app.post("/timeseries", response_model=TimeSeriesResponse)
async def get_timeseries(request: TimeSeriesRequest):
    """Get time series with predictions for a single metric."""
    req_id = uuid.uuid4().hex[:8]
    logger.info(f"[{req_id}] TimeSeries request: {request.metric} at ({request.center_lat}, {request.center_lon})")
    
    polygon = coords_to_polygon(request.center_lat, request.center_lon, request.field_size_hectares)
    field_hash = FieldStorage.get_field_hash(polygon)
    
    # Computed vegetation indices and their required bands
    COMPUTED_INDICES = {
        'NDVI': {'bands': ['B08', 'B04'], 'formula': lambda b08, b04: (b08 - b04) / (b08 + b04) if (b08 + b04) != 0 else 0},
        'NDRE': {'bands': ['B08', 'B05'], 'formula': lambda b08, b05: (b08 - b05) / (b08 + b05) if (b08 + b05) != 0 else 0},
        'PRI':  {'bands': ['B03', 'B04'], 'formula': lambda b03, b04: (b03 - b04) / (b03 + b04) if (b03 + b04) != 0 else 0},
        'EVI':  {'bands': ['B08', 'B04', 'B02'], 'formula': lambda b08, b04, b02: 2.5 * (b08 - b04) / (b08 + 6 * b04 - 7.5 * b02 + 1) if (b08 + 6 * b04 - 7.5 * b02 + 1) != 0 else 0},
    }
    
    # Check cache first
    if FieldStorage.field_exists(field_hash):
        logger.info(f"[{req_id}] Using cached data for {field_hash}")
        data = FieldStorage.get_all_data(field_hash)
        
        historical = []
        forecast = []
        
        if request.metric in ['VV', 'VH']:
            col = f"{request.metric}_mean_dB"
            if data.get("sar_data"):
                for row in data["sar_data"]:
                    if col in row and row[col] is not None:
                        historical.append(DataPoint(date=str(row['ds']), value=round(float(row[col]), 4)))
            if data.get("sar_predictions"):
                for row in data["sar_predictions"]:
                    if col in row and row[col] is not None:
                        value = round(float(row[col]), 4)
                        forecast.append(ForecastPoint(
                            date=str(row['ds']),
                            value=value,
                            confidence_low=round(value * 0.9, 4),
                            confidence_high=round(value * 1.1, 4)
                        ))
        
        # Handle computed vegetation indices
        elif request.metric in COMPUTED_INDICES:
            index_info = COMPUTED_INDICES[request.metric]
            bands = index_info['bands']
            formula = index_info['formula']
            
            logger.info(f"[{req_id}] Computing {request.metric} from bands: {bands}")
            
            # Compute from historical data
            if data.get("sentinel2_data"):
                for row in data["sentinel2_data"]:
                    # Check if all required bands are present
                    if all(b in row and row[b] is not None for b in bands):
                        try:
                            band_values = [float(row[b]) for b in bands]
                            computed_value = formula(*band_values)
                            # Clamp to valid range
                            computed_value = max(-1.0, min(1.0, computed_value))
                            historical.append(DataPoint(date=str(row['ds']), value=round(computed_value, 4)))
                        except (ValueError, ZeroDivisionError):
                            pass
            
            # Compute from prediction data
            if data.get("sentinel2_predictions"):
                for row in data["sentinel2_predictions"]:
                    if all(b in row and row[b] is not None for b in bands):
                        try:
                            band_values = [float(row[b]) for b in bands]
                            computed_value = formula(*band_values)
                            computed_value = max(-1.0, min(1.0, computed_value))
                            forecast.append(ForecastPoint(
                                date=str(row['ds']),
                                value=round(computed_value, 4),
                                confidence_low=round(computed_value * 0.9, 4),
                                confidence_high=round(computed_value * 1.1, 4)
                            ))
                        except (ValueError, ZeroDivisionError):
                            pass
        
        # Raw Sentinel-2 bands
        else:
            if data.get("sentinel2_data"):
                for row in data["sentinel2_data"]:
                    if request.metric in row and row[request.metric] is not None:
                        historical.append(DataPoint(date=str(row['ds']), value=round(float(row[request.metric]), 4)))
            if data.get("sentinel2_predictions"):
                for row in data["sentinel2_predictions"]:
                    if request.metric in row and row[request.metric] is not None:
                        value = round(float(row[request.metric]), 4)
                        forecast.append(ForecastPoint(
                            date=str(row['ds']),
                            value=value,
                            confidence_low=round(value * 0.9, 4),
                            confidence_high=round(value * 1.1, 4)
                        ))
        
        if historical:
            all_values = [p.value for p in historical]
            return TimeSeriesResponse(
                success=True,
                metric=request.metric,
                field_hash=field_hash,
                historical=historical,
                forecast=forecast,
                trend=calculate_trend(all_values),
                stats={
                    "min": round(min(all_values), 4),
                    "max": round(max(all_values), 4),
                    "mean": round(sum(all_values) / len(all_values), 4),
                    "count": len(historical),
                    "forecast_count": len(forecast)
                },
                csv_files=get_csv_urls(field_hash),
                timestamp=datetime.now().isoformat()
            )
    
    # No cache - run on-demand
    logger.info(f"[{req_id}] No cache, running on-demand prediction for {field_hash}")
    
    # Create field-specific directory for on-demand data
    field_dir = FieldStorage.get_field_dir(field_hash)
    os.makedirs(field_dir, exist_ok=True)
    
    try:
        fetcher = SatelliteFetcher(polygon)
        
        if request.metric in ['VV', 'VH']:
            csv_file = os.path.join(field_dir, 'sar_data.csv')
            # Only fetch if file doesn't exist (avoid race condition)
            if not os.path.exists(csv_file):
                fetcher.fetch_sar_data(csv_file)
            target_col = f'{request.metric}_mean_dB'
        else:
            csv_file = os.path.join(field_dir, 'sentinel2_data.csv')
            # Only fetch if file doesn't exist (avoid race condition)
            if not os.path.exists(csv_file):
                fetcher.fetch_sentinel2_data(csv_file)
            # For computed indices, we'll compute them after loading the data
            # Raw bands can use direct column name
            target_col = request.metric
        
        if not os.path.exists(csv_file):
            raise HTTPException(404, "No satellite data available")
        
        df = pd.read_csv(csv_file)
        logger.info(f"[{req_id}] Loaded {len(df)} rows from {csv_file}")
        
        # Handle computed vegetation indices
        if request.metric in COMPUTED_INDICES:
            index_info = COMPUTED_INDICES[request.metric]
            bands = index_info['bands']
            formula = index_info['formula']
            
            logger.info(f"[{req_id}] Computing {request.metric} from bands: {bands}")
            
            # Check all required bands exist
            missing_bands = [b for b in bands if b not in df.columns]
            if missing_bands:
                raise HTTPException(400, f"Missing bands for {request.metric}: {missing_bands}")
            
            # Compute the index for each row
            computed_values = []
            for _, row in df.iterrows():
                if all(pd.notna(row[b]) for b in bands):
                    try:
                        band_values = [float(row[b]) for b in bands]
                        computed_value = formula(*band_values)
                        computed_value = max(-1.0, min(1.0, computed_value))  # Clamp
                        computed_values.append({
                            'ds': row['ds'],
                            'value': round(computed_value, 4)
                        })
                    except (ValueError, ZeroDivisionError):
                        pass
            
            if len(computed_values) < 10:
                raise HTTPException(400, f"Insufficient data after computing {request.metric}: {len(computed_values)} points")
            
            historical = [
                DataPoint(date=str(v['ds']), value=v['value'])
                for v in computed_values
            ]
            
            # For forecast, return simplified prediction based on recent trend
            # (Full AutoNHITS on computed indices is complex, use last 30 days average)
            recent_values = [v['value'] for v in computed_values[-30:]]
            avg_value = sum(recent_values) / len(recent_values) if recent_values else 0
            
            forecast = []
            from datetime import timedelta
            last_date = pd.to_datetime(computed_values[-1]['ds'])
            for i in range(1, 31):
                future_date = last_date + timedelta(days=i)
                # Simple trend continuation with slight variation
                variation = 0.02 * (i / 30)  # Increasing uncertainty over time
                forecast.append(ForecastPoint(
                    date=str(future_date.date()),
                    value=round(avg_value, 4),
                    confidence_low=round(avg_value - variation, 4),
                    confidence_high=round(avg_value + variation, 4)
                ))
            
            all_values = [p.value for p in historical]
            
        else:
            # Raw band/metric - use direct column
            historical = []
            for _, row in df.iterrows():
                if target_col in row and not pd.isna(row[target_col]):
                    historical.append(DataPoint(
                        date=str(row['ds']),
                        value=round(float(row[target_col]), 4)
                    ))
            
            if len(historical) < 10:
                raise HTTPException(400, f"Insufficient data: {len(historical)} points")
            
            logger.info(f"[{req_id}] Running AutoNHITS prediction...")
            predictor = AutoTimeSeriesPredictor()
            
            predictions = predictor.tune_and_predict(
                csv_path=csv_file,
                field_coords=polygon,
                target_col=target_col,
                output_file='predictions.csv',
                num_samples=3
            )
            
            forecast = []
            for _, row in predictions.iterrows():
                value = float(row['predicted_y'])
                forecast.append(ForecastPoint(
                    date=str(row['ds'].date()) if hasattr(row['ds'], 'date') else str(row['ds']),
                    value=round(value, 4),
                    confidence_low=round(value * 0.9, 4),
                    confidence_high=round(value * 1.1, 4)
                ))
            
            all_values = [p.value for p in historical]
        
        # Cleanup
        for f in ['sar_data.csv', 'sentinel2_data.csv', 'predictions.csv']:
            if os.path.exists(f):
                os.remove(f)
        
        logger.info(f"[{req_id}] Success: {len(historical)} historical, {len(forecast)} forecast points")
        
        return TimeSeriesResponse(
            success=True,
            metric=request.metric,
            field_hash=field_hash,
            historical=historical,
            forecast=forecast,
            trend=calculate_trend(all_values[-20:] if len(all_values) > 20 else all_values),
            stats={
                "min": round(min(all_values), 4),
                "max": round(max(all_values), 4),
                "mean": round(sum(all_values) / len(all_values), 4),
                "count": len(all_values),
                "forecast_count": len(forecast)
            },
            csv_files={},
            timestamp=datetime.now().isoformat()
        )
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"[{req_id}] Error: {str(e)}")
        logger.error(traceback.format_exc())
        raise HTTPException(500, str(e))


if __name__ == "__main__":
    import uvicorn
    logger.info("Starting AGROW Time Series Service v2.1.0")
    uvicorn.run(app, host="0.0.0.0", port=7860)