%MSAC_WRITE - Write out a SAC time series file
%
%  [] = msac_write(filename,sac_trace_structure) ;
%
%  Write a (single) SAC trace structure to a file. See msac_new.m for
%  details of how to create a SAC structure. The value in the field
%  sac_trace_structure.endian is used to determine the byte-order of
%  the resulting file.

%------------------------------------------------------------------------------%
%                                                                              %
%   (c) James Wookey, University of Bristol, 2006                              %
%                                                                              %
%   Version 1.0                                                                %
%                                                                              %
%   This program is free software; you can redistribute it and/or modify       %
%   it under the terms of the GNU General Public License as published by       %
%   the Free Software Foundation; either version 2 of the License, or          %
%   (at your option) any later version.                                        %
%                                                                              %
%   This program is distributed in the hope that it will be useful,            %
%   but WITHOUT ANY WARRANTY; without even the implied warranty of             %
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              %
%   GNU General Public License for more details.                               %
%                                                                              %
%   You should have received a copy of the GNU General Public License          %
%   along with this program; if not, write to:                                 %
%                                                                              %
%   Free Software Foundation, Inc., 59 Temple Place, Suite 330,                %
%   Boston, MA  02111-1307  USA                                                %
%                                                                              %
%   See http://www.gnu.org/licenses/gpl.txt for full GPL terms.                %
%                                                                              %
%------------------------------------------------------------------------------%
% Version history

% v1.0 - Original version
% 2006-06-15

function msac_write(str,sactrace) ;

[fid,message] = fopen(str,'wb',sactrace.endian) ;

if (~isempty(message))
   fprintf('Cannot open file for writing: ''%s''\n',str) ;
   return ;
end

hd(001)  = sactrace.delta    ;
hd(002)  = sactrace.depmin   ;
hd(003)  = sactrace.depmax   ;
hd(004)  = sactrace.scale    ;
hd(005)  = sactrace.odelta   ;
hd(006)  = sactrace.b        ;
hd(007)  = sactrace.e        ;
hd(008)  = sactrace.o        ;
hd(009)  = sactrace.a        ;
hd(010)  = sactrace.internal0;
hd(011)  = sactrace.t0       ;
hd(012)  = sactrace.t1       ;
hd(013)  = sactrace.t2       ;
hd(014)  = sactrace.t3       ;
hd(015)  = sactrace.t4       ;
hd(016)  = sactrace.t5       ;
hd(017)  = sactrace.t6       ;
hd(018)  = sactrace.t7       ;
hd(019)  = sactrace.t8       ;
hd(020)  = sactrace.t9       ;
hd(021)  = sactrace.f        ;
hd(022)  = sactrace.resp0    ;
hd(023)  = sactrace.resp1    ;
hd(024)  = sactrace.resp2    ;
hd(025)  = sactrace.resp3    ;
hd(026)  = sactrace.resp4    ;
hd(027)  = sactrace.resp5    ;
hd(028)  = sactrace.resp6    ;
hd(029)  = sactrace.resp7    ;
hd(030)  = sactrace.resp8    ;
hd(031)  = sactrace.resp9    ;
hd(032)  = sactrace.stla     ;
hd(033)  = sactrace.stlo     ;
hd(034)  = sactrace.stel     ;
hd(035)  = sactrace.stdp     ;
hd(036)  = sactrace.evla     ;
hd(037)  = sactrace.evlo     ;
hd(038)  = sactrace.evel     ;
hd(039)  = sactrace.evdp     ;
hd(040)  = sactrace.mag      ;
hd(041)  = sactrace.user0    ;
hd(042)  = sactrace.user1    ;
hd(043)  = sactrace.user2    ;
hd(044)  = sactrace.user3    ;
hd(045)  = sactrace.user4    ;
hd(046)  = sactrace.user5    ;
hd(047)  = sactrace.user6    ;
hd(048)  = sactrace.user7    ;
hd(049)  = sactrace.user8    ;
hd(050)  = sactrace.user9    ;
hd(051)  = sactrace.dist     ;
hd(052)  = sactrace.az       ;
hd(053)  = sactrace.baz      ;
hd(054)  = sactrace.gcarc    ;
hd(055)  = sactrace.internal1;
hd(056)  = sactrace.internal2;
hd(057)  = sactrace.depmen   ;
hd(058)  = sactrace.cmpaz    ;
hd(059)  = sactrace.cmpinc   ;
hd(060)  = sactrace.xminimum ;
hd(061)  = sactrace.xmaximum ;
hd(062)  = sactrace.yminimum ;
hd(063)  = sactrace.ymaximum ;
hd(064)  = sactrace.unused1  ;
hd(065)  = sactrace.unused2  ;
hd(066)  = sactrace.unused3  ;
hd(067)  = sactrace.unused4  ;
hd(068)  = sactrace.unused5  ;
hd(069)  = sactrace.unused6  ;
hd(070)  = sactrace.unused7  ;

ihd(001) = sactrace.nzyear    ;
ihd(002) = sactrace.nzjday    ;
ihd(003) = sactrace.nzhour    ;
ihd(004) = sactrace.nzmin     ;
ihd(005) = sactrace.nzsec     ;
ihd(006) = sactrace.nzmsec    ;
ihd(007) = sactrace.nvhdr     ;
ihd(008) = sactrace.norid     ;
ihd(009) = sactrace.nevid     ;
ihd(010) = sactrace.npts      ;
ihd(011) = sactrace.internal3 ;
ihd(012) = sactrace.nwfid     ;
ihd(013) = sactrace.nxsize    ;
ihd(014) = sactrace.nysize    ;
ihd(015) = sactrace.unused8   ;
ihd(016) = sactrace.iftype    ;
ihd(017) = sactrace.idep      ;
ihd(018) = sactrace.iztype    ;
ihd(019) = sactrace.unused9   ;
ihd(020) = sactrace.iinst     ;
ihd(021) = sactrace.istreg    ;
ihd(022) = sactrace.ievreg    ;
ihd(023) = sactrace.ievtyp    ;
ihd(024) = sactrace.iqual     ;
ihd(025) = sactrace.isynth    ;
ihd(026) = sactrace.imagtyp   ;
ihd(027) = sactrace.imagsrc   ;
ihd(028) = sactrace.unused10  ;
ihd(029) = sactrace.unused11  ;
ihd(030) = sactrace.unused12  ;
ihd(031) = sactrace.unused13  ;
ihd(032) = sactrace.unused14  ;
ihd(033) = sactrace.unused15  ;
ihd(034) = sactrace.unused16  ;
ihd(035) = sactrace.unused17  ;
ihd(036) = sactrace.leven     ;
ihd(037) = sactrace.lpspol    ;
ihd(038) = sactrace.lovrok    ;
ihd(039) = sactrace.lcalda    ;
ihd(040) = sactrace.unused18  ;


% output the float header
fwrite(fid,hd,'float32') ;

% output the integer header
fwrite(fid,ihd,'int') ;

nullstr8 =  char([0 0 0 0 0 0 0 0]) ;
nullstr16 = char([0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]) ;

% output the character header, padding with nulls where necessary
out=[sactrace.kstnm  nullstr8]; fwrite(fid,out(1:8), 'char') ;
out=[sactrace.kevnm  nullstr16];fwrite(fid,out(1:16),'char') ;
out=[sactrace.khole  nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.ko     nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.ka     nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kt0    nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kt1    nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kt2    nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kt3    nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kt4    nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kt5    nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kt6    nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kt7    nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kt8    nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kt9    nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kf     nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kuser0 nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kuser1 nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kuser2 nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kcmpnm nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.knetwk nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kdatrd nullstr8]; fwrite(fid,out(1:8),'char' ) ;
out=[sactrace.kinst  nullstr8]; fwrite(fid,out(1:8),'char' ) ;

% write the trace
fwrite(fid,sactrace.trace,'float32') ;


% end of MSAC_WRITE.M

