-
Notifications
You must be signed in to change notification settings - Fork 5
/
aedes_readvnmr.m
1410 lines (1275 loc) · 44.7 KB
/
aedes_readvnmr.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function [DATA,msg_out] = aedes_readvnmr(filename,varargin)
% AEDES_READVNMR - Read VNMR (Varian) FID-files
%
%
% Synopsis:
% [DATA,msg]=aedes_readvnmr(filename,'PropertyName1',value1,'PropertyName2',value2,...)
% [DATA,msg]=aedes_readvnmr(filename,'header')
% [DATA,msg]=aedes_readvnmr(filename,output_filename)
%
% Description:
% The function reads VNMR (Varian) FID-file and return a structure
% DATA with fields DataFormat, HDR, FTDATA, KSPACE, PROCPAR, and
% PHASETABLE. The fields of the DATA structure are constructed as
% follows:
%
% DATA
% |-> DataFormat (string identifier for data format 'vnmr')
% |-> HDR
% |-> FileHeader (data file header)
% |-> BlockHeaders (data block headers, not stored by default)
% |-> fname (file name)
% |-> fpath (file path)
% |-> Param (parameter values used by AEDES_READFID to read the data)
% |-> FTDATA (real fourier transformed image data)
% |-> KSPACE (complex k-space, empty by default)
% |-> PROCPAR (parameters from procpar file)
% |-> PHASETABLE (phasetable)
%
% The DATA structure is returned as empty (without HDR, FTDATA,
% KSPACE, and PROCPAR fields) if an error has occurred during
% reading. The error message is returned in the second output
% argument msg. If AEDES_READVNMR is called with only one output argument
% (i.e. without MSG), the error message is echoed to the workspace.
%
% The first input argument is either a string containing full path to
% the FID-file or the header structure HDR. Only the data file header
% can be read by giving a string 'header' as the second input
% argument.
%
% By default the k-space is not returned, i.e. the field KSPACE is
% empty. The returned data can be adjusted by using the 'return'
% property and values 1, 2, or 3 (see below for more information).
%
% The supported property-value pairs in AEDES_READVNMR (property strings
% are not case sensitive):
%
% Property: Value: Description:
% ********* ****** ************
% 'Return' : [ {1} | 2 | 3 | 4 ] % 1=return only ftdata (default)
% % 2=return only k-space
% % 3=return both ftdata & kspace
% % 4=return raw kspace
%
% 'FastRead' : [{'off'} | 'on' ] % Enables an experimental
% % method for very fast reading
% % of fid-files. This is not as
% % robust as the older
% % method and can consume a lot
% % of memory.
%
% 'SumOfSquares' : [ {1} | 2 | 3 ] % 1=Return only the
% % sum-of-squares image
% % for multireceiver
% % data (default).
% % 2=Return both SoQ and
% % individual receiver data
% % 3=Return only individual
% % receiver data
% % NOTE: This property has
% % no effect on single
% % receiver data.
%
% 'FileChunkSize' : [ megabytes ] % Chunk size in megabytes for
% % reading fid-files
% % (default=500 MB).
% % Lowering the chunk size
% % helps to conserve memory
% % when reading large files,
% % but is slower. This
% % property has no effect if
% % FastRead='off'.
%
%
% 'OutputFile' : filename % Writes the slices into
% % NIfTI-format files
% % using the given filename.
%
% 'DCcorrection': [ {'off'} | 'on' ] % 'on'=use DC correction
% % 'off'=don't use DC correction
% % (default)
%
% 'Procpar' : (procpar-structure) % Input procpar
% % structure. If this
% % property is not set the
% % procpar structure
% % will be read using
% % AEDES_READPROCPAR
%
% 'SortPSS' : [ 'off' | {'on'} ] % Sort slices in 2D stacks
% % using pss. Turn this 'off'
% % if interleaved slice
% % order is to be preserved.
% % The original pss is saved
% % in procpar.pss_orig
%
% 'ZeroPadding' : ['off'|'on'|{'auto'}] % 'off' = force
% % zeropadding off
% % 'on' = force
% % zeropadding on (force
% % images to be square)
% % 'auto' = autoselect
% % using relative FOV
% % dimensions (default)
%
% 'PhaseTable' : (custom phase table) % User-specified
% % line order for
% % k-space. The phase table
% % is obtained from the file
% % specified in
% % procpar.petable if
% % necessary.
%
% 'sorting' : [ 'off' | {'on'} ] % 'off' =disable k-space
% % sorting, i.e. ignore the
% % phase table and/or arrays.
% % 'on' =sort k-space using
% % phase table and/or array
% % information if necessary
% % (default)
%
% 'wbar' : [ {'on'} | 'off' ] % 'on' = show waitbar
% % 'off' = don't show waitbar
%
% 'Precision' : [{'single'}|'double'] % Precision of the
% % outputted data.
% % 'single' = 32-bit float
% % 'double' = 64-bit float
%
% 'OrientImages': [ {'on'} | 'off' ] % Orient FTDATA after
% % reading the data using
% % PROCPAR.orient property.
%
% 'RemoveEPIphaseIm' : [{'on'}|'off'] % Remove the phase image
% % from EPI data. This
% % option only affects EPI
% % images.
%
% 'EPIBlockSize' : [integer value] % Block size (number of
% % volumes) used for
% % processing multireceiver
% % EPI data. Default=300
%
% 'EPIPhasedArrayData' : ['on'|{'off'}] % Return data from
% % individual receivers from
% % phased array EPI files.
%
% 'EPI_PC' : [{'auto'}|'pointwise'| % Phase correction for EPI.
% 'triple'|'off'] % 'auto' = Choose correction
% % based on procpar.
% % 'pointwise' = Pointwise
% % single reference.
% % 'triple' = Use triple
% % reference correction
% % 'off' = Do not perform
% % phase correction.
%
%
% Examples:
% [DATA,msg]=aedes_readvnmr(filename) % Read image data from 'filename'
% [DATA,msg]=aedes_readvnmr(filename,'header') % Read only data file header
%
% [DATA,msg]=aedes_readvnmr(filename,'return',1) % Return only image data (default)
% [DATA,msg]=aedes_readvnmr(filename,'return',2) % Return only k-space
% [DATA,msg]=aedes_readvnmr(filename,'return',3) % Return both image data and k-space
%
% % Read first data file header and then image data and k-space
% [DATA,msg]=aedes_readvnmr(filename,'header')
% [DATA,msg]=aedes_readvnmr(DATA.HDR,'return',3)
%
% % Read VNMR-data with default options and save slices in NIfTI
% % format
% DATA=aedes_readvnmr('example.fid','saved_slices.nii');
%
% % If you want to use non-default options and also write
% % NIfTI-files, use the property/value formalism, for example
% DATA=aedes_readvnmr('example.fid','ZeroPadding','off',...
% 'OutputFile','saved_slices.nii');
%
% See also:
% AEDES_READFIDPREFS, AEDES_READPROCPAR, AEDES_READPHASETABLE,
% AEDES_DATA_READ, AEDES_WRITE_NIFTI, AEDES
% This function is a part of Aedes - A graphical tool for analyzing
% medical images
%
% Copyright (C) 2010 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uef.fi>
%
% Department of Physics, Department of Neurobiology
% University of Kuopio, FINLAND
%
% This program may be used under the terms of the GNU General Public
% License version 2.0 as published by the Free Software Foundation
% and appearing in the file LICENSE.TXT included in the packaging of
% this program.
%
% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
if nargout==2
Dat.ShowErrors = false;
msg_out='';
else
Dat.ShowErrors = true;
end
%% ---------------------
% Defaults
Dat.ReturnKSpace = false;
Dat.ReturnFTData = true;
Dat.ReturnRawKspace = false;
Dat.DCcorrection = false;
Dat.ZeroPadding = 2;
Dat.Sorting = true;
Dat.UsePhaseTable = true;
Dat.FastDataRead = true;
Dat.precision = 'single';
Dat.FileChunkSize = 500; % Chunk size (in MB) for FastRead
%% Other defaults
Dat.ShowWaitbar = true;
procpar=[];
Dat.phasetable=[];
Dat.OutputFile = false;
Dat.ReorientEPI = false;
Dat.RemoveEPIphaseIm = true;
Dat.EPIBlockSize = 300;
Dat.EPIPhasedArrayData = false;
Dat.EPI_PC = 'auto';
Dat.OrientImages = true;
Dat.SumOfSquares = 1;
Dat.UseCustomRecon = true;
Dat.SortPSS = true;
% Default custom reconstruction file directory
tmp = which('aedes');
if ~isempty(tmp)
tmp_path=fileparts(tmp);
recon_dir = [tmp_path,filesep,'vnmr_recon',filesep];
else
% If all else fails, look in the current directory
recon_dir = [pwd,filesep,'vnmr_recon',filesep];
end
%% Set data format label
DATA.DataFormat = 'vnmr';
%% Parse input arguments
if nargin==0 || isempty(filename)
%% Get default directory
try
defaultdir = getpref('Aedes','GetDataFileDir');
catch
defaultdir = [pwd,filesep];
end
%% If no input arguments are given, ask for a file
[f_name, f_path, f_index] = uigetfile( ...
{'fid;FID','Varian FID-files (fid)'; ...
'*.*', 'All Files (*.*)'}, ...
'Select VNMR (Varian) FID file',defaultdir);
if ( all(f_name==0) || all(f_path==0) ) % Cancel is pressed
DATA=[];
msg_out='Canceled';
return
end
ReadHdr = true;
ReadData = true;
filename=[f_path,f_name];
%% Set default directory to preferences
setpref('Aedes','GetDataFileDir',f_path)
end
if nargin==1
if isstruct(filename)
hdr = filename;
filename = [hdr.fpath,hdr.fname];
ReadHdr = false;
ReadData = true;
elseif ischar(filename)
ReadHdr = true;
ReadData = true;
end
elseif nargin==2
if strcmpi(varargin{1},'header')
ReadHdr = true;
ReadData = false;
elseif ischar(varargin{1})
ReadHdr = true;
ReadData = true;
Dat.OutputFile = varargin{1};
else
if ~Dat.ShowErrors
DATA=[];
msg_out=sprintf('Invalid second input argument "%s".',varargin{1});
return
else
error('Invalid second input argument "%s".',varargin{1})
end
end
else
if isstruct(filename)
hdr = filename;
filename = [hdr.fpath,hdr.fname];
ReadHdr = false;
ReadData = true;
elseif isempty(filename)
[f_name, f_path, f_index] = uigetfile( ...
{'fid;FID','Varian FID-files (fid)'; ...
'*.*', 'All Files (*.*)'}, ...
'Select VNMR (Varian) FID file');
if ( all(f_name==0) || all(f_path==0) ) % Cancel is pressed
DATA=[];
msg_out='Canceled';
return
end
ReadHdr = true;
ReadData = true;
filename=[f_path,f_name];
else
ReadHdr = true;
ReadData = true;
end
for ii=1:2:length(varargin)
switch lower(varargin{ii})
case 'return'
if length(varargin)<2
DATA=[];
if ~Dat.ShowErrors
msg_out='"Return" value not specified!';
else
error('"Return" value not specified!')
end
return
else
if varargin{ii+1}==1
Dat.ReturnKSpace = false;
Dat.ReturnFTData = true;
elseif varargin{ii+1}==2
Dat.ReturnKSpace = true;
Dat.ReturnFTData = false;
elseif varargin{ii+1}==3
Dat.ReturnKSpace = true;
Dat.ReturnFTData = true;
elseif varargin{ii+1}==4
Dat.ReturnRawKspace = true;
Dat.ReturnKSpace = true;
Dat.ReturnFTData = false;
end
end
case {'dc','dccorrection','dccorr'}
if strcmpi(varargin{ii+1},'on')
Dat.DCcorrection = true;
else
Dat.DCcorrection = false;
end
case 'procpar'
procpar=varargin{ii+1};
case 'zeropadding'
if ischar(varargin{ii+1})
if strcmpi(varargin{ii+1},'on')
Dat.ZeroPadding = 1; % on
elseif strcmpi(varargin{ii+1},'off')
Dat.ZeroPadding = 0; % off
else
Dat.ZeroPadding = 2; % auto
end
else
% Undocumented
Dat.ZeroPadding = 3; % Custom
Dat.CustomZeroPadding = varargin{ii+1};
end
case {'sumofsquares','soq'}
Dat.SumOfSquares = varargin{ii+1};
case 'phasetable'
Dat.phasetable = varargin{ii+1};
case 'sorting'
if strcmpi(varargin{ii+1},'on')
Dat.UsePhaseTable = true;
Dat.Sorting = true;
else
Dat.UsePhaseTable = false;
Dat.Sorting = false;
end
case 'fastread'
if strcmpi(varargin{ii+1},'on')
Dat.FastDataRead = true;
else
Dat.FastDataRead = false;
end
case 'filechunksize'
Dat.FileChunkSize = round(varargin{ii+1});
case 'wbar'
if strcmpi(varargin{ii+1},'on')
Dat.ShowWaitbar = 1;
else
Dat.ShowWaitbar = 0;
end
case 'precision'
if strcmpi(varargin{ii+1},'single')
Dat.precision = 'single';
elseif strcmpi(varargin{ii+1},'double')
Dat.precision = 'double';
else
warning('Unsupported precision "%s". Using single...',varargin{ii+1});
Dat.precision = 'single';
end
case 'outputfile'
Dat.OutputFile = varargin{ii+1};
case 'reorientepi'
if strcmpi(varargin{ii+1},'on')
Dat.ReorientEPI = true;
end
case 'removeepiphaseim'
if strcmpi(varargin{ii+1},'on')
Dat.RemoveEPIphaseIm = true;
end
case 'epiblocksize'
Dat.EPIBlockSize = round(varargin{ii+1});
case 'epiphasedarraydata'
if strcmpi(varargin{ii+1},'on')
Dat.EPIPhasedArrayData = true;
else
Dat.EPIPhasedArrayData = false;
end
case 'epi_pc'
if any(strcmpi(varargin{ii+1},...
{'off','auto','triple','pointwise'}))
Dat.EPI_PC = varargin{ii+1};
else
if ~Dat.ShowErrors
msg_out=sprintf('Unknown EPI phase correction type "%s".',varargin{ii+1});
else
error('Unknown EPI phase correction type "%s".',varargin{ii+1})
end
return
end
case 'orientimages'
if strcmpi(varargin{ii+1},'off')
Dat.OrientImages = false;
end
case 'sortpss'
if strcmpi(varargin{ii+1},'off')
Dat.SortPSS = false;
end
otherwise
DATA=[];
if ~Dat.ShowErrors
msg_out = ['Unknown property "' varargin{ii} '"'];
else
error(['Unknown property "' varargin{ii} '"'])
end
return
end
end
end
% Parse filename
[fpath,fname,fext]=fileparts(filename);
if ~strcmpi(fname,'fid')
if isempty(fname)
fpath = [fpath,filesep];
else
if isempty(fpath)
fpath = [pwd,filesep,fname,fext,filesep];
else
fpath = [fpath,filesep,fname,fext,filesep];
end
end
fname = 'fid';
else
fpath = [fpath,filesep];
end
%% Read procpar if not given as input argument
if isempty(procpar) || nargin~=2
[procpar,proc_msg]=aedes_readprocpar([fpath,'procpar']);
if isempty(procpar)
DATA=[];
if ~Dat.ShowErrors
msg_out=proc_msg;
else
error(proc_msg)
end
return
end
end
% Don't use DC correction if number of transients is greater that 1
if procpar.nt>1
Dat.DCcorrection = false;
end
%% Read phasetable if necessary
if Dat.Sorting
% Look in preferences for tablib-directory
if isfield(procpar,'petable') && ~strcmpi(procpar.petable{1},'n')
try
tabpath = getpref('Aedes','TabLibDir');
catch
% If the table path was not found in preferences, try to use aedes
% directory
tmp_path = which('aedes');
if ~isempty(tmp_path)
aedes_path=fileparts(tmp_path);
tabpath = [aedes_path,filesep,'tablib',filesep];
else
% If all else fails, look in the current directory
tabpath = [pwd,filesep,'tablib',filesep];
end
end
[phasetable,msg] = aedes_readphasetable([tabpath,procpar.petable{1}]);
else
phasetable = [];
end
% If petable cannot be found, check if it is in procpar...
if isempty(phasetable) && isfield(procpar,'pe_table')
phasetable = {'t1',procpar.pe_table(:)};
elseif isempty(phasetable) && isfield(procpar,'pelist') && ...
~isempty(procpar.pelist) && isnumeric(procpar.pelist) && ...
length(procpar.pelist)>1
phasetable = {'t1',reshape(procpar.pelist,procpar.etl,[]).'};
end
if ~isempty(phasetable)
Dat.phasetable = phasetable{1,2};
else
Dat.phasetable = [];
end
%% Abort and throw error if phasetable cannot be read and the FID-file
% has not been sorted
% if isempty(phasetable) && ~isfield(procpar,'flash_converted')
% DATA=[];
% if ~Dat.ShowErrors
% msg_out={['Could not find the required phase table "' ...
% procpar.petable{1} '" in the following folders'],...
% ['"' tabpath '".']};
% else
% error('Could not find the required phase table "%s" in %s',...
% procpar.petable{1},tabpath)
% end
% return
% elseif ( isempty(phasetable) && isfield(procpar,'flash_converted') ) || ...
% ~Dat.UsePhaseTable
% %% If the FID-file has been sorted, the reading can continue but
% % throw a warning.
% fprintf(1,'Warning: aedes_readfid: Could not find phasetable "%s" in "%s"!\n',procpar.petable{1},tabpath)
% Dat.phasetable=[];
% else
% Dat.phasetable = phasetable{1,2};
% end
end
% Convert phasetable indices to MATLAB indices
if ~isempty(Dat.phasetable)
Dat.phasetable=Dat.phasetable+(-min(min(Dat.phasetable))+1);
else
Dat.UsePhaseTable = false;
end
%% Open FID-file
[file_fid,msg]=fopen([fpath,fname],'r','ieee-be');
if file_fid < 0
DATA=[];
if ~Dat.ShowErrors
msg_out=['Could not open file "' filename '" for reading.'];
else
error('Could not open file "%s" for reading.',filename)
end
return
end
% Read only header
if ReadHdr && ~ReadData
[hdr,msg_out]=l_ReadDataFileHeader(file_fid);
if ~isempty(msg_out)
DATA=[];
fclose(file_fid);
if Dat.ShowErrors
error(msg_out)
end
return
else
DATA.HDR.FileHeader = hdr.FileHeader;
DATA.FTDATA=[];
DATA.KSPACE=[];
DATA.PROCPAR=[];
DATA.PHASETABLE=[];
end
elseif ~ReadHdr && ReadData % Header structure given as input argument
% Read only data.
[hdr,data,kspace,msg_out]=l_ReadBlockData(file_fid,hdr,Dat,procpar);
if ~isempty(msg_out)
DATA=[];
fclose(file_fid);
if Dat.ShowErrors
error(msg_out)
end
return
else
DATA.HDR.FileHeader=hdr.FileHeader;
DATA.HDR.BlockHeaders = hdr.BlockHeaders;
DATA.FTDATA=data;
DATA.KSPACE=kspace;
DATA.PROCPAR=procpar;
DATA.PHASETABLE=Dat.phasetable;
end
elseif ReadHdr && ReadData % Read headers and data
[hdr,msg_out]=l_ReadDataFileHeader(file_fid);
[hdr,kspace,msg_out]=l_ReadBlockData(file_fid,hdr,Dat);
if ~isempty(msg_out)
DATA=[];
fclose(file_fid);
if Dat.ShowErrors
error(msg_out)
end
return
else
DATA.HDR.FileHeader=hdr.FileHeader;
DATA.HDR.BlockHeaders = hdr.BlockHeaders;
DATA.FTDATA=[];
DATA.KSPACE=[];
DATA.PROCPAR=procpar;
DATA.PHASETABLE=Dat.phasetable;
end
end
% Close file
fclose(file_fid);
% Set file name and path to the HDR structure
DATA.HDR.fname=fname;
DATA.HDR.fpath=fpath;
% Set parameter values
if ~Dat.DCcorrection
DATA.HDR.Param.DCcorrection = 'off';
else
DATA.HDR.Param.DCcorrection = 'on';
end
% Reconstruct kspace =======================================
if Dat.ReturnRawKspace
% If asked to return raw unsorted kspace, return immediately
DATA.KSPACE=kspace;
return
else
% Check if the sequence used has a custom reconstruct code
recon_func=l_GetReconFunc(recon_dir);
recon_func_ind = 0;
if ~isempty(recon_func)
% Get sequence name
seqfil = procpar.seqfil;
% Check if any of the custom reconstruction files would
% like to do the reconstruction...
for ii=1:length(recon_func)
try
list=recon_func{ii}();
catch
warning('Error in custom reconsruction function "%s", skipping...',func2str(recon_func{ii}));
continue
end
if any(strcmp(seqfil,list))
recon_func_ind = ii;
break
end
end
end
if recon_func_ind==0
Dat.UseCustomRecon = false;
end
if Dat.UseCustomRecon
if Dat.ShowWaitbar
wbh=aedes_calc_wait(sprintf('%s\n%s',...
['Using custom function ',upper(func2str(recon_func{recon_func_ind}))],...
['to reconstruct sequence ',procpar.seqfil{1}]));
drawnow
end
[kspace,data,msg_out]=recon_func{recon_func_ind}(kspace,Dat,procpar);
if Dat.ShowWaitbar
close(wbh)
end
if isempty(data) && Dat.ReturnFTData
% Fourier transform data if not done in custom reconstruction code
[data,msg_out]=l_CalculateFFT(kspace,Dat,procpar);
end
else
% Use the default reconstruction code
[kspace,msg_out,procpar]=l_ReconstructKspace(kspace,Dat,procpar);
DATA.PROCPAR = procpar;
% Fourier transform data
if Dat.ReturnFTData
[data,msg_out]=l_CalculateFFT(kspace,Dat,procpar);
end
end
end
if Dat.ReturnKSpace
DATA.KSPACE = kspace;
else
DATA.KSPACE = [];
end
if Dat.ReturnFTData
DATA.FTDATA = data;
else
DATA.FTDATA = [];
end
clear kspace data
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read Data File (Main) Header
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [hdr,msg_out]=l_ReadDataFileHeader(file_fid)
msg_out='';
%% Read Data File Header
try
FH.nblocks = fread(file_fid,1,'long'); % Number of blocks in file
FH.ntraces = fread(file_fid,1,'long'); % Number of traces per block
FH.np = fread(file_fid,1,'long'); % Number of elements per trace
FH.ebytes = fread(file_fid,1,'long'); % Number of bytes per element
FH.tbytes = fread(file_fid,1,'long'); % Number of bytes per trace
FH.bbytes = fread(file_fid,1,'long'); % Number of bytes per block
FH.vers_id = fread(file_fid,1,'short'); % Software version, file_id status bits
FH.status = fread(file_fid,1,'short'); % Status of whole file
FH.nbheaders = fread(file_fid,1,'long'); % Number of block headers per block
hdr.FileHeader = FH;
%% Parse status bits
hdr.FileHeader.status=[];
tmp_str = {'S_DATA',... % 0 = no data, 1 = data
'S_SPEC',... % 0 = FID, 1 = spectrum
'S_32',... % 0 = 16 bit, 1 = 32 bit integer
'S_FLOAT',... % 0 = integer, 1 = floating point
'S_COMPLEX',... % 0 = real, 1 = complex
'S_HYPERCOMPLEX',... % 1 = hypercomplex
'',... % Unused bit
'S_ACQPAR',... % 0 = not Acqpar, 1 = Acqpar
'S_SECND',... % 0 = first FT, 1 = second FT
'S_TRANSF',... % 0 = regular, 1 = transposed
'',... % Unused bit
'S_NP',... % 1 = np dimension is active
'S_NF',... % 1 = nf dimension is active
'S_NI',... % 1 = ni dimension is active
'S_NI2',... % 1 = ni2 dimension is active
''... % Unused bit
};
status_bits = fliplr(double(dec2bin(FH.status,16))==49);
for ii=1:length(tmp_str)
if ~isempty(tmp_str{ii})
hdr.FileHeader.status.(tmp_str{ii}) = status_bits(ii);
end
end
catch
hdr=[];
msg_out=['Error occurred while reading file header from "' filename '".'];
return
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read Block Headers and Data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [hdr,kspace,msg_out]=l_ReadBlockData(file_fid,hdr,Dat)
hdr.BlockHeaders = [];
%hdr.HypercmplxBHeaders = [];
kspace=[];
count = [];
msg_out='';
BlockHeadStatusLabels = {...
'S_DATA',... % 0 = no data, 1 = data
'S_SPEC',... % 0 = FID, 1 = spectrum
'S_32',... % 0 = 16 bit, 1 = 32 bit integer
'S_FLOAT',... % 0 = integer, 1 = floating point
'S_COMPLEX',... % 0 = real, 1 = complex
'S_HYPERCOMPLEX',... % 1 = hypercomplex
'',... % Unused bit
'MORE_BLOCKS',... % 0 = absent, 1 = present
'NP_CMPLX',... % 0 = real, 1 = complex
'NF_CMPLX',... % 0 = real, 1 = complex
'NI_CMPLX',... % 0 = real, 1 = complex
'NI2_CMPLX',... % 0 = real, 1 = complex
'',... % Unused bit
'',... % Unused bit
'',... % Unuesd bit
''... % Unused bit
};
BlockHeadModeLabels = {...
'NP_PHMODE',... % 1 = ph mode
'NP_AVMODE',... % 1 = av mode
'NP_PWRMODE',... % 1 = pwr mode
'',... % Unused bit
'NF_PHMODE',... % 1 = ph mode
'NF_AVMODE',... % 1 = av mode
'NF_PWRMODE',... % 1 = pwr mode
'',... % Unused bit
'NI_PHMODE',... % 1 = ph mode
'NI_AVMODE',... % 1 = av mode
'NI_PWRMODE',... % 1 = pwr mode
'',... % Unused bit
'NI2_PHMODE',... % 1 = ph mode
'NI2_AVMODE',... % 1 = av mode
'NI2_PWRMODE',... % 1 = pwr mode
''... % Unused bit
};
% The nbheaders -field is not correct in some cases. Thus, this field
% cannot be trusted and the real nbheaders has to be calculated from the
% byte counts.
tmp_bytes=hdr.FileHeader.bbytes-hdr.FileHeader.tbytes*hdr.FileHeader.ntraces;
header_bytes=28;
if rem(tmp_bytes,header_bytes)~=0
msg_out = 'Block header byte count does not match with file header';
return
else
nbheaders = tmp_bytes/28;
end
% Allocate space for kspace
% kspace = complex(zeros(hdr.FileHeader.np/2,...
% hdr.FileHeader.ntraces*hdr.FileHeader.nblocks,...
% Dat.precision));
kspace = complex(zeros(hdr.FileHeader.np/2,...
hdr.FileHeader.ntraces*hdr.FileHeader.nblocks,Dat.precision));
%% - The older robust (but also slower) way for reading the data.
%% When the blocksize is relatively small, this is also quite fast.
if ~Dat.FastDataRead
% Initialize waitbar
if Dat.ShowWaitbar
wb_h = aedes_wbar(0/hdr.FileHeader.nblocks,...
{['Reading VNMR data.'],...
['(Processing data block ' ...
num2str(0) '/' num2str(hdr.FileHeader.nblocks) ')']});
end
%% Read data and block headers
for ii=1:hdr.FileHeader.nblocks
%% Update waitbar
if Dat.ShowWaitbar
aedes_wbar(ii/hdr.FileHeader.nblocks,...
wb_h,...
{['Reading VNMR data.'],...
['(Processing data block ' ...
num2str(ii) '/' num2str(hdr.FileHeader.nblocks) ')']})
end
%% Read block header and hypercomplex header
for kk=1:nbheaders
%% Read block header
if kk==1
hdr.BlockHeaders.scale = fread(file_fid,1,'short'); % Scaling factor
tmp_status = fread(file_fid,1,'short'); % Status of data in block
hdr.BlockHeaders.status = [];
hdr.BlockHeaders.index = fread(file_fid,1,'short'); % Block index
tmp_mode = fread(file_fid,1,'short'); % Mode of data in block
hdr.BlockHeaders.mode = [];
hdr.BlockHeaders.ctcount = fread(file_fid,1,'long'); % ct value for FID
hdr.BlockHeaders.lpval = fread(file_fid,1,'float'); % f2 (2D-f1) left phase in phasefile
hdr.BlockHeaders.rpval = fread(file_fid,1,'float'); % f2 (2D-f1) right phase in phasefile
hdr.BlockHeaders.lvl = fread(file_fid,1,'float'); % level drift correction
hdr.BlockHeaders.tlt = fread(file_fid,1,'float'); % tilt drift correction
%% Parse status and mode bits
status_bits = fliplr(double(dec2bin(tmp_status,16))==49);
mode_bits = fliplr(double(dec2bin(tmp_mode,16))==49);
for tt=1:length(BlockHeadStatusLabels)
if ~isempty(BlockHeadStatusLabels{tt})
hdr.BlockHeaders.status.(BlockHeadStatusLabels{tt}) = status_bits(tt);
end
if ~isempty(BlockHeadModeLabels{tt})
hdr.BlockHeaders.mode.(BlockHeadModeLabels{tt}) = mode_bits(tt);
end
end
else %% Read hypercomplex header
fread(file_fid,1,'short'); % Spare
hdr.BlockHeaders.HCHstatus = fread(file_fid,1,'short');
fread(file_fid,1,'short'); % Spare
fread(file_fid,1,'short'); % Spare
fread(file_fid,1,'long'); % Spare
hdr.BlockHeaders.HCHlpval1 = fread(file_fid,1,'float');
hdr.BlockHeaders.HCHrpval1 = fread(file_fid,1,'float');
fread(file_fid,1,'float'); % Spare
fread(file_fid,1,'float'); % Spare
end
end
%% Check block index to be sure about the data type
if hdr.BlockHeaders.index~=ii
fprintf(1,'Warning: Index mismatch in "%s" block %d\n',fopen(file_fid),ii);
% Use information from the file header instead of the BlockHeader if
% there is a mismatch in blockheader index...
useFileHeader = true;
else
useFileHeader = false;
end
%% Determine data precision
if useFileHeader
if hdr.FileHeader.status.S_FLOAT==1
prec_str = ['single=>',Dat.precision];
elseif hdr.FileHeader.status.S_32==1 ...
&& hdr.FileHeader.status.S_FLOAT==0
prec_str = ['int32=>',Dat.precision];
elseif hdr.FileHeader.status.S_32==0 ...
&& hdr.FileHeader.status.S_FLOAT==0
prec_str = ['int16=>',Dat.precision];
end
else
if hdr.BlockHeaders.status.S_FLOAT==1
prec_str = ['single=>',Dat.precision];
elseif hdr.BlockHeaders.status.S_32==1 ...
&& hdr.BlockHeaders.status.S_FLOAT==0
prec_str = ['int32=>',Dat.precision];
elseif hdr.BlockHeaders.status.S_32==0 ...
&& hdr.BlockHeaders.status.S_FLOAT==0
prec_str = ['int16=>',Dat.precision];
end
end
% Read k-space
tmp=fread(file_fid,...
[hdr.FileHeader.np,hdr.FileHeader.ntraces],...
prec_str);
% Store complex block and perform DC correction
if ~Dat.DCcorrection
complex_block = complex(tmp(1:2:end,:),tmp(2:2:end,:));
else
complex_block = complex(tmp(1:2:end,:)-hdr.BlockHeaders.lvl,...
tmp(2:2:end,:)-hdr.BlockHeaders.tlt);
end
inds = ((ii-1)*hdr.FileHeader.ntraces+1):(ii*hdr.FileHeader.ntraces);
kspace(:,inds) = complex_block;
% Do not save blockheaders by default. When reading data files with a lot of
% blocks (e.g. over 1000) the processing of the DATA structure can be
% slowed down considerably. If you for some reason want to save also the
% block headers in the DATA structure comment out the code line below.
hdr.BlockHeaders = [];
end % for ii=1:hdr.