%incoming.m

clear;

WAVE = 0;

float = 'FAUCON';

if (WAVE)
    path =    sprintf('/u/bogcpf/data');
    %path =    sprintf('');
    infile = sprintf('%s/%s.raw',path,float);
    outFile = sprintf('%s/%s_sbd.mat',path,float);
    figpath = '/var/www/html/CPF';
    outfig = sprintf('%s/%s.T.jpeg',figpath,float);
    outfigS = sprintf('%s/%s.S.jpeg',figpath,float);
    outfigENG = sprintf('%s/%s.ENG.jpeg',figpath,float);
    outkml = sprintf('%s/%s',figpath,float);
else
    fprintf('output for windows');
    %path =    sprintf('/u/bogcpf/data');
    %path =    sprintf('');
    infile = sprintf('%s.raw',float);
    outFile = sprintf('%s_sbd.mat',float);
    figpath = '';
    outfig = sprintf('%s.T.jpeg',float);
    outfigS = sprintf('%s.S.jpeg',float);
    outfigENG = sprintf('%s.ENG.jpeg',float);
    outkml = sprintf('%s',float);
end

numfiles=0;
inFID = fopen(infile);
while ~feof(inFID)
  inString = fgets(inFID);
  if(inString(1)=='!')
      numfiles=numfiles+1;
  end
end
fclose(inFID);


MAXi=125;
P = NaN(numfiles,MAXi);
T = NaN(numfiles,MAXi);
S = NaN(numfiles,MAXi);
CPtime = NaN(numfiles,MAXi);
maxP = NaN(numfiles);
maxI = NaN(numfiles);
vac = NaN(numfiles);
battV = NaN(numfiles);
relh = NaN(numfiles);
lat = NaN(numfiles);
lon = NaN(numfiles);
gps_t = cell(numfiles);



 ii=1;
 inFID = fopen(infile,'r');
 while ~feof(inFID)
  inString = fgets(inFID);
  %fprintf('&&&%s&&&\n',inString);
  if(inString(1)=='!')
   inFields = strsplit(inString, ',', 'CollapseDelimiters', false);
   %date and time
   A = sscanf(inFields{1},'!!!%d-%d-%dT%d:%d:%dZ');
   format long;
   prof_time = datenum(A(1),A(2),A(3),A(4),A(5),A(6));
   gps_t{ii}=sprintf('%02d%02d',A(4),A(5));

   %lat lon
   latmin=str2double(inFields{3})-3600;
   lonmin=str2double(inFields{6})-12100;
   lat(ii) = 36 + latmin/60;
   lon(ii) = -1*(121 + lonmin/60);
        
   maxP(ii) = -str2double(inFields{25});
   %maxI(ii) = str2double(inFields{11});
   battV(ii) = str2double(inFields{29});
   vac(ii) = str2double(inFields{31});
   relh(ii) = str2double(inFields{33});
   
   
   begin = 36;
   
   for UBX = begin:1:size(inFields,2)
       if (strfind(inFields{UBX}, 'AT+SBDIX') > 0)
           ubx = UBX;
       end
   end
   dat=1;
   for UBX = begin:1:ubx-1
       %disp(inFields{UBX});
       %F[P(ii,dat),T(ii,dat),S(ii,dat),n] = parseCPHex(inFields{UBX});
       cpString = inFields{UBX};
       if(size(cpString,2)>2)
        P(ii,dat)= -1 * hex2dec(cpString(1:4)) / 10;
        T(ii,dat)=  hex2dec(cpString(5:8)) / 1000;
        if(T(ii,dat)<5)
            T(ii,dat)=NaN;
        end
        S(ii,dat)=  hex2dec(cpString(9:12)) / 1000;
        if(S(ii,dat)<32)
            S(ii,dat)=NaN;
        end
        CPtime(ii,dat)=prof_time;
        %fprintf('dat=%d, P=%f, T=%f,S=%f\n',dat,P(ii,dat),T(ii,dat),S(ii,dat));
        if(P(ii,dat)<0)
            dat=dat+1;
        end
        
       end
   end

   ii=ii+1;
   %fprintf('\nfeof = %d\n',feof(inFID));
  end
end
fclose(inFID);

numfiles = ii-1;

%write out the kml file
%bdjkmlwrite(outkml,lat,lon,gps_t);

ofile = strcat(outkml,'.kml');
outFID = fopen(ofile,'w');

fprintf('outfile = %s, outFID=%d\n',ofile,outFID);

%print header
fprintf(outFID,'<?xml version="1.0" encoding="utf-8"?>\n');
fprintf(outFID,'<kml xmlns="http://www.opengis.net/kml/2.2">\n');
fprintf(outFID,'<Document>\n');
fprintf(outFID,'   <name>%s</name>\n',outFile);

for i = 1:1:size(lat,1)
    fprintf(outFID,'     <Placemark>\n       <Snippet maxLines="0"> </Snippet>\n       <description> </description>\n');
    fprintf(outFID,'       <name>%04d</name>\n',str2double(gps_t(i,1)));
    fprintf(outFID,'          <Point><coordinates>%f,%f,0</coordinates></Point>\n',lon(i,1),...
                    lat(i,1));
    fprintf(outFID,'     </Placemark>\n');
end

fprintf(outFID,'</Document>\n</kml>\n');

fclose(outFID);




fprintf('Making fig1\n');
figure(1);
if(WAVE)
    set(gcf,'visible','off');
end
hold on;
for prof=1:numfiles
    scatter(CPtime(prof,:),P(prof,:),350,T(prof,:),'filled','s');
    %disp(P(prof,1:30));
    %pause;
end
colorbar;datetick;grid on
title('Temp');
xlabel('time');
%ylim([min(min(P))-5 0]);
%print(outfig,'-djpeg');
if(WAVE)
    close gcf;
    clear gcf;
end



fprintf('Making fig2\n');
figure(2);
if(WAVE)
    set(gcf,'visible','off');
end
hold on;
for prof=1:numfiles
    scatter(CPtime(prof,:),P(prof,:),350,S(prof,:),'filled','s');
end
colorbar;datetick;grid on;
title('Sal');
xlabel('time');
%ylim([min(min(P))-5 0]);

print(outfigS,'-djpeg');
if(WAVE)
    close gcf;
    clear gcf;
end

fprintf('Making fig3\n');
figure(3);
if(WAVE)
    set(gcf,'visible','off');
end
subplot(5,1,1);
plot(battV);
ylabel('battv');
grid on;
subplot(5,1,2);
plot(maxI);
ylabel('max amp');
grid on;
subplot(5,1,3);
plot(relh);
ylabel('relh');
grid on;
subplot(5,1,4);
plot(vac);
ylabel('int vac');
grid on;
subplot(5,1,5);
plot(maxP);
ylabel('max dBar');
grid on;
xlabel('prof');


%print(outfigENG,'-djpeg');
if(WAVE)
    close gcf;
    clear gcf;
end

sci_prof_bar;


%pause(15);

fprintf('Done with realtime.m\n');
%bdjkmlwrite(outkml,lat,lon,gps_t);
