Skip to content

Instantly share code, notes, and snippets.

@mhx
Created December 6, 2018 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhx/de92a228bb49b4b8b3f7b4fe9e858b64 to your computer and use it in GitHub Desktop.
Save mhx/de92a228bb49b4b8b3f7b4fe9e858b64 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use File::Glob ':bsd_glob';
use File::Basename;
use File::Spec;
use File::Path 'make_path';
use File::Temp;
use Data::Dumper;
my %opt = (
raw => "raw",
out => "output",
ext => ".fit",
siril => "/home/mhx/Downloads/siril/Siril-0.9.9.glibc2.14-x86_64.AppImage",
);
GetOptions(\%opt, qw(raw|r=s out|o=s ext=s)) or pod2usage(2);
my @raw = bsd_glob("$opt{raw}/*$opt{ext}");
my %darks;
my %flats;
my %lights;
for my $r (@raw) {
my(undef, undef, $f) = File::Spec->splitpath($r);
# TODO: temperature?
my($type, $name, $exp, $filt, $bin, $frame) = $f =~ /^(Dark|Flat|Light)_(.*?)_(\d+\.\d{3})sec_([^_]+)_bin(\d)_frame(\d+)\Q$opt{ext}\E$/;
my $light_id = "$name/Light/$exp/$filt/$bin";
my $flat_id = "Flat/$filt/$bin";
my $dark_id = "Dark/$exp/$bin";
my $id;
my $hash;
if ($type eq 'Light') {
$id = $light_id;
$hash = \%lights;
$hash->{$id}{flat} = $flat_id;
$hash->{$id}{dark} = $dark_id;
} elsif ($type eq 'Flat') {
$id = $flat_id;
$hash = \%flats;
$hash->{$id}{dark} = $dark_id;
} elsif ($type eq 'Dark') {
$id = $dark_id;
$hash = \%darks;
} else {
die "unsupported type: $type ($f)\n";
}
my $target = $id;
$target =~ tr/\//_/;
my $tdir = File::Spec->catdir($opt{out}, $target);
make_path($tdir);
$hash->{$id}{dir} = File::Spec->rel2abs($tdir);
my $link = File::Spec->catfile($tdir, "\L${type}_$frame$opt{ext}");
symlink File::Spec->rel2abs($r), $link or die "link $r, $link: $!\n";
}
# print STDERR Dumper(\%lights, \%flats, \%darks);
sub siril {
my $script = shift;
my $fh = File::Temp->new;
print $fh $script;
system $opt{siril}, '-s', $fh->filename and die "siril: $?\n";
}
sub cleanup {
my($dir, @glob) = @_;
for my $g (@glob) {
for my $f (bsd_glob(File::Spec->catfile($dir, $g))) {
unlink $f;
}
}
}
while (my($k, $v) = each %darks) {
siril(<<"EOF");
cd $v->{dir}
convertraw dark_
stack dark_ rej 3 3 -nonorm
EOF
cleanup($v->{dir}, '*.seq');
}
while (my($k, $v) = each %flats) {
my $dark = $darks{$v->{dark}} or die "no dark for $v->{dark}\n";
siril(<<"EOF");
cd $v->{dir}
convertraw flat_
preprocess flat_ -dark=$dark->{dir}/dark_stacked
stack pp_flat_ rej 3 3 -norm=mul
EOF
rename File::Spec->catfile($v->{dir}, "pp_flat_stacked.fit"), File::Spec->catfile($v->{dir}, "flat_stacked.fit");
cleanup($v->{dir}, '*.seq', 'pp_flat_*.fit');
}
while (my($k, $v) = each %lights) {
my $flat = $flats{$v->{flat}} or die "no flat for $v->{flat}\n";
my $dark = $darks{$v->{dark}} or die "no dark for $v->{dark}\n";
siril(<<"EOF");
cd $v->{dir}
convertraw light_
preprocess light_ -dark=$dark->{dir}/dark_stacked -flat=$flat->{dir}/flat_stacked
register pp_light_
stack r_pp_light_ rej 3 3 -norm=addscale
EOF
rename File::Spec->catfile($v->{dir}, "r_pp_light_stacked.fit"), File::Spec->catfile($v->{dir}, "light_stacked.fit");
cleanup($v->{dir}, '*.seq', 'pp_light_*.fit', 'r_pp_light_*.fit');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment