4343import time
4444import multiprocessing as mp
4545from itertools import repeat
46+ import shutil
4647warnings .filterwarnings ("ignore" )
4748
4849#######################################################################################################################################
@@ -762,9 +763,10 @@ def hi_remove_saturation(data, header, saturation_limit=14000, nsaturated=5):
762763 # pixels are summed up column-wise
763764 # where nsaturated is exceeded, values are replaced by nan
764765
765- colmask = np .sum (mask , 0 )
766+ colmask = np .nansum (mask , 0 )
766767 ii = np .array (np .where (colmask > nsaturated ))
767-
768+ # print('nsaturated:', nsaturated)
769+ # print('colmask:', colmask)
768770 if len (ii ) > 0 :
769771 ans [:, ii ] = np .nan #np.nanmedian(data)
770772 else :
@@ -788,7 +790,7 @@ def hi_remove_saturation_rdif(data):
788790 # number of pixels in a column before column is considered saturated
789791 nsaturated = 3
790792
791- dsatval = np .nanmedian (data ) + 2 * np .std (data )
793+ dsatval = np .nanmedian (data ) + 2 * np .nanstd (data )
792794
793795 ind = np .where (np .abs (data ) > dsatval )
794796
@@ -805,7 +807,7 @@ def hi_remove_saturation_rdif(data):
805807 # pixels are summed up column-wise
806808 # where nsaturated is exceeded, values are replaced by nan
807809
808- colmask = np .sum (mask , 0 )
810+ colmask = np .nansum (mask , 0 )
809811 ii = np .where (colmask > nsaturated )
810812
811813 if ii [0 ].size > 0 :
@@ -3826,7 +3828,6 @@ def scc_img_stats(img0):
38263828 """
38273829
38283830 img1 = img0 .astype (float )
3829-
38303831 img1 [img1 == 0 ] = np .nan
38313832 finite_mask = np .isfinite (img1 )
38323833 img = img1 [finite_mask ]
@@ -3876,6 +3877,7 @@ def scc_update_hdr(im, hdr0, silent=True):
38763877
38773878
38783879 # Calculate Data Dependent Values
3880+
38793881 stats = scc_img_stats (im )
38803882 hdr ['DATAMIN' ] = stats ['mn' ]
38813883 hdr ['DATAMAX' ] = stats ['mx' ]
@@ -4925,7 +4927,6 @@ def hi_correction(im, hdr, post_conj, calpath, sebip_off=False, calimg_off=False
49254927
49264928 if not silent :
49274929 print (f"Subtracted BIAS={ biasmean } " )
4928-
49294930 # Extract and correct for cosmic ray reports
49304931
49314932 ### hi_cosmics modifies the images as reference!
@@ -5023,26 +5024,23 @@ def hi_prep(im, hdr, post_conj, calpath, pointpath, calibrate_on=True, smask_on=
50235024 corr_kw : dict, optional
50245025 Dictionary of correction keywords passed to hi_correction().
50255026 """
5026-
50275027 # Update IMGSEQ for hi-res images if imgseq is not 0
5028+
50285029 if hdr ['NAXIS1' ] > 1024 and hdr ['IMGSEQ' ] != 0 and hdr ['N_IMAGES' ] == 1 :
50295030 hdr ['imgseq' ] = 0
50305031
50315032 # Calibration corrections
50325033 if calibrate_on :
50335034 im , hdr = hi_correction (im , hdr , post_conj , calpath , ** kw_args )
50345035 # hdr = hi_fix_pointing(hdr, pointpath, post_conj, silent=silent)
5035- if (im [ 0 ] .shape [0 ]== 1024 ):
5036+ if (im .shape [0 ]== 1024 ):
50365037 hdr = hi_fix_pointing (hdr , pointpath , post_conj , silent = silent )
50375038 else :
50385039 hi_calib_point (hdr , post_conj , 0 )
50395040 hdr ['ravg' ] = - 881.0
50405041 else :
50415042 cosmics = - 1
50425043
5043-
5044-
5045-
50465044 # Smooth Mask (only for HI2 detector)
50475045 if smask_on and calibrate_on and hdr ['DETECTOR' ] == 'HI2' :
50485046 mask = get_smask (hdr , calpath , post_conj , silent = True )
@@ -5056,7 +5054,6 @@ def hi_prep(im, hdr, post_conj, calpath, pointpath, calibrate_on=True, smask_on=
50565054 if not silent :
50575055 print ('Mask applied to HI2 image' )
50585056
5059-
50605057 if kw_args ['calfac_off' ] and kw_args ['nocalfac_butcorrforipsum' ]:
50615058 sumcount = hdr ['ipsum' ] - 1
50625059 divfactor = (2 ** sumcount ) ** 2
@@ -5175,6 +5172,7 @@ def reduction(start,hdul,hdul_data,hdul_header,ftpsc,ins,bflag,calpath,pointpath
51755172 if len (bad_ind ) >= len (indices ):
51765173 print ('Too many corrupted images - can\' t determine correct CRVAL1. Exiting...' )
51775174 sys .exit ()
5175+
51785176 if bflag == 'science' :
51795177 #Must find way to do this for beacon also
51805178 datamin_test = [hdul_header [i ]['DATAMIN' ] for i in range (len (hdul_header ))]
@@ -5189,6 +5187,15 @@ def reduction(start,hdul,hdul_data,hdul_header,ftpsc,ins,bflag,calpath,pointpath
51895187 test_data = np .where (test_data == 0 , np .nan , test_data )
51905188
51915189 bad_ind = [i for i in range (len (test_data )) if np .isnan (test_data [i ]).all () == True ]
5190+ bad_img += bad_ind
5191+
5192+ if bflag == 'beacon' :
5193+ num_zero = np .array ([hdul_header [i ]['DATAZER' ] for i in range (len (hdul_header ))])
5194+
5195+ num_pixels = np .array ([hdul_header [i ]['NAXIS1' ] * hdul_header [i ]['NAXIS2' ] for i in range (len (hdul_header ))])
5196+ zero_percentage = num_zero / num_pixels
5197+
5198+ bad_ind = [i for i in range (len (zero_percentage )) if zero_percentage [i ] > 0.25 ]
51925199 bad_img += bad_ind
51935200
51945201 base = 34
@@ -5385,8 +5392,12 @@ def data_reduction(start, path, datpath, ftpsc, instrument, bflag, silent, save_
53855392
53865393 print ('No files found for ' , ins , ' on ' , start )
53875394 continue
5395+
5396+ if os .path .exists (savepath + ins + '/' ):
5397+ shutil .rmtree (savepath + ins + '/' )
5398+ os .makedirs (savepath + ins + '/' )
53885399
5389- if not os . path . exists ( savepath + ins + '/' ) :
5400+ else :
53905401 os .makedirs (savepath + ins + '/' )
53915402
53925403 if not silent :
0 commit comments