% proc_output.m
% proc_output.m is a matlab script used to process data in ARGO NetCDF 
% files to produce graphical displays of various parameters.  The output
% is currently in PostScript form and is converted to .jpg for use
% with a web browser.
% The script is invoked by the shell script output.sh, substituting
% (using sed) the float identifier for the variable YY.
% 
%    Version 1.1          May 2002          R. Goldsmith
%            1.1 to handle requests from either ORBCOMM or generic ARGO.
%                Effected by looking at USER identifier so be careful.
%    Version 1.2          Nov 2003          R. Goldsmith
%            1.2 to separate identifier for ORBCOMM to use WMO.
%
warning off

[s, ARGOHOME] = system('echo -n $ARGOHOME'); % home directory for argo

% path information will be set in startup.m
% Directory names will also be set in startup.m

%% change to main argo directroy, define path, etc
eval(['cd ' ARGOHOME]);
startup

% define Argo directories
setargo

%% set platform ID 
%ID = 'theID'; % set float id from unix script using sed
ID = '300034012609580'; % set float id from unix script using sed

%% Something to tell us where we are in the log.
fprintf (1, '\nAt %d/%02d/%02d %02d:%02d:%2.0f, Processing float %s to produce Plots\n', ...
    clock, ID);

%% define directories
theFile = qfltdict ('PLAT', ID, 'FILE'); % file name for this float

MDIR = fullfile(ARGOHOME,'tmp');
NDIR = fullfile (ARGOHOME, 'DATA'); % directory containing netcdf files
HDIR = fullfile (ARGOHOME,'Sites'); % HTML accessible directory
PDIR = fullfile(HDIR,'PLOTS',theFile);
PSDIR = fullfile(PDIR,'ps');
UPDIR = fullfile(UARGOHOME,'public_html','PLOTS',theFile);

% check to make sure directories exist
if exist(PDIR) ~= 7
    eval(['!mkdir ' PDIR]);
    eval(['!chmod 777 ' PDIR]);
end
if exist(PSDIR) ~= 7
    eval(['!mkdir ' PSDIR]);
    eval(['!chmod 777 ' PSDIR]);
end
%###if exist(UPDIR) ~= 7
%###    eval(['!mkdir ' UPDIR]);
%###    eval(['!chmod 777 ' UPDIR]);
%###end



%% change directory into MDIR so that all the .gmt files are written into
% this directory
eval(['cd ' MDIR]);

%% open netcdf file
cdf_file = fullfile(NDIR,[theFile '.nc']);
flt = netcdf(cdf_file);

%% make profile plots including all profiles
% remove previous versions of the .gmt files
if exist(fullfile(pwd,'plt_T.gmt'))
    eval('!rm -f plt_T.gmt')
end
if exist(fullfile(pwd,'plt_S.gmt'))
    eval('!rm -f plt_S.gmt')
end
if exist(fullfile(pwd,'plt_traj.gmt'))
    eval('!rm -f plt_traj.gmt')
end
if exist(fullfile(pwd,'plt_TS.gmt'))
    eval('!rm -f plt_TS.gmt')
end

plt_profiles(flt);
% run gmt files and copy output to appropriate files
gmtscript = 'plt_T.gmt';
gmtfile = 'gmt_T.ps';
plt_dis = '_tema';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

gmtscript = 'plt_S.gmt';
gmtfile = 'gmt_S.ps';
plt_dis = '_sala';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

gmtscript = 'plt_traj.gmt';
gmtfile = 'gmt_traj.ps';
plt_dis = '_posa';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

gmtscript = 'plt_TS.gmt';
gmtfile = 'gmt_TS.ps';
plt_dis = '_ts_a';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

%% make profile and trajectory plots for last 5 profiles
% remove previous versions of the .gmt files
if exist(fullfile(MDIR,'plt_T.gmt'))
    eval('!rm -f plt_T.gmt')
end
if exist(fullfile(MDIR,'plt_S.gmt'))
    eval('!rm -f plt_S.gmt')
end
if exist(fullfile(pwd,'plt_traj.gmt'))
    eval('!rm -f plt_traj.gmt')
end
if exist(fullfile(pwd,'plt_TS.gmt'))
    eval('!rm -f plt_TS.gmt')
end

plt_profiles(flt,-5);

% run gmt files and copy output to appropriate files
gmtscript = 'plt_T.gmt';
gmtfile = 'gmt_T.ps';
plt_dis = '_temb';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

gmtscript = 'plt_S.gmt';
gmtfile = 'gmt_S.ps';
plt_dis = '_salb';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

gmtscript = 'plt_traj.gmt';
gmtfile = 'gmt_traj.ps';
plt_dis = '_posb';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

gmtscript = 'plt_TS.gmt';
gmtfile = 'gmt_TS.ps';
plt_dis = '_ts_b';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

%% plot changes on theta surfaces
if (exist(fullfile(pwd,'plt_thchng.gmt')))
    eval('!rm -f plt_thchng.gmt')
end
plot_theta_values(flt);

% run gmt files and copy output to appropriate files
gmtscript = 'plt_thchng.gmt';
gmtfile = 'gmt_thchng.ps';
plt_dis = '_th_ch';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

%% plot battery voltages
if (exist(fullfile(pwd,'plt_eng.gmt')))
    eval('!rm -f plt_eng.gmt')
end

plt_eng(flt);

% run gmt files and copy output to appropriate files
gmtscript = 'plt_eng.gmt';
gmtfile = 'gmt_eng.ps';
plt_dis = '_eng';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

%% plot drift data for all profiles
if (exist(fullfile(pwd,'plt_drft.gmt')))
    eval('!rm -f plt_drft.gmt')
end

plt_drft_data(flt);

% run gmt files and copy output to appropriate files
gmtscript = 'plt_drft.gmt';
gmtfile = 'gmt_drft.ps';
plt_dis = '_drft_a';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

%% plot drift data for last 5 profiles
if (exist(fullfile(pwd,'plt_drft.gmt')))
    eval('!rm -f plt_drft.gmt')
end

plt_drft_data(flt,-5);

% run gmt files and copy output to appropriate files
gmtscript = 'plt_drft.gmt';
gmtfile = 'gmt_drft.ps';
plt_dis = '_drft_b';
conv_arg = ' -crop 0x0 -border 10x10 ';
run_gmt_file

%% make contour plots of T and S
if (exist(fullfile(pwd,'plt_t_cont.gmt')))
    eval('!rm -f plt_t_cont.gmt')
end
if (exist(fullfile(pwd,'plt_s_cont.gmt')))
    eval('!rm -f plt_s_cont.gmt')
end

plt_TS_cont(flt);

gmtscript = 'plt_t_cont.gmt';
gmtfile = 'gmt_t_cont.ps';
plt_dis = '_t_cont';
conv_arg = ' -rotate 90 -crop 0x0 -border 10x10 ';
run_gmt_file

gmtscript = 'plt_s_cont.gmt';
gmtfile = 'gmt_s_cont.ps';
plt_dis = '_s_cont';
conv_arg = ' -rotate 90 -crop 0x0 -border 10x10 ';
run_gmt_file

%% clean up files used to make plots
eval(['!rm ' theFile '_?.dat']);
eval('!rm traj.cpt')

%% close netcdf file
close(flt);

%% exit
quit
