当前位置: 首页 > news >正文

逐时nc数据批量处理为日平均

现有1系列的nc数据,每个数据格式如下:

需要进行批量处理将数据处理为日平均的tif,可使用下列脚本:

import os
import xarray as xr
import pandas as pd
import multiprocessing as mul
import numpy as np
from rasterio.transform import from_origin
import rasterio
from datetime import datetimedef save_as_geotiff(data_array, lons, lats, filename):"""将数据保存为 GeoTIFF 文件"""# 计算仿射变换参数transform = from_origin(lons.min(),  lats.max(),  (lons.max() - lons.min()) / len(lons),  (lats.max() - lats.min()) / len(lats)   )with rasterio.open(filename,'w',driver='GTiff',height=len(lats),width=len(lons),count=1,dtype=data_array.dtype,crs='EPSG:4326',  # WGS84 坐标系transform=transform,) as dst:dst.write(data_array, 1)  def process_snow_daily_average(args):"""处理雪深数据并计算日平均"""inpath, outpath, start_year, end_year = argsfor year in range(start_year, end_year + 1):for month in range(1, 13):filename = f"snow_{year}_{month:02d}.nc"file_path = os.path.join(inpath, filename)if not os.path.exists(file_path):print(f"Warning: File {file_path} does not exist, skipping...")continuetry:ds = xr.open_dataset(file_path)snow_data = ds['sde']time_values = pd.to_datetime(snow_data.valid_time.values)dates = [t.date() for t in time_values]  unique_dates = sorted(set(dates))output_year_path = os.path.join(outpath, f"y{year}")os.makedirs(output_year_path, exist_ok=True)for current_date in unique_dates:date_mask = [d == current_date for d in dates]daily_data = snow_data[date_mask, :, :]daily_avg = daily_data.mean(dim='valid_time', skipna=True)lons = daily_data.longitude.valueslats = daily_data.latitude.valuesoutname = os.path.join(output_year_path,f"snow_{current_date.strftime('%Y%m%d')}.tif")save_as_geotiff(daily_avg.values, lons, lats, outname)print(f"{outname} has been converted!")ds.close()except Exception as e:print(f"Error processing file {file_path}: {str(e)}")continueif __name__ == "__main__":inpath = r"data/era5_sd_2025/1981_1989"  outpath = r"data/era5_sd_2025/daily_1981_1989"  start_year = 1981  end_year = 1989    num_processes = min(8, os.cpu_count())  # 使用较少的进程total_years = end_year - start_year + 1years_per_process = max(1, total_years // num_processes)args_list = []for i in range(num_processes):current_start = start_year + i * years_per_processcurrent_end = min(current_start + years_per_process - 1, end_year)if i == num_processes - 1:current_end = end_yearif current_start > end_year:breakargs_list.append((inpath, outpath, current_start, current_end))with mul.Pool(processes=num_processes) as pool:pool.map(process_snow_daily_average, args_list)print('--'*50)print('all jobs have finished!!!')


文章转载自:

http://yCu9krXG.rkwjs.cn
http://zJkpTJwz.rkwjs.cn
http://3kmFtmk0.rkwjs.cn
http://iZGr71Wp.rkwjs.cn
http://7KHYPCkN.rkwjs.cn
http://AsSHMcAB.rkwjs.cn
http://7L0IkzXg.rkwjs.cn
http://YaWnNbaf.rkwjs.cn
http://P7yIHX0L.rkwjs.cn
http://mfYT21Xf.rkwjs.cn
http://ay8VuLLB.rkwjs.cn
http://XfprYIcY.rkwjs.cn
http://dQ2eyX4a.rkwjs.cn
http://JmGJZknR.rkwjs.cn
http://pA72OrhA.rkwjs.cn
http://6smvQW12.rkwjs.cn
http://vhBi8eab.rkwjs.cn
http://uGeFx06S.rkwjs.cn
http://ndP0tQpc.rkwjs.cn
http://rhfg8gG4.rkwjs.cn
http://sbgtWC1d.rkwjs.cn
http://aM5Cw1IG.rkwjs.cn
http://R2bVL5pp.rkwjs.cn
http://N9cC10B0.rkwjs.cn
http://yRVRUJPf.rkwjs.cn
http://kaw1UKob.rkwjs.cn
http://YbIcGdeD.rkwjs.cn
http://EcaVEwTo.rkwjs.cn
http://YhWVD3xH.rkwjs.cn
http://h6qGNgul.rkwjs.cn
http://www.dtcms.com/a/381881.html

相关文章:

  • ffmpeg推流测试
  • SQL注入常见攻击点与防御详解
  • 后端(FastAPI)学习笔记(CLASS 3):Tortoise ORM
  • C++-STL
  • Java 大视界 -- Java 大数据在智能家居场景联动与用户行为模式挖掘中的应用
  • XCKU15P-2FFVA1760I AMD 赛灵思 Xilinx Kintex UltraScale+ FPGA
  • 图论基础知识
  • DMA硬件架构解析:总线矩阵与核心组件
  • 从军用到掌心:固态硬盘(SSD)的演进与革命
  • 通俗解释redis高级:redis持久化(RDB持久化、AOF持久化)、redis主从、redis哨兵、redis分片集群
  • 【C++】类和对象——(上)
  • 解决Windows系统“‘php‘ 不是内部或外部命令”报错的完整指南
  • 用 Go 打造一个服务器资源指标采集器:结合 Prometheus Exporter 实战
  • Unity学习----【进阶】TextMeshPro学习(二)--进阶知识点(样式表,颜色渐变预设,精灵图片资源)
  • 从理论到落地:神经网络稀疏化设计构架中网络剪枝的深度实践与创新
  • ARM、AArch64、amd64、x86_64、x86有什么区别?
  • 机器学习项目-南方电网电力负荷预测
  • python标准库有哪些模块,简单总结下。
  • 文献阅读·MCformer:基于混合通道变换的多变量时间序列预测
  • 【软件操作】飞牛nas系统:笔记本息屏、合盖均不关机
  • 【SPI】【二】SPI控制器驱动代码详解
  • pandas读取复合列名列头及数据和处理
  • jenkins触发部署
  • 【pure-admin】项目登录验证码实现分析
  • Docker快速入门手册
  • 【C++设计模式】第五篇:装饰器模式
  • linux C 语言开发 (十) 进程间通讯--信号
  • 绿色环保活动平台(AI问答、WebSocket即时通讯、协同过滤算法、Echarts图形化分析)
  • 飞算JavaAI实战高效构建电商系统核心功能模块全解析
  • CSS 技巧使页脚始终位于网页的底部