%BAOPLT07
% Plot monthly precip data from BAO data loggers

clear
close all
outpath  = 'Z:\bao\Tower\Processed_Images\monthly\';

pltflg = 1;  % save data to jpg's flag  1= yes 0 = no

name10 =   'Z:\bao\Tower\Processed\monthly\BAO_010_200712.dat';
name100 =  'Z:\bao\Tower\Processed\monthly\BAO_100_200712.dat';
name300 =  'Z:\bao\Tower\Processed\monthly\BAO_300_200712.dat';
ym = input('Input year month to process (200710) ','s');
name10(40:45) = ym;
name100(40:45) = ym;
name300(40:45) = ym;

%%%%%%%10m data
%1    2   3   4     5     6      7      8       9      10        11     12     13    14
%ID  YR  YD  hhmm  T      RH     WSv   WSs     Dir    WDt       Bat    press  precip WDT
%10,2007,165,1405,15.095,50.858,1.810,-999.000,238.61,-999.0000,13.515,843.01,0.000,212.610
%10,2007,165,1406,15.115,50.448,1.692,-999.000,245.47,-999.0000,13.514,842.98,0.000,219.470

%%%%%%%100m data
%1    2   3   4     5     6      7      8       9      10        11     12     13    14
%ID  YR  YD  hhmm  T      RH     WSv   WSs     Dir    WDt       Bat    press  precip WDT
%100,2007,244,5,29.848,19.414,0,0,0,0,13.132
%100,2007,244,6,29.849,19.333,0,0,0,0,13.132


%%%%300m data
%"TOA5","CR1000","CR1000","2846","CR1000.Std.11","CPU:060516b_Met.CR1","10044","wkt30m"
%"TIMESTAMP","RECORD","SE1_SONICWS_Avg","SE2_SONICWD_Avg","SE2_SONICWD_Std","SE1_SONICWS_Std","SE3_SONICTEMP_Avg","D3_PRTRES_Avg","D3_PRTTEMPAPPROX_Avg","SE4_HMPTEMP_Avg","SE9_HMPRH_Avg","P1_AIRFLOW_Avg","SE10_ACPOWER","BT_BATVOLT_Min"
%"TS","RN","mV","Deg","Deg","mV","mV","","Deg C","Deg C","%","Counts","mV","Volts"
%"","","WVc","WVc","WVc","Std","Avg","Avg","Avg","Avg","Avg","Avg","Smp","Min"
%"2007-10-01 00:00:00",312159,5.06,201.1,8.43,0.523,15.68,106.923,17,17.91,23.68,0,110.7,13.26,17.8,163.1
%"2007-10-01 00:00:30",312160,5.008,183.5,3.734,0.294,15.49,106.9227,17,17.9,25.13,0,110.7,13.26,17.8,145.5

data10 = load(name10);
data100 = load(name100);

nmax = 90000;
year = zeros(1,nmax);
month = zeros(1,nmax);
day = zeros(1,nmax);
hour = zeros(1,nmax);
minute = zeros(1,nmax);
sec = zeros(1,nmax);
yday = zeros(1,nmax);
bao300 = zeros( nmax, 15);

f300 = fopen(name300,'r');
%load f:\BAO_CR10\data\bao10.dat
%read in 4 header lines
tmp=fgetl(f300);
tmp=fgetl(f300);
tmp=fgetl(f300);
tmp=fgetl(f300);
n = 0;
while ~feof(f300)
    %for x = 1:10000
    n = n + 1;
    temp=fgetl(f300);
    year(n)= str2double(temp(2:5));
    month(n)= str2double(temp(7:8));
    day(n)= str2double(temp(10:11));
    hour(n)= str2double(temp(13:14));
    minute(n)= str2double(temp(16:17));
    sec(n)= str2double(temp(19:20));
    lt= length(temp);
    % strip off date/time info from first part of each line
    s1 = temp(23:lt);
    % convert each , to a blank space
    s1 = strrep( s1, ',', ' ' );

    [data]=sscanf(s1,'%f') ;
    bao300(n,1) = data(2,1); % WS ms Sonic
    bao300(n,2) = data(3,1); % WD deg Sonic
    bao300(n,3) = data(4,1); % WD  std deg Sonic
    bao300(n,4) = data(5,1); % WS  STD Sonic
    bao300(n,5) = data(6,1); % Temp deg C Sonic
    bao300(n,6) = data(7,1); % PRT temp resistance
    bao300(n,7) = data(8,1); % PRT temp deg C
    bao300(n,8) = data(9,1); % HMT Temp Deg C
    bao300(n,9) = data(10,1); % RH
    bao300(n,10) = data(11,1); % airflow
    bao300(n,11) = data(12,1); % VAC
    bao300(n,12) = data(13,1); % Bat VDC
    bao300(n,13) = data(14,1); % PRT corrected deg C
    bao300(n,14) = data(15,1); % Dir true

end
yd300 = datenum(year,month,day,hour,minute,sec)-datenum(2006,12,31);

hhmm = data10(:,4);
hh = fix(hhmm/100);
mn = hhmm - (hh*100);
data10(:,3) = data10(:,3) + (((mn/60.) + hh)/24); %Julian day
clear hhmm hh mm

hhmm = data100(:,4);
hh = fix(hhmm/100);
mn = hhmm - (hh*100);
data100(:,3) = data100(:,3) + (((mn/60.) + hh)/24); %Julian day
clear hhmm hh mm

%10m prop vane was on the north boom until Aug 3 ~1600 UTC when it was moved to
%the south boom
% N is 154  to correct we add 206 and correct for > 360
ld = length(data10);
jdb = fix(data10(1,3));
jde = fix(data10(ld,3))+1;

ld2 = length(data100);
jdb2 = fix(data100(1,3));
jde2 = fix(data100(ld2,3))+1
%CSD experiment
%jdb = 208;
%jde = 222;
ct = 0;

if 1 %skip plotting

    figure(1)
    clf
    subplot(2,1,1),plot(data10(:,3),data10(:,7))
    hold
    %subplot(2,1,1),plot(data100(:,3),data100(:,7),'k')    
    subplot(2,1,1),plot(yd300,bao300(:,1),'r')    
    title(name10(40:45))
    ylabel('Speed (m/s)')
    xlim([jdb,jde])
    elim = max(data10(:,7))+2;
    ylim([0 elim])
    legend('10m','300m');

    %axis([110,116,820,850])
    subplot(2,1,2),plot(data10(:,3),data10(:,14))
       hold
    %subplot(2,1,1),plot(data100(:,3),data100(:,14),'k')
    subplot(2,1,2),plot(yd300,bao300(:,14),'r')
    xlabel('Year Day')
    ylabel('Dir (True)')
    xlim([jdb,jde])
    ylim([0 360])
    %axis([110,116,0,.5])
    if pltflg
        print_buffer = [outpath 'BAO10300.WS.' ym '.jpg'];
        print('-djpeg90 ', print_buffer);
    end
    
    figure(2)
    clf
    subplot(2,1,1),plot(data10(:,3),data10(:,5))
    hold
        subplot(2,1,1),plot(data100(:,3),data100(:,5),'k')
    subplot(2,1,1),plot(yd300,bao300(:,13),'r')    
    title(name10(40:45))
    ylabel('Temp (C)')
    xlim([jdb,jde])   
    blim = min(data10(:,5))-2;
    elim = max(data10(:,5))+2;
    ylim([blim elim])
    legend('10m','100m','300m');

    subplot(2,1,2),plot(data10(:,3),data10(:,6))
       hold
    subplot(2,1,2),plot(data100(:,3),data100(:,6),'k')       
    subplot(2,1,2),plot(yd300,bao300(:,9),'r')
    xlabel('Year Day')
    ylabel('RH (%)')
    xlim([jdb,jde])
    ylim([0 105])
    if pltflg
        print_buffer = [outpath 'BAO10100300.TRH.' ym '.jpg'];
        print('-djpeg90 ', print_buffer);
    end
end % skip plotting


