File size: 3,273 Bytes
fab54be
 
7db73fd
 
 
 
 
 
 
 
 
fab54be
 
 
 
 
 
 
 
 
 
 
 
 
ccc7737
fab54be
 
 
 
 
 
7db73fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fab54be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccc7737
fab54be
 
 
 
 
 
 
 
 
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
import os
import torch
import logging

# Configure structured enterprise logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s [%(levelname)s] %(name)s: %(message)s',
    handlers=[logging.StreamHandler()]
)
logger = logging.getLogger("SatVision")

CHECKPOINT_PATH = os.environ.get("CHECKPOINT_PATH", "./models/SatVision_Model.ckpt")
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DOWNLOADS_DIR = os.environ.get("DOWNLOADS_DIR", os.path.join(BASE_DIR, "server_downloads"))
os.makedirs(DOWNLOADS_DIR, exist_ok=True)

MONGO_URI = os.environ.get("MONGO_URI", "mongodb://localhost:27017/")
MONGO_DB_NAME = "satvision_db"
MONGO_COLLECTION_NAME = "generations"

EE_CREDENTIALS_JSON = os.environ.get("EE_CREDENTIALS")
EE_PROJECT_ID = os.environ.get("EE_PROJECT_ID", "gen-lang-client-0114614261")

GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
GEMINI_MODEL_ID = "gemini-2.5-flash"
GEMINI_TEMPERATURE = 0.1
GEMINI_TOP_P = 0.9

def validate_environment():
    """Validates required API keys and credentials at system startup."""
    missing = []
    if not EE_CREDENTIALS_JSON:
        missing.append("EE_CREDENTIALS")
    if not GEMINI_API_KEY:
        missing.append("GEMINI_API_KEY")
    
    if missing:
        logger.warning(
            f"⚠️  [Startup Security Audit] Missing required environment variables: {', '.join(missing)}. "
            "Google Earth Engine downloads and Gemini report generation will fail in runtime if not configured."
        )
    else:
        logger.info("✅ [Startup Security Audit] All core environmental secrets verified successfully.")


S2_CLOUD_THRESHOLD = 35
OPTICAL_CLEAR_THRESHOLD = 85.0
GEE_SCALE_OPTICAL = 100
GEE_REQUEST_TIMEOUT = 240  # Increased timeout limit to protect massive calculations

# Dynamic Tile Segmentation Rules based on Coordinate Span Degrees
MAX_DEG_STEP_XLARGE = 0.80
MAX_DEG_STEP_LARGE = 0.40
MAX_DEG_STEP_SMALL = 0.08

# Meter Resolution Adjustments (Higher numbers = rougher zoom but significantly faster)
SCALE_XLARGE = 90   # For presets like Lake Manchar / Dadu
SCALE_LARGE = 40    # For regional maps
SCALE_SMALL = 10    # For highly focused cities

TILE_SIZE = 256
TILE_OVERLAP_RATIO = 0.5
BATCH_SIZE = 16

CLOUD_BUFFER_KERNEL_SIZE = (10, 10)
CLOUD_DETECTION_THRESHOLD = 0.5
BRIGHTNESS_THRESHOLD = 0.35
NDWI_CLOUD_THRESHOLD = 0.15
NDWI_WATER_THRESHOLD = 0.35
NDWI_MIN_THRESHOLD = -0.1

CLOSE_KERNEL_SIZE = (3, 3)
OPEN_KERNEL_SIZE = (2, 2)
MIN_CONTOUR_AREA = 50

SAR_WATER_DB_RANGE = (-16, -90)
SAR_DB_OFFSET = 25

RGB_BAND_INDICES = [0, 1, 2]
RGB_SCALING_FACTOR = 3.0
OPTICAL_NORMALIZATION_FACTOR = 10000.0
GAUSSIAN_BLUR_SIGMA = 2.0
GAUSSIAN_BLUR_SHARPEN_STRENGTH = 1.5
GAUSSIAN_BLUR_SUBTRACT_STRENGTH = -0.5

OVERLAY_WATER_COLOR_BGR = (255, 0, 0)
OVERLAY_PREVIOUS_COLOR_BGR = (255, 255, 0)
OVERLAY_FLOOD_COLOR_BGR = (0, 0, 255)
OVERLAY_ALPHA = 180

FLASK_HOST = "0.0.0.0"
FLASK_PORT = int(os.environ.get("FLASK_PORT", os.environ.get("PORT", 5000)))
FLASK_THREADED = True

def calculate_pixel_to_sq_km(scale_meters):
    return (scale_meters ** 2) / 1_000_000

S2_BANDS = ['B2', 'B3', 'B4', 'B8', 'B11', 'B12']
S1_POLARIZATION = 'VV'
S1_INSTRUMENT_MODE = 'IW'
DW_WATER_CLASS = 0