% This is to plot the Stokes parameters loaded from a Novoptel PM1000 polarimeter
% supports GUI version 2.0.0.0, with Timestamps
% Copyright 2020 Benjamin Koch, Novoptel GmbH

% Usage: pm1000plot(filename, withtimestamps);
% filename can for instance be 'myrecord.txt' (text file) or 'myrecord.bin' (binary file).
% For GUI >=2.0.0.0, firmware >=1.0.6.0, the records contain timestamps. 
% To accomodate this, withtimestamps is chosen as 1 or, if dropped, is chosen as 1 by default.
% Examples: pm1000plot('myrecord.txt');   pm1000plot('myrecord.bin'); 
%           pm1000plot('myrecord.txt',1); pm1000plot('myrecord.bin',1);
% For GUI <2.0.0.0, there are no timestamps, and one sets withtimestamps as 0. 
% Examples: pm1000plot('myrecord.txt',0); pm1000plot('myrecord.bin',0);

function pm1000plot(filename, withtimestamps)

if (nargin<2) || isempty(withtimestamps)
   withtimestamps=1;
end

% open the data file
FID=fopen(filename);

if sum(filename(end-3:end)=='.bin')==4, % this is a binary file

   % get the header length (mimimum 256 bytes)
   C=fread(FID, 256, 'uint8');
   headerstrings=strsplit(char(transpose(C)), char(13));
   try eval(char(headerstrings(1)))
   catch err
      disp(sprintf('ERROR: Unknown command: %s', char(headerstrings(1))));
      pause
   end

   % get the full header with meta data
   fseek(FID, 0, 'bof');
   C=fread(FID, headerlength, 'uint8');
   headerstrings=strsplit(char(transpose(C)), char(13));
   size_headerstrings=size(headerstrings);
   nlines=size_headerstrings(2);

   % load information from meta data
   if nlines>2,
      for ii=2:nlines-1
         try eval(char(headerstrings(ii)))
         catch err
            disp(sprintf('ERROR: Unknown command: %s', char(headerstrings(ii))));
            pause
         end
      end;
   end;

   % get the data after the header
   fseek(FID, headerlength, 'bof');
   C=fread(FID, 'uint16');


   if withtimestamps,
      % get the timestamps and stokes parameters
      TS_N = reshape(C, 8, floor(length(C)/8));
      
      % get the timestamps, referred to the first sample
      Timestamp_ns = uint64(TS_N(4,:)*2^48) + uint64(TS_N(3,:)*2^32) ...
                  + uint64(TS_N(2,:)*2^16 )+ uint64(TS_N(1,:));
      Timestamp_ns = Timestamp_ns - Timestamp_ns(1);
      
      % restore the Stokes parameters
      N=TS_N(5:8,:);
   else
      N = reshape(C, 4, floor(length(C)/4));
      Timestamp_ns = (1:length(N))*SamplePeriod_ns;
   end;

else % this is text file

   % get the meta data
   C=textscan(FID, '#%s', 'Delimiter', '@');

   % load information from meta data
   if length(C{1})>0,
      for ii = 1:length(C{1}),
         try eval(C{1}{ii})
         catch err
            disp(sprintf('ERROR: Unknown command: %s', C{1}{ii}));
         end
      end
   end

   if withtimestamps,
      % get the timestamps and stokes parameters
      NN=textscan(FID,'%f %f %f %f %f','Delimiter',',');
  
      clear Timestamp_ns;
      Timestamp_ns(1,:)=NN{1};

      % restore the Stokes parameters
      clear N
      for ii=2:5,
         N(ii-1,:)=NN{ii};
      end;
   else
      % get the stokes parameters
      NN=textscan(FID,'%f %f %f %f','Delimiter',',');
  
      % restore the Stokes parameters
      clear N
      for ii=1:4,
         N(ii,:)=NN{ii};
      end;

      Timestamp_ns = (1:length(N))*SamplePeriod_ns;
   end

   
end;

% close the data file
fclose(FID);

% remove offset from Stokes parameters 1..3
N(2:4,:)=N(2:4,:)-2^15;
S=N/2^15;
  
% plot the data
figure;
x=Timestamp_ns; %X-axis in Nanoseconds
plot(x, S(1,:), '.:k', x, S(2,:), '.:r', x, S(3,:), '.:b', ...
         x, S(4,:), '.:g');
legend(Data1Name, 'Stokes 1', 'Stokes 2', 'Stokes 3');
xlabel('Nanoseconds');
grid on;







