def extract(filename, outname, owave=None): d = nirspecOpen(filename) w = d[1].data.copy() s = d[2].data.copy() d = d[0].data[:-20].copy() if owave is None: w0 = w[0] w1 = w[-1] owave = numpy.linspace(w0, w1, w.size) trace = d.sum(1) maxd = ndimage.maximum_filter(trace, 3) peaks = numpy.where(maxd == trace)[0] peak = peaks[maxd[peaks].argmax() - 1] spec = d[peak - 3:peak + 4].sum(0) specmod = interpolate.splrep(w, spec) outspec = interpolate.splev(owave, specmod) skymod = interpolate.splrep(w, s) sky = interpolate.splev(owave, skymod) specmean = outspec.mean() outspec /= specmean var = ((sky * 7 + outspec) * 5. + 2 * 25**2) / (5. * specmean)**2 st.make_spec(outspec, var, owave, outname, clobber=True)
def nir_extract(filename, outname, x1=0, x2=0, y1=0, y2=0, informat='new', outformat='text', apmin=-4., apmax=4., muorder=3, sigorder=3, fitrange=None, weight='gauss', owave=None, do_plot=True, do_subplot=True, stop_if_nan=True): """ Given the calibrated and sky-subtracted 2d spectrum, extracts the 1d spectrum, taking into account things specific to the form of the 2d NIRSPEC spectra. """ """ Set up NIRSPEC detector characteristics """ gain = 4. # Value in e-/ADU rdnoise = 25. # Value in e- """ Read the 2d spectrum """ print "" d, w, v = read_nirspec_spec(filename, x1, x2, y1, y2, informat=informat) if informat == 'old': s = v else: s = numpy.sqrt(v) """ Show the 2d spectrum """ mp, sp = c.sigma_clip(d) if do_plot: plot.figure(1) plot.clf() plot.imshow(d, origin='lower', cmap=plot.cm.gray, vmin=mp - sp, vmax=mp + 4 * sp) """ Find the trace """ if (do_plot): # plot.figure(2) plot.figure(2, figsize=(15, 12)) plot.clf() mu0, sig0 = ss.find_trace(d, apmin=apmin, apmax=apmax, do_plot=do_plot, do_subplot=do_subplot) """ Trace the object down the 2d spectrum """ mupoly, sigpoly = ss.trace_spectrum(d, mu0, sig0, fitrange=fitrange, muorder=muorder, sigorder=sigorder, do_plot=do_plot, do_subplot=do_subplot) """ Extract the 1d spectrum """ spec, varspec = ss.extract_spectrum(d, mupoly, sigpoly, apmin=apmin, apmax=apmax, weight=weight, sky=s, gain=gain, rdnoise=rdnoise, do_plot=do_plot, do_subplot=do_subplot) if informat == 'new': varspec = v """ Check for NaN values in the returned spectra """ spec_nan = False if ((numpy.isnan(spec)).sum() > 0 or (numpy.isnan(varspec)).sum() > 0): spec_nan = True if (stop_if_nan and spec_nan): print "" print "WARNING: There is at least one NaN in the extracted spectrum " print " or variance spectrum. Stopping here. " print "If you understand the cause of this/these NaN value(s) and want" print " to continue, re-run nir_extract with stop_if_nan=False" print "" return elif (spec_nan): spec[numpy.isnan(spec)] = 0.0 varspec[numpy.isnan(varspec)] = 2.0 * numpy.nanmax(varspec) """ Linearize the wavelength scale if needed (taken from code by Matt Auger) Old format: explicitly linearize here New format: wavelength scale is alread linearized by niredux """ if owave is None: if informat == 'old': w0 = w[0] w1 = w[-1] owave = numpy.linspace(w0, w1, w.size) else: owave = w tmpwave, outspec = ss.resample_spec(w, spec, owave) tmpwave, outvar = ss.resample_spec(w, varspec, owave) tmpwave, sky = ss.resample_spec(w, s, owave) """ Plot the linearized spectrum, with rms """ rms = numpy.sqrt(outvar) if do_plot: if (do_subplot): plot.figure(3) else: # Re-use fig 4, which showed an earlier version of the extracted # spectrum plot.figure(4) plot.clf() plot.axhline(color='k') plot.plot(owave, outspec, linestyle='steps', label='spectrum') plot.plot(owave, rms, 'r', linestyle='steps', label='RMS') plot.xlabel('Wavelength') plot.ylabel('Relative Flux Density') plot.title('Output Spectrum') plot.legend(loc='best') plot.xlim([owave[0], owave[-1]]) """ Write the output spectrum in the requested format """ if (outformat == 'mwa'): st.make_spec(outspec, outvar, owave, outname, clobber=True) else: ss.save_spectrum(outname, owave, outspec, outvar)
def extract(image, varimage, outname, width=2., offset=0., pos=None, minwave=None, maxwave=None, response_image=None, response_model=None, regions=[], centroid=None): if outname.lower().find('fits') < 1: outname = "%s.fits" % outname resp = None respwave = None if response_image is not None: if response_model is None: # print "No response model included; not performing response correction." import cPickle resp = cPickle.load(open(response_image)) else: resp = get_response(response_image, response_model, regions) elif response_model is not None: print "No response image included; not performing response correction." if minwave is None: minwave = -10. if maxwave is None: maxwave = 1e99 data = pyfits.open(image)[0].data.copy() if data.ndim == 3: if data.shape[0] > 1: print "Only working on first image plane of 3D image." data = data[0].copy() wave = st.wavelength(image) indx = scipy.where((wave > minwave) & (wave < maxwave))[0] minx, maxx = indx.min(), indx.max() wave = wave[minx:maxx] data = data[:, minx:maxx] tmp = data.copy() tmp[numpy.isnan(data)] = 0. ospec = wave * 0. ovar = wave * 0. x = numpy.arange(data.shape[0]) if centroid is None: n = tmp.shape[1] / 100 peaks = [] bounds = numpy.linspace(0, tmp.shape[1], n + 1) for i in range(n): a, b = bounds[i], bounds[i + 1] p = tmp[:, a:b].sum(1).argmax() peaks.append([(a + b) / 2., p]) peak = sf.lsqfit(numpy.asarray(peaks), 'polynomial', 3) if pos is not None: peak['coeff'][0] = pos peaks = sf.genfunc(numpy.arange(tmp.shape[1]), 0., peak) center = wave * 0. for i in range(center.size): d = data[:, i].copy() peak = peaks[i] if peak < 0: peak = 0 if peak >= d.shape: peak = d.shape - 1. fit = numpy.array([0., d[peak], peak, 1.]) cond = ~numpy.isnan(d) input = numpy.empty((d[cond].size, 2)) input[:, 0] = x[cond].copy() input[:, 1] = d[cond].copy() fit = sf.ngaussfit(input, fit)[0] center[i] = fit[2] fit = sf.lsqfit(ndimage.median_filter(center, 17), 'polynomial', 5) centroid = sf.genfunc(scipy.arange(wave.size), 0., fit) #import pylab #pylab.plot(center-centroid) #pylab.show() xvals = scipy.arange(data.shape[0]) var = pyfits.open(varimage)[0].data.copy() if var.ndim == 3: var = var[0].copy() var = var[:, minx:maxx] cond = (numpy.isnan(var)) | (numpy.isnan(data)) for i in range(ovar.size): c = centroid[i] + offset wid = width mask = xvals * 0. mask[(xvals - c < wid) & (xvals - c > 0.)] = wid - (xvals[ (xvals - c < wid) & (xvals - c > 0.)] - c) mask[(xvals - c < wid - 1) & (xvals - c > 0.)] = 1. mask[(c - xvals < wid + 1) & (c - xvals > 0.)] = (wid + 1) - ( c - xvals[(c - xvals < wid + 1) & (c - xvals > 0.)]) mask[(c - xvals < wid) & (c - xvals > 0.)] = 1. mask[cond[:, i]] = 0. mask /= mask.sum() ospec[i] = (mask[~cond[:, i]] * data[:, i][~cond[:, i]]).sum() ovar[i] = ((mask[~cond[:, i]]**2) * var[:, i][~cond[:, i]]).sum() badpix = numpy.where(numpy.isnan(ospec))[0] ospec[badpix] = 0. ovar[badpix] = ovar[~numpy.isnan(ovar)].max() * 1e6 med = numpy.median(ospec) ospec /= med ovar /= med**2 if resp is not None: # if respwave is None: # resp = sf.genfunc(wave,0,resp) # else: # resp = interpolate.splrep(respwave,resp) resp = interpolate.splev(wave, resp) ospec *= resp ovar *= resp**2 st.make_spec(ospec, ovar, wave, outname, clobber=True) return centroid
def lris_extract(filename, outname, weightfile=None,trimfile=False,x1=0, x2=0, y1=0, y2=0, informat='new',outformat='text', apmin=-4., apmax=4., muorder=3, sigorder=3, fitrange=None, weight='gauss', owave=None, do_plot=True, do_subplot=True, stop_if_nan=True, weighted_var=True, output_plot = None, output_plot_dir = None,crval1=None,dispaxis='x',findmultiplepeaks=False,return_data=True,nan_to_zero=False,maxpeaks=2): #clear_all() """ Given the calibrated and sky-subtracted 2d spectrum, extracts the 1d spectrum, taking into account things specific to the form of the 2d LRIS spectra. """ """ Set up NIRSPEC detector characteristics """ gain = 4. # Value in e-/ADU rdnoise = 25. # Value in e- """ Read the 2d spectrum """ print "" d,w,v = read_lris_spec(filename,weightfilename=weightfile,x1=x1,x2=x2,y1=y1,y2=y2,trimfile=trimfile,informat=informat,weighted_var=weighted_var,crval1=crval1) if informat=='old': s = v else: s = numpy.sqrt(v) bounds_arr = np.array([0,np.min(np.shape(d))]) fitmp,fixmu = False,False apertures = 4*np.ones(1) if findmultiplepeaks: if np.min(np.shape(d)) > 3+(1+2*np.fabs(apmin)+2*apmax)*(maxpeaks-1): fitmp,fixmu,mp_out,bounds_arr,apertures = ss.find_multiple_peaks(d,maxpeaks=maxpeaks,output_plot=output_plot,output_plot_dir=output_plot_dir,check_aps=True) else: mpflag,ipk = False,maxpeaks-1 while ((not mpflag) & (ipk > 1)): if np.min(np.shape(d)) > 3+(1+2*np.fabs(apmin)+2*apmax)*(ipk-1): fitmp,fixmu,mp_out,bounds_arr,apertures = ss.find_multiple_peaks(d,maxpeaks=ipk,output_plot=output_plot,output_plot_dir=output_plot_dir,check_aps=True) mpflag = True ipk -= 1 if fitmp: numpeaks = np.shape(mp_out)[1] mutmp = mp_out[1] for imle in range(0,numpeaks): output_plot_tmp = output_plot if output_plot != None: if imle != 0: output_plot_tmp = 'line%i.'%(imle+1) + output_plot dlb,dub = bounds_arr[2*imle],bounds_arr[2*imle+1] if dispaxis == 'x': owavet,outspect,outvart = lris_extract_(d[dlb:dub+1,:],w,v[dlb:dub+1,:],s[dlb:dub+1,:],gain,rdnoise,outname,informat=informat,outformat=outformat, apmin=-1*apertures[imle], apmax=apertures[imle],muorder=muorder, sigorder=sigorder, fitrange=fitrange, weight=weight,owave=None, do_plot=do_plot, do_subplot=do_subplot, stop_if_nan=stop_if_nan, weighted_var=weighted_var, output_plot = output_plot_tmp, output_plot_dir = output_plot_dir,dispaxis=dispaxis,nan_to_zero=nan_to_zero,return_data=return_data) else: owavet,outspect,outvart = lris_extract_(d[:,dlb:dub+1],w,v[dlb:dub+1,:],s[:,dlb:dub+1],gain,rdnoise,outname,informat=informat,outformat=outformat, apmin=-1*apertures[imle], apmax=apertures[imle],muorder=muorder, sigorder=sigorder, fitrange=fitrange, weight=weight,owave=None, do_plot=do_plot, do_subplot=do_subplot, stop_if_nan=stop_if_nan, weighted_var=weighted_var, output_plot = output_plot_tmp, output_plot_dir = output_plot_dir,dispaxis=dispaxis,nan_to_zero=nan_to_zero,return_data=return_data) if imle == 0: owave,outspec,outvar = np.zeros((numpeaks,len(owavet))),np.zeros((numpeaks,len(outspect))),np.zeros((numpeaks,len(outvart))) owave[imle],outspec[imle],outvar[imle] = owavet,outspect,outvart else: dlb,dub = bounds_arr[0],bounds_arr[1] if dispaxis == 'x': owave,outspec,outvar = lris_extract_(d[dlb:dub+1,:],w,v[dlb:dub+1,:],s[dlb:dub+1,:],gain,rdnoise,outname,informat=informat,outformat=outformat, apmin=-1*apertures[0], apmax=apertures[0],muorder=muorder, sigorder=sigorder, fitrange=fitrange, weight=weight,owave=None, do_plot=do_plot, do_subplot=do_subplot, stop_if_nan=stop_if_nan, weighted_var=weighted_var, output_plot = output_plot, output_plot_dir = output_plot_dir,dispaxis=dispaxis,nan_to_zero=nan_to_zero,return_data=return_data) else: owave,outspec,outvar = lris_extract_(d[:,dlb:dub+1],w,v[:,dlb:dub+1],s[:,dlb:dub+1],gain,rdnoise,outname,informat=informat,outformat=outformat, apmin=-1*apertures[0], apmax=apertures[0],muorder=muorder, sigorder=sigorder, fitrange=fitrange, weight=weight,owave=None, do_plot=do_plot, do_subplot=do_subplot, stop_if_nan=stop_if_nan, weighted_var=weighted_var, output_plot = output_plot, output_plot_dir = output_plot_dir,dispaxis=dispaxis,nan_to_zero=nan_to_zero,return_data=return_data) """ Write the output spectrum in the requested format """ if(outformat == 'mwa'): st.make_spec(outspec,outvar,owave,outname,clobber=True) else: ss.save_spectrum(outname,owave,outspec,outvar)