update 01 file train_odc.py

This commit is contained in:
Victor Phan
2026-02-26 15:33:36 +07:00
parent ce227825a8
commit 0db4148a40
+22 -22
View File
@@ -523,19 +523,32 @@
" print(\" Trying unsigned access as fallback...\")\n",
" configure_s3_access(aws_unsigned=True)\n",
" print(\" Switched to aws_unsigned=True (public buckets only)\")\n",
" # Get all scenes from datacube metadata (filtered to Vietnam region)\n",
"\n",
" # ===== CHECK IF SHOULD SKIP DOWNLOAD =====\n",
" if use_cache and data is not None:\n",
" print(f\"\\n✅ Using cached dataset - skipping download!\")\n",
" print(f\" Variables: {len(data.data_vars)}\")\n",
" print(f\" Shape: {data.dims}\")\n",
" display(data)\n",
" \n",
" else:\n",
" # ===== DOWNLOAD FROM S3 =====\n",
" print(f\"\\n📥 Downloading from S3...\")\n",
" \n",
" # Get all scenes from datacube metadata (filtered to Vietnam region)\n",
" datasets = list(dc.find_datasets(\n",
" product='s2_l2a',\n",
" time=date_range,\n",
" lat=latitude_range,\n",
" lon=longtitude_range\n",
" ))\n",
" \n",
" if not datasets:\n",
" raise ValueError(\"No datasets found for date range\")\n",
" raise ValueError(\"No datasets found for date range and region\")\n",
" \n",
" print(f\"\\n📦 Found {len(datasets)} available scenes\")\n",
" print(f\" Date range: {date_range[0]} to {date_range[1]}\")\n",
" print(f\" Region: lon={longtitude_range}, lat={latitude_range}\")\n",
" \n",
" # ===== LOAD ALL SCENES WITH ALL AVAILABLE BANDS (NO MAGNIFICATION) =====\n",
" # num_scenes = len(datasets) # Load ALL scenes\n",
@@ -607,7 +620,7 @@
" max_size = size\n",
" ref_resolution = size\n",
" max_band = band_name\n",
" max_size = 0\n",
" \n",
" print(f\" Reference resolution: {max_size}×{max_size} pixels (native {max_band})\")\n",
" \n",
" # Resample all bands to match reference resolution (both up and down)\n",
@@ -631,7 +644,7 @@
" if scene_idx == 0: # Print for first scene only to reduce clutter\n",
" print(f\" Resampling {band_name}: {current_size}×{current_size} → {new_size}×{new_size}\")\n",
" resampled_count += 1\n",
" if band_name == 'scl':\n",
" \n",
" print(f\"✅ Resolution normalization complete! ({resampled_count} bands resampled)\")\n",
" \n",
" # ===== CALCULATE SPECTRAL INDICES FOR EACH SCENE =====\n",
@@ -679,7 +692,7 @@
" \n",
" except Exception as e:\n",
" pass\n",
" if 'nir' in scene_data and 'red' in scene_data and 'blue' in scene_data:\n",
" \n",
" print(f\"✅ Calculated {indices_count} spectral indices per scene\")\n",
" \n",
" # ===== STACK SCENES ALONG TIME DIMENSION =====\n",
@@ -713,7 +726,7 @@
" \n",
" # ===== CALCULATE TEMPORAL FEATURES FOR ACCURACY =====\n",
" print(f\"\\n[TEMPORAL FEATURES] Computing temporal features from time-series...\")\n",
" data_vars,\n",
" \n",
" temporal_features_added = 0\n",
" \n",
" # For NDVI: temporal statistics\n",
@@ -746,7 +759,7 @@
" band_ts = data[band_name]\n",
" data[f'{band_name}_mean'] = band_ts.mean(dim='time')\n",
" temporal_features_added += 1\n",
" # NDVI std (temporal consistency)\n",
" \n",
" print(f\"✅ Added {temporal_features_added} temporal/aggregate features\")\n",
" \n",
" # ===== SAVE TO CACHE =====\n",
@@ -790,7 +803,7 @@
" print(f\" Temporal aggregates ({len(temporal_vars)}):\")\n",
" for v in temporal_vars:\n",
" print(f\" - {v}\")\n",
" spatial_vars.append(f\"{var_name} {data[var_name].shape}\")\n",
" \n",
" print(f\" {'='*70}\")\n",
" \n",
" display(data)\n",
@@ -817,21 +830,8 @@
" traceback.print_exc()\n",
" data = None\n",
" ndvi = None\n",
" else:\n",
"\n",
"print(\"=\"*70)\n",
" print(f\"❌ NDVI not found in dataset\")\n",
"\n",
"\n",
" ndvi = Noneprint(\"=\"*70)\n",
"\n",
" \n",
"\n",
"except Exception as e: ndvi = None\n",
"\n",
" print(f\"❌ Error: {e}\") data = None\n",
"\n",
" import traceback traceback.print_exc()"
"print(\"=\"*70)\n"
]
},
{