Ejemplo n.º 1
0
    def data_ready(self):
        # Import within method to prevent cylindrical imports
        from reduction_variables.utils import InstrumentVariablesUtils, VariableUtils

        logger.info("Data ready for processing run %s on %s" % (str(self._data_dict['run_number']), self._data_dict['instrument']))
        
        instrument = InstrumentUtils().get_instrument(self._data_dict['instrument'])
        # Activate the instrument if it is currently set to inactive
        if not instrument.is_active:
            instrument.is_active = True
            instrument.save()
            
        status = StatusUtils().get_skipped() if instrument.is_paused else StatusUtils().get_queued()

        last_run = ReductionRun.objects.filter(run_number=self._data_dict['run_number']).order_by('-run_version').first()
        highest_version = last_run.run_version if last_run is not None else -1

        experiment, experiment_created = Experiment.objects.get_or_create(reference_number=self._data_dict['rb_number'])
        if experiment_created:
            experiment.save()
            
        script_text = InstrumentVariablesUtils().get_current_script_text(instrument.name)[0]


        run_version = highest_version+1
        reduction_run = ReductionRun( run_number=self._data_dict['run_number']
                                    , run_version=run_version
                                    , experiment=experiment
                                    , instrument=instrument
                                    , status=status
                                    , script=script_text
                                    )
        reduction_run.save()
        self._data_dict['run_version'] = reduction_run.run_version

        data_location = DataLocation(file_path=self._data_dict['data'], reduction_run=reduction_run)
        data_location.save()

        variables = InstrumentVariablesUtils().create_variables_for_run(reduction_run)
        if not variables:
            logger.warning("No instrument variables found on %s for run %s" % (instrument.name, self._data_dict['run_number']))
        
        
        reduction_script, arguments = ReductionRunUtils().get_script_and_arguments(reduction_run)
        self._data_dict['reduction_script'] = reduction_script
        self._data_dict['reduction_arguments'] = arguments

        if instrument.is_paused:
            logger.info("Run %s has been skipped" % self._data_dict['run_number'])
        else:
            self._client.send('/queue/ReductionPending', json.dumps(self._data_dict), priority=self._priority)
            logger.info("Run %s ready for reduction" % self._data_dict['run_number'])