设为首页 加入收藏

TOP

Python处理NetCDF格式数据为TIFF数据(附脚本代码)(一)
2019-08-15 00:09:46 】 浏览:180
Tags:Python 处理 NetCDF 格式 数据 TIFF 脚本 代码

博客小序:NetCDF格式数据广泛应用于科学数据的存储,最近几日自己利用python处理了一些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.GetD
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇十三道Python练习题 下一篇Python02之continue,break语句

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目