Skip to content

Instantly share code, notes, and snippets.

@SpheMakh
Created January 20, 2016 07:28
Show Gist options
  • Save SpheMakh/cfebd0d2d3c83a5ef06b to your computer and use it in GitHub Desktop.
Save SpheMakh/cfebd0d2d3c83a5ef06b to your computer and use it in GitHub Desktop.
Convert s3-sex sky model to tigger LSM format
#!/usr/bin/env python
# Convert s3-sex catalog to tigger LSM format
# Usage: ./s3-sex2tigger.py <s3 catalog> <output name> [freq for fluxes]
# The frequency is optional. It uses 1400 by default
import os
import sys
import tempfile
s3model = sys.argv[1]
outname = sys.argv[2]
if not outname.endswith(".lsm.html"):
outname += ".lsm.html"
try:
freq = sys.argv[3]
except IndexError:
freq = "1400"
# First make a txt file, then use tigger-convert
tfile = tempfile.NamedTemporaryFile(suffix=".txt")
tfile.flush()
# Initialise skymodel file
model = open(tfile.name, "w")
model.write("#format:name ra_d dec_d i q u v emaj_s emin_s pa_d\n")
with open(s3model) as std:
# Get field names
names = std.readline().split(",")
# Get sources
sources = std.readlines()
def flux_s3(a):
#Remember s3 gives the log10 of the flux
try:
b = 10**float(a)
except ValueError:
b = 0.0
return b
for counter,source in enumerate(sources):
props = source.split(",")
ra = props[names.index("right_ascension")]
dec = props[names.index("declination")]
i = flux_s3( props[names.index("i_%s"%freq)] )
q = flux_s3( props[names.index("q_%s"%freq)] )
u = flux_s3( props[names.index("u_%s"%freq)] )
v = flux_s3( props[names.index("v_%s"%freq)] )
emaj = props[names.index("major_axis")]
emin = props[names.index("minor_axis")]
pa = props[names.index("position_angle")]
name = "SRC%d"%counter
model.write("%(name)s %(ra)s %(dec)s %(i).5g %(q).5g %(u).5g %(v).5g %(emaj)s %(emin)s %(pa)s\n"%locals())
model.close()
os.system("tigger-convert %s %s -f --rename"%(tfile.name, outname))
tfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment