forked from mjlaine/mcmcf90
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatfiles.F90
403 lines (355 loc) · 10 KB
/
matfiles.F90
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
!!! $Id: matfiles.F90,v 1.13 2017/08/10 07:18:33 mjlaine Exp $
!!! ------------------------------------------------------------------------
!!! mcmc library
!!! File: matfiles.F90
!!! Purpose: Making and reading Matlab V4 MAT-files
!!!
!!! Marko Laine 2001 <[email protected]>
!!! Copyrights licensed under a MIT License.
!!! See the accompanying LICENSE.txt file for terms.
!!! ------------------------------------------------------------------------
!!!
module matfiles
#define FILEUNIT 1234
#if defined(__IFC) || defined(__INTEL_COMPILER)
use ifport
#endif
#if defined(__PGI)
interface
integer function fseek(lu, offset, from)
integer lu, offset, from
end function fseek
end interface
#endif
interface writemat
module procedure writemat4_mat, writemat4_vec
end interface
interface readmat
module procedure readmat4_mat, readmat4_vec
end interface readmat
interface addtomat
module procedure addtomat4_mat
end interface
!! mat v4 header
type Fmatrix
sequence
integer(kind=4) :: type
integer(kind=4) :: mrows
integer(kind=4) :: ncols
integer(kind=4) :: imagf
integer(kind=4) :: namelen
end type Fmatrix
contains
!!!
!!! Write a matrix with 32 bit reals into matlab Level 1.0 (V4) MAT file.
!!!
!!! Format is
!!! 5*4 = 20 first bytes:
!!! type = 0
!!! mrows = number of rows
!!! ncols = number of columns
!!! imagf = 0
!!! namelen = length of the name + 1 for achar(0)
!!! namelen next bytes: name_of_x+\0 as char bytes
!!! mrow*ncol*8 next bytes: data as 8 byte floats
!!!
subroutine writemat4_mat(fname, x, xname, status)
implicit none
real(kind=8), intent(in) :: x(:,:)
character(len=*), intent(in) :: fname
character(len=*), intent(in) :: xname
integer, intent(out), optional :: status
integer :: fstat, i, j
type(Fmatrix) :: xtype
if (present(status)) status = 0
#if defined(__GFORTRAN__) || defined(AIX)
open(unit=FILEUNIT, file=fname, status='replace', &
form='unformatted',access='stream',iostat=fstat)
#else
open(unit=FILEUNIT, file=fname, status='replace', &
form='binary', position='rewind', iostat=fstat)
#endif
if (fstat /= 0) then
write(*,*) 'Error opening file ', trim(fname),' fstat: ',fstat
if (present(status)) then
status = fstat
else
stop
end if
return
end if
xtype%type = 0 ! Little Endian, 1000 for Big Endian
xtype%mrows = size(x,1)
xtype%ncols = size(x,2)
xtype%imagf = 0
xtype%namelen = len_trim(xname)+1
!!! write x to file in binary form
! write(FILEUNIT, iostat=fstat) xtype, trim(xname)//achar(0), x(:,:)
write(FILEUNIT, iostat=fstat) xtype
write(FILEUNIT, iostat=fstat) trim(xname)//achar(0)
if (fstat /= 0) then
write(*,*) 'Error writing to file', fname
if (present(status)) then
status = fstat
close(FILEUNIT)
else
stop
end if
return
end if
! write(FILEUNIT, iostat=fstat) x(:,:)
! write(FILEUNIT, iostat=fstat) x
! write x in loop to avoid stack overflow
do j=1,size(x,2)
do i=1,size(x,1)
write(FILEUNIT) x(i,j)
end do
end do
close(FILEUNIT)
end subroutine writemat4_mat
subroutine writemat4_vec(fname, x, xname, status)
implicit none
real(kind=8), intent(in) :: x(:)
character(len=*), intent(in) :: fname
character(len=*), intent(in) :: xname
integer, intent(out), optional :: status
integer :: fstat, i
type(Fmatrix) :: xtype
if (present(status)) status = 0
#if defined(__GFORTRAN__) || defined(AIX)
open(unit=FILEUNIT, file=fname, status='replace', &
form='unformatted', position='rewind', access='stream', iostat=fstat)
#else
open(unit=FILEUNIT, file=fname, status='replace', &
form='binary', position='rewind', iostat=fstat)
#endif
if (fstat /= 0) then
write(*,*) 'Error opening file', fname
if (present(status)) then
status = fstat
else
stop
end if
return
end if
xtype%type = 0 ! Little Endian, 1000 for Big Endian
xtype%mrows = size(x,1)
xtype%ncols = 1
xtype%imagf = 0
xtype%namelen = len_trim(xname)+1
!!! write x to file in binary form
! write(FILEUNIT, iostat=fstat) xtype, trim(xname)//achar(0), x(:)
write(FILEUNIT, iostat=fstat) xtype, trim(xname)//achar(0)
if (fstat /= 0) then
write(*,*) 'Error writing to file', fname
if (present(status)) then
status = fstat
close(FILEUNIT)
else
stop
end if
return
end if
! write in loop to avoid stack overflow
do i=1,size(x)
write(FILEUNIT) x(i)
end do
close(FILEUNIT)
end subroutine writemat4_vec
!!!
!!! Adding columns to existing mat v4 file.
!!!
subroutine addtomat4_mat(fname, x, status)
implicit none
real(kind=8), intent(in) :: x(:,:)
character(len=*), intent(in) :: fname
integer, intent(out), optional :: status
integer :: fstat !, reclen
type(Fmatrix) :: xtype
if (present(status)) status = 0
! inquire(iolength=reclen)
#if defined(__GFORTRAN__) || defined(AIX)
open(unit=FILEUNIT, file=fname, status='old', &
form='unformatted', position='rewind', access='stream', iostat=fstat)
#else
open(unit=FILEUNIT, file=fname, status='old', &
form='binary', position='rewind', access='direct', iostat=fstat)
#endif
if (fstat /= 0) then
write(*,*) 'Error opening file ', trim(fname),' fstat: ',fstat
if (present(status)) then
status = fstat
return
else
stop
end if
end if
!! read the header
read(FILEUNIT, iostat=fstat) xtype
if (fstat /= 0) then
write(*,*) 'Error reading file', fname
close(FILEUNIT)
if (present(status)) then
status = fstat
return
else
stop
end if
end if
!! check the size
if (xtype%mrows > 0 .and. xtype%mrows /= size(x,1) ) then
write(*,*) 'error: xmat should have same number of rows'
write(*,*) xtype
write(*,*) size(x,2)
if (present(status)) then
status = -1
return
else
stop
end if
end if
!! goto end of file
!! how to do it in fortran? call fseek(FILEUNIT,0,2)?
!! NOW: close and open with append
close(FILEUNIT)
#if defined(__GFORTRAN__) || defined(AIX)
open(unit=FILEUNIT, file=fname, status='old', &
form='unformatted', position='append', access='stream', iostat=fstat)
#else
open(unit=FILEUNIT, file=fname, status='old', &
form='binary', access='append', iostat=fstat)
#endif
if (fstat /= 0) then
write(*,*) 'Error opening file (2)', trim(fname),' fstat: ',fstat
if (present(status)) then
status = fstat
return
else
stop
end if
end if
!! append the new data
write(FILEUNIT, iostat=fstat) x
if (fstat /= 0) then
write(*,*) 'Error writing new data to ', trim(fname),' fstat: ',fstat
if (present(status)) then
status = fstat
return
else
stop
end if
end if
!! goto bof
rewind(FILEUNIT)
!! write new header
xtype%mrows = size(x,1)
xtype%ncols = xtype%ncols + size(x,2)
write(FILEUNIT, iostat=fstat) xtype
if (fstat /= 0) then
write(*,*) 'Error writing new header ', trim(fname),' fstat: ',fstat
if (present(status)) then
status = fstat
return
else
stop
end if
end if
!! close file
close(FILEUNIT)
end subroutine addtomat4_mat
!!!
!!! read the first matrix form V4 mat file
!!!
subroutine readmat4_mat(fname,x,status)
implicit none
character(len=*), intent(in) :: fname
real(kind=8), pointer :: x(:,:)
integer, intent(out), optional :: status
integer :: fstat
type(Fmatrix) :: xtype
#if defined(__GFORTRAN__) || defined(AIX)
open(unit=FILEUNIT, file=fname, status='old', &
form='unformatted',access='stream',iostat=fstat)
#else
open(unit=FILEUNIT, file=fname, status='old', &
form='binary', position='rewind', iostat=fstat)
#endif
if (fstat /= 0) then
write(*,*) 'Error opening file ', trim(fname),' fstat: ',fstat
if (present(status)) then
status = fstat
else
stop
end if
return
end if
read(FILEUNIT) xtype
if (xtype%imagf.ne.0.or.xtype%imagf.ne.0) stop 'Not a mat file?'
#if defined(__IFC) || defined(__INTEL_COMPILER)
fstat = fseek(lunit=FILEUNIT,from=1,offset=xtype%namelen)
#elif defined(__PGI)
fstat = fseek(lu=FILEUNIT,from=1,offset=xtype%namelen)
#else
call fseek(unit=FILEUNIT,whence=1,offset=xtype%namelen)
#endif
allocate(x(xtype%mrows,xtype%ncols))
read(FILEUNIT,iostat=fstat) x
if (fstat /= 0) then
write(*,*) 'Error reading the file ', trim(fname),' fstat: ',fstat
if (present(status)) then
status = fstat
else
stop
end if
return
end if
close(FILEUNIT)
if (present(status)) status=0
end subroutine readmat4_mat
subroutine readmat4_vec(fname,x,status)
implicit none
character(len=*), intent(in) :: fname
real(kind=8), pointer :: x(:)
integer, intent(out), optional :: status
integer :: fstat
type(Fmatrix) :: xtype
#if defined(__GFORTRAN__) || defined(AIX)
open(unit=FILEUNIT, file=fname, status='old', &
form='unformatted',access='stream',iostat=fstat)
#else
open(unit=FILEUNIT, file=fname, status='old', &
form='binary', position='rewind', iostat=fstat)
#endif
if (fstat /= 0) then
write(*,*) 'Error opening file ', trim(fname),' fstat: ',fstat
if (present(status)) then
status = fstat
else
stop
end if
return
end if
read(FILEUNIT) xtype
if (xtype%imagf.ne.0.or.xtype%imagf.ne.0) stop 'Not a mat file?'
#if defined(__IFC) || defined(__INTEL_COMPILER)
fstat = fseek(lunit=FILEUNIT,from=1,offset=xtype%namelen)
#elif defined(__PGI)
fstat = fseek(lu=FILEUNIT,from=1,offset=xtype%namelen)
#else
call fseek(unit=FILEUNIT,whence=1,offset=xtype%namelen)
#endif
allocate(x(xtype%mrows*xtype%ncols))
read(FILEUNIT,iostat=fstat) x
if (fstat /= 0) then
write(*,*) 'Error reading the file ', trim(fname),' fstat: ',fstat
if (present(status)) then
status = fstat
else
stop
end if
return
end if
close(FILEUNIT)
if (present(status)) status=0
end subroutine readmat4_vec
end module matfiles