update 01

This commit is contained in:
Victor Phan
2025-11-12 13:21:43 +07:00
parent ab864a1c16
commit 7db1027ac6
6 changed files with 1665 additions and 93 deletions
+54 -93
View File
@@ -5,10 +5,14 @@
"id": "72723821",
"metadata": {},
"source": [
"# Prepare Data on Server\n",
"Chuẩn bị dữ liệu trên server: Tải từ S3, xử lý mây, tính NDVI, lưu thành file\n",
"# Prepare Data on Server - Load Raw Data Only\n",
"Chuẩn bị dữ liệu trên server: **Chỉ tải dữ liệu thô từ S3, không xử lý**\n",
"\n",
"**Sau khi chạy xong, tải các file data xuống máy cá nhân để train model**"
"**Workflow:**\n",
"1. Server: Tải S2 (red, nir, scl) + S1 (VH, VV) thô → Lưu NetCDF\n",
"2. Local: Tải xuống → Tính NDVI → Cloud mask → Fill NaN → Aggregation → Train model\n",
"\n",
"**Sau khi chạy xong, tải các file data xuống máy cá nhân để xử lý + train model**"
]
},
{
@@ -203,70 +207,6 @@
" print(\"\\n❌ Failed to load any scenes\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5dc550ce",
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# Loại bỏ các vị trí bị mây ảnh hưởng\n",
"print(\"☁️ Xử lý mây...\")\n",
"result = mask_clean(data)\n",
"progress(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd11fab1",
"metadata": {},
"outputs": [],
"source": [
"# Tính toán NDVI\n",
"print(\"🌱 Tính NDVI...\")\n",
"ds1 = calculate_indices(result, index=\"NDVI\", satellite_mission=\"s2\")\n",
"ndvi = ds1[\"NDVI\"]\n",
"print(f\"✅ NDVI shape: {ndvi.shape}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45bb6665",
"metadata": {},
"outputs": [],
"source": [
"# Điền mây sử dụng seasonal interpolation\n",
"print(\"🔧 Điền mây theo mùa vụ...\")\n",
"time_split = [\n",
" slice(\"2022-09-01\", \"2023-01-01\"),\n",
" slice(\"2023-01-01\", \"2023-05-01\"),\n",
" slice(\"2023-05-01\", \"2023-07-01\"),\n",
" slice(\"2023-07-01\", \"2023-10-01\"),\n",
"]\n",
"\n",
"fill_nan_ndvi = fill_nan(ndvi, time_split)\n",
"print(f\"✅ Mây đã được điền\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6892fb6b",
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"# Tính NDVI theo tháng\n",
"print(\"📊 Tính NDVI trung bình theo tháng...\")\n",
"average_ndvi = fill_nan_ndvi.resample(time=\"1M\").mean().persist()\n",
"progress(average_ndvi)\n",
"average_ndvi = average_ndvi.compute()\n",
"print(f\"✅ NDVI theo tháng shape: {average_ndvi.shape}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -274,14 +214,16 @@
"metadata": {},
"outputs": [],
"source": [
"# Tải dữ liệu Sentinel-1 (VH, VV)\n",
"## Tải dữ liệu Sentinel-1 (VH, VV) - Raw data, không xử lý\n",
"print(\"📡 Tải dữ liệu Sentinel-1 từ S3...\")\n",
"dsvh, dsvv = load_data_sen1(dc, date_range, coordinates)\n",
"print(\"📊 Tính VV, VH trung bình theo tháng...\")\n",
"average_vv = calculate_average(dsvv, time_pattern='1M')\n",
"average_vh = calculate_average(dsvh, time_pattern='1M')\n",
"print(f\"✅ VV shape: {average_vv.shape}\")\n",
"print(f\"✅ VH shape: {average_vh.shape}\")"
"try:\n",
" dsvh, dsvv = load_data_sen1(dc, date_range, coordinates)\n",
" print(f\"✅ VH shape: {dsvh.shape}\")\n",
" print(f\"✅ VV shape: {dsvv.shape}\")\n",
"except Exception as e:\n",
" print(f\"⚠️ Error loading S1: {e}\")\n",
" dsvh = None\n",
" dsvv = None"
]
},
{
@@ -289,7 +231,8 @@
"id": "5ba52958",
"metadata": {},
"source": [
"## Lưu dữ liệu đã xử lý thành file NetCDF"
"## Lưu dữ liệu RAW thành file NetCDF (chưa xử lý)\n",
"Dữ liệu này sẽ được tải xuống máy local để tiếp tục xử lý và training"
]
},
{
@@ -305,28 +248,46 @@
"data_dir = \"data_for_training\"\n",
"if not os.path.exists(data_dir):\n",
" os.makedirs(data_dir)\n",
" print(f\"✅ Tạo thư mục {data_dir}\")\n",
" print(f\"✅ Tạo thư mục {data_dir}\\n\")\n",
"\n",
"# Lưu NDVI\n",
"ndvi_path = os.path.join(data_dir, \"average_ndvi.nc\")\n",
"print(f\"💾 Lưu NDVI vào {ndvi_path}...\")\n",
"average_ndvi.to_netcdf(ndvi_path)\n",
"print(f\"✅ NDVI đã lưu ({os.path.getsize(ndvi_path) / 1024**2:.2f} MB)\")\n",
"print(\"💾 Lưu dữ liệu RAW từ S2 (red, nir, scl)...\")\n",
"\n",
"# Lưu VV\n",
"vv_path = os.path.join(data_dir, \"average_vv.nc\")\n",
"print(f\"💾 Lưu VV vào {vv_path}...\")\n",
"average_vv.to_netcdf(vv_path)\n",
"print(f\"✅ VV đã lưu ({os.path.getsize(vv_path) / 1024**2:.2f} MB)\")\n",
"# Lưu raw Sentinel-2\n",
"s2_path = os.path.join(data_dir, \"sentinel2_raw.nc\")\n",
"print(f\" - Lưu vào {s2_path}...\")\n",
"data.to_netcdf(s2_path)\n",
"s2_size = os.path.getsize(s2_path) / 1024**3 # Convert to GB\n",
"print(f\" ✅ Sentinel-2 đã lưu ({s2_size:.2f} GB)\")\n",
"\n",
"# Lưu VH\n",
"vh_path = os.path.join(data_dir, \"average_vh.nc\")\n",
"print(f\"💾 Lưu VH vào {vh_path}...\")\n",
"average_vh.to_netcdf(vh_path)\n",
"print(f\"✅ VH đã lưu ({os.path.getsize(vh_path) / 1024**2:.2f} MB)\")\n",
"# Lưu raw Sentinel-1 (nếu thành công)\n",
"if dsvh is not None and dsvv is not None:\n",
" print(f\"\\n💾 Lưu dữ liệu RAW từ S1 (VH, VV)...\")\n",
" \n",
" # Tạo xarray Dataset chứa cả VH và VV\n",
" s1_data = xr.Dataset({\n",
" 'VH': dsvh,\n",
" 'VV': dsvv,\n",
" })\n",
" \n",
" s1_path = os.path.join(data_dir, \"sentinel1_raw.nc\")\n",
" print(f\" - Lưu vào {s1_path}...\")\n",
" s1_data.to_netcdf(s1_path)\n",
" s1_size = os.path.getsize(s1_path) / 1024**3 # Convert to GB\n",
" print(f\" ✅ Sentinel-1 đã lưu ({s1_size:.2f} GB)\")\n",
"else:\n",
" print(\"⚠️ Sentinel-1 không được lưu (load thất bại)\")\n",
"\n",
"print(f\"\\n✅ Tất cả dữ liệu đã lưu trong thư mục '{data_dir}'\")\n",
"print(f\"📥 Hãy tải các file này xuống máy cá nhân để train model\")"
"print(f\"\\n✅ Tất cả dữ liệu RAW đã lưu trong thư mục '{data_dir}'\")\n",
"print(f\"📥 Hãy tải các file này xuống máy cá nhân\")\n",
"print(f\"🔧 Sẽ tính NDVI, cloud mask, aggregation trên local\")\n",
"\n",
"# Hiển thị tổng kích thước\n",
"import subprocess\n",
"try:\n",
" result = subprocess.run(['du', '-sh', data_dir], capture_output=True, text=True)\n",
" print(f\"\\n📊 Tổng dung lượng: {result.stdout.strip()}\")\n",
"except:\n",
" pass"
]
},
{