IT pass HikiWiki - [Exp2020]NetCDF データの内容確認 Diff

  • Added parts are displayed like this.
  • Deleted parts are displayed like this.

((<"スケジュール表・各回資料 (09/30)"|[Exp2020]スケジュール表・各回資料#09-2F30>))

{{toc}}

((<データと可視化ソフトウェアの実習準備準備|[Exp2020]データと可視化ソフトウェアの実習準備準備>))でダウンロードした NetCDF データの内容を確認してみましょう.

= NetCDF データの全体を確認する

NetCDF 形式のファイルにはデータが Binary 形式で保存されているため, cat/more/less などのコマンドではその内容を確認することができません.
例えば,

  $ more air.2019.nc

としても文字化けして内容を理解することができないでしょう.

NetCDF 形式のデータの内容を確認するためのコマンドは ncdump です.
下のように使ってみましょう.

  $ ncdump air.2019.nc | less

"|" はパイプで, ここでは ncdump の出力結果を less を使って見ています.
(less を終了するには q を押しましょう.)

パイプと less は必ずしも必要ではありません.
しかし, 一般に NetCDF データは膨大なサイズになり, ncdump はファイルの内容をすべて画面 (標準出力) に表示するため, less などを使わない限り見たい部分を見ることができません.

上の ncdump を使用例では下のように表示されるでしょう.
(右側に示した # 以降の文字は説明のために追記したものですので ncdump の出力には含まれません.)

netcdf air.2019 {
dimensions:                                        # 次元の名前, サイズ
        level = 17 ;
        lat = 73 ;
        lon = 144 ;
        time = UNLIMITED ; // (365 currently)
        nbnds = 2 ;
variables:                                         # 変数の型, 名前, 属性
        float level(level) ;                        # 変数の型, 名前, 次元
                level:units = "millibar" ;          # 変数の単位
                level:actual_range = 1000.f, 10.f ; # 変数の取りうる範囲
                level:long_name = "Level" ;         # 変数の長い名前
                level:positive = "down" ;           # 値が増加する向き
                level:GRIB_id = 100s ;
                level:GRIB_name = "hPa" ;
                level:axis = "Z" ;
        float lat(lat) ;                            # 変数の型, 名前, 次元
                lat:units = "degrees_north" ;       # 変数の単位
                lat:actual_range = 90.f, -90.f ;    # 変数の取りうる範囲
                lat:long_name = "Latitude" ;        # 変数の長い名前
                lat:standard_name = "latitude" ;    # 変数の標準的な名前
                lat:axis = "Y" ;
        float lon(lon) ;
                lon:units = "degrees_east" ;
                lon:long_name = "Longitude" ;
                lon:actual_range = 0.f, 357.5f ;
                lon:standard_name = "longitude" ;
                lon:axis = "X" ;
        double time(time) ;
                time:long_name = "Time" ;
                time:delta_t = "0000-00-01 00:00:00" ;
                time:standard_name = "time" ;
                time:axis = "T" ;
                time:units = "hours since 1800-01-01 00:00:0.0" ;
                time:avg_period = "0000-00-01 00:00:00" ;
                time:coordinate_defines = "start" ;
                time:actual_range = 1919712., 1928448. ;
        float air(time, level, lat, lon) ;
                air:long_name = "mean Daily Air temperature" ;
                air:units = "degK" ;
                air:precision = 2s ;
                air:least_significant_digit = 1s ;
                air:GRIB_id = 11s ;
                air:GRIB_name = "TMP" ;
                air:var_desc = "Air temperature" ;
                air:level_desc = "Multiple levels" ;
                air:statistic = "Mean" ;
                air:parent_stat = "Individual Obs" ;
                air:missing_value = -9.96921e+36f ;
                air:valid_range = 150.f, 350.f ;
                air:dataset = "NCEP Reanalysis Daily Averages" ;
                air:actual_range = 178.35f, 320.05f ;
        double time_bnds(time, nbnds) ;

// global attributes:                                                      # 大域属性
                :Conventions = "COARDS" ;                                   # 規約名
                :title = "mean daily NMC reanalysis (2014)" ;               # タイトル
                :history = "created 2013/12 by Hoop (netCDF2.3)" ;          # データ生成の沿革
                :description = "Data is from NMC initialized reanalysis\n", # データの説明
                        "(4x/day).  It consists of most variables interpolated to\n",
                        "pressure surfaces from model (sigma) surfaces." ;
                :platform = "Model" ;
                :References = "http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.html" ;
                :dataset_title = "NCEP-NCAR Reanalysis 1" ;
data:                                                     # 変数の値

level = 1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50,
    30, 20, 10 ;

lat = 90, 87.5, 85, 82.5, 80, 77.5, 75, 72.5, 70, 67.5, 65, 62.5, 60, 57.5,
    55, 52.5, 50, 47.5, 45, 42.5, 40, 37.5, 35, 32.5, 30, 27.5, 25, 22.5, 20,
    17.5, 15, 12.5, 10, 7.5, 5, 2.5, 0, -2.5, -5, -7.5, -10, -12.5, -15,
    -17.5, -20, -22.5, -25, -27.5, -30, -32.5, -35, -37.5, -40, -42.5, -45,
    -47.5, -50, -52.5, -55, -57.5, -60, -62.5, -65, -67.5, -70, -72.5, -75,
    -77.5, -80, -82.5, -85, -87.5, -90 ;

lon = 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, 22.5, 25, 27.5, 30, 32.5, 35,
    37.5, 40, 42.5, 45, 47.5, 50, 52.5, 55, 57.5, 60, 62.5, 65, 67.5, 70,
    72.5, 75, 77.5, 80, 82.5, 85, 87.5, 90, 92.5, 95, 97.5, 100, 102.5, 105,
    107.5, 110, 112.5, 115, 117.5, 120, 122.5, 125, 127.5, 130, 132.5, 135,
    137.5, 140, 142.5, 145, 147.5, 150, 152.5, 155, 157.5, 160, 162.5, 165,
    167.5, 170, 172.5, 175, 177.5, 180, 182.5, 185, 187.5, 190, 192.5, 195,
    197.5, 200, 202.5, 205, 207.5, 210, 212.5, 215, 217.5, 220, 222.5, 225,
    227.5, 230, 232.5, 235, 237.5, 240, 242.5, 245, 247.5, 250, 252.5, 255,
    257.5, 260, 262.5, 265, 267.5, 270, 272.5, 275, 277.5, 280, 282.5, 285,
    287.5, 290, 292.5, 295, 297.5, 300, 302.5, 305, 307.5, 310, 312.5, 315,
    317.5, 320, 322.5, 325, 327.5, 330, 332.5, 335, 337.5, 340, 342.5, 345,
    347.5, 350, 352.5, 355, 357.5 ;

time = 1919712, 1919736, 1919760, 1919784, 1919808, 1919832, 1919856,
    1919880, 1919904, 1919928, 1919952, 1919976, 1920000, 1920024, 1920048,
    1920072, 1920096, 1920120, 1920144, 1920168, 1920192, 1920216, 1920240,
    1920264, 1920288, 1920312, 1920336, 1920360, 1920384, 1920408, 1920432,
    1920456, 1920480, 1920504, 1920528, 1920552, 1920576, 1920600, 1920624,
    1920648, 1920672, 1920696, 1920720, 1920744, 1920768, 1920792, 1920816,
    1920840, 1920864, 1920888, 1920912, 1920936, 1920960, 1920984, 1921008,
    1921032, 1921056, 1921080, 1921104, 1921128, 1921152, 1921176, 1921200,
    1921224, 1921248, 1921272, 1921296, 1921320, 1921344, 1921368, 1921392,
    1921416, 1921440, 1921464, 1921488, 1921512, 1921536, 1921560, 1921584,
    1921608, 1921632, 1921656, 1921680, 1921704, 1921728, 1921752, 1921776,
    1921800, 1921824, 1921848, 1921872, 1921896, 1921920, 1921944, 1921968,
    1921992, 1922016, 1922040, 1922064, 1922088, 1922112, 1922136, 1922160,
    1922184, 1922208, 1922232, 1922256, 1922280, 1922304, 1922328, 1922352,
    1922376, 1922400, 1922424, 1922448, 1922472, 1922496, 1922520, 1922544,
    1922568, 1922592, 1922616, 1922640, 1922664, 1922688, 1922712, 1922736,
    1922760, 1922784, 1922808, 1922832, 1922856, 1922880, 1922904, 1922928,
    1922952, 1922976, 1923000, 1923024, 1923048, 1923072, 1923096, 1923120,
    1923144, 1923168, 1923192, 1923216, 1923240, 1923264, 1923288, 1923312,
    1923336, 1923360, 1923384, 1923408, 1923432, 1923456, 1923480, 1923504,
    1923528, 1923552, 1923576, 1923600, 1923624, 1923648, 1923672, 1923696,
    1923720, 1923744, 1923768, 1923792, 1923816, 1923840, 1923864, 1923888,
    1923912, 1923936, 1923960, 1923984, 1924008, 1924032, 1924056, 1924080,
    1924104, 1924128, 1924152, 1924176, 1924200, 1924224, 1924248, 1924272,
    1924296, 1924320, 1924344, 1924368, 1924392, 1924416, 1924440, 1924464,
    1924488, 1924512, 1924536, 1924560, 1924584, 1924608, 1924632, 1924656,
    1924680, 1924704, 1924728, 1924752, 1924776, 1924800, 1924824, 1924848,
    1924872, 1924896, 1924920, 1924944, 1924968, 1924992, 1925016, 1925040,
    1925064, 1925088, 1925112, 1925136, 1925160, 1925184, 1925208, 1925232,
    1925256, 1925280, 1925304, 1925328, 1925352, 1925376, 1925400, 1925424,
    1925448, 1925472, 1925496, 1925520, 1925544, 1925568, 1925592, 1925616,
    1925640, 1925664, 1925688, 1925712, 1925736, 1925760, 1925784, 1925808,
    1925832, 1925856, 1925880, 1925904, 1925928, 1925952, 1925976, 1926000,
    1926024, 1926048, 1926072, 1926096, 1926120, 1926144, 1926168, 1926192,
    1926216, 1926240, 1926264, 1926288, 1926312, 1926336, 1926360, 1926384,
    1926408, 1926432, 1926456, 1926480, 1926504, 1926528, 1926552, 1926576,
    1926600, 1926624, 1926648, 1926672, 1926696, 1926720, 1926744, 1926768,
    1926792, 1926816, 1926840, 1926864, 1926888, 1926912, 1926936, 1926960,
    1926984, 1927008, 1927032, 1927056, 1927080, 1927104, 1927128, 1927152,
    1927176, 1927200, 1927224, 1927248, 1927272, 1927296, 1927320, 1927344,
    1927368, 1927392, 1927416, 1927440, 1927464, 1927488, 1927512, 1927536,
    1927560, 1927584, 1927608, 1927632, 1927656, 1927680, 1927704, 1927728,
    1927752, 1927776, 1927800, 1927824, 1927848, 1927872, 1927896, 1927920,
    1927944, 1927968, 1927992, 1928016, 1928040, 1928064, 1928088, 1928112,
    1928136, 1928160, 1928184, 1928208, 1928232, 1928256, 1928280, 1928304,
    1928328, 1928352, 1928376, 1928400, 1928424, 1928448 ;

air =
  245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8, 245.8,
    245.8, 245.8, 245.8, 245.8,
  245.7, 245.675, 245.625, 245.6, 245.6, 245.55, 245.55, 245.55, 245.525,
    245.525, 245.5, 245.5, 245.525, 245.5, 245.525, 245.525, 245.55, 245.575,
    245.55, 245.625, 245.625, 245.65, 245.7, 245.725, 245.725, 245.8, 245.8,
    245.875, 245.875, 245.9, 245.95, 246, 246.025, 246.025, 246.05, 246.1,
    246.125, 246.125, 246.175, 246.175, 246.175, 246.2, 246.2, 246.2, 246.2,
    246.15, 246.15, 246.15, 246.125, 246.075, 246.05, 246.05, 245.975,
    245.95, 245.875, 245.85, 245.775, 245.7, 245.625, 245.6, 245.525,
  ...
}


NetCDF 形式のファイルには様々な情報が保存されていることがわかります.


* "dimensions" の項目には, 次元(軸)の名前と長さが表示されます.
  上の例では, level, lat, lon, time, nbnds という 5 種類の次元(軸)が定義されており, それぞれの長さが 17, 73, 144, 無制限 (現時点では 365), 2 であることがわかります.
  * NetCDF では, データの増減に合わせて変化することのできる「無制限」の長さを持った軸 (time) を定義することができます.

* "variables" の項目には, 変数の型, 名前, 属性が表示されます.
  NetCDF では次元(軸)の値も同名の変数として保存しておくことが望ましく, 上の例では慣例に倣って次元と同じ名前の変数として属性と値が含まれています.
  変数の属性に単位と変数の長い名前 (より詳しい名前) 等を含めることでわかりやすいデータとなっています.
  上の例では, 下の次元が含まれていることがわかります.
  * この項目の説明から, 変数 air がどのような変数/物理量であるかがわかります.
    * 実数型 : float
    * 次元 :  4 次元 (time, level, lat, lon)
    * 変数の意味 : long_name = "mean Daily Air temperature" (日平均大気温度)
    * 変数の単位 : units = "degK" ;
    * 変数の欠損値 : missing_value = -9.96921e+36f ;

* "global attributes" の項目には, ファイル全体に含まれるデータ全体の属性が表示されます. "Convention" にはデータ格納の規約の名前が書かれています.
  * NetCDF はデータ格納の形式であり, その形式でどのような(メタ)データを保存するかについては決まりがあるわけではありません. したがって, 上の例のような変数の単位や説明といったメタデータをほとんど含まない NetCDF ファイルを作成することも可能です.
    しかしそれでは使いやすい/わかりやすいデータにならないため, 研究者が議論して使いやすい/わかりやすいデータとするための決まり (規約) を定めています. その規約にも複数の種類(バージョン)があるため, "Convention" の欄にその名前を保存しています.

* "data" の項目には, ファイルが含むデータの値が表示されます.
  上に述べたように, 次元(軸)の値も保存されています.

= ncdump のオプション

ncdump には様々なオプションがあります.

== -h オプション

格納されている次元や変数の情報のみを確認するには, 例えば,

  $ ncdump -h air.2019.nc

とすると, データの値を表示せずヘッダー部分だけを表示することができます.
NetCDF ファイルに保存されるデータは膨大になることがありますので, 保存されている変数情報のみを確認したいときには大変役に立ちます.

この例では下のように表示されるでしょう.

netcdf air.2019 {
dimensions:
        level = 17 ;
        lat = 73 ;
        lon = 144 ;
        time = UNLIMITED ; // (365 currently)
        nbnds = 2 ;
variables:
        float level(level) ;
                level:units = "millibar" ;
                level:actual_range = 1000.f, 10.f ;
                level:long_name = "Level" ;
                level:positive = "down" ;
                level:GRIB_id = 100s ;
                level:GRIB_name = "hPa" ;
                level:axis = "Z" ;
        float lat(lat) ;
                lat:units = "degrees_north" ;
                lat:actual_range = 90.f, -90.f ;
                lat:long_name = "Latitude" ;
                lat:standard_name = "latitude" ;
                lat:axis = "Y" ;
        float lon(lon) ;
                lon:units = "degrees_east" ;
                lon:long_name = "Longitude" ;
                lon:actual_range = 0.f, 357.5f ;
                lon:standard_name = "longitude" ;
                lon:axis = "X" ;
        double time(time) ;
                time:long_name = "Time" ;
                time:delta_t = "0000-00-01 00:00:00" ;
                time:standard_name = "time" ;
                time:axis = "T" ;
                time:units = "hours since 1800-01-01 00:00:0.0" ;
                time:avg_period = "0000-00-01 00:00:00" ;
                time:coordinate_defines = "start" ;
                time:actual_range = 1919712., 1928448. ;
        float air(time, level, lat, lon) ;
                air:long_name = "mean Daily Air temperature" ;
                air:units = "degK" ;
                air:precision = 2s ;
                air:least_significant_digit = 1s ;
                air:GRIB_id = 11s ;
                air:GRIB_name = "TMP" ;
                air:var_desc = "Air temperature" ;
                air:level_desc = "Multiple levels" ;
                air:statistic = "Mean" ;
                air:parent_stat = "Individual Obs" ;
                air:missing_value = -9.96921e+36f ;
                air:valid_range = 150.f, 350.f ;
                air:dataset = "NCEP Reanalysis Daily Averages" ;
                air:actual_range = 178.35f, 320.05f ;
        double time_bnds(time, nbnds) ;

// global attributes:
                :Conventions = "COARDS" ;
                :title = "mean daily NMC reanalysis (2014)" ;
                :history = "created 2013/12 by Hoop (netCDF2.3)" ;
                :description = "Data is from NMC initialized reanalysis\n",
                        "(4x/day).  It consists of most variables interpolated to\n",
                        "pressure surfaces from model (sigma) surfaces." ;
                :platform = "Model" ;
                :References = "http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.html" ;
                :dataset_title = "NCEP-NCAR Reanalysis 1" ;
}


== -v オプション

特定の変数の値のみを確認したいときには, 例えば,

  $ ncdump -v lat air.2019.nc

とすると, ヘッダーと変数 lat の値のみを表示することができます.
膨大なデータの中で特定の変数の値のみを確認したいときには役に立ちます.

この例では下のように表示されるでしょう.

netcdf air.2019 {
dimensions:
        level = 17 ;
        lat = 73 ;
        lon = 144 ;
        time = UNLIMITED ; // (365 currently)
        nbnds = 2 ;
variables:
        float level(level) ;
                level:units = "millibar" ;
                level:actual_range = 1000.f, 10.f ;
                level:long_name = "Level" ;
                level:positive = "down" ;
                level:GRIB_id = 100s ;
                level:GRIB_name = "hPa" ;
                level:axis = "Z" ;
        float lat(lat) ;
                lat:units = "degrees_north" ;
                lat:actual_range = 90.f, -90.f ;
                lat:long_name = "Latitude" ;
                lat:standard_name = "latitude" ;
                lat:axis = "Y" ;
        float lon(lon) ;
                lon:units = "degrees_east" ;
                lon:long_name = "Longitude" ;
                lon:actual_range = 0.f, 357.5f ;
                lon:standard_name = "longitude" ;
                lon:axis = "X" ;
        double time(time) ;
                time:long_name = "Time" ;
                time:delta_t = "0000-00-01 00:00:00" ;
                time:standard_name = "time" ;
                time:axis = "T" ;
                time:units = "hours since 1800-01-01 00:00:0.0" ;
                time:avg_period = "0000-00-01 00:00:00" ;
                time:coordinate_defines = "start" ;
                time:actual_range = 1919712., 1928448. ;
        float air(time, level, lat, lon) ;
                air:long_name = "mean Daily Air temperature" ;
                air:units = "degK" ;
                air:precision = 2s ;
                air:least_significant_digit = 1s ;
                air:GRIB_id = 11s ;
                air:GRIB_name = "TMP" ;
                air:var_desc = "Air temperature" ;
                air:level_desc = "Multiple levels" ;
                air:statistic = "Mean" ;
                air:parent_stat = "Individual Obs" ;
                air:missing_value = -9.96921e+36f ;
                air:valid_range = 150.f, 350.f ;
                air:dataset = "NCEP Reanalysis Daily Averages" ;
                air:actual_range = 178.35f, 320.05f ;
        double time_bnds(time, nbnds) ;

// global attributes:
                :Conventions = "COARDS" ;
                :title = "mean daily NMC reanalysis (2014)" ;
                :history = "created 2013/12 by Hoop (netCDF2.3)" ;
                :description = "Data is from NMC initialized reanalysis\n",
                        "(4x/day).  It consists of most variables interpolated to\n",
                        "pressure surfaces from model (sigma) surfaces." ;
                :platform = "Model" ;
                :References = "http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.html" ;
                :dataset_title = "NCEP-NCAR Reanalysis 1" ;
data:

lat = 90, 87.5, 85, 82.5, 80, 77.5, 75, 72.5, 70, 67.5, 65, 62.5, 60, 57.5,
    55, 52.5, 50, 47.5, 45, 42.5, 40, 37.5, 35, 32.5, 30, 27.5, 25, 22.5, 20,
    17.5, 15, 12.5, 10, 7.5, 5, 2.5, 0, -2.5, -5, -7.5, -10, -12.5, -15,
    -17.5, -20, -22.5, -25, -27.5, -30, -32.5, -35, -37.5, -40, -42.5, -45,
    -47.5, -50, -52.5, -55, -57.5, -60, -62.5, -65, -67.5, -70, -72.5, -75,
    -77.5, -80, -82.5, -85, -87.5, -90 ;
}


= マニュアル

== man

ncdump の詳しい解説は, man コマンドで確認できます.

  $ man ncdump


== 引数なし実行 (オプションとは言いませんが)

ncdump を引数なしで (ファイル名も指定せずに) 実行すると help が表示されます.

  $ ncdump

のようにすると, 下のように表示されます.

ncdump [-c|-h] [-v ...] [[-b|-f] [c|f]] [-l len] [-n name] [-p n[,n]] [-k] [-x] [-s] [-t|-i] [-g ...] [-w] [-Ln] file
  [-c]             Coordinate variable data and header information
  [-h]             Header information only, no data
  [-v var1[,...]]  Data for variable(s) <var1>,... only
  [-b [c|f]]       Brief annotations for C or Fortran indices in data
  [-f [c|f]]       Full annotations for C or Fortran indices in data
  [-l len]         Line length maximum in data section (default 80)
  [-n name]        Name for netCDF (default derived from file name)
  [-p n[,n]]       Display floating-point values with less precision
  [-k]             Output kind of netCDF file
  [-s]             Output special (virtual) attributes
  [-t]             Output time data as date-time strings
  [-i]             Output time data as date-time strings with ISO-8601 'T' separator
  [-g grp1[,...]]  Data and metadata for group(s) <grp1>,... only
  [-w]             With client-side caching of variables for DAP URLs
  [-x]             Output XML (NcML) instead of CDL
  [-Xp]            Unconditionally suppress output of the properties attribute
  [-Ln]            Set log level to n (>= 0); ignore if logging not enabled.
  file             Name of netCDF file (or URL if DAP access enabled)
netcdf library version 4.4.1.1 of Apr  4 2017 06:09:19 $

ncdump の様々なオプションを確認することができます.

また, ((<ここ|URL:http://www.gfd-dennou.org/arch/ucar/netcdf-jman/guidef/netCDF_ja91.html>))には, 地球流体電脳倶楽部の有志が日本語訳したマニュアルがありますので, 必要に応じて参考にするとよいでしょう.