-
Notifications
You must be signed in to change notification settings - Fork 19
/
read_arcinfo_ascii.m
116 lines (96 loc) · 2.97 KB
/
read_arcinfo_ascii.m
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
% read_arcinfo_ascii : Reads ascii formatted ArcInfo files
%
% Call :
% [data,x,y,dx,nanval,x0,y0,xll,yll]=read_arcinfo_ascii(filename);
%
%
% Copyright (C) 2004 Thomas Mejer Hansen
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
%
function [data,x,y,dx,nanval,x0,y0,xll,yll]=read_arcinfo_ascii(filename);
mfilename='';
mgstat_verbose(sprintf('Calling %s, file %s',mfilename,filename),2);
fid=fopen(filename,'r');
% NCOLS
l=fgetl(fid);
nc=str2num(l(7:length(l)));
mgstat_verbose(sprintf('%s : NCOLS=%4.2d',l(1:5),nc),10);
% NROWS
l=fgetl(fid);
nr=str2num(l(7:length(l)));
mgstat_verbose(sprintf('%s : NROWS=%4.2d',l(1:5),nr),10);
% XLLCENTER/XLLCORNER
l=fgetl(fid);
if (strcmp(upper(l(4:9)),'CORNER'));
xll='CORNER';
else
xll='CENTER';
end
x0=str2num(l(11:length(l)));
mgstat_verbose(sprintf('XLL%s=%4.2g',xll,x0),10);
% YLLCENTER/YLL/CORNER
l=fgetl(fid);
if (strcmp(upper(l(4:9)),'CORNER'));
yll='CORNER';
else
yll='CENTER';
end
y0=str2num(l(11:length(l)));
mgstat_verbose(sprintf('YLL%s=%8.4g',yll,y0),10);
% CELLSIZE
l=fgetl(fid);
dx=str2num(l(10:length(l)));
mgstat_verbose(sprintf('%s : DX=%8.4g',l(1:8),dx),10);
% NO DATA VALUE
fpos=ftell(fid); % GET POSITION IN FILE
l=fgetl(fid);
if (strcmp(upper(l(1:12)),'NODATA_VALUE'))
nanval=str2num(l(14:length(l)));
mgstat_verbose(sprintf('%s : NANVALUE=%8.4g',l(1:12),nanval),10);
else
nanval=[];
fseek(fid,0,'bof');
fseek(fid,fpos,'bof'); % RETURN TO 'FPOS' (START OF LINE)
end
% CALCULATE X AND Y
if strcmp(xll,'CENTER');
x=[0:1:(nc-1)].*dx+x0;
mgstat_verbose(sprintf('X : AT CENTER'),10);
else
x=[0:1:(nc-1)].*dx+x0+dx/2;
mgstat_verbose(sprintf('X : AT CORNER'),10);
end
if strcmp(yll,'CENTER');
y=[0:1:(nr-1)].*dx+y0;
mgstat_verbose(sprintf('Y : AT CENTER'),10);
else
y=[0:1:(nr-1)].*dx+y0+dx/2;
mgstat_verbose(sprintf('Y : AT CORNER'),10);
end
% now read the data
try
fpos=ftell(fid);
data=flipud(reshape(fscanf(fid,'%g'),length(x),length(y))');
catch
fseek(fid,0,'bof');
fseek(fid,fpos,'bof'); % RETURN TO 'FPOS' (START OF LINE)
d=fread(fid,'char');
data=flipud(str2num(setstr(d)'));
end
if ~isempty(nanval)
data(find(data==nanval))=NaN;
end
fclose(fid);