def run():
    if os.path.exists('xia2-working.phil'):
        sys.argv.append('xia2-working.phil')
    try:
        check_environment()
    except Exception as e:
        traceback.print_exc(file=open('xia2.error', 'w'))
        Chatter.write('Status: error "%s"' % str(e))

    if len(sys.argv) < 2 or '-help' in sys.argv:
        help()
        sys.exit()

    wd = os.getcwd()

    try:
        multi_crystal_analysis()
        Chatter.write('Status: normal termination')

    except Exception as e:
        traceback.print_exc(file=open(os.path.join(wd, 'xia2.error'), 'w'))
        Chatter.write('Status: error "%s"' % str(e))
        Chatter.write(
            'Please send the contents of xia2.txt, xia2.error and xia2-debug.txt to:'
        )
        Chatter.write('*****@*****.**')
        sys.exit(1)
Exemple #2
0
def run():
    try:
        check_environment()
    except Exception as e:
        with open("xia2-error.txt", "w") as fh:
            traceback.print_exc(file=fh)
        logger.info('Status: error "%s"', str(e))

    if len(sys.argv) < 2 or "-help" in sys.argv:
        help()
        sys.exit()

    wd = os.getcwd()

    try:
        # xia2_index()
        from .xia2_main import xia2_main

        xia2_main(stop_after="index")
        logger.info("Status: normal termination")

    except Exception as e:
        with open(os.path.join(wd, "xia2-error.txt"), "w") as fh:
            traceback.print_exc(file=fh)
        logger.error('Status: error "%s"', str(e))
Exemple #3
0
def run():
    try:
        check_environment()
    except Exception as e:
        with open("xia2.error", "w") as fh:
            traceback.print_exc(file=fh)
        Chatter.write('Status: error "%s"' % str(e))
        sys.exit(1)

    if len(sys.argv) < 2 or "-help" in sys.argv:
        help()
        sys.exit()

    wd = os.getcwd()

    try:
        from xia2.command_line.xia2_main import xia2_main

        xia2_main(stop_after="integrate")
        Chatter.write("Status: normal termination")

    except Exception as e:
        with open(os.path.join(wd, "xia2.error"), "w") as fh:
            traceback.print_exc(file=fh)
        Chatter.write('Status: error "%s"' % str(e))
        sys.exit(1)
def run():
    if os.path.exists("xia2-working.phil"):
        sys.argv.append("xia2-working.phil")
    try:
        check_environment()
    except Exception as e:
        traceback.print_exc(file=open("xia2.error", "w"))
        Chatter.write('Status: error "%s"' % str(e))

    # print the version
    Chatter.write(Version)
    Citations.cite("xia2")

    start_time = time.time()

    assert os.path.exists("xia2.json")
    from xia2.Schema.XProject import XProject

    xinfo = XProject.from_json(filename="xia2.json")

    crystals = xinfo.get_crystals()
    for crystal_id, crystal in crystals.iteritems():
        # cwd = os.path.abspath(os.curdir)
        from libtbx import Auto

        scale_dir = PhilIndex.params.xia2.settings.scale.directory
        if scale_dir is Auto:
            scale_dir = "scale"
            i = 0
            while os.path.exists(os.path.join(crystal.get_name(), scale_dir)):
                i += 1
                scale_dir = "scale%i" % i
            PhilIndex.params.xia2.settings.scale.directory = scale_dir
        working_directory = Environment.generate_directory(
            [crystal.get_name(), scale_dir])
        # os.chdir(working_directory)

        crystals[crystal_id]._scaler = None  # reset scaler

        scaler = crystal._get_scaler()
        Chatter.write(xinfo.get_output())
        crystal.serialize()

    duration = time.time() - start_time

    # write out the time taken in a human readable way
    Chatter.write("Processing took %s" %
                  time.strftime("%Hh %Mm %Ss", time.gmtime(duration)))

    # delete all of the temporary mtz files...
    cleanup()

    write_citations()

    xinfo.as_json(filename="xia2.json")

    Environment.cleanup()
Exemple #5
0
def run():
    if os.path.exists("xia2-working.phil"):
        sys.argv.append("xia2-working.phil")
    try:
        check_environment()
    except Exception as e:
        with open("xia2-error.txt", "w") as fh:
            traceback.print_exc(file=fh)
        logger.error('Status: error "%s"', str(e))

    # print the version
    logger.info(Version)
    Citations.cite("xia2")

    start_time = time.time()

    assert os.path.exists("xia2.json")
    from xia2.Schema.XProject import XProject

    xinfo = XProject.from_json(filename="xia2.json")

    with cleanup(xinfo.path):
        crystals = xinfo.get_crystals()
        for crystal_id, crystal in crystals.items():
            scale_dir = PhilIndex.params.xia2.settings.scale.directory
            if scale_dir is Auto:
                scale_dir = "scale"
                i = 0
                while os.path.exists(os.path.join(crystal.get_name(), scale_dir)):
                    i += 1
                    scale_dir = "scale%i" % i
                PhilIndex.params.xia2.settings.scale.directory = scale_dir

            # reset scaler
            crystals[crystal_id]._scaler = None
            crystal._get_scaler()

            logger.info(xinfo.get_output())
            crystal.serialize()

        duration = time.time() - start_time

        # write out the time taken in a human readable way
        logger.info(
            "Processing took %s", time.strftime("%Hh %Mm %Ss", time.gmtime(duration))
        )

        write_citations()

        xinfo.as_json(filename="xia2.json")
Exemple #6
0
def run():
    if len(sys.argv) < 2 or "-help" in sys.argv or "--help" in sys.argv:
        help()
        sys.exit()

    if "-version" in sys.argv or "--version" in sys.argv:
        print(xia2.XIA2Version.Version)
        print(dials_version())
        ccp4_version = get_ccp4_version()
        if ccp4_version:
            print("CCP4 %s" % ccp4_version)
        sys.exit()

    xia2.Handlers.Streams.setup_logging(logfile="xia2.txt",
                                        debugfile="xia2-debug.txt")

    try:
        check_environment()
    except Exception as e:
        traceback.print_exc(file=open("xia2-error.txt", "w"))
        logger.debug(traceback.format_exc())
        logger.error("Error setting up xia2 environment: %s" % str(e))
        logger.warning(
            "Please send the contents of xia2.txt, xia2-error.txt and xia2-debug.txt to:"
        )
        logger.warning("*****@*****.**")
        sys.exit(1)

    wd = os.getcwd()

    try:
        xia2_main()
        logger.debug("\nTiming report:")
        logger.debug("\n".join(xia2.Driver.timing.report()))
        logger.info("Status: normal termination")
        return
    except Sorry as s:
        logger.error("Error: %s", str(s))
        sys.exit(1)
    except Exception as e:
        with open(os.path.join(wd, "xia2-error.txt"), "w") as fh:
            traceback.print_exc(file=fh)
        logger.debug(traceback.format_exc())
        logger.error("Error: %s", str(e))
        logger.warning(
            "Please send the contents of xia2.txt, xia2-error.txt and xia2-debug.txt to:"
        )
        logger.warning("*****@*****.**")
        sys.exit(1)
Exemple #7
0
def run():
    from libtbx.utils import Sorry
    if len(sys.argv) < 2 or '-help' in sys.argv or '--help' in sys.argv:
        help()
        sys.exit()

    if '-version' in sys.argv or '--version' in sys.argv:
        import xia2.XIA2Version
        print xia2.XIA2Version.Version
        print dials_version()
        ccp4_version = get_ccp4_version()
        if ccp4_version is not None:
            print 'CCP4 %s' % ccp4_version
        sys.exit()

    try:
        check_environment()
    except Exception as e:
        traceback.print_exc(file=open('xia2.error', 'w'))
        Chatter.write('Error setting up xia2 environment: %s' % str(e))
        Chatter.write(
            'Please send the contents of xia2.txt, xia2.error and xia2-debug.txt to:'
        )
        Chatter.write('*****@*****.**')
        sys.exit(1)

    wd = os.getcwd()

    try:
        xinfo = xia2_main()
        Chatter.write('Status: normal termination')

        Debug.write('\n------\nTiming summary:')
        import xia2.Driver.DefaultDriver
        xia2.Driver.DefaultDriver.output_timing_information()
        return xinfo
    except Sorry as s:
        Chatter.write('Error: %s' % str(s))
        sys.exit(1)
    except Exception as e:
        traceback.print_exc(file=open(os.path.join(wd, 'xia2.error'), 'w'))
        Chatter.write('Error: %s' % str(e))
        Chatter.write(
            'Please send the contents of xia2.txt, xia2.error and xia2-debug.txt to:'
        )
        Chatter.write('*****@*****.**')
        sys.exit(1)
Exemple #8
0
def run():
  try:
    check_environment()
  except Exception as e:
    traceback.print_exc(file = open('xia2.error', 'w'))
    Chatter.write('Status: error "%s"' % str(e))

  if len(sys.argv) < 2 or '-help' in sys.argv:
    help()
    sys.exit()

  wd = os.getcwd()

  try:
    from xia2.command_line.xia2_main import xia2_main
    xia2_main(stop_after='integrate')
    Chatter.write('Status: normal termination')

  except Exception as e:
    traceback.print_exc(file = open(os.path.join(wd, 'xia2.error'), 'w'))
    Chatter.write('Status: error "%s"' % str(e))
Exemple #9
0
def run():
    try:
        check_environment()
    except Exception as e:
        with open("xia2-error.txt", "w") as fh:
            traceback.print_exc(file=fh)
        logger.error('Status: error "%s"', str(e))

    if len(sys.argv) < 2 or "-help" in sys.argv:
        help()
        sys.exit()

    cwd = os.getcwd()

    try:
        from .xia2_main import xia2_main

        xia2_main(stop_after="integrate")
        logger.info("Status: normal termination")

        wd = os.path.join(cwd, "strategy")
        if not os.path.exists(wd):
            os.mkdir(wd)
        os.chdir(wd)

        CommandLine = get_command_line()
        xinfo = CommandLine.get_xinfo()
        crystals = xinfo.get_crystals()

        assert len(crystals) == 1
        crystal = list(crystals.values())[0]
        assert len(crystal.get_wavelength_names()) == 1
        wavelength = crystal.get_xwavelength(crystal.get_wavelength_names()[0])
        sweeps = wavelength.get_sweeps()

        from xia2.Handlers.Phil import PhilIndex

        params = PhilIndex.get_python_object()
        strategy_params = params.strategy
        if not len(strategy_params):
            strategy_params = [
                PhilIndex.get_scope_by_name("strategy")[0].extract()
            ]

        from dxtbx.model import MultiAxisGoniometer

        gonio = sweeps[0].get_imageset().get_goniometer()
        if (isinstance(gonio, MultiAxisGoniometer)
                and len(gonio.get_axes()) == 3 and gonio.get_scan_axis() == 2):
            from xia2.Wrappers.Dials.AlignCrystal import AlignCrystal

            align_crystal = AlignCrystal()
            align_crystal.set_experiments_filename(
                sweeps[0]._get_integrater().get_integrated_experiments())
            align_crystal.set_working_directory(wd)
            auto_logfiler(align_crystal)
            align_crystal.set_json_filename("%i_align_crystal.json" %
                                            align_crystal.get_xpid())
            align_crystal.run()
            logger.info("".join(align_crystal.get_all_output()))

        results_all = {}

        def process_one_strategy(args):
            assert len(args) == 4
            experiments, reflections, strategy, t_ref = args
            from xia2.Wrappers.EMBL import Best

            best = Best.BestStrategy()
            for isweep, (expt, refl) in enumerate(zip(experiments,
                                                      reflections)):
                from xia2.Wrappers.Dials.ExportBest import ExportBest

                export = ExportBest()
                export.set_experiments_filename(expt)
                export.set_reflections_filename(refl)
                export.set_working_directory(wd)
                auto_logfiler(export)
                prefix = "%i_best" % export.get_xpid()
                export.set_prefix(prefix)
                export.run()
                if isweep == 0:
                    best.set_t_ref(t_ref)
                    best.set_mos_dat("%s.dat" % prefix)
                    best.set_mos_par("%s.par" % prefix)
                best.add_mos_hkl("%s.hkl" % prefix)
            best.set_i2s(strategy.i_over_sigi)
            best.set_T_max(strategy.max_total_exposure)
            best.set_t_min(strategy.min_exposure)
            # best.set_trans_ref(25.0)
            best.set_S_max(strategy.max_rotation_speed)
            best.set_w_min(strategy.min_oscillation_width)
            best.set_M_min(strategy.multiplicity)
            best.set_C_min(strategy.completeness)
            best.set_GpS(strategy.dose_rate)
            best.set_shape(strategy.shape)
            best.set_susceptibility(strategy.susceptibility)
            best.set_anomalous(strategy.anomalous)

            best.set_detector("pilatus6m")
            best.set_working_directory(wd)
            auto_logfiler(best)
            xmlout = os.path.join(best.get_working_directory(),
                                  "%i_best.xml" % best.get_xpid())
            best.set_xmlout(xmlout)
            best.strategy()

            results = best.get_results_dict()
            results["description"] = strategy.description
            if "phi_end" not in results:
                results["phi_end"] = str(
                    float(results["phi_start"]) +
                    float(results["number_of_images"]) *
                    float(results["phi_width"]))
            from dxtbx.serialize import load

            expt = load.experiment_list(experiments[0])[0]
            results["spacegroup"] = (
                expt.crystal.get_space_group().type().lookup_symbol())
            return results

        args = []
        for istrategy, strategy in enumerate(strategy_params):
            imageset = sweeps[0].get_imageset()
            scan = imageset.get_scan()
            experiments = [
                sweep._get_integrater().get_integrated_experiments()
                for sweep in sweeps
            ]
            reflections = [
                sweep._get_integrater().get_integrated_reflections()
                for sweep in sweeps
            ]
            t_ref = scan.get_exposure_times()[0]
            args.append((experiments, reflections, strategy, t_ref))

        nproc = params.xia2.settings.multiprocessing.nproc
        from libtbx import easy_mp

        results = easy_mp.parallel_map(
            process_one_strategy,
            args,
            processes=nproc,
            method="multiprocessing",
            preserve_order=True,
            preserve_exception_message=True,
        )

        for istrategy, (result,
                        strategy) in enumerate(zip(results, strategy_params)):
            name = strategy.name
            description = strategy.description
            if name is None:
                name = "Strategy%i" % (istrategy + 1)
            results_all[name] = result
            multiplicity = result["redundancy"]
            try:
                multiplicity = "%.2f" % multiplicity
            except TypeError:
                pass
            logger.info("Strategy %i", istrategy)
            if description is not None:
                logger.info(description)
            logger.info(
                "Start / end / width: %.2f/%.2f/%.2f",
                float(result["phi_start"]),
                float(result["phi_end"]),
                float(result["phi_width"]),
            )
            logger.info(
                "Completeness / multiplicity / resolution: %.2f/%s/%.2f",
                float(result["completeness"]),
                multiplicity,
                float(result["resolution"]),
            )
            logger.info(
                "Transmission / exposure %.3f/%.3f",
                float(result["transmission"]),
                float(result["exposure_time"]),
            )

        with open("strategies.json", "wb") as f:
            json.dump(results_all, f, indent=2)

    except Exception as e:
        with open(os.path.join(cwd, "xia2-error.txt"), "w") as fh:
            traceback.print_exc(file=fh)
        logger.error('Status: error "%s"', str(e))
    os.chdir(cwd)
Exemple #10
0
def run():
    if len(sys.argv) < 2 or "-help" in sys.argv or "--help" in sys.argv:
        help()
        sys.exit()

    if "-version" in sys.argv or "--version" in sys.argv:
        print(xia2.XIA2Version.Version)
        print(dials_version())
        ccp4_version = get_ccp4_version()
        if ccp4_version is not None:
            print("CCP4 %s" % ccp4_version)
        sys.exit()

    xia2.Handlers.Streams.setup_logging(logfile="xia2.txt", debugfile="xia2-debug.txt")
    xia2.Handlers.Streams.reconfigure_streams_to_logging()

    try:
        check_environment()
    except Exception as e:
        traceback.print_exc(file=open("xia2.error", "w"))
        Debug.write(traceback.format_exc(), strip=False)
        Chatter.write("Error setting up xia2 environment: %s" % str(e))
        Chatter.write(
            "Please send the contents of xia2.txt, xia2.error and xia2-debug.txt to:"
        )
        Chatter.write("*****@*****.**")
        sys.exit(1)

    wd = os.getcwd()

    # Temporarily patch os.chdir() to help identify source of #214
    origpid = os.getpid()
    origchdir = os.chdir

    def chdir_override(arg):
        if os.getpid() != origpid:
            return origchdir(arg)
        # Try to determine the name of the calling module.
        # Use exception trick to pick up the current frame.
        try:
            raise Exception()
        except Exception:
            f = sys.exc_info()[2].tb_frame.f_back

        Debug.write(
            "Directory change to %r in %s:%d" % (arg, f.f_code.co_filename, f.f_lineno)
        )
        return origchdir(arg)

    os.chdir = chdir_override

    try:
        xia2_main()
        Debug.write("\nTiming report:")
        for line in xia2.Driver.timing.report():
            Debug.write(line, strip=False)

        Chatter.write("Status: normal termination")
        return
    except Sorry as s:
        Chatter.write("Error: %s" % str(s))
        sys.exit(1)
    except Exception as e:
        traceback.print_exc(file=open(os.path.join(wd, "xia2.error"), "w"))
        Debug.write(traceback.format_exc(), strip=False)
        Chatter.write("Error: %s" % str(e))
        Chatter.write(
            "Please send the contents of xia2.txt, xia2.error and xia2-debug.txt to:"
        )
        Chatter.write("*****@*****.**")
        sys.exit(1)
Exemple #11
0
def run():
    try:
        check_environment()
    except Exception as e:
        traceback.print_exc(file=open('xia2.error', 'w'))
        Chatter.write('Status: error "%s"' % str(e))

    if len(sys.argv) < 2 or '-help' in sys.argv:
        help()
        sys.exit()

    cwd = os.getcwd()

    try:
        from xia2.command_line.xia2_main import xia2_main
        xia2_main(stop_after='integrate')
        Chatter.write('Status: normal termination')

        wd = os.path.join(cwd, 'strategy')
        if not os.path.exists(wd):
            os.mkdir(wd)
        os.chdir(wd)

        CommandLine = get_command_line()
        xinfo = CommandLine.get_xinfo()
        crystals = xinfo.get_crystals()

        assert len(crystals) == 1
        crystal = crystals.values()[0]
        assert len(crystal.get_wavelength_names()) == 1
        wavelength = crystal.get_xwavelength(crystal.get_wavelength_names()[0])
        sweeps = wavelength.get_sweeps()

        from xia2.Handlers.Phil import PhilIndex
        params = PhilIndex.get_python_object()
        strategy_params = params.strategy
        if not len(strategy_params):
            strategy_params = [
                PhilIndex.get_scope_by_name('strategy')[0].extract()
            ]

        from dxtbx.model import MultiAxisGoniometer
        gonio = sweeps[0].get_imageset().get_goniometer()
        if (isinstance(gonio, MultiAxisGoniometer)
                and len(gonio.get_axes()) == 3 and gonio.get_scan_axis() == 2):
            from xia2.Wrappers.Dials.AlignCrystal import AlignCrystal
            align_crystal = AlignCrystal()
            align_crystal.set_experiments_filename(
                sweeps[0]._get_integrater().get_integrated_experiments())
            align_crystal.set_working_directory(wd)
            auto_logfiler(align_crystal)
            align_crystal.set_json_filename('%i_align_crystal.json' %
                                            align_crystal.get_xpid())
            align_crystal.run()
            Chatter.write("".join(align_crystal.get_all_output()))

        results_all = {}

        def process_one_strategy(args):
            assert len(args) == 4
            experiments, reflections, strategy, t_ref = args
            from xia2.Wrappers.EMBL import Best
            best = Best.BestStrategy()
            for isweep, (expt, refl) in enumerate(zip(experiments,
                                                      reflections)):
                integrater = sweep._get_integrater()
                from xia2.Wrappers.Dials.ExportBest import ExportBest
                export = ExportBest()
                export.set_experiments_filename(expt)
                export.set_reflections_filename(refl)
                export.set_working_directory(wd)
                auto_logfiler(export)
                prefix = '%i_best' % export.get_xpid()
                export.set_prefix(prefix)
                export.run()
                if isweep == 0:
                    imageset = sweep.get_imageset()
                    scan = imageset.get_scan()
                    best.set_t_ref(t_ref)
                    best.set_mos_dat('%s.dat' % prefix)
                    best.set_mos_par('%s.par' % prefix)
                best.add_mos_hkl('%s.hkl' % prefix)
            best.set_i2s(strategy.i_over_sigi)
            best.set_T_max(strategy.max_total_exposure)
            best.set_t_min(strategy.min_exposure)
            #best.set_trans_ref(25.0)
            best.set_S_max(strategy.max_rotation_speed)
            best.set_w_min(strategy.min_oscillation_width)
            best.set_M_min(strategy.multiplicity)
            best.set_C_min(strategy.completeness)
            best.set_GpS(strategy.dose_rate)
            best.set_shape(strategy.shape)
            best.set_susceptibility(strategy.susceptibility)
            best.set_anomalous(strategy.anomalous)

            best.set_detector('pilatus6m')
            best.set_working_directory(wd)
            auto_logfiler(best)
            xmlout = '%s/%i_best.xml' % (best.get_working_directory(),
                                         best.get_xpid())
            best.set_xmlout(xmlout)
            best.strategy()

            results = best.get_results_dict()
            results['description'] = strategy.description
            if 'phi_end' not in results:
                results['phi_end'] = str(
                    float(results['phi_start']) +
                    float(results['number_of_images']) *
                    float(results['phi_width']))
            from dxtbx.serialize import load
            expt = load.experiment_list(experiments[0])[0]
            results['spacegroup'] = expt.crystal.get_space_group().type(
            ).lookup_symbol()
            return results

        args = []
        for istrategy, strategy in enumerate(strategy_params):
            imageset = sweeps[0].get_imageset()
            scan = imageset.get_scan()
            experiments = [
                sweep._get_integrater().get_integrated_experiments()
                for sweep in sweeps
            ]
            reflections = [
                sweep._get_integrater().get_integrated_reflections()
                for sweep in sweeps
            ]
            t_ref = scan.get_exposure_times()[0]
            args.append((experiments, reflections, strategy, t_ref))

        nproc = params.xia2.settings.multiprocessing.nproc
        from libtbx import easy_mp
        results = easy_mp.parallel_map(process_one_strategy,
                                       args,
                                       processes=nproc,
                                       method='multiprocessing',
                                       preserve_order=True,
                                       preserve_exception_message=True)

        for istrategy, (result,
                        strategy) in enumerate(zip(results, strategy_params)):
            name = strategy.name
            description = strategy.description
            if name is None:
                name = 'Strategy%i' % (istrategy + 1)
            results_all[name] = result
            multiplicity = result['redundancy']
            try:
                mutiplicity = '%.2f' % multiplicity
            except TypeError:
                pass
            Chatter.write('Strategy %i' % istrategy)
            if description is not None:
                Chatter.write(description)
            Chatter.write('Start / end / width: %.2f/%.2f/%.2f' %
                          (float(result['phi_start']), float(
                              result['phi_end']), float(result['phi_width'])))
            Chatter.write(
                'Completeness / multiplicity / resolution: %.2f/%s/%.2f' %
                (float(result['completeness']), multiplicity,
                 float(result['resolution'])))
            Chatter.write('Transmission / exposure %.3f/%.3f' % (float(
                result['transmission']), float(result['exposure_time'])))

        import json
        with open('strategies.json', 'wb') as f:
            json.dump(results_all, f, indent=2)

    except Exception as e:
        traceback.print_exc(file=open(os.path.join(cwd, 'xia2.error'), 'w'))
        Chatter.write('Status: error "%s"' % str(e))
    os.chdir(cwd)
Exemple #12
0
def run():
    from libtbx.utils import Sorry
    if len(sys.argv) < 2 or '-help' in sys.argv or '--help' in sys.argv:
        help()
        sys.exit()

    if '-version' in sys.argv or '--version' in sys.argv:
        import xia2.XIA2Version
        print(xia2.XIA2Version.Version)
        print(dials_version())
        ccp4_version = get_ccp4_version()
        if ccp4_version is not None:
            print('CCP4 %s' % ccp4_version)
        sys.exit()

    try:
        check_environment()
    except Exception as e:
        traceback.print_exc(file=open('xia2.error', 'w'))
        Chatter.write('Error setting up xia2 environment: %s' % str(e))
        Chatter.write(
            'Please send the contents of xia2.txt, xia2.error and xia2-debug.txt to:'
        )
        Chatter.write('*****@*****.**')
        sys.exit(1)

    wd = os.getcwd()

    # Temporarily patch os.chdir() to help identify source of #214
    pid = os.getpid()
    origchdir = os.chdir

    def chdir_override(arg):
        if os.getpid() != pid:
            return origchdir(arg)
        # Try to determine the name of the calling module.
        # Use exception trick to pick up the current frame.
        try:
            raise Exception()
        except Exception:
            f = sys.exc_info()[2].tb_frame.f_back

        Debug.write('Directory change to %r in %s:%d' %
                    (arg, f.f_code.co_filename, f.f_lineno))
        return origchdir(arg)

    os.chdir = chdir_override

    try:
        xinfo = xia2_main()
        Chatter.write('Status: normal termination')

        Debug.write('\n------\nTiming summary:')
        import xia2.Driver.DefaultDriver
        xia2.Driver.DefaultDriver.output_timing_information()
        return xinfo
    except Sorry as s:
        Chatter.write('Error: %s' % str(s))
        sys.exit(1)
    except Exception as e:
        traceback.print_exc(file=open(os.path.join(wd, 'xia2.error'), 'w'))
        Chatter.write('Error: %s' % str(e))
        Chatter.write(
            'Please send the contents of xia2.txt, xia2.error and xia2-debug.txt to:'
        )
        Chatter.write('*****@*****.**')
        sys.exit(1)