记上一篇数据下载的博客Python-NOAA提取气象数据的下载链接(核密度分析)继续我们后面的工作。
以.nc
结尾的数据需要下载netCDF库来读取以及使用pandas.Dataframe()存储nc文件的数据
一、netCDF库的安装
pip install netCDF4 -i https://pypi.tuna.tsinghua.edu.cn/simple
这里使用清华源来安装netCDF,速度快,无差别
二、读取nc文件的内容
import netCDF4 as nc
import pandas as pd
import os
datapath = "./precip.1979.nc"
ncdata = nc.Dataset(datapath)
print(ncdata.variables.keys())
for i in ncdata.variables.keys():
print("-------------------------------------------------------")
print(i)
print(ncdata.variables[i])
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time = nc.variables['time'][:]
precip = nc.variables['precip'][:]
#将dict_keys里lat导入pd.DataFrame
ds = pd.DataFrame(lat)
#导出为csv
df = ds.to_csv("./nc.time.csv",sep=',',header=True,index=True)
print("ok!")