python netCDF4

发布时间:2024年01月12日

NetCDF简介

NetCDF 即 network Common Data Form(网络通用数据格式),是一种面向数组型并适于网络共享的数据的描述和编码标准。文件的后缀是?.nc。nc 在气象领域应用很广,因为它可以存储不同波段的长时间观测结果。

NetCDF 文件中的数据以数组形式存储。例如,某个位置处随时间变化的温度以一维数组的形式存储。某个区域内在指定时间的温度以二维数组的形式存储。来源:【知乎Assimov】。

netCDF4 是一个专门处理 nc数据的 python库。

netCDF4 安装

在安装?netCDF4 之前,需要先安装 numpy 和?cftime,不然大概率会报错。numpy 版本必须大于1.9cftime的版本最好与netCDF4一致,比如我安装了cftime 1.5.2 和 netCDF4 1.5.8。

netCDF4 使用

# nc_path : nc文件路径。示例为2003-2021年OBS4MIPS全球甲烷数据。
nc_path = "200301_202112-C3S-L3_GHG-GHG_PRODUCTS-MERGED-MERGED-OBS4MIPS-MERGED-v4.4.nc"
ds = Dataset(nc_path)
print(ds)

"""
输出信息:
<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF3_CLASSIC data model, file format NETCDF3):
    activity_id: obs4MIPs
    comment: Since long time, climate modellers use ensemble approaches to calculate the
ensemble median and to estimate uncertainties of climate projections where
no ground-truth is known. Following this idea, the ensemble median
algorithm EMMA composes level 2 data of independently developed retrieval
algorithms. EMMA determines in 10x10 degree grid boxes monthly averages and
selects level 2 data of the median algorithm.
    contact: Maximilian Reuter (maximilian.reuter@iup.physik.uni-bremen.de)
    Conventions: CF-1.7 ODS-2.1
    creation_date: 2022-07-13T12:28:13Z
    data_specs_version: 2.1.0
    frequency: mon
    further_info_url: https://climate.copernicus.eu
    grid: L2 data gridded by arithmetic averaging
    grid_label: gn
    institution: Institute of Environmental Physics, University of Bremen
    institute_id: IUP
    license: GHG-CCI Licence: 
As condition of using this product, you agree
... to inform us prior to any publication where the data products are planned to be used. Please do this by sending us the
manuscript for review before submission for publication to ensure that our data are accurately represented.
... to offer us co-authorship for any planned peer-reviewed publication based on our data products. (For non peer-reviewed
publications it is sufficient if you add an appropriate acknowledgement.) In these instances, please contact the project management
(Michael Buchwitz (michael.buchwitz@iup.physik.uni-bremen.de) or Maximilian Reuter (maximilian.reuter@iup.physik.uni-bremen.de))
who will then forward the information to the respective retrieval teams.
    nominal_resolution: 5.00x5.00 degree
    product: observations
    realm: atmos
    references: M. Reuter, H. B?sch, H. Bovensmann, A. Bril, M. Buchwitz, A. Butz,
J. P. Burrows, C. W. ODell, S. Guerlet, O. Hasekamp, J. Heymann, N. Kikuchi,
S. Oshchepkov, R. Parker, S. Pfeifer, O. Schneising, T. Yokota, and
Y. Yoshida: A joint effort to deliver satellite retrieved atmospheric CH4
concentrations for surface flux inversions: the ensemble median algorithm
EMMA. Atmospheric Chemistry and Physics, 13, 1771-1780, 2013
    region: global
    source: C3S XCH4 v4.4 (2022)
    source_id: C3S-XCH4-v4-4
    source_label: C3S-XCH4
    source_type: satellite_retrieval
    source_version_number: v4.4
    title: C3S XCH4 v4.4
    tracking_id: 2dbf9794-a7c5-45ea-be6c-ea140fe809ec
    variable_id: xch4
    variant_info: Best Estimate
    variant_label: BE
    dimensions(sizes): time(228), bnds(2), lat(36), lon(72), pressure(10)
    variables(dimensions): float64 time(time), float64 time_bnds(time, bnds), float64 lat(lat), float64 lat_bnds(lat, bnds), float64 lon(lon), float64 lon_bnds(lon, bnds), float64 pre(pressure), float64 pre_bnds(pressure, bnds), float64 land_fraction(lat, lon), float32 xch4(time, lat, lon), int32 xch4_nobs(time, lat, lon), float32 xch4_stderr(time, lat, lon), float32 xch4_stddev(time, lat, lon), float32 column_averaging_kernel(time, pressure, lat, lon), float32 vmr_profile_ch4_apriori(time, pressure, lat, lon)
    groups: 

Process finished with exit code 0
"""

variable_names = ds.variables.keys()  # 变量名,类似于该数据的属性
print(len(variable_names), variable_names)

"""
输出信息:
15 dict_keys(['time', 'time_bnds', 'lat', 'lat_bnds', 'lon', 'lon_bnds', 'pre', 'pre_bnds', 'land_fraction', 'xch4', 'xch4_nobs', 'xch4_stderr', 'xch4_stddev', 'column_averaging_kernel', 'vmr_profile_ch4_apriori'])

Process finished with exit code 0
"""

print(ds.variables['time'])
"""
<class 'netCDF4._netCDF4.Variable'>
float64 time(time)
    standard_name: time
    long_name: time
    units: days since 1990-01-01
    calendar: standard
    axis: T
    comment: time center
unlimited dimensions: 
current shape = (228,)
filling on, default _FillValue of 9.969209968386869e+36 used
"""

print(ds.variables['time'][0])
"""
4763.5  # 意思是从1990-01-01开始加上4763.5天
"""

time = nc.num2date(ds.variables['time'][:], 'days since 1990-01-01').data  # 转换成时间戳
print(time[0])
"""
2003-01-16 12:00:00
"""

文章来源:https://blog.csdn.net/qq_40292148/article/details/135538539
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。