def run_cube_gen(metadata, prev_suffix, curr_suffix, **args): # now generate cubes sci_obs_list = get_primary_sci_obs_list(metadata) std_obs_list = get_primary_std_obs_list(metadata) for fn in sci_obs_list+std_obs_list: in_fn = '%s%s.p%s.fits' % (out_dir, fn, prev_suffix) out_fn = '%s%s.p%s.fits' % (out_dir, fn, curr_suffix) if skip_done and os.path.isfile(out_fn): continue print 'Generating Data Cube for %s' % in_fn.split('/')[-1] # decide whether to use global or local wsol and wire files local_wires = get_associated_calib(metadata,fn, 'wire') if local_wires : wire_fn = '%s%s.wire.fits' % (out_dir, local_wires[0]) print '(Note: using %s as wire file)' % wire_fn.split('/')[-1] else: wire_fn = wire_out_fn local_arcs = get_associated_calib(metadata,fn, 'arc') if local_arcs : # Do I have two arcs ? Do they surround the Science file ? # Implement linear interpolation as suggested by Mike I. if len(local_arcs) ==2: # First, get the Science time f = pyfits.open(in_fn) sci_header = f[0].header sci_time = sci_header['DATE-OBS'] # Now get the arc times arc_times = ['',''] for i in range(2): # Fetch the arc time from the "extra" pkl file local_wsol_out_fn_extra = '%s%s.wsol.fits_extra.pkl' % (out_dir, local_arcs[i]) f = open(local_wsol_out_fn_extra) f_pickled = pickle.load(f) f.close() arc_times[i] = f_pickled[-1][0] # Now, make sure the Science is between the arcs: t0 = datetime.datetime( np.int(arc_times[0].split('-')[0]), np.int(arc_times[0].split('-')[1]), np.int(arc_times[0].split('-')[2].split('T')[0]), np.int(arc_times[0].split('T')[1].split(':')[0]), np.int(arc_times[0].split(':')[1]), np.int(arc_times[0].split(':')[2].split('.')[0]), ) t1 = datetime.datetime( np.int(sci_time.split('-')[0]), np.int(sci_time.split('-')[1]), np.int(sci_time.split('-')[2].split('T')[0]), np.int(sci_time.split('T')[1].split(':')[0]), np.int(sci_time.split(':')[1]), np.int(sci_time.split(':')[2].split('.')[0]), ) t2 = datetime.datetime( np.int(arc_times[01].split('-')[0]), np.int(arc_times[1].split('-')[1]), np.int(arc_times[1].split('-')[2].split('T')[0]), np.int(arc_times[1].split('T')[1].split(':')[0]), np.int(arc_times[1].split(':')[1]), np.int(arc_times[1].split(':')[2].split('.')[0]), ) ds1 = (t1 - t0).total_seconds() ds2 = (t2 - t1).total_seconds() if ds1>0 and ds2>0: # Alright, I need to interpolate betweent the two arcs w1 = ds1/(ds1+ds2) w2 = ds2/(ds1+ds2) # Open the arc solution files fn0 = '%s%s.wsol.fits' % (out_dir, local_arcs[0]) fn1 = '%s%s.wsol.fits' % (out_dir, local_arcs[1]) fits0 = pyfits.open(fn0) fits1 = pyfits.open(fn1) for i in range(1,len(fits0)): fits0[i].data = w1*fits0[i].data + w2*fits1[i].data wsol_fn = '%s%s.wsol.fits' % (out_dir, fn) fits0.writeto(wsol_fn, clobber=True) print '(2 arcs found)' print '(Note: using %sx%s.wsol.fits + %sx%s.wsol.fits as wsol file)' % (np.round(w1,2),local_arcs[0],np.round(w2,2),local_arcs[1]) else: # Arcs do not surround the Science frame # Revert to using the first one instead wsol_fn = '%s%s.wsol.fits' % (out_dir, local_arcs[0]) print '(2 arcs found, but they do not bracket the Science frame!)' print '(Note: using %s as wsol file)' % wsol_fn.split('/')[-1] else: # Either 1 or more than two arcs present ... only use the first one ! wsol_fn = '%s%s.wsol.fits' % (out_dir, local_arcs[0]) print '(Note: using %s as wsol file)' % wsol_fn.split('/')[-1] else: wsol_fn = wsol_out_fn # All done, let's generate the cube pywifes.generate_wifes_cube( in_fn, out_fn, wire_fn=wire_fn, wsol_fn=wsol_fn, ny_orig=76, offset_orig=2.0, **args) #print squirrel return
def run_cube_gen(metadata, prev_suffix, curr_suffix, **args): # now generate cubes sci_obs_list = get_primary_sci_obs_list(metadata) std_obs_list = get_primary_std_obs_list(metadata) for fn in sci_obs_list+std_obs_list: in_fn = os.path.join(out_dir, '%s.p%s.fits' % (fn, prev_suffix)) out_fn = os.path.join(out_dir, '%s.p%s.fits' % (fn, curr_suffix)) if skip_done and os.path.isfile(out_fn): continue print('Generating Data Cube for %s' % in_fn.split('/')[-1]) # decide whether to use global or local wsol and wire files local_wires = get_associated_calib(metadata,fn, 'wire') if local_wires : wire_fn = os.path.join(out_dir, '%s.wire.fits' % (local_wires[0])) print('(Note: using %s as wire file)' % wire_fn.split('/')[-1]) else: wire_fn = wire_out_fn local_arcs = get_associated_calib(metadata,fn, 'arc') if local_arcs : # Do I have two arcs ? Do they surround the Science file ? # Implement linear interpolation as suggested by Mike I. if len(local_arcs) ==2: # First, get the Science time f = pyfits.open(in_fn) sci_header = f[0].header sci_time = sci_header['DATE-OBS'] # Now get the arc times arc_times = ['',''] for i in range(2): # Fetch the arc time from the "extra" pkl file local_wsol_out_fn_extra = os.path.join(out_dir, '%s.wsol.fits_extra.pkl' % (local_arcs[i])) f = open(local_wsol_out_fn_extra, 'rb') try: f_pickled = pickle.load(f, protocol=2) # TODO: see if this works except: f_pickled = pickle.load(f) # Python 3 f.close() arc_times[i] = f_pickled[-1][0] # Now, make sure the Science is between the arcs: t0 = datetime.datetime( np.int(arc_times[0].split('-')[0]), np.int(arc_times[0].split('-')[1]), np.int(arc_times[0].split('-')[2].split('T')[0]), np.int(arc_times[0].split('T')[1].split(':')[0]), np.int(arc_times[0].split(':')[1]), np.int(arc_times[0].split(':')[2].split('.')[0]), ) t1 = datetime.datetime( np.int(sci_time.split('-')[0]), np.int(sci_time.split('-')[1]), np.int(sci_time.split('-')[2].split('T')[0]), np.int(sci_time.split('T')[1].split(':')[0]), np.int(sci_time.split(':')[1]), np.int(sci_time.split(':')[2].split('.')[0]), ) t2 = datetime.datetime( np.int(arc_times[1].split('-')[0]), np.int(arc_times[1].split('-')[1]), np.int(arc_times[1].split('-')[2].split('T')[0]), np.int(arc_times[1].split('T')[1].split(':')[0]), np.int(arc_times[1].split(':')[1]), np.int(arc_times[1].split(':')[2].split('.')[0]), ) ds1 = (t1 - t0).total_seconds() ds2 = (t2 - t1).total_seconds() if ds1>0 and ds2>0: # Alright, I need to interpolate betweent the two arcs w1 = ds1/(ds1+ds2) w2 = ds2/(ds1+ds2) # Open the arc solution files fn0 = os.path.join(out_dir, '%s.wsol.fits' % (local_arcs[0])) fn1 = os.path.join(out_dir, '%s.wsol.fits' % (local_arcs[1])) fits0 = pyfits.open(fn0) fits1 = pyfits.open(fn1) for i in range(1,len(fits0)): fits0[i].data = w1*fits0[i].data + w2*fits1[i].data wsol_fn = os.path.join(out_dir, '%s.wsol.fits' % (fn)) fits0.writeto(wsol_fn, clobber=True) print('(2 arcs found)') print('(Note: using %sx%s.wsol.fits + %sx%s.wsol.fits as wsol file)' % (np.round(w1,2),local_arcs[0],np.round(w2,2),local_arcs[1])) else: # Arcs do not surround the Science frame # Revert to using the first one instead wsol_fn = os.path.join(out_dir, '%s.wsol.fits' % (local_arcs[0])) print('(2 arcs found, but they do not bracket the Science frame!)') print('(Note: using %s as wsol file)' % wsol_fn.split('/')[-1]) else: # Either 1 or more than two arcs present ... only use the first one ! wsol_fn = os.path.join(out_dir, '%s.wsol.fits' % (local_arcs[0])) print('(Note: using %s as wsol file)' % wsol_fn.split('/')[-1]) else: wsol_fn = wsol_out_fn # All done, let's generate the cube pywifes.generate_wifes_cube( in_fn, out_fn, wire_fn=wire_fn, wsol_fn=wsol_fn, ny_orig=76, offset_orig=2.0, **args) #print squirrel return
wsol_fn = os.path.join(out_dir, '%s.wsol.fits' % (local_arcs[0])) print('(2 arcs found, but they do not bracket the Science frame!)') print('(Note: using %s as wsol file)' % wsol_fn.split('/')[-1]) else: # Either 1 or more than two arcs present ... only use the first one ! wsol_fn = os.path.join(out_dir, '%s.wsol.fits' % (local_arcs[0])) print('(Note: using %s as wsol file)' % wsol_fn.split('/')[-1]) else: wsol_fn = wsol_out_fn # All done, let's generate the cube pywifes.generate_wifes_cube( in_fn, out_fn, wire_fn=wire_fn, wsol_fn=wsol_fn, ny_orig=76, offset_orig=2.0, **args) #print squirrel return #------------------------------------------------------ # Standard star extraction def run_extract_stars(metadata, prev_suffix, curr_suffix, type='all',**args): # for each std, extract spectrum as desired std_obs_list = get_primary_std_obs_list(metadata, type=type) #print std_obs_list for fn in std_obs_list: in_fn = os.path.join(out_dir, '%s.p%s.fits' % (fn, prev_suffix)) out_fn = os.path.join(out_dir, '%s.x%s.dat' % (fn, prev_suffix)) print('Extract %s standard star from %s' % (type, in_fn.split('/')[-1]))