operands could not be broadcast together with shapes (456,10368) (360,10368)のエラーが出ます
以下のプログラムを実行すると
operands could not be broadcast together with shapes (456,10368) (360,10368)
のエラーが出ます。おそらく
vara[:,grid_ocean_idx] = varm[:,grid_ocean_idx] - tmp[:,grid_ocean_idx]
で数があっていないためだと思いますがどのように修正すればよろしいでしょうか。
プログラム
YEAR_BEGIN = 1971
YEAR_END = 2016
nyear = 2010 - 1981 +1
nmon = 12
nc = Dataset(filei1,'r',format='NETCDF4')
latitude = nc.variables['lat'][:]
latitude_units = nc.variables['lat'].units
longitude = nc.variables['lon'][:]
longitude_units = nc.variables['lon'].units
lat_range = [np.min(latitude),np.max(latitude)]
long_range = [np.min(longitude),np.max(longitude)]
time = nc.variables['time'][:]
time_units = nc.variables['time'].units
dates = num2date(time, time_units,calendar = 'standard')
dates = pd.DatetimeIndex(dates)
idx = {}
idx['time'] = (dates.year >= YEAR_BEGIN) & (dates.year <= YEAR_END)
time = time[idx['time']]
ntime, nlat, nlong = len(time), len(latitude), len(longitude)
print(ntime,nlat,nlong)
# Array of var1 is (ntime,nlat,nlong)
var1 = np.zeros((ntime,nlat,nlong))
var1[:,:,:] = nc.variables[VAR][idx['time'],:,:]
#VAR1 = var1[;,;,;] * scale_factor + add_ofset
ntime = len(time)
nc.close()
# Reshape var1(ntime,nlat,nlong) to var2(ntime,ngrid)
ngrid = nlat*nlong
var2 = np.reshape(var1,(ntime,ngrid))
varm = np.full((ntime,ngrid),fill_value)
varc = np.full((nmon,ngrid),fill_value)
vara = np.full((ntime,ngrid),fill_value)
# land mask (the grid is filled with missing_value or _FillValue)
landmask = np.zeros(ngrid,dtype=np.int)
tmp = np.arange(ngrid,dtype=float)
tmp[:] = np.reshape(var2[0,:],(ngrid))
landmask[:] = 1
if VAR == 'sst':
grid_ocean_idx = np.where(tmp < 400.)[0]
# grid_ocean_idx = np.where(tmp != miss_value)[0]
else:
grid_ocean_idx = np.where(tmp != fill_value)[0]
ngrid_ocean = len(np.array(grid_ocean_idx))
landmask[grid_ocean_idx] = 0
varm[:,grid_ocean_idx] = var2[:,grid_ocean_idx]
varm[:,:] = var2[:,:]
# Calculate the monthly climatologies (monthly long-term mean of 月ごとの気候を計算する(月ごとの長期平均
# C.E. 1971-2000 (30 years)
# vara, varc = remove_cycle(var2,nmon,grid_ocean_idx,nyear)
ntime_clim = nmon*30
for imon in range(nmon):
varc[imon,grid_ocean_idx] = np.mean(var2[imon:ntime_clim-1:nmon,grid_ocean_idx],axis=0)
tmp=np.tile(varc,(nyear,1))
vara[:,grid_ocean_idx] = varm[:,grid_ocean_idx] - tmp[:,grid_ocean_idx]