我有一個很大的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