-
Notifications
You must be signed in to change notification settings - Fork 65
/
readimage.m
135 lines (134 loc) · 4.59 KB
/
readimage.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
%-*-octave-*--
%
% READIMAGE
%
% Read image produced by sw4 on format with the *.sw4img extension.
% This script will not read the older WPP image format.
%
% [im,x,y,z,plane,t,timestring]=readimage( imfile, pnr, verbose, machineformat )
%
% Input: imfile - Image file name
% pnr - Patch number, if more than one grid is used.
% verbose - Set to 1 to display file header information.
% machineformat - passed to fopen to read big endian, little endian, etc
% Output: im - The image, as a 2D array.
% x, y, z - The spatial coordinates of the image, one of these is a scalar.
% plane - 0: x=const, 1: y=const, 2: z=const
% t - Simulation time at which the image was output.
% timestring - String holding creation date
function [im,x,y,z,plane,t,timestring,npatches]=readimage( imfile, pnr, verbose, machineformat )
if nargin < 4
machineformat='native';
end;
if nargin < 3
verbose= 0;
end;
if nargin < 2
pnr = 1;
end;
fd=fopen(imfile,'r',machineformat);
if fd ~= -1
% Read header
prec =fread(fd,1,'int');
npatches=fread(fd,1,'int');
t =fread(fd,1,'double');
plane =fread(fd,1,'int');
coord =fread(fd,1,'double');
mode =fread(fd,1,'int');
gridinfo=fread(fd,1,'int');
timecreated=fread(fd,[1 25],'uchar');
timestring=num2str(timecreated,'%c');
mstr=getimagemodestr(mode);
% Display header
if verbose == 1
disp(['Found: prec = ' num2str(prec) ' t= ' num2str(t) ' plane= ' num2str(plane)]);
disp([' coord = ' num2str(coord) ' mode= ' mstr ' npatches= ' num2str(npatches) ]);
disp([' gridinfo (number of curvilinear patches)= ' num2str(gridinfo) ]);
printf(' file created on %s\n', timecreated(1:24) );
end;
firstCurviPatch = npatches - gridinfo + 1;
for p=1:npatches
h(p) = fread(fd,1,'double');
zmin(p) = fread(fd,1,'double');
ib(p) = fread(fd,1,'int');
ni(p) = fread(fd,1,'int');
jb(p) = fread(fd,1,'int');
nj(p) = fread(fd,1,'int');
if verbose == 1
disp([' patch nr ' num2str(p) ' has h = ' num2str(h(p)) ' zmin = ' num2str(zmin(p))]);
end;
end;
% Read data
readz = 0;
if pnr <= npatches
for p=1:pnr-1
fseek(fd,(ni(p)-ib(p)+1)*(nj(p)-jb(p)+1)*prec,'cof');
end;
if prec == 4
im0 = fread(fd,[ni(pnr)-ib(pnr)+1 nj(pnr)-jb(pnr)+1],'float');
else
im0 = fread(fd,[ni(pnr)-ib(pnr)+1 nj(pnr)-jb(pnr)+1],'double');
end;
for p=pnr+1:npatches
fseek(fd,(ni(p)-ib(p)+1)*(nj(p)-jb(p)+1)*prec,'cof');
end;
% read grid z-coordinates
% if (pnr == npatches) && (gridinfo == 1 )
if (pnr >= firstCurviPatch && gridinfo >= 1 )
% printf("Skipping earlier z-ccordinate patches\n");
for p=firstCurviPatch:pnr-1
fseek(fd,(ni(p)-ib(p)+1)*(nj(p)-jb(p)+1)*prec,'cof');
end;
% printf("Reading z-coordinates for patch %d\n", pnr);
if prec == 4
z0 = fread(fd,[ni(pnr)-ib(pnr)+1 nj(pnr)-jb(pnr)+1],'float');
else
z0 = fread(fd,[ni(pnr)-ib(pnr)+1 nj(pnr)-jb(pnr)+1],'double');
end;
% transpose z0 so it gets the same dimensions as im
z = z0';
readz = 1;
end;
% transpose im0 and return result in im
im = im0';
fclose(fd);
if plane == 0
x = coord;
% disp(['Making the grid: plane = ' num2str(plane) ' readz = ' num2str(readz)]);
if readz == 0
y = h(pnr)*((ib(pnr):ni(pnr))-1);
z = zmin(pnr) + h(pnr)*((jb(pnr):nj(pnr))-1);
else
n1 = size(im,1);
n2 = size(im,2);
% make y two-dimensional (same size as z and im)
y = zeros(n1,n2);
for i=1:n1
y(i,:) = h(pnr)*((ib(pnr):ni(pnr))-1);
end
end;
elseif plane == 1
y = coord;
if readz == 0
x = h(pnr)*((ib(pnr):ni(pnr))-1);
z = zmin(pnr) + h(pnr)*((jb(pnr):nj(pnr))-1);
else
n1 = size(im,1);
n2 = size(im,2);
% make x two-dimensional (same size as z and im)
x = zeros(n1,n2);
for i=1:n1
x(i,:) = h(pnr)*((ib(pnr):ni(pnr))-1);
end
end;
elseif plane == 2
x = h(pnr)*((ib(pnr):ni(pnr))-1);
y = h(pnr)*((jb(pnr):nj(pnr))-1);
z = coord;
end;
else
disp(['Error: number of patches on file ' num2str(npatches) ' is smaller than input pnr = ' num2str(pnr)]);
end;
else
disp(['Error: could not open file ' imfile ]);
end;