Home > rfi_tuning > tune_rfi.m

tune_rfi

PURPOSE ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SYNOPSIS ^

function r = tune_rfi(startTime, endTime, stage)

DESCRIPTION ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  function r = tune_rfi(startTime, endTime, stage)

   25-Jan-2012 (MAS): Created.

   Run the automated RFI tuning procedure on a stretch of data.  Split
   into three stages.

   1.  Folding, parameter determination, and statistical trial.
   2.  User verification of flagged data.
   3.  Final, automatic verification, statistics, and output.


   Inputs are as follows:
       startTime: string of starting time.
       endTime: string of ending time.
       stage: which stage of the tuning are we currently doing.  If
       greater than 1, then the previous stages must have been completed
       for the chosen date range.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function r = tune_rfi(startTime, endTime, stage)
0002 
0003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0004 %  function r = tune_rfi(startTime, endTime, stage)
0005 %
0006 %   25-Jan-2012 (MAS): Created.
0007 %
0008 %   Run the automated RFI tuning procedure on a stretch of data.  Split
0009 %   into three stages.
0010 %
0011 %   1.  Folding, parameter determination, and statistical trial.
0012 %   2.  User verification of flagged data.
0013 %   3.  Final, automatic verification, statistics, and output.
0014 %
0015 %
0016 %   Inputs are as follows:
0017 %       startTime: string of starting time.
0018 %       endTime: string of ending time.
0019 %       stage: which stage of the tuning are we currently doing.  If
0020 %       greater than 1, then the previous stages must have been completed
0021 %       for the chosen date range.
0022 %
0023 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0024 
0025 
0026 
0027 % Verify parameter count.
0028 if (nargin ~=3)
0029     error('Incorrect number of inputs.  Syntax: r = tune_rfi(startTime, endTime, stage)');
0030 end
0031 
0032 
0033 % Convert the start and stop times to MJD.
0034 try
0035     startMjd = tstr2mjd(startTime);
0036 catch me
0037     error('startTime must be a sensible date-time string.  Try again!');
0038 end
0039 try
0040     endMjd = tstr2mjd(endTime);
0041 catch me
0042     error('endTime must be a sensible date-time string.  Try again!');
0043 end
0044 
0045 
0046 % Fail if less than a day was chosen
0047 if (endMjd-startMjd) < 1
0048     error('This procedure requires more than a day of data to function well.');
0049 end
0050 
0051 
0052 
0053 % Figure out the required directories...
0054 [home installeddir] = where_am_i();
0055 
0056 [year1 month1 day1] = mjd2date(startMjd);
0057 [year2 month2 day2] = mjd2date(endMjd);
0058 datestring = sprintf('%04i%02i%02i-%04i%02i%02i' , ...
0059     year1, month1, day1, year2, month2, day2);
0060 
0061 basedir = [home '/' installeddir '/rfi_tuning/' datestring '/'];
0062 folddir = [basedir 'fold/'];
0063 plotdir = [basedir 'plot/'];
0064 
0065 
0066 % Now create them (if necessary):
0067 if ~exist(basedir,'dir')
0068     mkdir(basedir);
0069 end
0070 if ~exist(folddir,'dir')
0071     mkdir(folddir);
0072 end
0073 if ~exist(plotdir,'dir')
0074     mkdir(plotdir);
0075 end
0076 
0077 
0078 % The status file (just it's name;  don't create it just yet):
0079 statfile = [basedir 'status.mat'];
0080 
0081 
0082 
0083 % Let's get started.  Which stage are we on?
0084 switch stage
0085 
0086 % First stage:  automated folding, parameter determination, and statistical trial.
0087 %----------------------------------------------
0088     case 1
0089 
0090         % Give the user a little message of encouragement.
0091         disp(['Stage 1 chosen for data range ' startTime ' to ' endTime]);
0092         disp('   Good luck!');
0093         disp(' ');
0094         
0095         % Check the current status:
0096         if exist(statfile,'file');
0097 
0098             statObj = open(statfile);
0099             statVec = statObj.statVec;
0100             
0101             if statVec(3) == 1 
0102                 error('Stage 1 already completed.  If results were unsatisfactory, delete the status file and try again.');
0103             end
0104         else
0105             statVec = zeros(5,1);
0106         end
0107 
0108         
0109         if statVec(1) ~= 1
0110             % Alright, time to start folding.
0111             disp('===============================');
0112             disp('Now Beginning the Folding Stage');
0113             disp('===============================');
0114 
0115 
0116             % Here it is...
0117             try
0118                 r = fold_rfi(startMjd,endMjd,basedir,folddir);
0119             catch me
0120                 error('Terribly sorry: fold_rfi() appears to have crashed!');
0121             end
0122             if r ~= 1
0123                 error('Terribly sorry: fold_rfi() exited with an error!');
0124             end
0125 
0126             % Update the status vector.
0127             statVec(1) = 1;
0128         else
0129             disp('Folding Stage already completed on these data...');
0130         end
0131         
0132         save(statfile,'statVec');
0133 
0134         
0135         
0136         if statVec(2) ~= 1
0137             % Next up, time to determine the optimal parameters.
0138             disp('=========================================');
0139             disp('Now Beginning the Parameter Determination');
0140             disp('=========================================');
0141 
0142 
0143             % Here it is...
0144             try
0145                 [pArray_f pArray_p] = param_rfi(basedir);
0146             catch me
0147 
0148                 % Save status file before crashing, so that folding is not
0149                 % repeated.
0150                 save(statfile,'statVec');
0151 
0152                 error('Terribly sorry: param_rfi() appears to have crashed!');
0153             end
0154             if isempty(pArray_f)
0155 
0156                 % Save status file before crashing, so that folding is not
0157                 % repeated.
0158                 save(statfile,'statVec');
0159 
0160                 error('Terribly sorry: param_rfi() exited with an error!');
0161             end
0162 
0163             % Update the status vector.
0164             statVec(2) = 1;
0165         else
0166             disp('Parameter Determination already completed on these data...');
0167 
0168             paramFile = open([basedir 'params.mat']);
0169             pArray_f = paramFile.pArray_f;
0170             pArray_p = paramFile.pArray_p;
0171         end
0172         
0173         save(statfile,'statVec');
0174 
0175         
0176         % Now we run these parameters through the actual statistical
0177         % flagging.
0178         disp('============================================');
0179         disp('Now Beginning the Statistical Flagging Stage');
0180         disp('============================================');
0181 
0182 
0183         % Here it is...
0184         try
0185             r = stat_rfi(startMjd,endMjd,pArray_f,pArray_p,basedir);
0186         catch me
0187 
0188             % Save status file before crashing, so that folding and
0189             % parameter determination are not repeated.
0190             save(statfile,'statVec');
0191 
0192             error('Terribly sorry: stat_rfi() appears to have crashed!');
0193         end
0194         if r ~= 1
0195 
0196             % Save status file before crashing, so that folding and
0197             % parameter determination are not repeated.
0198             save(statfile,'statVec');
0199 
0200             error('Terribly sorry: stat_rfi() exited with an error!');
0201         end
0202 
0203         % Update the status vector.
0204         statVec(3) = 1;
0205         
0206         
0207         
0208         % We've done the main three parts.  Now time to write the status to
0209         % the status file.
0210         save(statfile,'statVec');
0211         
0212         
0213 
0214         % Congratulate the user.
0215         disp(['Stage 1 completed for data range ' startTime ' to ' endTime]);
0216         disp(' ');
0217         disp('On to stage 2!');
0218         disp(' ');
0219         
0220         
0221 
0222         
0223         
0224         
0225 % Second stage:  interactive verification.
0226 %----------------------------------------------
0227     case 2
0228         
0229         % Give the user a little message of encouragement.
0230         disp(['Stage 2 chosen for data range ' startTime ' to ' endTime]);
0231         disp('   Good luck!');
0232         disp(' ');
0233         
0234 
0235         % Check the current status:
0236         disp('Checking status...');
0237         if exist(statfile,'file');
0238 
0239             statObj = open(statfile);
0240             statVec = statObj.statVec;
0241             
0242             if min(statVec(1:3)) ~= 1 
0243                 error('Stage 1 not completed.  Please run stage 1 before stage 2.');
0244             end
0245             if statVec(4) == 1 
0246                 error('Stage 2 already completed.  XXX');
0247             end
0248         else
0249             error('Status file not found.  Have you run Stage 1, yet?');
0250         end
0251 
0252         
0253         % Alright, time to start the user verification.
0254         disp('===================================');
0255         disp('Now Beginning the User Verification');
0256         disp('===================================');
0257 
0258         % Here it is...
0259         try
0260             r = plot_rfi(startMjd,endMjd,basedir);
0261         catch me
0262             error('Terribly sorry: plot_rfi() appears to have crashed!');
0263         end
0264         if r ~= 1
0265             error('Terribly sorry: plot_rfi() exited with an error!');
0266         end
0267 
0268         % Update the status vector.
0269         statVec(4) = 1;
0270         
0271         
0272         
0273         % We've done the main three parts.  Now time to write the status to
0274         % the status file.
0275         save(statfile,'statVec');
0276         
0277         
0278 
0279         % Congratulate the user.
0280         disp(['Stage 2 completed for data range ' startTime ' to ' endTime]);
0281         disp(' ');
0282         disp('On to stage 3!');
0283         disp(' ');
0284         
0285         
0286 
0287 
0288         
0289         
0290         
0291         
0292 % Third stage:  automated verification, statistics, and output.  Almost done!
0293 %----------------------------------------------
0294     case 3
0295 
0296         
0297         % Give the user a little message of encouragement.
0298         disp(['Stage 3 chosen for data range ' startTime ' to ' endTime]);
0299         disp('   Good luck!');
0300         disp(' ');
0301         
0302 
0303         % Check the current status:
0304         disp('Checking status...');
0305         if exist(statfile,'file');
0306 
0307             statObj = open(statfile);
0308             statVec = statObj.statVec;
0309             
0310             if min(statVec(1:3)) ~= 1 
0311                 error('Stage 1 not completed.  Please run stages 1 and 2.');
0312             end
0313             if statVec(4) ~= 1 
0314                 error('Stage 2 not completed.  Please run stage 2 before continuing.');
0315             end
0316             if statVec(5) == 1 
0317                 error('Stage 3 already completed.  XXX');
0318             end
0319         else
0320             error('Status file not found.  Have you run Stage 1, yet?');
0321         end
0322         
0323         
0324         
0325 %         if statVec(5) ~= 1
0326 %             % Next up, time to automatically verify the given parameters.
0327 %             disp('=========================================');
0328 %             disp('Now Beginning the Parameter Verification');
0329 %             disp('=========================================');
0330 %
0331 %
0332 %             % Here it is...
0333 %             try
0334 %                 r = verify_rfi(startMjd,endMjd,basedir);
0335 %             catch me
0336 %                 error('Terribly sorry: verify_rfi() appears to have crashed!');
0337 %             end
0338 %             if r ~= 1
0339 %                 error('Terribly sorry: verify_rfi() exited with an error!');
0340 %             end
0341 %
0342 %             % Update the status vector.
0343 %             statVec(5) = 1;
0344 %         else
0345 %             disp('Parameter Verification already completed on these data...');
0346 %         end
0347         
0348 
0349         
0350         % Now generate the final statistics and plots.
0351         disp('========================================');
0352         disp('Now Generating the Final Stats and Plots');
0353         disp('========================================');
0354 
0355 
0356         % Here it is...
0357         try
0358             r = results_rfi(startMjd,endMjd,basedir);
0359         catch me
0360 
0361             % Save status file before crashing, so that folding and
0362             % parameter determination are not repeated.
0363             save(statfile,'statVec');
0364 
0365             error('Terribly sorry: results_rfi() appears to have crashed!');
0366         end
0367         if r ~= 1
0368 
0369             % Save status file before crashing, so that folding and
0370             % parameter determination are not repeated.
0371             save(statfile,'statVec');
0372 
0373             error('Terribly sorry: results_rfi() exited with an error!');
0374         end
0375 
0376         % Update the status vector.
0377         statVec(5) = 1;
0378         
0379         
0380         
0381         % We've done the main three parts.  Now time to write the status to
0382         % the status file.
0383         save(statfile,'statVec');
0384         
0385         
0386 
0387         % Congratulate the user.
0388         disp(['Stage 3 completed for data range ' startTime ' to ' endTime]);
0389         disp(' ');
0390         disp('All done!');
0391         disp(' ');
0392 
0393         
0394         
0395         
0396         
0397 
0398 % If the chosen stage doesn't make sense...
0399 %----------------------------------------------
0400     otherwise
0401         error('Invalid stage choice.  Please choose 1, 2, or 3.');
0402 end
0403 
0404 
0405 
0406 
0407 r = 1;
0408 
0409 end

Generated on Sun 14-Jun-2015 17:12:45 by m2html © 2005