% script that is used to process the email file that has been saved after
% downloading it by a perl script.   It produces a hex listing of the
% attached base64 data  sent by the float to produce an intermediate listing, update the netcdf file, and send a listing to AOML.
% Jan '07 WBO

% this script used to reprocess hex files to make intermiediate listings,
% etc

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

eval(['cd ' ARGOHOME]);
startup

% define Argo directories
setargo
ARGOIRIDIUM = fullfile(ARGOHOME,'iridium');

eval(['cd ' fullfile(ARGOHOME, 'tmp')]); % work in tmp directory so that data files, etc are there
% Have assumed that this file is launched from $ARGOHOME which has a
% startup.m file
NDIR = fullfile (ARGOHOME, 'DATA'); % directory containing netcdf files

%% information that will be filled out by proc_mail.sh using sed
hfile = '/Volumes/DVP/argo/iridium/300034012000390/0390_061230091107.hex'; % Hex file
ifile = '/Volumes/DVP/argo/iridium/300034012000390/0390_061230091107_inter.lis'; % Intermediate file
IMEI =   '300034012000390';
theFile = qfltdict ('PLAT', IMEI, 'FILE'); % file name for this float
if isempty(theFile)
    disp(['No entry found in fltdict for IMEI: ' IMEI])
    theFile = IMEI;
    vers = '120';
    sensors = 'STP'; % if no information, assume standard float
    Active = 0; % assume the float is not yet deployed
else
    theInst = qfltdict ('PLAT', IMEI, 'INST');
    und = findstr(theInst,'_');
    % get seascan version number
    vers = str2num(theInst(und-3:und-1))/100;
    theCtd = theInst(und+1:end);
    if ~isempty(findstr(theCtd, 'DO'));
        sensors = 'STPO';
    else
        sensors = 'STP';
    end
    Active = str2num(qfltdict ('PLAT', IMEI, 'ACT')); % active flag
end
%thePath = qfltdict ('PLAT', IMEI, 'PATH');
nfile = fullfile(NDIR,[theFile '.nc']);

%% Process hex file and if all data available make intermediate file
w = dir(ifile);
if ~isempty(w)
    eval(['!rm ' ifile]);
end

% set up description of drift data and profile interval
% temporary fix for acoustic DOXY float
if strcmp(IMEI, '300034012600050')
    rafos_sched = 'pppp'
else
    rafos_sched = 'pppp'; % normal ARGO mission with 4 get parameters every 2 days during drift phase
end

prof_interval = 10;

hstat = proc_hex(hfile, ifile, sensors, vers, rafos_sched);
if ~hstat % status flag indicates if all data was avaiable, beacon mode, or ack message
    quit
end

%% Check to see if the float is active before loading netcdf file and
%% outputing .phy files

if Active
    %% write out netcdf file
    disp(['Updating netcdf file: ' nfile])

    w = dir(nfile);
    if isempty(w)
        disp(['ERROR in proc_mail.m, netcdf file ' nfile ' does not exist'])
        quit
    end

    inter2cdf(ifile,nfile,sensors,vers,prof_interval); %Takes the intermediate file and puts data into netCDF file

    %% take new file and generate listings for AOML
    flt=netcdf(nfile);

    % write out listing of last profile for AOML
    prof = round(flt.last_profile(:));
    aoml_id = flt.DAC_internal_id(:); % AOML internal id
    aoml_id = strtrim(aoml_id); % strip off leading and following spaces
    imei = flt{'PTT'}(:);
    ok = regexp(reshape(imei,1,length(imei)),'[0-9]');
    imei = reshape(imei(ok),1,length(ok));
    i1 = imei(end-4:end); % get last 5 digits of imei number
    % assume that 1st profile is at start of mission, so for AOML, prof = prof-1
    pfile = fullfile(ARGOIRIDIUM,'PHY',sprintf('%s_%s_%03d.phy',aoml_id,i1,prof-1)); % filename = aaaa_nnnnn_ppp.phy
    nc2phy(flt,prof,pfile, sensors);
    % send listing to AOML
%    eval(['!/sw/bin/ncftpput -u argoin -p \#go8Way\! 192.111.123.154 /phod/incoming/ARGO_FTP/argo/real-time/whoi ' pfile]) % send copy of file to AOML
%    eval(['!/sw/bin/ncftpput -u argo -p aX#f5aW\!gfPO\& 65.210.29.38 /real-time/whoi ' pfile]) % send copy of file to ARGOSINC
    %% Close netcdf file
    cmd = sprintf('echo "Mail file %s processed for IMEI %s\nFile %s sent to AOML" | mail -s PROC_MAIL bowens@whoi.edu bcarr@whoi.edu',hfile, IMEI, pfile);
%    system(cmd);
    close(flt);
end

%% done
quit
