% flux_eval_VOCALS2008
% VOCALS 2008 :: 2008-10-30 :: Simon de Szoeke
% Processes daily flux files, makes daily dianostic plots and intermediate
% data files. Averages to 5 and 30 minutes and writes files.
% Calls:
%  read_parameters
%  read_pc_day;
%  read_scs_day;
%  read_gps_day;
%  read_son_day;
%  read_motion (optionally)
%  read_co2
%  read_wave
%  timatch_5;
%  timatch_30;
clear;
close('all');
fclose('all');

read_parameters; %Reading parameter files
plotit=true; % for making plots. Some plots inseparable from analyses will still be made.
prtit=true;  % for printing plots--ineffective unless plotit is also true
saveit=true; % for saving data files
clearit=true;% for clearing data sometimes to save memory
backup=false;% backup somthing to hard disk

dirodir=dir([way_raw_data_flux 'day*']);
ddd=input(['Evaluate last yearday ' dirodir(end).name(end-2:end) ' or input other yearday: ']);
if isempty(ddd)
    ddd=str2double(dirodir(end).name(end-2:end));
end

close all

read_pc_day;
read_scs_day;
read_gps_day;
 close all;
read_son_day;
 close all;
%read_motion; % slow but works; commented to save memory on VOCALS
licor_prefix='lic';
%read_co2; % What is the state of VOCALS CO2 measurement?
%read_wave % slow but works, only makes diagnostic plots of a subset of data

timatch_5;
timatch_30;

if backup
    backup_to_harddisk_rhb;
end;

% compute radiation with model
jjx=min(length(latm),length(qvais));
rlclr=(.52+.13/60*abs(latm(1:jjx))+(.082-.03/60*abs(latm(1:jjx))).*sqrt(qvais(1:jjx))).*(5.67e-8*(Tvais(1:jjx)'+273.15).^4);

clear jdy;
jdys=jd_pc';
latd=nanmean(latm);
lond=nanmean(lonm);
qa=qvais; % humidity needed for solar model
iv=ones(length(qa),1)*NaN;

% solar model
epic01_Rsclear_1;

if plotit
    figure;plot(jd_pc,Rhvais,'.b',jd_scs,rhm,'.g');xlim([ddd ddd+1])
    title(['Relative Humidity in %. ', cruise, year]);
    xlabel(['yearday', year]);
    legend('PSD', 'Ship');
    if prtit; print('-djpeg90 ',[way_images_flux '\RH_' num2str(ddd) '.jpg']);xlim([ddd ddd+1]); end

    figure;plot(jd_pc,Tsea,'b',jd_scs,tsgm,'.g');xlim([ddd ddd+1])
    title(['Sea Temperature (C). ', cruise, year]);
    xlabel(['yearday ', year]);
    legend('PSD', 'Ship');
    if prtit; print('-djpeg90 ',[way_images_flux '\SST_' num2str(ddd) '.jpg']);xlim([ddd ddd+1]); end

    figure;plot(jd_pc,Tvais,'b',jd_scs,tam,'.g');xlim([ddd ddd+1])
    title(['Air Temperature (C). ', cruise, year]);
    xlabel(['yearday ', year]);
    legend('PSD','Ship');
    if prtit; print('-djpeg90 ',[way_images_flux '\Tair_' num2str(ddd) '.jpg']);xlim([ddd ddd+1]); end

    ii=find(~isnan(rlclr));
    figure;plot(jd_pc,pirm,'b',jd_scs,irm,'g',jd_scs(ii),rlclr(ii),'r');xlim([ddd ddd+1])
    title(['IR Flux (W/m^2). ', cruise, year]);
    xlabel(['yearday ', year]);
    legend('PSD', 'Ship', 'Clearsky');
    if prtit; print('-djpeg90 ',[way_images_flux '\IRflux_' num2str(ddd) '.jpg']);xlim([ddd ddd+1]); end

    ii=find(~isnan(Rscl));
    figure;plot(jd_pc,pspm,'.b',jd_scs,solarm,'.g',jd_pc(ii),Rscl(ii),'r');xlim([ddd ddd+1])
    title(['Solar Flux (W/m^2). ', cruise, year]);
    xlabel(['Time of day (', year, ')']);
    legend('PSD', 'Ship','Clearsky');
    if prtit; print('-djpeg90 ',[way_images_flux '\SOLflux_' num2str(ddd) '.jpg']);xlim([ddd ddd+1]); end
end % if plotit
disp(['END of flux_eval_VOCALS2008 yearday ' jd '.']);
