简体   繁体   English

如何将 3 维数组平均化为 2 维数组 Python

[英]How to average a 3-D Array Into 2-D Array Python

I would like to take a temperature variable from a.netcdf file in python and average over all of the satellite's scans.我想从 python 中的 .netcdf 文件中获取一个温度变量,并对所有卫星扫描进行平均。

The temperature variable is given as:温度变量给出如下:

tdisk  = file.variables['tdisk'][:,:,:]               # Disk Temp(nscans,nlons,nlats)

The shape of the tdisk array is 68,52,46. tdisk 数组的形状是 68,52,46。 The satellite makes 68 scans per day.卫星每天进行 68 次扫描。 The longitude and latitude variables are given as:经度和纬度变量给出如下:

lats   = file.variables['latitude'][:,:]              # Latitude(nlons,nlats)
lons   = file.variables['longitude'][:,:]             # Longitude(nlons,nlats)

Which have sizes of 52,46.其大小为 52,46。 I would like to average the each nscan of temperature together to get a daily mean so the temperature size becomes 52,46.我想将每个 nscan 的温度平均在一起以获得每日平均值,因此温度大小变为 52,46。 I've seen ways to stack the arrays and concatenate them, but I would like a mean value.我已经看到了堆叠 arrays 并将它们连接起来的方法,但我想要一个平均值。 Eventually I am looking to make a contour plot with (x=longitude, y=latitude, and z=temp)最终我希望用(x=经度,y=纬度和 z=temp)制作轮廓 plot

Is this possible to do?这可能吗? Thanks for any help谢谢你的帮助

If you are using Xarray, you can do this using DataArray.mean :如果您使用的是 Xarray,则可以使用DataArray.mean执行此操作:

import xarray as xr


# open netcdf file
ds = xr.open_dataset('file.nc')

# take the mean of the tdisk variable
da = ds['tdisk'].mean(dim='nscans')

# make a contour plot
da.plot.contour('longitude', 'latitude')

Based on the question you seem to want to calculate a temporal mean, not a daily mean as you seem to only have one day.根据您似乎想要计算时间平均值的问题,而不是每天的平均值,因为您似乎只有一天。 So the following will probably work:所以以下可能会起作用:

ds.mean(“time”)

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

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