%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%                            OUTPACE 2015                           %%%%
%%%%    Load_outpace_met.m                                              %%%
%%%%        Description: This script takes the netcdf files found in    %%%
%%%%        DATABASE/METEO/L0, opens them, and writes out a .txt ascii  %%%
%%%%        that can be read by excel. The output is Time (UTC), air    %%%
%%%%        pressure (mBar), heatflux (solar radiation, W/m2), moisture %%%
%%%%        (%), relative air heading (degrees), relative air speed     %%%
%%%%        (knots), the water and air temperature (deg C), true air    %%%
%%%%        velocity (knots), dewpoint (deg C), true air heading (degr- %%%
%%%%        ees)                                                        %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pathL0 = '/mnt/OUTPACE/DATABASE/METEO/L0';
cd(pathL0); % Tested on deVerneil laptop, MATLAB 2014b, MacOSX 10.11.1

% seems like BATOS, Gille1 and Gille2 files in hard drive copies all give
% the same output, so we're using the BATOS files in L0...

dirinfo = dir;

numfiles = length(dirinfo);

airpressure = [];
heatflux = [];
moisture = [];
relairheading = [];
relairspeed = [];
watertemp = [];
airtemp = [];
trueairspeed = [];
dewpoint = [];
trueairheading = [];
time = [];
for j = 1:numfiles
    if length(dirinfo(j).name) == 40
        tempairpres = ncread(dirinfo(j).name,'airpressure');
        tempheatflx = ncread(dirinfo(j).name,'heatflux');
        tempmois = ncread(dirinfo(j).name,'moisture');
        temprelairhead = ncread(dirinfo(j).name,'relairheading');
        temprelairsped = ncread(dirinfo(j).name,'relairspeed');
        tempwatertemp = ncread(dirinfo(j).name,'watertemp');
        tempairtemp = ncread(dirinfo(j).name,'airtemp');
        temptrueairspd = ncread(dirinfo(j).name,'trueairspeed');
        tempdewpt = ncread(dirinfo(j).name,'dewpoint');
        temptruairhead = ncread(dirinfo(j).name,'trueairheading');
        temptime = ncread(dirinfo(j).name,'time');
   
        % Convert into matlab time...
        temptime = datenum(1899,12,30,0,0,0) + temptime;
        
        airpressure = [airpressure tempairpres'];
        heatflux = [heatflux tempheatflx'];
        moisture = [moisture tempmois'];
        relairheading = [relairheading temprelairhead'];
        relairspeed = [relairspeed temprelairsped'];
        watertemp = [watertemp tempwatertemp'];
        airtemp = [airtemp tempairtemp'];
        trueairspeed = [trueairspeed temptrueairspd'];
        dewpoint = [dewpoint tempdewpt'];
        trueairheading = [trueairheading temptruairhead'];
        time = [time temptime'];
    
    end
end

clear temp*

airpressure = airpressure(54:60:end);
heatflux = heatflux(54:60:end);
moisture = moisture(54:60:end);
relairheading = relairheading(54:60:end);
relairspeed = relairspeed(54:60:end);
watertemp = watertemp(54:60:end);
airtemp = airtemp(54:60:end);
trueairspeed = trueairspeed(54:60:end);
dewpoint = dewpoint(54:60:end);
trueairheading = trueairheading(54:60:end);
time = time(54:60:end);

fid = fopen('OUTPACE_met.txt','w');
fprintf(fid,'TIME\tAIRPRESS\tHEATFLUX\tMOISTURE\tRELAIRHEAD\tRELAIRVEL\tWATERTEMP\tAIRTEMP\tTRUEAIRVEL\tDEWPNT\tTRUEAIRHEAD\n');
for j = 1:length(time)
    fprintf(fid,[datestr(time(j)) '\t']);
    fprintf(fid,'%9.2f',airpressure(j));
    fprintf(fid,'\t');
    fprintf(fid,'%9.2f',heatflux(j));
    fprintf(fid,'\t');
    fprintf(fid,'%9.2f',moisture(j));
    fprintf(fid,'\t');
    fprintf(fid,'%9.2f',relairheading(j));
    fprintf(fid,'\t');
    fprintf(fid,'%9.2f',relairspeed(j));
    fprintf(fid,'\t');
    fprintf(fid,'%9.2f',watertemp(j));
    fprintf(fid,'\t');
    fprintf(fid,'%9.2f',airtemp(j));
    fprintf(fid,'\t');
    fprintf(fid,'%9.2f',trueairspeed(j));
    fprintf(fid,'\t');
    fprintf(fid,'%9.2f',dewpoint(j));
    fprintf(fid,'\t');
    fprintf(fid,'%9.2f',trueairheading(j));
    
    fprintf(fid,'\n');
end
fclose(fid);