%===============================================
%
% PLMR processing software
%
% This program takes the PLMR files and converts them into PLMR2SP-
% readable files using CCCP and then uses PLMR2SP to create RASP input
% files. 
% This is a stand alone program and not intended for distribution!!! 
%
% Author          Date           version
% C. Rüdiger      10-Nov-09      1.0
% C. Rüdiger      21-Nov-09      1.1    added 1_CubeTimes
%
%===============================================

function f_PLMR_extract

clear all
close all

%-----------------------------------
% set parameters
%-----------------------------------

[linux,preproc,ussr,p2sp,rasp,calib,anci,kml,plmr_pro,graphs,experi,geb,pol,res,chk,sve,plt,tpause]=c_input_arg;
[fpath,file_date,file_name_PLMR,file_name_RASP,file_name_RT,file_name_counts, ... 
    prosensing,experi,pol,calib, ...
    plt] ... 
    =c_file_defs;
[high_res_start,high_res_end,low_res_start,low_res_end, ...
    minE,maxE,minN,maxN,UTM,datum,s_bin, ...
    temp_corr, ...
    RTstart,RTend,GPS_offset, ...
    step,range_h,bins_h,range_v,bins_v,range_TIR] ...
    =c_flight_params;

file_path=fullfile(fpath,'Data',experi,'field',file_date,'CUBE');
file_path_batch=fullfile(fpath,'Data',experi,'field',file_date,'ESV','rcf');
file_path_INC=fullfile(fpath,'Data',experi,'field','ARA_inc');

if (ussr)
%-----------------------------------
% Convert PLMR files to PLMR2SP binary file with the 
% output files containing all recorded data
%-----------------------------------
    disp('Processing binary file');
    cccp_command=['!cccp +S -L 3NnAGSTEP -W 3NnAGSTEP ',fullfile(file_path,file_name_RASP) ... 
            ,' ',fullfile(file_path,[file_name_RASP,'.all'])];

    eval(cccp_command)

    cccp_command=['!cccp +S -L B -W A ',fullfile(file_path,file_name_RASP) ... 
            ,' ',fullfile(file_path,[file_name_RASP,'.allB'])];

    eval(cccp_command)

%-----------------------------------
% Convert PLMR files to ascii file
%-----------------------------------
    disp('Processing ascii file');
    cccp_command=['!cccp +U -L P ',fullfile(file_path,file_name_RASP) ... 
            ,' ',fullfile(file_path,[file_name_RASP,'.dat'])];

    eval(cccp_command)

%-----------------------------------
% Extract event information
%-----------------------------------
    disp('Extracting event information');
    cccp_command=['!cccp +D -L  E ',fullfile(file_path,file_name_RASP) ... 
            ,' ',fullfile(file_path,[file_name_RASP,'.eve'])];

    eval(cccp_command)

%-----------------------------------
% Calculate GPS offset to last Sunday
%-----------------------------------
    disp('Calculating Cube Time');
    eval(['cd ',file_path_batch]);
    d=dir(fullfile(file_path_INC,[strtrim(geb),'*.inc']));
    eval(['!copy ',fullfile(file_path_INC,d.name),' ',fullfile(file_path_INC,'Control.inc')]);
    eval(['!pprep 1_CubeTimes.rcf 1_CubeTimes.tmp']);
    pause(tpause/5);
    eval(['!ramf run 1_CubeTimes.tmp &']);
    pause(tpause/5);
end
%-----------------------------------
% Use PLMR2SP to convert binary CCCP output files to RASP input files
% and also extract physical temperatures of instrument.
% This step only extracts the polarisations and the physical temperatures
%-----------------------------------
if (p2sp)
    p=['h'; 'v'; 't'];
    eval(['cd ',file_path]);

    for i=1:size(p,1)
        disp(['Processing PLMR2SP ',p(i)]);
        plmr2sp_command=['start plmr2sp -g ',num2str(GPS_offset),' -i ',fullfile(file_path,[file_name_PLMR,'.all']) ... 
                ,' -p ',p(i),' -o ',fullfile(file_path,[file_name_PLMR,'_',p(i),'.spa'])];
    %    disp(plmr2sp_command);    
        dos(plmr2sp_command);
        pause(tpause*2)
    end    
end
    
end