计算python中netcdf文件几年的年度异常 [英] Calculate yearly anomalies for several years on netcdf files in python

查看:87
本文介绍了计算python中netcdf文件几年的年度异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算 44 年的 netcdf 月度文件的月、季和年气温异常,并使用一些功能可以让我自动获取月度、季节性和年度期间的异常值并将结果保存在文件夹中.我只知道如何做它一年,而不是用一个函数做几年.

I need to calculate monthly, seasonal and annualy anomalies of air temperature of netcdf monthly files of 44 years with some function that allows me to obtain anomalies in the period on a monthly, seasonal and annualy automatically and save the results in a folder. I only know how to do It for one year and not for several years with a function.

from netCDF4 import Dataset, num2date
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeat
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from mpl_toolkits.basemap import Basemap

ds = Dataset('./interim_t2m_19792017.nc')
lats = ds.variables['latitude'][:]  # extract/copy the data
lons = ds.variables['longitude'][:]
time = ds.variables['time']
var = ds.variables['t2m'] 

lon, lat = np.meshgrid(lons, lats)
dates = num2date(time[:], time.units)
dates_pd = pd.to_datetime(dates)
periods = dates_pd.to_period(freq='M')

def plt_map(data):
    m = Basemap(projection='mill',llcrnrlat=-80,urcrnrlat=80,\
            llcrnrlon=0,urcrnrlon=360,lat_ts=20,resolution='c')
    x, y = m(lon, lat)
    plt.figure(figsize=(10,7))
    m.drawcoastlines()
    m.drawparallels(np.arange(-80.,81.,20.))
    m.drawmeridians(np.arange(-180.,181.,20.))
    m.drawmapboundary(fill_color='white')
    m.contourf(x,y,data, extend="both",cmap="jet");
    plt.colorbar(orientation='horizontal', pad=0.05)
plt_map(var[0,:,:])

mask_2016 = periods.year==2016
data = var[mask_2016,:,:].mean(axis=0)
plt_map(data)

推荐答案

我知道您正在寻找 Python 的答案,但这是 CDO(气候数据操作员)的基础,它允许您进行此类计算在终端窗口的一两个命令中.

I know you are looking for a python answer, but this is bread and butter of CDO (climate data operators), that allows you to do these sort of calculations in one or two commands from the terminal window.

例如,要获得您可以执行的 Era Interim 数据的年度平均值

For example, to get the annual means of your Era Interim data you can do

cdo yearmean interim_t2m_19792017.nc erai_yearmean.nc

然后要计算年度异常,您需要做长期平均值并减去它

and then to calculate the annual anomaly you need to do the long term average and subtract it

cdo timmean interim_t2m_19792017.nc erai_timemean.nc
cdo sub erai_yearmean.nc erai_timemean.nc yearanom.nc

您可以使用管道"来组合以上 3 个命令,但我将它们分开在这里,因为这样更容易看到发生了什么.

You can combine all these above 3 commands using "piping", but I keep them separate here as it is easier to see what is going on.

您可以通过以下方式获得平均每月季节性周期:

you can get the mean monthly seasonal cycle with:

cdo ymonmean interim_t2m_19792017.nc erai_ymonmean.nc

这会为您提供一个文件,其中包含所有一月、二月等(12 个时间片)的平均值.然后您可以计算每月异常,每个异常都相对于自己的每月平均值

this gives you a file with the average of all the januarys, Feb etc (12 time slices). And then you can calculate the monthly anomaly, each with respect to its own monthly mean with

cdo monmean interim_t2m_19792017.nc erai_monmean.nc
cdo sub erai_monmean.nc erai_ymonmean.nc erai_monanom.nc

还有用于季节性平均值的函数.

There are also functions for seasonal averages.

有关更多详细信息,请参阅在线文档:https://code.mpimet.mpg.de/projects/cdo/

See the online documentation for further details: https://code.mpimet.mpg.de/projects/cdo/

最后,msi_gerva 在评论中是正确的,在问题中不清楚异常与什么有关,因为您还可以计算与年度或长期平均值有关的月度异常.此外,你要求年度异常并说你只知道如何做一年,但我认为这没有多大意义,因为异常将为零.更准确地澄清问题可能会有所帮助.

Lastly, msi_gerva is correct in the comment, it is not clear in the question what the anomalies are with respect to, as you could also calculate monthly anomalies with respect to the annual or long term mean. Moreover, you ask for annual anomalies and say you only know how to do it for one year, but I don't think that makes much sense, as the anomalies would be zero. It may be helpful to clarify the question more precisely.

这篇关于计算python中netcdf文件几年的年度异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆