% program to read all data and convert it into mat files

% *************************************************************************
% Input Raw Timestamps and Data format in ASCII file:
% UTC Timestamp format: [yyyy] [mm] [dd] [hh] [minmin] [sec]
%
% Sonics Example:
% Data format: [UTC Timestamp] [u] [v] [w] [temp in celsius] [errorcode]
% 2006 08 31 22 40 28.430385   -0.01  -0.10  -0.02   20.16   0
%
% Sonic Example with voltage:
% Data format: [UTC Timestamp] [u] [v] [w] [temp in celsius] [errorcode] [systemvoltage]
% 2006 08 31 22 40 28.434523   -0.01  -0.10   0.11   20.59   0 11.5
%
% Barometer Example:
% Data format: [UTC Timestamp] [pressure in mBar] [temp in celsius] [error flag] [error text]
% 2006 08 31 22 40 28.646402 1017.726   22.414  0   OK
%
% Temperature Example:
% Data format: [sensor number] [UTC Timestamp] [temp in celsius]
% 1  2006 08 31 22 40 28.938589  0033.31
% 2  2006 08 31 22 40 29.750414  0033.93
% 3  2006 08 31 22 40 30.602444  0025.62
% 4  2006 08 31 22 40 31.438497  0032.25
% 
% Local clock offset Example from GPS:
% Data format of peerstats file: 
% from http://www.ee.udel.edu/~mills/ntp/html/monopt.html:
% Enables recording of peer statistics information. 
% This includes statistics records of all peers of a NTP server and 
% of special signals, where present and configured. 
% Each valid update appends a line of the following form to the current 
% element of a file generation set named peerstats: 
% 48773 10847.650 127.127.4.1 9714 -0.001605376 0.000000000 0.001424877 0.000958674
% The first two fields show the date (Modified Julian Day) and time 
% (seconds and fraction past UTC midnight). 
% The next two fields show the peer address in dotted-quad 
% notation and status, respectively. 
% The status field is encoded in hex in the format described 
% in Appendix B of the NTP specification RFC 1305. 
% The final four fields show the offset, delay, 
% dispersion and RMS jitter, all in seconds.
%
% 53978 13.274 127.127.30.0 96f4 0.000000889 0.000000000 0.000000000 0.000001907
% *************************************************************************

clear

% Arizona Meteor crater east rim low point

% Notes:
% -first data set sonic 3 was 4 hz and wrong (preset) format
% initial_time_stamp_of_files='';
% -all subsequential data has correct format and sonic 3 and 4 with battery
% voltage included


 initial_time_stamp_of_files_list={'20070706231345',...
                                   '20070707000000',...
                                   '20070708000000',...
                                   '20070709000000',...
                                   '20070710000000',...
                                   '20070710202007'}
                               
                         
subdirectory_list={'../raw/',...
                   '../raw/',...
                   '../raw/',...
                   '../raw/',...
                   '../raw/',...
                   '../raw/'}
               
target_directory='../mat/';                   
                   
% 
%            
 for i=1:length(subdirectory_list)
% for i=5:length(subdirectory_list)
%i=6
% for i=length(subdirectory_list)-2:length(subdirectory_list)
     
 initial_time_stamp_of_files=char(initial_time_stamp_of_files_list(i))
 subdirectory=char(subdirectory_list(i))

% *************************************************************************
% Read and convert SONIC 1 data with battery voltage 
% % *************************************************************************
try
tic
    [yyyy,mm,dd,hh,minmin,sec,u,v,w,temp,errorcode,battery] = ...
    textread(strcat(subdirectory,...
                    'SONIC_1_',...
                    initial_time_stamp_of_files,...
                    '.dat'),...
                    '%u %u %u %u %u %f   %f %f %f %f  %2c %f');
    serial_date_number = datenum(yyyy,mm,dd,hh,minmin,sec);
    errorcode=hex2dec(char(errorcode));
    clear('yyyy','mm','dd','hh','minmin','sec');
    mat_filename = strcat(target_directory,...
                      'SONIC_1_u_v_w_temp_',...
                      datestr(serial_date_number(1),30),...
                      '_',...
                      datestr(serial_date_number(end),30),...
                      '.mat');
    save(mat_filename,'serial_date_number','u','v','w','errorcode','temp','battery','-mat');
    clear('mat_filename','serial_date_number','u','v','w','temp','battery');
toc
catch
        'sonic 1 failed'    
        mylasterror = lasterror;
        fprintf(1, mylasterror.message);
end    
% *************************************************************************

% *************************************************************************
% Read and convert SONIC 3 data with battery voltage 
% % *************************************************************************
try
tic
    [yyyy,mm,dd,hh,minmin,sec,u,v,w,temp,errorcode,battery] = ...
    textread(strcat(subdirectory,...
                    'SONIC_3_',...
                    initial_time_stamp_of_files,...
                    '.dat'),...
                    '%u %u %u %u %u %f   %f %f %f %f  %2c %f');
    serial_date_number = datenum(yyyy,mm,dd,hh,minmin,sec);
    errorcode=hex2dec(char(errorcode));
    clear('yyyy','mm','dd','hh','minmin','sec');
    mat_filename = strcat(target_directory,...
                      'SONIC_3_u_v_w_temp_',...
                      datestr(serial_date_number(1),30),...
                      '_',...
                      datestr(serial_date_number(end),30),...
                      '.mat');
    save(mat_filename,'serial_date_number','u','v','w','errorcode','temp','battery','-mat');
    clear('mat_filename','serial_date_number','u','v','w','temp','battery');
toc
catch
            'sonic 3 failed'
            mylasterror = lasterror;
            fprintf(1, mylasterror.message);
end
% *************************************************************************

% *************************************************************************
% Read and convert SONIC 4 data with battery voltage 
% % *************************************************************************
try
tic
    [yyyy,mm,dd,hh,minmin,sec,u,v,w,temp,errorcode,battery] = ...
    textread(strcat(subdirectory,...
                    'SONIC_4_',...
                    initial_time_stamp_of_files,...
                    '.dat'),...
                    '%u %u %u %u %u %f   %f %f %f %f  %2c %f');
    serial_date_number = datenum(yyyy,mm,dd,hh,minmin,sec);
    errorcode=hex2dec(char(errorcode));
    clear('yyyy','mm','dd','hh','minmin','sec');
    mat_filename = strcat(target_directory,...
                      'SONIC_4_u_v_w_temp_',...
                      datestr(serial_date_number(1),30),...
                      '_',...
                      datestr(serial_date_number(end),30),...
                      '.mat');
    save(mat_filename,'serial_date_number','u','v','w','errorcode','temp','battery','-mat');
    clear('mat_filename','serial_date_number','u','v','w','temp','battery');
toc
catch
        'sonic 4 failed'    
        mylasterror = lasterror;
        fprintf(1, mylasterror.message);
end    
% *************************************************************************

% *************************************************************************
% Read and convert SONIC 5 data with battery voltage 
% % *************************************************************************
try
tic
    [yyyy,mm,dd,hh,minmin,sec,u,v,w,temp,errorcode,battery] = ...
    textread(strcat(subdirectory,...
                    'SONIC_5_',...
                    initial_time_stamp_of_files,...
                    '.dat'),...
                    '%u %u %u %u %u %f   %f %f %f %f  %2c %f');
    serial_date_number = datenum(yyyy,mm,dd,hh,minmin,sec);
    errorcode=hex2dec(char(errorcode));
    clear('yyyy','mm','dd','hh','minmin','sec');
    mat_filename = strcat(target_directory,...
                      'SONIC_5_u_v_w_temp_',...
                      datestr(serial_date_number(1),30),...
                      '_',...
                      datestr(serial_date_number(end),30),...
                      '.mat');
    save(mat_filename,'serial_date_number','u','v','w','errorcode','temp','battery','-mat');
    clear('mat_filename','serial_date_number','u','v','w','temp','battery');
toc
catch
            'sonic 5 failed'
            mylasterror = lasterror;
            fprintf(1, mylasterror.message);
end
% *************************************************************************

end
 