Navigation : Top/netcdf

Matlabでnetcdfを使うときのメモ

Outline

  • netcdf -> mexncと改名
  • Combination
    • MEXNC + SNCTOOLS
    • MEXNC + NetCDF Toolbox netcdfファイルのインターフェースはsnctoolsとNetCDF Toolboxとなる.両者は共存できるが,snctoolsとNetCDF Toolboxではコマンド体系が異なるので(プログラムがこんがらがらないように)注意する.

MEXNC

MEXNC is a mex-file interface to NetCDF files for MATLAB, and has roughly a one-to-one equivalence with the C API for NetCDF. In fact, there's no better introduction to MEXNC than to read the NetCDF User's Guide for C provided by Unidata.

SNCTOOLS

SNCTOOLS is a set of m-files that sit on top of MEXNC. You might find them a little easier to use in day-to-day activities than the mex-file itself. Check the tutorial for some examples.

NetCDF Toolbox

Completely unsupported. It's available here as a download, but I didn't write it and so I can't answer any questions about it. Seriously. If you download it, you are on your own.

Linuxの場合 (2007b)

ダウンロード

http://mexcdf.sourceforge.net

  1. mexcdf.tar.gz をダウンロード
  2. snctools-X.zip をダウンロード
  3. netcdf_toolbox-1.XX.tar.gzをダウンロード

インストール

mexnc

  • mexcdf.tar.gzを解凍し、mexncの下にあるmakefileをコンパイルする.
  • ifortの環境ではmexopts.shに以下の変更が必要
    CC='icc'    
    NETCDF="/usr/local/netcdf-ifort"
  • mexncを生成.
    • make -f makefile
  • test
    >> mexnc ( 'STRERROR', 0 )
    ans =
    No error
    ! 注意) 環境に合わせてmexopt.sh(サンプル)の編集が必要
    • tests/test_mexnc.mを実施して,動作確認

snctools

  • unzip snctools-X.zip
  • pathを定義する.
  • tests/test_snctoos.mを実施して,動作確認

mexcdf and snctoolsの仕上げ

  • UnidataからtoolsUI version 2.2.18 (don't use 4.0)をダウンロード.
    >> javaaddpath ( '/home/jevans/matlab/classes/toolsUI-2.2.18.jar' ) ; 
    >> setpref ( 'SNCTOOLS', 'USE_JAVA', true ); % this requires SNCTOOLS 2.4.3 or better
  • USGS作成のマニュアル

NetCDF Toolbox

  • netcdf_toolbox-1.XX.tar.gzを解凍し以下のディレクトリーにパスを通す.
  • addpath /path/to/wherever/you/installed/this/netcdf
  • addpath /path/to/wherever/you/installed/this/netcdf/nctype
  • addpath /path/to/wherever/you/installed/this/netcdf/ncutility

Windows (2007b)の場合

ダウンロード

http://mexcdf.sourceforge.net

  1. mexcdf.tar.gz をダウンロード
    • 2.0.xのバージョンからLinux/Windows等すべて共通のファイルになった.
  2. snctools-X.zip をダウンロード
  3. netcdf_toolbox-1.XX.tar.gzをダウンロード
  4. netcdf-javaのtoolsUI.jarをダウンロード

インストール

mexnc

  • mexcdf.tar.gzを解凍し,netcdf.dllをmatlabのパスの通る場所におく
    • 例)C:\Users\mori\src\matlab\matlab_public\netcdf
    • mexcdfをaddpathする
    • tests/test_mexnc.mを実施して,動作確認
      • この時点ではjavaの設定をしていないので一部エラーが出る.
        >> mexnc ( 'STRERROR', 0 )
        ans =
        No error

snctools

  • unzip snctools-X.zip
  • pathを定義する.
  • tests/test_snctoos.mを実施して,動作確認

snctools

  • 解凍してパスを通すだけ

toolsUI

  • UnidataからtoolsUI version 2.2.18 (don't use 4.0)をダウンロード.
    >> javaaddpath ( '/C:\Users\mori\src\matlab\matlab_public\netcdf/toolsUI-2.2.18.jar' ) ; 
    >> setpref ( 'SNCTOOLS', 'USE_JAVA', true ); % this requires SNCTOOLS 2.4.3 or better
  • もう一度テストする
    • tests/test_mexnc.mを実施して,Java関係の動作確認

NetCDF Toolbox

startup.mに以下を記述する。

addpath /usr/local/matlab7/toolbox/mexnc
addpath /usr/local/matlab7/toolbox/netcdf_toolbox
addpath /usr/local/matlab7/toolbox/netcdf_toolbox/nctype
addpath /usr/local/matlab7/toolbox/netcdf_toolbox/ncutility

matlabを起動し、mexnc/test/test_mexnxを実行するとテストプログラムが起動する。 ちゃんと起動したら、正しくインストールされたことになる。

Archive

SNCTOOLS の使い方

NetCDF Toolbox の使い方

netCDFデータを読む

  • 文法
    • nc = netcdf('path', 'mode')
    • nc -- The output "netcdf" object, or [] if error.
    • path -- The path (filename) as a string.
    • mode -- The string 'nowrite' or 'write'.
    • nc = netcdf('foo.nc', 'nowrite')

ファイルを閉じる

  • 文法
    • result = close(nc)

ファイルの情報を得る

  • 文法
    • theDims = dim(nc)
    • theVars = var(nc)
    • theGAtts = att(nc)
    • theRecdim = recdim(nc)
  • 内容
    • nc -- "netcdf" object.
    • theDims -- List of "ncdim" dimension objects.
    • theVars -- List of "ncvar" variable objects.
    • theGAtts -- List of "ncatt" global attribute objects.
    • theRecdim -- The "ncdim" record-dimension object.
    • nc = netcdf('foo.nc', 'write')
    • if isempty(nc), error(' ## Bad netcdf operation.'), end
    • theDims = dim(nc) % List of "ncdim" dimension objects.
    • theVars = var(nc) % List of "ncvar" variable objects.
    • theGAtts = att(nc) % List of "ncatt" global attribute objects.
    • theRecdim = recdim(nc) % The "ncdim" record-dimension object.

オブジェクトからデータを得る

  • 文法
    • theVar = nc{'theVarname'};
    • theVar = nc{'theVarname'}(:);
    • theVar = ncvar('theVarname', nc);
  • 内容
    • theVar -- The "ncvar" variable object.
    • nc -- The parent "netcdf" object.
    • theVarname -- The string-name of the variable.
    • theVar = nc{'elevation'}

データを書き込む

nc=netcdf(fname,'write'); nc{'h'}(:)=h; close(nc);

Link