第一个nc文件的信息读取

1.读入nc文件

from netCDF4 import Dataset

path = "F:\\liulu\\2018satellite_dataset\\NPR_SMOPS_CMAP_D20180101.nc"
dst = Dataset(path, mode='r', format="netCDF4")

2.查看dst

print(dst)
<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF3_CLASSIC data model, file format NETCDF3):
    # 数据来自下列卫星
    Satellite: ESA's SMOS, EUMETSAT's MetOp-A and MetOp-B, JAXA's GCOM-W1, NASA's GPM and SMAP
    # 数据来自下面元件
    Instrument: MIRAS on SMOS, ASCAT on MetOp-A, ASCAT on MetOp-B, AMSR2 on GCOM-W1, GMI on GPM, and SMAP
    # 该图像采用可利投影/等距柱状投影
    Projection: Plate Carree (Equirectangular Projection)
    # 数据覆盖整个地球表面
    Latitude_at_Lower_Left_Corner: -90.0
    Longitude_at_Lower_Left_Corner: -180.0
    Latitude_at_Upper_Right_Corner: 90.0
    Longitude_at_Upper_Right_Corner: 180.0
    # 产品分辨率为0.25度
    Product_Resolution: 0.25 degree
    # 数据起始时间为2018/01/01 00:00-23:59
    Date_Start: 20180101
    Date_End: 20180101
    Time_Start: 00:00 UTC
    Time_End: 23:59 UTC
    # 数据按大端字节序存储,即高字节数据存放在低地址端,低字节数据存放在高地址端
    Byte_Order: Big endian
    # 产品版本是3.0
    Product_Version_Number: 3.0
    # 数据无压缩
    Data_Compression_Type: No Compression
    # Production Location
    Production_Location: NOAA/NESDIS/OSPO at Camp Springs, Maryland, USA
    # Contact Information和Operation Lead,NOAA是美国国家海洋和大气局
    Contact_Information: Science Lead  : Xiwu Zhan,  NOAA/NESDIS/STAR, xiwu.zhan@noaa.gov
      Operation Lead: Limin Zhao, NOAA/NESDIS/OSPO, limin.zhao@noaa.gov
    # 数据维度size:经度1440,维度720——正好对应分辨率为0.25
    dimensions(sizes): Longitude(1440), Latitude(720)
    # 变量(维度)
    variables(dimensions): int16 Blended_SM(Latitude,Longitude), int16 NRT_SMOS_SM(Latitude,Longitude), int16 SMOS_SM(Latitude,Longitude), int16 ASCAT_A_SM(Latitude,Longitude), int16 ASCAT_B_SM(Latitude,Longitude), int16 AMSR2_SM(Latitude,Longitude), int16 GMI_SM(Latitude,Longitude), int16 NSMAP_SM(Latitude,Longitude), int16 SMAP_SM(Latitude,Longitude), int16 Blended_SM_SD(Latitude,Longitude), int8 Blended_hour(Latitude,Longitude), int8 Blended_minute(Latitude,Longitude), int8 NRT_SMOS_hour(Latitude,Longitude), int8 NRT_SMOS_minute(Latitude,Longitude), int8 SMOS_hour(Latitude,Longitude), int8 SMOS_minute(Latitude,Longitude), int8 ASCAT_A_hour(Latitude,Longitude), int8 ASCAT_A_minute(Latitude,Longitude), int8 ASCAT_B_hour(Latitude,Longitude), int8 ASCAT_B_minute(Latitude,Longitude), int8 AMSR2_hour(Latitude,Longitude), int8 AMSR2_minute(Latitude,Longitude), int8 GMI_hour(Latitude,Longitude), int8 GMI_minute(Latitude,Longitude), int8 NSMAP_hour(Latitude,Longitude), int8 NSMAP_minute(Latitude,Longitude), int8 SMAP_hour(Latitude,Longitude), int8 SMAP_minute(Latitude,Longitude), int8 Blended_SM_SD_hour(Latitude,Longitude), int8 Blended_SM_SD_minute(Latitude,Longitude), int16 Blended_QA(Latitude,Longitude), int16 NSMOS_QA(Latitude,Longitude), int16 SMOS_QA(Latitude,Longitude), int16 ASCAT_A_QA(Latitude,Longitude), int16 ASCAT_B_QA(Latitude,Longitude), int16 AMSR2_QA(Latitude,Longitude), int16 GMI_QA(Latitude,Longitude), int16 NSMAP_QA(Latitude,Longitude), int16 SMAP_QA(Latitude,Longitude), int16 Blended_SM_SD_QA(Latitude,Longitude)
    groups: 

3.查看变量variable

# 查看nc文件有哪些变量
print(dst.variables.keys())
# 变量类型为odict_keys
odict_keys(['Blended_SM', 'NRT_SMOS_SM', 'SMOS_SM', 'ASCAT_A_SM', 'ASCAT_B_SM', 'AMSR2_SM', 'GMI_SM', 'NSMAP_SM', 'SMAP_SM', 'Blended_SM_SD', 'Blended_hour', 'Blended_minute', 'NRT_SMOS_hour', 'NRT_SMOS_minute', 'SMOS_hour', 'SMOS_minute', 'ASCAT_A_hour', 'ASCAT_A_minute', 'ASCAT_B_hour', 'ASCAT_B_minute', 'AMSR2_hour', 'AMSR2_minute', 'GMI_hour', 'GMI_minute', 'NSMAP_hour', 'NSMAP_minute', 'SMAP_hour', 'SMAP_minute', 'Blended_SM_SD_hour', 'Blended_SM_SD_minute', 'Blended_QA', 'NSMOS_QA', 'SMOS_QA', 'ASCAT_A_QA', 'ASCAT_B_QA', 'AMSR2_QA', 'GMI_QA', 'NSMAP_QA', 'SMAP_QA', 'Blended_SM_SD_QA'])

4.查看变量属性名称

# 查看nc文件中变量的属性名称
for i in dst.variables.keys():
    print('%s: %s' % (i, dst.variables[i].ncattrs()))
# 包括全拼、单位、填充值、变量有效范围、缩放因子、偏移量
Blended_SM: ['long_name', 'units', 'FillValue', 'valid_range', 'scale_factor', 'add_offset']
NRT_SMOS_SM: ['long_name', 'units', 'FillValue', 'valid_range', 'scale_factor', 'add_offset']
SMOS_SM: ['long_name', 'units', 'FillValue', 'valid_range', 'scale_factor', 'add_offset']
ASCAT_A_SM: ['long_name', 'units', 'FillValue', 'valid_range', 'scale_factor', 'add_offset']
ASCAT_B_SM: ['long_name', 'units', 'FillValue', 'valid_range', 'scale_factor', 'add_offset']
AMSR2_SM: ['long_name', 'units', 'FillValue', 'valid_range', 'scale_factor', 'add_offset']
GMI_SM: ['long_name', 'units', 'FillValue', 'valid_range', 'scale_factor', 'add_offset']
NSMAP_SM: ['long_name', 'units', 'FillValue', 'valid_range', 'scale_factor', 'add_offset']
SMAP_SM: ['long_name', 'units', 'FillValue', 'valid_range', 'scale_factor', 'add_offset']
Blended_SM_SD: ['long_name', 'units', 'FillValue', 'valid_range', 'scale_factor', 'add_offset']
# 包括全拼、填充值、变量有效范围
Blended_hour: ['long_name', 'FillValue', 'valid_range']
Blended_minute: ['long_name', 'FillValue', 'valid_range']
NRT_SMOS_hour: ['long_name', 'FillValue', 'valid_range']
NRT_SMOS_minute: ['long_name', 'FillValue', 'valid_range']
SMOS_hour: ['long_name', 'FillValue', 'valid_range']
SMOS_minute: ['long_name', 'FillValue', 'valid_range']
ASCAT_A_hour: ['long_name', 'FillValue', 'valid_range']
ASCAT_A_minute: ['long_name', 'FillValue', 'valid_range']
ASCAT_B_hour: ['long_name', 'FillValue', 'valid_range']
ASCAT_B_minute: ['long_name', 'FillValue', 'valid_range']
AMSR2_hour: ['long_name', 'FillValue', 'valid_range']
AMSR2_minute: ['long_name', 'FillValue', 'valid_range']
GMI_hour: ['long_name', 'FillValue', 'valid_range']
GMI_minute: ['long_name', 'FillValue', 'valid_range']
NSMAP_hour: ['long_name', 'FillValue', 'valid_range']
NSMAP_minute: ['long_name', 'FillValue', 'valid_range']
SMAP_hour: ['long_name', 'FillValue', 'valid_range']
SMAP_minute: ['long_name', 'FillValue', 'valid_range']
Blended_SM_SD_hour: ['long_name', 'FillValue', 'valid_range']
Blended_SM_SD_minute: ['long_name', 'FillValue', 'valid_range']
# 包括全拼、填充值
Blended_QA: ['long_name', 'FillValue']
NSMOS_QA: ['long_name', 'FillValue']
SMOS_QA: ['long_name', 'FillValue']
ASCAT_A_QA: ['long_name', 'FillValue']
ASCAT_B_QA: ['long_name', 'FillValue']
AMSR2_QA: ['long_name', 'FillValue']
GMI_QA: ['long_name', 'FillValue']
NSMAP_QA: ['long_name', 'FillValue']
SMAP_QA: ['long_name', 'FillValue']
Blended_SM_SD_QA: ['long_name', 'FillValue']

:如果add_offset出现在变量中,则在访问数据的应用程序读取数据后将其添加到数据中。如果scale_factor和add_offset属性同时存在,则在添加偏移量之前先对数据进行缩放。scale_factor和add_offset属性可以一起用于提供简单的数据压缩,将低分辨率的浮点数据作为小整数存储在netCDF文件中。当写入缩放数据时,应用程序应该首先减去偏移量,然后除以缩放因子。

原始数据=存储数据*scale_factor+add_offset
反之,存储数据=(原始数据-add_offset)/scale_factor

5.查看变量属性的具体信息

for i in dst.variables.keys():
    print(dst.variables[i])
    print('\n')
# dst.variables['Blended_SM']的数据类型为<class 'netCDF4._netCDF4.Variable'>
<class 'netCDF4._netCDF4.Variable'>
# 变量Blended_SM的数据类型为int16,维度为Latitude、Longitude
int16 Blended_SM(Latitude, Longitude)
	# Blended_SM的全称为Blended Soil Moisture
    long_name: Blended Soil Moisture
    # Blended_SM的单位是立方米
    units: m^3/m^3
    # Blended_SM的填充值为-999
    FillValue: -999
    # Blended_SM的有效范围是[0,10000]
    valid_range: [    0 10000]
    # Blended_SM的收缩因子为1e-04,无偏移量
    scale_factor: 1e-04
    add_offset: 0.0
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used

<class 'netCDF4._netCDF4.Variable'>
int16 NRT_SMOS_SM(Latitude, Longitude)
    long_name: NRT SMOS Soil Moisture
    units: m^3/m^3
    FillValue: -999
    valid_range: [    0 10000]
    scale_factor: 1e-04
    add_offset: 0.0
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 SMOS_SM(Latitude, Longitude)
    long_name: SMOS Soil Moisture
    units: m^3/m^3
    FillValue: -999
    valid_range: [    0 10000]
    scale_factor: 1e-04
    add_offset: 0.0
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 ASCAT_A_SM(Latitude, Longitude)
    long_name: ASCAT_A Soil Moisture
    units: Degree of Saturation
    FillValue: -999
    valid_range: [    0 10000]
    scale_factor: 1e-04
    add_offset: 0.0
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 ASCAT_B_SM(Latitude, Longitude)
    long_name: ASCAT_B Soil Moisture
    units: Degree of Saturation
    FillValue: -999
    valid_range: [    0 10000]
    scale_factor: 1e-04
    add_offset: 0.0
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 AMSR2_SM(Latitude, Longitude)
    long_name: AMSR2 Soil Moisture
    units: m^3/m^3
    FillValue: -999
    valid_range: [    0 10000]
    scale_factor: 1e-04
    add_offset: 0.0
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 GMI_SM(Latitude, Longitude)
    long_name: GMI Soil Moisture
    units: m^3/m^3
    FillValue: -999
    valid_range: [    0 10000]
    scale_factor: 1e-04
    add_offset: 0.0
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 NSMAP_SM(Latitude, Longitude)
    long_name: NRT SMAP Soil Moisture
    units: m^3/m^3
    FillValue: -999
    valid_range: [    0 10000]
    scale_factor: 1e-04
    add_offset: 0.0
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 SMAP_SM(Latitude, Longitude)
    long_name: SMAP Soil Moisture
    units: m^3/m^3
    FillValue: -999
    valid_range: [    0 10000]
    scale_factor: 1e-04
    add_offset: 0.0
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 Blended_SM_SD(Latitude, Longitude)
    long_name: Standard Deviation of Blended Soil Moisture
    units: m^3/m^3
    FillValue: -999
    valid_range: [    0 10000]
    scale_factor: 1e-04
    add_offset: 0.0
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int8 Blended_hour(Latitude, Longitude)
    long_name: Hour for Blended Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 Blended_minute(Latitude, Longitude)
    long_name: Minute for Blended Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 NRT_SMOS_hour(Latitude, Longitude)
    long_name: Hour for NRT SMOS Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 NRT_SMOS_minute(Latitude, Longitude)
    long_name: Minute for NRT SMOS Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 SMOS_hour(Latitude, Longitude)
    long_name: Hour for SMOS Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 SMOS_minute(Latitude, Longitude)
    long_name: Minute for SMOS Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 ASCAT_A_hour(Latitude, Longitude)
    long_name: Hour for ASCAT_A Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 ASCAT_A_minute(Latitude, Longitude)
    long_name: Minute for ASCAT_A Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 ASCAT_B_hour(Latitude, Longitude)
    long_name: Hour for ASCAT_B Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 ASCAT_B_minute(Latitude, Longitude)
    long_name: Minute for ASCAT_B Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 AMSR2_hour(Latitude, Longitude)
    long_name: Hour for AMSR2 Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 AMSR2_minute(Latitude, Longitude)
    long_name: Minute for AMSR2 Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 GMI_hour(Latitude, Longitude)
    long_name: Hour for GMI Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 GMI_minute(Latitude, Longitude)
    long_name: Minute for GMI Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 NSMAP_hour(Latitude, Longitude)
    long_name: Hour for NRT SMAP Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 NSMAP_minute(Latitude, Longitude)
    long_name: Minute for NRT SMAP Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 SMAP_hour(Latitude, Longitude)
    long_name: Hour for SMAP Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 SMAP_minute(Latitude, Longitude)
    long_name: Minute for SMAP Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 Blended_SM_SD_hour(Latitude, Longitude)
    long_name: Hour for Spare Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int8 Blended_SM_SD_minute(Latitude, Longitude)
    long_name: Minute for Spare Soil Moisture
    FillValue: -99
    valid_range: [ 0 59]
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -127 ignored
<class 'netCDF4._netCDF4.Variable'>
int16 Blended_QA(Latitude, Longitude)
    long_name: QA for Blended Soil Moisture
    FillValue: -999
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 NSMOS_QA(Latitude, Longitude)
    long_name: QA for NRT SMOS Soil Moisture
    FillValue: -999
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 SMOS_QA(Latitude, Longitude)
    long_name: QA for SMOS Soil Moisture
    FillValue: -999
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 ASCAT_A_QA(Latitude, Longitude)
    long_name: QA for ASCAT_A Soil Moisture
    FillValue: -999
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 ASCAT_B_QA(Latitude, Longitude)
    long_name: QA for ASCAT_B Soil Moisture
    FillValue: -999
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 AMSR2_QA(Latitude, Longitude)
    long_name: QA for AMSR2 Soil Moisture
    FillValue: -999
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 GMI_QA(Latitude, Longitude)
    long_name: QA for GMI Soil Moisture
    FillValue: -999
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 NSMAP_QA(Latitude, Longitude)
    long_name: QA for NRT SMAP Soil Moisture
    FillValue: -999
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 SMAP_QA(Latitude, Longitude)
    long_name: QA for SMAP Soil Moisture
    FillValue: -999
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
int16 Blended_SM_SD_QA(Latitude, Longitude)
    long_name: QA for Spare Soil Moisture
    FillValue: -999
unlimited dimensions: 
current shape = (720, 1440)
filling on, default _FillValue of -32767 used

6.查看维dimension

print(dst.dimensions.keys())
# 维度数据类型也为odict_keys,只有两个维
odict_keys(['Longitude', 'Latitude'])

7.查看维度size信息

# 获取维度的sizes信息,获得索引范围
for i in dst.dimensions.keys():
    print('%s_sizes: %s' % (i, dst.dimensions[i].size))
print('------------------------------------------------------')
# 经度size为1440,纬度size为720,则变量size为1440*720
Longitude_sizes: 1440
Latitude_sizes: 720
上一篇:微信小程序简单封装获取定位


下一篇:KNN 使用k-近邻法来预测那些未调查区域最喜欢的编程语言是什么。