-
Notifications
You must be signed in to change notification settings - Fork 3
/
tinitalia.m
95 lines (79 loc) · 3 KB
/
tinitalia.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
function tinitalia(dirp,diro)
% TINITALIA(dirp,diro)
%
% Converts the entire available TINITALY topography data set into newly
% made *.MAT and *.hdr files. See also $UFILES/tinitalia
%
% INPUT:
%
% dirp Subdirectory [e.g. 'DATA'] of:
% diro Main directory [e.g. '/home/fjsimonsIFILES/TOPOGRAPHY/ITALY/TINITALY']
%
% SEE ALSO:
%
% TINITALY, which reads individual files
%
% EXAMPLE:
%
% Making the default inputs work, my directory
% /u/fjsimons/IFILES/TOPOGRAPHY/ITALY/TINITALY/DATA
% contains at least one necessary file
% e43010_s10.zip
% And, in there, I am able to do, without any further inputs:
% tinitalia
% which produces the outputs as *.MAT and *.hdr files
%
% Last modified by fjsimons-at-alum.mit.edu, 08/29/2019
% Bottom-level directory name, taken from the Tinitaly download
defval('dirp','DATA')
% Top-level directory name, where you keep the Tinitaly directory
defval('diro','/u/fjsimons/IFILES/TOPOGRAPHY/ITALY/TINITALY')
% Find all the zip files inside the directory
zpf=ls2cell(fullfile(fullfile(diro,dirp),'*.zip'));
% We know how many header lines there are, this is fixed
nhdr=6;
% Find all the headers and turn them into a structure
for index=1:length(zpf)
% The root filename
fpref=pref(zpf{index});
% Prepare the full MAT filename
fmat=fullfile(diro,dirp,sprintf('%s.mat',fpref));
% The full HDR filename, if it exists
fhdr=fullfile(diro,dirp,sprintf('%s.hdr',fpref));
% If the MAT file doesn't exist, makes it (and the HDR file if it didn't)
if exist(fmat)~=2
%%%%%%%%%% METADATA AND DATA READ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Now load the data and save them in the structure also
fname=unzip(fpref);
% Read the uncompressed asc file, open the asc filename
fid=fopen(fname{1});
%%%%%%%%%% METADATA (RE-)READ%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read what we already knew, could have bypassed $UFILES/tinitaly
H=textscan(fid,'%s %d',nhdr);
% Make a header structure variable... overwrite what you might have had
eval(sprintf('%s=cell2struct(mat2cell(H{2},ones(size(H{2}))),H{1},1);',...
fpref))
%%%%%%%%%% DATA READ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read the actual data into a structure variable
reddit=textscan(fid,'%f');
% Make a data structure variable, verified orientation with the pictures
eval(sprintf('%s.topodata=[reshape(reddit{1}'',H{2}(1),H{2}(2))]'';',...
fpref))
fclose(fid);
% Now if you do image(topodata) the picture comes out in the right orientation
% Remove the uncompressed file
system(sprintf('rm -rf %s',fname{1}));
% And then save it all as a matfile
eval(sprintf('save %s %s',fmat,fpref))
% And also write the hdr file; see also $UFILES/tinitalia
if exist(fhdr)~=2
% See also STRUCT2ASCI
fidh=fopen(fhdr,'w');
for index=1:nhdr
fiel=cell2mat(H{1}(index));
fprintf(fidh,'%-13s %d\n',fiel,eval(sprintf('%s.%s',fpref,fiel)));
end
fclose(fidh);
end
end
end