Netcdf4:在编辑时保存更改

时间:2019-05-13 14:05:11

标签: python-3.x netcdf4

我的netcdf文件有问题,其中包括平均海平面压力数据。我需要对该数据进行EOF分析,因此需要使用纬度余弦的平方根对数据进行加权。我打开数据集,提取变量,然后将它们乘以权重。

我检查了在加权之前和之后以固定的时间序列和经度打印MSLP值的计算。第二次打印是正确的,并且正是我想要的,但是数据似乎没有保存。实际上,当(关闭数据集后)我重新打开它并重新打印数据时,它们是原始数据,并且没有更改。

Python:

dset = Dataset(newpath,'r+')
lat=dset.variables['lat'][:]
msl=dset.variables['msl'][:]

j=0 #lat counter
print(msl[0,:,0])   #first print check (fixed times series and longitude)
while j<160:
    msl[:,j,:]=msl[:,j,:]*math.sqrt(math.fabs(math.cos(lat[j]*0.015708))
   j=j+1
print(msl[0,:,0])   #second print check (correct weight in output)
dset.close()

print ('____________________________________________________')

dset = Dataset(newpath)
lat=dset.variables['lat'][:]
msl=dset.variables['msl'][:]
print(msl[0,:,0])    #third print check (original data)
dset.close()

输出:

[101021.06 100858.19 100738.31 100654.19 100615.19 100593.69 100562.56
100511.81 100424.94 100336.31 100258.81 100210.44 100158.69 100104.44
100056.94 100008.06  99980.31  99977.81  99994.44 100021.31 100072.69
100129.69 100195.56 100277.31 100384.06 100517.81 100673.69 100818.69
100937.81 101032.44 101123.56 101225.31 101333.56 101422.81 101508.94
101596.69 101717.69 101827.06 101929.19 102002.31 102085.81 102153.69
102106.94 101980.81 101912.19 101924.94 101964.31 102036.94 102144.56
102230.81 102190.31 102142.06 102129.06 102114.19 102074.81 102042.19
102006.56 101979.81 101939.44 101901.81 101856.69 101799.69 101737.31
101677.81 101612.69 101551.06 101489.06 101424.19 101343.06 101250.44
101160.81 101095.19 101048.06 101033.31 101053.56 101084.81 101087.44
101099.44 101110.44 101126.56 101138.06 101147.31 101155.94 101171.19
101184.44 101209.94 101230.31 101256.69 101282.19 101309.06 101336.06
101372.81 101408.69 101452.81 101490.69 101540.81 101587.69 101642.81
101695.19 101751.56 101803.31 101860.44 101909.06 101956.31 101996.06
102028.94 102050.44 102060.56 102055.81 102040.56 102016.69 101979.31
101924.06 101845.06 101743.94 101633.31 101505.81 101370.44 101216.69
101061.56 100902.94 100739.06 100571.44 100416.94 100282.56 100150.94
100012.94  99876.06  99737.19  99624.56  99533.69  99483.81  99457.81
 99473.81  99510.69  99571.06  99627.94  99690.19  99731.69  99777.81
 99788.44  99837.69  99868.44  99936.81  99964.44 100129.44 100214.56
100257.94 100333.81 100380.06 100444.94 100485.81 100578.06 100684.31
100783.81 100866.69 100939.44 100999.81 101061.06 101111.31]
   [ 41619.645  43605.746  45517.273  47355.477  49134.914  50851.395
 52497.957  54074.137  55574.52   57021.02   58424.58   59799.477
 61129.76   62418.176  63672.594  64889.86   66086.44   67267.34
 68430.6    69571.84   70702.53   71810.86   72900.21   73976.79
 75048.58   76118.43   77183.93   78220.58   79216.72   80173.18
 81106.555  82028.58   82936.46   83809.59   84661.07   85495.07
 86338.734  87154.586  87946.35   88695.16   89435.1    90143.61
 90732.586  91231.664  91762.88   92349.1    92941.94   93548.02
 94169.59   94755.02   95205.89   95631.93   96073.62   96496.445
 96879.016  97250.93   97603.02   97946.78   98260.68   98560.51
 98836.43   99084.21   99310.16   99522.38   99712.65   99889.96
100050.59  100192.15  100301.43  100383.13  100451.66  100527.97
100606.76  100701.945 100816.305 100925.96  100991.34  101050.41
101092.79  101124.6   101136.1   101129.66  101106.88  101075.016
101025.43  100972.31  100898.305 100814.47  100713.92  100598.87
100468.02  100330.875 100176.836 100014.86   99830.57   99642.12
 99434.164  99217.91   98982.5    98734.42   98465.234  98184.53
 97878.83   97554.945  97206.97   96835.48   96436.2    96009.18
 95551.17   95066.375  94556.56   94017.25   93444.54   92833.24
 92185.11   91511.78   90806.73   90078.13   89316.77   88537.77
 87739.2    86919.49   86079.88   85234.65   84389.22   83528.47
 82644.56   81743.57   80822.664  79904.28   78983.86   78075.61
 77164.734  76264.016  75355.77   74440.77   73497.734  72532.51
 71525.42   70494.195  69409.86   68323.33   67193.75   66057.68
 64861.55   63718.73   62486.844  61190.336  59873.098  58495.234
 57082.75   55608.207  54109.78   52561.516  50947.504  49257.734
 47489.68   45635.43   43693.46   41656.83 ]
____________________________________________________

[101021.06 100858.19 100738.31 100654.19 100615.19 100593.69 100562.56
100511.81 100424.94 100336.31 100258.81 100210.44 100158.69 100104.44
100056.94 100008.06  99980.31  99977.81  99994.44 100021.31 100072.69
100129.69 100195.56 100277.31 100384.06 100517.81 100673.69 100818.69
100937.81 101032.44 101123.56 101225.31 101333.56 101422.81 101508.94
101596.69 101717.69 101827.06 101929.19 102002.31 102085.81 102153.69
102106.94 101980.81 101912.19 101924.94 101964.31 102036.94 102144.56
102230.81 102190.31 102142.06 102129.06 102114.19 102074.81 102042.19
102006.56 101979.81 101939.44 101901.81 101856.69 101799.69 101737.31
101677.81 101612.69 101551.06 101489.06 101424.19 101343.06 101250.44
101160.81 101095.19 101048.06 101033.31 101053.56 101084.81 101087.44
101099.44 101110.44 101126.56 101138.06 101147.31 101155.94 101171.19
101184.44 101209.94 101230.31 101256.69 101282.19 101309.06 101336.06
101372.81 101408.69 101452.81 101490.69 101540.81 101587.69 101642.81
101695.19 101751.56 101803.31 101860.44 101909.06 101956.31 101996.06
102028.94 102050.44 102060.56 102055.81 102040.56 102016.69 101979.31
101924.06 101845.06 101743.94 101633.31 101505.81 101370.44 101216.69
101061.56 100902.94 100739.06 100571.44 100416.94 100282.56 100150.94
100012.94  99876.06  99737.19  99624.56  99533.69  99483.81  99457.81
 99473.81  99510.69  99571.06  99627.94  99690.19  99731.69  99777.81
 99788.44  99837.69  99868.44  99936.81  99964.44 100129.44 100214.56
100257.94 100333.81 100380.06 100444.94 100485.81 100578.06 100684.31
100783.81 100866.69 100939.44 100999.81 101061.06 101111.31]

1 个答案:

答案 0 :(得分:1)

这时在脚本的开头:

msl=dset.variables['msl'][:]

msl只是一个Numpy数组,不再与您的NetCDF文件相关,因此您需要将该数组写回到NetCDF文件:

dset.variables['msl'][:] = msl

在第一次关闭NetCDF文件之前。