From e4e3689d9bf4e6676b4c85e869aaadda2ba77b20 Mon Sep 17 00:00:00 2001 From: "james-smith-za[ska-dell-laptop-mintRebecca]" Date: Thu, 8 Dec 2016 17:01:06 +0200 Subject: [PATCH 1/3] Added ability to read pointing model from Field System specification file. --- katpoint/pointing.py | 68 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/katpoint/pointing.py b/katpoint/pointing.py index 8823157..8019ae3 100644 --- a/katpoint/pointing.py +++ b/katpoint/pointing.py @@ -24,7 +24,7 @@ import numpy as np -from .model import Parameter, Model +from .model import Parameter, Model, BadModelFile from .ephem_extra import rad2deg, deg2rad, angle_from_degrees logger = logging.getLogger(__name__) @@ -376,3 +376,69 @@ def fit(self, az, el, delta_az, delta_el, sigma_daz=None, sigma_del=None, enable # logger.info('Fit pointing model using %dx%d design matrix with condition number %.2f' % (N, M, s[0] / s[-1])) return param_vector, sigma_params + + def fromfile(self, file_like): + """Load pointing model either from an ini-style config file (both header + and parameters) or from a Field System mdlpo.ctl file (parameters only). + + Parameters + ---------- + file-like : object + File-like object with readline() method representing config file + """ + try: + super(PointingModel, self).fromfile(file_like) + except BadModelFile: + file_like.seek(0) # To reset the thing. + self.fromstring(_fs_to_kp_pointing_model(file_like)) + + +def _fs_to_kp_pointing_model(pmodl_file): + """Parses a Field System pointing model file (mdlpo.ctl) + Returns: + katpoint pointing model (object? string?) + """ + lines = ["This line deliberately ignored."] # So that I can start line numbering at 1, as Himwich does in the description. + for i in pmodl_file: + if i[0] != '*': + lines.append(i) + if len(lines) != 8: + raise BadModelFile("%s not correct length for pointing model file (8 lines). File is %d lines long."%(repr(pmodl_file), len(lines) - 1)) + # Line 2 gives the enabled parameters: + params_implemented = lines[2].split() + + if len(params_implemented) != 31: + raise BadModelFile("%s not correct format for pointing model file." % (repr(pmodl_file))) + # The first number on the line is the phi value + phi = float(params_implemented[0]) + + # If any of the higher ones are used, throw a warning: + if params_implemented[23] == '1' or \ + params_implemented[24] == '1' or \ + params_implemented[25] == '1' or \ + params_implemented[26] == '1' or \ + params_implemented[27] == '1' or \ + params_implemented[28] == '1' or \ + params_implemented[29] == '1' or \ + params_implemented[30] == '1': + logger.warning("Warning: pointing model file uses params above the 22 implemented in katpoint.") + + # Lines 3 - 8 each have 5 parameters on them. + params = [ 0 ] # Pad list entry number 0 so that the param numbers correspond. + params.extend(lines[3].split()) + params.extend(lines[4].split()) + params.extend(lines[5].split()) + params.extend(lines[6].split()) + params.extend(lines[7].split()) + params.extend(lines[8].split()) + + pmodl_string = "" + + for i in range(1,23): + if params_implemented[i] == '1' and float(params[i]) != 0: + pmodl_string += "%s "%(params[i]) + else: + pmodl_string += "0 " + + pmodl_string = pmodl_string[:-1] # Remove the resulting space on the end. + return pmodl_string From 4b0e0594ae49365bb139b668cc746fa5ca5407d3 Mon Sep 17 00:00:00 2001 From: "james-smith-za[ska-dell-laptop-mintRebecca]" Date: Tue, 13 Dec 2016 12:29:34 +0200 Subject: [PATCH 2/3] Housekeeping. Tidied the code up a bit. --- katpoint/pointing.py | 63 ++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/katpoint/pointing.py b/katpoint/pointing.py index 8019ae3..7c8bdd4 100644 --- a/katpoint/pointing.py +++ b/katpoint/pointing.py @@ -394,51 +394,50 @@ def fromfile(self, file_like): def _fs_to_kp_pointing_model(pmodl_file): - """Parses a Field System pointing model file (mdlpo.ctl) - Returns: - katpoint pointing model (object? string?) + """Parse a Field System pointing model file to a format katpoint can understand. + + Parameters + ---------- + pmodl_file : file-like object referring to a Field System mdlpo.ctl file. + + Returns + ------- + pmodl_string : katpoint pointing model in string form """ - lines = ["This line deliberately ignored."] # So that I can start line numbering at 1, as Himwich does in the description. + lines = [] for i in pmodl_file: if i[0] != '*': lines.append(i) if len(lines) != 8: - raise BadModelFile("%s not correct length for pointing model file (8 lines). File is %d lines long."%(repr(pmodl_file), len(lines) - 1)) - # Line 2 gives the enabled parameters: - params_implemented = lines[2].split() + raise BadModelFile("File not correct length for mdlop.ctl file, %d lines, should be 8."% (len(lines))) + # Line 2 gives the enabled parameters and the phi value: + params_implemented = lines[1].split() if len(params_implemented) != 31: raise BadModelFile("%s not correct format for pointing model file." % (repr(pmodl_file))) - # The first number on the line is the phi value - phi = float(params_implemented[0]) - - # If any of the higher ones are used, throw a warning: - if params_implemented[23] == '1' or \ - params_implemented[24] == '1' or \ - params_implemented[25] == '1' or \ - params_implemented[26] == '1' or \ - params_implemented[27] == '1' or \ - params_implemented[28] == '1' or \ - params_implemented[29] == '1' or \ - params_implemented[30] == '1': - logger.warning("Warning: pointing model file uses params above the 22 implemented in katpoint.") - - # Lines 3 - 8 each have 5 parameters on them. - params = [ 0 ] # Pad list entry number 0 so that the param numbers correspond. - params.extend(lines[3].split()) - params.extend(lines[4].split()) - params.extend(lines[5].split()) - params.extend(lines[6].split()) - params.extend(lines[7].split()) - params.extend(lines[8].split()) - pmodl_string = "" + # The first number on the line is the phi value. Not really needed since this is hardcoded in katpoint. + phi = params_implemented.pop(0) - for i in range(1,23): + # If any of the higher ones (23 or above) are used, throw a warning: + if any(param_used == '1' for param_used in params_implemented[22:]): + logger.warning(" Pointing model file uses param(s) not among the 22 implemented in katpoint." \ + "\nIt is likely that they are not implemented in Field System either.") + + # Lines 3 and above each have 5 parameters on them. + params = [] + for line in lines[2:]: + params.extend(line.split()) + + # Create a string which katpoint will be able to parse. + pmodl_string = "" + for i in range(0,22): if params_implemented[i] == '1' and float(params[i]) != 0: + # Not really any point in converting it to a float or dms value, since the precision in the FS file is + # what FS was using anyway. Just use the string. pmodl_string += "%s "%(params[i]) else: pmodl_string += "0 " - pmodl_string = pmodl_string[:-1] # Remove the resulting space on the end. + return pmodl_string From 04fe88c9a4c43fe5e78cf8c7e212439e556d6037 Mon Sep 17 00:00:00 2001 From: "james-smith-za[ska-dell-laptop-mintRebecca]" Date: Tue, 13 Dec 2016 12:32:47 +0200 Subject: [PATCH 3/3] Tidied docstrings to be in-line with the convention. --- katpoint/pointing.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/katpoint/pointing.py b/katpoint/pointing.py index 7c8bdd4..7bf7489 100644 --- a/katpoint/pointing.py +++ b/katpoint/pointing.py @@ -398,11 +398,13 @@ def _fs_to_kp_pointing_model(pmodl_file): Parameters ---------- - pmodl_file : file-like object referring to a Field System mdlpo.ctl file. + pmodl_file : object + File-like object with readline() method referring to a Field System mdlpo.ctl file. Returns ------- - pmodl_string : katpoint pointing model in string form + pmodl_string : string + Pointing model string parseable by katpoint in the usual way. """ lines = [] for i in pmodl_file: