forked from MarineBioAcousticsRC/Triton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_write_ltsahead.m
131 lines (117 loc) · 5.6 KB
/
batch_write_ltsahead.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
function write_ltsahead
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% write_ltsahead.m
%
% setup values for ltsa file and write header + directories for new ltsa
% file
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global PARAMS
% disp('set up ltsa file')
% get file name
% filterSpec1 = '*.ltsa';
% boxTitle1 = 'Save LTSA File';
% user interface retrieve file to open through a dialog box
PARAMS.ltsa.outdir = PARAMS.ltsa.indir;
% PARAMS.ltsa.outfile = 'LTSAout.ltsa';
% DefaultName = fullfile(PARAMS.ltsa.outdir,PARAMS.ltsa.outfile);
% [PARAMS.ltsa.outfile,PARAMS.ltsa.outdir]=uiputfile(filterSpec1,boxTitle1,DefaultName);
% if the cancel button is pushed, then no file is loaded
% so exit this script
if strcmp(num2str(PARAMS.ltsa.outfile),'0')
PARAMS.ltsa.gen = 0;
disp_msg('Canceled file creation')
return
else
PARAMS.ltsa.gen = 1;
disp('Opened File: ')
disp([PARAMS.ltsa.outdir,PARAMS.ltsa.outfile])
% disp(' ')
cd(PARAMS.ltsa.outdir)
end
% calculate file header values, open file and fill up header
maxNrawfiles = PARAMS.ltsa.nrftot + 100; % maximum number of raw files + a few more
lhsz = 64; % LTSA header size [bytes]
rhsz = 64 + 40; % LTSA rawfile header (directory) size (add 40 bytes for longer (upto 80 char) xwav files names
dirStartLoc = lhsz + 1; % directory start location in bytes
dataStartLoc = rhsz * maxNrawfiles + lhsz; % data start location in bytes
% open output ltsa file
fid = fopen(fullfile(PARAMS.ltsa.outdir,PARAMS.ltsa.outfile),'w');
% LTSA file Header - 64 bytes total
fwrite(fid,'LTSA','char'); % 4 bytes - file ID type
fwrite(fid,PARAMS.ltsa.ver,'uint8'); % 1 byte - version number
fwrite(fid,'xxx','char'); % 3 bytes - spare
fwrite(fid,dirStartLoc,'uint32'); % 4 bytes - directory start location [bytes]
fwrite(fid,dataStartLoc,'uint32'); % 4 bytes - data start location [bytes]
fwrite(fid,PARAMS.ltsa.tave,'float32'); % 4 bytes - time bin average for spectra [seconds]
fwrite(fid,PARAMS.ltsa.dfreq,'float32'); % 4 bytes - frequency bin size [Hz]
fwrite(fid,PARAMS.ltsa.fs,'uint32'); % 4 bytes - sample rate [Hz]
fwrite(fid,PARAMS.ltsa.nfft,'uint32'); % 4 bytes - number of samples per fft
if PARAMS.ltsa.ver == 4
fwrite(fid,PARAMS.ltsa.nrftot,'uint32'); % 4 bytes - total number of raw files from all xwavs
nz = 25;
else
disp_msg(['Error: incorrect version number ',num2str(PARAMS.ltsa.ver)])
return
end
fwrite(fid,PARAMS.ltsa.nxwav,'uint16'); % 2 bytes - total number of xwavs files used
% 36 bytes used, up to here
% add channel ltsa'ed 061011 smw
fwrite(fid,PARAMS.ltsa.ch,'uint8'); % 1 byte - channel number that was ltsa'ed
% pad header for future growth, but backward compatible
fwrite(fid,zeros(nz,1),'uint8'); % 1 bytes x 27 = 27 bytes - 0 padding / spare
% 64 bytes used - up to here
% Directory - one for each raw file - 64 + 40 bytes for each directory listing
for k = 1 : PARAMS.ltsa.nrftot
% write time values to directory
fwrite(fid,PARAMS.ltsahd.year(k) ,'uchar'); % 1 byte - Year
fwrite(fid,PARAMS.ltsahd.month(k) ,'uchar'); % 1 byte - Month
fwrite(fid,PARAMS.ltsahd.day(k) ,'uchar'); % 1 byte - Day
fwrite(fid,PARAMS.ltsahd.hour(k) ,'uchar'); % 1 byte - Hour
fwrite(fid,PARAMS.ltsahd.minute(k) ,'uchar'); % 1 byte - Minute
fwrite(fid,PARAMS.ltsahd.secs(k) ,'uchar'); % 1 byte - Seconds
fwrite(fid,PARAMS.ltsahd.ticks(k) ,'uint16'); % 2 byte - Milliseconds
% 8 bytes up to here
%
% calculate number of spectral averages for this raw file
% number of samples in this raw file = # sectors in rawfile * # samples/sector:
if PARAMS.ltsa.ftype ~= 1 % for HARP and ARP data
Nsamp = (PARAMS.ltsahd.write_length(k) * PARAMS.ltsa.blksz) / PARAMS.ltsa.nch;
else % for wav/Ishmael type data
Nsamp = PARAMS.ltsahd.nsamp(k);
end
%
% number of spectral averages = # samples in rawfile / # samples in fft * compression factor
% needs to be an integer
%
PARAMS.ltsa.nave(k) = ceil(Nsamp/(PARAMS.ltsa.nfft * PARAMS.ltsa.cfact));
%
% calculate byte location in ltsa file for 1st spectral
% average of this raw file
if k == 1
ltsaByteLoc = dataStartLoc;
else
% ltsa data byte loc = previous loc + # spectral ave (of previous loc) * # freqs in each spectra * # of bytes per spectrum level value
ltsaByteLoc = ltsaByteLoc + PARAMS.ltsa.nave(k-1) * PARAMS.ltsa.nfreq * 1;
end
PARAMS.ltsa.byteloc(k) = ltsaByteLoc;
%
% write ltsa parameters:
%
fwrite(fid,PARAMS.ltsa.byteloc(k) ,'uint32'); % 4 byte - Byte location in ltsa file of the spectral averages for this rawfile
fwrite(fid,PARAMS.ltsa.nave(k) ,'uint32'); % 4 byte - number of spectral averages for this raw file
% 16 bytes up to here
fwrite(fid,PARAMS.ltsahd.fname(k,:),'uchar'); % 80 byte - xwav file name for this raw file header
nz = 4;
fwrite(fid,PARAMS.ltsahd.rfileid(k),'uint32'); % 4 byte - raw file id / number for this xwav
fwrite(fid,zeros(nz,1),'uint8'); % 4 bytes
% 64 + 40 bytes for each directory listing for each raw file
end
%
% fill up rest of header with zeros before data start
dndir = maxNrawfiles - PARAMS.ltsa.nrftot; % number of directories not used - to be filled with zeros
dfill = zeros(rhsz * dndir,1);
fwrite(fid,dfill,'uint8');
% close file
fclose(fid);