爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 25409|回复: 6

[求助] 计算年分位数后绘画平均出错

[复制链接]

新浪微博达人勋

发表于 2020-12-10 15:30:14 | 显示全部楼层 |阅读模式
3金钱
大家好,有一个问题一直查不出错来,想要请教各位大神。
目前手头上有自己封装的NC文件,包含41年的tmax,tmin,precipitation的逐日降水资料及其经纬度。
现在是在每年每个季节的变量进行95的分位线计算(降水加一个对大于0的情况才进行95th分位),代码没有报错,但是画图出来令人匪夷所思。比如东亚的tmax,95th分位线上的夏季还是有-15度,然后降水的图出来也不是很好看,我想可能是哪个算法出了问题,还请各位大神帮我看看。
以下是代码:
  1. import matplotlib.pyplot as plt
  2. import matplotlib.cm as cm
  3. import cartopy.crs as ccrs
  4. from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
  5. import cartopy.feature as cfeature
  6. import xarray as xr
  7. import numpy as np
  8. import pandas as pd
  9. from pylab import *
  10. from tqdm import tqdm

  11. f = xr.open_dataset("/Volumes/Forests/era rusults/EA/data_day.nc")
  12. ##f = xr.open_dataset('/Volumes/Forests/all sources data/1961-2018/CN05.1_Tmin_1961_2018_daily_025x025.nc',mask_and_scale=True)##我们输出的日数据###
  13. tmin = f["tmin"] - 273.15
  14. tmax = f["tmax"] - 273.15
  15. pre = f["pre"] * 1000

  16. lat = tmin.latitude
  17. lon = tmin.longitude
  18. def year_season_cal(var, year_s=1979,year_e=2019):
  19.         year = np.arange(year_s,year_e+1,1)
  20.         season_var = var[:4,:,:].expand_dims({"year":year},axis=0).copy()
  21.         for i in range(0,len(year)):
  22.                 ind, = np.where((var.time.dt.season== 'MAM')&(var.time.dt.year==year[i]))
  23.                 season_var.values[i,0]= np.nanpercentile(var[ind],95,axis=0)
  24.                 ind, = np.where((var.time.dt.season== 'JJA')&(var.time.dt.year==year[i]))
  25.                 season_var.values[i,1]= np.nanpercentile(var[ind],95,axis=0)
  26.                 ind, = np.where((var.time.dt.season== 'SON')&(var.time.dt.year==year[i]))
  27.                 season_var.values[i,2] = np.nanpercentile(var[ind],95,axis=0)
  28.                 ind = ind+91
  29.                 season_var.values[i,3] = np.percentile(var[ind],95,axis=0)
  30.                 return(season_var)
  31. tmin_season = year_season_cal(tmin, year_s=1979,year_e=2019)
  32. tmax_season = year_season_cal(tmax, year_s=1979,year_e=2019)

  33. def pre_year_season_cal(var,year_s=1979,year_e=2019):
  34.         year = np.arange(year_s,year_e+1,1)
  35.         season_var = var[:4,:,:].expand_dims({"year":year},axis=0).copy()
  36.         var_ = np.where(var==0,np.nan,var)
  37.         for i in range(0,len(year)):
  38.                 ind, = np.where((var.time.dt.season== 'MAM')&(var.time.dt.year==year[i]))
  39.                 season_var.values[i,0] = np.nanpercentile(var_[ind],95,axis=0)
  40.                 ind, = np.where((var.time.dt.season== 'JJA')&(var.time.dt.year==year[i]))
  41.                 season_var.values[i,1] = np.nanpercentile(var_[ind],95,axis=0)
  42.                 ind, = np.where((var.time.dt.season== 'SON')&(var.time.dt.year==year[i]))
  43.                 season_var.values[i,2] = np.nanpercentile(var_[ind],95,axis=0)
  44.                 ind = ind+91
  45.                 season_var.values[i,3] = np.nanpercentile(var_[ind],95,axis=0)
  46.                 return(season_var)
  47. pre_season = pre_year_season_cal(pre, year_s=1979,year_e=2019)

  48. tmin1 = np.zeros((4,len(lat),len(lon)))
  49. for dd in range(4):
  50.         tmin1[dd,:,:] = np.mean(tmax_season[:,dd,:,:],axis=0)

  51. def plot(tmin1,lat,lon,cmap = plt.get_cmap("Spectral_r"),levels=19):
  52.     fig, axes = plt.subplots(2, 2,figsize=(10,10),subplot_kw=dict(projection=ccrs.PlateCarree()))
  53.     label = ["MAM", "JJA","SON","DJF"]
  54.     for i, ax in enumerate(axes.flat):
  55.         ax.set_extent([100, 145, 20, 50], crs=ccrs.PlateCarree())
  56.         ax.set_xticks(np.linspace(100,145,5), crs=ccrs.PlateCarree())
  57.         ax.set_yticks(np.linspace(20,50,5), crs=ccrs.PlateCarree())
  58.         ax.add_feature(cfeature.LAND.with_scale('10m'))
  59.         ax.add_feature(cfeature.OCEAN, zorder = 100, color = 'w')
  60.         ax.add_feature(cfeature.COASTLINE.with_scale('10m'),lw=3)
  61.         ax.add_feature(cfeature.BORDERS.with_scale('50m'),linestyle='-',lw=3)
  62.         lon_formatter = LongitudeFormatter(number_format='.1f', degree_symbol=r'$^o
  63. )
  64.         lat_formatter = LatitudeFormatter(number_format='.1f', degree_symbol=r'$^o
  65. )
  66.         ax.xaxis.set_major_formatter(lon_formatter)
  67.         ax.yaxis.set_major_formatter(lat_formatter)
  68.         ax.tick_params(labelsize=8)
  69.         cf = ax.contourf(lon, lat, tmin1[i], cmap=cmap,levels= levels, extend='both')
  70.         ax.set_title(label[i], loc="left",fontsize=10)
  71.         ax.set_title("Annual 95thTmax", loc="right",fontsize=10)
  72.         cbar = plt.colorbar(cf, ax=ax, orientation='horizontal',
  73.                         extendrect=False, pad=0.08, shrink=0.8)
  74.         cbar.ax.tick_params(labelsize=8)
  75.         plt.subplots_adjust(wspace=0.15, hspace=0.03)
  76.     plt.savefig("Annual 95thTmax.png")
  77. plot(tmin1,lat,lon)
复制代码

澳大利亚降水95分位线

澳大利亚降水95分位线

东亚的95tmax

东亚的95tmax
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2020-12-10 18:43:54 | 显示全部楼层
你可以用cdo处理一下,比较两者之间的差异,不过要注意numpy和cdo的分位数算法。祝好
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2020-12-10 18:49:06 | 显示全部楼层
Lancelot 发表于 2020-12-10 18:43
你可以用cdo处理一下,比较两者之间的差异,不过要注意numpy和cdo的分位数算法。祝好

我装了CDO但是没有用过,我想问你觉得这里我可以用CDO处理什么呢?
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2020-12-10 19:14:17 | 显示全部楼层
lynpatty 发表于 2020-12-10 18:49
我装了CDO但是没有用过,我想问你觉得这里我可以用CDO处理什么呢?

用cdo算分位数啊
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2020-12-10 19:53:00 | 显示全部楼层

可是还要对每年/每个季节划分呢?
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2020-12-11 09:53:42 | 显示全部楼层

hi你好,我昨天去查了一下CDO手册,找到了按年/季节划分然后计算分位数的办法,但是有一个问题能不能问一下你,手册说对于降水的划分也是要判断它是否大于0.1才进行计算,我想问这个对0.1的判断是CDO自动会进行的吗?还是我们要筛选一下导入进行计算呢?
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2020-12-13 00:18:26 | 显示全部楼层
lynpatty 发表于 2020-12-11 09:53
hi你好,我昨天去查了一下CDO手册,找到了按年/季节划分然后计算分位数的办法,但是有一个问题能不能问一 ...

应该不会,你可以用cdo 简单mask一下
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表