博客小序:NetCDF数据广泛的用于科学数据存储,最近几日自己处理了一些NetCDF数据,特撰此博文以记之。
参考博客:
https://www.cnblogs.com/shoufengwei/p/9068379.html
https://blog.csdn.net/EWBA_GIS_RS_ER/article/details/84076201
http://www.clarmy.net/2018/11/01/python%E8%AF%BB%E5%8F%96nc%E6%96%87%E4%BB%B6%E7%9A%84%E5%85%A5%E9%97%A8%E7%BA%A7%E6%93%8D%E4%BD%9C/
1.NetCDF数据简介
NetCDF官方文档
https://www.unidata.ucar.edu/software/netcdf/docs/netcdf_introduction.html
2.Python对NetCDF数据基本操作
python中专门用于处理NetCDF数据的库为netCDF4库,需在自己的python路径中安装
In[1]:import netCDF4 as nc #模块导入
In[2]:data = 'F:\\data___python_test\\nc_to_tif\\nc\\ndvi3g_geo_v1_1990_0106.nc4'
nc_data = nc.Dataset(data) #利用.Dataset()方法读取nc数据
nc_data
Out[2]: <type 'netCDF4._netCDF4.Dataset'>
In[3]:nc_data.variables #以存储ndvi的nc数据为例,查看nc文件包含的变量
Out[3]:OrderedDict([(u'lon', <type 'netCDF4._netCDF4.Variable'>
float64 lon(lon)
unlimited dimensions:
current shape = (4320,)
filling on, default _FillValue of 9.96920996839e+36 used),
(u'lat', <type 'netCDF4._netCDF4.Variable'>
float64 lat(lat)
unlimited dimensions:
current shape = (2160,)
filling on, default _FillValue of 9.96920996839e+36 used),
(u'time', <type 'netCDF4._netCDF4.Variable'>
float64 time(time)
unlimited dimensions:
current shape = (12,)
filling on, default _FillValue of 9.96920996839e+36 used),
(u'satellites', <type 'netCDF4._netCDF4.Variable'>
int16 satellites(time)
unlimited dimensions:
current shape = (12,)
filling on, default _FillValue of -32767 used),
(u'ndvi', <type 'netCDF4._netCDF4.Variable'>
int16 ndvi(time, lat, lon)
units: 1
scale: x 10000
missing_value: -5000.0
valid_range: [-0.3 1. ]
unlimited dimensions:
current shape = (12, 2160, 4320)
filling on, default _FillValue of -32767 used),
(u'percentile', <type 'netCDF4._netCDF4.Variable'>
int16 percentile(time, lat, lon)
units: %
scale: x 10
flags: flag 0: from data flag 1: spline interpolation flag 2: possible snow/cloud cover
valid_range: flag*2000 + [0 1000]
unlimited dimensions:
current shape = (12, 2160, 4320)
filling on, default _FillValue of -32767 used)])
In[4]:ndvi = nc_data.variables['ndvi'] #单独查看nc文件中存储的变量信息
ndvi
Out[4]:<type 'netCDF4._netCDF4.Variable'>
int16 ndvi(time, lat, lon)
units: 1
scale: x 10000
missing_value: -5000.0
valid_range: [-0.3 1. ]
unlimited dimensions:
current shape = (12, 2160, 4320)
filling on, default _FillValue of -32767 used
3.代码——利用Python将NetCDF文件转存为Tiff文件
此代码是自己在处理NDVI数据时所写的脚本,目的是将每一期NDVI的NC格式数据提取并另存为12期的TIFF数据,便于后期分析处理。
# -*- coding: utf-8 -*-
# 模块导入
import numpy as np
import netCDF4 as nc
from osgeo import gdal,osr,ogr
import os
import glob
# 单个nc数据ndvi数据读取为多个tif文件,并将ndvi值化为-1-1之间
def NC_to_tiffs(data,Output_folder):
nc_data_obj = nc.Dataset(data)
Lon = nc_data_obj.variables['lon'][:]
Lat = nc_data_obj.variables['lat'][:]
ndvi_arr = np.asarray(nc_data_obj.variables['ndvi']) #将ndvi数据读取为数组
ndvi_arr_float = ndvi_arr.astype(float)/10000 #将int类型改为float类型,并化为-1 - 1之间
#影像的左上角和右下角坐标
LonMin,LatMax,LonMax,LatMin = [Lon.min(),Lat.max(),Lon.max(),Lat.min()]
#分辨率计算
N_Lat = len(Lat)
N_Lon = len(Lon)
Lon_Res = (LonMax - LonMin) /(float(N_Lon)-1)
Lat_Res = (LatMax - LatMin) / (float(N_Lat)-1)
for i in range(len(ndvi_arr[:])):
#创建.tif文件
driver = gdal.GetDriverByName('GTiff')
out_tif_name = Output_folder + '\\'+ data.split('\\')[-1].split('.')[0] + '_' + str(i+1) + '.tif'
out_tif = driver.Create(out_tif_name,N_Lon,N_Lat,1,gdal.GDT_Float32)
# 设置影像的显示范围
#-Lat_Res一定要是-的
geotransform = (LonMin,Lon_Res, 0, LatMax, 0, -Lat_Res)
out_tif.SetGeoTransform(geotransform)
#获取地理坐标系统信息,用于选取需要的地理坐标系统
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326) # 定义输出的坐标系为"WGS 84",AUTHORITY["EPSG","4326"]
out_tif.SetProjection(srs.ExportToWkt()) # 给新建图层赋予投影信息
#数据写出
out_tif.GetRasterBand(1).WriteArray(ndvi_arr_float[i]) # 将数据写入内存,此时没有写入硬盘
out_tif.FlushCache() # 将数据写入硬盘
out_tif = None # 注意必须关闭tif文件
def main():
Input_folder = 'F:\\data___python_test\\nc_to_tif\\nc'
Output_folder = 'F:\\data___python_test\\nc_to_tif\\tif_result'
# 读取所有nc数据
data_list = glob.glob(Input_folder + '\\*.nc4')
for i in range(len(data_list)):
data = data_list[i]
NC_to_tiffs(data,Output_folder)
print data + '-----转tif成功'
print'----转换结束----'
main()
本文作者:DQTDQT
限于作者水平有限,如文中存在任何错误,欢迎不吝指正、交流。
联系方式:
QQ:1426097423
e-mail:duanquntaoyx@163.com
本文版权归作者和博客园共有,欢迎转载、交流,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,如果觉得本文对您有益,欢迎点赞、探讨。