当前位置:   article > 正文

【技巧】matlab批量读取nc文件并转为tif

matlab批量读取nc文件

matlab在批量处理nc文件上相比于arcgis既方便又快捷,这里介绍如何利用matlab读取nc文件并导出为tif
基本代码:

clc;
clear;

%读取nc文件基本信息
inpath = 'A0_1961-2020_tm_pre_nc\';  %nc文件所在文件夹
info1 = ncinfo('A0_1961-2020_tm_pre_nc\CN05.1_Pre_1961_2018_month_025x025.nc');  %nc文件基本信息

%先在arcgis中读取nc文件中的一个时间点数据,并导出为tif,以便后面为nc文件批量导出提供坐标系
maskname = 'A1_1961-2020_tm_pre_tif\mask0.25x0.25.tif';
[A,R] = geotiffread(maskname);  %主要目的是获取R,也就是坐标系
info = geotiffinfo(maskname);

%获取nc文件基本信息
infile = strcat(inpath,'CN05.1_Pre_1961_2018_month_025x025.nc');
lon = ncread(infile,'lon');
lat = ncread(infile,'lat');
D = ncread(infile,'pre');   %读取变量D的内容

%按时间分别读取
for year = 2015:2018  %年循环 
    for month = 1:12  %月循环
        data = D(:,:,month);  %逐月读取
        data = rot90(data);  %rot90逆时针旋转90°/flipud上下翻转/fliplr左右翻转  
                             %有时候数据行列数反了,需要旋转数据来调整
        export2tif = ['A1_1961-2020_tm_pre_tif\CN05.1_Pre_',int2str(year),'_M',int2str(month),'_0.25x0.25.tif'];   %导出路径和名字
        geotiffwrite(export2tif,data,R,'GeoKeyDirectoryTag',info.GeoTIFFTags.GeoKeyDirectoryTag);  %批量导出为tif
    end
    disp(year);
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/656380
推荐阅读
相关标签
  

闽ICP备14008679号