Python is TIFF format data processing NetCDF data (script code attached)

Blog small order: NetCDF data format widely used to store scientific data, the most recent days own use python dealt with some NetCDF data, special essays in this blog post to remember it.
Reference blog:
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 data Profile

NetCDF official document
https://www.unidata.ucar.edu/software/netcdf/docs/netcdf_introduction.html

2.Python basic operations data NetCDF

Python library is used exclusively for data processing NetCDF netCDF4 libraries, to be installed in their path 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. Code - using the Python NetCDF Tiff file for the dump file

This code is written in its own script when processing NDVI data, the aim of each of the NC NDVI data extraction format and saved as TIFF data 12, to facilitate post-processing analysis.

# -*- 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()

Author: DQTDQT
limited the author is limited, as there are any errors in the text, welcome hesitate to correct me, exchanges.

Contact:
QQ: 1426097423
E-mail: [email protected]

This article belongs to the author and blog Park total, welcome to reprint, exchange, but without the author's consent declared by this section must be retained, and given the original article link in the apparent position of the page, if you feel you find this helpful, thumbs welcome to explore.

Guess you like

Origin www.cnblogs.com/xsman/p/11298685.html