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

博文

NetCDF格式转换TIFF-以全球GIMMS3g NDVI数据为例

已有 9862 次阅读 2017-10-17 22:24 |系统分类:科研笔记

改编自Matlab中文论坛https://ilovematlab.cn/thread-498179-1-1.html by 'rainbowag'.

提取netcdf数据中的NDVI,并写入geotiff文件,文件名为201401a.tif, 201401b.tif, ...



% ------------ four files in the directory to process ---------------

% ndvi3g_geo_v1_2014_0106.nc4

% ndvi3g_geo_v1_2014_0712.nc4

% ndvi3g_geo_v1_2015_0106.nc4

% ndvi3g_geo_v1_2015_0712.nc4


% ---------------- the code ----------------


clc; clear; close all


%% set the input path forcontaining inputdata

% read the nerCDF4 file

% initial image lat [-90 90] lon[-180 180]

filename = 'ndvi3g*2015*.nc4';

Ifiles = dir(filename);

nIfiles = length(Ifiles);


%% add the referenceinformation

% set the maximum andminimun latitude and longitude

latlim = [-90 90];

lonlim = [-180 180];

% define the geo-reference

R = georefcells(latlim,lonlim,[2160 4320],'ColumnsStartFrom','north');

for nf = 1:nIfiles

   Ifilename = Ifiles(nf).name;

   Ifiletime = Ifilename(end-6);

   year = Ifilename(end-12:end-9);

   year = str2double(year);

   IData = ncread(Ifilename,'ndvi');

   % each.nc4 file contain 12 NDVI images in half year

   if str2double(Ifiletime)==1

       Imonth = 1;

   else

       Imonth = 7;

   end


   if Imonth==1

       for i = 1:2:12

           ndvidata = IData(:,:,i);

           ndvidata = ndvidata';

           % name the files as yyyymma.tif or yyyymmb.tif

           ndviname = [num2str(year),num2str((i+1)/2,'%02d'),'a.tif'];

           % save a TIFF image

           geotiffwrite(ndviname,ndvidata,R);

       end

       for i=2:2:12

           ndvidata = IData(:,:,i);

           ndvidata = ndvidata';

           % name the files as yyyymma.tif or yyyymmb.tif

           ndviname = [num2str(year),num2str(i/2,'%02d'),'b.tif'];

           % save a TIFF image

           geotiffwrite(ndviname,ndvidata,R);

       end

   elseif Imonth==7

       for i = 1:2:12

           ndvidata = IData(:,:,i);

           ndvidata = ndvidata';

           % name the files as yyyymma.tif or yyyymmb.tif

           ndviname = [num2str(year),num2str((i+1)/2+6,'%02d'),'a.tif'];

           % save a TIFF image

           geotiffwrite(ndviname,ndvidata,R);

       end

       for i=2:2:12

           ndvidata = IData(:,:,i);

           ndvidata = ndvidata';

           % name the files as yyyymma.tif or yyyymmb.tif

           ndviname = [num2str(year),num2str(i/2+6,'%02d'),'b.tif'];

           % save a TIFF image

           geotiffwrite(ndviname,ndvidata,R);

       end

   end

end


%%




https://blog.sciencenet.cn/blog-700639-1081275.html

上一篇:地表-地下耦合模型研究地下水在源头流域产流过程中的作用
下一篇:模拟气候和植被类型变化对土壤水量分配的影响
收藏 IP: 139.133.86.*| 热度|

0

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

数据加载中...

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

GMT+8, 2024-4-25 22:58

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部