赞
踩
使用rasterio将国家气象局的grib格式的气象数据解析成单个的tif数据
气象数据说明:https://www.docin.com/p-2089521384.html
# 实现代码 import rasterio import datetime import os from pathlib import Path def grib2tif(fpath:str,save_path:str,is_created=True,start_time='2022-08-17_08-00-00',intv=3)->None: ''' :param fpath: 文件路径 :param save_path: 存储文件夹 :param is_created: 当存储文件夹不存在时,是否创建新的文件夹 :param start_time: 数据的起始时间,默认最新时间 :param intv: 数据的时间间隔,默认3小时 :return: ''' if not Path(fpath).is_file(): raise ValueError("file path is not exist!") if not Path(save_path).is_dir() and not is_created: raise ValueError("dir path is not exist!") # 输入的文件夹不存在时,新建文件夹 elif not Path(save_path).is_dir() and is_created: os.mkdir(save_path) with rasterio.open(fpath,'r+') as dataset: profile = dataset.profile profile.update(count=1, driver="GTiff") for band in range(0,dataset.count): if band > 0: # 以时间命名数据 start_time = datetime.datetime.strptime(start_time, "%Y-%m-%d_%H-%M-%S") start_time = (start_time+datetime.timedelta(hours=intv)).strftime("%Y-%m-%d_%H-%M-%S") with rasterio.open(f"{os.path.join(save_path,start_time)}.tif",'w+',**profile) as dst: # 默认的dataset的id是从1开始的 dst.write(dataset.read(band+1),1) return None if __name__ == '__main__': url = '/mnt/d/降雨网格/20220817/Z_NWGD_C_BABJ_20220817075702_P_RFFC_SCMOC-ER03_202208170800_24003.GRB2' grib2tif(url)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。