Created
February 9, 2022 13:51
-
-
Save mauritiusdadd/b84f3148d79a5dfbb8ce1578a2bb9ff0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Two simple functions to convert some of the sextractor output catalogs | |
into more readable file formats | |
Created on Tue Feb 8 14:42:53 2022 | |
Copyright (c) 2022 Maurizio D'Addona <mauritiusdadd@gmail.com> | |
Permission is hereby granted, free of charge, to any person | |
obtaining a copy of this software and associated documentation | |
files (the "Software"), to deal in the Software without | |
restriction, including without limitation the rights to use, | |
copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the | |
Software is furnished to do so, subject to the following | |
conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
import os | |
import sys | |
from astropy.table import Table | |
from astropy.io import ascii | |
def makeCatalogsFromLDAC(input_fit, out_formats=['ascii', 'fits'], hdu=2): | |
base_inp_dir = os.path.dirname(input_fit) | |
base_inp_name = os.path.basename(input_fit) | |
base_inp_name = os.path.splitext(base_inp_name)[0] | |
orig_table = Table.read(input_fit, format='fits', hdu=hdu) | |
# Transform columns containing vectors in multiple columns | |
new_table = Table() | |
for colname in orig_table.columns: | |
col = orig_table[colname] | |
# If there is a colum that contains a vector | |
if len(col.shape) > 1: | |
# then split the column in multiple columns | |
for i in range(col.shape[1]): | |
sub_col = col[..., i] | |
sub_col.name = f"{col.name}_{i+1}" | |
new_table.add_column(sub_col) | |
else: | |
# otherwise just use the column as it is | |
new_table.add_column(col) | |
for fmt in out_formats: | |
out_name = base_inp_name.replace('_ldac', '') + f"_cat.{fmt}" | |
out_name = os.path.join(base_inp_dir, out_name) | |
new_table.write(out_name, format=fmt, overwrite=True) | |
def makeCatalogsFromSexASCII(input_ascii, out_formats=['ascii', 'fits'], | |
comment_header='#'): | |
base_inp_dir = os.path.dirname(input_ascii) | |
base_inp_name = os.path.basename(input_ascii) | |
base_inp_name = os.path.splitext(base_inp_name)[0] | |
with open(input_ascii, 'r') as f: | |
ascii_lines = f.readlines() | |
myt = ascii.read(ascii_lines) | |
for fmt in out_formats: | |
out_name = base_inp_name.replace('_ldac', '') + f"_cat.{fmt}" | |
out_name = os.path.join(base_inp_dir, out_name) | |
myt.write(out_name, format=fmt, overwrite=True) | |
if __name__ == '__main__': | |
for inp_cat in sys.argv[1:]: | |
print(f"reading '{inp_cat}'") | |
try: | |
makeCatalogsFromLDAC(inp_cat) | |
except Exception: | |
makeCatalogsFromSexASCII(inp_cat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment