#!/usr/bin/env python
# -*- coding: utf8 -*-
#
#    Project: Fast Azimuthal integration 
#             https://forge.epn-campus.eu/projects/azimuthal
#
#    File: "$Id$"
#
#    Copyright (C) European Synchrotron Radiation Facility, Grenoble, France
#
#    Principal author:       Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

__author__ = "Jerome Kieffer"
__contact__ = "Jerome.Kieffer@ESRF.eu"
__license__ = "GPLv3+"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "21/12/2011"
__status__ = "production"
__doc__ = """ 
pyFAI-waxs is the Waxs script of pyFAI that allows data reduction for Wide Angle Scattering, 
producing output in 2-theta range output in radial dimension (and in degrees).

Parameters:
 -p=param.poni         PyFAI parameter file 
 -m=mask.edf           mask image
 -d=-2                 dummy value for dead pixels
 -dd=-1.1              delta dummy 

 -h                    print help and exit
 
    Usage:
python pyFAI-waxs -p=param.poni  file.edf file2.edf file3.edf
"""
import os, sys, time, fabio

import pyFAI
from pyFAI import AzimuthalIntegrator

if __name__ == "__main__":
    paramFile = None
    processFile = []
    dummy = None
    delta_dummy = None
    mask = None
    for param in sys.argv[1:]:
        if param.startswith("-p="):
            paramFile = param.split("=", 1)[1]
        elif param.startswith("-d="):
            dummy = float(param.split("=", 1)[1])
        elif param.startswith("-dd="):
            delta_dummy = float(param.split("=", 1)[1])
        elif param.startswith("-m="):
            mask = param.split("=", 1)[1]
        elif param.startswith("--version"):
            print(pyFAI.version + os.linesep)
            sys.exit(0)
        elif param.find("-h") in [0, 1]:
            print(__doc__)
            sys.exit(0)
        elif os.path.isfile(param):
            processFile.append(param)
    if paramFile and processFile:
        integrator = AzimuthalIntegrator()
        integrator.setChiDiscAtZero()
        integrator.load(paramFile)
        if mask is not None:
            mask = fabio.open(mask).data
        print integrator
        for oneFile in processFile:
            sys.stdout.write("Integrating %s --> " % oneFile)
            outFile = os.path.splitext(oneFile)[0] + ".xy"
            azimFile = os.path.splitext(oneFile)[0] + ".azim"
            data = fabio.open(oneFile).data.astype("float32")
            t0 = time.time()
            tth, I = integrator.xrpd(data=data, nbPt=min(data.shape), filename=outFile, correctSolidAngle=True,
                                     mask=mask, dummy=dummy, delta_dummy=delta_dummy)
            t1 = time.time()
            integrator.xrpd2(data, 1000, 360, azimFile,
                             mask=mask, dummy=dummy, delta_dummy=delta_dummy)
            print "%s\t 1D took  %.3fs, 2D took %.3fs" % (outFile, t1 - t0, time.time() - t1)
            print "raw int: %s ; integrated: %s " % (data.sum() / data.size, I.sum() / I.size)
    else:
        print(__doc__)

