% Read in balloon data RAW files.
% DEW 10/30/2000

%Read in wind data file
clear data balw balt
% read in list of balloon files
%07/26/2002  12:05a             245,512 0207252302.wnd
%07/25/2002  08:15p             254,403 0207251655.wnd

ballst = 'bal.lst';
fid1 = fopen(ballst, 'r');
% Loop through list of 92 files
for bl = 1:92   %92
   clear balw balt bal1
   fnbal = fgetl(fid1);
   keyboard
   % special test.....
   if bl == 1
   %   fnbal(40:53) = '0207211106.wnd'; % no winds
   %   fnbal(40:53) = '0207211353.wnd';%  no PTU
fnbal(40:53) = '04123119.wnd';%   
end
   
   fnamew(1:10) = fnbal(40:49);
   fnamet(1:10) = fnbal(40:49);
   fnamet(11:14) = '.ptu';
   fnamew(11:14) = '.wnd';
   fnamew
   
   
   %%mjf  
   %fnamew = '0207132323.wnd'
   
   fid2=fopen(fnamew, 'r');
   if fid2 > 0 % file found
      
      %
      %
      %Complete listing of Fine Structure file
      %File: C:\Metgraph\archive\001007.001\00100716.06e
      %
      %Started at       7 October 2000 16:06 UTC
      %
      %Station  :      313
      %Location :      33.24 N 128.89 W        10 m/MSL
      %
      %Sounding type : PTU GPS
      %RS-number     : 805103110
      %Sounding processor serial number: 0
      %
      %
      %Ground check omitted
      %
      %Clouds    : ///// 
      %Special 1 : ///// 
      %Special 2 : ///// 
      %
      %Telemetry noise level  : 80 %
      %
      %                 P      T      U
      %Accepted (%) :  35.3   64.2   60.7
      %Replaced (%) :  52.1   23.2   26.7
      %Rejected (%) :  12.6   12.6   12.6
      %
      %GPS data quality
      %    Valid raw wind ratio        :   73.0 %
      %    Valid raw wind on 5 ratio   :   73.0 %
      %    Invalid raw wind on 4 ratio :    1.0 %
      %    Poor PDOP ratio             :    1.0 %
      %    Unidentified signals ratio  :    0.0 %
      %    Unused                      :    0.0 %
      %    Mean satellite track count  :    4.7
      %    Mean track count valid      :    6.4
      %    Gaps over 1 min             :    0.0 min
      %
      %
      %Reason for termination : Manual stop
      %
      %   Time Pressure      Height   Speed  Direction   Significance flags
      %
      % min  s      hPa     gpm     m   m/s    deg  Automatic  Operator
      %
      %
      % Read in all header lines
      for n = 1:46
         h = fgetl(fid2);
         % print out Launch time
         if n == 6
            baltm = h;
         end
      end
      % Read in data lines
      %   Time Pressure      Height   Speed  Direction   Significance flags
      % min  s      hPa     gpm     m   m/s    deg  Automatic  Operator
      %   0  0   1017.0      10 /////   3.6     12       dfv         dfv         
      %   0  2   1015.5      23 /////   3.6     11                               
      %   0  4   1014.7      29 /////   3.7     11                               
      wt = 0;
      for i=1:1500
         data = fgetl(fid2);
         if feof(fid2) == 1
            break
         end
         
         if (data(1,41:43) ~= '///') &  (data(1,14:16) ~= '///' )     % Test for flagged data
            wt = wt + 1; % count good wind pts   
            balw(wt,1) = str2num(data(1,1:4)); %min
            balw(wt,3) = str2num(data(1,8:16));%p
            balw(wt,4) = str2num(data(1,17:24))/1000.;%ht msl km
            balw(wt,6) = str2num(data(1,37:43));%WD
            balw(wt,7) = str2num(data(1,31:36));%WS
            balw(wt,8) = str2num(data(1,43:49));%dz
         end
      end
      lb = size(balw);
      if lb(1) > 10
         %De-Bob balloon sounding
         bct = 1;
         % save 1st point
               bal1(bct,1) = balw(1,1); %min
               bal1(bct,4) = balw(1,2); %ht
               bal1(bct,3) = balw(1,3); %P
               bal1(bct,6) = balw(1,6); %wd
               bal1(bct,7) = balw(1,7); %ws
               bal1(bct,8) = balw(1,8); %dz
         blast = balw(1,2);
         for bb = 2:(length(balw))
            if balw(bb,2) > blast
               bct = bct + 1;
               bal1(bct,1) = balw(bb,1); %min
               bal1(bct,4) = balw(bb,2); %ht
               bal1(bct,3) = balw(bb,3); %P
               bal1(bct,6) = balw(bb,6); %wd
               bal1(bct,7) = balw(bb,7); %ws
               bal1(bct,8) = balw(bb,8); %dz
               blast = balw(bb,2);  % save last good pt assuming lower pt is good
            end
         end
         % put de-bobbed data back into array for later comparisons
         %clear bal
         
         baltmpw = balw; % save original data for testing
         balw = bal1; % copy de-bobbed file into original name 
      end % test > 10 pts to de-bob
      %Wind plot
      figure(2)
      
      subplot(1,2,1),plot(balw(:,6),balw(:,2),'b.');
      axis([0,360,0,12])
      xlabel('Dir True')
      ylabel('Ht km-MSL')
      %hold
      
      subplot(1,2,2),plot(balw(:,7),balw(:,2),'b');
      title('')
      axis([0,30,0,12])
      title(fnamew)
      xlabel('Speed m/s')
      ylabel('Ht km-MSL')
      %hold
      
      pause(1)
      
      fclose(fid2); % wnd file
   end %file found
   
   %%%%%%%%%%%%%%%%
   %Read in Temp data file
   %mjf fnamet = '0207132323.ptu'
   fid3=fopen(fnamet, 'r');
   
   if fid3 > 0 % PTU file found
      %
      %
      %Complete listing of Fine Structure file
      %File: C:\Metgraph\archive\001007.001\00100716.06e
      %
      %Started at       7 October 2000 16:06 UTC
      %
      %Station  :      313
      %Location :      33.24 N 128.89 W        10 m/MSL
      %
      %Sounding type : PTU GPS
      %RS-number     : 805103110
      %Sounding processor serial number: 0
      %
      %
      %Ground check omitted
      %
      %Clouds    : ///// 
      %Special 1 : ///// 
      %Special 2 : ///// 
      %
      %Telemetry noise level  : 80 %
      %
      %                 P      T      U
      %Accepted (%) :  35.3   64.2   60.7
      %Replaced (%) :  52.1   23.2   26.7
      %Rejected (%) :  12.6   12.6   12.6
      %
      %GPS data quality
      %    Valid raw wind ratio        :   73.0 %
      %    Valid raw wind on 5 ratio   :   73.0 %
      %    Invalid raw wind on 4 ratio :    1.0 %
      %    Poor PDOP ratio             :    1.0 %
      %    Unidentified signals ratio  :    0.0 %
      %    Unused                      :    0.0 %
      %    Mean satellite track count  :    4.7
      %    Mean track count valid      :    6.4
      %    Gaps over 1 min             :    0.0 min
      %
      %
      %Reason for termination : Manual stop
      %
      %   Time Pressure   Height  Temperature  RH   Dewp   Significance flags
      %
      % min  s      hPa      gpm     deg C      %  deg C  Automatic  Operator
      %
      % Read in all header lines
      for n = 1:46
         h = fgetl(fid3);
         % print out Launch time
         if n == 6
            h;
         end
      end
      % Read in data lines
      %   Time Pressure   Height  Temperature  RH   Dewp   Significance flags
      % min  s      hPa      gpm     deg C      %  deg C  Automatic  Operator
      %   0  0   1017.0       10      18.3     58    9.9 TU          TU            
      %   0  2   1015.5       23      18.3     55    9.1                           
      %   0  4   1014.7       29      18.2     54    8.8 
      tt = 0;
      for i=1:1500
         data = fgetl(fid3);
         if feof(fid3) == 1
            break
         end
         
         
         if (data(1,33:35) ~= '///') &  (data(1,14:16) ~= '///' )   % Test for flagged data
            tt = tt + 1; %count good data pts
            balt(tt,1) = str2num(data(1,1:4)); %min
            balt(tt,3) = str2num(data(1,8:16));%p
            balt(tt,4) = str2num(data(1,26:35));%Temp
            balt(tt,5) = str2num(data(1,36:42));%RH
            balt(tt,2) = str2num(data(1,17:25))/1000.;%ht msl km
         end
      end
      %De-Bob balloon sounding
      clear bal1
      % Test for > 10 pts to de-bob
       lb = size(balw);
      if lb(1) > 10
         bct = 1;
         %save 1st point
         bal1(bct,1) = balt(bb,1); %min
            bal1(bct,2) = balt(1,2); %ht
            bal1(bct,3) = balt(1,3); %P
            bal1(bct,4) = balt(1,4); %T
            bal1(bct,5) = balt(1,5); %RH
      blast = balt(1,2);
      for bb = 2:(length(balt))
         if balt(bb,2) > blast
            bct = bct + 1;
            bal1(bct,1) = balt(bb,1); %min
            bal1(bct,2) = balt(bb,2); %ht
            bal1(bct,3) = balt(bb,3); %P
            bal1(bct,4) = balt(bb,4); %T
            bal1(bct,5) = balt(bb,5); %RH
            blast = balt(bb,2);  % save last good pt assuming lower pt is good
         end
      end
      % put de-bobbed data back into ba array for later comparisons
      %clear bal
      
      baltmpt = balt; % save original data for testing
      clear balt
      balt = bal1; % copy de-bobbed file into original name 
      
      end % > 10 pts to de-bob
      %%%%%%%%%%%%%%%%%
      
      % plotting data
      %temp plot
      if 1
         figure(1)
         %subplot(1,1,1),plot(balt(:,4),balt(:,2),'b')
         subplot(1,2,1),plot(balt(:,4),balt(:,2),'b');
         axis([-25,30,0,7])
         xlabel('Temp C')
         ylabel('Ht km-MSL')
         %hold
         
         subplot(1,2,2),plot(balt(:,5),balt(:,2),'b');
         title(fnamet)
         axis([0,100,0,7])
         xlabel('RH %')
         ylabel('Ht km-MSL')
         %hold
      end
      fclose(fid3); % ptu file
   end % ptu file found
   
   if 1
      %      cd 'G:\Cruise_Data\EPIC 2001'
      fnb = fnamew(1:10);
      figure(2)
      print( '-djpeg99', '-r300', fnb);
   end
   
end  % end reading bal.lst loop
fclose(fid1);