wenzhaofei的个人博客分享 http://blog.sciencenet.cn/u/wenzhaofei

博文

利用Python读写GIMMS NDVI3g数据

已有 10701 次阅读 2015-2-2 13:28 |个人分类:科研笔记|系统分类:科研笔记| and, Reading, GIMMS, Rewriting, NDVI3g

    NDVI3g是新一代GIMMS 时序NDVI数据,时间范围从1981.07-2012.12,数据频率为15天。目前,可在线下载数据集所有数据及其说明文档,一共有约13.5G

   针对该数据不能直接用ENVIArcGIS读取和需手动配准的问题,网上已介绍很多方法,包括利用ENVI读取和配准ArcGIS读取和配准以及利用MATLAB读写该数据的方法。前两个方法都试了,个人觉得后一种方法靠谱些,主要是配准的时候出现的一些小问题后一种解决得好一些。我没用过matlab,虽然很它强大,但据说处理影像速度慢。因此,我很多时候习惯用python+gdal来处理影像。下面是我参考Prof.Nanmatlab代码写的利用python+gdal+numpy读写GIMMS的代码(已测)。简单修改一下这段代码,就可以做批量处理了。

 

# -*- coding: utf-8 -*-

"""

Created on Sat Jan 31 21:52:23 2015

@author: Zhaofei Wen

Note: The output NDVI value have been multiplied by 1000

"""

# importing modules

from osgeo import gdal

import numpy as np

 

# setting pj parameters

transform = (-180.0000000, 0.083333, 0.0,90.00000, 0.0, -0.083333)

proj = 'GEOGCS["WGS84",DATUM["WGS_1984",

SPHEROID["WGS84",6378137,298.257223563,

AUTHORITY["EPSG","7030"]],

AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],

UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]'

 

# read ndvi3g data and reshape it to 2-D array

inds = np.fromfile('F:/test/geo81sep15b',dtype='>i2') # ‘geo81sep15b’ is the filename of ndvi3g

inds = inds.reshape(4320, 2160)

 

#transpose (flip and rotate)

ndvi3g = np.transpose(inds)

 

# retrieve ndvi and flag data

ndvi = np.floor(ndvi3g/10)  # ndvi*1000

flag = ndvi3g-np.floor(ndvi3g/10)*10+1

 

#output data

fn_ndvi = 'F:/test/ndvi.tif'

fn_flag = 'F:/test/flag.tif'

 

driver = gdal.GetDriverByName('GTIFF')  # other formats can also be specified here

outndvi = driver.Create(fn_ndvi, 4320,2160, 1, gdal.GDT_Int16)

outflag = driver.Create(fn_flag, 4320,2160, 1, gdal.GDT_Byte)  # 1 <= flagvalue<= 7

 

bandndvi = outndvi.GetRasterBand(1)

bandndvi.WriteArray(ndvi, 0, 0)

bandndvi.FlushCache()

bandndvi.SetNoDataValue(-9999)

bandndvi.GetStatistics(0, 1)

 

bandflag = outflag.GetRasterBand(1)

bandflag.WriteArray(flag, 0, 0)

bandflag.FlushCache()

bandflag.SetNoDataValue(-9999)

bandflag.GetStatistics(0, 1)

 

# georeferencing the output images

outndvi.SetGeoTransform(transform)

outndvi.SetProjection(proj)

 

outflag.SetGeoTransform(transform)

outflag.SetProjection(proj)

 

# release memory

outndvi = None

outflag = None

 




https://blog.sciencenet.cn/blog-365459-864875.html


下一篇:Python2.7同时导入arcpy和gdal
收藏 IP: 124.16.212.*| 热度|

1 徐明

该博文允许注册用户评论 请点击登录 评论 (8 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-12-22 15:30

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部