%===============================================
%
% PLMR processing software
%
% This program takes the PLMR2SP files and runs them through RASP. 
% This is a stand alone program and not intended for distribution!!! 
%
% Author          Date           version
% C. Rüdiger      10-Nov-09      1.0
%
%===============================================

function f_RASP

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;

if (anci)
    file_path=fullfile(fpath,'Data','projects','EVS',file_date,'proc','ascii');
else
    file_path=fullfile(fpath,'Data',experi,'field',file_date,'CUBE');
end

%-----------------------------------
% Open PLMR2SP output file and replace with trimmed data set
%-----------------------------------
for i=1:size(pol,1)
    fPLMR=fopen(fullfile(file_path,[file_name_PLMR,'_',pol(i),'.spa']),'r');

    fgetl(fPLMR);fgetl(fPLMR);
    PLMR=fscanf(fPLMR,'%i %f %f %f %f %f %f\n',inf);
    PLMR=reshape(PLMR,7,size(PLMR,1)/7)';
    fclose('all');

    PLMR(PLMR(:,1)<RTstart.*1000,:)=[];
    PLMR(PLMR(:,1)>RTend.*1000,:)=[];

    eval(['cd ',file_path]);
    kill_PLMR=['!ren ',[file_name_PLMR,'_',pol(i),'.spa'] ...
        ,' ',[file_name_PLMR,'_',pol(i),'.origspa']];
    eval(kill_PLMR);

    fid=fopen(fullfile(file_path,[file_name_PLMR,'_',pol(i),'.spa']),'w');
    fprintf(fid,'%8i  %7.3f  %7.3f  %7.3f  %7.3f  %7.3f  %7.3f\n',PLMR');
    fclose('all');
end

%-----------------------------------
% Use PLMR2SP files as input to RASP 
%-----------------------------------
eval(['cd ',file_path]);
RASP_command=['start rasp spa2geo.rsp'];
dos(RASP_command);

end