Python读取nc格式的CSIF数据转成csv格式
1 下载安装netCDF4包
使用清华大学的镜像: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple netCDF4
2 编写程序
2.1 获取netCDF4文件的格式
from netCDF4 import Dataset
import numpy as np
import os
path = "F:\\OCO2.SIF.all.daily.2001.nc"
csv_path = "F:\\test.csv"
dst = Dataset(path, mode='r', format="netCDF4")
print(dst)
运行结果:
<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF4 data model, file format HDF5):
dimensions(sizes): lat(360), lon(720), doy(92)
variables(dimensions): float32 lat(lat), float32 lon(lon), int16 doy(doy), float32 all_daily_sif(doy, lat, lon)
groups:
由上可见CSIF数据all_daily_sif是三维的,具体size为:
d
o
y
(
92
)
×
l
a
t
(
360
)
×
l
o
n
(
720
)
doy(92)\times lat(360)\times lon(720)
doy(92)×lat(360)×lon(720) 由于CSIF时间分辨率是4天,所以doy维是92;空间分辨率是0.5°,所以lat是360,lon是720。
print(dst.variables.keys())
for i in dst.variables.keys():
print('%s: %s' % (i, dst.variables[i].ncattrs()))
运行结果是:
dict_keys(['lat', 'lon', 'doy', 'all_daily_sif'])
lat: ['units', 'standard_name', 'axis', 'long_name']
lon: ['units', 'standard_name', 'axis', 'long_name']
doy: []
all_daily_sif: ['missing_value']
print(dst.variables.keys())
for i in dst.variables.keys():
print(dst.variables[i])
print('\n')
运行结果:
dict_keys(['lat', 'lon', 'doy', 'all_daily_sif'])
<class 'netCDF4._netCDF4.Variable'>
float32 lat(lat)
units: degrees_north
standard_name: latitude
axis: Y
long_name: latitude
unlimited dimensions:
current shape = (360,)
filling on, default _FillValue of 9.969209968386869e+36 used
<class 'netCDF4._netCDF4.Variable'>
float32 lon(lon)
units: degrees_east
standard_name: longitude
axis: X
long_name: longitude
unlimited dimensions:
current shape = (720,)
filling on, default _FillValue of 9.969209968386869e+36 used
<class 'netCDF4._netCDF4.Variable'>
int16 doy(doy)
unlimited dimensions:
current shape = (92,)
filling on, default _FillValue of -32767 used
<class 'netCDF4._netCDF4.Variable'>
float32 all_daily_sif(doy, lat, lon)
missing_value: -999.9
unlimited dimensions:
current shape = (92, 360, 720)
filling on, default _FillValue of 9.969209968386869e+36 used
for i in dst.dimensions.keys():
print('%s_sizes: %s' % (i, dst.dimensions[i].size))
运行结果:
lat_sizes: 360
lon_sizes: 720
doy_sizes: 92
2.2 提取nc数据转存为csv
IniSif = dst.variables['all_daily_sif'][:].data
latdata = dst.variables['lat'][:].data
londata = dst.variables['lon'][:].data
latname = []
lonname = []
for lat in range(0, 360):
latname.append(str(latdata[lat]))
for lon in range(0, 720):
lonname.append(str(londata[lon]))
sif = IniSif[0][:]
sifframe = pd.DataFrame(sif, index=latname, columns=lonname)
for doy in range(1,92):
sif = IniSif[doy][:]
siflist = pd.DataFrame(sif,index = latname,columns =lonname)
sifframe = pd.concat([sifframe, siflist])
sifframe.to_csv(csv_path,sep=',')
dst.close()
生成csv文件如下图所示,列索引为lon值,行索引为lat值:
|