Skip to content

Commit

Permalink
Generate new package tools to store auxiliary scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
nicocardiel committed Jul 13, 2018
1 parent 70df56b commit 9d104b3
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 17 deletions.
Empty file added megaradrp/tools/__init__.py
Empty file.
61 changes: 45 additions & 16 deletions tools/overplot_traces.py → megaradrp/tools/overplot_traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
from numpy.polynomial import Polynomial
import pkgutil
import sys
from uuid import uuid4

from numina.array.display.polfit_residuals import polfit_residuals
Expand Down Expand Up @@ -39,16 +40,20 @@ def assign_boxes_to_fibers(insmode):

fibid_with_box = []
n1 = 1
print('\n* Fiber description for INSMODE=' + insmode)
list_to_print = []
for dumbox in pseudo_slit_config:
nfibers = dumbox['nfibers']
name = dumbox['name']
n2 = n1 + nfibers
fibid_with_box += \
["{} [{}]".format(val1, val2)
for val1, val2 in zip(range(n1, n2), [name] * nfibers)]
print('Box {:>2}, fibers {:3d} - {:3d}'.format(name, n1, n2 - 1))
dumstr ='Box {:>2}, fibers {:3d} - {:3d}'.format(name, n1, n2 - 1)
list_to_print.append(dumstr)
n1 = n2
print('\n* Fiber description for INSMODE=' + insmode)
for dumstr in reversed(list_to_print):
print(dumstr)
print('---------------------------------')

return fibid_with_box
Expand Down Expand Up @@ -81,7 +86,9 @@ def plot_trace(ax, coeff, xmin, xmax, ix_offset,

def main(args=None):
# parse command-line options
parser = argparse.ArgumentParser(prog='overplot_traces')
parser = argparse.ArgumentParser(
description="description: overplot traces"
)
# positional parameters
parser.add_argument("fits_file",
help="FITS image containing the spectra",
Expand All @@ -91,12 +98,12 @@ def main(args=None):
type=argparse.FileType('r'))
# optional parameters
parser.add_argument("--rawimage",
help="FITS file is a RAW image (RSS assumed instead)",
help="FITS file is a RAW image (otherwise trimmed "
"image is assumed)",
action="store_true")
parser.add_argument("--yoffset",
help="Vertical offset (+upwards, -downwards)",
default=0,
type=float)
parser.add_argument("--global_offset",
help="Global offset polynomial coefficients "
"(+upwards, -downwards)")
parser.add_argument("--fibids",
help="Display fiber identification number",
action="store_true")
Expand All @@ -122,16 +129,27 @@ def main(args=None):
parser.add_argument("--pdffile",
help="ouput PDF file name",
type=argparse.FileType('w'))
parser.add_argument("--echo",
help="Display full command line",
action="store_true")

args = parser.parse_args(args=args)

if args.echo:
print('\033[1m\033[31m% ' + ' '.join(sys.argv) + '\033[0m\n')

# global_offset in command line
if args.global_offset is None:
args_global_offset = [0.0]
else:
args_global_offset = [float(dum) for dum in
str(args.global_offset).split(",")]

# read pdffile
if args.pdffile is not None:
from matplotlib.backends.backend_pdf import PdfPages
pdf = PdfPages(args.pdffile.name)
else:
import matplotlib
matplotlib.use('Qt5Agg')
pdf = None

ax = ximshow_file(args.fits_file.name,
Expand Down Expand Up @@ -159,8 +177,14 @@ def main(args=None):
'expected number from account from boxes')
if 'global_offset' in bigdict.keys():
global_offset = bigdict['global_offset']
if args_global_offset != [0.0] and global_offset != [0.0]:
raise ValueError('global_offset != 0 argument cannot be employed '
'when global_offset != 0 in JSON file')
elif args_global_offset != [0.0]:
global_offset = args_global_offset
else:
global_offset = [0.0]
global_offset = args_global_offset
print('>>> Using global_offset:', global_offset)
pol_global_offset = np.polynomial.Polynomial(global_offset)
if 'ref_column' in bigdict.keys():
ref_column = bigdict['ref_column']
Expand All @@ -177,7 +201,7 @@ def main(args=None):
pol_trace = np.polynomial.Polynomial(coeff)
y_at_ref_column = pol_trace(ref_column)
correction = pol_global_offset(y_at_ref_column)
coeff[0] += correction + args.yoffset
coeff[0] += correction
# update values in bigdict (JSON structure)
bigdict['contents'][fibid-1]['fitparms'] = coeff.tolist()
plot_trace(ax, coeff, start, stop, ix_offset, args.rawimage,
Expand Down Expand Up @@ -284,12 +308,17 @@ def main(args=None):
if start < start_orig:
plot_trace(ax, coeff, start, start_orig,
ix_offset,
args.rawimage, False, fiblabel,
args.rawimage, True, fiblabel,
colour='green')
if stop_orig < stop:
plot_trace(ax, coeff, stop_orig, stop,
ix_offset,
args.rawimage, False, fiblabel,
args.rawimage, True, fiblabel,
colour='green')
if start_orig <= start <= stop <= stop_orig:
plot_trace(ax, coeff, start, stop,
ix_offset,
args.rawimage, True, fiblabel,
colour='green')
else:
print('(extrapolation SKIPPED) fibid:', fiblabel)
Expand Down Expand Up @@ -371,6 +400,8 @@ def main(args=None):
print('(sandwich) fibid:', fiblabel)
fraction = operation['fraction']
nf1, nf2 = operation['neighbours']
start = operation['start']
stop = operation['stop']
tmpf1 = bigdict['contents'][nf1 - 1]
tmpf2 = bigdict['contents'][nf2 - 1]
if nf1 != tmpf1['fibid'] or nf2 != tmpf2['fibid']:
Expand All @@ -379,8 +410,6 @@ def main(args=None):
)
coefff1 = np.array(tmpf1['fitparms'])
coefff2 = np.array(tmpf2['fitparms'])
start = np.min([tmpf1['start'], tmpf2['start']])
stop = np.min([tmpf1['stop'], tmpf2['stop']])
coeff = coefff1 + fraction * (coefff2 - coefff1)
plot_trace(ax, coeff, start, stop, ix_offset,
args.rawimage, args.fibids,
Expand Down
78 changes: 78 additions & 0 deletions megaradrp/tools/overplot_traces_healing_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"operations": [
{
"description": "vertical_shift_in_pixels",
"fibid_ini": 350,
"fibid_end": 350,
"vshift": 6
},
{
"description": "vertical_shift_in_pixels",
"fibid_list": [350, 351, 408],
"vshift": 6
},
{
"description": "duplicate_trace",
"fibid_original": 349,
"fibid_duplicated": 350,
"vshift": 6
},
{
"description": "extrapolation",
"fibid_ini": 1,
"fibid_end": 622,
"start": 4,
"stop": 4092
},
{
"description": "extrapolation",
"fibid_list": [603, 605, 607, 608, 609, 612, 613, 615, 616, 617, 619, 622],
"start": 4,
"stop": 4092
},
{
"description": "fit_through_user_points",
"fibid": 614,
"poldeg": 5,
"start": 4,
"stop": 4092,
"user_points": [
{"x": 57.0, "y": 3891.0},
{"x": 388.5, "y": 3895.0},
{"x": 849.0, "y": 3900.0},
{"x": 1400.0, "y": 3905.0},
{"x": 2030.0, "y": 3910.0},
{"x": 3250.0, "y": 3915.0},
{"x": 3860.5, "y": 3917.0}
]
},
{
"description": "extrapolation_through_user_points",
"fibid": 614,
"start_reuse": 4,
"stop_reuse": 1500,
"resampling": 10,
"poldeg": 5,
"start": 4,
"stop": 4092,
"user_points": [
{"x": 2800, "y": 3864},
{"x": 3900, "y": 3865}
]
},
{
"description": "sandwich",
"fibid": 535,
"fraction": 0.5,
"neighbours": [534, 536],
"start": 4,
"stop": 4092
},
{
"description": "renumber_fibids_within_box",
"fibid_ini": 275,
"fibid_end": 350,
"fibid_shift": -1
}
]
}
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@
],
'numinadb.extra.1': [
'MEGARA = megaradrp.db [DB]'
]
],
'console_scripts': [
'megaradrp-overplot_traces = megaradrp.tools.overplot_traces:main',
],
},
classifiers=[
"Programming Language :: Python :: 2.7",
Expand Down

0 comments on commit 9d104b3

Please sign in to comment.