バイナリファイルは、コンピュータが直接的に処理するために2進数で表現されるデータであるため、人間の目には、そのファイルの中のデータを見ても分かりません。
しかし、NetCDFファイルは、バイナリファイルでありながらも、ncdumpコマンドを用いることで、どのようなデータが入っているのか確認できます。
NetCDFファイルは気象、海洋、気候変動などの分野で広く使われています。
ということで、本記事では、NetCDFファイルを扱う際に必須であるncdumpコマンドの使い方を紹介します。
ncdump
先述の通り、NetCDFファイルの全体像を把握するために、ncdumpコマンドは用いられます。
ncdumpコマンドの引数にファイル名を指定しましょう。
例では、気象庁のMSMデータです。
$ ncdump MSM20200717P.nc
実行結果
netcdf MSM2020071700P.nc {
dimensions:
lon = 241 ;
lat = 253 ;
p = 16 ;
time = 12 ;
variables:
float lon(lon) ;
lon:long_name = "longitude" ;
lon:units = "degrees_east" ;
lon:standard_name = "longitude" ;
float lat(lat) ;
lat:long_name = "latitude" ;
lat:units = "degrees_north" ;
lat:standard_name = "latitude" ;
float p(p) ;
p:long_name = "pressure level" ;
p:units = "hPa" ;
p:standard_name = "air_pressure" ;
float time(time) ;
time:long_name = "time" ;
time:units = "hours since 2020-07-17 00:00:00+00:00" ;
time:standard_name = "time" ;
float z(time, p, lat, lon) ;
z:long_name = "geopotential height" ;
z:units = "m" ;
z:standard_name = "geopotential_height" ;
float w(time, p, lat, lon) ;
w:long_name = "vertical velocity in p" ;
w:units = "Pa/s" ;
w:standard_name = "lagrangian_tendency_of_air_pressure" ;
short u(time, p, lat, lon) ;
u:scale_factor = 0.006116208155 ;
u:add_offset = 0. ;
u:long_name = "eastward component of wind" ;
u:units = "m/s" ;
u:standard_name = "eastward_wind" ;
short v(time, p, lat, lon) ;
v:scale_factor = 0.006116208155 ;
v:add_offset = 0. ;
v:long_name = "northward component of wind" ;
v:units = "m/s" ;
v:standard_name = "northward_wind" ;
short temp(time, p, lat, lon) ;
temp:scale_factor = 0.002613491379 ;
temp:add_offset = 255.4004974 ;
temp:long_name = "temperature" ;
temp:units = "K" ;
temp:standard_name = "air_temperature" ;
short rh(time, p, lat, lon) ;
rh:scale_factor = 0.002293577883 ;
rh:add_offset = 75. ;
rh:long_name = "relative humidity" ;
rh:units = "%" ;
// global attributes:
:Conventions = "CF-1.0" ;
:history = "created by trans_latest_msm_p.rb 2020-07-17" ;
data:
lon = 120, 120.125, 120.25, 120.375, 120.5, 120.625, 120.75, 120.875, 121,
121.125, 121.25, 121.375, 121.5, 121.625, 121.75, 121.875, 122, 122.125,
122.25, 122.375, 122.5, 122.625, 122.75, 122.875, 123, 123.125, 123.25,
.......
以上のように、NetCDFファイルの情報が列挙されます。
demensionでは、次元の名前、長さが入ります。例では、lon(経度),241,lat(緯度),253,p(気圧)16,time(時間),12の4次元データです。
variablesでは、変数の型などの説明が入ります。例えば、変数temp(気温)では、short(符号付16ビット整数)であり、4次元(time,p,lat,lon)、変数名がtemperature、単位はK(ケルビン)、標準の名前がair_temperatureです。
scale_factorは、そのデータが読み込まれるときに掛け合わせる係数です。
add_offsetは、そのデータが読み込まれるときに加える係数です。
したがって、データ×scale_factor+add_offsetで真のデータが得られる訳です。
scale_factorやadd_offsetによって、よりデータの圧縮を行うことができます。
dataでは、各変数のデータが表示されます。
例では、1034行にも及びます。
したがって、非常にみづらいので、オプションをつけて実行します。
以下に紹介するものは、オプションの中でもよく使うものを示します。
ncdump -h
-hオプションをつけると、データの値を表示せずヘッダーの部分のみ表示します。変数の情報のみ確認したい場合に用います。
$ ncdump -h MSM20200717P.nc
実行結果
netcdf MSM2020071700P.nc {
dimensions:
lon = 241 ;
lat = 253 ;
p = 16 ;
time = 12 ;
variables:
float lon(lon) ;
lon:long_name = "longitude" ;
lon:units = "degrees_east" ;
lon:standard_name = "longitude" ;
float lat(lat) ;
lat:long_name = "latitude" ;
lat:units = "degrees_north" ;
lat:standard_name = "latitude" ;
float p(p) ;
p:long_name = "pressure level" ;
p:units = "hPa" ;
p:standard_name = "air_pressure" ;
float time(time) ;
time:long_name = "time" ;
time:units = "hours since 2020-07-17 00:00:00+00:00" ;
time:standard_name = "time" ;
float z(time, p, lat, lon) ;
z:long_name = "geopotential height" ;
z:units = "m" ;
z:standard_name = "geopotential_height" ;
float w(time, p, lat, lon) ;
w:long_name = "vertical velocity in p" ;
w:units = "Pa/s" ;
w:standard_name = "lagrangian_tendency_of_air_pressure" ;
short u(time, p, lat, lon) ;
u:scale_factor = 0.006116208155 ;
u:add_offset = 0. ;
u:long_name = "eastward component of wind" ;
u:units = "m/s" ;
u:standard_name = "eastward_wind" ;
short v(time, p, lat, lon) ;
v:scale_factor = 0.006116208155 ;
v:add_offset = 0. ;
v:long_name = "northward component of wind" ;
v:units = "m/s" ;
v:standard_name = "northward_wind" ;
short temp(time, p, lat, lon) ;
temp:scale_factor = 0.002613491379 ;
temp:add_offset = 255.4004974 ;
temp:long_name = "temperature" ;
temp:units = "K" ;
temp:standard_name = "air_temperature" ;
short rh(time, p, lat, lon) ;
rh:scale_factor = 0.002293577883 ;
rh:add_offset = 75. ;
rh:long_name = "relative humidity" ;
rh:units = "%" ;
// global attributes:
:Conventions = "CF-1.0" ;
:history = "created by trans_latest_msm_p.rb 2020-07-17" ;
}
ncdump -v
-vオプションをつけると、特定の変数の値のみ(ヘッダーは有)を表示します。
-vのあとに表示させたい変数名を追加してください。
$ ncdump -v temp MSM20200717P.nc
実行結果
netcdf MSM2020071700P.nc {
dimensions:
lon = 241 ;
lat = 253 ;
p = 16 ;
time = 12 ;
variables:
float lon(lon) ;
lon:long_name = "longitude" ;
lon:units = "degrees_east" ;
lon:standard_name = "longitude" ;
float lat(lat) ;
lat:long_name = "latitude" ;
lat:units = "degrees_north" ;
lat:standard_name = "latitude" ;
float p(p) ;
p:long_name = "pressure level" ;
p:units = "hPa" ;
p:standard_name = "air_pressure" ;
float time(time) ;
time:long_name = "time" ;
time:units = "hours since 2020-07-17 00:00:00+00:00" ;
time:standard_name = "time" ;
float z(time, p, lat, lon) ;
z:long_name = "geopotential height" ;
z:units = "m" ;
z:standard_name = "geopotential_height" ;
float w(time, p, lat, lon) ;
w:long_name = "vertical velocity in p" ;
w:units = "Pa/s" ;
w:standard_name = "lagrangian_tendency_of_air_pressure" ;
short u(time, p, lat, lon) ;
u:scale_factor = 0.006116208155 ;
u:add_offset = 0. ;
u:long_name = "eastward component of wind" ;
u:units = "m/s" ;
u:standard_name = "eastward_wind" ;
short v(time, p, lat, lon) ;
v:scale_factor = 0.006116208155 ;
v:add_offset = 0. ;
v:long_name = "northward component of wind" ;
v:units = "m/s" ;
v:standard_name = "northward_wind" ;
short temp(time, p, lat, lon) ;
temp:scale_factor = 0.002613491379 ;
temp:add_offset = 255.4004974 ;
temp:long_name = "temperature" ;
temp:units = "K" ;
temp:standard_name = "air_temperature" ;
short rh(time, p, lat, lon) ;
rh:scale_factor = 0.002293577883 ;
rh:add_offset = 75. ;
rh:long_name = "relative humidity" ;
rh:units = "%" ;
// global attributes:
:Conventions = "CF-1.0" ;
:history = "created by trans_latest_msm_p.rb 2020-07-17" ;
data:
temp =
16585, 16621, 16585, 16606, 16615, 16765, 16944, 17022, 17025, 16920,
16810, 16828, 16953, 16854, 17052, 17115, 17007, 16798, 16783, 16974,
16986, 17049, 17201, 17186, 17267, 17303, 17276, 17159, 17115, 17109,
17058, 16854, 16795, 17010, 16902, 16962, 16938, 17067, 17147, 17174,
17112, 17070, 17073, 17052, 17031, 17126, 17252, 17321, 17372, 17372,
17210, 16992, 16983, 17177, 17171, 17124, 17052, 17297, 16953, 16941,
17079, 17088, 17156, 17333, 17399, 17276, 17192, 17372, 17527, 17578,
17614, 17581, 17886, 17871, 17413, 17231, 17192, 17219, 17467, 17422,
17662, 17626, 17617, 17554, 17357, 17162, 17046, 17109, 17171, 17100,
17144, 17171, 17273, 17321, 17285, 17156, 17019, 16920, 16866, 16834,
16732, 16660, 16690, 16547, 16409, 16355, 16391, 16505, 16591, 16642,
16678, 16708, 16729, 16687, 16337, 16245, 16382, 16618, 17010, 17028,
17094, 16890, 16633, 16562, 16597, 16463, 16597, 16603, 16502, 16385,
16331, 16173, 16278, 16397, 16119, 15946, 15916, 16239, 16481, 16690,
16618, 16391, 16582, 16385, 16370, 16505, 16487, 16143, 16137, 16260,
16496, 16621, 16185, 13635, 12460, 12451, 12448, 12472, 12520, 12583,
12657, 12747, 12831, 12888, 12956, 13097, 13181, 13196, 13211, 13249,
13291, 13336, 13390, 13429, 13450, 13372, 13961, 13686, 13761, 14051,
14412, 14179, 13596, 13372, 13079, 12792, 12463, 12167, 11970, 11928,
11913, 11850, 11806, 11782, 11746, 11674, 11602, 11516, 11474, 11423,
11420, 11408, 11357, 11321, 11327, 11345, 11342, 11330, 11297, 11294,
11309, 11279, 11160, 11046, 11094, 11175, 11187, 11136, 11082, 11067,
11022, 10915, 10900, 10804, 10774, 10750, 10711, 10688, 10544, 10475,
10457, 10430, 10445, 10460, 10454, 10401, 10329, 10335, 10293, 10221,
10194,
16526, 16475, 16448, 16433, 16550, 16696, 16687, 16965, 16816, 16672,
16627, 16660, 16845, 16941, 17070, 17097, 17121, 17085, 16917, 16845,
16875, 17144, 17261, 17309, 17213, 17228, 17366, 17162, 17129, 17106,
17004, 16881, 16884, 16917, 16851, 17112, 16977, 17070, 17135, 17159,
17118, 17073, 17067, 17040, 17043, 17186, 17282, 17267, 17348, 17288,
17109, 17031, 17222, 17174, 17198, 17138, 17004, 16795, 16875, 17001,
17073, 17138, 17204, 17156, 17010, 17019, 17106, 17375, 17404, 17378,
17443, 17372, 17685, 17781, 17141, 17118, 17132, 17246, 17419, 17419,
17671, 17697, 17572, 17587, 17366, 17180, 17150, 17189, 17294, 17106,
17118, 17309, 17360, 17336, 17297, 17192, 17022, 16974, 16944, 16893,
16786, 16735, 16624, 16439, 16361, 16304, 16406, 16538, 16612, 16690,
16765, 16798, 16786, 16556, 16131, 16325, 16553, 16905, 17153, 17252,
17094, 16663, 16511, 16529, 16472, 16343, 16278, 16260, 16511, 16487,
16236, 16266, 16164, 16361, 16346, 16179, 16044, 16113, 16448, 16735,
16559, 16337, 16544, 16367, 16535, 16819, 16388, 16056, 16191, 16266,
16337, 16711, 14879, 12535, 12436, 12445, 12457, 12490, 12541, 12598,
12672, 12762, 12846, 12903, 12995, 13151, 13214, 13222, 13228, 13264,
13324, 13378, 13426, 13471, 13492, 13420, 13575, 13617, 13701, 14003,
............
ncdump -c
-cをオプションをつけるとヘッダーの部分とデータ(lon,lat,p,time)のみを表示します。
$ ncdump -c MSM20200717P.nc
実行結果
netcdf MSM2020071700P.nc {
dimensions:
lon = 241 ;
lat = 253 ;
p = 16 ;
time = 12 ;
variables:
float lon(lon) ;
lon:long_name = "longitude" ;
lon:units = "degrees_east" ;
lon:standard_name = "longitude" ;
float lat(lat) ;
lat:long_name = "latitude" ;
lat:units = "degrees_north" ;
lat:standard_name = "latitude" ;
float p(p) ;
p:long_name = "pressure level" ;
p:units = "hPa" ;
p:standard_name = "air_pressure" ;
float time(time) ;
time:long_name = "time" ;
time:units = "hours since 2020-07-17 00:00:00+00:00" ;
time:standard_name = "time" ;
float z(time, p, lat, lon) ;
z:long_name = "geopotential height" ;
z:units = "m" ;
z:standard_name = "geopotential_height" ;
float w(time, p, lat, lon) ;
w:long_name = "vertical velocity in p" ;
w:units = "Pa/s" ;
w:standard_name = "lagrangian_tendency_of_air_pressure" ;
short u(time, p, lat, lon) ;
u:scale_factor = 0.006116208155 ;
u:add_offset = 0. ;
u:long_name = "eastward component of wind" ;
u:units = "m/s" ;
u:standard_name = "eastward_wind" ;
short v(time, p, lat, lon) ;
v:scale_factor = 0.006116208155 ;
v:add_offset = 0. ;
v:long_name = "northward component of wind" ;
v:units = "m/s" ;
v:standard_name = "northward_wind" ;
short temp(time, p, lat, lon) ;
temp:scale_factor = 0.002613491379 ;
temp:add_offset = 255.4004974 ;
temp:long_name = "temperature" ;
temp:units = "K" ;
temp:standard_name = "air_temperature" ;
short rh(time, p, lat, lon) ;
rh:scale_factor = 0.002293577883 ;
rh:add_offset = 75. ;
rh:long_name = "relative humidity" ;
rh:units = "%" ;
// global attributes:
:Conventions = "CF-1.0" ;
:history = "created by trans_latest_msm_p.rb 2020-07-17" ;
data:
lon = 120, 120.125, 120.25, 120.375, 120.5, 120.625, 120.75, 120.875, 121,
121.125, 121.25, 121.375, 121.5, 121.625, 121.75, 121.875, 122, 122.125,
122.25, 122.375, 122.5, 122.625, 122.75, 122.875, 123, 123.125, 123.25,
123.375, 123.5, 123.625, 123.75, 123.875, 124, 124.125, 124.25, 124.375,
124.5, 124.625, 124.75, 124.875, 125, 125.125, 125.25, 125.375, 125.5,
125.625, 125.75, 125.875, 126, 126.125, 126.25, 126.375, 126.5, 126.625,
126.75, 126.875, 127, 127.125, 127.25, 127.375, 127.5, 127.625, 127.75,
127.875, 128, 128.125, 128.25, 128.375, 128.5, 128.625, 128.75, 128.875,
129, 129.125, 129.25, 129.375, 129.5, 129.625, 129.75, 129.875, 130,
130.125, 130.25, 130.375, 130.5, 130.625, 130.75, 130.875, 131, 131.125,
131.25, 131.375, 131.5, 131.625, 131.75, 131.875, 132, 132.125, 132.25,
132.375, 132.5, 132.625, 132.75, 132.875, 133, 133.125, 133.25, 133.375,
133.5, 133.625, 133.75, 133.875, 134, 134.125, 134.25, 134.375, 134.5,
134.625, 134.75, 134.875, 135, 135.125, 135.25, 135.375, 135.5, 135.625,
135.75, 135.875, 136, 136.125, 136.25, 136.375, 136.5, 136.625, 136.75,
136.875, 137, 137.125, 137.25, 137.375, 137.5, 137.625, 137.75, 137.875,
138, 138.125, 138.25, 138.375, 138.5, 138.625, 138.75, 138.875, 139,
139.125, 139.25, 139.375, 139.5, 139.625, 139.75, 139.875, 140, 140.125,
140.25, 140.375, 140.5, 140.625, 140.75, 140.875, 141, 141.125, 141.25,
141.375, 141.5, 141.625, 141.75, 141.875, 142, 142.125, 142.25, 142.375,
142.5, 142.625, 142.75, 142.875, 143, 143.125, 143.25, 143.375, 143.5,
143.625, 143.75, 143.875, 144, 144.125, 144.25, 144.375, 144.5, 144.625,
144.75, 144.875, 145, 145.125, 145.25, 145.375, 145.5, 145.625, 145.75,
145.875, 146, 146.125, 146.25, 146.375, 146.5, 146.625, 146.75, 146.875,
147, 147.125, 147.25, 147.375, 147.5, 147.625, 147.75, 147.875, 148,
148.125, 148.25, 148.375, 148.5, 148.625, 148.75, 148.875, 149, 149.125,
149.25, 149.375, 149.5, 149.625, 149.75, 149.875, 150 ;
lat = 47.6, 47.5, 47.4, 47.3, 47.2, 47.1, 47, 46.9, 46.8, 46.7, 46.6, 46.5,
46.4, 46.3, 46.2, 46.1, 46, 45.9, 45.8, 45.7, 45.6, 45.5, 45.4, 45.3,
45.2, 45.1, 45, 44.9, 44.8, 44.7, 44.6, 44.5, 44.4, 44.3, 44.2, 44.1, 44,
43.9, 43.8, 43.7, 43.6, 43.5, 43.4, 43.3, 43.2, 43.1, 43, 42.9, 42.8,
42.7, 42.6, 42.5, 42.4, 42.3, 42.2, 42.1, 42, 41.9, 41.8, 41.7, 41.6,
41.5, 41.4, 41.3, 41.2, 41.1, 41, 40.9, 40.8, 40.7, 40.6, 40.5, 40.4,
40.3, 40.2, 40.1, 40, 39.9, 39.8, 39.7, 39.6, 39.5, 39.4, 39.3, 39.2,
39.1, 39, 38.9, 38.8, 38.7, 38.6, 38.5, 38.4, 38.3, 38.2, 38.1, 38, 37.9,
37.8, 37.7, 37.6, 37.5, 37.4, 37.3, 37.2, 37.1, 37, 36.9, 36.8, 36.7,
36.6, 36.5, 36.4, 36.3, 36.2, 36.1, 36, 35.9, 35.8, 35.7, 35.6, 35.5,
35.4, 35.3, 35.2, 35.1, 35, 34.9, 34.8, 34.7, 34.6, 34.5, 34.4, 34.3,
34.2, 34.1, 34, 33.9, 33.8, 33.7, 33.6, 33.5, 33.4, 33.3, 33.2, 33.1, 33,
32.9, 32.8, 32.7, 32.6, 32.5, 32.4, 32.3, 32.2, 32.1, 32, 31.9, 31.8,
31.7, 31.6, 31.5, 31.4, 31.3, 31.2, 31.1, 31, 30.9, 30.8, 30.7, 30.6,
30.5, 30.4, 30.3, 30.2, 30.1, 30, 29.9, 29.8, 29.7, 29.6, 29.5, 29.4,
29.3, 29.2, 29.1, 29, 28.9, 28.8, 28.7, 28.6, 28.5, 28.4, 28.3, 28.2,
28.1, 28, 27.9, 27.8, 27.7, 27.6, 27.5, 27.4, 27.3, 27.2, 27.1, 27, 26.9,
26.8, 26.7, 26.6, 26.5, 26.4, 26.3, 26.2, 26.1, 26, 25.9, 25.8, 25.7,
25.6, 25.5, 25.4, 25.3, 25.2, 25.1, 25, 24.9, 24.8, 24.7, 24.6, 24.5,
24.4, 24.3, 24.2, 24.1, 24, 23.9, 23.8, 23.7, 23.6, 23.5, 23.4, 23.3,
23.2, 23.1, 23, 22.9, 22.8, 22.7, 22.6, 22.5, 22.4 ;
p = 1000, 975, 950, 925, 900, 850, 800, 700, 600, 500, 400, 300, 250, 200,
150, 100 ;
time = 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33 ;
}
その他のオプション
ncdumpのみ実行することで、ncdumpのオプションが表示されます。
$ ncdump
実行結果
ncdump [-c|-h] [-v ...] [[-b|-f] [c|f]] [-l len] [-n name] [-p n[,n]] [-k] [-x] [-s] [-t|-i] [-g ...] [-w] 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
file Name of netCDF file (or URL if DAP access enabled)
netcdf library version 4.3.3.1 $
また、NetCDFファイルの読み込み、書き込みの記事も今後出したいと思います。