import lsst.pipe.drivers.constructCalibs
assert type(config)==lsst.pipe.drivers.constructCalibs.DarkConfig, 'config is of type %s.%s instead of lsst.pipe.drivers.constructCalibs.DarkConfig' % (type(config).__module__, type(config).__name__)
import lsst.afw.display.rgb
import lsst.afw.display._displayLib
import lsst.obs.subaru.crosstalk
import lsst.obs.hsc.vignette
import lsst.obs.subaru.isr
import lsst.afw.display.displayLib
import lsst.obs.subaru.crosstalkYagi
# DataId keywords specifying a CCD
config.ccdKeys=['ccd']

# DataId keywords specifying a calibration
config.calibKeys=[]

import lsst.pipe.drivers.constructCalibs
config.combination.retarget(target=lsst.pipe.drivers.constructCalibs.DarkCombineTask, ConfigClass=lsst.pipe.drivers.constructCalibs.CalibCombineConfig)

# Clipping iterations for combination
config.combination.nIter=3

# Number of rows to read at a time
config.combination.rows=512

# Clipping iterations for background
config.combination.stats.nIter=3

# Statistic to use to estimate background (from lsst.afw.math)
config.combination.stats.stat=128

# Mask planes to reject
config.combination.stats.mask=['DETECTED', 'BAD']

# Clipping threshold for background
config.combination.stats.clip=3.0

# Clipping threshold for combination
config.combination.clip=3.0

# Mask planes to respect
config.combination.mask=['SAT', 'DETECTED', 'INTRP', 'CR']

# Statistic to use for combination (from lsst.afw.math)
config.combination.combine=128

# Interpolate over defects? (ignored unless you provide a list of defects)
config.repair.doInterpolate=True

# Type of statistic to calculate edge fallbackValue for interpolation
# Allowed values:
# 	None	Field is optional
# 	MEAN	mean
# 	MEDIAN	median
# 	USER	user value set in fallbackUserValue config
# 	MEANCLIP	clipped mean
# 
config.repair.interp.fallbackValueType='MEANCLIP'

# Add a Gaussian to represent wings?
config.repair.interp.modelPsf.addWing=True

# wing amplitude, as a multiple of core amplitude (dimensionless); ignored if addWing false
config.repair.interp.modelPsf.wingAmplitude=0.1

# Minimum kernel size if using sizeFactor (pixels); ignored if size is not None
config.repair.interp.modelPsf.minSize=5

# Maximum kernel size if using sizeFactor (pixels); ignored if size is not None
config.repair.interp.modelPsf.maxSize=None

# Kernel size as a factor of fwhm (dimensionless); size = sizeFactor * fwhm; ignored if size is not None
config.repair.interp.modelPsf.sizeFactor=3.0

# Default FWHM of Gaussian model of core of star (pixels)
config.repair.interp.modelPsf.defaultFwhm=3.0

# wing width, as a multiple of core width (dimensionless); ignored if addWing false
config.repair.interp.modelPsf.wingFwhmFactor=2.5

# Kernel size (width and height) (pixels); if None then sizeFactor is used
config.repair.interp.modelPsf.size=None

# If fallbackValueType is 'USER' then use this as the fallbackValue; ignored otherwise
config.repair.interp.fallbackUserValue=0.0

# Smoothly taper to the fallback value at the edge of the image?
config.repair.interp.useFallbackValueAtEdge=True

# Allow negative values for egde interpolation fallbackValue?  If False, set fallbackValue to max(fallbackValue, 0.0)
config.repair.interp.negativeFallbackAllowed=True

# Find and mask out cosmic rays?
config.repair.doCosmicRay=True

# Don't interpolate over CR pixels
config.repair.cosmicray.keepCRs=False

# used in condition 3 for CR; see CR.cc code
config.repair.cosmicray.cond3_fac=2.5

# used in condition 3 for CR; see CR.cc code
config.repair.cosmicray.cond3_fac2=0.6

# Approximation order in X for background Chebyshev (valid only with useApprox=True)
config.repair.cosmicray.background.approxOrderX=6

# Approximation order in Y for background Chebyshev (valid only with useApprox=True)
config.repair.cosmicray.background.approxOrderY=-1

# Names of mask planes to ignore while estimating the background
config.repair.cosmicray.background.ignoredPixelMask=['BAD', 'EDGE', 'DETECTED', 'DETECTED_NEGATIVE', 'NO_DATA']

# behaviour if there are too few points in grid for requested interpolation style
# Allowed values:
# 	None	Field is optional
# 	INCREASE_NXNYSAMPLE	Increase the number of samples used to make the interpolation grid.
# 	THROW_EXCEPTION	throw an exception if there are too few points
# 	REDUCE_INTERP_ORDER	use an interpolation style with a lower order.
# 
config.repair.cosmicray.background.undersampleStyle='REDUCE_INTERP_ORDER'

# how to interpolate the background values. This maps to an enum; see afw::math::Background
# Allowed values:
# 	None	Field is optional
# 	NONE	No background estimation is to be attempted
# 	CONSTANT	Use a single constant value
# 	LINEAR	Use linear interpolation
# 	AKIMA_SPLINE	higher-level nonlinear spline that is more robust to outliers
# 	NATURAL_SPLINE	cubic spline with zero second derivative at endpoints
# 
config.repair.cosmicray.background.algorithm='AKIMA_SPLINE'

# Ignore NaNs when estimating the background
config.repair.cosmicray.background.isNanSafe=False

# Sky region size to be used for each background point in X direction. If 0, the binSize config is used.
# 	Valid Range = [0,inf)
config.repair.cosmicray.background.binSizeX=0

# Sky region size to be used for each background point in Y direction. If 0, the binSize config is used.
# 	Valid Range = [0,inf)
config.repair.cosmicray.background.binSizeY=0

# how large a region of the sky should be used for each background point
# 	Valid Range = [1,inf)
config.repair.cosmicray.background.binSize=100000

# Use inverse variance weighting in calculation (valid only with useApprox=True)
config.repair.cosmicray.background.weighting=True

# type of statistic to use for grid points
# Allowed values:
# 	None	Field is optional
# 	MEDIAN	median
# 	MEANCLIP	clipped mean
# 	MEAN	unclipped mean
# 
config.repair.cosmicray.background.statisticsProperty='MEDIAN'

# Use Approximate (Chebyshev) to model background.
config.repair.cosmicray.background.useApprox=False

# number of times to look for contaminated pixels near known CR pixels
config.repair.cosmicray.niteration=3

# maximum number of contaminated pixels
config.repair.cosmicray.nCrPixelMax=1000000

# CRs must be > this many sky-sig above sky
config.repair.cosmicray.minSigma=5.0

# CRs must have > this many DN (== electrons/gain) in initial detection
config.repair.cosmicray.min_DN=50.0

# DataId keywords specifying a visit
config.visitKeys=['visit']

import lsst.obs.subaru.isr
config.isr.retarget(target=lsst.obs.subaru.isr.SubaruIsrTask, ConfigClass=lsst.obs.subaru.isr.SubaruIsrConfig)

# Number of stdev below the background to set thumbnail minimum
config.isr.thumbnailStdev=3.0

# How to estimate the average value for BAD regions.
# Allowed values:
# 	None	Field is optional
# 	MEDIAN	Correct using the median of the good data
# 	MEANCLIP	Correct using the (clipped) mean of good data
# 
config.isr.badStatistic='MEANCLIP'

# Order of polynomial or to fit if overscan fit type is a polynomial, or number of spline knots if overscan fit type is a spline.
config.isr.overscanOrder=30

# Widen bleed trails based on their width?
config.isr.doWidenSaturationTrails=True

import lsst.obs.subaru.crosstalk
config.isr.crosstalk.retarget(target=lsst.obs.subaru.crosstalk.CrosstalkTask, ConfigClass=lsst.obs.subaru.crosstalk.CrosstalkConfig)

# Shape of coeffs array
config.isr.crosstalk.coeffs.shape=[4, 4]

# Crosstalk coefficients
config.isr.crosstalk.coeffs.values=[0.0, -0.000125, -0.000149, -0.000156, -0.000124, 0.0, -0.000132, -0.000157, -0.000171, -0.000134, 0.0, -0.000153, -0.000157, -0.000151, -0.000137, 0.0]

# Name for crosstalk mask plane
config.isr.crosstalk.crosstalkMaskPlane='CROSSTALK'

# Set crosstalk mask plane for pixels over this value
config.isr.crosstalk.minPixelToMask=45000.0

# Apply dark frame correction?
config.isr.doDark=False

# update exposure metadata in the assembled ccd to reflect the effective gain of the assembled chip
config.isr.setGainAssembledCcd=True

# Fallback default filter name for calibrations
config.isr.fallbackFilterName=None

# Correct for crosstalk
config.isr.doCrosstalk=True

# FWHM of PSF used when interpolating over bad columns (arcsec)
config.isr.fwhmForBadColumnInterpolation=1.0

# The read noise to use if no Detector is present in the Exposure
config.isr.readNoise=0.0

# FWHM of PSF (arcsec)
config.isr.fwhm=1.0

# Name of mask plane to use for suspect pixels
config.isr.suspectMaskName='SUSPECT'

# Maximum number of iterations for the brighter fatter correction
config.isr.brighterFatterMaxIter=10

# If flatScalingType is 'USER' then scale flat by this amount; ignored otherwise
config.isr.flatUserScale=1.0

# Rejection threshold (sigma) for collapsing overscan before fit
config.isr.overscanRej=3.0

# Dataset type for input data; users will typically leave this alone, but camera-specific ISR tasks will override it
config.isr.datasetType='raw'

# Assemble amp-level exposures into a ccd-level exposure?
config.isr.doAssembleCcd=True

# Name of mask plane to use in saturation detection and interpolation
config.isr.saturatedMaskName='SAT'

# Should the gain be applied when applying the brighter fatter correction?
config.isr.brighterFatterApplyGain=True

# Kernel file used for the brighter fatter correction
config.isr.brighterFatterKernelFile=''

# Threshold used to stop iterating the brighter fatter correction.  It is the  absolute value of the difference between the current corrected image and the one from the previous iteration summed over all the pixels.
config.isr.brighterFatterThreshold=1000.0

# Correct for nonlinearity of the detector's response?
config.isr.doLinearize=True

# Should we set the level of all BAD patches of the chip to the chip's average value?
config.isr.doSetBadRegions=True

# Apply the brighter fatter correction
config.isr.doBrighterFatter=False

# Apply bias frame correction?
config.isr.doBias=True

# Mask suspect pixels?
config.isr.doSuspect=True

# Apply flat field correction?
config.isr.doFlat=False

# Remove any PC cards in the header
config.isr.removePcCards=True

# Apply fringe correction?
config.isr.doFringe=False

# trim out non-data regions?
config.isr.assembleCcd.doTrim=True

# FITS headers to remove (in addition to DATASEC, BIASSEC, TRIMSEC and perhaps GAIN)
config.isr.assembleCcd.keysToRemove=[]

# renormalize to a gain of 1? (ignored if setGain false). Setting to True gives 1 ADU per electron. Setting to True is not recommended for mosaic cameras because it breaks normalization across the focal plane. However, if the CCDs are sufficiently flat then the resulting error may be acceptable.
config.isr.assembleCcd.doRenorm=False

# set gain?
config.isr.assembleCcd.setGain=True

# The saturation level to use if no Detector is present in the Exposure (ignored if NaN)
config.isr.saturation=float('nan')

# Calculate variance?
config.isr.doVariance=True

# Assemble amp-level calibration exposures into ccd-level exposure?
config.isr.doAssembleIsrExposures=False

# Tweak flats to match observed amplifier ratios?
config.isr.doTweakFlat=False

# fields to remove from the metadata of the assembled ccd.
config.isr.keysToRemoveFromAssembledCcd=[]

# Center of vignetting pattern, in x (focal plane coords)
config.isr.vignette.xCenter=-100.0

# Radius of vignetting pattern, in focal plane coords
config.isr.vignette.radius=17500.0

# Center of vignetting pattern, in y (focal plane coords)
config.isr.vignette.yCenter=100.0

# The approximate flux of a zero-magnitude object in a one-second exposure, per filter
config.isr.fluxMag0T1={'g': 398107170553.49854, 'N816': 15848931924.611174, 'i': 275422870333.81744, 'r': 398107170553.49854, 'N921': 19054607179.632523, 'N515': 20892961308.54041, 'y': 91201083935.59116, 'z': 120226443461.74132}

# Do overscan subtraction?
config.isr.doOverscan=True

# The gain to use if no Detector is present in the Exposure (ignored if NaN)
config.isr.gain=float('nan')

# Do fringe subtraction after flat-fielding?
config.isr.fringeAfterFlat=True

# Border around saturated pixels for thumbnail
config.isr.thumbnailSatBorder=2

# Mask saturated pixels?
config.isr.doSaturation=True

# Trim guider shadow
config.isr.doGuider=False

# Softening parameter for thumbnail mapping
config.isr.thumbnailQ=20.0

# Default value for fluxMag0T1 (for an unrecognised filter)
config.isr.defaultFluxMag0T1=158489319246.11172

# Correct the amplifiers for their gains
# 
# N.b. this is intended to be used *instead* of doFlat; it's useful if you're measuring system throughput
# 
config.isr.doApplyGains=False

# Number of points to define the Vignette polygon
config.isr.numPolygonPoints=100

# Persist Polygon used to define vignetted region?
config.isr.doWriteVignettePolygon=True

# Offset to the random number generator seed (full seed includes exposure ID)
config.isr.fringe.stats.rngSeedOffset=0

# Ignore pixels with these masks
config.isr.fringe.stats.badMaskPlanes=['SAT']

# Statistic to use
config.isr.fringe.stats.stat=32

# Number of fitting iterations
config.isr.fringe.stats.iterations=3

# Sigma clip threshold
config.isr.fringe.stats.clip=3.0

# Only fringe-subtract these filters
config.isr.fringe.filters=['y', 'N921', 'N926', 'N973', 'N1010']

# Sigma clip threshold
config.isr.fringe.clip=3.0

# Half-size of large (background) measurements (pixels)
config.isr.fringe.large=30

# Number of fringe measurements
config.isr.fringe.num=30000

# Number of fitting iterations
config.isr.fringe.iterations=20

# Half-size of small (fringe) measurements (pixels)
config.isr.fringe.small=3

# Remove fringe pedestal?
config.isr.fringe.pedestal=False

# The method for scaling the flat on the fly.
# Allowed values:
# 	None	Field is optional
# 	MEDIAN	Scale by the inverse of the median
# 	USER	Scale by flatUserScale
# 	MEAN	Scale by the inverse of the mean
# 
config.isr.flatScalingType='USER'

# Write OverScan-Subtracted thumbnail?
config.isr.qa.doThumbnailOss=True

# Mesh size in X (pix) to calculate count statistics
config.isr.qa.flatness.meshX=256

# How many times do we iterate clipping outliers in calculate count statistics?
config.isr.qa.flatness.nIter=3

# Do we clip outliers in calculate count statistics?
config.isr.qa.flatness.doClip=True

# How many sigma is used to clip outliers in calculate count statistics?
config.isr.qa.flatness.clipSigma=3.0

# Mesh size in Y (pix) to calculate count statistics
config.isr.qa.flatness.meshY=256

# Write flattened thumbnail?
config.isr.qa.doThumbnailFlattened=True

# Write OverScan-Subtracted image?
config.isr.qa.doWriteOss=False

# Write flattened image?
config.isr.qa.doWriteFlattened=False

# Range for thumbnail mapping
config.isr.thumbnailRange=5.0

# Persist postISRCCD?
config.isr.doWrite=False

# Mask defect pixels?
config.isr.doDefect=True

# Normalize all the amplifiers in each CCD to have the same gain
# 
# This does not measure the gains, it simply forces the median of each amplifier to be equal
# after applying the nominal gain
# 
config.isr.normalizeGains=False

# Maximum deviation from the median for overscan
config.isr.overscanMaxDev=1000.0

# The method for fitting the overscan bias level.
# Allowed values:
# 	None	Field is optional
# 	LEG	Fit Legendre polynomial to the longest axis of the overscan region
# 	CUBIC_SPLINE	Fit cubic spline to the longest axis of the overscan region
# 	MEDIAN	Correct using the median of the overscan region
# 	POLY	Fit ordinary polynomial to the longest axis of the overscan region
# 	CHEB	Fit Chebyshev polynomial to the longest axis of the overscan region
# 	AKIMA_SPLINE	Fit Akima spline to the longest axis of the overscan region
# 	NATURAL_SPLINE	Fit natural spline to the longest axis of the overscan region
# 	MEAN	Correct using the mean of the overscan region
# 
config.isr.overscanFitType='AKIMA_SPLINE'

# Number of pixels by which to grow the saturation footprints
config.isr.growSaturationFootprintSize=1

# Binning factor for thumbnail
config.isr.thumbnailBinning=4

# Repair PSF size (pixels)
config.psfSize=21

# Grow radius for CR (pixels)
config.crGrow=2

# Key for observation date in exposure registry
config.dateObs='dateObs'

# Clobber existing processed images?
config.clobber=True

# Key for filter name in exposure/calib registries
config.filter='filter'

# Repair artifacts?
config.doRepair=True

# Repair PSF FWHM (pixels)
config.psfFwhm=3.0

# Key for calib date in calib registry
config.dateCalib='calibDate'

