# Path: osrefl/loaders/reduction/correction.py
# class Correction(object):
#     properties = []  # Property sheet for interacting with correction
#     def apply(self, data):
#         """
#         Apply the correction to the dataset, modifying the dataset
#         in the process.  The data is returned, allowing users to
#         apply multiple corrections in one step:
#              C2(C1(data))
#         Each correction will automatically be logged in the data using
#         data.log(str(Correction))
#         """
#         raise NotImplementedError
# 
#     def __str__(self):
#         """
#         Name of the correction, and enough detail to record in the
#         reduced data log.
#         """
#         raise NotImplementedError
# 
#     # ==== Inherited behaviours ====
#     def __call__(self, data):
#         data = copy(data)
#         data.log(str(self))
#         self.apply(data)
#         return data
# 
#     def __or__(self, other):
#         # "Correction | Correction" forms a pipeline
#         if isinstance(other,Correction):
#             return Pipeline([self,other])
#         raise NotImplementedError
# 
#     def __ror__(self, other):
#         # "Pipeline | Correction" is handled by pipeline
#         # "data | Correction" applies the correction to the data
#         if not isinstance(other, Pipeline):
#             return self(other)
#         raise NotImplementedError
# 
# Path: osrefl/loaders/reduction/fresnel.py
# class Fresnel:
#     """
#     Function for computing the Fresnel reflectivity for a single interface.
# 
#     rho,Vrho
#         scattering length density of substrate and vacuum
#     mu,Vmu
#         absorption of substrate and vacuum (note that we do not correct
#         for attenuation of the beam through the incident medium since we
#         do not know the path length).
#     sigma (angstrom)
#         interfacial roughness
#     """
#     def __init__(self, rho=1e-6, mu=0, sigma=0, Vrho=0, Vmu=0):
#         self.rho,self.Vrho,self.mu,self.Vmu,self.sigma = rho,Vrho,mu,Vmu,sigma
# 
#     def reflectivity(self, Q, L=1):
#         """
#         Compute the Fresnel reflectivity at the given Q/wavelength.
#         """
#         # If Q < 0, then we are going from substrate into incident medium.
#         # In that case we must negate the change in scattering length density
#         # and ignore the absorption.
#         drho = self.rho-self.Vrho
#         S = 4*pi*choose(Q<0,(-drho,drho)) \
#             + 2j*pi/L*choose(Q<0,(self.Vmu,self.mu))
#         kz = abs(Q)/2
#         f = sqrt(kz**2 - S)  # fresnel coefficient
# 
#         # Compute reflectivity amplitude, with adjustment for roughness
#         amp = (kz-f)/(kz+f) * exp(-2*self.sigma**2*kz*f)
#         # Note: we do not need to check for a divide by zero.
#         # Qc^2 = 16 pi rho.  Since rho is non-zero then Qc is non-zero.
#         # For mu = 0:
#         # * If |Qz| < Qc then f has an imaginary component, so |Qz|+f != 0.
#         # * If |Qz| > Qc then |Qz| > 0 and f > 0, so |Qz|+f != 0.
#         # * If |Qz| = Qc then |Qz| != 0 and f = 0, so |Qz|+f != 0.
#         # For mu != 0:
#         # * f has an imaginary component, so |Q|+f != 0.
# 
#         R = real(amp*conj(amp))
#         return R
# 
#     # Make the reflectivity method the default
#     __call__ = reflectivity


from math import *
from .correction import Correction
from .fresnel import Fresnel
    from reflectometry.reduction import normalize
    from .examples import ng7 as dataset
import reflectometry.model1d as reflfit
import numpy
    import pylab
    Returns a correction object that can be applied to data.
    """
    def _set_fresnel_coefficients(self):
        # SLDs calculated with NBCU.XLS from the NCNR website.
        if self.probe == 'neutron':
            rho_H2O,mu_H2O = -0.560, 0.001
            rho_D2O,mu_D2O = 5.756, 0.00
        elif self.probe == 'xray':
            rho_H2O,mu_H2O = 9.466, 0.098
            rho_D2O,mu_D2O = 8.516, 0.089
        else:
            raise ValueError, "water_ratio needs probe 'neutron' or 'xray'"
        D2O = self._D2O
        self.model.rho = 1e-6*((100-D2O)*(rho_H2O) + D2O*rho_H2O)/100.
        self.model.mu = 1e-6*((100-D2O)*(mu_H2O) + D2O*mu_H2O)/100.

    def _setD2O(self, D2O):
        self._D2O = D2O
        self._set_fresnel_coefficients
    def _getD2O(self):
        return self._D2O
    def _setprobe(self,probe):
        self._probe = probe
        self._set_fresnel_coefficients
    def _getprobe(self):
        return self._probe
    D2O = property(_getD2O,_setD2O)
    probe = property(_getprobe,_setprobe)

    def __init__(self,D2O=0,probe=None,background=0):

# Path: osrefl/loaders/reduction/correction.py
# class Correction(object):
#     properties = []  # Property sheet for interacting with correction
#     def apply(self, data):
#         """
#         Apply the correction to the dataset, modifying the dataset
#         in the process.  The data is returned, allowing users to
#         apply multiple corrections in one step:
#              C2(C1(data))
#         Each correction will automatically be logged in the data using
#         data.log(str(Correction))
#         """
#         raise NotImplementedError
# 
#     def __str__(self):
#         """
#         Name of the correction, and enough detail to record in the
#         reduced data log.
#         """
#         raise NotImplementedError
# 
#     # ==== Inherited behaviours ====
#     def __call__(self, data):
#         data = copy(data)
#         data.log(str(self))
#         self.apply(data)
#         return data
# 
#     def __or__(self, other):
#         # "Correction | Correction" forms a pipeline
#         if isinstance(other,Correction):
#             return Pipeline([self,other])
#         raise NotImplementedError
# 
#     def __ror__(self, other):
#         # "Pipeline | Correction" is handled by pipeline
#         # "data | Correction" applies the correction to the data
#         if not isinstance(other, Pipeline):
#             return self(other)
#         raise NotImplementedError
# 
# Path: osrefl/loaders/reduction/fresnel.py
# class Fresnel:
#     """
#     Function for computing the Fresnel reflectivity for a single interface.
# 
#     rho,Vrho
#         scattering length density of substrate and vacuum
#     mu,Vmu
#         absorption of substrate and vacuum (note that we do not correct
#         for attenuation of the beam through the incident medium since we
#         do not know the path length).
#     sigma (angstrom)
#         interfacial roughness
#     """
#     def __init__(self, rho=1e-6, mu=0, sigma=0, Vrho=0, Vmu=0):
#         self.rho,self.Vrho,self.mu,self.Vmu,self.sigma = rho,Vrho,mu,Vmu,sigma
# 
#     def reflectivity(self, Q, L=1):
#         """
#         Compute the Fresnel reflectivity at the given Q/wavelength.
#         """
#         # If Q < 0, then we are going from substrate into incident medium.
#         # In that case we must negate the change in scattering length density
#         # and ignore the absorption.
#         drho = self.rho-self.Vrho
#         S = 4*pi*choose(Q<0,(-drho,drho)) \
#             + 2j*pi/L*choose(Q<0,(self.Vmu,self.mu))
#         kz = abs(Q)/2
#         f = sqrt(kz**2 - S)  # fresnel coefficient
# 
#         # Compute reflectivity amplitude, with adjustment for roughness
#         amp = (kz-f)/(kz+f) * exp(-2*self.sigma**2*kz*f)
#         # Note: we do not need to check for a divide by zero.
#         # Qc^2 = 16 pi rho.  Since rho is non-zero then Qc is non-zero.
#         # For mu = 0:
#         # * If |Qz| < Qc then f has an imaginary component, so |Qz|+f != 0.
#         # * If |Qz| > Qc then |Qz| > 0 and f > 0, so |Qz|+f != 0.
#         # * If |Qz| = Qc then |Qz| != 0 and f = 0, so |Qz|+f != 0.
#         # For mu != 0:
#         # * f has an imaginary component, so |Q|+f != 0.
# 
#         R = real(amp*conj(amp))
#         return R
# 
#     # Make the reflectivity method the default
#     __call__ = reflectivity


# Path: osrefl/loaders/reduction/ratiocor.py
from math import *
from .correction import Correction
from .fresnel import Fresnel
    from reflectometry.reduction import normalize
    from .examples import ng7 as dataset
import reflectometry.model1d as reflfit
import numpy
    import pylab

    Returns a correction object that can be applied to data.
    """
    def _set_fresnel_coefficients(self):
        # SLDs calculated with NBCU.XLS from the NCNR website.
        if self.probe == 'neutron':
            rho_H2O,mu_H2O = -0.560, 0.001
            rho_D2O,mu_D2O = 5.756, 0.00
        elif self.probe == 'xray':
            rho_H2O,mu_H2O = 9.466, 0.098
            rho_D2O,mu_D2O = 8.516, 0.089
        else:
            raise ValueError, "water_ratio needs probe 'neutron' or 'xray'"
        D2O = self._D2O
        self.model.rho = 1e-6*((100-D2O)*(rho_H2O) + D2O*rho_H2O)/100.
        self.model.mu = 1e-6*((100-D2O)*(mu_H2O) + D2O*mu_H2O)/100.

    def _setD2O(self, D2O):
        self._D2O = D2O
        self._set_fresnel_coefficients
    def _getD2O(self):
        return self._D2O
    def _setprobe(self,probe):
        self._probe = probe
        self._set_fresnel_coefficients
    def _getprobe(self):
        return self._probe
    D2O = property(_getD2O,_setD2O)
    probe = property(_getprobe,_setprobe)

    def __init__(self,D2O=0,probe=None,background=0):

        self.model = Fresnel(sigma=3)