diff --git a/post_pro/merge_tif.py b/post_pro/merge_tif.py index dc2f48f..866eae6 100644 --- a/post_pro/merge_tif.py +++ b/post_pro/merge_tif.py @@ -108,6 +108,7 @@ class MergeTif: input_tif1, input_tif2 = None, None merge_count = 0 + temp_files = [] try: for grid_id, points in grid_points.items(): @@ -127,8 +128,8 @@ class MergeTif: # 如果文件大于600MB,则不使用original文件 file_size_mb = os.path.getsize( grid_tif) / (1024 * 1024) # 转换为MB - file_name = file_name.replace(".original", "") if file_size_mb > 600: + file_name = file_name.replace(".original", "") grid_tif = os.path.join( self.output_dir, f"grid_{grid_id[0]}_{grid_id[1]}", @@ -158,38 +159,20 @@ class MergeTif: self.merge_two_tifs(input_tif1, input_tif2, temp_output) merge_count += 1 - # 如果之前的输入文件是临时文件,则删除它 - if 'temp_merged_' in input_tif1: - try: - os.remove(input_tif1) - except Exception as e: - self.logger.warning(f"删除临时文件失败: {str(e)}") - input_tif1 = temp_output input_tif2 = None + temp_files.append(temp_output) - # 重命名最终的临时文件为目标文件名 final_output = os.path.join( self.output_dir, product_info['output']) - if os.path.exists(final_output): - try: - os.remove(final_output) - except Exception as e: - self.logger.warning(f"删除已存在的最终输出文件失败: {str(e)}") - final_output = os.path.join( - self.output_dir, - f"merged_{int(time.time())}_{product_info['output']}" - ) + shutil.copy2(input_tif1, final_output) - try: - os.rename(input_tif1, final_output) - except Exception as e: - self.logger.warning(f"重命名最终文件失败: {str(e)}") - shutil.copy2(input_tif1, final_output) + # 清理所有临时文件 + for temp_file in temp_files: try: - os.remove(input_tif1) - except: - pass + os.remove(temp_file) + except Exception as e: + self.logger.warning(f"删除临时文件失败: {str(e)}") self.logger.info( f"{product_name}合并完成,共执行 {merge_count} 次合并,"