2017-04-18 141 views
0

我有一個netCDF文件,並試圖使用python netCDF4創建新變量。但是,程序未能將值分配給數組。NetCDF4將值分配給變量問題

下面是我的代碼

import netCDF4 
file = netCDF4.Dataset(filename, "r+") 
tmp = file.createVariable("tmp", "f4", ('time','height','lat','lon'), zlib=True) 
tmp.set_auto_mask(False) 
tmp.set_auto_maskandscale(False) 

//setting values 
tmp[0][0][0][0] = 0.1234 
print(tmp[0][0][0][0]) 
//I got 9.96921e+36, which seems to be the default fill value 

我應該怎麼做才能解決這個問題?

任何幫助,將不勝感激。

TIA

回答

0

訪問是在NetCDF文件變量就像下面這樣:

file.variables["tmp"][0,0,0,0] = 0.1234 
//read complete var 
putDataInHere = file.variables["tmp"][:] 

如果你看一下tmp變量和打印,你會看到這樣的事情:

<type "netCDF4._netCDF4.Variable"> 
float32 temp(time, level, lat, lon) 
    least_significant_digit: 3 
    units: K 
unlimited dimensions: time, level 
current shape = (0, 0, 73, 144) 
+0

因此,如果我想爲該位置分配一個新值,我會'file.variable [「tmp」] [0,0,0,0] = 0.1234'而不是'file.variable [「tmp」] [ 0] [0] [0] [0] = 0.1234'對不對? 我剛剛嘗試,似乎它的工作原理。準確地說@lpoorthuis –

+0

。看看[numpy索引](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html)。蟒蛇netcdf4使用numpy數組,所以索引是有點不同,然後本機Python列表 – lpoorthuis

+0

我看到。它現在工作順利。謝謝您的幫助。 @lpoorthuis –