FY4B卫星L2级产品掌握和python处理

张开发
2026/4/20 17:11:22 15 分钟阅读

分享文章

FY4B卫星L2级产品掌握和python处理
废话不多说展示二级产品CTT为例抱歉没空了解FY4B产品情况了直接看代码# CTT色标配置bounds_CTT[180,200,220,240,260,280,300,320]# 根据你的数据设定colors_CTT[(0,0,139/255),(10/255,0,245/255),(0,164/255,235/255),(0,246/255,192/255),(89/255,255/255,5/255),(255/255,122/255,0),(255/255,68/255,0),(155/255,14/255,0)]defget_fy4b_l2_png(filePaths,pngPath):try:file_typefilePaths.split(.)[-1]datasetnc.Dataset(filePaths,r)timedataset.date_created fyTimedatetime.strptime(time,%Y-%m-%dT%H:%M:%SZ).strftime(%Y-%m-%d %H:%M:%S)timeStrfyTime.replace( ,_).replace(:,)file_nameos.path.basename(filePaths)typefile_name.split(-)[4].replace(_,)iftypeQPE:datadataset.variables[Precipitation][:]else:datadataset.variables[type][:]# 经纬度32°N-42°N104°E-125°E# 设置经纬度网格并通过坐标转换转为CTT数据中的行列号来读取数据latnp.arange(3,55,0.1)lonnp.arange(60,137,0.1)# 将经纬度转为格点变为[lon lat]形式转换为[l c]目标输出每个经纬度格点行列号的CTT数据维度CTTlat,lon90120lon,latnp.meshgrid(lon,lat)line,columnlatlontolinecolumn(lat,lon,4000M)lline[:,:,np.newaxis]ccolumn[:,:,np.newaxis]lcnp.concatenate((l,c),axis2)# (90*120*2)sichun[[]foriinrange(520)]i0forpoint_linlc:# for point_c in point_l:# CTT_sichun[i].append(CTT[round(point_c[0])][round(point_c[1])])# i1forpoint_cinpoint_l:# 添加有效性检查确保行列号在合理范围内if0point_c[0]data.shape[0]-1and0point_c[1]data.shape[1]-1:sichun[i].append(data[round(point_c[0])][round(point_c[1])])else:# 如果行列号越界可以添加一些处理逻辑比如跳过该点或者使用默认值sichun[i].append(0)# 这里使用了默认值你可以根据需要进行调整i1iftypeCLM:boundsbounds_CLM colorscolors_CLMeliftypeCTT:boundsbounds_CTT colorscolors_CTTelse:logger.error(数据错误或格式错误)# 创建自定义颜色映射custom_cmapListedColormap(colors)normBoundaryNorm(bounds,custom_cmap.N,clipFalse)# # 绘制数据fig,axplt.subplots(subplot_kw{projection:ccrs.PlateCarree()})# 添加省界chinacfeature.ShapelyFeature(Reader(shp_path).geometries(),ccrs.PlateCarree(),linewidth0.5,facecolornone,edgecoloryellow,alpha0.7)ax.add_feature(china)# 绘制数据使用 viridis 颜色映射bax.contourf(lon,lat,sichun,cmapcustom_cmap,transformccrs.PlateCarree())# # 添加网格线# ax.gridlines(draw_labelsTrue, linestyle--, colorgray, alpha0.5)dir_pngpngPath/typeifnotos.path.exists(dir_png):# 如果路径不存在os.makedirs(dir_png)# 则创建该目录else:print(路径已经存在)out_pathdir_png/type_timeStr.png# img ax.imshow(CTT_sichun, cmapcustom_cmap, normnorm, transformccrs.Mercator())plt.savefig(out_path,transparentTrue,dpi600,bbox_inchestight,pad_inches0)dataset.close()logger.debug(out_pathProduct production successful)pathget_path(out_path,pngPath)add_fy4b_record(fyTime,type,path)exceptExceptionase:# 记录异常信息logger.error(绘制FY4Btype时发生错误: str(e))# 根据需要抛出 HTTP 异常或其他类型的异常# raise HTTPException(status_code500, detail处理 type 时发生错误)defget_path(path,path1):# 找到path1在path中的结束位置end_indexpath.find(path1)len(path1)# 截取从path1之后的部分sub_pathpath[end_index:]returnsub_path完整代码详情关注微信公众号《关于气象遥感》文章点击FY4B卫星L2级产品掌握和python处理

更多文章