%Verify that the files you are going to treat have these specificity:
radar_name='SMART-R1';
scan_mode=1;%1 for PPI 3 for RHI
radar_long=-80.6875;
radar_lat=28.341389;
radar_alt=4.248;
unamb_vel=33.880741;
unamb_rang=58.759327;
angle_vol=[0.5;0.8;1.1;1.4;1.7;2.0;2.3;2.6;2.9;3.2;3.5;3.8;4.1;4.5;5.0;5.5;6.0;6.5;7.0;7.5;8.0;8.5];%[0.5;2.0;3.5;5.0;6.5;8.0;9.5];
julian_day=248;%248:09/04/2004, 249 09/05/2004

files=dir(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\data\dorade\*EV*.dat']); 

for fil=1%:22%:length(files)
    sweepfile=files(fil).name;
    data=load(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\data\dorade\',sweepfile]);
    data2=load(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\data\dorade\compressed\',sweepfile]);
    if fil==1 
        start_yr=str2num(sweepfile(6:7))+2000;
        start_month=str2num(sweepfile(8:9));
        start_day=str2num(sweepfile(10:11));
        start_hr=str2num(sweepfile(12:13));
        start_min=str2num(sweepfile(14:15));
        start_sec=str2num(sweepfile(16:17));
    end
    %information from file and filename:
    az=data(length(data),:);
    data=data(1:length(data)-1,:);
    [li,col]=size(data);
    num_cell=li;
    start_angle=az(1);
    stop_angle=az(length(az));
    num_ray=length(az);
    fixed_angle=str2num(sweepfile(30:32));
    yr=str2num(sweepfile(6:7))+2000;
    month=str2num(sweepfile(8:9));
    day=str2num(sweepfile(10:11));
    hr=str2num(sweepfile(12:13));
    min=str2num(sweepfile(14:15));
    sec=str2num(sweepfile(16:17));
    dist=0:67:67*(num_cell-1);
    diff=angle_vol-fixed_angle;
    sw_num0=find(diff==0);
    
    
    name_of_file=sweepfile(1:37);
    cd ('C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\raw2\')
    fid2=fopen([name_of_file,'v1'],'r')
    fseek(fid2,2817272,'bof');
    ftell(fid2)
    s=fread(fid2,4,'char');
    s=char(s')
    L=fread(fid2,1,'int32')
    for i=1:1926
        valeur(i)=fread(fid2,1,'int16');
    end
    ftell(fid2)
    s=fread(fid2,4,'char');
    s=char(s')
    L=fread(fid2,1,'int32')
    for i=1:451
        valeur2(i)=fread(fid2,1,'int16');
    end
    ftell(fid2)
    fclose(fid2)
    cd ('C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\data\dorade\')
    fid=fopen([name_of_file,'v1'],'w')
    eofstat=feof(fid)
    
    %--------------------------------------SSWB bock----------------------
    fwrite(fid,'SSWB','char');
    fwrite(fid,196,'int32');
    fwrite(fid,1096908391,'int32');
    fwrite(fid,1094342249,'int32');
    fwrite(fid,1094342249,'int32');
    fwrite(fid,2826796,'int32');
    fwrite(fid,0,'int32');
    fwrite(fid,1096908391,'int32');
    fwrite(fid,4,'int32');
    vide_8='        ';
    fwrite(fid,vide_8,'char');
    fwrite(fid,0,'double');
    fwrite(fid,0,'double');
    fwrite(fid,0,'int32');
    fwrite(fid,2,'int32');
    fwrite(fid,0,'int32');
    fwrite(fid,0,'int32');
    fwrite(fid,0,'int32');
    fwrite(fid,0,'int32');
    fwrite(fid,0,'int32');
    fwrite(fid,0,'int32');
    fwrite(fid,0,'int32');
    fwrite(fid,0,'int32');
    fwrite(fid,2817272,'int32');
    fwrite(fid,7712,'int32');
    fwrite(fid,2,'int32');
    fwrite(fid,2824984,'int32');
    fwrite(fid,1812,'int32');
    fwrite(fid,3,'int32');
    for i=1:6
        fwrite(fid,0,'int32');
        fwrite(fid,0,'int32');
        fwrite(fid,0,'int32');
    end
    ftell(fid)
    %--------------------------------------- VOLD descriptor------------------%
    s='VOLD';
    fwrite(fid,s,'char');
    L_vol_desc=72;
    fwrite(fid,L_vol_desc,'int32');
    fwrite(fid,1,'int16');
    fwrite(fid,1,'int16');
    fwrite(fid,65500,'int32');
    
    
    project='TRMMGVUF            ';
    fwrite(fid,project,'char');
    fwrite(fid,start_yr,'int16');
    fwrite(fid,start_month,'int16');
    fwrite(fid,start_day,'int16');
    fwrite(fid,start_hr,'int16');
    fwrite(fid,start_min,'int16');
    fwrite(fid,start_sec,'int16');
    vide='        ';
    fwrite(fid,vide,'char');
    gen_facility='NCAR/ATD';
    fwrite(fid,gen_facility,'char');
    fwrite(fid,yr,'int16');
    fwrite(fid,10,'int16');
    fwrite(fid,day,'int16');
    fwrite(fid,1,'int16');
    ftell(fid)
    
    
    %---------------------------------------RADD descriptor------------------%
    fwrite(fid,'RADD','char');
    fwrite(fid,144,'int32')
    fwrite(fid,radar_name,'char');
    fwrite(fid,-31.5,'float32');
    fwrite(fid,16.5,'float32');
    fwrite(fid,-31.5,'float32');
    fwrite(fid,27,'float32');
    fwrite(fid,18.5,'float32');
    fwrite(fid,27,'float32');
    fwrite(fid,1,'float32');
    fwrite(fid,1,'float32');
    fwrite(fid,0,'int16');
    fwrite(fid,scan_mode,'int16');
    fwrite(fid,18,'float32');
    fwrite(fid,-32768,'float32');
    fwrite(fid,-32768,'float32');
    fwrite(fid,1,'int16');
    fwrite(fid,1,'int16');
    fwrite(fid,1,'int16');
    fwrite(fid,0,'int16');
    fwrite(fid,-32768,'float32');
    fwrite(fid,-32768,'float32');
    fwrite(fid,radar_long,'float32');
    fwrite(fid,radar_lat,'float32');
    fwrite(fid,radar_alt,'float32');
    fwrite(fid,unamb_vel,'float32');
    fwrite(fid,unamb_rang,'float32');
    fwrite(fid,1,'int16');
    fwrite(fid,1,'int16');
    fwrite(fid,5.643153,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0.000392,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    
    ftell(fid)
    
    %  %---------------------------------------PARM descriptor------------------%
    fwrite(fid,'PARM','char');
    fwrite(fid,104,'int32');
    nom='EV      ';
    fwrite(fid,nom,'char');
    vide_40='                                        ';
    fwrite(fid,vide_40,'char');
    vide_8='        ';
    fwrite(fid,vide_8,'char');
    fwrite(fid,1,'int16');
    fwrite(fid,1,'int16');
    fwrite(fid,0.000361,'float32');
    fwrite(fid,88,'int16');
    fwrite(fid,0,'int16');
    fwrite(fid,80,'int16');
    fwrite(fid,2,'int16');
    fwrite(fid,'NONE    ','char');
    fwrite(fid,0,'float32');
    fwrite(fid,100,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,-32768,'int32');
    ftell(fid)
    
    
    
    %  %---------------------------------------CELV descriptor------------------%
    fwrite(fid,'CELV','char');    
    fwrite(fid,6012,'int32');
    
    fwrite(fid,num_cell,'int32');
    fwrite(fid,0,'float32');
    for i=2:num_cell
        fwrite(fid,dist(i),'float32');
    end
    
    
    rajout=(1500-num_cell);
    for i=1:rajout
        fwrite(fid,0,'float32');
    end
    ftell(fid)
    
    %        
    %       
    %    %---------------------------------------CFAC descriptor------------------%
    %    
    fwrite(fid,'CFAC','char'); 
    fwrite(fid,72,'int32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    fwrite(fid,0,'float32');
    ftell(fid)
    
    
    
    %       %---------------------------------------SWIB descriptor------------------%
    
    fwrite(fid,'SWIB','char'); 
    fwrite(fid,40,'int32');
    fwrite(fid,radar_name,'char');
    fwrite(fid,sw_num0,'int32');
    fwrite(fid,num_ray,'int32');
    fwrite(fid,start_angle,'float32');
    fwrite(fid,stop_angle,'float32');
    fwrite(fid,fixed_angle,'float32');
    fwrite(fid,0,'int32');
    ftell(fid)
    
    
    
    %       %---------------------------------------RYIB descriptor------------------%
    
    for i=1:num_ray 
        fwrite(fid,'RYIB','char');
        fwrite(fid,44,'int32'); 
        fwrite(fid,sw_num0,'int32'); 
        fwrite(fid,julian_day,'int32'); 
        fwrite(fid,hr,'int16');
        fwrite(fid,min,'int16');
        fwrite(fid,sec,'int16');
        fwrite(fid,0,'int16');
        fwrite(fid,az(i),'float32');
        fwrite(fid,0,'float32');
        fwrite(fid,16.5,'float32');
        fwrite(fid,18,'float32');
        fwrite(fid,0,'int32');
        
        fwrite(fid,'ASIB','char'); 
        fwrite(fid,80,'int32');
        fwrite(fid,radar_long,'float32');
        fwrite(fid,radar_lat,'float32');
        fwrite(fid,radar_alt,'float32');
        fwrite(fid,radar_alt,'float32');
        
        fwrite(fid,0,'float32');
        fwrite(fid,0,'float32');
        fwrite(fid,0,'float32');
        fwrite(fid,0,'float32');
        fwrite(fid,0,'float32');
        fwrite(fid,0,'float32');
        fwrite(fid,0,'float32');
        fwrite(fid,az(i),'float32');
        
        fwrite(fid,0,'float32');
        fwrite(fid,0,'float32');
        fwrite(fid,0,'float32');
        fwrite(fid,0,'float32');
        
        fwrite(fid,-32768,'float32');
        fwrite(fid,-32768,'float32');
        
        %ftell(fid)
        
        fwrite(fid,'RDAT','char'); 
        
        
        ray=data2(:,i);
        u_nan=isnan(ray);
        u_nan=find(u_nan==1);
        
        ray=ray(1:u_nan(1)-1);
        %cd ('C:\Documents and Settings\sylvie lorsolo\My Documents\research\programs\');
        
        %[recomp_ray]=recompress(ray);
        %cd ('C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\data\dorade\')
        
        L_data_block=length(ray)*2+16;
        fwrite(fid,L_data_block,'int32');
        fwrite(fid,nom,'char');
        fwrite(fid,ray,'int16');
    end
    fwrite(fid,'NULL','char');
    fwrite(fid,8,'int32');
    ftell(fid)
    
    %---------------------------------------RKTB
    fwrite(fid,'RKTB','char');
    fwrite(fid,7712,'int32'); 
    
    for i=1:length(valeur)
        fwrite(fid,valeur(i),'int16');
    end
    
    fwrite(fid,'SEDS','char');
    fwrite(fid,1812,'int32');
    for i=1:length(valeur2)
        fwrite(fid,valeur2(i),'int32');
    end
    
    ftell(fid)  
    
    fclose(fid)
end
