使用 Python 在 netCDF4 中使用 scale_factor 和 add_offset

来自此处的 netCDF4 文档:http ://unidata.github.io/netcdf4-python/#netCDF4.Variable

谁能给我一个在 Python 中使用 scale_factor 和 add_offset 参数写入数据的简单示例?


明月笑刀无情
浏览 655回答 1
1回答

精慕HU

如果你想知道如何使用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)&nbsp; &nbsp; long_name: Monthly Mean Air Temperature at sigma level 0.995&nbsp; &nbsp; valid_range: [-2000.&nbsp; 2000.]&nbsp; &nbsp; units: degC&nbsp; &nbsp; add_offset: 0.0&nbsp; &nbsp; scale_factor: 1.0&nbsp; &nbsp; missing_value: -9.96921e+36&nbsp; &nbsp; precision: 1&nbsp; &nbsp; least_significant_digit: 0&nbsp; &nbsp; var_desc: Air Temperature&nbsp; &nbsp; level_desc: Surface&nbsp; &nbsp; statistic: Mean&nbsp; &nbsp; parent_stat: Individual Obs&nbsp; &nbsp; dataset: NCEP Reanalysis Derived Products&nbsp; &nbsp; actual_range: [-73.78001&nbsp; 42.14595]unlimited dimensions: timecurrent 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_valuearray([[[-34.926773&nbsp; , -34.926773&nbsp; , -34.926773&nbsp; , ..., -34.926773&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-34.926773&nbsp; , -34.926773&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [-35.13935&nbsp; &nbsp;, -35.129673&nbsp; , -35.12742&nbsp; &nbsp;, ..., -35.188705&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-35.170002&nbsp; , -35.14935&nbsp; &nbsp;],&nbsp; &nbsp; &nbsp; &nbsp; [-34.352573&nbsp; , -34.04226&nbsp; &nbsp;, -33.768707&nbsp; , ..., -35.333866&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-35.002903&nbsp; , -34.671288&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; ...,&nbsp; &nbsp; &nbsp; &nbsp; [-16.525156&nbsp; , -16.404509&nbsp; , -16.284832&nbsp; , ..., -16.795155&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-16.737736&nbsp; , -16.643543&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [-16.190313&nbsp; , -16.202248&nbsp; , -16.21677&nbsp; &nbsp;, ..., -16.132574&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-16.161928&nbsp; , -16.178377&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [-17.697733&nbsp; , -17.697733&nbsp; , -17.697733&nbsp; , ..., -17.697733&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-17.697733&nbsp; , -17.697733&nbsp; ]],&nbsp; &nbsp; &nbsp; &nbsp;[[-33.311375&nbsp; , -33.311375&nbsp; , -33.311375&nbsp; , ..., -33.311375&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-33.311375&nbsp; , -33.311375&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [-34.65034&nbsp; &nbsp;, -34.476204&nbsp; , -34.29689&nbsp; &nbsp;, ..., -35.18448&nbsp; &nbsp;,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-35.009308&nbsp; , -34.835514&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [-34.1031&nbsp; &nbsp; , -33.619995&nbsp; , -33.161373&nbsp; , ..., -35.606552&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-35.103443&nbsp; , -34.602757&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; ...,&nbsp; &nbsp; &nbsp; &nbsp; [-34.338963&nbsp; , -34.21862&nbsp; &nbsp;, -34.08241&nbsp; &nbsp;, ..., -34.359997&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-34.42724&nbsp; &nbsp;, -34.418617&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [-33.795513&nbsp; , -33.896553&nbsp; , -33.977238&nbsp; , ..., -33.41517&nbsp; &nbsp;,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-33.56758&nbsp; &nbsp;, -33.690342&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [-32.942413&nbsp; , -32.942413&nbsp; , -32.942413&nbsp; , ..., -32.942413&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-32.942413&nbsp; , -32.942413&nbsp; ]],&nbsp; &nbsp; &nbsp; &nbsp;[[-29.716127&nbsp; , -29.716127&nbsp; , -29.716127&nbsp; , ..., -29.716127&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-29.716127&nbsp; , -29.716127&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [-29.4471&nbsp; &nbsp; , -29.499353&nbsp; , -29.551613&nbsp; , ..., -29.365162&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-29.385166&nbsp; , -29.41258&nbsp; &nbsp;],&nbsp; &nbsp; &nbsp; &nbsp; [-28.544516&nbsp; , -28.366776&nbsp; , -28.227749&nbsp; , ..., -29.282906&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-29.01323&nbsp; &nbsp;, -28.763546&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; ...,&nbsp; &nbsp; &nbsp; &nbsp; [-51.964516&nbsp; , -52.206455&nbsp; , -52.362263&nbsp; , ..., -50.628704&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-51.18032&nbsp; &nbsp;, -51.631298&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [-52.846123&nbsp; , -53.07613&nbsp; &nbsp;, -53.290974&nbsp; , ..., -52.069355&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-52.344517&nbsp; , -52.60097&nbsp; &nbsp;],&nbsp; &nbsp; &nbsp; &nbsp; [-54.835476&nbsp; , -54.835476&nbsp; , -54.835476&nbsp; , ..., -54.835476&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-54.835476&nbsp; , -54.835476&nbsp; ]],&nbsp; &nbsp; &nbsp; &nbsp;...,&nbsp; &nbsp; &nbsp; &nbsp;[[&nbsp; 0.7949912 ,&nbsp; &nbsp;0.7949912 ,&nbsp; &nbsp;0.7949912 , ...,&nbsp; &nbsp;0.7949912 ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.7949912 ,&nbsp; &nbsp;0.7949912 ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; 0.4383262 ,&nbsp; &nbsp;0.44082558,&nbsp; &nbsp;0.44582742, ...,&nbsp; &nbsp;0.44332498,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.44166178,&nbsp; &nbsp;0.43915424],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; 0.85249335,&nbsp; &nbsp;0.9124919 ,&nbsp; &nbsp;0.9749919 , ...,&nbsp; &nbsp;0.6591595 ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.7224956 ,&nbsp; &nbsp;0.7849895 ],&nbsp; &nbsp; &nbsp; &nbsp; ...,&nbsp; &nbsp; &nbsp; &nbsp; [-47.735012&nbsp; , -47.71085&nbsp; &nbsp;, -47.643345&nbsp; , ..., -47.286682&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-47.53668&nbsp; &nbsp;, -47.68001&nbsp; &nbsp;],&nbsp; &nbsp; &nbsp; &nbsp; [-48.136677&nbsp; , -48.31417&nbsp; &nbsp;, -48.46751&nbsp; &nbsp;, ..., -47.466675&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-47.71334&nbsp; &nbsp;, -47.937504&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [-49.84001&nbsp; &nbsp;, -49.84001&nbsp; &nbsp;, -49.84001&nbsp; &nbsp;, ..., -49.84001&nbsp; &nbsp;,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-49.84001&nbsp; &nbsp;, -49.84001&nbsp; &nbsp;]],&nbsp; &nbsp; &nbsp; &nbsp;[[&nbsp; 2.017739&nbsp; ,&nbsp; &nbsp;2.017739&nbsp; ,&nbsp; &nbsp;2.017739&nbsp; , ...,&nbsp; &nbsp;2.017739&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.017739&nbsp; ,&nbsp; &nbsp;2.017739&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; 1.7024138 ,&nbsp; &nbsp;1.6927387 ,&nbsp; &nbsp;1.6862857 , ...,&nbsp; &nbsp;1.7282208 ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.719346&nbsp; ,&nbsp; &nbsp;1.7040253 ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; 2.255639&nbsp; ,&nbsp; &nbsp;2.275&nbsp; &nbsp; &nbsp;,&nbsp; &nbsp;2.2983825 , ...,&nbsp; &nbsp;2.1927364 ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.2120905 ,&nbsp; &nbsp;2.2282214 ],&nbsp; &nbsp; &nbsp; &nbsp; ...,&nbsp; &nbsp; &nbsp; &nbsp; [-55.12824&nbsp; &nbsp;, -55.170174&nbsp; , -55.13468&nbsp; &nbsp;, ..., -54.45&nbsp; &nbsp; &nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-54.787907&nbsp; , -55.01049&nbsp; &nbsp;],&nbsp; &nbsp; &nbsp; &nbsp; [-54.267754&nbsp; , -54.429844&nbsp; , -54.56291&nbsp; &nbsp;, ..., -53.68952&nbsp; &nbsp;,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-53.907265&nbsp; , -54.09678&nbsp; &nbsp;],&nbsp; &nbsp; &nbsp; &nbsp; [-56.350006&nbsp; , -56.350006&nbsp; , -56.350006&nbsp; , ..., -56.350006&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-56.350006&nbsp; , -56.350006&nbsp; ]],&nbsp; &nbsp; &nbsp; &nbsp;[[&nbsp; 1.2532172 ,&nbsp; &nbsp;1.2532172 ,&nbsp; &nbsp;1.2532172 , ...,&nbsp; &nbsp;1.2532172 ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.2532172 ,&nbsp; &nbsp;1.2532172 ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; 0.5870904 ,&nbsp; &nbsp;0.59515494,&nbsp; &nbsp;0.60886717, ...,&nbsp; &nbsp;0.5814466 ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.5798341 ,&nbsp; &nbsp;0.5846687 ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; 0.99273497,&nbsp; &nbsp;1.063704&nbsp; ,&nbsp; &nbsp;1.137088&nbsp; , ...,&nbsp; &nbsp;0.8080569 ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.860479&nbsp; ,&nbsp; &nbsp;0.9241818 ],&nbsp; &nbsp; &nbsp; &nbsp; ...,&nbsp; &nbsp; &nbsp; &nbsp; [-52.7613&nbsp; &nbsp; , -52.795174&nbsp; , -52.786297&nbsp; , ..., -52.214523&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-52.4871&nbsp; &nbsp; , -52.66856&nbsp; &nbsp;],&nbsp; &nbsp; &nbsp; &nbsp; [-52.11856&nbsp; &nbsp;, -52.348392&nbsp; , -52.553234&nbsp; , ..., -51.348396&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-51.628242&nbsp; , -51.880657&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [-53.070175&nbsp; , -53.070175&nbsp; , -53.070175&nbsp; , ..., -53.070175&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-53.070175&nbsp; , -53.070175&nbsp; ]]], dtype=float32)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python