
#list MODULES -add /home/kiawe/eugene/src/panstarrs/ipp/ippTasks

module images.pro

$flatlevel = 10000
$biaslevel = 500
$skyrate  = 200
$darkrate = 100

macro cleanup
  exec rm -f *.fit
  exec rm -f *.jpg
  exec rm -f *.fits
  exec rm -rf logs
end

$simdir = `ipp_datapath.pl path://SIMTEST`

macro init
  exec pxadmin -delete -dbname simtest
  exec pxadmin -create -dbname simtest
  exec mkdir -p $simdir/raw
  exec mkdir -p $simdir/proc
  mkbiases
  mkdarks
  mkflats
  mkobjects
  exec ipp_serial_inject.pl $simdir/raw/*.fits --workdir path://SIMTEST/proc --dbname simtest
end

### create a bias detrun:
# exec dettool -pretend -definebyquery -inst SIMPLE -filter NONE -det_type bias -select_exp_type bias -workdir path://SIMTEST/mkbias.0 -dbname simtest

### If something goes wrong:
# exec dettool -updatedetrun -det_id 1 -state stop -dbname simtest

### create a dark detrun:
# exec dettool -pretend -definebyquery -inst SIMPLE -filter NONE -det_type dark -select_exp_type dark -workdir path://SIMTEST/mkdark.0 -dbname simtest

### create a flat detrun:
# exec dettool -pretend -definebyquery -inst SIMPLE -filter B -det_type flat -select_exp_type flat -select_filter B -workdir path://SIMTEST/mkflat.0 -dbname simtest 
# exec dettool -pretend -definebyquery -inst SIMPLE -filter V -det_type flat -select_exp_type flat -select_filter V -workdir path://SIMTEST/mkflat.1 -dbname simtest 

# generate a set of fake biases
macro mkbiases
  echo "generating 10 bias images"
  for i 0 10
    mkimages.init 1024 1024 5 1000 0 2.0
    set bias = blank + $mkiRD*mki_noisei + $biaslevel
    keyword bias AIRMASS  -wf {1 + 0.1*$i}
    keyword bias FILTER   -w NONE
    keyword bias POSANGLE -wf 0.0
    keyword bias RA       -wf 0.0
    keyword bias DEC      -wf 0.0
    keyword bias EXPTIME  -wf 0.0
    keyword bias DARKTIME -wf 0.0
    keyword bias DATE-OBS -w "2006/10/01"
    sprintf time "00:00:%02d" $i
    keyword bias UTC-OBS  -w $time
    keyword bias CCDSUM   -w "1 1"
    keyword bias TELESCOP -w simscope
    keyword bias INSTRUME -w SIMPLE
    keyword bias OBSTYPE  -w BIAS
    keyword bias OBJECT   -w BIAS
    sprintf name "$simdir/raw/bias.%02d.fits" $i
    wd bias $name
  end
end

macro mkdarks
  echo "generating 20 dark images"
  for i 0 20
    $exptime = 3 * ($i + 1)
    mkimages.init 1024 1024 5 1000 0 2.0
    set dark = blank + sqrt($mkiRD*$mkiRD + $darkrate*$exptime)*mki_noisei + $darkrate*$exptime + $biaslevel
    keyword dark AIRMASS  -wf {1 + 0.1*$i}
    keyword dark FILTER   -w  NONE
    keyword dark POSANGLE -wf 0.0
    keyword dark RA       -wf 0.0
    keyword dark DEC      -wf 0.0
    keyword dark EXPTIME  -wf $exptime
    keyword dark DARKTIME -wf $exptime
    keyword dark DATE-OBS -w "2006/10/01"
    sprintf time "00:01:%02d" $i
    keyword dark UTC-OBS  -w $time
    keyword dark CCDSUM   -w "1 1"
    keyword dark TELESCOP -w simscope
    keyword dark INSTRUME -w SIMPLE
    keyword dark OBSTYPE  -w DARK
    keyword dark OBJECT   -w DARK
    sprintf name "$simdir/raw/dark.%02d.fits" $i
    wd dark $name
  end  
end

macro mkflats
  echo "generating 20 flat images in 2 filters"
  $exptime = 10.0
  for i 0 10
    mkimages.init 1024 1024 5 1000 0 2.0
    set flat = blank + sqrt($mkiRD*$mkiRD + $darkrate*$exptime + $flatlevel)*mki_noisei + $darkrate*$exptime + $flatlevel + $biaslevel
    keyword flat AIRMASS  -wf {1 + 0.1*$i}
    keyword flat FILTER   -w  V
    keyword flat POSANGLE -wf 0.0
    keyword flat RA       -wf 0.0
    keyword flat DEC      -wf 0.0
    keyword flat EXPTIME  -wf $exptime
    keyword flat DARKTIME -wf $exptime
    keyword flat DATE-OBS -w "2006/10/01"
    sprintf time "00:01:%02d" $i
    keyword flat UTC-OBS  -w $time
    keyword flat CCDSUM   -w "1 1"
    keyword flat TELESCOP -w simscope
    keyword flat INSTRUME -w SIMPLE
    keyword flat OBSTYPE  -w FLAT
    keyword flat OBJECT   -w FLAT
    sprintf name "$simdir/raw/flat.%02d.fits" $i
    wd flat $name
  end  

  for i 10 20
    mkimages.init 1024 1024 5 1000 0 2.0
    set flat = blank + sqrt($mkiRD*$mkiRD + $darkrate*$exptime + $flatlevel)*mki_noisei + $darkrate*$exptime + $flatlevel + $biaslevel
    keyword flat AIRMASS  -wf {1 + 0.1*$i}
    keyword flat FILTER   -w  B
    keyword flat POSANGLE -wf 0.0
    keyword flat RA       -wf 0.0
    keyword flat DEC      -wf 0.0
    keyword flat EXPTIME  -wf $exptime
    keyword flat DARKTIME -wf $exptime
    keyword flat DATE-OBS -w "2006/10/01"
    sprintf time "00:02:%02d" $i
    keyword flat UTC-OBS  -w $time
    keyword flat CCDSUM   -w "1 1"
    keyword flat TELESCOP -w simscope
    keyword flat INSTRUME -w SIMPLE
    keyword flat OBSTYPE  -w FLAT
    keyword flat OBJECT   -w FLAT
    sprintf name "$simdir/raw/flat.%02d.fits" $i
    wd flat $name
  end  
end

macro mkobjects
  echo "generating 10 object images in 2 filters"
  for i 0 5
    $exptime = 3 * ($i + 1)
    mkimages.init 1024 1024 5 1000 0 2.0
    set obj = blank + sqrt($mkiRD*$mkiRD + $darkrate*$exptime + $skyrate*$exptime)*mki_noisei + $darkrate*$exptime + $skyrate*$exptime + $biaslevel
    keyword obj AIRMASS  -wf {1 + 0.1*$i}
    keyword obj FILTER   -w  V
    keyword obj POSANGLE -wf 0.0
    keyword obj RA       -wf 0.0
    keyword obj DEC      -wf 0.0
    keyword obj EXPTIME  -wf $exptime
    keyword obj DARKTIME -wf $exptime
    keyword obj DATE-OBS -w "2006/10/01"
    sprintf time "00:01:%02d" $i
    keyword obj UTC-OBS  -w $time
    keyword obj CCDSUM   -w "1 1"
    keyword obj TELESCOP -w simscope
    keyword obj INSTRUME -w SIMPLE
    keyword obj OBSTYPE  -w OBJECT
    keyword obj OBJECT   -w OBJECT
    sprintf name "$simdir/raw/obj.%02d.fits" $i
    wd obj $name
  end  

  for i 5 10
    $exptime = 3 * ($i - 4)
    mkimages.init 1024 1024 5 1000 0 2.0
    set flat = blank + sqrt($mkiRD*$mkiRD + $darkrate*$exptime + $skyrate*$exptime)*mki_noisei + $darkrate*$exptime + $skyrate*$exptime + $biaslevel
    keyword flat AIRMASS  -wf {1 + 0.1*$i}
    keyword flat FILTER   -w  B
    keyword flat POSANGLE -wf 0.0
    keyword flat RA       -wf 0.0
    keyword flat DEC      -wf 0.0
    keyword flat EXPTIME  -wf $exptime
    keyword flat DARKTIME -wf $exptime
    keyword flat DATE-OBS -w "2006/10/01"
    sprintf time "00:02:%02d" $i
    keyword flat UTC-OBS  -w $time
    keyword flat CCDSUM   -w "1 1"
    keyword flat TELESCOP -w simscope
    keyword flat INSTRUME -w SIMPLE
    keyword flat OBSTYPE  -w OBJECT
    keyword flat OBJECT   -w OBJECT
    sprintf name "$simdir/raw/obj.%02d.fits" $i
    wd flat $name
  end  
end

macro junk
-definebyquery Optional arguments, with default values:
    -det_type          ()                         define the type of detrend run (required)
    -mode              (master)                   define the mode of this detrend run
    -exp_type          ()                         search for exp_type
    -inst              ()                         search for camera
    -telescope         ()                         search for telescope
    -filter            ()                         search for filter
    -uri               ()                         search for uri
    -set_exp_type      ()                         define exposure type
    -set_filter        ()                         define filter
    -set_airmass       (nan)            define airmass
    -set_exp_time      (nan)            define exposure time
    -set_ccd_temp      (nan)            define ccd tempature
    -set_posang        (nan)            define rotator position angle
    -set_object        ()                         define exposure object
    -set_registered    (2006-12-13T22:40:18.5)    time detrend run was registered
    -set_use_begin     ()                         start of detrend run applicable peroid
    -set_use_end       ()                         end of detrend run applicable peroid
    -pretend           (FALSE)                    print the exposures that would be included in the detrend run and exit
end
