コード例 #1
0
    def get_cal_req(self, inputs, caltype, write_input = False):
        """
        For each input finds astrodata type to find corresponding xml file,
        loads the file.         
        
        @param inputs: list of input fits AstroData instances
        @type inputs: list
        
        @param caltype: Calibration, ie bias, flat, dark, etc.
        @type caltype: string
        
        @return: Returns a list of Calibration Request Events.
        @rtype: list
        """        
        reqEvents = []
        
        for inp in inputs:
            cr = CalibrationRequest()
            # print "CDL56:", repr(inp), inp.filename, str(inp)
            # @@REVIEW: write_input is a bad name... you write outputs!
            if write_input == True:
                if (False):
                # don't write files... we will use headers
                    if os.path.exists(inp.filename):
                        # then asked to write something already on disk and we
                        # don't want to blindly clobber... throw informative error
                        msg = "Overwriting %s with in-memory version " + \
                                     "to ensure a current version of the dataset " + \
                                     "is available " + \
                                     "to Calibration Service." 
                        msg = msg % inp.filename
                        self.log.warning(msg)
                    else:
                        self.log.status("Writing in-memory AstroData instance " +
                                   "to new disk file (%s) to ensure availability " +
                                   "to Calibration Service." % inp.filename)
                try:
                    inp.write(clobber = True)
                except AstroDataError("Mode is readonly"):
                    self.log.warning("Skipped writing dataset, as it was "
                                "readonly input. This write "
                                "is done to ensure the file in memory is on disk "
                                "as the calibration system inspects the file itself. "
                                "As the file is protected as readonly, the system will "
                                "assume it is unchanged since loading.")
            
            cr.filename = inp.filename
            cr.ad = inp
            cr.caltype = caltype
            # @@NOTE: should use IDFactory, not data_label which HAPPENS to be the id
            cr.datalabel = inp.data_label().for_db()
            
            ad = inp # saves me time, as I cut/pasted the below from a test script

            # Old version of the descriptor dictionary: this fails badly
            # for any data type that doesn't have all these descriptors
            # defined
            """
            #print "CDL99:", str(ad.ut_datetime())
            cr.descriptors =  {'instrument':ad.instrument().for_db(),
                         'observation_type': ad.observation_type().for_db(),
                         'data_label': ad.data_label().for_db(),
                         'detector_x_bin':ad.detector_x_bin().for_db(),
                         'detector_y_bin':ad.detector_y_bin().for_db(),
                         'read_speed_setting':ad.read_speed_setting().for_db(),
                         'gain_setting':ad.gain_setting().for_db(),
                         'amp_read_area':ad.amp_read_area().for_db(),
                         #'ut_datetime':repr(ad.ut_datetime().for_db()),
                         'ut_datetime':ad.ut_datetime().for_db(),
                         'exposure_time':ad.exposure_time().for_db(),
                         'object': ad.object().for_db(),
                         'filter_name':ad.filter_name().for_db(),
                         'focal_plane_mask':ad.focal_plane_mask().for_db(),
                         }
            """
            # List of all possible needed descriptors
            descriptor_list = ['amp_read_area',
                               'central_wavelength',
                               'data_label',
                               'detector_x_bin',
                               'detector_y_bin',
                               'disperser',
                               'exposure_time',
                               'coadds',
                               'filter_name',
                               'focal_plane_mask',
                               'gain_setting',
                               'instrument',
                               'nod_count',
                               'nod_pixels',
                               'object',
                               "observation_class",
                               'observation_type',
                               'program_id',
                               'read_speed_setting',
                               'ut_datetime',
                               'detector_roi_setting',
                               'data_section',
                               'read_mode',
                               'well_depth_setting',
                               'camera'
                               ]
            options = {'central_wavelength':'asMicrometers=True'}

            # Check that each descriptor works and returns a 
            # sensible value before adding it to the dictionary
            desc_dict = {}
            for desc_name in descriptor_list:
                if options.has_key(desc_name):
                    opt = options[desc_name]
                else:
                    opt = ''
                try:
                    exec_cmd = 'dv = ad.%s(%s)' % (desc_name,opt)
                    exec(exec_cmd)
                    # print "cdl161:"+exec_cmd+"=="+str(dv)
                except (ExistError,KeyError,DescriptorTypeError):
                    continue
                if dv is not None:
                    desc_dict[desc_name] = dv.for_db()
                else:
                    desc_dict[desc_name] = None
                
            cr.descriptors = desc_dict
            cr.types = ad.types
            
            reqEvents.append(cr)
            
        # Goes to reduction context object to add to queue
        return reqEvents