1 回答
TA贡献1845条经验 获得超8个赞
如果你想知道如何使用add_offset和scale_factor参数来打包或解包.nc文件中的数据,你可以阅读这里。当您使用python读取netCDF4文件时(例如下载NCEP reanalysis I数据),您可以参考以下代码:
>>> import netCDF4 as nc
>>> file_obj = nc.Dataset('./air.mon.mean.nc')
>>> file_obj.variables.keys()
odict_keys(['lat', 'lon', 'time', 'air'])
我们想读取air变量并检查它
file_obj.variables['air']
它将表明:
<class 'netCDF4._netCDF4.Variable'>
float32 air(time, lat, lon)
long_name: Monthly Mean Air Temperature at sigma level 0.995
valid_range: [-2000. 2000.]
units: degC
add_offset: 0.0
scale_factor: 1.0
missing_value: -9.96921e+36
precision: 1
least_significant_digit: 0
var_desc: Air Temperature
level_desc: Surface
statistic: Mean
parent_stat: Individual Obs
dataset: NCEP Reanalysis Derived Products
actual_range: [-73.78001 42.14595]
unlimited dimensions: time
current shape = (848, 73, 144)
filling on, default _FillValue of 9.969209968386869e+36 used
我们可以阅读add_offset并scale_factor通过
>>> add_offset = file_obj.variables['air'].add_offset
>>> scale_factor = file_obj.variables['air'].scale_factor
然后读取packed_value
>>> packed_value = file_obj.variables['air'][:]
最后,我们可以通过包表达式计算 upacked_value(real value)
>>> unpacked_value = packed_value * scale_factor + add_offset
结果:
>>> unpacked_value
array([[[-34.926773 , -34.926773 , -34.926773 , ..., -34.926773 ,
-34.926773 , -34.926773 ],
[-35.13935 , -35.129673 , -35.12742 , ..., -35.188705 ,
-35.170002 , -35.14935 ],
[-34.352573 , -34.04226 , -33.768707 , ..., -35.333866 ,
-35.002903 , -34.671288 ],
...,
[-16.525156 , -16.404509 , -16.284832 , ..., -16.795155 ,
-16.737736 , -16.643543 ],
[-16.190313 , -16.202248 , -16.21677 , ..., -16.132574 ,
-16.161928 , -16.178377 ],
[-17.697733 , -17.697733 , -17.697733 , ..., -17.697733 ,
-17.697733 , -17.697733 ]],
[[-33.311375 , -33.311375 , -33.311375 , ..., -33.311375 ,
-33.311375 , -33.311375 ],
[-34.65034 , -34.476204 , -34.29689 , ..., -35.18448 ,
-35.009308 , -34.835514 ],
[-34.1031 , -33.619995 , -33.161373 , ..., -35.606552 ,
-35.103443 , -34.602757 ],
...,
[-34.338963 , -34.21862 , -34.08241 , ..., -34.359997 ,
-34.42724 , -34.418617 ],
[-33.795513 , -33.896553 , -33.977238 , ..., -33.41517 ,
-33.56758 , -33.690342 ],
[-32.942413 , -32.942413 , -32.942413 , ..., -32.942413 ,
-32.942413 , -32.942413 ]],
[[-29.716127 , -29.716127 , -29.716127 , ..., -29.716127 ,
-29.716127 , -29.716127 ],
[-29.4471 , -29.499353 , -29.551613 , ..., -29.365162 ,
-29.385166 , -29.41258 ],
[-28.544516 , -28.366776 , -28.227749 , ..., -29.282906 ,
-29.01323 , -28.763546 ],
...,
[-51.964516 , -52.206455 , -52.362263 , ..., -50.628704 ,
-51.18032 , -51.631298 ],
[-52.846123 , -53.07613 , -53.290974 , ..., -52.069355 ,
-52.344517 , -52.60097 ],
[-54.835476 , -54.835476 , -54.835476 , ..., -54.835476 ,
-54.835476 , -54.835476 ]],
...,
[[ 0.7949912 , 0.7949912 , 0.7949912 , ..., 0.7949912 ,
0.7949912 , 0.7949912 ],
[ 0.4383262 , 0.44082558, 0.44582742, ..., 0.44332498,
0.44166178, 0.43915424],
[ 0.85249335, 0.9124919 , 0.9749919 , ..., 0.6591595 ,
0.7224956 , 0.7849895 ],
...,
[-47.735012 , -47.71085 , -47.643345 , ..., -47.286682 ,
-47.53668 , -47.68001 ],
[-48.136677 , -48.31417 , -48.46751 , ..., -47.466675 ,
-47.71334 , -47.937504 ],
[-49.84001 , -49.84001 , -49.84001 , ..., -49.84001 ,
-49.84001 , -49.84001 ]],
[[ 2.017739 , 2.017739 , 2.017739 , ..., 2.017739 ,
2.017739 , 2.017739 ],
[ 1.7024138 , 1.6927387 , 1.6862857 , ..., 1.7282208 ,
1.719346 , 1.7040253 ],
[ 2.255639 , 2.275 , 2.2983825 , ..., 2.1927364 ,
2.2120905 , 2.2282214 ],
...,
[-55.12824 , -55.170174 , -55.13468 , ..., -54.45 ,
-54.787907 , -55.01049 ],
[-54.267754 , -54.429844 , -54.56291 , ..., -53.68952 ,
-53.907265 , -54.09678 ],
[-56.350006 , -56.350006 , -56.350006 , ..., -56.350006 ,
-56.350006 , -56.350006 ]],
[[ 1.2532172 , 1.2532172 , 1.2532172 , ..., 1.2532172 ,
1.2532172 , 1.2532172 ],
[ 0.5870904 , 0.59515494, 0.60886717, ..., 0.5814466 ,
0.5798341 , 0.5846687 ],
[ 0.99273497, 1.063704 , 1.137088 , ..., 0.8080569 ,
0.860479 , 0.9241818 ],
...,
[-52.7613 , -52.795174 , -52.786297 , ..., -52.214523 ,
-52.4871 , -52.66856 ],
[-52.11856 , -52.348392 , -52.553234 , ..., -51.348396 ,
-51.628242 , -51.880657 ],
[-53.070175 , -53.070175 , -53.070175 , ..., -53.070175 ,
-53.070175 , -53.070175 ]]], dtype=float32)
添加回答
举报