我有一个很大的4D数据集,需要从它创建一个更小的4D数组。我对python相当陌生,用于IDL或matlab。我读了我的值,然后使用where函数,找到我需要的更小的一维数组的每个维度的索引号。我想创建这些索引号的新数组,但我不断收到形状不匹配错误(不能broacast到一个单一的形状。如何从一个较大的数组创建一个较小的数组python
import numpy as n
import matplotlib.pyplot as plt
import Scientific.IO.NetCDF as S
file=S.NetCDFFile('wspd.mon.mean.nc',mode='r') #Opening File
Lat=file.variables['lat'].getValue() # Reading in the latitude variables, 73
Lon=file.variables['lon'].getValue() # Reading in the longitude variables, 144
Level=file.variables['level'].getValue() # Reading in the levels, 17 of them
Defaulttime=file.variables['time'].getValue() # Reading in the time, hrs since 1-1-1
Defaultwindspeed=file.variables['wspd'].getValue() # Reading in the windspeed(time, level, lat, lon)
Time=n.arange(len(Defaulttime))/12.+1948 #Creates time array into readable years with 12 months
goodtime=n.where((Time>=1948)&(Time<2013)) #Creates a time array for the years that I want, 1948-2012, since 2013 only has until October, I will not be using that data.
goodlat=n.where((Lat>=35)&(Lat<=50)) #Latitudes where the rockies and plains are in the US
plainslon=n.where((Lon>=275)&(Lon<=285))
Windspeedsplains=Defaultwindspeed[goodtime,:,goodlat,plainslon]
以下信息由上述的(最后一行行所产生的错误码)。
>>>ValueError: shape mismatch: objects cannot be broadcast to a single shape
我怀疑任何人都可以帮你解决这个问题。你至少可以做的是找出哪条线实际上是失败的 – Hammer
'Defaultwindspeed'实际上是4D吗?使用'Defaultwindspeed.shape'或'Defaultwindspeed.ndim'来检查... – atomh33ls
@ atomh33ls它必须是,否则错误会是'索引太多'。我认为问题的形式是“goodtime”,“goodlat”和“plainslon”。 @Cwilliams,如果你印刷'goodtime.shape,goodlat.shape,plainslon.shape',你会得到什么? – askewchan