2017-03-27 16 views
0

netCDFファイルのデータに陸上/海のマスクを適用しようとしています。私のコードは動作しますが、ディスク上のファイルは変更されます。私はメモリにそれを行うことができるようにしたいと思いますが、変数は(240, 46, 400, 568)のような形状とだけでもnp.zerosを実行しているPythonでnetCDFデータに陸海のマスクを適用する

with Dataset('parameters/masks.nc', 'r') as mask, Dataset('remapped/' + ifile, 'a') as to_mask: 
    masked_data = np.zeros(to_mask['alk'].shape) 
    if len(to_mask['alk'].shape) == 4: # The dimensions are time,depth,lat,lon 
     for i in range(0, to_mask[var].shape[0]): 
      masked_data[i, :, :, :] = ma.masked_where(
       np.logical_not(np.array(mask['tmask'][0, :, :, :], dtype=bool)), 
       np.array(to_mask[var][i, :, :, :]))[:] 

ような何かをしようとする私にMemoryErrorを与えるを持っています。私はデータをロードしてメモリの問題なしで使うことができるので、私には奇妙に思える。

簡単な時間平均のようなデータを処理するために使いたい機能があるようですが、xarrayを見てきましたが、私がしようとしているようなマスク。

現在のコード:通常通り

from netCDF4 import Dataset 
import numpy as np 

with Dataset('parameters/masks.nc', 'r') as mask, Dataset('remapped/' + ifile, 'a') as to_mask: 
    for var in to_mask.variables: 
     if len(to_mask[var].shape) == 4: # The dimensions are time,depth,lat,lon 
      for i in range(0, to_mask[var].shape[0]): 
       to_mask[var][i, :, :, :] = ma.masked_where(
        np.logical_not(np.array(mask['tmask'][0, :, :, :], dtype=bool)), 
        np.array(to_mask[var][i, :, :, :]))[:] 

答えて

0

、ほんの数時間以上問題にして頭を強打することを助けることができます。私の作業コード(変数名の変更を許してください)は、私が思っていたようにxarrayを使います。

import xarray as xr 

with xr.open_dataset(mask_file) as m_f, xr.open_dataset(input_file) as i_f: 
    mask = m_f['tmask'][0, :, :, :].values 
    data = i_f[var].where(mask) 
関連する問題