-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetupSwarpStack.py
More file actions
executable file
·107 lines (82 loc) · 3.11 KB
/
Copy pathSetupSwarpStack.py
File metadata and controls
executable file
·107 lines (82 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# coding: utf-8
import sys
import os
from glob import glob
from datetime import datetime
from astropy.io import fits
from astropy.table import Table
from astropy.io import fits
from astropy.table import Table
def get_last_word_in_path(file_path):
# Normalize the path to handle different path separators
normalized_path = os.path.normpath(file_path)
# Split the normalized path into individual components
path_components = normalized_path.split(os.sep)
# Get the last component (last word in the path)
last_word = path_components[-1]
return last_word
def add_leading_zero(integer_value, fixed_length=3):
# Convert the integer to a string and prepend zeros to achieve the fixed length
str_value = f"{integer_value:0{fixed_length}}"
return str_value
# directory containing FITS files
workdir = os.getcwd()
swarp_dir = workdir + '/' + 'Skysub/'
files = glob('%s/sfd*.sw.fits' % (swarp_dir))
sorted_files = sorted(files)
rows = []
for fitsfile in sorted_files:
try:
hdr = fits.getheader(fitsfile)
short_name = get_last_word_in_path(fitsfile)
rows.append({
"FILENAME": short_name,
"OBJECT": hdr.get("OBJECT", "UNKNOWN"),
"FILTER": hdr.get("FILTER", "UNKNOWN"),
"EXPTIME": hdr.get("EXPTIME", 0.0),
"EXPCOADD": hdr.get("EXPCOADD", 0.0),
"COADDS": hdr.get("COADDS", 1),
"FSAMPLE": hdr.get("FSAMPLE", "UNKNOWN")
})
except Exception as e:
print(f"Skipping {fitsfile}: {e}")
# create astropy table
tab = Table(rows=rows)
print(tab)
#grouped = tab.group_by(["OBJECT", "FILTER", "EXPTIME", "EXPCOADD", "COADDS"])
grouped = tab.group_by(["OBJECT", "FILTER", "EXPCOADD", "COADDS"])
for key, group in zip(grouped.groups.keys, grouped.groups):
obj = key["OBJECT"]
filt = key["FILTER"]
# expt = key["EXPTIME"]
# int_exp = int(expt)
# sexp = add_leading_zero(int_exp, fixed_length=3)
expc = key["EXPCOADD"]
int_expc = int(expc)
sexpc = add_leading_zero(int_expc, fixed_length=3)
coadds = key["COADDS"]
scoadds = add_leading_zero(coadds, fixed_length=2)
filelist = list(group["FILENAME"])
print(f"\nOBJECT={obj} FILTER={filt} EXPCOADD={int_expc} COADDS={coadds}")
print(filelist)
for key, group in zip(grouped.groups.keys, grouped.groups):
obj = key["OBJECT"]
filt = key["FILTER"]
# expt = key["EXPTIME"]
# int_exp = int(expt)
# sexp = add_leading_zero(int_exp, fixed_length=3)
expc = key["EXPCOADD"]
int_expc = int(expc)
sexpc = add_leading_zero(int_expc, fixed_length=3)
coadds = key["COADDS"]
scoadds = add_leading_zero(coadds, fixed_length=2)
outfile = swarp_dir + f"{obj}.{filt}.{sexpc}s.{scoadds}c.sw.txt".replace(" ", "_")
with open(outfile, "w") as f:
for fn in group["FILENAME"]:
f.write(swarp_dir + fn + "\n")
maskout = swarp_dir + f"{obj}.{filt}.{sexpc}s.{scoadds}c.mask.txt".replace(" ", "_")
with open(maskout, "w") as f:
for fn in group["FILENAME"]:
mfn = fn.replace("sw","mask")
f.write(swarp_dir + mfn + "\n")