NetCDF格式存储的栅格数据转换的MATLAB批处理程序

处理环境

操作系统:Windows 10 64bit
软件:MATLAB R2016a
   ArcGIS Desktop 10.2
数据下载:链接:http://pan.baidu.com/s/1slIZ5nR 密码:2z3f

MATLAB批量处理NetCDF文件

设置输入输出

  将待处理的*.nc文件统一放在NDVI文件夹中,在MATLAB中设置输入输出路径:

1
2
pathinput = 'D:\Program Files\MATLAB\Work\NDVI';
pathoutput = 'D:\output\';

获取输入文件名

  执行以下命令,可将输入文件夹NDVI中的所有文件信息存储到files变量中:

1
2
filedir = dir(pathinput);
files = filedir(3:end);

查看NetCDF文件内部格式

  查看NetCDF文件内部格式以读取其中存储的数据:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
>> ncdisp(files(1).name);
Source:
D:\Program Files\MATLAB\Work\NDVI\A_19810816.nc
Format:
netcdf4
Global Attributes:
title = 'Normalized Difference Vegetation Index parameters derived from NOAA-07 GAC data for day 1981/228'
institution = 'NASA/GSFC/SED/ESD/HBSL/TIS/MODIS-LAND > MODIS Land Science Team, Terrestrial Information Systems, Hydrospheric and Biospheric Science Laboratory, Earth Sciences Division, Science and Exploration Directorate, Goddard Space Flight Center, NASA'
Conventions = 'CF-1.6'
Metadata_Conventions = 'CF-1.6, Unidata Dataset Discovery v1.0'
standard_name_vocabulary = 'CF Standard Name Table (v25, 05 July 2013)'
naming_authority = 'gov.noaa.ncdc'
license = 'No constraints on data access or use'
cdm_data_type = 'Grid'
time_coverage_start = '1981-08-16T00:00:00Z'
time_coverage_end = '1981-08-16T23:59:00Z'
product_version = 'v4r0'
platform = 'NOAA-4 > National Oceanic & Atmospheric Administration-4'
sensor = 'AVHRR > Advanced Very High Resolution Radiometer'
keywords_vocabulary = 'NASA Global Change Master Directory (GCMD) Earth Science Keywords, Version 8.0'
keywords = 'EARTH SCIENCE > BIOSPHERE > VEGETATION > VEGETATION INDEX'
source = 'NSS.GHRR.NC.D81227.S2221.E0015.B0075556.GC, NSS.GHRR.NC.D81228.S0016.E0210.B0075657.WI, NSS.GHRR.NC.D81228.S0205.E0359.B0075758.WI, NSS.GHRR.NC.D81228.S0354.E0548.B0075859.WI, NSS.GHRR.NC.D81228.S0545.E0718.B0075960.WI, NSS.GHRR.NC.D81228.S0713.E0859.B0076061.WI, NSS.GHRR.NC.D81228.S0854.E1036.B0076162.GC, NSS.GHRR.NC.D81228.S1031.E1157.B0076263.GC, NSS.GHRR.NC.D81228.S1157.E1217.B0076363.GC, NSS.GHRR.NC.D81228.S1212.E1357.B0076364.GC, NSS.GHRR.NC.D81228.S1354.E1546.B0076465.WI, NSS.GHRR.NC.D81228.S1542.E1735.B0076566.WI, NSS.GHRR.NC.D81228.S1731.E1846.B0076667.WI, NSS.GHRR.NC.D81228.S1842.E2025.B0076768.WI, NSS.GHRR.NC.D81228.S2020.E2214.B0076769.GC, NSS.GHRR.NC.D81228.S2210.E2356.B0076970.GC, NSS.GHRR.NC.D81228.S2351.E0140.B0077071.GC, NCEP_REANALYSIS_SURFACE_1981228.hdf, NCEP_REANALYSIS_SURFACE_1981228.hdf, TOMS_1981228.hdf, GLOBAL_LAND_SEA.4KM, ephemeris.ccar.23Oct2007'
date_created = '2013-08-17T18:43:40Z'
spatial_resolution = '0.050000 degrees per pixel'
geospatial_lat_min = -90
geospatial_lat_max = 90
geospatial_lon_min = -180
geospatial_lon_max = 180
metadata_link = 'gov.noaa.ncdc:C00813'
cdr_program = 'NOAA Climate Data Record Program for satellites'
cdr_variable = 'NDVI'
Process = 'LTDR_GAPS'
PostProcessingVersion = '2.3'
PFIIVersion = '3.5.28'
Satellite = 'NOAA-07'
Instrument = 'AVHRR'
InputDataType = 'GAC'
summary = 'AVHRR GAC data from NOAA-07 for 1981, days 228 to 228, processed by the Long-Term Land Data Record (LTDR) project (v3.5.28) into normalized difference vegetation index (NDVI) and quality-control flags'
ProductionDate = '2013-08-17T18:43:40.000000Z'
grid_mapping_name = 'latitude_longitude'
ESDT = 'AVH13C1'
LocalGranuleId = 'AVHRR-Land_v004_AVH13C1_NOAA-07_19810816_c20130817184340.nc'
id = 'AVHRR-Land_v004_AVH13C1_NOAA-07_19810816_c20130817184340.nc'
RangeBeginningDate = '1981-228'
RangeBeginningTime = '00:00:00.0000'
RangeEndingDate = '1981-228'
RangeEndingTime = '23:59:59.9999'
PercentMissingData = '11.46'
PercentValidDaytimeData = '26.85'
PercentValidDaytimeLand = '26.85'
PercentValidClearDaytimeLand = '6.95'
PercentValidDaytimeLandInCloudShadow = '1.21'
PercentValidClearDaytimeWater = '0.00'
PercentValidDaytimeWaterInCloudShadow = '0.00'
Dimensions:
latitude = 3600
longitude = 7200
time = 1
Variables:
latitude
Size: 3600x1
Dimensions: latitude
Datatype: single
Attributes:
units = 'degrees_north'
long_name = 'latitude'
axis = 'Y'
standard_name = 'latitude'
longitude
Size: 7200x1
Dimensions: longitude
Datatype: single
Attributes:
units = 'degrees_east'
long_name = 'longitude'
axis = 'X'
standard_name = 'longitude'
time
Size: 1x1
Dimensions: time
Datatype: single
Attributes:
units = 'days since 1981-01-01 00:00:00'
long_name = 'time'
axis = 'T'
standard_name = 'time'
calendar = 'gregorian'
NDVI
Size: 7200x3600x1
Dimensions: longitude,latitude,time
Datatype: int16
Attributes:
scale_factor = 0.0001
add_offset = 0
long_name = 'NOAA Climate Data Record of Normalized Difference Vegetation Index'
units = '1'
valid_range = [-1000 10000]
_FillValue = -9999
standard_name = 'normalized_difference_vegetation_index'
TIMEOFDAY
Size: 7200x3600x1
Dimensions: longitude,latitude,time
Datatype: int16
Attributes:
scale_factor = 0.01
add_offset = 0
long_name = 'Time since Start of Data Day'
units = 'h since 1981-08-16 00:00:00'
valid_range = [0 2399]
_FillValue = -9999
QA
Size: 7200x3600x1
Dimensions: longitude,latitude,time
Datatype: int16
Attributes:
flag_masks = [2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 -32768]
flag_meanings = 'cloudy cloud_shadow water sunglint dense_dark_vegetation night ch1_to_5_valid ch1_invalid ch2_invalid ch3_invalid ch4_invalid ch5_invalid rho3_invalid BRDF_corr_problem polar_flag'
long_name = 'Quality Assurance'

批量读取变量并另存为txt

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
for i = 1:length(files)
fileinput = [pathinput,'\',files(i).name];
NDVI = ncread(fileinput,'NDVI');
FillValue = ncreadatt(fileinput,'NDVI','_FillValue');
longitude = ncread(fileinput,'longitude');
latitude = ncread(fileinput,'latitude');
NDVI(isnan(NDVI))=FillValue;
[m, n] = size(NDVI);
fileoutput = [pathoutput,files(i).name(1:end-3),'.txt'];
ncols = ['ncols ' num2str(m)];
nrows = ['nrows ' num2str(n)];
xllcorner = ['xllcorner ' num2str(min(longitude))];
yllcorner = ['yllcorner ' num2str(min(latitude))];
cellsize = ['cellsize ' num2str(0.05)];
NODATA_value = ['NODATA_value ' num2str(FillValue)];
//写入栅格数据的头文件
dlmwrite(fileoutput,ncols,'delimiter', '','newline','pc','-append');
dlmwrite(fileoutput,nrows,'delimiter', '','newline','pc','-append');
dlmwrite(fileoutput,xllcorner,'delimiter', '','newline','pc','-append');
dlmwrite(fileoutput,yllcorner,'delimiter', '','newline','pc','-append');
dlmwrite(fileoutput,cellsize,'delimiter', '','newline','pc','-append');
dlmwrite(fileoutput,NODATA_value,'delimiter', '','newline','pc','-append');
//写入数据
dlmwrite(fileoutput,fliplr(rot90(NDVI,3)),'delimiter','\t','newline','pc','-append');
end

ArcMap中另存为栅格数据

  在ArcMap中加载txt格式的栅格数据:

  使用工具箱中的ASCIIToRaster工具导出数据:

数据下载:链接:http://pan.baidu.com/s/1slIZ5nR 密码:2z3f
完整代码:ndvi2raster.m

若本文对您有帮助,请打赏鼓励本人!
---------------- End ----------------
扫二维码
扫一扫,使用手机查看

扫一扫,使用手机查看

QQ