From 0db4148a40f1176fd3104018227a86018cbc0b09 Mon Sep 17 00:00:00 2001 From: Victor Phan Date: Thu, 26 Feb 2026 15:33:36 +0700 Subject: [PATCH] update 01 file train_odc.py --- 01.train_ODC.ipynb | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/01.train_ODC.ipynb b/01.train_ODC.ipynb index 285ddfe..533460c 100755 --- a/01.train_ODC.ipynb +++ b/01.train_ODC.ipynb @@ -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" ] }, {