简体   繁体   English

获取 netCDF 中特定区域的平均值

[英]Obtain mean value of specific area in netCDF

I am trying to plot a time series of the sea surface temperature (SST) for a specific region from a.nc file.我正在尝试 plot 来自 a.nc 文件的特定区域的海面温度 (SST) 时间序列。 The SST is a three-dimensional variable (lat,lon,time), that has mean daily values for a specific region from 1982 to 2016. I want my plot to reflect the seasonal sst variability of the entire period of time. SST 是一个三维变量(纬度、经度、时间),它具有从 1982 年到 2016 年特定区域的日均值。我希望我的 plot 反映整个时间段的季节性 sst 变化。 I assume that what I need to do first is to obtain a mean sst value for my lat,lon region for each of the days with which I can work alter on.我假设我首先需要做的是为我的 lat,lon 区域在我可以改变的每一天获得一个平均 sst 值 So far, I assume that I need to read the.nc file and the variables:到目前为止,我假设我需要读取 .nc 文件和变量:

import netCDF4 as nc

f = nc.Dataset('cmems_SST_MED_SST_L4_REP_OBSERVATIONS_010_021_1639073212518.nc')
sst = f.variables['analysed_sst'][:]
lon = f.variables['longitude'][:]
lat = f.variables['latitude'][:]

Next, following the code suggested here , I tried to reshape and obtain the mean, but an error pops up:接下来,按照此处建议的代码,我尝试重塑并获得平均值,但弹出错误:

global_average= np.nanmean(sst[:,:,:],axis=(1,2))
annual_temp = np.nanmean(np.reshape(global_average, (34,12)), axis = 1) 

#34 years between 1982 and 2016, and 12 months per year.

ERROR cannot reshape array of size 14008 into shape (34,12)错误无法将大小为 14008 的数组重新整形为形状 (34,12)

From here I found different ways, like using cdo or nco (which didn't work due installation problems) among others, which were not suitable for my case.从这里我发现了不同的方法,比如使用 cdo 或 nco (由于安装问题而无法使用)等,它们不适合我的情况。 I used nanmean because know that in MATLAB this is done using the nanmean function.我使用了 nanmean,因为知道在 MATLAB 中,这是使用 nanmean function 完成的。 I am quite new to this topic and I would like to ask for some hints, like, where should I focus more or what path is more suitable for this case.我对这个话题很陌生,我想请教一些提示,比如,我应该在哪里更多地关注或者什么路径更适合这种情况。 Thank you!!谢谢!!

Handling daily data with just pure python is difficult because you should consider leap years and sub-setting a region require tedious indexing striding....仅使用纯 python 处理日常数据很困难,因为您应该考虑闰年和子设置区域需要繁琐的索引跨步......

As steTATO mentioned, since the data that you are working has daily temporal resolution you need to consider the following正如 steTATO 所提到的,由于您正在处理的数据具有每日时间分辨率,因此您需要考虑以下几点

You need to reshape the global_average in the shape of (34,365) or (34,366) depending on the year (1984,1988,1992,1996,2000,2004,2008,2012,2016).您需要根据年份 (1984,1988,1992,1996,2000,2004,2008,2012,2016) 将global_average重塑为 (34,365) 或 (34,366)。 So your above code should look something like所以你上面的代码应该看起来像

annual_temp = np.nanmean(np.reshape(global_average, (34,365)), axis = 1) 

But, like I said, because of the leap years, you can't do the things you want by simply reshaping the global_average .但是,就像我说的那样,由于闰年,你不能通过简单地重塑global_average来做你想做的事情。

If I had no choice but to use python only, I'd do the following如果我别无选择,只能使用 python,我会执行以下操作

import numpy as np

def days_in_year(in_year):
   leap_years = [1984,1988,1992,1996,2000,2004,2008,2012,2016]
   if (in_year in leap_years):
      out_days = 366
   else:
      out_days = 365

   return out_days


# some of your code, importing netcdf data

year = np.arange(1982,2017)

global_avg= np.nanmean(sst[:,:,:],axis=(1,2))

annual_avgs = []
i = 0
for yr in range(35):
   i  =  i + days_in_year(year[yr])
   f  =  i - days_in_year(year[yr])

   annual_avg  =  np.nanmean(global_avg[i:f])
   annual_avgs.append(annual_avg)

Above code basically takes and averages by taking strides of the global_avg considering the leap year, and saving it as annual_avgs .上面的代码基本上通过考虑闰年的global_avg的步幅来取平均值,并将其保存为annual_avgs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM