def atd9(): """ Verify that transformation preserve flux. The test creates a mosaic from an input AstroData object and displays it in the DS9 display. Using IRAF imexam we measure objects that are on the the middle block for GMOS or the lower left block for GSAOI. We also measure the same objects in the input amplifers. We calculate the magnitude difference. """ from astrodata import AstroData from gempy.adlibrary.mosaicAD import MosaicAD # This is the default Mosaic function from gempy.mosaic.gemMosaicFunction import gemini_mosaic_function try: from stsci.numdisplay import display except ImportError: from numdisplay import display print '\n atd9 REQUIREMENT.......' print ('***** From a given AstroData object, the system shall conserve flux' ' after "transforming"') ad = AstroData(file) # Now make a mosaicAD object using the input ad # and the default mosaic function named # gemini_mosaic_function. mo = MosaicAD(ad, gemini_mosaic_function) mosaic_data = mo.mosaic_image_data() display(mosaic_data,frame=1) # display input amplifier 3. Assumning that there is one # amplifier per block display(ad['SCI',3].data,frame=2)
def __init__(self,pfile=None): if pfile != None: self.xyr = np.zeros([20,2]) self.xyb = np.zeros([20,2]) self.npin = 0 self.imr = pf.getdata(pfile,1) self.imb = pf.getdata(pfile,2) #if not ndis.checkDisplay(): # print "\n ****ERROR: ds9 is not running." # return ndis.display(self.imr,frame=1) ndis.display(self.imb,frame=2)
def _findxycenter(hdr, im,ext,log): def xy_tran(out): """ Function to transform blue coordinates to red frame coordinates """ xref = out[1] - 990.5897 yref = out[0] - 37.82109 x = -0.9985259*xref - 0.0178984*yref y = -0.0181331*xref + 0.9991414*yref return y,x #hdr = pf.getheader(file) #im = pf.getdata(file) xcen = hdr.get('xcen') ycen = hdr.get('ycen') if (xcen == None): #ratio,xc,yc = peak2halo('',image=im) #xcen = xc #ycen = yc #if (xcen < 0 or ycen < 0): if True: if ext == 2: # register blue frame to red's frame coordinate system im = nd.geometric_transform (im,xy_tran) try: ndis.display(im,zscale=False) except IOError,err: sys.stderr.write('\n ***** ERROR: %s Start DS9.\n' % str(err)) sys.exit(1) print " Mark center with left button, then use 'q' to continue, 's' to skip." cursor = ndis.readcursor(sample=0) cursor = cursor.split() if cursor[3] == 's': hdr.update("XCEN",-1, "Start mask x-center") hdr.update("YCEN",-1, "Start mask y-center") updated = True print '\nFrame skipped... ****Make sure not to use it in your science script. ***\n' #return updated,xcen,ycen,im return xcen,ycen,im x1 = float(cursor[0]) y1 = float(cursor[1]) xcen,ycen = gcentroid(im, x1,y1,9) xcen = xcen[0] ycen = ycen[0] hdr.update("XCEN",xcen, "Start mask x-center") hdr.update("YCEN",ycen, "Start mask y-center") log.info(' (%.2f,%.2f) :: ' % (xcen,ycen)+('red','blue')[ext-1])
def plot_footprint_cut(data,x,y,orientation,evf1,evf2,region,filename,instru): """ Plot the footprint cut inside a rectangle then display the same cut on frame 2. """ try: from stsci.numdisplay import display except ImportError: from numdisplay import display pl.clf() bval = data.max() bval += bval/10. #rx1,rx2,ry1,ry2 = region ny,nx = data.shape rxmin,rymin=(nx,ny) rxmax,rymax=(0,0) if (True): if orientation == 0: x = np.arange(nx) rxmin,rxmax = (0,nx) for evf,color in zip([evf1,evf2],['b','r']): zy = evf(x) pl.plot(x,zy,color) imin = np.argmin(zy) imax = np.argmax(zy) rymin = min(rymin,zy[imin]) rymax = max(rymax,zy[imax]) pl.fill([rxmin,rxmax,rxmax,rxmin], [rymin,rymin,rymax,rymax],fill=False) else: # Orientation is 90 y = np.arange(ny) rymin,rymax = (0,ny) for evf,color in zip([evf1,evf2],['b','r']): zx = evf(y) pl.plot(zx,y,color) imin = np.argmin(zx) imax = np.argmax(zx) rxmin = min(rxmin,zx[imin]) rxmax = max(rxmax,zx[imax]) pl.fill([rxmin,rxmax,rxmax,rxmin], [rymin,rymin,rymax,rymax],fill=False) fname = os.path.basename(filename) pl.title(fname+' ('+instru+')') #pl.xlabel(str(region)) pl.draw() display(data,frame=2,z1=0,z2=bval,quiet=True) time.sleep(1)
def atd9(): """ Verify that transformation preserve flux. The test creates a mosaic from an input AstroData object and displays it in the DS9 display. Using IRAF imexam we measure objects that are on the the middle block for GMOS or the lower left block for GSAOI. We also measure the same objects in the input amplifers. We calculate the magnitude difference. """ print('\n atd9 REQUIREMENT.......') print('Conserve flux after "transforming"') ad = astrodata.open(file) # Now make a mosaicAD object using the input ad # and the default mosaic function named # gemini_mosaic_function. mo = MosaicAD(ad, gemini_mosaic_function) mosaic_data = mo.mosaic_image_data() display(mosaic_data,frame=1) display(ad['SCI',3].data,frame=2) return
def _plot_footprints(image,footprints): """ NOTE: This for development. Not for public release Plot the edges in the list array self.trace Plot the footprint edges using the fitting functions. """ try: from stsci.numdisplay import display except ImportError: from numdisplay import display orientation = footprints[0].edges[0].orientation pl.clf() med = np.median(np.where(image>0,image,0)) for k,footprint in enumerate(footprints): edge1 = footprint.edges[0]; edge2 = footprint.edges[1] xx1,yy1 = np.asarray(edge1.trace,int) xx2,yy2 = np.asarray(edge2.trace,int) # Plot (x_array,y_array) evf1 = edge1.evalfunction evf2 = edge2.evalfunction if orientation == 90: xx1 = np.asarray(evf1(yy1),int) xx2 = np.asarray(evf2(yy2),int) pl.plot(xx1,yy1,'b',xx2,yy2,'r') else: yy1 = np.asarray(evf1(xx1),int) yy2 = np.asarray(evf2(xx2),int) pl.plot(xx1,yy1,'b',xx2,yy2,'r') image[yy1,xx1]=med*2 image[yy2,xx2]=med*2 display(image,frame=2,quiet=True)
def plot_footprint(ad): """ Plot and display the edges found by trace_footprints. This information is the TRACEFP bintable extension in the AD object. """ try: from stsci.numdisplay import display except ImportError: from numdisplay import display if type(ad) is list: ad=ad[0] if ad.instrument() == 'F2': orientation = 90 else: if ad['SCI',1].dispersion_axis() == 1: orientation = 0 else: orientation = 90 pl.clf() tb = ad['TRACEFP'].data data = ad['SCI',1].data for rec in tb: region = rec.field('region') cutrange1 = rec.field('cutrange1') cutrange2 = rec.field('cutrange2') coeff1 = rec.field('cutcoeff1') coeff2 = rec.field('cutcoeff2') evf1 = set_evalfunction(coeff1) evf2 = set_evalfunction(coeff2) if orientation == 0: x1,x2,y1,y2 = region x = np.arange(x1,x2+1) pl.plot(x,evf1(x)+y1,'b') # edge_1 pl.plot(x,evf2(x)+y1,'r') # edge_2 for x in np.arange(int(x1),int(x2+1.),20): xi = slice(x,x+5) xr = np.arange(x,x+5) yi = list(evf1(xr)+y1) + list(evf2(xr)+y1) data[yi,xi] = 9999 else: # Orientation is 90 x1,x2,y1,y2 = region y = np.arange(y1,y2+1) pl.plot(evf1(y)+x1,y,'b') # edge 1 pl.plot(evf2(y)+x1,y,'r') # edges 2 off = evf1(y1) for y in range(int(y1),int(y2+1.),20): yi = slice(y,y+5) xi = list(evf1(np.arange(y,y+5))+x1-off) + \ list(evf2(np.arange(y,y+5))+x1-off) data[yi,xi] = 99999 fname = os.path.basename(ad.filename) pl.title(fname+' ('+ad.instrument()+')') display(data,frame=1,quiet=True) time.sleep(3)
def ncqlook(inputs, idir='', odir='./', log=True,lists=True, saturate=5000, nodisplay=False, full=False, port=5137): """ nqlook Get NICI data from SBF: /net/petrohue/dataflow repository for last night date. Produces a quick display of each frame plus log file and lists that can be used with niciprepare, nicimkflats and niciscience scripts. The output lists are written in the working directory. nqlook inputs='20081116' saturate=2000 display=False Get data from 20081116 with a saturation limit at 2000 and does not display (for speed)' nqlook @inlis idir='/data/myfiles' saturate=2000 display=False Get data from 'inlis' which is a list of input FITS files pathnames -one per line. The actual files are in the directory 'idir' Use a saturation limit at 2000 and does not display (for speed)' Even though you have 'inputs' parameter in here, it is not taken into account since all the FITS files from the input directory will be read. NOTE: The files bad_r.fits ans bad_b.fits are read from the nici directory wherever is it installed. """ if (inputs == None): today = datetime.date.today() dd = today.timetuple() inputs = str(dd[0]) + '%.2d'%dd[1] + '%.2d'%dd[2] if nodisplay == False: # NOTE: pyds9 should be installed in ritchie. # Needs to be part of the general installation and # an explanatory should be given in the installation Guide #ds9 = sao.ds9() try: pp = 'inet:%d' % port ndis.open(imtdev=pp) except: print "\n ****************** WARNING: Please start ds9." return #else: # ndis.close() # We need to know were we are. 'nici' is the package name fp, pathname, description = imp.find_module('nici') pathname += '/' #loads the badpixels list for the blue and red channel bad_b = pf.getdata(pathname+'bad_b.fits') bad_r = pf.getdata(pathname+'bad_r.fits') # Chech that we can write in the current directory # the output FITS file is written here. odir = os.path.join(odir,'') # Make sure odir has '/' if not os.access(odir,os.W_OK): print "\nERROR: You do not have write access to \ this directory (output FITS file)\n" return if idir != '': idir = os.path.join(idir,'') # Make sure odir has '/' if not os.access(idir,os.R_OK): print "\nERROR: No access to input directory:",idir return pathS = '/net/petrohue/dataflow/S' froot = 'ncqlook' if 'list' not in str(type(inputs)): date = str(inputs) fits_files = getFileList(date) # see if it date format 20yymmdd. Will not work for 20?.fits if len(fits_files) == 1: dd = fits_files[0] if len(dd) == 8 and dd[:2] == '20': print 'Looking for files: ' +pathS+date+'S*.fits' fits_files = glob.glob(pathS+date+'S*.fits') froot = date else: # it is already a list: fits_files = inputs command = 'ncqlook ('+str(inputs)+',idir='+idir+',odir='+odir+',log=' \ +str(log)+',lists='+str(lists)+',saturate='+str(saturate)\ +',nodisplay='+str(nodisplay)+',full='+str(full)+')' fits_files = np.sort(fits_files) if len(fits_files)==0: print 'couldn''t find files for :'+pathS +inputs+'S*.fits' return print '\nfound '+str(len(fits_files))+ ' files.' previous_angle = -999 #define a few dummy variables previous_time = -999 previous_ra = -99 previous_dec = -999 previous_mode = 'NONE' nfiles = 0 nff = len(fits_files) cube = np.zeros((nff,256,512),dtype=np.float32) hh = 'NAME DATALAB EXPTIME CEN_WAVE FILTER_R FILTER_B (MIN-MAX rms)'+\ ' [ext1,ext2 median] OBJECT CLASS MODE NCOADD CORE2HALO' print command print hh lg = 0 if nff > 0 and (log or lists): # Open outputfiles if log: lg = open(odir+froot+'.log','w+') lg.write('COMMAND: '+command+'\n') lg.write(hh+'\n') if lists: flis_1 = open(odir+froot+'.1_flats','w+') flis_2 = open(odir+froot+'.2_flats','w+') slis_adi = open(odir+froot+'.adi','w+') slis_sdi = open(odir+froot+'.sdi','w+') slis_asdi = open(odir+froot+'.asdi','w+') slis_other = open(odir+froot+'.other','w+') parser = mathtext.MathTextParser("Bitmap") for fitsname in fits_files: t1=time.time() fd = pf.open(idir+fitsname) # open the Fits file fname = os.path.basename(fitsname) if len(fd) == 0: print fname," is EMPTY." continue phu = fd[0].header inst = phu['instrume'] # instrument name for this image if inst != 'NICI': #if not NICI, get the next filename # print fname+' .. '+inst fd.close() continue if len(fd) != 3: line = ' Nici file '+fitsname+' does not have 2 extensions.' print line if log: lg.write(line+'\n') fd.close() continue nfiles += 1 im_ext1 = fd[1].data hd1 = fd[1].header # reads extension header 1 im_ext2 = fd[2].data hd2 = fd[2].header # reads extension header 1 shift_status1=-1 shift_status2=-1 # extension 1 and blue channel in # extension 2. # -1 if we do not have red channel # 0 is all good # 1 is shifted rows if hd1.get('filter_b') == None and hd1.get('filter_r') != None: shift_status1 = check_missing_rows (im_ext1,bad_r,log,lg) if hd2.get('filter_b') != None and hd1.get('filter_r') == None: shift_status2 = check_missing_rows (im_ext2,bad_b,log,lg) devs = np.zeros(8, dtype=np.float32) # Extracting the RMS of the # 8x8-folded image sample. A high value may # indicate readout noise problems for sample in range(4): x1 = 200+600*(sample % 2) x2 = 263+600*(sample % 2) y1 = 200+600*(sample/2) y2 = 263+600*(sample/2) devs[sample] = np.std(fold_88(im_ext1[x1:x2,y1:y2])) for sample in range(4): x1 = 200+600*(sample % 2) x2 = 263+600*(sample % 2) y1 = 200+600*(sample/2) y2 = 263+600*(sample/2) devs[sample+4] = np.std(fold_88(im_ext2[x1:x2,y1:y2])) ncoadd_r = hd1['ncoadd_r'] ncoadd_b = hd2['ncoadd_b'] itime_r = hd1['itime_r'] crmode = phu.get('crmode') if crmode == None: line = 'Header keyword CRMODE not found in '+fname+' -skipped.' print line if log: lg.write(line+'\n') fd.close() continue # Start forming the output line. obstype = str(phu.get('obstype')) oclass = str(phu.get('obsclass')) dichr = str(phu.get('dichroic')).lower() #adi = '' chan = '' if 'mirror' in dichr: chan = 'B' if 'open' in dichr: chan = 'R' mask = phu.get('fpmw') if 'clear' in mask.lower(): mask = '-NoMASK' else: mask = '' tmp = obstype if obstype == 'FLAT': tmp = 'FLAT['+phu.get('gcalshut')+']' # 1-Channel (ADI) (Dichroic is Mirror*(100% blue) or Open (100% red)) # 2-Channel (SDI or ASDI) (Dichroic is H-50/50*, H-CH4, H/K) mode = "OTHER" if (obstype == 'FLAT' or obstype == 'DARK'): if chan: mode = 'ADI-'+chan else: mode = 'SDI' elif (oclass == 'science'): if (crmode == 'FIXED'): if chan: mode = 'ADI-'+chan+mask else: mode = 'ASDI'+mask elif (crmode == 'FOLLOW'): if chan: mode = 'Normal'+chan+mask else: mode = 'SDI'+mask elif '-ENG' in phu.get('OBSID'): mode = '* ENGINEERING DATA *' else: mode = '***WARNING***: Not ADI, SDI nor ASDI-- '+crmode+' '+dichr line = ' '+str(phu.get('object'))+','+ oclass+','+tmp+','+mode ti_coad = ' '+str(itime_r)+','+str(ncoadd_r) dlab =' '+phu.get('DATALAB') exp_time =' %.2f'%(itime_r*ncoadd_r/60.) # exposure time ... in minutes! cenwave =' %.3f'%(float(phu.get('WAVELENG'))/10000.) # Central wavelenght in microns filter_r = ' %10.10s'%phu.get('FILTER_R') filter_b = ' %10.10s'%phu.get('FILTER_B') mindevs = ' (%8.2f,'%min(devs) maxdevs = '%8.2f)'%max(devs) medext1 = ' [%8.2f,'%np.median(im_ext1) medext2 = '%8.2f]'%np.median(im_ext2) longl = fname+dlab+exp_time+cenwave+filter_r+filter_b+mindevs+maxdevs+medext1+medext2+line+ti_coad if 'ENGINE' in mode: print longl if log: lg.write(longl+'\n') continue # hh = 'MIN-MAX rms 8x8 boxes: (min - max) MEDIAN: [ext1,ext2],(OBJECT),'\ # '(OBSCLASS),(OBSTYPE),(CRMODE),(DICHROIC),(FPMW),(ITIME),(NCOADD)' # Don't print until core2halo value is defined below. # print longl if lists: if (obstype == 'FLAT' or obstype == 'DARK'): if chan: flis_1.write(fname+'\n') else: flis_2.write(fname+'\n') elif oclass == 'science': md = mode.lower() if 'mask' in md: slis_other.write(fname+'\n') elif 'adi' in md: slis_adi.write(fname+'\n') elif 'asdi' in md: slis_asdi.write(fname+'\n') elif 'sdi' in md: slis_sdi.write(fname+'\n') else: slis_other.write(fname+'\n') # extast,hd1,astr ;extract the astrometric structure (astr) from the # first extension header # cd=astr.cd ;extract the CD matrix from the astrometric structure, # this matrix defines scale/rotation/shear ra_pointing = hd1['crval1'] # pointing RA from astrometry dec_pointing = hd1['crval2'] # pointing DEC st = np.asfarray(phu['st'].split(':')) st = dmstod(phu['st']) # sidereal time HA = st - ra_pointing/15. # defining the hour angle harr = HA + np.asfarray([0,-1/60.]) para_angle = parangle(harr, dec_pointing, -30.240750) # computing the paralactic angle. -30.240750 is the # latitude of Cerro Pachon obs_time = (dmstod(phu['LT'])*60.+720) % 1440 # here time is expressed in minutes after noon angular_rate = para_angle[0] - para_angle[1] # computing the rate of change of the paralactic angle current_angle = para_angle[0] astrom_angular_rate = (current_angle - previous_angle)/ \ (obs_time - previous_time) # angle change as found by taking the difference in # angle between this frame and the previous one telescope_offset = np.sqrt( ((ra_pointing - previous_ra)*\ math.cos(np.radians(dec_pointing)))**2+\ (dec_pointing - previous_dec)**2 ) if (crmode == 'FIXED') and (previous_mode == 'FIXED'): # we are in ADI mode if (abs(angular_rate - astrom_angular_rate) > 1/10.) \ and (telescope_offset < 1E-3) and \ ((obs_time - previous_time) < 3*exp_time): print '---------- !!!!!!!! ---------------' print 'Expected angular rate : '+str(angular_rate)+\ ' deg/minute' print 'Astrometric angular rate : '+\ str(astrom_angular_rate)+' deg/minute' # CROP THE FRAMES and get core2halo ratios crop1=0; crop2=0; c2h_1=-1; c2h_2=-1 if full: out_ext1 = rebin(im_ext1,256,256) out_ext2 = rebin(im_ext2,256,256) else: Ndev = lambda im : (np.amax(im) - np.median(im))/robust_sigma(im) tmp = rebin(im_ext1,32,32) if Ndev(tmp) > 50: # We might have a peak: crop1 = 1 c2h_1,x,y = peak2halo('',image=im_ext1) if x-128 < 0 or x+128 > 1024 or y-128 < 0 or y+128 > 1024: out_ext1 = rebin(im_ext1,256,256) crop1 = 0 else: #shift frame to center out_ext1 = im_ext1[y-128:y+128,x-128:x+128] else: out_ext1 = rebin(im_ext1,256,256) tmp = rebin(im_ext2,32,32) if Ndev(tmp) > 50: # We might have a peak: crop2 = 1 c2h_2,x,y = peak2halo('',image=im_ext2) if x-128 < 0 or x+128 > 1024 or y-128 < 0 or y+128 > 1024: out_ext2 = rebin(im_ext2,256,256) crop2 = 0 else: #shift frame to center out_ext2 = im_ext2[y-128:y+128,x-128:x+128] else: out_ext2 = rebin(im_ext2,256,256) if 1: # Do not process if there is no red flux a = out_ext1/ncoadd_r saturated_1 = a > saturate saturated_1[0:20,0:140] = 0 sat_red = np.sum(saturated_1) #IDL out_ext1>=out_ext1[ (sort(out_ext1))[.01*n_elements(out_ext1)]] szx1 = np.size(out_ext1) sout = np.sort(out_ext1.ravel()) limit = sout[.01*szx1] xmin = sout[0]; xmax= sout[np.size(sout)-1] out_ext1 = np.clip(out_ext1, limit, xmax) # we put lower and upper limits and the 1% and # 99.5% (OR NCOADD*3500) of the image #IDL out_ext1<=( out_ext1[ (sort(out_ext1))[.995*n_elements(out_ext1)]] # < (sxpar(hd1,'ncoadd_r')*saturate) ) szx1 = np.size(out_ext1) if sout[.995*szx1] < ncoadd_r*saturate: limit = ncoadd_r*saturate else: limit = sout[.995*szx1] out_ext1 = np.clip(out_ext1, xmin, limit) if 1: a = out_ext2/ncoadd_b saturated_2 = nd.rotate(a > saturate,5,reshape=False) saturated_2[0:20,0:140] = 0 sat_blue = np.sum(saturated_2) szx2 = np.size(out_ext2) sout = np.sort(out_ext2.ravel()) limit = sout[.01*szx2] xmin = sout[0]; xmax= sout[np.size(sout)-1] out_ext2 = np.clip(out_ext2, limit, xmax) # we put lower and upper limits and the 1% and # 99.5% (OR NCOADD*3500) of the image #IDL: out_ext2<=( out_ext2[ (sort(out_ext2))[.995*n_elements(out_ext2)]] # < (sxpar(hd2,'ncoadd_b')*saturate) ) szx2 = np.size(out_ext2) if sout[.995*szx2] < ncoadd_b*saturate: limit = ncoadd_b*saturate else: limit = sout[.995*szx2] out_ext2 = np.clip(out_ext2, xmin, limit) out = np.zeros((256,512),dtype=np.float32) #create the image that will receive the frames to display out[:,:256] = ((out_ext1 - np.amin(out_ext1)) / \ (np.amax(out_ext1) - np.amin(out_ext1)))[::-1,:] a = out_ext2 - np.amin(out_ext2) b = np.amax(out_ext2) - np.amin(out_ext2) out[:,256:] = nd.rotate( (a/b), 5, reshape=False)[::-1,::-1] #extract the root name of the file. name_out = os.path.splitext(os.path.basename(fname))[0] name_out += '['+('full','crop')[crop1]+'/'+('full','crop')[crop2]+']' # Form the text to overlay onto the lower left of the output array. rgba, depth1 = parser.to_rgba(name_out, color='gray', fontsize=10, dpi=200) c = rgba[::2,::2,3] far = np.asarray(c,dtype=np.float32) stx = np.shape(far) out[:stx[0],:stx[1]] = far[::-1,:]/256 if not full: if sat_red != 0: tmp1=out[:,:256] tmp1[np.where(saturated_1)] = np.nan out[:,:256]=tmp1 if not full: if sat_blue != 0: tmp2=out[:,256:] tmp2[np.where(saturated_2)] = np.nan out[:,256:]=tmp2 if nodisplay == False: ndis.display(out,z1=-1,z2=1,zscale=False) #ds9.view(out) cube[nfiles-1,:,:] = out[::-1,:] # Now print if oclass == 'science': if chan == 'red': c2h_2=-1 if chan == 'blue': c2h_1=-1 if c2h_1 > 0 and c2h_2 > 0: longl += ',['+'%.2f,%.2f' % (c2h_1,c2h_2)+']' elif c2h_1*c2h_2 == 1: # both -1 longl += ',[,]' elif c2h_1 > 0: longl += ',['+'%.2f,' % (c2h_1)+']' else: longl += ',['+',%.2f' % (c2h_2)+']' if sat_red or sat_blue: longl += ' Saturated('+str(sat_red)+','+str(sat_blue)+')' print longl if log: lg.write(longl+'\n') previous_angle=current_angle #keep track of some variables for next iteration previous_time =obs_time previous_ra =ra_pointing previous_dec =dec_pointing previous_mode =crmode fd.close() if log: lg.close() if lists: flis_1.close() flis_2.close() slis_adi.close() slis_sdi.close() slis_asdi.close() slis_other.close() if (nfiles > 0): pf.writeto(odir+froot+'_cube.fits',cube[:nfiles,::-1,:], clobber=True) # write the output cube with cropped/binned images else: print 'No NICI files found for the specified input: ',inputs return
def at5(): """ The test instantiates a MosaicGeometry object from an input dictionary, a MosaicData object from a set of 4 data ndarrays and with these 2 object as input, instantiates a Mosaic object. """ import numpy as np try: from stsci.numdisplay import display except ImportError: from numdisplay import display print('\n at5 REQUIREMENT.......') print ('MosaicData uses the geometry dictionary to mosaic the input ndarrays.') geometry = { 'transformation': { 'shift': [(0., 0.), (-10, 20), (-10, 20), (0, 0)], 'rotation': (0.0, 0.0, 45.0, 45.), 'magnification': (1., 1., 1., .5), }, 'interpolator': 'linear', 'gap_dict': { 'tile_gaps': {(0,0):(20,30),(1,0):(20,30), (0,1):(20,30),(1,1):(20,30) }, 'transform_gaps': {(0, 0): (20, 30), (1, 0): (20, 30), (0, 1): (20, 30), (1, 1): (20, 30) }, }, 'blocksize': (200, 300), 'ref_block': (0, 0), # 0-based 'mosaic_grid': (2, 2) } mosaic_geometry = MosaicGeometry(geometry) # Make a rectangle (200,300) (wide,high). data = np.ones((300,200),dtype=np.float32) data = data * 20 # make the background 20 # Make a 2x2 array with this rectangle data_list = [data,data,data,data] # make a box 50x50 starting at (50,50) with value 100 for k in range(4): data_list[k][50: 101, 50: 101] = 100. # Mark the block borders with value 400 data_list[k][:,0] = 400 data_list[k][:,199]= 400 data_list[k][0,:] = 400 data_list[k][299,:]= 400 mosaic_data = MosaicData(data_list) # With these two objects we instantiate a Mosaic object mo = Mosaic(mosaic_data, mosaic_geometry) # Finally make the mosaic mosaic_data = mo.mosaic_image_data() # Success Criterion 1. ref_block = mo.geometry.ref_block blksz_x,blksz_y = mo.geometry.blocksize print(int(mosaic_data.shape[0]/blksz_y), int(mosaic_data.shape[1]/blksz_x)) # Success Criterion 2. x_gap,y_gap= mo.geometry.gap_dict['tile_gaps'][ref_block] nblkx,nblky = mo.geometry.mosaic_grid print('nx:', nblkx*blksz_x + (x_gap)*( nblkx-1)) print('ny:', nblky*blksz_y + (y_gap)*( nblky-1)) # Success Criterion 3. # Now display the mosaic and the mask display(mosaic_data,frame=1,z1=0,z2=400.5) # Results: # 1) Lower left block, top right corner of # test box: (101,101) # 2) Lower right block, top right corner # of test box: (200+20-10+101, 101+20) or (311,121); # gap: (20,0), shift: (-10,20) # 3) Top left block, top right corner of # test box gets rotated 45 degrees w/r to the # block center: (50*cos(45),(50-50*sin(45)) or (35.4,14.6) # in distance change of this corner. Now the new # positioning with respect to the mosaic origin is # (135.4-10,300+20+30+114.6) or (125.4, 464.6); # gap: (0,30), shift: (-10,20) # 4) top right block, top right corner of # test box gets rotated 45 degrees but we have a # magnification of 0.5 so the distance between the # block center and the top right corner is 25 now. # The distance change for rotation is then: (17.7,7.4) # This corner position with no rotation is (100,125) # now adding the rotation we have: (117.7,132.4). # The position w/r to the mosaic origin is: # (200+20+117.7, 300+30+132.4) or (337.7, 362.4) return
def nici_cntrd(im,hdr,center_im=True): """ Read xcen,ycen and update header if necessary If the automatic finding of the center mask fails, then the interactive session will start. The SAOIMAGE/ds9 display must be up. If the default port is busy, then it will use port 5199, so make sure you start "ds9 -port 5199". """ xcen = hdr.get('xcen') ycen = hdr.get('ycen') updated = False if (xcen == None): ratio,xc,yc = peak2halo('',image=im) #xcen = xc[0] #ycen = yc[0] xcen = xc ycen = yc if (xcen < 0 or ycen < 0): try: ndis.display(im) except IOError,err: sys.stderr.write('\n ***** ERROR: %s Start DS9.\n' % str(err)) sys.exit(1) print " Mark center with left button, then use 'q' to continue, 's' to skip." cursor = ndis.readcursor(sample=0) cursor = cursor.split() if cursor[3] == 's': hdr.update("XCEN",-1, "Start mask x-center") hdr.update("YCEN",-1, "Start mask y-center") updated = True print '\nFrame skipped... ****Make sure not to use it in your science script. ***\n' #return updated,xcen,ycen,im return xcen,ycen,im x1 = float(cursor[0]) y1 = float(cursor[1]) box = im[y1-64:y1+64,x1-64:x1+64].copy() box -= scipy.signal.signaltools.medfilt2d(np.float32(box),11) box = box[32:32+64,32:32+64] bbx = box * ((box>(-robust_sigma(box)*5)) & \ (box <(15*robust_sigma(box)))) imbb = congrid(bbx,(1024,1024)) ndis.display(imbb, name='bbx') del imbb cursor = ndis.readcursor(sample=0) cursor = cursor.split() x2 = float(cursor[0]) y2 = float(cursor[1]) xcen,ycen = gcentroid(box, x2/16., y2/16., 4) xcen = (xcen+x1)[0] - 32 ycen = (ycen+y1)[0] - 32 hdr.update("XCEN",xcen, "Start mask x-center") hdr.update("YCEN",ycen, "Start mask y-center") updated = True
def pyexam(scidata, function='both', pixelscale=1, frame=1, \ outFile='testout.txt', verbose=True, pymark=True, \ residuals=False, clip=True, sigma=3, boxSize=9., \ debug=False, niters=4.): import pylab from iqtool.gemplotlib import overlay # Get box size around each object apertureSize = fit.getApSize(pixelscale, boxSize) outstring = '%10s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s\n'%('function', 'Cx', 'Cy', 'Bg', 'Peak', 'Wx', 'Wy', 'CooX', 'CooY', 'Ellipticity', 'FWHM_pix','FWHM_arcsec', 'Theta', 'PA_deg', ' FWHMx', 'FWHMy', 'Beta') outFile.write(outstring) print 'PYEXAM - Please select objects using spacebar, and press \'q\' when finished' print ' pressing k, j, r, or e will make plots along with fits' keystroke = '' numdisplay.display(scidata, z1=scidata.mean()-scidata.std(), z2=scidata.mean()+scidata.std(), frame=frame) if verbose: print '%10s%12s%12s%12s%12s%12s%12s'%('function', 'Coox', 'CooY', 'FWHMpix', 'FWHMarcsec', 'Ellipticity', 'PAdeg') gAllstars = [] mAllstars = [] done = 'no' while done == 'no': # Get x,y cursorInput = numdisplay.readcursor(sample=0) components = cursorInput.split() xPos = float(components[0]) yPos = float(components[1]) keystroke = components[3] option = '' if keystroke == '\\040' or keystroke == 'a' or keystroke == 'k' or keystroke == 'j' or keystroke == 'e' or keystroke == 'r': print 'PYEXAM - X:',xPos,'Y:',yPos gfitArray=None mfitArray=None gfxn=None mfxn=None positionCoords = (xPos,yPos) stampArray, stampPars = fit.makeStamp(scidata, positionCoords, apertureSize, outFile, debug=debug) if stampArray == None: return None, None gfitPars, gReturnModel, mfitPars, mReturnModel = \ fit.fitprofile(stampArray, function, positionCoords, \ outFile, pixelscale, stampPars, debug=debug, frame=frame) imageDim = list(scidata.shape) # Gives [y,x] dimensions #imageDim.reverse() #give [x,y] dimensions if gfitPars != None: clipobsPars = ('gauss', gfitPars['CooX'], gfitPars['CooY'], gfitPars['FWHMpix'], gfitPars['FWHMarcsec'], gfitPars['Ellip'], gfitPars['PAdeg']) iqUtil.printPars(clipobsPars, verbose) gAllstars.append(gfitPars) if pymark: overlay.circle(x=gfitPars['CooX']-1,y=gfitPars['CooY']-1, \ radius=gfitPars['FWHMpix'],frame=frame,\ color=204) gfxn = gReturnModel((gfitPars['Bg'], gfitPars['Peak'], \ gfitPars['Cy'], \ gfitPars['Cx'], gfitPars['Wy'], \ gfitPars['Wx'], gfitPars['Theta']+90)) gfitArray = np.zeros(imageDim) gfitArray[0:stampPars[3],0:stampPars[1]] = gfxn(*np.indices((stampPars[3], stampPars[1]))) if mfitPars != None: clipobsPars = ('moffat', mfitPars['CooX'],mfitPars['CooY'], mfitPars['FWHMpix'], mfitPars['FWHMarcsec'], mfitPars['Ellip'], mfitPars['PAdeg']) iqUtil.printPars(clipobsPars, verbose) print mfitPars['CooX'] print mfitPars['CooY'] print mfitPars['Cx'] print mfitPars['Cy'] mAllstars.append(mfitPars) if pymark: overlay.circle(x=mfitPars['CooX'], y=mfitPars['CooY'], frame=frame, radius=mfitPars['FWHMpix'], color=212) mfxn = mReturnModel((mfitPars['Bg'], mfitPars['Peak'], \ mfitPars['Cy'],\ mfitPars['Cx'], mfitPars['Wy'], \ mfitPars['Wx'], mfitPars['Theta']+90, \ mfitPars['Beta'])) mfitArray = np.zeros(imageDim) mfitArray[0:stampPars[3],0:stampPars[1]] = mfxn(*np.indices((stampPars[3], stampPars[1]))) if keystroke == 'k': print "PYEXAM - Yslice Plotting" pylab.clf() pylab.plot(stampArray.max(0), 'ro') if mfitArray != None: pylab.plot(mfitArray.max(0), 'b', label='moffat fit FWHM='+str(mfitPars['FWHMarcsec'])) pylab.axis([0,stampPars[1]-stampPars[0],mfitPars['Bg'],1.3*(mfitPars['Bg']+mfitPars['Peak'])]) if gfitArray != None: pylab.plot(gfitArray.max(0), 'g',label='gauss fit FWHM='+str(gfitPars['FWHMarcsec'])) pylab.axis([0,stampPars[1]-stampPars[0],gfitPars['Bg'],1.3*(gfitPars['Bg']+gfitPars['Peak'])]) pylab.legend() if keystroke == 'j': print "PYEXAM - Xslice Plotting" pylab.clf() pylab.plot(stampArray.max(1), 'ro') if mfitArray != None: pylab.plot(mfitArray.max(1), 'b', label='moffat fit FWHM='+str(mfitPars['FWHMarcsec'])) pylab.axis([0,stampPars[1]-stampPars[0],mfitPars['Bg'],1.3*(mfitPars['Bg']+mfitPars['Peak'])]) if gfitArray != None: pylab.plot(gfitArray.max(1), 'g',label='gauss fit FWHM='+str(gfitPars['FWHMarcsec'])) pylab.axis([0,stampPars[1]-stampPars[0],gfitPars['Bg'],1.3*(gfitPars['Bg']+gfitPars['Peak'])]) pylab.legend() if keystroke == 'r': print "PYEXAM - radial Plotting doesnt work yet!" print "new version" #rad1 = np.sqrt(stampArray.max(1)**2 + stampArray.max(0)**2) pylab.clf() pylab.plot(stampArray.max(1), 'ro') if mfitArray != None: pylab.plot(mfitArray.max(1), 'b', label='moffat fit FWHM='+str(mfitPars['FWHMarcsec'])) pylab.axis([0,stampPars[1]-stampPars[0],mfitPars['Bg'],1.3*(mfitPars['Bg']+mfitPars['Peak'])]) if gfitArray != None: pylab.plot(gfitArray.max(1), 'g',label='gauss fit FWHM='+str(gfitPars['FWHMarcsec'])) pylab.axis([0,stampPars[1]-stampPars[0],gfitPars['Bg'],1.3*(gfitPars['Bg']+gfitPars['Peak'])]) pylab.legend() if keystroke == 'e': print 'PYEXAM - Plotting contours' pylab.clf() pylab.contour(stampArray) elif keystroke == 'q': done = 'yes' if verbose: print "Done looping through stars" if clip: if gAllstars: if verbose: print "# PYEXAM - performing Gaussian clipping" gAllstars, gEllMean, gEllSigma, gFWHMMean, gFWHMSigma = \ iqUtil.sigmaClip(gAllstars, outFile, sigma, verbose, niters, garbageStat=garbageStat) if verbose: print "PYEXAM - Mean Gaussian Ellipticity:", gEllMean print "PYEXAM - Mean Gaussian FWHM (""):", gFWHMMean if mAllstars: if verbose: print "# PYEXAM - performing Moffat clipping" mAllstars, mEllMean, mEllSigma, mFWHMMean, mFWHMSigma = \ iqUtil.sigmaClip(mAllstars, outFile, sigma, verbose, niters, garbageStat=garbageStat) if verbose: print "PYEXAM - Mean Moffat Ellipticity:", mEllMean print "PYEXAM - Mean Moffat FWHM (""):", mFWHMMean for star in gAllstars: iqUtil.writePars(star, outFile, 'gaussian') for star in mAllstars: iqUtil.writePars(star, outFile, 'moffat')
def at5(): """ The test instantiates a MosaicGeometry object from an input dictionary, a MosaicData object from a set of 4 data ndarrays and with these 2 object as input, instantiates a Mosaic object. """ import numpy as np from gempy.library.mosaic import Mosaic,MosaicGeometry, MosaicData try: from stsci.numdisplay import display except ImportError: from numdisplay import display print '\n at5 REQUIREMENT.......' print ('*****MosaicData shall correctly use the geometry dictionary to mosaic the input ndarrays.') geometry = { 'transformation': { 'shift': [(0.,0.), (-10,20), (-10,20), (0,0)], 'rotation': (0.0, 0.0, 45.0, 45.), 'magnification': (1., 1., 1., .5), }, 'interpolator': 'linear', 'gap_dict': { 'tile_gaps': {(0,0):(20,30),(1,0):(20,30), (0,1):(20,30),(1,1):(20,30)}, 'transform_gaps': {(0,0):(20,30),(1,0):(20,30), (0,1):(20,30),(1,1):(20,30)}, }, 'blocksize': (200,300), 'ref_block': (0,0), # 0-based 'mosaic_grid': (2,2) } mosaic_geometry = MosaicGeometry(geometry) # Make a rectangle (200,300) (wide,high). data = np.ones((300,200),dtype=np.float32) data = data*20 # make the background 20 # Make a 2x2 array with this rectangle data_list = [data,data,data,data] # Inside each block, make a small box 50x50 # starting at (50,50) with value 100 for k in range(4): data_list[k][50:101,50:101] = 100. # Mark the block borders with value 400 data_list[k][:,0] =400 data_list[k][:,199]=400 data_list[k][0,:] =400 data_list[k][299,:]=400 # Now create the MosaicData object mosaic_data = MosaicData(data_list) # With these two objects we instantiate a Mosaic object mo = Mosaic(mosaic_data, mosaic_geometry) # Finally make the mosaic mosaic_data = mo.mosaic_image_data() # Success Criteria 1. ref_block = mo.geometry.ref_block blksz_x,blksz_y = mo.geometry.blocksize print (int(mosaic_data.shape[0]/blksz_y), int(mosaic_data.shape[1]/blksz_x)) # Success Criteria 2. x_gap,y_gap= mo.geometry.gap_dict['tile_gaps'][ref_block] nblkx,nblky = mo.geometry.mosaic_grid print 'nx:', nblkx*blksz_x + (x_gap)*( nblkx-1) print 'ny:', nblky*blksz_y + (y_gap)*( nblky-1) # Success Criteria 3. # Now display the mosaic and the mask display(mosaic_data,frame=1,z1=0,z2=400.5)
def frcheck(infile): #<--- count number of lines in the list and backup the orignal file ---> # open list try: f = open(infile) except IOError: print >> sys.stderr, 'cannot open %s' % infile return 1 nline = 0 orgfile = infile + ".org" f_org = open(orgfile, 'w') for line in f: f_org.write(line) if not line.startswith('#'): nline += 1 # close list f.close() f_org.close() #<--- input the file names into array ---># # open list f = open(infile) fname = [[] for i in range(nline)] frchk = [[] for i in range(nline)] i = 0 for line in f: if not line.startswith('#'): fname[i] = line[:-1] if not os.access(fname[i], os.R_OK): print >> sys.stderr, "cannot read %s" % (fname[i]) f.close() return 1 i += 1 # close list f.close() #<--- check frame ---> for i in range(nline): im1 = pyfits.open(fname[i]) coadds = float(im1[0].header['COADDS']) ndr = float(im1[0].header['NDR']) base1, ext = os.path.splitext(os.path.basename(fname[i])) if i != nline - 1: im2 = pyfits.open(fname[i + 1]) base2, ext = os.path.splitext(os.path.basename(fname[i + 1])) else: im2 = pyfits.open(fname[i - 1]) base2, ext = os.path.splitext(os.path.basename(fname[i - 1])) name = '%s-%s' % (base1, base2) sub = im1[0].data - im2[0].data z1_tmp = -1000.0 * float(coadds) * float(ndr) z2_tmp = 1000.0 * float(coadds) * float(ndr) zs = nd.zscale.zscale(sub, nsamples=1000, contrast=0.25) #print 'test: %s %s' % (str(zs[0]),str(zs[1])) if str(zs[0]) != 'nan': z1_tmp = zs[0] if str(zs[1]) != 'nan': z2_tmp = zs[1] nd.display(sub, bufname='imt1024', name=name, z1=z1_tmp, z2=z2_tmp) d.set('zoom to fit') check = '' while check != 'OK' and check != 'NG': check = raw_input('%s - %s (ok/ng): ' % (base1, base2)) if check.lower() == '' or check.lower() == 'ok': check = 'OK' elif check.lower() == 'ng': check = 'NG' else: print 'Error: unknown answer (%s)' % (check) print check if check == 'OK': frchk[i] = 0 else: frchk[i] = 1 # close fits files im1.close() im2.close() #<--- output bad removed list ---># # open list try: f = open(orgfile) except IOError: print >> sys.stderr, 'cannot read original file %s' % (orgfile) f_out = open(infile, 'w') i = 0 for line in f: if not line.startswith('#'): if i == 0: if frchk[i] == 0: f_out.write('%s\n' % fname[i]) else: f_out.write('#%s\n' % fname[i]) i += 1 else: if frchk[i] == 0 or frchk[i - 1] == 0: f_out.write('%s\n' % fname[i]) else: f_out.write('#%s\n' % fname[i]) i += 1 else: f_out.write(line) # close list f.close() f_out.close() return 0