0001 function day_reduce_gs(date,output_stub,sched_to_reduce)
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
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 if(nargin < 6)
0052 final_dir = [];
0053 end
0054
0055
0056 max_doable_obs_len=360;
0057
0058
0059
0060
0061
0062
0063
0064
0065 path_to_redscript_prep='reduc/redScript_prep_and_save_gs.red';
0066
0067
0068 path_to_redscript_apply='reduc/redScript_load_and_apply_gs.red';
0069
0070 if(~exist('runhealth','var'))
0071 runhealth=1;
0072 end
0073
0074 [home,installeddir]=where_am_i();
0075 location = [home,'/',installeddir];
0076
0077 date_number=datenum(date);
0078 num_date = datestr(date_number, 'yyyymmdd');
0079
0080 antenna_health_dir = ['reduc/',num_date,output_stub,'_gs'];
0081
0082 outputdir = [num_date,output_stub];
0083 errorfile = [num_date,output_stub,'_errors.txt'];
0084 errorfilepath = [location,'/','reduc/',outputdir,'/',errorfile];
0085 mkdir([location,'/','reduc/',outputdir]);
0086 disp(['day_reduce_gs:: Writing errors to ',errorfilepath]);
0087 errfile = fopen(errorfilepath,'w');
0088
0089 file = [location,'/','log/obs_log.html'];
0090
0091 [start_dates,end_dates,schedule] =get_days_obs(file,date);
0092
0093 disp(['day_reduce_gs:: FOUND ',int2str(length(start_dates)),' observations...']);
0094
0095 for i=1:length(start_dates)
0096
0097
0098
0099 sched_bits = regexp(char(schedule(i)), '/', 'split');
0100 sched_name = char(sched_bits(length(sched_bits)));
0101
0102 sched_name = strrep(sched_name, '(','');
0103 disp(['day_reduce_gs:: Schedule is ',sched_name]);
0104
0105
0106 if strcmp(char(start_dates(i)),'Error')
0107 continue;
0108 end;
0109
0110
0111
0112 if (exist('sched_to_reduce','var'))
0113 negate = 0;
0114 stripped_sched_to_reduce=sched_to_reduce;
0115 if (~isempty(regexp(sched_to_reduce,'^~')))
0116
0117
0118 negate = 1;
0119 stripped_sched_to_reduce = regexprep(sched_to_reduce,'^~','');
0120 end
0121 does_sched_match = ~isempty(regexp(sched_name,stripped_sched_to_reduce,'match'));
0122 xor_exp = ~xor(does_sched_match,negate);
0123
0124 if (xor_exp)
0125 disp(['day_reduce_gs:: SKIPPING this observation ', sched_name,' - does not match the requested schedule substring, namely ''',sched_to_reduce,'''']);
0126 continue;
0127 end;
0128 end;
0129
0130 start_string = remove_colon_datestring(char(start_dates(i)));
0131 end_string = remove_colon_datestring(char(end_dates(i)));
0132 start_num = datenum(start_string);
0133 end_num = datenum(end_string);
0134
0135 obs_length_mins = (end_num-start_num)*24*60;
0136
0137
0138 if (obs_length_mins<max_doable_obs_len)
0139
0140
0141 try
0142 disp(['day_reduce_gs:: READING Obs No. ', int2str(i),' of ',int2str(length(start_dates)),' -> ',char(start_dates(i)),' --- ',char(end_dates(i))]);
0143 d=read_arc(char(start_dates(i)),char(end_dates(i)));
0144
0145 catch exception
0146 error_string= ['day_reduce_gs:: FAILED TO READ ', int2str(i),' of ',int2str(length(start_dates)), ' -> ',char(start_dates(i)),' --- ',char(end_dates(i))];
0147 disp(error_string);
0148 rep = getReport(exception, 'basic');
0149 fprintf(errfile,'%s\n',error_string);
0150 fprintf(errfile,'%s\n',rep);
0151 continue;
0152 end;
0153
0154 try
0155 disp(['day_reduce_gs:: REDUCING Obs No. ', int2str(i),' of ',int2str(length(start_dates)),' -> ',char(start_dates(i)),' --- ',char(end_dates(i))]);
0156 dcal2 = reduceData(d, path_to_redscript_prep, 0, outputdir,sched_name);
0157 catch exception
0158 error_string = ['day_reduce_gs:: FAILED TO REDUCE ',int2str(i),' of ',int2str(length(start_dates)), ' -> ',char(start_dates(i)),' --- ',char(end_dates(i))];
0159 disp(error_string);
0160 rep = getReport(exception, 'basic');
0161 fprintf(errfile,'%s\n',error_string);
0162 fprintf(errfile,'%s\n',rep);
0163 continue;
0164 end;
0165
0166 else
0167
0168
0169
0170 start_num_mins = start_num*24*60;
0171 end_num_mins = end_num*24*60;
0172
0173 no_chunks_reqd = ceil( obs_length_mins/max_doable_obs_len);
0174
0175
0176 disp(['day_reduce_gs:: Obs longer than 360 mins will split obs from ',datestr(start_num),' to ', datestr(end_num), ' into ',int2str(no_chunks_reqd),' sub-observations']);
0177 for j=1:no_chunks_reqd
0178 if (j== no_chunks_reqd)
0179 new_start_mins= start_num_mins + (j-1)*max_doable_obs_len;
0180 new_start_days= new_start_mins/(24*60);
0181 new_start_string = datestr(new_start_days,0);
0182 new_start_string = insert_colon_datestring(new_start_string);
0183 new_end_string = char(end_dates(i));
0184 else
0185 new_start_mins= start_num_mins + (j-1)*max_doable_obs_len;
0186 new_start_days= new_start_mins/(24*60);
0187 new_start_string = datestr(new_start_days,0);
0188 new_end_mins= start_num_mins + (j)*max_doable_obs_len;
0189 new_end_days= new_end_mins/(24*60);
0190
0191 new_end_string = datestr(new_end_days);
0192 whos
0193
0194
0195 new_start_string = insert_colon_datestring(new_start_string) ;
0196 new_end_string = insert_colon_datestring(new_end_string) ;
0197
0198 end
0199
0200
0201
0202 try
0203 disp(['day_reduce_gs:: READING Obs No. ', int2str(i),', chunk ',int2str(j),' of ',int2str(no_chunks_reqd),' -> ',...
0204 new_start_string, ' --- ',new_end_string]);
0205 d=read_arc(new_start_string,new_end_string);
0206
0207 catch exception
0208 error_string= ['day_reduce_gs:: FAILED TO READ ',int2str(i),':',int2str(j), ' of ',int2str(length(start_dates)),':',int2str(no_chunks_reqd),' -> ',...
0209 new_start_string, ' --- ',new_end_string];
0210
0211 disp(error_string);
0212 rep = getReport(exception, 'basic');
0213 fprintf(errfile,'%s\n',error_string);
0214 fprintf(errfile,'%s\n',rep);
0215 continue;
0216 end;
0217
0218 try
0219 disp(['day_reduce_gs:: REDUCING Obs No. ', int2str(i),':',int2str(j), ' of ',int2str(length(start_dates)),':',int2str(no_chunks_reqd),' -> ',...
0220 new_start_string, ' --- ',new_end_string]);
0221 dcal2 = reduceData(d, path_to_redscript_prep, 0, outputdir,sched_name);
0222 catch exception
0223 error_string = ['day_reduce_gs:: FAILED TO REDUCE ',int2str(i),':',int2str(j), ' of ',int2str(length(start_dates)),':',int2str(no_chunks_reqd),' -> ',...
0224 new_start_string, ' --- ',new_end_string];
0225
0226 disp(error_string);
0227 rep = getReport(exception, 'basic');
0228 fprintf(errfile,'%s\n',error_string);
0229 fprintf(errfile,'%s\n',rep);
0230 continue;
0231 end;
0232
0233 end;
0234
0235 end;
0236 end;
0237
0238 disp( ['day_reduce_gs: FINISHED ',date,' output dir is ',outputdir] );
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252 disp('day_reduce_gs: !!!!!! Creating files.txt' );
0253 files_file_filename=[location,'/','reduc/',outputdir,'/','files.txt'];
0254 files_file = fopen(files_file_filename,'w');
0255
0256 top_level_dir = [location,'/','reduc/',outputdir];
0257 current_dir=pwd;
0258 cd(top_level_dir);
0259 [status,cmdout] = unix(['find . -name "*.fits" |grep -v map']);
0260 cd(current_dir);
0261 fprintf(files_file,'%s\n',cmdout);
0262 fclose(files_file);
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277 disp('day_reduce_gs: !!!!! Making ground templates...' );
0278
0279 cd([location,'/']);
0280
0281 el_list = get_days_observed_els('log/obs_log.html',date)
0282
0283
0284
0285
0286
0287
0288
0289 cd(top_level_dir);
0290
0291 MakeGroundTemplate(files_file_filename, 1, date, 32,el_list);
0292
0293 cd(current_dir);
0294
0295 disp('day_reduce_gs: ... !!!!! done making ground templates.' );
0296
0297
0298
0299
0300
0301 outputdir = [num_date,output_stub,'_gs'];
0302 errorfilepath = [location,'/','reduc/',outputdir,'/',errorfile];
0303 mkdir([location,'/','reduc/',outputdir]);
0304
0305 disp(['day_reduce_gs: ... !!!!! Preparing to rereduce in directory',outputdir]);
0306
0307
0308
0309
0310 for i=1:length(start_dates)
0311
0312 sched_bits = regexp(char(schedule(i)), '/', 'split');
0313 sched_name = char(sched_bits(length(sched_bits)));
0314
0315 sched_name = strrep(sched_name, '(','');
0316 disp(['day_reduce_gs: Schedule is ',sched_name]);
0317
0318
0319 if strcmp(char(start_dates(i)),'Error')
0320 continue;
0321 end;
0322
0323 if (exist('sched_to_reduce','var'))
0324 negate = 0;
0325 stripped_sched_to_reduce=sched_to_reduce;
0326 if (~isempty(regexp(sched_to_reduce,'^~')))
0327
0328
0329 negate = 1;
0330 stripped_sched_to_reduce = regexprep(sched_to_reduce,'^~','');
0331 end
0332 does_sched_match = ~isempty(regexp(sched_name,stripped_sched_to_reduce,'match'));
0333 xor_exp = ~xor(does_sched_match,negate);
0334
0335 if (xor_exp)
0336 disp(['day_reduce_gs:: SKIPPING this observation ', sched_name,' - does not match the requested schedule substring, namely ''',sched_to_reduce,'''']);
0337 continue;
0338 end;
0339 end;
0340
0341 start_string = remove_colon_datestring(char(start_dates(i)));
0342 end_string = remove_colon_datestring(char(end_dates(i)));
0343 start_num = datenum(start_string);
0344 end_num = datenum(end_string);
0345
0346 obs_length_mins = (end_num-start_num)*24*60;
0347
0348
0349 if (obs_length_mins<max_doable_obs_len)
0350
0351
0352 try
0353 disp(['day_reduce_gs:: READING Obs No. ', int2str(i),' of ',int2str(length(start_dates)),' -> ',char(start_dates(i)),' --- ',char(end_dates(i))]);
0354 d=read_arc(char(start_dates(i)),char(end_dates(i)));
0355
0356 catch exception
0357 error_string= ['day_reduce_gs:: FAILED TO READ ', int2str(i),' of ',int2str(length(start_dates)), ' -> ',char(start_dates(i)),' --- ',char(end_dates(i))];
0358 disp(error_string);
0359 rep = getReport(exception, 'basic');
0360 fprintf(errfile,'%s\n',error_string);
0361 fprintf(errfile,'%s\n',rep);
0362 continue;
0363 end;
0364
0365 try
0366 disp(['day_reduce_gs:: REDUCING Obs No. ', int2str(i),' of ',int2str(length(start_dates)),' -> ',char(start_dates(i)),' --- ',char(end_dates(i))]);
0367 dcal2 = reduceData(d, path_to_redscript_apply, 0, outputdir,sched_name);
0368 catch exception
0369 error_string = ['day_reduce_gs:: FAILED TO REDUCE ',int2str(i),' of ',int2str(length(start_dates)), ' -> ',char(start_dates(i)),' --- ',char(end_dates(i))];
0370 disp(error_string);
0371 rep = getReport(exception, 'basic');
0372 fprintf(errfile,'%s\n',error_string);
0373 fprintf(errfile,'%s\n',rep);
0374 continue;
0375 end;
0376
0377 else
0378
0379
0380
0381 start_num_mins = start_num*24*60;
0382 end_num_mins = end_num*24*60;
0383
0384 no_chunks_reqd = ceil( obs_length_mins/max_doable_obs_len);
0385
0386
0387 disp(['day_reduce_gs:: Obs longer than 360 mins will split obs from ',datestr(start_num),' to ', datestr(end_num), ' into ',int2str(no_chunks_reqd),' sub-observations']);
0388 for j=1:no_chunks_reqd
0389 if (j== no_chunks_reqd)
0390 new_start_mins= start_num_mins + (j-1)*max_doable_obs_len;
0391 new_start_days= new_start_mins/(24*60);
0392 new_start_string = datestr(new_start_days,0);
0393 new_start_string = insert_colon_datestring(new_start_string);
0394 new_end_string = char(end_dates(i));
0395 else
0396 new_start_mins= start_num_mins + (j-1)*max_doable_obs_len;
0397 new_start_days= new_start_mins/(24*60);
0398 new_start_string = datestr(new_start_days,0);
0399 new_end_mins= start_num_mins + (j)*max_doable_obs_len;
0400 new_end_days= new_end_mins/(24*60);
0401
0402 new_end_string = datestr(new_end_days);
0403 whos
0404
0405
0406 new_start_string = insert_colon_datestring(new_start_string) ;
0407 new_end_string = insert_colon_datestring(new_end_string) ;
0408
0409 end
0410
0411
0412
0413 try
0414 disp(['day_reduce_gs:: READING Obs No. ', int2str(i),', chunk ',int2str(j),' of ',int2str(no_chunks_reqd),' -> ',...
0415 new_start_string, ' --- ',new_end_string]);
0416 d=read_arc(new_start_string,new_end_string);
0417
0418 catch exception
0419 error_string= ['day_reduce_gs:: FAILED TO READ ',int2str(i),':',int2str(j), ' of ',int2str(length(start_dates)),':',int2str(no_chunks_reqd),' -> ',...
0420 new_start_string, ' --- ',new_end_string];
0421
0422 disp(error_string);
0423 rep = getReport(exception, 'basic');
0424 fprintf(errfile,'%s\n',error_string);
0425 fprintf(errfile,'%s\n',rep);
0426 continue;
0427 end;
0428
0429 try
0430 disp(['day_reduce_gs:: REDUCING Obs No. ', int2str(i),':',int2str(j), ' of ',int2str(length(start_dates)),':',int2str(no_chunks_reqd),' -> ',...
0431 new_start_string, ' --- ',new_end_string]);
0432 dcal2 = reduceData(d, path_to_redscript_apply, 0, outputdir,sched_name);
0433 catch exception
0434 error_string = ['day_reduce_gs:: FAILED TO REDUCE ',int2str(i),':',int2str(j), ' of ',int2str(length(start_dates)),':',int2str(no_chunks_reqd),' -> ',...
0435 new_start_string, ' --- ',new_end_string];
0436
0437 disp(error_string);
0438 rep = getReport(exception, 'basic');
0439 fprintf(errfile,'%s\n',error_string);
0440 fprintf(errfile,'%s\n',rep);
0441 continue;
0442 end;
0443
0444 end;
0445
0446 end;
0447 end;
0448
0449
0450
0451
0452
0453
0454
0455 if runhealth ~= 0
0456 try
0457 disp( ['day_reduce_gs:: Attempting to run antenna health script : location',antenna_health_dir]);
0458 antHealth(date,antenna_health_dir);
0459 catch exception
0460 error_string = ['day_reduce_gs:: FAILED to run antenna health script : location',antenna_health_dir];
0461 disp(error_string);
0462 rep = getReport(exception, 'basic');
0463 fprintf(errfile,'%s\n',error_string);
0464 fprintf(errfile,'%s\n',rep);
0465
0466 end;
0467 else
0468 disp('day_reduce_gs:: NOT attempting to run antenna health script.');
0469 end
0470
0471
0472 fclose(errfile);
0473
0474 if(~isempty(final_dir))
0475 txt = sprintf('mv reduc/%s/ %s/', outputdir, final_dir);
0476 unix(txt);
0477 end
0478
0479 disp('day_reduce_gs:: FINISHED rereduction with ground removal');
0480
0481 return;
0482
0483
0484
0485 function processed_string = remove_colon_datestring(date_string)
0486 splitline = regexp(char(date_string), ':', 'split');
0487 processed_string=[char(splitline(1)),' ',char(splitline(2)),':',char(splitline(3)),':',char(splitline(4))];
0488
0489 function processed_string = insert_colon_datestring(date_string)
0490 splitline = regexp(char(date_string), ' ', 'split');
0491 processed_string=[char(splitline(1)),':',char(splitline(2))];