  sweepfile=input('enter the file name    ','s');
fid=fopen(sweepfile,'r')

eofstat=feof(fid)
temp2=[];
while eofstat==0 
      [indic,count]=fread(fid,4,'char');
      s=char(indic');
       
     
%--------------------------------------- VOLD descriptor------------------%
      if strcmp(s,'VOLD')==1
          disp('VOLD Descriptor');
          position=ftell(fid)
          L=fread(fid,1,'int32')  
          L=L-4+position;        
          fseek(fid,2,'cof');
          vol_num=fread(fid,1,'int16');
          fseek(fid,4,'cof');
          project=fread(fid,20,'char');
          project=char(project');
          yr=fread(fid,1,'int16');
          month=fread(fid,1,'int16');
          day=fread(fid,1,'int16');
          hr=fread(fid,1,'int16');
          mn=fread(fid,1,'int16');
          sec=fread(fid,1,'int16');
          status=fseek(fid,L,'bof');
      end
      
      
      
%---------------------------------------RADD descriptor------------------%
      if strcmp(s,'RADD')==1
          disp('RADD descriptor')
          position=ftell(fid);
          L=fread(fid,1,'int32')
          L=L-4+position;
          radar_name=fread(fid,8,'char');
          fseek(fid,48,'cof');
          tot_parm=fread(fid,1,'int16');
          tot_desc=fread(fid,1,'int16');
          fseek(fid,12,'cof');
          radar_long=fread(fid,1,'float32');
          radar_lat=fread(fid,1,'float32');
          radar_alt=fread(fid,1,'float32');
          unamb_vel=fread(fid,1,'float32');
          unamb_rang=fread(fid,1,'float32');
          status=fseek(fid,L,'bof');   
          if status==-1
              disp('error in reading RADD')
          end
   end
   
   
 %---------------------------------------PARM descriptor------------------%
  
   if strcmp(s,'PARM')==1
       disp('PARM descriptor')
       position=ftell(fid);
       L=fread(fid,1,'int32');
       
       
       
           for i=1:tot_parm
               position1=ftell(fid);   
               L_parm=L+position1;
              name=fread(fid,8,'char');
              name=char(name');
             param(i).name=name;
%              disp(param(i).name)
             desc=fread(fid,40,'char');
              param(i).desc=char(desc');
             units=fread(fid,8,'char');
             units=char(units');
             param(i).units=units;
             fseek(fid,14,'cof');
             type=fread(fid,1,'int16');
             param(i).type_num=type;
             [param_type]=type_size(type);
             param(i).type=param_type;
             fseek(fid,12,'cof');
             param(i).scale=fread(fid,1,'float32');
             param(i).offset=fread(fid,1,'float32');
             param(i).data=[];
           
        fseek(fid,L_parm,'bof');%look for the next parameter descriptor
    end
    L=tot_parm*L+position-4;
      status=fseek(fid,L,'bof')
   end
   
   
 %---------------------------------------CELV descriptor------------------%
  
   if strcmp(s,'CELV')==1
          disp('CELV Descriptor');
          position=ftell(fid);
          L=fread(fid,1,'int32');
          L=L-4+position;
          num_cell=fread(fid,1,'int32');
          for i=1:num_cell
              dist(i)=fread(fid,1,'float32');
          end
      end
       
      
   %---------------------------------------CFAC descriptor------------------%
   
      if strcmp(s,'CFAC')==1
          disp('CFAC Descriptor');
          position=ftell(fid);
          L=fread(fid,1,'int32')
          L=L-4+position;
          fseek(fid,L,'bof');
      end
   
      
      
      
      %---------------------------------------SWIB descriptor------------------%

      
      if strcmp(s,'SWIB')==1
          disp('SWIB Descriptor');
          position=ftell(fid);
          L=fread(fid,1,'int32');
          L=L-4+position;
          radar=fread(fid,8,'char');
          radar=char(radar')
          sw_numO=fread(fid,1,'int32');
          num_ray=fread(fid,1,'int32');
          fseek(fid,L,'bof');
          
      end
    
      
      
      
      %---------------------------------------RYIB descriptor------------------%

      if strcmp(s,'RYIB')==1
          disp('RYIB Descriptor');
          
          for i=1:num_ray           %read data ray per ray
              position=ftell(fid);
              L=fread(fid,1,'int32');
              L1=L+position-4;
              sw_num=fread(fid,1,'int32');
              fseek(fid,12,'cof');
              az(i)=fread(fid,1,'float32');
             el(i)=fread(fid,1,'float32');
             fseek(fid,L1,'bof');
                      
             fread(fid,4,'char');     %platform descriptor
             position=ftell(fid);
             L2=fread(fid,1,'int32');
             L2=L2+position-4;
             fseek(fid,L2,'bof');
            
            
             B=fread(fid,4,'char');  %identifier of the parameter data descriptor
             B=char(B');
         
             for j=1:tot_parm      % read datfor each parameter
                 
                 L_data=fread(fid,1,'int32');
                 L_data=L_data-16 ;  %data start at byte 16 so L_data is the length of the data to read
                 position=ftell(fid);
                 name_param=fread(fid,8,'char');
                 nam=char(name_param');
   
                   [L_data]=num_data_read(param(j).type_num,L_data);   %num_data_read give the length of the data depending on
                  rdata=fread(fid,L_data,param(j).type);               %the type of the parameter
                  rdata=(rdata-param(j).offset)/param(j).scale;
                  fin=find(rdata==0.01);                            % look for 0.01 in the data read
                  rdata=rdata(2:fin(length(fin))-2);      % take the data from the 2nd data point read to the 2nd point above the last 0.01 found
                  whol=round(rdata);
                  u=find(whol==-327  |  whol==-328); %look for the missing flags
                  urdata=[u rdata(u)];
                  v=find(urdata(:,2)~=-327.68); % v contains only the missing flags for more than 1 missing data
                  v=u(v);
                  if length(v)~=0
                     for z=1:length(v)-1
                         miss=rdata(v(z)-1)*100;
                         miss=int2str(miss);
                         miss=str2num(miss);
                         adds=NaN*ones(miss,1);
                         temp=[adds;rdata(v(z)+1:v(z+1)-2)];
                         temp2=[temp2;temp];    
                     end
                   temp2=[rdata(1:v(1)-2);temp2]; %adds the 1st part of the data
                   miss=rdata(v(length(v))-1)*100; %take care of the last missig flag
                   miss=int2str(miss);
                   miss=str2num(miss);
                   adds=NaN*ones(miss,1);
                   temp3=[temp2;adds;rdata(v(length(v))+1:length(rdata))];
                   miss=num_cell-length(temp3);
                   miss=int2str(miss);
                   miss=str2num(miss);
                   adds=NaN*ones(miss,1);
                   rdata=[temp3;adds];
                   param(j).data=[param(j).data rdata];
                   temp2=[];

              else  % if there is no missing data
                 miss=num_cell-length(rdata);
                 miss=int2str(miss);
                 miss=str2num(miss);
                 adds=NaN*ones(miss,1);
                 rdata=[rdata;adds];
                 param(j).data=[param(j).data rdata];
                    
             end 
                  
            
             
              C=fread(fid,4,'char'); %reads the next parameter data block or goes to the next ray
             C=char(C');
               
        end 
          
             
   end              

          eofstat=1;  
    end
    
    
end

fclose(fid)



%----------------------------------------------------Saving files

cd ('C:\Documents and Settings\sylvie lorsolo\My Documents\research\data\');
sweepfile=sweepfile(1:length(sweepfile)-3);
for i=1:tot_parm
    name=param(i).name;
    u=isletter(name);
    v=find(u==1);
    name =name(v);
    data=param(i).data;
   save([sweepfile,name,'.dat'],'data','-ascii')
end
