def _init_as_astrodata(self): """ Initialize parameters to be used by as_astrodata. Creates a WCS object (pywcs) from the SCI header and form the output AD object with the PHU and MDF from the input AD. We are adding the TRACEFP extension as well for later use on the spectral reduction process. Input: self.ad: AD object. Output: adout: Output AD object with AD phu and MDF """ ad = self.ad # Start output AD with the original phu and the MDF extension. adout = AstroData(phu=ad.phu) adout.append(ad['MDF']) adout.append(ad['TRACEFP']) # Get wcs information. It is in the PHU try: self.wcs = pywcs.WCS(ad.phu.header) if not hasattr(self.wcs.wcs, 'cd'): self.wcs = None except: # Something wrong with WCS, set it to None self.wcs = None return adout
def runappend(f1=None, f2=None, auto=False): ad = AstroData(f1) md = AstroData(f2) pstr = "\n\n >>>>>>> AD <<<<<<<<\n" pstr += str(ad.infostr()) pstr += "\n\n >>>>>>> AD APPEND <<<<<<<<\n" pstr += str(md.infostr()) ad.append(moredata=md, auto_number=auto) pstr +="\n\n >>>>>>> NEW AD <<<<<<<<\n" pstr += str(ad.infostr()) print(pstr) return ad
def test_method_append_8(): ad = AstroData(TESTFILE) ad.append(header=header1, data=data1, extname='TESTH', extver=2) assert ad[3].header['EXTVER'] == 2
def test_method_append_6(): ad = AstroData(TESTFILE) initial_len = len(ad) ad.append(header=header1, data=data1, auto_number=True)
def test_method_append_5(): ad = AstroData(TESTFILE) ad.append(moredata=hdu1, auto_number=True) assert ad.hdulist[-1] == hdu1
def test_method_append_4(): ad = AstroData(TESTFILE) initial_len = len(ad) ad.append(moredata=hdu1, auto_number=True) assert len(ad) == initial_len + 1
def test_method_append_2(): ad = AstroData(TESTFILE) ad2 = AstroData(TESTFILE2) with pytest.raises(AstroDataError): ad.append(moredata=ad2)
def test_method_append_9(): ad = AstroData(TESTFILE) ad.append(header=header1, data=data1, extname='TESTH', extver=1) assert ad[('TESTH', 1)]
def test_method_close_2(): ad = AstroData(TESTFILE) ad.close() with pytest.raises(AstroDataError): ad.append(moredata=hdu1)
def makeFringeFrame(self,rc): # Instantiate the log log = gemLog.getGeminiLog(logType=rc["logType"], logLevel=rc["logLevel"]) # Log the standard "starting primitive" debug message log.debug(gt.log_message("primitive", "makeFringeFrame", "starting")) # Initialize the list of output AstroData objects adoutput_list = [] # Check for at least 3 input frames adinput = rc.get_inputs_as_astrodata() if len(adinput)<3: log.stdinfo('Fewer than 3 frames provided as input. ' + 'Not making fringe frame.') # Report the empty list to the reduction context rc.report_output(adoutput_list) else: rc.run("correctBackgroundToReferenceImage"\ "(remove_zero_level=True)") # If needed, do a rough median on all frames, subtract, # and then redetect to help distinguish sources from fringes sub_med = rc["subtract_median_image"] if sub_med: adinput = rc.get_inputs_as_astrodata() # Get data by science extension data = {} for ad in adinput: for sciext in ad["SCI"]: key = (sciext.extname(),sciext.extver()) if data.has_key(key): data[key].append(sciext.data) else: data[key] = [sciext.data] # Make a median image for each extension import pyfits as pf median_ad = AstroData() median_ad.filename = gt.filename_updater( adinput=adinput[0], suffix="_stack_median", strip=True) for key in data: med_data = np.median(np.dstack(data[key]),axis=2) hdr = pf.Header() ext = AstroData(data=med_data, header=hdr) ext.rename_ext(key) median_ad.append(ext) # Subtract the median image rc["operand"] = median_ad rc.run("subtract") # Redetect to get a good object mask rc.run("detectSources") # Add the median image back in to the input rc.run("add") # Add the object mask into the DQ plane rc.run("addObjectMaskToDQ") # Stack frames with masking from DQ plane rc.run("stackFrames(operation=%s)" % rc["operation"]) yield rc
def as_astrodata(self, extname=None, tile=False, block=None, return_ROI=True, return_associated_bintables=True, return_non_associations=True, update_catalog_method='wcs'): """ Returns an AstroData object containing by default the mosaiced IMAGE extensions, the merged associated BINTABLEs and all other non-associated extensions of any other type. WCS information in the headers of the IMAGE extensions and any pixel coordinates in BINTABLEs will be updated appropriately. :param extname: If None mosaic all IMAGE extensions. Otherwise only the given extname. This becomes the ref_extname. :type extname: (string). Default is None :param tile: (boolean). If True, the mosaics returned are not corrected for shifting and rotation. :param block: See description below in method 'mosaic_image_data'. :param return_ROI: (True). Returns the minimum frame size calculated from the location of the amplifiers in a given block. If False uses the blocksize value. :param return_associated_bintables: (True). If a bintable is associated to the ref_extname then is returned as a merged table in the output AD. If False, they are not returned in the output AD. :param return_non_associations (True). Specifies whether to return extensions that are not deemed to be associated with the ref_extname. :param update_catalog_method: ('wcs'). Specifies if the X and Y pixel coordinates of any source positions in the BINTABLEs are to be recalculated using the output WCS and the sources R.A. and Dec. values within the table. If set to 'transform' the updated X and Y pixel coordinates will be determined using the transformations used to mosaic the pixel data. In the case of tiling, a shift is technically being applied and therefore update_catalog_method='wcs' should be set internally (Not yet implemented). :type update_catalog_method: (string). Possible values are 'wcs' or 'transform'. """ # If extname is None create mosaics of all image data in ad, merge # the bintables if they are associated with the image extensions # and append to adout all non_associatiated extensions. Appending # these extensions to the output AD is controlled by # return_associated_bintables and return_non_associations. # Make blank ('') same as None; i.e. handle all extensions. if extname == '': extname = None if (extname != None) and (extname not in self.extnames): raise ValueError("as_astrodata: Extname '"+extname+\ "' not found in AD object.") adin = self.ad # alias # Load input data if data_list attribute is not defined. #if not hasattr(self, "data_list"): # self.data_list = self.get_data_list(extname) adout = AstroData() # Prepare output AD adout.phu = adin.phu.copy() # Use input AD phu as output phu adout.phu.header.update('TILED', ['FALSE', 'TRUE'][tile], 'False: Image Mosaicked, True: tiled') # Set up extname lists with all the extension names that are going to # be mosaiced and table extension names to associate. # if extname is None: # Let's work through all extensions if self.associated_im_extns: extname_list = self.associated_im_extns else: extname_list = self.im_extnames else: self.ref_extname = extname # Redefine reference extname if extname in self.associated_im_extns: self.associated_im_extns = [extname] # We need this extname only extname_list = [extname] elif extname in self.non_associated_extns: # Extname is not in associated lists; so clear these lists. extname_list = [] self.associated_im_extns = [] self.associated_tab_extns = [] elif extname in self.associated_tab_extns: # Extname is an associated bintable. extname_list = [] self.associated_im_extns = [] self.associated_tab_extns = [extname] else: extname_list = [extname] # ------ Create mosaic ndarrays, update the output WCS, create an # AstroData object and append to the output list. # Make the list to have the order 'sci','var','dq' svdq = [k for k in ['SCI','VAR','DQ'] if k in extname_list] # add the rest of the extension names. extname_list = svdq + list(set(extname_list)-set(svdq)) for extn in extname_list: # Mosaic the IMAGE extensions now mosarray = self.mosaic_image_data(extn,tile=tile,block=block, return_ROI=return_ROI) # Create the mosaic FITS header using the reference # extension header. header = self.mosaic_header(mosarray.shape,block,tile) # Generate WCS object to be used in the merging the object # catalog table for updating the objects pixel coordinates # w/r to the new crpix1,2. ref_wcs = pywcs.WCS(header) # Setup output AD new_ext = AstroData(data=mosarray,header=header) # Reset extver to 1. new_ext.rename_ext(name=extn,ver=1) adout.append(new_ext) if return_associated_bintables: # If we have associated bintables with image extensions, then # merge the tables. for tab_extn in self.associated_tab_extns: # adout will get the merge table new_tab = self.merge_table_data(ref_wcs, tile, tab_extn, block, update_catalog_method) adout.append(new_tab[0]) # If we have a list of extension names that have not tables extension # names associated, then mosaic them. # if return_non_associations: for extn in self.non_associated_extns: # Now get the list of extver to append if extn in self.im_extnames: # Image extensions # We need to mosaic image extensions having more # than one extver. # if adin.count_exts(extn) > 1: mosarray = self.mosaic_image_data(extn, tile=tile,block=block, return_ROI=return_ROI) # Get reference extension header header = self.mosaic_header(mosarray.shape,block,tile) new_ext = AstroData(data=mosarray,header=header) # Reset extver to 1. new_ext.rename_ext(name=extn,ver=1) adout.append(new_ext) else: self.log.warning("as_astrodata: extension '"+extn+\ "' has 1 extension.") adout.append(adin[extn]) if extn in self.tab_extnames: # We have a list of extvers for extv in self.tab_extnames[extn]: adout.append(adin[extn,extv]) # rediscover classifications. adout.refresh_types() return adout