function bang = loadpic(fid)
%LOADBANG reads *.pic files created by the the opto-picker
%    y = LOADPIC(filename) takes as input the filename of a opto-banger
%    PIC file and returns a struct containing:
%         meta          STRUCT  containing metadata
%         time          [1 N]   vector of times of data points (in seconds)
%         targ          [1 N]   vector of target values
%         meas          [1 N]   vector of measurements.
%         filt          [1 N]   vector of filtered measurements.
%         erro          [1 N]   vector of error values.
%         erin          [1 N]   vector of error integral values.
%         clon          [1 N]   vector of controller engaged logicals.
%         out1          [1 N]   vector of output 1 doubles.
%         out2          [1 N]   vector of output 2 doubles.
% 
%    Created by: Jon Newman (jnewman6<snail>gatech<dot>edu)
%    Created on: Sept 12, 2012
%    Last modified: Sept 12, 2012
%
%    Licensed under the GPL: http://www.gnu.org/licenses/gpl.txt

if nargin < 1
    error('You must provide a file path')
end
if(~strcmp(fid(end-3:end),'bang'))
    warning(['This file does not have a .bang extension. '... 
             'trying to extract data anayway...'])
end

% Main code
h = fopen(fid,'r');

% Get file size
fseek(h,0,'eof');
len = ftell(h);
fseek(h,0,'bof');

% Read header info
numstreams = fread(h,1,'int32'); % number of feedback streams
fs = fread(h,1,'double'); % sampling rate
dt = fread(h,7,'ushort'); % date and time
str_len  = fread(h,1,'uint8'); % date and time
type = fread(h,str_len,'*char')';

% Constants
HEADER_BYTES = 26 + str_len + 1;
REC_BYTES = 8*(numstreams-1+1) + 1;

% Calculated number of stimuli
numdat = floor((len-HEADER_BYTES)/REC_BYTES);

% Display record info
fprintf('\nRT OPTO-BANGER RECORD\n');
fprintf(['\tLoop rate (Hz): ' num2str(fs) '\n']);
fprintf(['\tMeasurement used: ' type '\n']);
fprintf(['\tRecording time (yr-mo-dy-hr-mi-sc-ms): ' ... 
        num2str(dt(1)) '-' ...
        num2str(dt(2)) '-' ...
        num2str(dt(3)) '-' ...
        num2str(dt(4)) '-' ...
        num2str(dt(5)) '-' ...plo
        num2str(dt(6)) '-' ...
        num2str(dt(7)) '\n']);
fprintf(['\tNumber of streams recorded: ' num2str(numstreams) '\n\n']);

% Write metadata
bang.meta.date = dt; 
bang.meta.loop_hz = fs;
bang.meta.meas_type = type;

% Read the actual 'daturs

% Time
bang.time = fread(h,numdat,'double',REC_BYTES - 8)';

% Target
laststop = HEADER_BYTES + 8;
fseek(h,laststop,'bof');
bang.targ = fread(h,numdat,'double', REC_BYTES - 8)';

% Meas
laststop = laststop + 8;
fseek(h,laststop,'bof');
bang.meas = fread(h,numdat,'double', REC_BYTES - 8)';

% Filt
laststop = laststop + 8;
fseek(h,laststop,'bof');
bang.filt = fread(h,numdat,'double', REC_BYTES - 8)';

% Error
laststop = laststop + 8;
fseek(h,laststop,'bof');
bang.erro = fread(h,numdat,'double', REC_BYTES - 8)';

% Error Integral
laststop = laststop + 8;
fseek(h,laststop,'bof');
bang.erin = fread(h,numdat,'double', REC_BYTES - 8)';

% Control On
laststop = laststop + 8;
fseek(h,laststop,'bof');
bang.clon = logical(fread(h,numdat,'uint8', REC_BYTES - 1))';

% Out 1
laststop = laststop + 1;
fseek(h,laststop,'bof');
bang.out1 = fread(h,numdat,'double', REC_BYTES - 8)';

% Out 2
laststop = laststop + 8;
fseek(h,laststop,'bof');
bang.out2 = fread(h,numdat,'double', REC_BYTES - 8)';

% close file
fclose(h);

end



