update 01 file train_odc.py
This commit is contained in:
+22
-22
@@ -523,19 +523,32 @@
|
|||||||
" print(\" Trying unsigned access as fallback...\")\n",
|
" print(\" Trying unsigned access as fallback...\")\n",
|
||||||
" configure_s3_access(aws_unsigned=True)\n",
|
" configure_s3_access(aws_unsigned=True)\n",
|
||||||
" print(\" Switched to aws_unsigned=True (public buckets only)\")\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",
|
" # ===== CHECK IF SHOULD SKIP DOWNLOAD =====\n",
|
||||||
" if use_cache and data is not None:\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",
|
" time=date_range,\n",
|
||||||
" lat=latitude_range,\n",
|
" lat=latitude_range,\n",
|
||||||
" lon=longtitude_range\n",
|
" lon=longtitude_range\n",
|
||||||
" ))\n",
|
" ))\n",
|
||||||
" \n",
|
" \n",
|
||||||
" if not datasets:\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",
|
" \n",
|
||||||
" print(f\"\\n📦 Found {len(datasets)} available scenes\")\n",
|
" print(f\"\\n📦 Found {len(datasets)} available scenes\")\n",
|
||||||
" print(f\" Date range: {date_range[0]} to {date_range[1]}\")\n",
|
" print(f\" Date range: {date_range[0]} to {date_range[1]}\")\n",
|
||||||
|
" print(f\" Region: lon={longtitude_range}, lat={latitude_range}\")\n",
|
||||||
" \n",
|
" \n",
|
||||||
" # ===== LOAD ALL SCENES WITH ALL AVAILABLE BANDS (NO MAGNIFICATION) =====\n",
|
" # ===== LOAD ALL SCENES WITH ALL AVAILABLE BANDS (NO MAGNIFICATION) =====\n",
|
||||||
" # num_scenes = len(datasets) # Load ALL scenes\n",
|
" # num_scenes = len(datasets) # Load ALL scenes\n",
|
||||||
@@ -607,7 +620,7 @@
|
|||||||
" max_size = size\n",
|
" max_size = size\n",
|
||||||
" ref_resolution = size\n",
|
" ref_resolution = size\n",
|
||||||
" max_band = band_name\n",
|
" max_band = band_name\n",
|
||||||
" max_size = 0\n",
|
" \n",
|
||||||
" print(f\" Reference resolution: {max_size}×{max_size} pixels (native {max_band})\")\n",
|
" print(f\" Reference resolution: {max_size}×{max_size} pixels (native {max_band})\")\n",
|
||||||
" \n",
|
" \n",
|
||||||
" # Resample all bands to match reference resolution (both up and down)\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",
|
" 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",
|
" print(f\" Resampling {band_name}: {current_size}×{current_size} → {new_size}×{new_size}\")\n",
|
||||||
" resampled_count += 1\n",
|
" resampled_count += 1\n",
|
||||||
" if band_name == 'scl':\n",
|
" \n",
|
||||||
" print(f\"✅ Resolution normalization complete! ({resampled_count} bands resampled)\")\n",
|
" print(f\"✅ Resolution normalization complete! ({resampled_count} bands resampled)\")\n",
|
||||||
" \n",
|
" \n",
|
||||||
" # ===== CALCULATE SPECTRAL INDICES FOR EACH SCENE =====\n",
|
" # ===== CALCULATE SPECTRAL INDICES FOR EACH SCENE =====\n",
|
||||||
@@ -679,7 +692,7 @@
|
|||||||
" \n",
|
" \n",
|
||||||
" except Exception as e:\n",
|
" except Exception as e:\n",
|
||||||
" pass\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",
|
" print(f\"✅ Calculated {indices_count} spectral indices per scene\")\n",
|
||||||
" \n",
|
" \n",
|
||||||
" # ===== STACK SCENES ALONG TIME DIMENSION =====\n",
|
" # ===== STACK SCENES ALONG TIME DIMENSION =====\n",
|
||||||
@@ -713,7 +726,7 @@
|
|||||||
" \n",
|
" \n",
|
||||||
" # ===== CALCULATE TEMPORAL FEATURES FOR ACCURACY =====\n",
|
" # ===== CALCULATE TEMPORAL FEATURES FOR ACCURACY =====\n",
|
||||||
" print(f\"\\n[TEMPORAL FEATURES] Computing temporal features from time-series...\")\n",
|
" print(f\"\\n[TEMPORAL FEATURES] Computing temporal features from time-series...\")\n",
|
||||||
" data_vars,\n",
|
" \n",
|
||||||
" temporal_features_added = 0\n",
|
" temporal_features_added = 0\n",
|
||||||
" \n",
|
" \n",
|
||||||
" # For NDVI: temporal statistics\n",
|
" # For NDVI: temporal statistics\n",
|
||||||
@@ -746,7 +759,7 @@
|
|||||||
" band_ts = data[band_name]\n",
|
" band_ts = data[band_name]\n",
|
||||||
" data[f'{band_name}_mean'] = band_ts.mean(dim='time')\n",
|
" data[f'{band_name}_mean'] = band_ts.mean(dim='time')\n",
|
||||||
" temporal_features_added += 1\n",
|
" temporal_features_added += 1\n",
|
||||||
" # NDVI std (temporal consistency)\n",
|
" \n",
|
||||||
" print(f\"✅ Added {temporal_features_added} temporal/aggregate features\")\n",
|
" print(f\"✅ Added {temporal_features_added} temporal/aggregate features\")\n",
|
||||||
" \n",
|
" \n",
|
||||||
" # ===== SAVE TO CACHE =====\n",
|
" # ===== SAVE TO CACHE =====\n",
|
||||||
@@ -790,7 +803,7 @@
|
|||||||
" print(f\" Temporal aggregates ({len(temporal_vars)}):\")\n",
|
" print(f\" Temporal aggregates ({len(temporal_vars)}):\")\n",
|
||||||
" for v in temporal_vars:\n",
|
" for v in temporal_vars:\n",
|
||||||
" print(f\" - {v}\")\n",
|
" print(f\" - {v}\")\n",
|
||||||
" spatial_vars.append(f\"{var_name} {data[var_name].shape}\")\n",
|
" \n",
|
||||||
" print(f\" {'='*70}\")\n",
|
" print(f\" {'='*70}\")\n",
|
||||||
" \n",
|
" \n",
|
||||||
" display(data)\n",
|
" display(data)\n",
|
||||||
@@ -817,21 +830,8 @@
|
|||||||
" traceback.print_exc()\n",
|
" traceback.print_exc()\n",
|
||||||
" data = None\n",
|
" data = None\n",
|
||||||
" ndvi = None\n",
|
" ndvi = None\n",
|
||||||
" else:\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"=\"*70)\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()"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user