0001 function r = tune_rfi(startTime, endTime, stage)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 if (nargin ~=3)
0029 error('Incorrect number of inputs. Syntax: r = tune_rfi(startTime, endTime, stage)');
0030 end
0031
0032
0033
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
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
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
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
0079 statfile = [basedir 'status.mat'];
0080
0081
0082
0083
0084 switch stage
0085
0086
0087
0088 case 1
0089
0090
0091 disp(['Stage 1 chosen for data range ' startTime ' to ' endTime]);
0092 disp(' Good luck!');
0093 disp(' ');
0094
0095
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
0111 disp('===============================');
0112 disp('Now Beginning the Folding Stage');
0113 disp('===============================');
0114
0115
0116
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
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
0138 disp('=========================================');
0139 disp('Now Beginning the Parameter Determination');
0140 disp('=========================================');
0141
0142
0143
0144 try
0145 [pArray_f pArray_p] = param_rfi(basedir);
0146 catch me
0147
0148
0149
0150 save(statfile,'statVec');
0151
0152 error('Terribly sorry: param_rfi() appears to have crashed!');
0153 end
0154 if isempty(pArray_f)
0155
0156
0157
0158 save(statfile,'statVec');
0159
0160 error('Terribly sorry: param_rfi() exited with an error!');
0161 end
0162
0163
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
0177
0178 disp('============================================');
0179 disp('Now Beginning the Statistical Flagging Stage');
0180 disp('============================================');
0181
0182
0183
0184 try
0185 r = stat_rfi(startMjd,endMjd,pArray_f,pArray_p,basedir);
0186 catch me
0187
0188
0189
0190 save(statfile,'statVec');
0191
0192 error('Terribly sorry: stat_rfi() appears to have crashed!');
0193 end
0194 if r ~= 1
0195
0196
0197
0198 save(statfile,'statVec');
0199
0200 error('Terribly sorry: stat_rfi() exited with an error!');
0201 end
0202
0203
0204 statVec(3) = 1;
0205
0206
0207
0208
0209
0210 save(statfile,'statVec');
0211
0212
0213
0214
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
0226
0227 case 2
0228
0229
0230 disp(['Stage 2 chosen for data range ' startTime ' to ' endTime]);
0231 disp(' Good luck!');
0232 disp(' ');
0233
0234
0235
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
0254 disp('===================================');
0255 disp('Now Beginning the User Verification');
0256 disp('===================================');
0257
0258
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
0269 statVec(4) = 1;
0270
0271
0272
0273
0274
0275 save(statfile,'statVec');
0276
0277
0278
0279
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
0293
0294 case 3
0295
0296
0297
0298 disp(['Stage 3 chosen for data range ' startTime ' to ' endTime]);
0299 disp(' Good luck!');
0300 disp(' ');
0301
0302
0303
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
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351 disp('========================================');
0352 disp('Now Generating the Final Stats and Plots');
0353 disp('========================================');
0354
0355
0356
0357 try
0358 r = results_rfi(startMjd,endMjd,basedir);
0359 catch me
0360
0361
0362
0363 save(statfile,'statVec');
0364
0365 error('Terribly sorry: results_rfi() appears to have crashed!');
0366 end
0367 if r ~= 1
0368
0369
0370
0371 save(statfile,'statVec');
0372
0373 error('Terribly sorry: results_rfi() exited with an error!');
0374 end
0375
0376
0377 statVec(5) = 1;
0378
0379
0380
0381
0382
0383 save(statfile,'statVec');
0384
0385
0386
0387
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
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