feat: 完善云端与本地数据的读取与预处理的兼容逻辑.

This commit is contained in:
谢泓 2025-01-12 00:38:59 +08:00
parent 017714b254
commit 5ca6574780

View File

@ -61,7 +61,11 @@ def open_hls(
# (Add) 读取波段名称 # (Add) 读取波段名称
split_asset = url.split("/")[-1].split(".") split_asset = url.split("/")[-1].split(".")
asset_name = split_asset[1] asset_name = split_asset[1]
band_name = split_asset[-2] band_name = split_asset[-2] if split_asset[-2] != "subset" else split_asset[-3]
# 判断是否为 Fmask 波段
is_fmask = True if (band_name in ["Fmask", "FMASK"]) else False
# 判断是否为 L30 独有的热红外波段
is_tir = True if (band_name in ["B10", "B11", "TIR1", "TIR2"]) and (asset_name == "L30") else False
# Reproject ROI and Clip if ROI is provided and clip is True # Reproject ROI and Clip if ROI is provided and clip is True
if roi is not None and clip: if roi is not None and clip:
@ -70,15 +74,14 @@ def open_hls(
# (Add) 即使大部分影像已经被缩放且填补了缺失值, 但可能仍然有些影像需要进行手动在本地GIS软件中进行缩放和填补缺失值 # (Add) 即使大部分影像已经被缩放且填补了缺失值, 但可能仍然有些影像需要进行手动在本地GIS软件中进行缩放和填补缺失值
# Apply Scale Factor if desired for non-quality layer # Apply Scale Factor if desired for non-quality layer
if band_name != "Fmask": if not is_fmask:
if scale: if scale:
# Mask Fill Values # Mask Fill Values
da = xr.where(da == -9999, np.nan, da) da = xr.where(da == -9999, np.nan, da)
# Scale Data # Scale Data
# (Add) 除质量层, 以及 L30 的两个热红外波段外, 其他光谱波段缩放因子均为 0.0001 # (Add) 除质量层, 以及 L30 的两个热红外波段外, 其他光谱波段缩放因子均为 0.0001
# (Add) L30 的两个热红外波段缩放因子为 0.01 # (Add) L30 的两个热红外波段缩放因子为 0.01
# NOTE: 需要注意的是热红外此时未被改名 if is_tir:
if (band_name == "B10" or band_name == "B11") and (asset_name == "L30"):
da = da * 0.01 da = da * 0.01
else: else:
da = da * 0.0001 da = da * 0.0001
@ -96,12 +99,12 @@ def open_hls(
# Add Scale Factor to Attributes Manually - This will overwrite/add if the data is missing. # Add Scale Factor to Attributes Manually - This will overwrite/add if the data is missing.
# (Add) 若要手动缩放, 则需要手动添加缩放因子 # (Add) 若要手动缩放, 则需要手动添加缩放因子
else: else:
if (band_name == "B10" or band_name == "B11") and (asset_name == "L30"): if is_tir:
da.attrs["scale_factor"] = 0.01 da.attrs["scale_factor"] = 0.01
else: else:
da.attrs["scale_factor"] = 0.0001 da.attrs["scale_factor"] = 0.0001
# 销毁源数据 # 清除源数据
del da_org da_org = None
return da return da
@ -186,12 +189,13 @@ def process_granule(
quality_output_name = create_output_name(quality_url, band_dict) quality_output_name = create_output_name(quality_url, band_dict)
quality_output_file = f"{output_dir}/{quality_output_name}" quality_output_file = f"{output_dir}/{quality_output_name}"
# Check if quality asset is already processed # Check if quality asset is already processed
if not os.path.isfile(quality_output_file):
# Open Quality Layer # Open Quality Layer
qa_da = open_hls(quality_url, roi, clip, scale, chunk_size) qa_da = open_hls(quality_url, roi, clip, scale, chunk_size)
if not os.path.isfile(quality_output_file):
# Write Output # Write Output
qa_da.rio.to_raster(raster_path=quality_output_file, driver="COG") qa_da.rio.to_raster(raster_path=quality_output_file, driver="COG")
else: else:
qa_da = open_hls(quality_output_file, roi, clip, scale, chunk_size)
logging.info( logging.info(
f"Existing quality file {quality_output_name} found in {output_dir}." f"Existing quality file {quality_output_name} found in {output_dir}."
) )