% first check if sitename file exist
% this is to read in one NAME 2004 QC'd profiler data (Dave White)
% to get the header info
%  and write out a file with the mean composite profiles
% 10/05/2005 DEW
clf
clear
p2b = 1;

warning off MATLAB:divideByZero  % turn off warning message

pathdatap = 'c:\data\name04\profiler\QC';
outpath  = 'c:\data\name0404\results';
eval(['cd ', pathdatap]);


qc2 = dir('*.w1b');
[m2 n2] = size(qc2);
p = length(qc2);
[num] = num2str(p);
disp('Number of qc w1b files : '); disp( [num]);

%%%%%%%%%%%%%%%%%% last file == k %%%%%%%%%%%%%%%
ws=1;
wp=m2;
ct = 0;
outf = 'c:\data\name04\results\name04_rwp_comp.w1b';
% load composite data into data lines
for w2 = ws:1
    eval(['cd ', pathdatap]);

    kt = 0;
    %  Filename rbc04185.w1bcd

    fid3 = fopen(qc2(w2).name);
    fid4 = fopen(outf,'w');
    qc2(w2).name

    teof = feof(fid3);
    if teof ~= 0
        break;
    end
    % LOOP through a single file
    for k=0:23
        ct = ct + 1;
        %
        %        %%%%%%%%%% DEW qc file header loop
        %CDF Type:  Wind            Program:  NOAA/ETL Etl2Lap        Version:  2.0
        %
        %Project:               NOAA/ETL RASS Tv              (ETL Processed)
        %Station:               name_altair
        %Site ID:               name_altair
        %
        %Date:                  07/02/04        Julian Day:         184
        %Filename:              nam04184.w1b    Validation Level:   1.0
        %Created by:            Etl2Lap 2.0     Created on:         03/25/05  1615
        %Elevation (m msl):     5               Elevation (ft msl): 16
        %Lat (dec deg):         19.00           Long (dec deg):     104.00
        %UTMN (km):             19.00           UTME (km):          104.00
        %Time Zone:             UTC             Diff. to UTC (hr):  0
        %Avg. Int. (min):       60              Time Convention:    End
        %
        %Mode Number:           1               Mode Title:         56-m Winds
        %Pulse Len. (m):        60              Spacing (m):        56
        %
        %Ant. Directions:         V1   V2   N    S    E    W
        %Ant. Azimuth (deg):     999  999  999  999  999  999
        %Ant. Elev. (deg):      99.0 99.0 99.0 99.0 99.0 99.0
        %
        %QC codes apply to wind speed and direction
        %
        %QC Code Definition:    0=Valid
        %                       1=Valid, no vertical correction
        %                       2=Unassigned
        %                       3=Unassigned
        %                       4=Unassigned
        %                       5=Unassigned
        %                       6=Unassigned
        %                       7=Suspect
        %                       8=Invalid
        %                       9=Missing or failed consensus
        %
        %Data Code Definition: -940=Failed QC
        %                      -950=Failed consensus
        %                      -960=Exceeded Nyquist Velocity
        %                      -980=Flagged by reviewer
        %                      -999=Missing or not reported
        %
        %Comments:
        %
        %
        %
        %
        %Time    # of Gates  # of Beams Parameter Changes (0=no/1=yes)
        %HHMM  NNN           N          N
        %QC   Height    WS     WD     u      v      w    Vr-V1  Vr-V2  Vr-N   Vr-S   Vr-E   Vr-W   SNR-V1 SNR-V2 SNR-N  SNR-S  SNR-E  SNR-W
        %Code( m-msl) (m/s)  (deg)  (m/s)  (m/s)  (m/s)  (m/s)  (m/s)  (m/s)  (m/s)  (m/s)  (m/s)   (dB)   (dB)   (dB)   (dB)   (dB)   (dB)
        %0100     48         3          0
        %0    151      6.0    72.   -5.7   -1.9   -6.5   -6.5 -999.0 -999.0 -999.0 -999.0 -999.0     22   -999   -999   -999   -999   -999
        %0    208      5.7    69.   -5.3   -2.0   -6.6   -6.6 -999.0 -999.0 -999.0 -999.0 -999.0     31   -999   -999   -999
        % 0      151    1.8   228.    1.3    1.2    0.0    0.0 -999.0 -999.0 -999.0 -999.0 -999.0     19   -999   -999   -999   -999   -999
        % 0      208    2.7   244.    2.4    1.2    0.0    0.0 -999.0 -999.0 -999.0 -999.0 -999.0     16   -999   -999   -999   -999   -999
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % scan in header info....
        if k == 0
            for i = 1:51
                dumhdqc=fgetl(fid3);
                %fprintf(outfile1, '%s, %s, %s, %s, %10.4f, %10.4f, %10.4f, %8.0f\n', fnamet, fnamew, time_str, baltmn, jdatb, jdatrb, jdatsur,nread);
                if i == 3
                    dumhdqc(24:39) = 'Name 2004       ';
                end
                if i == 8
                    dumhdqc(24:36) = 'Composite.w1b';
                end
                if i == 9
                    dumhdqc(24:38) = 'DEW MATLAB     ';
                end

                %save mins
                if i == 7
                    dumhdqc(24:31) = '07/12/04';
                end
                if i == 51
                    timht = dumhdqc;  % save time/ht line
                    nhts = str2num(dumhdqc(10:11));
                    hp2 = str2num(dumhdqc(1:2));
                    hh2 = (dumhdqc(1:2));
                    min2 = (dumhdqc(3:4));
                    dumhdqc(2:2) = num2str(k,'%1.0f'); % load hr into header line
                end
                % Write out header info
                fprintf(fid4, '%s\n', dumhdqc);
            end
        end % reading in headers first time through
        % read in single time line between profiles

        if k > 0
           % dumhdqc=fgetl(fid3);
           % nhts = str2num(dumhdqc(10:11));
           % hp = str2num(dumhdqc(1:2));
           % hh = (dumhdqc(1:2));
           % min(1:2) = (dumhdqc(3:4));
            %% write out time and ht line for next profile
            if k <= 9
            dumhdqc(2:2) = num2str(k,'%1.0f'); % load hr into header line
            end
            if k > 9
            dumhdqc(1:2) = num2str(k,'%2.0f'); % load hr into header line
            end
            fprintf(fid4, '%s\n', dumhdqc);

        end
        %%Load composite profile data
        load c:\data\name04\name04_hrly_comp60  % d_s d_d d_u d_v rwp1h test file 24 profiles 55 hts each speed only
        %% get 1 data line to put composite profile into
        flgqc1 = ' 3                                      -950.0 -950.0 -999.0 -999.0 -999.0 -999.0 -999.0   -950   -999   -999   -999   -999   -999';
        flgqc2 = ' 3          -950.0  -950. -950.0 -950.0 -950.0 -950.0 -999.0 -999.0 -999.0 -999.0 -999.0   -950   -999   -999   -999   -999   -999';

        for i = 1:55
           % doutqc=fgetl(fid3)

            doutqc = flgqc2;  %load flag data line 1
                htmp  = num2str(rwp1h(i,1)*1000,'%8.0f'); % ht in meters
                lh = length(htmp);
                bs = 11 - (lh-1);
                doutqc(bs:11) = htmp; %ht

            if d_s(i,ct) >= 0
            doutqc = flgqc1; %load flag data line 2
            
                htmp  = num2str(rwp1h(i,1)*1000,'%8.0f'); % ht in meters
                lh = length(htmp);
                bs = 11 - (lh-1);
                doutqc(bs:11) = htmp; %ht


                htmp  = num2str(d_s(i,ct),'%7.1f'); % spd
                lh = length(htmp);
                bs = 18 - (lh-1);
                doutqc(bs:18) = htmp; % spd

                htmp  = num2str(d_d(i,ct),'%7.0f'); % dir
                lh = length(htmp);
                bs = 24 - (lh-1);
                doutqc(25) = '.'; 
                doutqc(bs:24) = htmp; % dir

                htmp  = num2str(d_u(i,ct),'%7.1f'); % u
                lh = length(htmp);
                bs = 32 - (lh-1);
                doutqc(bs:32) = htmp; % u
                
                htmp  = num2str(d_v(i,ct),'%7.1f'); % v
                lh = length(htmp);
                bs = 39 - (lh-1);
                doutqc(bs:39) = htmp; % v
            end

            % write out data line
            fprintf(fid4, '%s\n', doutqc);
        end

    end %looping through times in qc files
    fc=fclose(fid3); %close qc file
    fc=fclose(fid4); %close new output file

end % loop through qc files

%save c:\data\NAME04\results\NAME04_RWP2 trwp1 jul_day_rwp1 year month day hour minutes;

cd ..\..
