############################################
#
# Script for Siril 1.4
# December 2024
# (C) Cyril Richard
# Preprocessing v1.4
#
########### PREPROCESSING SCRIPT ###########
#
# Script for color camera preprocessing
#
# Needs 4 sets of RAW images in the working
# directory, within 4 directories:
#   biases/
#   flats/
#   darks/
#   lights/
# Saves masters to ./masters/
#
############################################

requires 1.3.4

# Convert Bias Frames to .fit files
cd biases
convert bias -out=../process
cd ../process

# Stack Bias Frames to bias_stacked.fit
stack bias rej 3 3 -nonorm -out=../masters/bias_stacked
cd ..

# Convert Flat Frames to .fit files
cd flats
convert flat -out=../process
cd ../process

# Calibrate Flat Frames
calibrate flat -bias=../masters/bias_stacked

# Stack Flat Frames to pp_flat_stacked.fit
stack pp_flat rej 3 3 -norm=mul -out=../masters/pp_flat_stacked
cd ..

# Convert Dark Frames to .fit files
cd darks
convert dark -out=../process
cd ../process

# Stack Dark Frames to dark_stacked.fit
stack dark rej 3 3 -nonorm -out=../masters/dark_stacked
cd ..

# Convert Light Frames to .fit files
cd lights
convert light -out=../process
cd ../process

# Calibrate Light Frames
calibrate light -dark=../masters/dark_stacked -flat=../masters/pp_flat_stacked -cc=dark -cfa -equalize_cfa -debayer

# Align lights
register pp_light

# Stack calibrated lights to result.fit
stack r_pp_light rej 3 3 -norm=addscale -output_norm -rgb_equal -32b -out=result

# flip if required
load result
mirrorx -bottomup
save ../result_$LIVETIME:%d$s

cd ..
close