From 231d6f1d7298cf737dd08d6a33100c303ff036d1 Mon Sep 17 00:00:00 2001 From: xhong Date: Mon, 18 May 2026 17:23:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(hls=20processing):=20=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E6=A0=85=E6=A0=BC=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E5=B9=B6?= =?UTF-8?q?=E8=B7=B3=E8=BF=87=E5=BC=82=E5=B8=B8=E6=95=B0=E6=8D=AE=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为 open_hls 函数添加异常捕获逻辑,打开栅格失败时返回 None;在处理数据块时检查质量图层可用性,无法访问时跳过并记录警告;移除 common_utils.py 中冗余的 netrc 相关 GDAL 配置项;更新 HLS_PER.py 的最后更新日期 --- HLS_SuPER/HLS_PER.py | 24 ++++++++++++++++++++---- utils/common_utils.py | 7 ------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/HLS_SuPER/HLS_PER.py b/HLS_SuPER/HLS_PER.py index eb89938..bcb8009 100644 --- a/HLS_SuPER/HLS_PER.py +++ b/HLS_SuPER/HLS_PER.py @@ -8,7 +8,7 @@ search results. ------------------------------------------------------------------------------- Authors: Cole Krehbiel, Mahsa Jami, and Erik Bolch Editor: Hong Xie -Last Updated: 2026-05-17 +Last Updated: 2026-05-18 =============================================================================== """ @@ -52,9 +52,13 @@ def open_hls( Some HLS Landsat scenes have the metadata in the wrong location. """ # Open using rioxarray - da = rxr.open_rasterio(url, chunks=chunk_size, mask_and_scale=False).squeeze( - "band", drop=True - ) + try: + da = rxr.open_rasterio(url, chunks=chunk_size, mask_and_scale=False).squeeze( + "band", drop=True + ) + except Exception as e: + logging.warning(f"Failed to open {url}: {e}") + return None # (Add) 若未获取到数据, 则返回 None if da is None: return None @@ -197,6 +201,12 @@ def process_granule( if not os.path.isfile(quality_output_file): # Open Quality Layer qa_da = open_hls(quality_url, roi, clip, scale, chunk_size) + # (Add) 若返回的da为None, 则表示该url对应的文件不存在/无法访问, 跳过整个granule + if qa_da is None: + logging.warning( + f"Quality layer {quality_url} not accessible. Skipping granule." + ) + return # Write Output # (Add) 添加压缩选项参数 compress # compress 参数是源自 rioxarray 继承的 rasterio 的选项, 可以参考 https://rasterio.readthedocs.io/en/latest/api/rasterio.enums.html#rasterio.enums.Compression @@ -207,6 +217,12 @@ def process_granule( ) else: qa_da = open_hls(quality_output_file, roi, clip, scale, chunk_size) + # (Add) 若返回的da为None, 则表示本地文件无法访问, 跳过整个granule + if qa_da is None: + logging.warning( + f"Local quality file {quality_output_file} not accessible. Skipping granule." + ) + return logging.info( f"Existing quality file {quality_output_name} found in {output_dir}." ) diff --git a/utils/common_utils.py b/utils/common_utils.py index 95dd4c4..401ca39 100644 --- a/utils/common_utils.py +++ b/utils/common_utils.py @@ -243,18 +243,11 @@ def setup_dask_environment(): os.path.expanduser("~/cookies.txt"), ) - netrc_path = os.environ.get("EARTHDATA_NETRC_FILE") or os.path.expanduser( - "~/.netrc" - ) - enable_netrc = "YES" if os.path.exists(netrc_path) else "NO" - global env gdal_config = { "GDAL_HTTP_UNSAFESSL": "YES", "GDAL_HTTP_COOKIEFILE": cookie_file_path, "GDAL_HTTP_COOKIEJAR": cookie_file_path, - "GDAL_HTTP_NETRC": enable_netrc, - "GDAL_HTTP_NETRC_FILE": netrc_path, "GDAL_DISABLE_READDIR_ON_OPEN": "YES", "CPL_VSIL_CURL_ALLOWED_EXTENSIONS": "TIF", "GDAL_HTTP_MAX_RETRY": "10",