index
int64
0
731k
package
stringlengths
2
98
name
stringlengths
1
76
docstring
stringlengths
0
281k
code
stringlengths
4
1.07M
signature
stringlengths
2
42.8k
44,206
gwcs.selector
LabelMapperArray
Maps array locations to labels. Parameters ---------- mapper : ndarray An array of integers or strings where the values correspond to a label in `~gwcs.selector.RegionsSelector` model. For pixels for which the transform is not defined the value should be set to 0 or " ". inputs_mapping : `~astropy.modeling.mappings.Mapping` An optional Mapping model to be prepended to the LabelMapper with the purpose to filter the inputs or change their order so that the output of it is (x, y) values to index the array. name : str The name of this transform. Use case: For an IFU observation, the array represents the detector and its values correspond to the IFU slice label.
class LabelMapperArray(_LabelMapper): """ Maps array locations to labels. Parameters ---------- mapper : ndarray An array of integers or strings where the values correspond to a label in `~gwcs.selector.RegionsSelector` model. For pixels for which the transform is not defined the value should be set to 0 or " ". inputs_mapping : `~astropy.modeling.mappings.Mapping` An optional Mapping model to be prepended to the LabelMapper with the purpose to filter the inputs or change their order so that the output of it is (x, y) values to index the array. name : str The name of this transform. Use case: For an IFU observation, the array represents the detector and its values correspond to the IFU slice label. """ n_inputs = 2 n_outputs = 1 linear = False fittable = False def __init__(self, mapper, inputs_mapping=None, name=None, **kwargs): if mapper.dtype.type is not np.str_: mapper = np.asanyarray(mapper, dtype=int) _no_label = 0 else: _no_label = "" super(LabelMapperArray, self).__init__(mapper, _no_label, name=name, **kwargs) self.inputs = ('x', 'y') self.outputs = ('label',) def evaluate(self, *args): args = tuple([_toindex(a) for a in args]) try: result = self._mapper[args[::-1]] except IndexError as e: raise LabelMapperArrayIndexingError(e) return result @classmethod def from_vertices(cls, shape, regions): """ Create a `~gwcs.selector.LabelMapperArray` from polygon vertices stores in a dict. Parameters ---------- shape : tuple shape of mapper array regions: dict {region_label : list_of_polygon_vertices} The keys in this dictionary should match the region labels in `~gwcs.selector.RegionsSelector`. The list of vertices is ordered in such a way that when traversed in a counterclockwise direction, the enclosed area is the polygon. The last vertex must coincide with the first vertex, minimum 4 vertices are needed to define a triangle. Returns ------- mapper : `~gwcs.selector.LabelMapperArray` This models is used with `~gwcs.selector.RegionsSelector`. A model which takes the same inputs as `~gwcs.selector.RegionsSelector` and returns a label. Examples -------- >>> regions = {1: [[795, 970], [2047, 970], [2047, 999], [795, 999], [795, 970]], ... 2: [[844, 1067], [2047, 1067], [2047, 1113], [844, 1113], [844, 1067]], ... 3: [[654, 1029], [2047, 1029], [2047, 1078], [654, 1078], [654, 1029]], ... 4: [[772, 990], [2047, 990], [2047, 1042], [772, 1042], [772, 990]] ... } >>> mapper = LabelMapperArray.from_vertices((2400, 2400), regions) """ labels = np.array(list(regions.keys())) mask = np.zeros(shape, dtype=labels.dtype) for rid, vert in regions.items(): pol = region.Polygon(rid, vert) mask = pol.scan(mask) return cls(mask)
(mapper, inputs_mapping=None, name=None, **kwargs)
44,209
gwcs.selector
__call__
Evaluate this model using the given input(s) and the parameter values that were specified when the model was instantiated.
@staticmethod def _has_overlapping(ranges): """ Test a list of tuple representing ranges of values has no overlapping ranges. """ d = dict(ranges) start = ranges[:, 0] end = ranges[:, 1] start.sort() l = [] for v in start: l.append([v, d[v]]) l = np.array(l) start = np.roll(l[:, 0], -1) end = l[:, 1] if any((end - start)[:-1] > 0) or any(start[-1] > end): return True else: return False
(self, *inputs, model_set_axis=None, with_bounding_box=False, fill_value=nan, equivalencies=None, inputs_map=None, **new_inputs)
44,210
gwcs.selector
__init__
null
def __init__(self, mapper, inputs_mapping=None, name=None, **kwargs): if mapper.dtype.type is not np.str_: mapper = np.asanyarray(mapper, dtype=int) _no_label = 0 else: _no_label = "" super(LabelMapperArray, self).__init__(mapper, _no_label, name=name, **kwargs) self.inputs = ('x', 'y') self.outputs = ('label',)
(self, mapper, inputs_mapping=None, name=None, **kwargs)
44,253
gwcs.selector
evaluate
null
def evaluate(self, *args): args = tuple([_toindex(a) for a in args]) try: result = self._mapper[args[::-1]] except IndexError as e: raise LabelMapperArrayIndexingError(e) return result
(self, *args)
44,266
gwcs.selector
LabelMapperDict
Maps a number to a transform, which when evaluated returns a label. Use case: inverse transforms of an IFU. For an IFU observation, the keys are constant angles (corresponding to a slice) and values are transforms which return a slice number. Parameters ---------- inputs : tuple of str Names for the inputs, e.g. ('alpha', 'beta', lam') mapper : dict Maps key values to transforms. inputs_mapping : `~astropy.modeling.mappings.Mapping` An optional Mapping model to be prepended to the LabelMapper with the purpose to filter the inputs or change their order. It returns a number which is one of the keys of ``mapper``. atol : float Absolute tolerance when comparing inputs to ``mapper.keys``. It is passed to np.isclose. name : str The name of this transform.
class LabelMapperDict(_LabelMapper): """ Maps a number to a transform, which when evaluated returns a label. Use case: inverse transforms of an IFU. For an IFU observation, the keys are constant angles (corresponding to a slice) and values are transforms which return a slice number. Parameters ---------- inputs : tuple of str Names for the inputs, e.g. ('alpha', 'beta', lam') mapper : dict Maps key values to transforms. inputs_mapping : `~astropy.modeling.mappings.Mapping` An optional Mapping model to be prepended to the LabelMapper with the purpose to filter the inputs or change their order. It returns a number which is one of the keys of ``mapper``. atol : float Absolute tolerance when comparing inputs to ``mapper.keys``. It is passed to np.isclose. name : str The name of this transform. """ standard_broadcasting = False linear = False fittable = False n_outputs = 1 def __init__(self, inputs, mapper, inputs_mapping=None, atol=10**-8, name=None, **kwargs): self._atol = atol _no_label = 0 self._inputs = inputs self._n_inputs = len(inputs) if not all([m.n_outputs == 1 for m in mapper.values()]): raise TypeError("All transforms in mapper must have one output.") self._input_units_strict = {key: False for key in self._inputs} self._input_units_allow_dimensionless = {key: False for key in self._inputs} super(LabelMapperDict, self).__init__(mapper, _no_label, inputs_mapping, name=name, **kwargs) self.outputs = ('labels',) @property def n_inputs(self): return self._n_inputs @property def inputs(self): """ The name(s) of the input variable(s) on which a model is evaluated. """ return self._inputs @inputs.setter def inputs(self, val): """ The name(s) of the input variable(s) on which a model is evaluated. """ self._inputs = val @property def atol(self): return self._atol @atol.setter def atol(self, val): self._atol = val def evaluate(self, *args): shape = args[0].shape args = [a.flatten() for a in args] # if n_inputs > 1, determine which one is to be used as keys if self.inputs_mapping is not None: keys = self._inputs_mapping.evaluate(*args) else: keys = args keys = keys.flatten() # create an empty array for the results res = np.zeros(keys.shape) + self._no_label # If this is part of a combined transform, some of the inputs # may be NaNs. # Set NaNs to the ``_no_label`` value mapper_keys = list(self.mapper.keys()) # Loop over the keys in mapper and compare to inputs. # Find the indices where they are within ``atol`` # and evaluate the transform to get the corresponding label. for key in mapper_keys: ind = np.isclose(key, keys, atol=self._atol) inputs = [a[ind] for a in args] res[ind] = self.mapper[key](*inputs) res.shape = shape return res
(inputs, mapper, inputs_mapping=None, atol=1e-08, name=None, **kwargs)
44,270
gwcs.selector
__init__
null
def __init__(self, inputs, mapper, inputs_mapping=None, atol=10**-8, name=None, **kwargs): self._atol = atol _no_label = 0 self._inputs = inputs self._n_inputs = len(inputs) if not all([m.n_outputs == 1 for m in mapper.values()]): raise TypeError("All transforms in mapper must have one output.") self._input_units_strict = {key: False for key in self._inputs} self._input_units_allow_dimensionless = {key: False for key in self._inputs} super(LabelMapperDict, self).__init__(mapper, _no_label, inputs_mapping, name=name, **kwargs) self.outputs = ('labels',)
(self, inputs, mapper, inputs_mapping=None, atol=1e-08, name=None, **kwargs)
44,313
gwcs.selector
evaluate
null
def evaluate(self, *args): shape = args[0].shape args = [a.flatten() for a in args] # if n_inputs > 1, determine which one is to be used as keys if self.inputs_mapping is not None: keys = self._inputs_mapping.evaluate(*args) else: keys = args keys = keys.flatten() # create an empty array for the results res = np.zeros(keys.shape) + self._no_label # If this is part of a combined transform, some of the inputs # may be NaNs. # Set NaNs to the ``_no_label`` value mapper_keys = list(self.mapper.keys()) # Loop over the keys in mapper and compare to inputs. # Find the indices where they are within ``atol`` # and evaluate the transform to get the corresponding label. for key in mapper_keys: ind = np.isclose(key, keys, atol=self._atol) inputs = [a[ind] for a in args] res[ind] = self.mapper[key](*inputs) res.shape = shape return res
(self, *args)
44,326
gwcs.selector
LabelMapperRange
The structure this class uses maps a range of values to a transform. Given an input value it finds the range the value falls in and returns the corresponding transform. When evaluated the transform returns a label. Example: Pick a transform based on wavelength range. For an IFU observation, the keys are (lambda_min, lambda_max) tuples and values are transforms which return a label corresponding to a slice. Parameters ---------- inputs : tuple of str Names for the inputs, e.g. ('alpha', 'beta', 'lambda') mapper : dict Maps tuples of length 2 to transforms. inputs_mapping : `~astropy.modeling.mappings.Mapping` An optional Mapping model to be prepended to the LabelMapper with the purpose to filter the inputs or change their order. atol : float Absolute tolerance when comparing inputs to ``mapper.keys``. It is passed to np.isclose. name : str The name of this transform.
class LabelMapperRange(_LabelMapper): """ The structure this class uses maps a range of values to a transform. Given an input value it finds the range the value falls in and returns the corresponding transform. When evaluated the transform returns a label. Example: Pick a transform based on wavelength range. For an IFU observation, the keys are (lambda_min, lambda_max) tuples and values are transforms which return a label corresponding to a slice. Parameters ---------- inputs : tuple of str Names for the inputs, e.g. ('alpha', 'beta', 'lambda') mapper : dict Maps tuples of length 2 to transforms. inputs_mapping : `~astropy.modeling.mappings.Mapping` An optional Mapping model to be prepended to the LabelMapper with the purpose to filter the inputs or change their order. atol : float Absolute tolerance when comparing inputs to ``mapper.keys``. It is passed to np.isclose. name : str The name of this transform. """ standard_broadcasting = False n_outputs = 1 linear = False fittable = False def __init__(self, inputs, mapper, inputs_mapping=None, name=None, **kwargs): if self._has_overlapping(np.array(list(mapper.keys()))): raise ValueError("Overlapping ranges of values are not supported.") self._inputs = inputs self._n_inputs = len(inputs) _no_label = 0 if not all([m.n_outputs == 1 for m in mapper.values()]): raise TypeError("All transforms in mapper must have one output.") self._input_units_strict = {key: False for key in self._inputs} self._input_units_allow_dimensionless = {key: False for key in self._inputs} super(LabelMapperRange, self).__init__(mapper, _no_label, inputs_mapping, name=name, **kwargs) self.outputs = ('labels',) @property def n_inputs(self): return self._n_inputs @property def inputs(self): """ The name(s) of the input variable(s) on which a model is evaluated. """ return self._inputs @inputs.setter def inputs(self, val): """ The name(s) of the input variable(s) on which a model is evaluated. """ self._inputs = val @staticmethod def _has_overlapping(ranges): """ Test a list of tuple representing ranges of values has no overlapping ranges. """ d = dict(ranges) start = ranges[:, 0] end = ranges[:, 1] start.sort() l = [] for v in start: l.append([v, d[v]]) l = np.array(l) start = np.roll(l[:, 0], -1) end = l[:, 1] if any((end - start)[:-1] > 0) or any(start[-1] > end): return True else: return False # move this to utils? def _find_range(self, value_range, value): """ Returns the index of the tuple which holds value. Parameters ---------- value_range : np.ndarray an (2, 2) array of non-overlapping (min, max) values value : float The value Returns ------- ind : int Index of the tuple which defines a range holding the input value. None, if the input value is not within any available range. """ a, b = value_range[:, 0], value_range[:, 1] ind = np.logical_and(value >= a, value <= b).nonzero()[0] if ind.size > 1: raise ValueError("There are overlapping ranges.") elif ind.size == 0: return None else: return ind.item() def evaluate(self, *args): shape = args[0].shape args = [a.flatten() for a in args] if self.inputs_mapping is not None: keys = self._inputs_mapping.evaluate(*args) else: keys = args keys = keys.flatten() # Define an array for the results. res = np.zeros(keys.shape) + self._no_label nan_ind = np.isnan(keys) res[nan_ind] = self._no_label value_ranges = list(self.mapper.keys()) # For each tuple in mapper, find the indices of the inputs # which fall within the range it defines. for val_range in value_ranges: temp = keys.copy() temp[nan_ind] = np.nan temp = np.where(np.logical_or(temp <= val_range[0], temp >= val_range[1]), np.nan, temp) ind = ~np.isnan(temp) if ind.any(): inputs = [a[ind] for a in args] res[ind] = self.mapper[tuple(val_range)](*inputs) else: continue res.shape = shape if len(np.nonzero(res)[0]) == 0: warnings.warn("All data is outside the valid range - {0}.".format(self.name)) return res
(inputs, mapper, inputs_mapping=None, name=None, **kwargs)
44,330
gwcs.selector
__init__
null
def __init__(self, inputs, mapper, inputs_mapping=None, name=None, **kwargs): if self._has_overlapping(np.array(list(mapper.keys()))): raise ValueError("Overlapping ranges of values are not supported.") self._inputs = inputs self._n_inputs = len(inputs) _no_label = 0 if not all([m.n_outputs == 1 for m in mapper.values()]): raise TypeError("All transforms in mapper must have one output.") self._input_units_strict = {key: False for key in self._inputs} self._input_units_allow_dimensionless = {key: False for key in self._inputs} super(LabelMapperRange, self).__init__(mapper, _no_label, inputs_mapping, name=name, **kwargs) self.outputs = ('labels',)
(self, inputs, mapper, inputs_mapping=None, name=None, **kwargs)
44,344
gwcs.selector
_find_range
Returns the index of the tuple which holds value. Parameters ---------- value_range : np.ndarray an (2, 2) array of non-overlapping (min, max) values value : float The value Returns ------- ind : int Index of the tuple which defines a range holding the input value. None, if the input value is not within any available range.
def _find_range(self, value_range, value): """ Returns the index of the tuple which holds value. Parameters ---------- value_range : np.ndarray an (2, 2) array of non-overlapping (min, max) values value : float The value Returns ------- ind : int Index of the tuple which defines a range holding the input value. None, if the input value is not within any available range. """ a, b = value_range[:, 0], value_range[:, 1] ind = np.logical_and(value >= a, value <= b).nonzero()[0] if ind.size > 1: raise ValueError("There are overlapping ranges.") elif ind.size == 0: return None else: return ind.item()
(self, value_range, value)
44,350
gwcs.selector
_has_overlapping
Test a list of tuple representing ranges of values has no overlapping ranges.
@staticmethod def _has_overlapping(ranges): """ Test a list of tuple representing ranges of values has no overlapping ranges. """ d = dict(ranges) start = ranges[:, 0] end = ranges[:, 1] start.sort() l = [] for v in start: l.append([v, d[v]]) l = np.array(l) start = np.roll(l[:, 0], -1) end = l[:, 1] if any((end - start)[:-1] > 0) or any(start[-1] > end): return True else: return False
(ranges)
44,375
gwcs.selector
evaluate
null
def evaluate(self, *args): shape = args[0].shape args = [a.flatten() for a in args] if self.inputs_mapping is not None: keys = self._inputs_mapping.evaluate(*args) else: keys = args keys = keys.flatten() # Define an array for the results. res = np.zeros(keys.shape) + self._no_label nan_ind = np.isnan(keys) res[nan_ind] = self._no_label value_ranges = list(self.mapper.keys()) # For each tuple in mapper, find the indices of the inputs # which fall within the range it defines. for val_range in value_ranges: temp = keys.copy() temp[nan_ind] = np.nan temp = np.where(np.logical_or(temp <= val_range[0], temp >= val_range[1]), np.nan, temp) ind = ~np.isnan(temp) if ind.any(): inputs = [a[ind] for a in args] res[ind] = self.mapper[tuple(val_range)](*inputs) else: continue res.shape = shape if len(np.nonzero(res)[0]) == 0: warnings.warn("All data is outside the valid range - {0}.".format(self.name)) return res
(self, *args)
44,388
gwcs.wcs
NoConvergence
An error class used to report non-convergence and/or divergence of numerical methods. It is used to report errors in the iterative solution used by the :py:meth:`~astropy.wcs.WCS.all_world2pix`. Attributes ---------- best_solution : `numpy.ndarray` Best solution achieved by the numerical method. accuracy : `numpy.ndarray` Estimate of the accuracy of the ``best_solution``. niter : `int` Number of iterations performed by the numerical method to compute ``best_solution``. divergent : None, `numpy.ndarray` Indices of the points in ``best_solution`` array for which the solution appears to be divergent. If the solution does not diverge, ``divergent`` will be set to `None`. slow_conv : None, `numpy.ndarray` Indices of the solutions in ``best_solution`` array for which the solution failed to converge within the specified maximum number of iterations. If there are no non-converging solutions (i.e., if the required accuracy has been achieved for all input data points) then ``slow_conv`` will be set to `None`.
class NoConvergence(Exception): """ An error class used to report non-convergence and/or divergence of numerical methods. It is used to report errors in the iterative solution used by the :py:meth:`~astropy.wcs.WCS.all_world2pix`. Attributes ---------- best_solution : `numpy.ndarray` Best solution achieved by the numerical method. accuracy : `numpy.ndarray` Estimate of the accuracy of the ``best_solution``. niter : `int` Number of iterations performed by the numerical method to compute ``best_solution``. divergent : None, `numpy.ndarray` Indices of the points in ``best_solution`` array for which the solution appears to be divergent. If the solution does not diverge, ``divergent`` will be set to `None`. slow_conv : None, `numpy.ndarray` Indices of the solutions in ``best_solution`` array for which the solution failed to converge within the specified maximum number of iterations. If there are no non-converging solutions (i.e., if the required accuracy has been achieved for all input data points) then ``slow_conv`` will be set to `None`. """ def __init__(self, *args, best_solution=None, accuracy=None, niter=None, divergent=None, slow_conv=None): super().__init__(*args) self.best_solution = best_solution self.accuracy = accuracy self.niter = niter self.divergent = divergent self.slow_conv = slow_conv
(*args, best_solution=None, accuracy=None, niter=None, divergent=None, slow_conv=None)
44,389
gwcs.wcs
__init__
null
def __init__(self, *args, best_solution=None, accuracy=None, niter=None, divergent=None, slow_conv=None): super().__init__(*args) self.best_solution = best_solution self.accuracy = accuracy self.niter = niter self.divergent = divergent self.slow_conv = slow_conv
(self, *args, best_solution=None, accuracy=None, niter=None, divergent=None, slow_conv=None)
44,390
gwcs.selector
RegionsSelector
This model defines discontinuous transforms. It maps inputs to their corresponding transforms. It uses an instance of `_LabelMapper` as a proxy to map inputs to the correct region. Parameters ---------- inputs : list of str Names of the inputs. outputs : list of str Names of the outputs. selector : dict Mapping of region labels to transforms. Labels can be of type int or str, transforms are of type `~astropy.modeling.Model`. label_mapper : a subclass of `~gwcs.selector._LabelMapper` A model which maps locations to region labels. undefined_transform_value : float, np.nan (default) Value to be returned if there's no transform defined for the inputs. name : str The name of this transform.
class RegionsSelector(Model): """ This model defines discontinuous transforms. It maps inputs to their corresponding transforms. It uses an instance of `_LabelMapper` as a proxy to map inputs to the correct region. Parameters ---------- inputs : list of str Names of the inputs. outputs : list of str Names of the outputs. selector : dict Mapping of region labels to transforms. Labels can be of type int or str, transforms are of type `~astropy.modeling.Model`. label_mapper : a subclass of `~gwcs.selector._LabelMapper` A model which maps locations to region labels. undefined_transform_value : float, np.nan (default) Value to be returned if there's no transform defined for the inputs. name : str The name of this transform. """ standard_broadcasting = False linear = False fittable = False def __init__(self, inputs, outputs, selector, label_mapper, undefined_transform_value=np.nan, name=None, **kwargs): self._inputs = inputs self._outputs = outputs self._n_inputs = len(inputs) self._n_outputs = len(outputs) self.label_mapper = label_mapper self._undefined_transform_value = undefined_transform_value self._selector = selector # copy.deepcopy(selector) if " " in selector.keys() or 0 in selector.keys(): raise ValueError('"0" and " " are not allowed as keys.') self._input_units_strict = {key: False for key in self._inputs} self._input_units_allow_dimensionless = {key: False for key in self._inputs} super(RegionsSelector, self).__init__(n_models=1, name=name, **kwargs) def set_input(self, rid): """ Sets one of the inputs and returns a transform associated with it. """ if rid in self._selector: return self._selector[rid] else: raise RegionError("Region {0} not found".format(rid)) def inverse(self): if self.label_mapper.inverse is not None: try: transforms_inv = {} for rid in self._selector: transforms_inv[rid] = self._selector[rid].inverse except AttributeError: raise NotImplementedError("The inverse of all regions must be defined" "for RegionsSelector to have an inverse.") return self.__class__(self.outputs, self.inputs, transforms_inv, self.label_mapper.inverse) else: raise NotImplementedError("The label mapper must have an inverse " "for RegionsSelector to have an inverse.") def evaluate(self, *args): """ Parameters ---------- args : float or ndarray Input pixel coordinate, one input for each dimension. """ # Get the region labels corresponding to these inputs rids = self.label_mapper(*args).flatten() # Raise an error if all pixels are outside regions if (rids == self.label_mapper.no_label).all(): warnings.warn("The input positions are not inside any region.") # Create output arrays and set any pixels not within regions to # "undefined_transform_value" no_trans_ind = (rids == self.label_mapper.no_label).nonzero() outputs = [np.empty(rids.shape) for n in range(self.n_outputs)] for out in outputs: out[no_trans_ind] = self.undefined_transform_value # Compute the transformations args = [a.flatten() for a in args] uniq = get_unique_regions(rids) for rid in uniq: ind = (rids == rid) inputs = [a[ind] for a in args] if rid in self._selector: result = self._selector[rid](*inputs) else: # If there's no transform for a label, return np.nan result = [np.empty(inputs[0].shape) + self._undefined_transform_value for i in range(self.n_outputs)] for j in range(self.n_outputs): outputs[j][ind] = result[j] return outputs @property def undefined_transform_value(self): return self._undefined_transform_value @undefined_transform_value.setter def undefined_transform_value(self, value): self._undefined_transform_value = value @property def outputs(self): """The name(s) of the output(s) of the model.""" return self._outputs @property def selector(self): return self._selector @property def inputs(self): """ The name(s) of the input variable(s) on which a model is evaluated. """ return self._inputs @inputs.setter def inputs(self, val): """ The name(s) of the input variable(s) on which a model is evaluated. """ self._inputs = val @outputs.setter def outputs(self, val): """ The name(s) of the output variable(s). """ self._outputs = val @property def n_inputs(self): return self._n_inputs @property def n_outputs(self): return self._n_outputs
(inputs, outputs, selector, label_mapper, undefined_transform_value=nan, name=None, **kwargs)
44,394
gwcs.selector
__init__
null
def __init__(self, inputs, outputs, selector, label_mapper, undefined_transform_value=np.nan, name=None, **kwargs): self._inputs = inputs self._outputs = outputs self._n_inputs = len(inputs) self._n_outputs = len(outputs) self.label_mapper = label_mapper self._undefined_transform_value = undefined_transform_value self._selector = selector # copy.deepcopy(selector) if " " in selector.keys() or 0 in selector.keys(): raise ValueError('"0" and " " are not allowed as keys.') self._input_units_strict = {key: False for key in self._inputs} self._input_units_allow_dimensionless = {key: False for key in self._inputs} super(RegionsSelector, self).__init__(n_models=1, name=name, **kwargs)
(self, inputs, outputs, selector, label_mapper, undefined_transform_value=nan, name=None, **kwargs)
44,419
gwcs.selector
inverse
null
def inverse(self): if self.label_mapper.inverse is not None: try: transforms_inv = {} for rid in self._selector: transforms_inv[rid] = self._selector[rid].inverse except AttributeError: raise NotImplementedError("The inverse of all regions must be defined" "for RegionsSelector to have an inverse.") return self.__class__(self.outputs, self.inputs, transforms_inv, self.label_mapper.inverse) else: raise NotImplementedError("The label mapper must have an inverse " "for RegionsSelector to have an inverse.")
(self)
44,438
gwcs.selector
evaluate
Parameters ---------- args : float or ndarray Input pixel coordinate, one input for each dimension.
def evaluate(self, *args): """ Parameters ---------- args : float or ndarray Input pixel coordinate, one input for each dimension. """ # Get the region labels corresponding to these inputs rids = self.label_mapper(*args).flatten() # Raise an error if all pixels are outside regions if (rids == self.label_mapper.no_label).all(): warnings.warn("The input positions are not inside any region.") # Create output arrays and set any pixels not within regions to # "undefined_transform_value" no_trans_ind = (rids == self.label_mapper.no_label).nonzero() outputs = [np.empty(rids.shape) for n in range(self.n_outputs)] for out in outputs: out[no_trans_ind] = self.undefined_transform_value # Compute the transformations args = [a.flatten() for a in args] uniq = get_unique_regions(rids) for rid in uniq: ind = (rids == rid) inputs = [a[ind] for a in args] if rid in self._selector: result = self._selector[rid](*inputs) else: # If there's no transform for a label, return np.nan result = [np.empty(inputs[0].shape) + self._undefined_transform_value for i in range(self.n_outputs)] for j in range(self.n_outputs): outputs[j][ind] = result[j] return outputs
(self, *args)
44,446
gwcs.selector
set_input
Sets one of the inputs and returns a transform associated with it.
def set_input(self, rid): """ Sets one of the inputs and returns a transform associated with it. """ if rid in self._selector: return self._selector[rid] else: raise RegionError("Region {0} not found".format(rid))
(self, rid)
44,452
gwcs.coordinate_frames
SpectralFrame
Represents Spectral Frame Parameters ---------- axes_order : tuple or int A dimension in the input data that corresponds to this axis. reference_frame : astropy.coordinates.builtin_frames Reference frame (usually used with output_frame to convert to world coordinate objects). unit : str or units.Unit instance Spectral unit. axes_names : str Spectral axis name. name : str Name for this frame. reference_position : str Reference position - one of ``STANDARD_REFERENCE_POSITION``
class SpectralFrame(CoordinateFrame): """ Represents Spectral Frame Parameters ---------- axes_order : tuple or int A dimension in the input data that corresponds to this axis. reference_frame : astropy.coordinates.builtin_frames Reference frame (usually used with output_frame to convert to world coordinate objects). unit : str or units.Unit instance Spectral unit. axes_names : str Spectral axis name. name : str Name for this frame. reference_position : str Reference position - one of ``STANDARD_REFERENCE_POSITION`` """ def __init__(self, axes_order=(0,), reference_frame=None, unit=None, axes_names=None, name=None, axis_physical_types=None, reference_position=None): super(SpectralFrame, self).__init__(naxes=1, axes_type="SPECTRAL", axes_order=axes_order, axes_names=axes_names, reference_frame=reference_frame, unit=unit, name=name, reference_position=reference_position, axis_physical_types=axis_physical_types) @property def _default_axis_physical_types(self): if self.unit[0].physical_type == "frequency": return ("em.freq",) elif self.unit[0].physical_type == "length": return ("em.wl",) elif self.unit[0].physical_type == "energy": return ("em.energy",) elif self.unit[0].physical_type == "speed": return ("spect.dopplerVeloc",) logging.warning("Physical type may be ambiguous. Consider " "setting the physical type explicitly as " "either 'spect.dopplerVeloc.optical' or " "'spect.dopplerVeloc.radio'.") else: return ("custom:{}".format(self.unit[0].physical_type),) @property def _world_axis_object_classes(self): return {'spectral': ( coord.SpectralCoord, (), {'unit': self.unit[0]})} @property def _world_axis_object_components(self): return [('spectral', 0, 'value')] def coordinates(self, *args): # using SpectralCoord if isinstance(args[0], coord.SpectralCoord): return args[0].to(self.unit[0]) else: if hasattr(args[0], 'unit'): return coord.SpectralCoord(*args).to(self.unit[0]) else: return coord.SpectralCoord(*args, self.unit[0]) def coordinate_to_quantity(self, *coords): if hasattr(coords[0], 'unit'): return coords[0] return coords[0] * self.unit[0]
(axes_order=(0,), reference_frame=None, unit=None, axes_names=None, name=None, axis_physical_types=None, reference_position=None)
44,453
gwcs.coordinate_frames
__init__
null
def __init__(self, axes_order=(0,), reference_frame=None, unit=None, axes_names=None, name=None, axis_physical_types=None, reference_position=None): super(SpectralFrame, self).__init__(naxes=1, axes_type="SPECTRAL", axes_order=axes_order, axes_names=axes_names, reference_frame=reference_frame, unit=unit, name=name, reference_position=reference_position, axis_physical_types=axis_physical_types)
(self, axes_order=(0,), reference_frame=None, unit=None, axes_names=None, name=None, axis_physical_types=None, reference_position=None)
44,457
gwcs.coordinate_frames
coordinate_to_quantity
null
def coordinate_to_quantity(self, *coords): if hasattr(coords[0], 'unit'): return coords[0] return coords[0] * self.unit[0]
(self, *coords)
44,458
gwcs.coordinate_frames
coordinates
null
def coordinates(self, *args): # using SpectralCoord if isinstance(args[0], coord.SpectralCoord): return args[0].to(self.unit[0]) else: if hasattr(args[0], 'unit'): return coord.SpectralCoord(*args).to(self.unit[0]) else: return coord.SpectralCoord(*args, self.unit[0])
(self, *args)
44,459
gwcs.wcs
Step
Represents a ``step`` in the WCS pipeline. Parameters ---------- frame : `~gwcs.coordinate_frames.CoordinateFrame` A gwcs coordinate frame object. transform : `~astropy.modeling.Model` or None A transform from this step's frame to next step's frame. The transform of the last step should be `None`.
class Step: """ Represents a ``step`` in the WCS pipeline. Parameters ---------- frame : `~gwcs.coordinate_frames.CoordinateFrame` A gwcs coordinate frame object. transform : `~astropy.modeling.Model` or None A transform from this step's frame to next step's frame. The transform of the last step should be `None`. """ def __init__(self, frame, transform=None): self.frame = frame self.transform = transform @property def frame(self): return self._frame @frame.setter def frame(self, val): if not isinstance(val, (cf.CoordinateFrame, str)): raise TypeError('"frame" should be an instance of CoordinateFrame or a string.') self._frame = val @property def transform(self): return self._transform @transform.setter def transform(self, val): if val is not None and not isinstance(val, (Model)): raise TypeError('"transform" should be an instance of astropy.modeling.Model.') self._transform = val @property def frame_name(self): if isinstance(self.frame, str): return self.frame return self.frame.name def __getitem__(self, ind): warnings.warn("Indexing a WCS.pipeline step is deprecated. " "Use the `frame` and `transform` attributes instead.", DeprecationWarning) if ind not in (0, 1): raise IndexError("Allowed inices are 0 (frame) and 1 (transform).") if ind == 0: return self.frame return self.transform def __str__(self): return f"{self.frame_name}\t {getattr(self.transform, 'name', 'None') or self.transform.__class__.__name__}" def __repr__(self): return f"Step(frame={self.frame_name}, \ transform={getattr(self.transform, 'name', 'None') or self.transform.__class__.__name__})"
(frame, transform=None)
44,460
gwcs.wcs
__getitem__
null
def __getitem__(self, ind): warnings.warn("Indexing a WCS.pipeline step is deprecated. " "Use the `frame` and `transform` attributes instead.", DeprecationWarning) if ind not in (0, 1): raise IndexError("Allowed inices are 0 (frame) and 1 (transform).") if ind == 0: return self.frame return self.transform
(self, ind)
44,461
gwcs.wcs
__init__
null
def __init__(self, frame, transform=None): self.frame = frame self.transform = transform
(self, frame, transform=None)
44,462
gwcs.wcs
__repr__
null
def __repr__(self): return f"Step(frame={self.frame_name}, \ transform={getattr(self.transform, 'name', 'None') or self.transform.__class__.__name__})"
(self)
44,463
gwcs.wcs
__str__
null
def __str__(self): return f"{self.frame_name}\t {getattr(self.transform, 'name', 'None') or self.transform.__class__.__name__}"
(self)
44,464
gwcs.coordinate_frames
StokesFrame
A coordinate frame for representing Stokes polarisation states. Parameters ---------- name : str Name of this frame. axes_order : tuple A dimension in the data that corresponds to this axis.
class StokesFrame(CoordinateFrame): """ A coordinate frame for representing Stokes polarisation states. Parameters ---------- name : str Name of this frame. axes_order : tuple A dimension in the data that corresponds to this axis. """ def __init__(self, axes_order=(0,), axes_names=("stokes",), name=None, axis_physical_types=None): super(StokesFrame, self).__init__(1, ["STOKES"], axes_order, name=name, axes_names=axes_names, unit=u.one, axis_physical_types=axis_physical_types) @property def _default_axis_physical_types(self): return ("phys.polarization.stokes",) @property def _world_axis_object_classes(self): return {'stokes': ( StokesCoord, (), {}, )} @property def _world_axis_object_components(self): return [('stokes', 0, 'value')] def coordinates(self, *args): if isinstance(args[0], u.Quantity): arg = args[0].value else: arg = args[0] return StokesCoord(arg) def coordinate_to_quantity(self, *coords): if isinstance(coords[0], StokesCoord): return coords[0].value << u.one return coords[0]
(axes_order=(0,), axes_names=('stokes',), name=None, axis_physical_types=None)
44,465
gwcs.coordinate_frames
__init__
null
def __init__(self, axes_order=(0,), axes_names=("stokes",), name=None, axis_physical_types=None): super(StokesFrame, self).__init__(1, ["STOKES"], axes_order, name=name, axes_names=axes_names, unit=u.one, axis_physical_types=axis_physical_types)
(self, axes_order=(0,), axes_names=('stokes',), name=None, axis_physical_types=None)
44,469
gwcs.coordinate_frames
coordinate_to_quantity
null
def coordinate_to_quantity(self, *coords): if isinstance(coords[0], StokesCoord): return coords[0].value << u.one return coords[0]
(self, *coords)
44,470
gwcs.coordinate_frames
coordinates
null
def coordinates(self, *args): if isinstance(args[0], u.Quantity): arg = args[0].value else: arg = args[0] return StokesCoord(arg)
(self, *args)
44,471
gwcs.coordinate_frames
TemporalFrame
A coordinate frame for time axes. Parameters ---------- reference_frame : `~astropy.time.Time` A Time object which holds the time scale and format. If data is provided, it is the time zero point. To not set a zero point for the frame initialize ``reference_frame`` with an empty list. unit : str or `~astropy.units.Unit` Time unit. axes_names : str Time axis name. axes_order : tuple or int A dimension in the data that corresponds to this axis. name : str Name for this frame.
class TemporalFrame(CoordinateFrame): """ A coordinate frame for time axes. Parameters ---------- reference_frame : `~astropy.time.Time` A Time object which holds the time scale and format. If data is provided, it is the time zero point. To not set a zero point for the frame initialize ``reference_frame`` with an empty list. unit : str or `~astropy.units.Unit` Time unit. axes_names : str Time axis name. axes_order : tuple or int A dimension in the data that corresponds to this axis. name : str Name for this frame. """ def __init__(self, reference_frame, unit=None, axes_order=(0,), axes_names=None, name=None, axis_physical_types=None): axes_names = axes_names or "{}({}; {}".format(reference_frame.format, reference_frame.scale, reference_frame.location) super().__init__(naxes=1, axes_type="TIME", axes_order=axes_order, axes_names=axes_names, reference_frame=reference_frame, unit=unit, name=name, axis_physical_types=axis_physical_types) self._attrs = {} for a in self.reference_frame.info._represent_as_dict_extra_attrs: try: self._attrs[a] = getattr(self.reference_frame, a) except AttributeError: pass @property def _default_axis_physical_types(self): return ("time",) @property def _world_axis_object_classes(self): comp = ( time.Time, (), {'unit': self.unit[0], **self._attrs}, self._convert_to_time) return {'temporal': comp} @property def _world_axis_object_components(self): if isinstance(self.reference_frame.value, np.ndarray): return [('temporal', 0, 'value')] def offset_from_time_and_reference(time): return (time - self.reference_frame).sec return [('temporal', 0, offset_from_time_and_reference)] def coordinates(self, *args): if np.isscalar(args): dt = args else: dt = args[0] return self._convert_to_time(dt, unit=self.unit[0], **self._attrs) def _convert_to_time(self, dt, *, unit, **kwargs): if (not isinstance(dt, time.TimeDelta) and isinstance(dt, time.Time) or isinstance(self.reference_frame.value, np.ndarray)): return time.Time(dt, **kwargs) if not hasattr(dt, 'unit'): dt = dt * unit return self.reference_frame + dt def coordinate_to_quantity(self, *coords): if isinstance(coords[0], time.Time): ref_value = self.reference_frame.value if not isinstance(ref_value, np.ndarray): return (coords[0] - self.reference_frame).to(self.unit[0]) else: # If we can't convert to a quantity just drop the object out # and hope the transform can cope. return coords[0] # Is already a quantity elif hasattr(coords[0], 'unit'): return coords[0] if isinstance(coords[0], np.ndarray): return coords[0] * self.unit[0] else: raise ValueError("Can not convert {} to Quantity".format(coords[0]))
(reference_frame, unit=None, axes_order=(0,), axes_names=None, name=None, axis_physical_types=None)
44,472
gwcs.coordinate_frames
__init__
null
def __init__(self, reference_frame, unit=None, axes_order=(0,), axes_names=None, name=None, axis_physical_types=None): axes_names = axes_names or "{}({}; {}".format(reference_frame.format, reference_frame.scale, reference_frame.location) super().__init__(naxes=1, axes_type="TIME", axes_order=axes_order, axes_names=axes_names, reference_frame=reference_frame, unit=unit, name=name, axis_physical_types=axis_physical_types) self._attrs = {} for a in self.reference_frame.info._represent_as_dict_extra_attrs: try: self._attrs[a] = getattr(self.reference_frame, a) except AttributeError: pass
(self, reference_frame, unit=None, axes_order=(0,), axes_names=None, name=None, axis_physical_types=None)
44,475
gwcs.coordinate_frames
_convert_to_time
null
def _convert_to_time(self, dt, *, unit, **kwargs): if (not isinstance(dt, time.TimeDelta) and isinstance(dt, time.Time) or isinstance(self.reference_frame.value, np.ndarray)): return time.Time(dt, **kwargs) if not hasattr(dt, 'unit'): dt = dt * unit return self.reference_frame + dt
(self, dt, *, unit, **kwargs)
44,477
gwcs.coordinate_frames
coordinate_to_quantity
null
def coordinate_to_quantity(self, *coords): if isinstance(coords[0], time.Time): ref_value = self.reference_frame.value if not isinstance(ref_value, np.ndarray): return (coords[0] - self.reference_frame).to(self.unit[0]) else: # If we can't convert to a quantity just drop the object out # and hope the transform can cope. return coords[0] # Is already a quantity elif hasattr(coords[0], 'unit'): return coords[0] if isinstance(coords[0], np.ndarray): return coords[0] * self.unit[0] else: raise ValueError("Can not convert {} to Quantity".format(coords[0]))
(self, *coords)
44,478
gwcs.coordinate_frames
coordinates
null
def coordinates(self, *args): if np.isscalar(args): dt = args else: dt = args[0] return self._convert_to_time(dt, unit=self.unit[0], **self._attrs)
(self, *args)
44,479
gwcs.wcs
WCS
Basic WCS class. Parameters ---------- forward_transform : `~astropy.modeling.Model` or a list The transform between ``input_frame`` and ``output_frame``. A list of (frame, transform) tuples where ``frame`` is the starting frame and ``transform`` is the transform from this frame to the next one or ``output_frame``. The last tuple is (transform, None), where None indicates the end of the pipeline. input_frame : str, `~gwcs.coordinate_frames.CoordinateFrame` A coordinates object or a string name. output_frame : str, `~gwcs.coordinate_frames.CoordinateFrame` A coordinates object or a string name. name : str a name for this WCS
class WCS(GWCSAPIMixin): """ Basic WCS class. Parameters ---------- forward_transform : `~astropy.modeling.Model` or a list The transform between ``input_frame`` and ``output_frame``. A list of (frame, transform) tuples where ``frame`` is the starting frame and ``transform`` is the transform from this frame to the next one or ``output_frame``. The last tuple is (transform, None), where None indicates the end of the pipeline. input_frame : str, `~gwcs.coordinate_frames.CoordinateFrame` A coordinates object or a string name. output_frame : str, `~gwcs.coordinate_frames.CoordinateFrame` A coordinates object or a string name. name : str a name for this WCS """ def __init__(self, forward_transform=None, input_frame='detector', output_frame=None, name=""): #self.low_level_wcs = self self._approx_inverse = None self._available_frames = [] self._pipeline = [] self._name = name self._initialize_wcs(forward_transform, input_frame, output_frame) self._pixel_shape = None pipe = [] for step in self._pipeline: if isinstance(step, Step): pipe.append(Step(step.frame, step.transform)) else: pipe.append(Step(*step)) self._pipeline = pipe def _initialize_wcs(self, forward_transform, input_frame, output_frame): if forward_transform is not None: if isinstance(forward_transform, Model): if output_frame is None: raise CoordinateFrameError("An output_frame must be specified " "if forward_transform is a model.") _input_frame, inp_frame_obj = self._get_frame_name(input_frame) _output_frame, outp_frame_obj = self._get_frame_name(output_frame) super(WCS, self).__setattr__(_input_frame, inp_frame_obj) super(WCS, self).__setattr__(_output_frame, outp_frame_obj) self._pipeline = [(input_frame, forward_transform.copy()), (output_frame, None)] elif isinstance(forward_transform, list): for item in forward_transform: if isinstance(item, Step): name, frame_obj = self._get_frame_name(item.frame) else: name, frame_obj = self._get_frame_name(item[0]) super(WCS, self).__setattr__(name, frame_obj) #self._pipeline.append((name, item[1])) self._pipeline = forward_transform else: raise TypeError("Expected forward_transform to be a model or a " "(frame, transform) list, got {0}".format( type(forward_transform))) else: # Initialize a WCS without a forward_transform - allows building a WCS programmatically. if output_frame is None: raise CoordinateFrameError("An output_frame must be specified " "if forward_transform is None.") _input_frame, inp_frame_obj = self._get_frame_name(input_frame) _output_frame, outp_frame_obj = self._get_frame_name(output_frame) super(WCS, self).__setattr__(_input_frame, inp_frame_obj) super(WCS, self).__setattr__(_output_frame, outp_frame_obj) self._pipeline = [(_input_frame, None), (_output_frame, None)] def get_transform(self, from_frame, to_frame): """ Return a transform between two coordinate frames. Parameters ---------- from_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Initial coordinate frame name of object. to_frame : str, or instance of `~gwcs.coordinate_frames.CoordinateFrame` End coordinate frame name or object. Returns ------- transform : `~astropy.modeling.Model` Transform between two frames. """ if not self._pipeline: return None from_ind = self._get_frame_index(from_frame) to_ind = self._get_frame_index(to_frame) if to_ind < from_ind: transforms = [step.transform for step in self._pipeline[to_ind: from_ind]] transforms = [tr.inverse for tr in transforms[::-1]] elif to_ind == from_ind: return None else: transforms = [step.transform for step in self._pipeline[from_ind: to_ind]] return functools.reduce(lambda x, y: x | y, transforms) def set_transform(self, from_frame, to_frame, transform): """ Set/replace the transform between two coordinate frames. Parameters ---------- from_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Initial coordinate frame. to_frame : str, or instance of `~gwcs.coordinate_frames.CoordinateFrame` End coordinate frame. transform : `~astropy.modeling.Model` Transform between ``from_frame`` and ``to_frame``. """ from_name, from_obj = self._get_frame_name(from_frame) to_name, to_obj = self._get_frame_name(to_frame) if not self._pipeline: if from_name != self._input_frame: raise CoordinateFrameError( "Expected 'from_frame' to be {0}".format(self._input_frame)) if to_frame != self._output_frame: raise CoordinateFrameError( "Expected 'to_frame' to be {0}".format(self._output_frame)) try: from_ind = self._get_frame_index(from_name) except ValueError: raise CoordinateFrameError("Frame {0} is not in the available frames".format(from_name)) try: to_ind = self._get_frame_index(to_name) except ValueError: raise CoordinateFrameError("Frame {0} is not in the available frames".format(to_name)) if from_ind + 1 != to_ind: raise ValueError("Frames {0} and {1} are not in sequence".format(from_name, to_name)) self._pipeline[from_ind].transform = transform @property def forward_transform(self): """ Return the total forward transform - from input to output coordinate frame. """ if self._pipeline: #return functools.reduce(lambda x, y: x | y, [step[1] for step in self._pipeline[: -1]]) return functools.reduce(lambda x, y: x | y, [step.transform for step in self._pipeline[:-1]]) else: return None @property def backward_transform(self): """ Return the total backward transform if available - from output to input coordinate system. Raises ------ NotImplementedError : An analytical inverse does not exist. """ try: backward = self.forward_transform.inverse except NotImplementedError as err: raise NotImplementedError("Could not construct backward transform. \n{0}".format(err)) try: backward.inverse except NotImplementedError: # means "hasattr" won't work backward.inverse = self.forward_transform return backward def _get_frame_index(self, frame): """ Return the index in the pipeline where this frame is locate. """ if isinstance(frame, cf.CoordinateFrame): frame = frame.name frame_names = [step.frame if isinstance(step.frame, str) else step.frame.name for step in self._pipeline] try: return frame_names.index(frame) except ValueError as e: raise CoordinateFrameError(f"Frame {frame} is not in the available frames") from e def _get_frame_name(self, frame): """ Return the name of the frame and a ``CoordinateFrame`` object. Parameters ---------- frame : str, `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame. Returns ------- name : str The name of the frame frame_obj : `~gwcs.coordinate_frames.CoordinateFrame` Frame instance or None (if `frame` is str) """ if isinstance(frame, str): name = frame frame_obj = None else: name = frame.name frame_obj = frame return name, frame_obj def __call__(self, *args, **kwargs): """ Executes the forward transform. args : float or array-like Inputs in the input coordinate system, separate inputs for each dimension. with_units : bool If ``True`` returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object, by using the units of the output cooridnate frame. Optional, default=False. with_bounding_box : bool, optional If True(default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is np.nan). """ transform = self.forward_transform if transform is None: raise NotImplementedError("WCS.forward_transform is not implemented.") with_units = kwargs.pop("with_units", False) if 'with_bounding_box' not in kwargs: kwargs['with_bounding_box'] = True if 'fill_value' not in kwargs: kwargs['fill_value'] = np.nan if self.bounding_box is not None: # Currently compound models do not attempt to combine individual model # bounding boxes. Get the forward transform and assign the bounding_box to it # before evaluating it. The order Model.bounding_box is reversed. transform.bounding_box = self.bounding_box result = transform(*args, **kwargs) if with_units: if self.output_frame.naxes == 1: result = self.output_frame.coordinates(result) else: result = self.output_frame.coordinates(*result) return result def in_image(self, *args, **kwargs): """ This method tests if one or more of the input world coordinates are contained within forward transformation's image and that it maps to the domain of definition of the forward transformation. In practical terms, this function tests that input world coordinate(s) can be converted to input frame and that it is within the forward transformation's ``bounding_box`` when defined. Parameters ---------- args : float, array like, `~astropy.coordinates.SkyCoord` or `~astropy.units.Unit` coordinates to be inverted kwargs : dict keyword arguments to be passed either to ``backward_transform`` (when defined) or to the iterative invert method. Returns ------- result : bool, numpy.ndarray A single boolean value or an array of boolean values with `True` indicating that the WCS footprint contains the coordinate and `False` if input is outside the footprint. """ kwargs['with_bounding_box'] = True kwargs['fill_value'] = np.nan coords = self.invert(*args, **kwargs) result = np.isfinite(coords) if self.input_frame.naxes > 1: result = np.all(result, axis=0) if self.bounding_box is None or not np.any(result): return result if self.input_frame.naxes == 1: x1, x2 = self.bounding_box.bounding_box() if len(np.shape(args[0])) > 0: result[result] = (coords[result] >= x1) & (coords[result] <= x2) elif result: result = (coords >= x1) and (coords <= x2) else: if len(np.shape(args[0])) > 0: for c, (x1, x2) in zip(coords, self.bounding_box): result[result] = (c[result] >= x1) & (c[result] <= x2) elif result: result = all([(c >= x1) and (c <= x2) for c, (x1, x2) in zip(coords, self.bounding_box)]) return result def invert(self, *args, **kwargs): """ Invert coordinates from output frame to input frame using analytical or user-supplied inverse. When neither analytical nor user-supplied inverses are defined, a numerical solution will be attempted using :py:meth:`numerical_inverse`. .. note:: Currently numerical inverse is implemented only for 2D imaging WCS. Parameters ---------- args : float, array like, `~astropy.coordinates.SkyCoord` or `~astropy.units.Unit` Coordinates to be inverted. The number of arguments must be equal to the number of world coordinates given by ``world_n_dim``. with_bounding_box : bool, optional If `True` (default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is ``np.nan``). with_units : bool, optional If ``True`` returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object, by using the units of the output cooridnate frame. Default is `False`. Other Parameters ---------------- kwargs : dict Keyword arguments to be passed to :py:meth:`numerical_inverse` (when defined) or to the iterative invert method. Returns ------- result : tuple or value Returns a tuple of scalar or array values for each axis. Unless ``input_frame.naxes == 1`` when it shall return the value. """ with_units = kwargs.pop('with_units', False) if not utils.isnumerical(args[0]): args = self.output_frame.coordinate_to_quantity(*args) if self.output_frame.naxes == 1: args = [args] try: if not self.backward_transform.uses_quantity: args = utils.get_values(self.output_frame.unit, *args) except (NotImplementedError, KeyError): args = utils.get_values(self.output_frame.unit, *args) if 'with_bounding_box' not in kwargs: kwargs['with_bounding_box'] = True if 'fill_value' not in kwargs: kwargs['fill_value'] = np.nan try: # remove iterative inverse-specific keyword arguments: akwargs = {k: v for k, v in kwargs.items() if k not in _ITER_INV_KWARGS} result = self.backward_transform(*args, **akwargs) except (NotImplementedError, KeyError): result = self.numerical_inverse(*args, **kwargs, with_units=with_units) if with_units and self.input_frame: if self.input_frame.naxes == 1: return self.input_frame.coordinates(result) else: return self.input_frame.coordinates(*result) else: return result def numerical_inverse(self, *args, tolerance=1e-5, maxiter=50, adaptive=True, detect_divergence=True, quiet=True, with_bounding_box=True, fill_value=np.nan, with_units=False, **kwargs): """ Invert coordinates from output frame to input frame using numerical inverse. .. note:: Currently numerical inverse is implemented only for 2D imaging WCS. .. note:: This method uses a combination of vectorized fixed-point iterations algorithm and `scipy.optimize.root`. The later is used for input coordinates for which vectorized algorithm diverges. Parameters ---------- args : float, array like, `~astropy.coordinates.SkyCoord` or `~astropy.units.Unit` Coordinates to be inverted. The number of arguments must be equal to the number of world coordinates given by ``world_n_dim``. with_bounding_box : bool, optional If `True` (default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is ``np.nan``). with_units : bool, optional If ``True`` returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object, by using the units of the output cooridnate frame. Default is `False`. tolerance : float, optional *Absolute tolerance* of solution. Iteration terminates when the iterative solver estimates that the "true solution" is within this many pixels current estimate, more specifically, when the correction to the solution found during the previous iteration is smaller (in the sense of the L2 norm) than ``tolerance``. Default ``tolerance`` is 1.0e-5. maxiter : int, optional Maximum number of iterations allowed to reach a solution. Default is 50. quiet : bool, optional Do not throw :py:class:`NoConvergence` exceptions when the method does not converge to a solution with the required accuracy within a specified number of maximum iterations set by ``maxiter`` parameter. Instead, simply return the found solution. Default is `True`. adaptive : bool, optional Specifies whether to adaptively select only points that did not converge to a solution within the required accuracy for the next iteration. Default (`True`) is recommended. .. note:: The :py:meth:`numerical_inverse` uses a vectorized implementation of the method of consecutive approximations (see ``Notes`` section below) in which it iterates over *all* input points *regardless* until the required accuracy has been reached for *all* input points. In some cases it may be possible that *almost all* points have reached the required accuracy but there are only a few of input data points for which additional iterations may be needed (this depends mostly on the characteristics of the geometric distortions for a given instrument). In this situation it may be advantageous to set ``adaptive`` = `True` in which case :py:meth:`numerical_inverse` will continue iterating *only* over the points that have not yet converged to the required accuracy. .. note:: When ``detect_divergence`` is `True`, :py:meth:`numerical_inverse` will automatically switch to the adaptive algorithm once divergence has been detected. detect_divergence : bool, optional Specifies whether to perform a more detailed analysis of the convergence to a solution. Normally :py:meth:`numerical_inverse` may not achieve the required accuracy if either the ``tolerance`` or ``maxiter`` arguments are too low. However, it may happen that for some geometric distortions the conditions of convergence for the the method of consecutive approximations used by :py:meth:`numerical_inverse` may not be satisfied, in which case consecutive approximations to the solution will diverge regardless of the ``tolerance`` or ``maxiter`` settings. When ``detect_divergence`` is `False`, these divergent points will be detected as not having achieved the required accuracy (without further details). In addition, if ``adaptive`` is `False` then the algorithm will not know that the solution (for specific points) is diverging and will continue iterating and trying to "improve" diverging solutions. This may result in ``NaN`` or ``Inf`` values in the return results (in addition to a performance penalties). Even when ``detect_divergence`` is `False`, :py:meth:`numerical_inverse`, at the end of the iterative process, will identify invalid results (``NaN`` or ``Inf``) as "diverging" solutions and will raise :py:class:`NoConvergence` unless the ``quiet`` parameter is set to `True`. When ``detect_divergence`` is `True` (default), :py:meth:`numerical_inverse` will detect points for which current correction to the coordinates is larger than the correction applied during the previous iteration **if** the requested accuracy **has not yet been achieved**. In this case, if ``adaptive`` is `True`, these points will be excluded from further iterations and if ``adaptive`` is `False`, :py:meth:`numerical_inverse` will automatically switch to the adaptive algorithm. Thus, the reported divergent solution will be the latest converging solution computed immediately *before* divergence has been detected. .. note:: When accuracy has been achieved, small increases in current corrections may be possible due to rounding errors (when ``adaptive`` is `False`) and such increases will be ignored. .. note:: Based on our testing using JWST NIRCAM images, setting ``detect_divergence`` to `True` will incur about 5-10% performance penalty with the larger penalty corresponding to ``adaptive`` set to `True`. Because the benefits of enabling this feature outweigh the small performance penalty, especially when ``adaptive`` = `False`, it is recommended to set ``detect_divergence`` to `True`, unless extensive testing of the distortion models for images from specific instruments show a good stability of the numerical method for a wide range of coordinates (even outside the image itself). .. note:: Indices of the diverging inverse solutions will be reported in the ``divergent`` attribute of the raised :py:class:`NoConvergence` exception object. Returns ------- result : tuple Returns a tuple of scalar or array values for each axis. Raises ------ NoConvergence The iterative method did not converge to a solution to the required accuracy within a specified number of maximum iterations set by the ``maxiter`` parameter. To turn off this exception, set ``quiet`` to `True`. Indices of the points for which the requested accuracy was not achieved (if any) will be listed in the ``slow_conv`` attribute of the raised :py:class:`NoConvergence` exception object. See :py:class:`NoConvergence` documentation for more details. NotImplementedError Numerical inverse has not been implemented for this WCS. ValueError Invalid argument values. Examples -------- >>> from astropy.utils.data import get_pkg_data_filename >>> from gwcs import NoConvergence >>> import asdf >>> import numpy as np >>> filename = get_pkg_data_filename('data/nircamwcs.asdf', package='gwcs.tests') >>> with asdf.open(filename, copy_arrays=True, lazy_load=False, ignore_missing_extensions=True) as af: ... w = af.tree['wcs'] >>> ra, dec = w([1,2,3], [1,1,1]) >>> assert np.allclose(ra, [5.927628, 5.92757069, 5.92751337]); >>> assert np.allclose(dec, [-72.01341247, -72.01341273, -72.013413]) >>> x, y = w.numerical_inverse(ra, dec) >>> assert np.allclose(x, [1.00000005, 2.00000005, 3.00000006]); >>> assert np.allclose(y, [1.00000004, 0.99999979, 1.00000015]); >>> x, y = w.numerical_inverse(ra, dec, maxiter=3, tolerance=1.0e-10, quiet=False) Traceback (most recent call last): ... gwcs.wcs.NoConvergence: 'WCS.numerical_inverse' failed to converge to the requested accuracy after 3 iterations. >>> w.numerical_inverse( ... *w([1, 300000, 3], [2, 1000000, 5], with_bounding_box=False), ... adaptive=False, ... detect_divergence=True, ... quiet=False, ... with_bounding_box=False ... ) Traceback (most recent call last): ... gwcs.wcs.NoConvergence: 'WCS.numerical_inverse' failed to converge to the requested accuracy. After 4 iterations, the solution is diverging at least for one input point. >>> # Now try to use some diverging data: >>> divra, divdec = w([1, 300000, 3], [2, 1000000, 5], with_bounding_box=False) >>> assert np.allclose(divra, [5.92762673, 148.21600848, 5.92750827]) >>> assert np.allclose(divdec, [-72.01339464, -7.80968079, -72.01334172]) >>> try: # doctest: +SKIP ... x, y = w.numerical_inverse(divra, divdec, maxiter=20, ... tolerance=1.0e-4, adaptive=True, ... detect_divergence=True, ... quiet=False) ... except NoConvergence as e: ... print(f"Indices of diverging points: {e.divergent}") ... print(f"Indices of poorly converging points: {e.slow_conv}") ... print(f"Best solution:\\n{e.best_solution}") ... print(f"Achieved accuracy:\\n{e.accuracy}") Indices of diverging points: None Indices of poorly converging points: [1] Best solution: [[1.00000040e+00 1.99999841e+00] [6.33507833e+17 3.40118820e+17] [3.00000038e+00 4.99999841e+00]] Achieved accuracy: [[2.75925982e-05 1.18471543e-05] [3.65405005e+04 1.31364188e+04] [2.76552923e-05 1.14789013e-05]] """ if not utils.isnumerical(args[0]): args = self.output_frame.coordinate_to_quantity(*args) if self.output_frame.naxes == 1: args = [args] args = utils.get_values(self.output_frame.unit, *args) args_shape = np.shape(args) nargs = args_shape[0] arg_dim = len(args_shape) - 1 if nargs != self.world_n_dim: raise ValueError("Number of input coordinates is different from " "the number of defined world coordinates in the " f"WCS ({self.world_n_dim:d})") if self.world_n_dim != self.pixel_n_dim: raise NotImplementedError( "Support for iterative inverse for transformations with " "different number of inputs and outputs was not implemented." ) # initial guess: if nargs == 2 and self._approx_inverse is None: self._calc_approx_inv(max_inv_pix_error=5, inv_degree=None) if self._approx_inverse is None: if self.bounding_box is None: x0 = np.ones(self.pixel_n_dim) else: x0 = np.mean(self.bounding_box, axis=-1) if arg_dim == 0: argsi = args if nargs == 2 and self._approx_inverse is not None: x0 = self._approx_inverse(*argsi) if not np.all(np.isfinite(x0)): return [np.array(np.nan) for _ in range(nargs)] result = tuple(self._vectorized_fixed_point( x0, argsi, tolerance=tolerance, maxiter=maxiter, adaptive=adaptive, detect_divergence=detect_divergence, quiet=quiet, with_bounding_box=with_bounding_box, fill_value=fill_value ).T.ravel().tolist()) else: arg_shape = args_shape[1:] nelem = np.prod(arg_shape) args = np.reshape(args, (nargs, nelem)) if self._approx_inverse is None: x0 = np.full((nelem, nargs), x0) else: x0 = np.array(self._approx_inverse(*args)).T result = self._vectorized_fixed_point( x0, args.T, tolerance=tolerance, maxiter=maxiter, adaptive=adaptive, detect_divergence=detect_divergence, quiet=quiet, with_bounding_box=with_bounding_box, fill_value=fill_value ).T result = tuple(np.reshape(result, args_shape)) if with_units and self.input_frame: if self.input_frame.naxes == 1: return self.input_frame.coordinates(result) else: return self.input_frame.coordinates(*result) else: return result def _vectorized_fixed_point(self, pix0, world, tolerance, maxiter, adaptive, detect_divergence, quiet, with_bounding_box, fill_value): # ############################################################ # # INITIALIZE ITERATIVE PROCESS: ## # ############################################################ # make a copy of the initial approximation pix0 = np.atleast_2d(np.array(pix0)) # 0-order solution pix = np.array(pix0) world0 = np.atleast_2d(np.array(world)) world = np.array(world0) # estimate pixel scale using approximate algorithm # from https://trs.jpl.nasa.gov/handle/2014/40409 if self.bounding_box is None: crpix = np.ones(self.pixel_n_dim) else: crpix = np.mean(self.bounding_box, axis=-1) l1, phi1 = np.deg2rad(self.__call__(*(crpix - 0.5))) l2, phi2 = np.deg2rad(self.__call__(*(crpix + [-0.5, 0.5]))) l3, phi3 = np.deg2rad(self.__call__(*(crpix + 0.5))) l4, phi4 = np.deg2rad(self.__call__(*(crpix + [0.5, -0.5]))) area = np.abs(0.5 * ((l4 - l2) * (np.sin(phi1) - np.sin(phi3)) + (l1 - l3) * (np.sin(phi2) - np.sin(phi4)))) inv_pscale = 1 / np.rad2deg(np.sqrt(area)) # form equation: def f(x): w = np.array(self.__call__(*(x.T), with_bounding_box=False)).T dw = np.mod(np.subtract(w, world) - 180.0, 360.0) - 180.0 return np.add(inv_pscale * dw, x) def froot(x): return np.mod(np.subtract(self.__call__(*x, with_bounding_box=False), worldi) - 180.0, 360.0) - 180.0 # compute correction: def correction(pix): p1 = f(pix) p2 = f(p1) d = p2 - 2.0 * p1 + pix idx = np.where(d != 0) corr = pix - p2 corr[idx] = np.square(p1[idx] - pix[idx]) / d[idx] return corr # initial iteration: dpix = correction(pix) # Update initial solution: pix -= dpix # Norm (L2) squared of the correction: dn = np.sum(dpix * dpix, axis=1) dnprev = dn.copy() # if adaptive else dn tol2 = tolerance**2 # Prepare for iterative process k = 1 ind = None inddiv = None # Turn off numpy runtime warnings for 'invalid' and 'over': old_invalid = np.geterr()['invalid'] old_over = np.geterr()['over'] np.seterr(invalid='ignore', over='ignore') # ############################################################ # # NON-ADAPTIVE ITERATIONS: ## # ############################################################ if not adaptive: # Fixed-point iterations: while (np.nanmax(dn) >= tol2 and k < maxiter): # Find correction to the previous solution: dpix = correction(pix) # Compute norm (L2) squared of the correction: dn = np.sum(dpix * dpix, axis=1) # Check for divergence (we do this in two stages # to optimize performance for the most common # scenario when successive approximations converge): if detect_divergence: divergent = (dn >= dnprev) if np.any(divergent): # Find solutions that have not yet converged: slowconv = (dn >= tol2) inddiv, = np.where(divergent & slowconv) if inddiv.shape[0] > 0: # Update indices of elements that # still need correction: conv = (dn < dnprev) iconv = np.where(conv) # Apply correction: dpixgood = dpix[iconv] pix[iconv] -= dpixgood dpix[iconv] = dpixgood # For the next iteration choose # non-divergent points that have not yet # converged to the requested accuracy: ind, = np.where(slowconv & conv) world = world[ind] dnprev[ind] = dn[ind] k += 1 # Switch to adaptive iterations: adaptive = True break # Save current correction magnitudes for later: dnprev = dn # Apply correction: pix -= dpix k += 1 # ############################################################ # # ADAPTIVE ITERATIONS: ## # ############################################################ if adaptive: if ind is None: ind, = np.where(np.isfinite(pix).all(axis=1)) world = world[ind] # "Adaptive" fixed-point iterations: while (ind.shape[0] > 0 and k < maxiter): # Find correction to the previous solution: dpixnew = correction(pix[ind]) # Compute norm (L2) of the correction: dnnew = np.sum(np.square(dpixnew), axis=1) # Bookkeeping of corrections: dnprev[ind] = dn[ind].copy() dn[ind] = dnnew if detect_divergence: # Find indices of pixels that are converging: conv = np.logical_or(dnnew < dnprev[ind], dnnew < tol2) if not np.all(conv): conv = np.ones_like(dnnew, dtype=bool) iconv = np.where(conv) iiconv = ind[iconv] # Apply correction: dpixgood = dpixnew[iconv] pix[iiconv] -= dpixgood dpix[iiconv] = dpixgood # Find indices of solutions that have not yet # converged to the requested accuracy # AND that do not diverge: subind, = np.where((dnnew >= tol2) & conv) else: # Apply correction: pix[ind] -= dpixnew dpix[ind] = dpixnew # Find indices of solutions that have not yet # converged to the requested accuracy: subind, = np.where(dnnew >= tol2) # Choose solutions that need more iterations: ind = ind[subind] world = world[subind] k += 1 # ############################################################ # # FINAL DETECTION OF INVALID, DIVERGING, ## # # AND FAILED-TO-CONVERGE POINTS ## # ############################################################ # Identify diverging and/or invalid points: invalid = ((~np.all(np.isfinite(pix), axis=1)) & (np.all(np.isfinite(world0), axis=1))) # When detect_divergence is False, dnprev is outdated # (it is the norm of the very first correction). # Still better than nothing... inddiv, = np.where(((dn >= tol2) & (dn >= dnprev)) | invalid) if inddiv.shape[0] == 0: inddiv = None # If there are divergent points, attempt to find a solution using # scipy's 'hybr' method: if detect_divergence and inddiv is not None and inddiv.size: bad = [] for idx in inddiv: worldi = world0[idx] result = optimize.root( froot, pix0[idx], method='hybr', tol=tolerance / (np.linalg.norm(pix0[idx]) + 1), options={'maxfev': 2 * maxiter} ) if result['success']: pix[idx, :] = result['x'] invalid[idx] = False else: bad.append(idx) if bad: inddiv = np.array(bad, dtype=int) else: inddiv = None # Identify points that did not converge within 'maxiter' # iterations: if k >= maxiter: ind, = np.where((dn >= tol2) & (dn < dnprev) & (~invalid)) if ind.shape[0] == 0: ind = None else: ind = None # Restore previous numpy error settings: np.seterr(invalid=old_invalid, over=old_over) # ############################################################ # # RAISE EXCEPTION IF DIVERGING OR TOO SLOWLY CONVERGING ## # # DATA POINTS HAVE BEEN DETECTED: ## # ############################################################ if (ind is not None or inddiv is not None) and not quiet: if inddiv is None: raise NoConvergence( "'WCS.numerical_inverse' failed to " "converge to the requested accuracy after {:d} " "iterations.".format(k), best_solution=pix, accuracy=np.abs(dpix), niter=k, slow_conv=ind, divergent=None) else: raise NoConvergence( "'WCS.numerical_inverse' failed to " "converge to the requested accuracy.\n" "After {:d} iterations, the solution is diverging " "at least for one input point." .format(k), best_solution=pix, accuracy=np.abs(dpix), niter=k, slow_conv=ind, divergent=inddiv) if with_bounding_box and self.bounding_box is not None: # find points outside the bounding box and replace their values # with fill_value valid = np.logical_not(invalid) in_bb = np.ones_like(invalid, dtype=np.bool_) for c, (x1, x2) in zip(pix[valid].T, self.bounding_box): in_bb[valid] &= (c >= x1) & (c <= x2) pix[np.logical_not(in_bb)] = fill_value return pix def transform(self, from_frame, to_frame, *args, **kwargs): """ Transform positions between two frames. Parameters ---------- from_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Initial coordinate frame. to_frame : str, or instance of `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame into which to transform. args : float or array-like Inputs in ``from_frame``, separate inputs for each dimension. output_with_units : bool If ``True`` - returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object. with_bounding_box : bool, optional If True(default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is np.nan). """ transform = self.get_transform(from_frame, to_frame) if not utils.isnumerical(args[0]): inp_frame = getattr(self, from_frame) args = inp_frame.coordinate_to_quantity(*args) if not transform.uses_quantity: args = utils.get_values(inp_frame.unit, *args) with_units = kwargs.pop("with_units", False) if 'with_bounding_box' not in kwargs: kwargs['with_bounding_box'] = True if 'fill_value' not in kwargs: kwargs['fill_value'] = np.nan result = transform(*args, **kwargs) if with_units: to_frame_name, to_frame_obj = self._get_frame_name(to_frame) if to_frame_obj is not None: if to_frame_obj.naxes == 1: result = to_frame_obj.coordinates(result) else: result = to_frame_obj.coordinates(*result) else: raise TypeError("Coordinate objects could not be created because" "frame {0} is not defined.".format(to_frame_name)) return result @property def available_frames(self): """ List all frames in this WCS object. Returns ------- available_frames : dict {frame_name: frame_object or None} """ if self._pipeline: #return [getattr(frame[0], "name", frame[0]) for frame in self._pipeline] return [step.frame if isinstance(step.frame, str) else step.frame.name for step in self._pipeline ] else: return None def insert_transform(self, frame, transform, after=False): """ Insert a transform before (default) or after a coordinate frame. Append (or prepend) a transform to the transform connected to frame. Parameters ---------- frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame which sets the point of insertion. transform : `~astropy.modeling.Model` New transform to be inserted in the pipeline after : bool If True, the new transform is inserted in the pipeline immediately after ``frame``. """ name, _ = self._get_frame_name(frame) frame_ind = self._get_frame_index(name) if not after: current_transform = self._pipeline[frame_ind - 1].transform self._pipeline[frame_ind - 1].transform = current_transform | transform else: current_transform = self._pipeline[frame_ind].transform self._pipeline[frame_ind].transform = transform | current_transform def insert_frame(self, input_frame, transform, output_frame): """ Insert a new frame into an existing pipeline. This frame must be anchored to a frame already in the pipeline by a transform. This existing frame is identified solely by its name, although an entire `~gwcs.coordinate_frames.CoordinateFrame` can be passed (e.g., the `input_frame` or `output_frame` attribute). This frame is never modified. Parameters ---------- input_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame at start of new transform transform : `~astropy.modeling.Model` New transform to be inserted in the pipeline output_frame: str or `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame at end of new transform """ input_name, input_frame_obj = self._get_frame_name(input_frame) output_name, output_frame_obj = self._get_frame_name(output_frame) try: input_index = self._get_frame_index(input_frame) except CoordinateFrameError: input_index = None if input_frame_obj is None: raise ValueError(f"New coordinate frame {input_name} must " "be defined") try: output_index = self._get_frame_index(output_frame) except CoordinateFrameError: output_index = None if output_frame_obj is None: raise ValueError(f"New coordinate frame {output_name} must " "be defined") new_frames = [input_index, output_index].count(None) if new_frames == 0: raise ValueError("Could not insert frame as both frames " f"{input_name} and {output_name} already exist") elif new_frames == 2: raise ValueError("Could not insert frame as neither frame " f"{input_name} nor {output_name} exists") if input_index is None: self._pipeline = (self._pipeline[:output_index] + [Step(input_frame_obj, transform)] + self._pipeline[output_index:]) super(WCS, self).__setattr__(input_name, input_frame_obj) else: split_step = self._pipeline[input_index] self._pipeline = (self._pipeline[:input_index] + [Step(split_step.frame, transform), Step(output_frame_obj, split_step.transform)] + self._pipeline[input_index + 1:]) super(WCS, self).__setattr__(output_name, output_frame_obj) @property def unit(self): """The unit of the coordinates in the output coordinate system.""" if self._pipeline: try: #return getattr(self, self._pipeline[-1][0].name).unit return self._pipeline[-1].frame.unit except AttributeError: return None else: return None @property def output_frame(self): """Return the output coordinate frame.""" if self._pipeline: frame = self._pipeline[-1].frame if not isinstance(frame, str): frame = frame.name return getattr(self, frame) else: return None @property def input_frame(self): """Return the input coordinate frame.""" if self._pipeline: frame = self._pipeline[0].frame if not isinstance(frame, str): frame = frame.name return getattr(self, frame) else: return None @property def name(self): """Return the name for this WCS.""" return self._name @name.setter def name(self, value): """Set the name for the WCS.""" self._name = value @property def pipeline(self): """Return the pipeline structure.""" return self._pipeline @property def bounding_box(self): """ Return the range of acceptable values for each input axis. The order of the axes is `~gwcs.coordinate_frames.CoordinateFrame.axes_order`. """ frames = self.available_frames transform_0 = self.get_transform(frames[0], frames[1]) try: bb = transform_0.bounding_box except NotImplementedError: return None return bb @bounding_box.setter def bounding_box(self, value): """ Set the range of acceptable values for each input axis. The order of the axes is `~gwcs.coordinate_frames.CoordinateFrame.axes_order`. For two inputs and axes_order(0, 1) the bounding box is ((xlow, xhigh), (ylow, yhigh)). Parameters ---------- value : tuple or None Tuple of tuples with ("low", high") values for the range. """ frames = self.available_frames transform_0 = self.get_transform(frames[0], frames[1]) if value is None: transform_0.bounding_box = value else: try: # Make sure the dimensions of the new bbox are correct. if isinstance(value, CompoundBoundingBox): bbox = CompoundBoundingBox.validate(transform_0, value, order='F') else: bbox = Bbox.validate(transform_0, value, order='F') except Exception: raise transform_0.bounding_box = bbox self.set_transform(frames[0], frames[1], transform_0) def attach_compound_bounding_box(self, cbbox, selector_args): frames = self.available_frames transform_0 = self.get_transform(frames[0], frames[1]) self.bounding_box = CompoundBoundingBox.validate(transform_0, cbbox, selector_args=selector_args, order='F') def _get_axes_indices(self): try: axes_ind = np.argsort(self.input_frame.axes_order) except AttributeError: # the case of a frame being a string axes_ind = np.arange(self.forward_transform.n_inputs) return axes_ind def __str__(self): from astropy.table import Table col1 = [step.frame for step in self._pipeline] col2 = [] for item in self._pipeline[: -1]: model = item.transform if model is None: col2.append(None) elif model.name is not None: col2.append(model.name) else: col2.append(model.__class__.__name__) col2.append(None) t = Table([col1, col2], names=['From', 'Transform']) return str(t) def __repr__(self): fmt = "<WCS(output_frame={0}, input_frame={1}, forward_transform={2})>".format( self.output_frame, self.input_frame, self.forward_transform) return fmt def footprint(self, bounding_box=None, center=False, axis_type="all"): """ Return the footprint in world coordinates. Parameters ---------- bounding_box : tuple of floats: (start, stop) ``prop: bounding_box`` center : bool If `True` use the center of the pixel, otherwise use the corner. axis_type : str A supported ``output_frame.axes_type`` or ``"all"`` (default). One of [``'spatial'``, ``'spectral'``, ``'temporal'``] or a custom type. Returns ------- coord : ndarray Array of coordinates in the output_frame mapping corners to the output frame. For spatial coordinates the order is clockwise, starting from the bottom left corner. """ def _order_clockwise(v): return np.asarray([[v[0][0], v[1][0]], [v[0][0], v[1][1]], [v[0][1], v[1][1]], [v[0][1], v[1][0]]]).T if bounding_box is None: if self.bounding_box is None: raise TypeError("Need a valid bounding_box to compute the footprint.") bb = self.bounding_box else: bb = bounding_box all_spatial = all([t.lower() == "spatial" for t in self.output_frame.axes_type]) if all_spatial: vertices = _order_clockwise(bb) else: vertices = np.array(list(itertools.product(*bb))).T if center: vertices = utils._toindex(vertices) result = np.asarray(self.__call__(*vertices, **{'with_bounding_box': False})) axis_type = axis_type.lower() if axis_type == 'spatial' and all_spatial: return result.T if axis_type != "all": axtyp_ind = np.array([t.lower() for t in self.output_frame.axes_type]) == axis_type if not axtyp_ind.any(): raise ValueError('This WCS does not have axis of type "{}".'.format(axis_type)) result = np.asarray([(r.min(), r.max()) for r in result[axtyp_ind]]) if axis_type == "spatial": result = _order_clockwise(result) else: result.sort() result = np.squeeze(result) return result.T def fix_inputs(self, fixed): """ Return a new unique WCS by fixing inputs to constant values. Parameters ---------- fixed : dict Keyword arguments with fixed values corresponding to ``self.selector``. Returns ------- new_wcs : `WCS` A new unique WCS corresponding to the values in ``fixed``. Examples -------- >>> w = WCS(pipeline, selector={"spectral_order": [1, 2]}) # doctest: +SKIP >>> new_wcs = w.set_inputs(spectral_order=2) # doctest: +SKIP >>> new_wcs.inputs # doctest: +SKIP ("x", "y") """ new_pipeline = [] step0 = self.pipeline[0] new_transform = fix_inputs(step0[1], fixed) new_pipeline.append((step0[0], new_transform)) new_pipeline.extend(self.pipeline[1:]) return self.__class__(new_pipeline) def to_fits_sip(self, bounding_box=None, max_pix_error=0.25, degree=None, max_inv_pix_error=0.25, inv_degree=None, npoints=32, crpix=None, projection='TAN', verbose=False): """ Construct a SIP-based approximation to the WCS for the axes corresponding to the `~gwcs.coordinate_frames.CelestialFrame` in the form of a FITS header. The default mode in using this attempts to achieve roughly 0.25 pixel accuracy over the whole image. Parameters ---------- bounding_box : tuple, optional A pair of tuples, each consisting of two numbers Represents the range of pixel values in both dimensions ((xmin, xmax), (ymin, ymax)) max_pix_error : float, optional Maximum allowed error over the domain of the pixel array. This error is the equivalent pixel error that corresponds to the maximum error in the output coordinate resulting from the fit based on a nominal plate scale. Ignored when ``degree`` is an integer or a list with a single degree. degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_pixel_error`` is ignored. max_inv_pix_error : float, optional Maximum allowed inverse error over the domain of the pixel array in pixel units. If None, no inverse is generated. Ignored when ``degree`` is an integer or a list with a single degree. inv_degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_inv_pixel_error`` is ignored. npoints : int, optional The number of points in each dimension to sample the bounding box for use in the SIP fit. Minimum number of points is 3. crpix : list of float, None, optional Coordinates (1-based) of the reference point for the new FITS WCS. When not provided, i.e., when set to `None` (default) the reference pixel will be chosen near the center of the bounding box for axes corresponding to the celestial frame. projection : str, `~astropy.modeling.projections.Pix2SkyProjection`, optional Projection to be used for the created FITS WCS. It can be specified as a string of three characters specifying a FITS projection code from Table 13 in `Representations of World Coordinates in FITS \ <https://doi.org/10.1051/0004-6361:20021326>`_ (Paper I), Greisen, E. W., and Calabretta, M. R., A & A, 395, 1061-1075, 2002. Alternatively, it can be an instance of one of the `astropy's Pix2Sky_* <https://docs.astropy.org/en/stable/modeling/\ reference_api.html#module-astropy.modeling.projections>`_ projection models inherited from :py:class:`~astropy.modeling.projections.Pix2SkyProjection`. verbose : bool, optional Print progress of fits. Returns ------- FITS header with all SIP WCS keywords Raises ------ ValueError If the WCS is not at least 2D, an exception will be raised. If the specified accuracy (both forward and inverse, both rms and maximum) is not achieved an exception will be raised. Notes ----- Use of this requires a judicious choice of required accuracies. Attempts to use higher degrees (~7 or higher) will typically fail due to floating point problems that arise with high powers. """ _, _, celestial_group = self._separable_groups(detect_celestial=True) if celestial_group is None: raise ValueError("The to_fits_sip requires an output celestial frame.") hdr = self._to_fits_sip( celestial_group=celestial_group, keep_axis_position=False, bounding_box=bounding_box, max_pix_error=max_pix_error, degree=degree, max_inv_pix_error=max_inv_pix_error, inv_degree=inv_degree, npoints=npoints, crpix=crpix, projection=projection, matrix_type='CD', verbose=verbose ) return hdr def _to_fits_sip(self, celestial_group, keep_axis_position, bounding_box, max_pix_error, degree, max_inv_pix_error, inv_degree, npoints, crpix, projection, matrix_type, verbose): r""" Construct a SIP-based approximation to the WCS for the axes corresponding to the `~gwcs.coordinate_frames.CelestialFrame` in the form of a FITS header. The default mode in using this attempts to achieve roughly 0.25 pixel accuracy over the whole image. Below we describe only parameters additional to the ones explained for `to_fits_sip`. Other Parameters ---------------- frame : gwcs.coordinate_frames.CelestialFrame A celestial frame. celestial_group : list of ``_WorldAxisInfo`` A group of two celestial axes to be represented using standard image FITS WCS and maybe ``-SIP`` polynomials. keep_axis_position : bool This parameter controls whether to keep/preserve output axes indices in this WCS object when creating FITS WCS and create a FITS header with ``CTYPE`` axes indices preserved from the ``frame`` object or whether to reset the indices of output celestial axes to 1 and 2 with ``CTYPE1``, ``CTYPE2``. Default is `False`. .. warning:: Returned header will have both ``NAXIS`` and ``WCSAXES`` set to 2. If ``max(axes_mapping) > 2`` this will lead to an invalid WCS. It is caller's responsibility to adjust NAXIS to a valid value. .. note:: The ``lon``/``lat`` order is still preserved regardless of this setting. matrix_type : {'CD', 'PC-CDELT1', 'PC-SUM1', 'PC-DET1', 'PC-SCALE'} Specifies formalism (``PC`` or ``CD``) to be used for the linear transformation matrix and normalization for the ``PC`` matrix *when non-linear polynomial terms are not required to achieve requested accuracy*. .. note:: ``CD`` matrix is always used when requested SIP approximation accuracy requires non-linear terms (when ``CTYPE`` ends in ``-SIP``). This parameter is ignored when non-linear polynomial terms are used. - ``'CD'``: use ``CD`` matrix; - ``'PC-CDELT1'``: set ``PC=CD`` and ``CDELTi=1``. This is the behavior of `~astropy.wcs.WCS.to_header` method; - ``'PC-SUM1'``: normalize ``PC`` matrix such that sum of its squared elements is 1: :math:`\Sigma PC_{ij}^2=1`; - ``'PC-DET1'``: normalize ``PC`` matrix such that :math:`|\det(PC)|=1`; - ``'PC-SCALE'``: normalize ``PC`` matrix such that ``CDELTi`` are estimates of the linear pixel scales. Returns ------- FITS header with all SIP WCS keywords Raises ------ ValueError If the WCS is not at least 2D, an exception will be raised. If the specified accuracy (both forward and inverse, both rms and maximum) is not achieved an exception will be raised. """ if isinstance(matrix_type, str): matrix_type = matrix_type.upper() if matrix_type not in ['CD', 'PC-CDELT1', 'PC-SUM1', 'PC-DET1', 'PC-SCALE']: raise ValueError(f"Unsupported 'matrix_type' value: {repr(matrix_type)}.") if npoints < 8: raise ValueError("Number of sampling points is too small. 'npoints' must be >= 8.") if isinstance(projection, str): projection = projection.upper() try: sky2pix_proj = getattr(projections, f'Sky2Pix_{projection}')(name=projection) except AttributeError: raise ValueError("Unsupported FITS WCS sky projection: {projection}") elif isinstance(projection, projections.Sky2PixProjection): sky2pix_proj = projection projection = projection.name if not projection or not isinstance(projection, str) or len(projection) != 3: raise ValueError("Unsupported FITS WCS sky projection: {sky2pix_proj}") try: getattr(projections, f'Sky2Pix_{projection}')() except AttributeError: raise ValueError("Unsupported FITS WCS sky projection: {projection}") else: raise TypeError( "'projection' must be either a FITS WCS string projection code " "or an instance of astropy.modeling.projections.Pix2SkyProjection.") frame = celestial_group[0].frame lon_axis = frame.axes_order[0] lat_axis = frame.axes_order[1] # identify input axes: input_axes = [] for wax in celestial_group: input_axes.extend(wax.input_axes) input_axes = sorted(set(input_axes)) if len(input_axes) != 2: raise ValueError("Only CelestialFrame that correspond to two " "input axes are supported.") # Axis number for FITS axes. # iax? - image axes; nlon, nlat - celestial axes: if keep_axis_position: nlon = lon_axis + 1 nlat = lat_axis + 1 iax1, iax2 = (i + 1 for i in input_axes) else: nlon, nlat = (1, 2) if lon_axis < lat_axis else (2, 1) iax1 = 1 iax2 = 2 # Determine reference points. if bounding_box is None and self.bounding_box is None: raise ValueError("A bounding_box is needed to proceed.") if bounding_box is None: bounding_box = self.bounding_box bb_center = np.mean(bounding_box, axis=1) fixi_dict = { k: bb_center[k] for k in set(range(self.pixel_n_dim)).difference(input_axes) } # transform = fix_inputs(self.forward_transform, fixi_dict) # This is a workaround to the bug in https://github.com/astropy/astropy/issues/11360 # Once that bug is fixed, the code below can be replaced with fix_inputs # statement commented out immediately above. transform = _fix_transform_inputs(self.forward_transform, fixi_dict) transform = transform | Mapping((lon_axis, lat_axis), n_inputs=self.forward_transform.n_outputs) (xmin, xmax) = bounding_box[input_axes[0]] (ymin, ymax) = bounding_box[input_axes[1]] # 0-based crpix: if crpix is None: crpix1 = round(bb_center[input_axes[0]], 1) crpix2 = round(bb_center[input_axes[1]], 1) else: crpix1 = crpix[0] - 1 crpix2 = crpix[1] - 1 # check that the bounding box has some reasonable size: if (xmax - xmin) < 1 or (ymax - ymin) < 1: raise ValueError("Bounding box is too small for fitting a SIP polynomial") lon, lat = transform(crpix1, crpix2) # Now rotate to native system and deproject. Recall that transform # expects pixels in the original coordinate system, but the SIP # transform is relative to crpix coordinates, thus the initial shift. ntransform = ((Shift(crpix1) & Shift(crpix2)) | transform | RotateCelestial2Native(lon, lat, 180) | sky2pix_proj) # standard sampling: u, v = _make_sampling_grid( npoints, tuple(bounding_box[k] for k in input_axes), crpix=[crpix1, crpix2] ) undist_x, undist_y = ntransform(u, v) # Double sampling to check if sampling is sufficient. ud, vd = _make_sampling_grid( 2 * npoints, tuple(bounding_box[k] for k in input_axes), crpix=[crpix1, crpix2] ) undist_xd, undist_yd = ntransform(ud, vd) # Determine approximate pixel scale in order to compute error threshold # from the specified pixel error. Computed at the center of the array. x0, y0 = ntransform(0, 0) xx, xy = ntransform(1, 0) yx, yy = ntransform(0, 1) pixarea = np.abs((xx - x0) * (yy - y0) - (xy - y0) * (yx - x0)) plate_scale = np.sqrt(pixarea) # The fitting section. if verbose: print("\nFitting forward SIP ...") fit_poly_x, fit_poly_y, max_resid = _fit_2D_poly( degree, max_pix_error, plate_scale, u, v, undist_x, undist_y, ud, vd, undist_xd, undist_yd, verbose=verbose ) # The following is necessary to put the fit into the SIP formalism. cdmat, sip_poly_x, sip_poly_y = _reform_poly_coefficients(fit_poly_x, fit_poly_y) # cdmat = np.array([[fit_poly_x.c1_0.value, fit_poly_x.c0_1.value], # [fit_poly_y.c1_0.value, fit_poly_y.c0_1.value]]) det = cdmat[0][0] * cdmat[1][1] - cdmat[0][1] * cdmat[1][0] U = ( cdmat[1][1] * undist_x - cdmat[0][1] * undist_y) / det V = (-cdmat[1][0] * undist_x + cdmat[0][0] * undist_y) / det detd = cdmat[0][0] * cdmat[1][1] - cdmat[0][1] * cdmat[1][0] Ud = ( cdmat[1][1] * undist_xd - cdmat[0][1] * undist_yd) / detd Vd = (-cdmat[1][0] * undist_xd + cdmat[0][0] * undist_yd) / detd if max_inv_pix_error: if verbose: print("\nFitting inverse SIP ...") fit_inv_poly_u, fit_inv_poly_v, max_inv_resid = _fit_2D_poly( inv_degree, max_inv_pix_error, 1, U, V, u-U, v-V, Ud, Vd, ud-Ud, vd-Vd, verbose=verbose ) # create header with WCS info: w = celestial_frame_to_wcs(frame.reference_frame, projection=projection) w.wcs.crval = [lon, lat] w.wcs.crpix = [crpix1 + 1, crpix2 + 1] w.wcs.pc = cdmat if nlon < nlat else cdmat[::-1] w.wcs.set() hdr = w.to_header(True) # data array info: hdr.insert(0, ('NAXIS', 2, 'number of array dimensions')) hdr.insert(1, (f'NAXIS{iax1:d}', int(xmax) + 1)) hdr.insert(2, (f'NAXIS{iax2:d}', int(ymax) + 1)) assert len(hdr['NAXIS*']) == 3 # list of celestial axes related keywords: cel_kwd = ['CRVAL', 'CTYPE', 'CUNIT'] # Add SIP info: if fit_poly_x.degree > 1: mat_kind = 'CD' # CDELT is not used with CD matrix (PC->CD later): del hdr['CDELT?'] hdr['CTYPE1'] = hdr['CTYPE1'].strip() + '-SIP' hdr['CTYPE2'] = hdr['CTYPE2'].strip() + '-SIP' hdr['A_ORDER'] = fit_poly_x.degree hdr['B_ORDER'] = fit_poly_x.degree _store_2D_coefficients(hdr, sip_poly_x, 'A') _store_2D_coefficients(hdr, sip_poly_y, 'B') hdr['sipmxerr'] = (max_resid, 'Max diff from GWCS (equiv pix).') if max_inv_pix_error: hdr['AP_ORDER'] = fit_inv_poly_u.degree hdr['BP_ORDER'] = fit_inv_poly_u.degree _store_2D_coefficients(hdr, fit_inv_poly_u, 'AP', keeplinear=True) _store_2D_coefficients(hdr, fit_inv_poly_v, 'BP', keeplinear=True) hdr['sipiverr'] = (max_inv_resid, 'Max diff for inverse (pixels)') else: if matrix_type.startswith('PC'): mat_kind = 'PC' cel_kwd.append('CDELT') if matrix_type == 'PC-CDELT1': cdelt = [1.0, 1.0] elif matrix_type == 'PC-SUM1': norm = np.sqrt(np.sum(w.wcs.pc**2)) cdelt = [norm, norm] elif matrix_type == 'PC-DET1': det_pc = np.linalg.det(w.wcs.pc) norm = np.sqrt(np.abs(det_pc)) cdelt = [norm, np.sign(det_pc) * norm] elif matrix_type == 'PC-SCALE': cdelt = proj_plane_pixel_scales(w) for i in range(1, 3): s = cdelt[i - 1] hdr[f'CDELT{i}'] = s for j in range(1, 3): pc_kwd = f'PC{i}_{j}' if pc_kwd in hdr: hdr[pc_kwd] = w.wcs.pc[i - 1, j - 1] / s else: mat_kind = 'CD' del hdr['CDELT?'] hdr['sipmxerr'] = (max_resid, 'Max diff from GWCS (equiv pix).') # Construct CD matrix while remapping input axes. # We do not update comments to typical comments for CD matrix elements # (such as 'partial of second axis coordinate w.r.t. y'), because # when input frame has number of axes > 2, then imaging # axes arbitrary. old_nlon, old_nlat = (1, 2) if nlon < nlat else (2, 1) # Remap input axes (CRPIX) and output axes-related parameters # (CRVAL, CUNIT, CTYPE, CD/PC). This has to be done in two steps to avoid # name conflicts (i.e., swapping CRPIX1<->CRPIX2). # remap input axes: axis_rename = {} if iax1 != 1: axis_rename['CRPIX1'] = f'CRPIX{iax1}' if iax2 != 2: axis_rename['CRPIX2'] = f'CRPIX{iax2}' # CP/PC matrix: axis_rename[f'PC{old_nlon}_1'] = f'{mat_kind}{nlon}_{iax1}' axis_rename[f'PC{old_nlon}_2'] = f'{mat_kind}{nlon}_{iax2}' axis_rename[f'PC{old_nlat}_1'] = f'{mat_kind}{nlat}_{iax1}' axis_rename[f'PC{old_nlat}_2'] = f'{mat_kind}{nlat}_{iax2}' # remap celestial axes keywords: for kwd in cel_kwd: for iold, inew in [(1, nlon), (2, nlat)]: if iold != inew: axis_rename[f'{kwd:s}{iold:d}'] = f'{kwd:s}{inew:d}' # construct new header cards with remapped axes: new_cards = [] for c in hdr.cards: if c[0] in axis_rename: c = fits.Card(keyword=axis_rename[c.keyword], value=c.value, comment=c.comment) new_cards.append(c) hdr = fits.Header(new_cards) hdr['WCSAXES'] = 2 hdr.insert('WCSAXES', ('WCSNAME', f'{self.output_frame.name}'), after=True) # for PC matrix formalism, set diagonal elements to 0 if necessary # (by default, in PC formalism, diagonal matrix elements by default # are 0): if mat_kind == 'PC': if nlon not in [iax1, iax2]: hdr.insert( f'{mat_kind}{nlon}_{iax2}', (f'{mat_kind}{nlon}_{nlon}', 0.0, 'Coordinate transformation matrix element') ) if nlat not in [iax1, iax2]: hdr.insert( f'{mat_kind}{nlat}_{iax2}', (f'{mat_kind}{nlat}_{nlat}', 0.0, 'Coordinate transformation matrix element') ) return hdr def _separable_groups(self, detect_celestial): """ This method finds sets (groups) of separable axes - axes that are dependent on other axes within a set/group but do not depend on axes from other groups. In other words, axes from different groups are separable. Parameters ---------- detect_celestial : bool If `True`, will return, as the third return value, the group of celestial axes separately from all other (groups of) axes. If no celestial frame is detected, then return value for the celestial axes group will be set to `None`. Returns ------- axes_groups : list of lists of ``_WorldAxisInfo`` Each inner list represents a group of non-separable (among themselves) axes and each axis in a group is independent of axes in *other* groups. Each axis in a group is represented through the `_WorldAxisInfo` class used to store relevant information about an axis. When ``detect_celestial`` is set to `True`, celestial axes group is not included in this list. world_axes : list of ``_WorldAxisInfo`` A flattened version of ``axes_groups``. Even though it is not difficult to flatten ``axes_groups``, this list is a by-product of other checks and returned here for efficiency. When ``detect_celestial`` is set to `True`, celestial axes group is not included in this list. celestial_group : list of ``_WorldAxisInfo`` A group of two celestial axes. This group is returned *only when* ``detect_celestial`` is set to `True`. """ def find_frame(axis_number): for frame in frames: if axis_number in frame.axes_order: return frame else: raise RuntimeError("Encountered an output axes that does not " "belong to any output coordinate frames.") # use correlation matrix to find separable axes: corr_mat = self.axis_correlation_matrix axes_sets = [set(np.flatnonzero(r)) for r in corr_mat.T] k = 0 while len(axes_sets) - 1 > k: for m in range(len(axes_sets) - 1, k, -1): if axes_sets[k].isdisjoint(axes_sets[m]): continue axes_sets[k] = axes_sets[k].union(axes_sets[m]) del axes_sets[m] k += 1 # create a mapping of output axes to input/image axes groups: mapping = {k: tuple(np.flatnonzero(r)) for k, r in enumerate(corr_mat)} axes_groups = [] world_axes = [] # flattened version of axes_groups input_axes = [] # all input axes if isinstance(self.output_frame, cf.CompositeFrame): frames = self.output_frame.frames else: frames = [self.output_frame] celestial_group = None # identify which separable group of axes belong for s in axes_sets: axis_info_group = [] # group of separable output axes info # Find the frame to which the first axis in the group belongs. # Most likely this frame will be the frame of all other axes in # this group; if not, we will update it later. s = sorted(s) frame = find_frame(s[0]) celestial = (detect_celestial and len(s) == 2 and len(frame.axes_order) == 2 and isinstance(frame, cf.CelestialFrame)) for axno in s: if axno not in frame.axes_order: frame = find_frame(axno) celestial = False # Celestial axes must belong to the same frame # index of the axis in this frame's fidx = frame.axes_order.index(axno) if hasattr(frame.unit[fidx], 'get_format_name'): cunit = frame.unit[fidx].get_format_name(u.format.Fits).upper() else: cunit = '' axis_info = _WorldAxisInfo( axis=axno, frame=frame, world_axis_order=self.output_frame.axes_order.index(axno), cunit=cunit, ctype=cf.get_ctype_from_ucd(self.world_axis_physical_types[axno]), input_axes=mapping[axno] ) axis_info_group.append(axis_info) input_axes.extend(mapping[axno]) world_axes.extend(axis_info_group) if celestial: celestial_group = axis_info_group else: axes_groups.append(axis_info_group) # sanity check: input_axes = set(sum((ax.input_axes for ax in world_axes), world_axes[0].input_axes.__class__())) n_inputs = len(input_axes) if (n_inputs != self.pixel_n_dim or max(input_axes) + 1 != n_inputs or min(input_axes) < 0): raise ValueError("Input axes indices are inconsistent with the " "forward transformation.") if detect_celestial: return axes_groups, world_axes, celestial_group else: return axes_groups, world_axes def to_fits_tab(self, bounding_box=None, bin_ext_name='WCS-TABLE', coord_col_name='coordinates', sampling=1): """ Construct a FITS WCS ``-TAB``-based approximation to the WCS in the form of a FITS header and a binary table extension. For the description of the FITS WCS ``-TAB`` convention, see "Representations of spectral coordinates in FITS" in `Greisen, E. W. et al. A&A 446 (2) 747-771 (2006) <https://doi.org/10.1051/0004-6361:20053818>`_ . Parameters ---------- bounding_box : tuple, optional Specifies the range of acceptable values for each input axis. The order of the axes is `~gwcs.coordinate_frames.CoordinateFrame.axes_order`. For two image axes ``bounding_box`` is of the form ``((xmin, xmax), (ymin, ymax))``. bin_ext_name : str, optional Extension name for the `~astropy.io.fits.BinTableHDU` HDU for those axes groups that will be converted using FITW WCS' ``-TAB`` algorith. Extension version will be determined automatically based on the number of separable group of axes. coord_col_name : str, optional Field name of the coordinate array in the structured array stored in `~astropy.io.fits.BinTableHDU` data. This corresponds to ``TTYPEi`` field in the FITS header of the binary table extension. sampling : float, tuple, optional The target "density" of grid nodes per pixel to be used when creating the coordinate array for the ``-TAB`` FITS WCS convention. It is equal to ``1/step`` where ``step`` is the distance between grid nodes in pixels. ``sampling`` can be specified as a single number to be used for all axes or as a `tuple` of numbers that specify the sampling for each image axis. Returns ------- hdr : `~astropy.io.fits.Header` Header with WCS-TAB information associated (to be used) with image data. bin_table_hdu : `~astropy.io.fits.BinTableHDU` Binary table extension containing the coordinate array. Raises ------ ValueError When ``bounding_box`` is not defined either through the input ``bounding_box`` parameter or this object's ``bounding_box`` property. ValueError When ``sampling`` is a `tuple` of length larger than 1 that does not match the number of image axes. RuntimeError If the number of image axes (``~gwcs.WCS.pixel_n_dim``) is larger than the number of world axes (``~gwcs.WCS.world_n_dim``). """ if bounding_box is None: if self.bounding_box is None: raise ValueError( "Need a valid bounding_box to compute the footprint." ) bounding_box = self.bounding_box else: # validate user-supplied bounding box: frames = self.available_frames transform_0 = self.get_transform(frames[0], frames[1]) Bbox.validate(transform_0, bounding_box) if self.forward_transform.n_inputs == 1: bounding_box = [bounding_box] if self.pixel_n_dim > self.world_n_dim: raise RuntimeError( "The case when the number of input axes is larger than the " "number of output axes is not supported." ) try: sampling = np.broadcast_to(sampling, (self.pixel_n_dim, )) except ValueError: raise ValueError("Number of sampling values either must be 1 " "or it must match the number of pixel axes.") _, world_axes = self._separable_groups(detect_celestial=False) hdr, bin_table_hdu = self._to_fits_tab( hdr=None, world_axes_group=world_axes, use_cd=False, bounding_box=bounding_box, bin_ext=bin_ext_name, coord_col_name=coord_col_name, sampling=sampling ) return hdr, bin_table_hdu def to_fits(self, bounding_box=None, max_pix_error=0.25, degree=None, max_inv_pix_error=0.25, inv_degree=None, npoints=32, crpix=None, projection='TAN', bin_ext_name='WCS-TABLE', coord_col_name='coordinates', sampling=1, verbose=False): """ Construct a FITS WCS ``-TAB``-based approximation to the WCS in the form of a FITS header and a binary table extension. For the description of the FITS WCS ``-TAB`` convention, see "Representations of spectral coordinates in FITS" in `Greisen, E. W. et al. A&A 446 (2) 747-771 (2006) <https://doi.org/10.1051/0004-6361:20053818>`_ . If WCS contains celestial frame, PC/CD formalism will be used for the celestial axes. .. note:: SIP distortion fitting requires that the WCS object has only two celestial axes. When WCS does not contain celestial axes, SIP fitting parameters (``max_pix_error``, ``degree``, ``max_inv_pix_error``, ``inv_degree``, and ``projection``) are ignored. When a WCS, in addition to celestial frame, contains other types of axes, SIP distortion fitting is disabled (ony linear terms are fitted for celestial frame). Parameters ---------- bounding_box : tuple, optional Specifies the range of acceptable values for each input axis. The order of the axes is `~gwcs.coordinate_frames.CoordinateFrame.axes_order`. For two image axes ``bounding_box`` is of the form ``((xmin, xmax), (ymin, ymax))``. max_pix_error : float, optional Maximum allowed error over the domain of the pixel array. This error is the equivalent pixel error that corresponds to the maximum error in the output coordinate resulting from the fit based on a nominal plate scale. degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_pixel_error`` is ignored. .. note:: When WCS object has When ``degree`` is `None` and the WCS object has max_inv_pix_error : float, optional Maximum allowed inverse error over the domain of the pixel array in pixel units. If None, no inverse is generated. inv_degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_inv_pixel_error`` is ignored. npoints : int, optional The number of points in each dimension to sample the bounding box for use in the SIP fit. Minimum number of points is 3. crpix : list of float, None, optional Coordinates (1-based) of the reference point for the new FITS WCS. When not provided, i.e., when set to `None` (default) the reference pixel will be chosen near the center of the bounding box for axes corresponding to the celestial frame. projection : str, `~astropy.modeling.projections.Pix2SkyProjection`, optional Projection to be used for the created FITS WCS. It can be specified as a string of three characters specifying a FITS projection code from Table 13 in `Representations of World Coordinates in FITS \ <https://doi.org/10.1051/0004-6361:20021326>`_ (Paper I), Greisen, E. W., and Calabretta, M. R., A & A, 395, 1061-1075, 2002. Alternatively, it can be an instance of one of the `astropy's Pix2Sky_* <https://docs.astropy.org/en/stable/modeling/\ reference_api.html#module-astropy.modeling.projections>`_ projection models inherited from :py:class:`~astropy.modeling.projections.Pix2SkyProjection`. bin_ext_name : str, optional Extension name for the `~astropy.io.fits.BinTableHDU` HDU for those axes groups that will be converted using FITW WCS' ``-TAB`` algorith. Extension version will be determined automatically based on the number of separable group of axes. coord_col_name : str, optional Field name of the coordinate array in the structured array stored in `~astropy.io.fits.BinTableHDU` data. This corresponds to ``TTYPEi`` field in the FITS header of the binary table extension. sampling : float, tuple, optional The target "density" of grid nodes per pixel to be used when creating the coordinate array for the ``-TAB`` FITS WCS convention. It is equal to ``1/step`` where ``step`` is the distance between grid nodes in pixels. ``sampling`` can be specified as a single number to be used for all axes or as a `tuple` of numbers that specify the sampling for each image axis. verbose : bool, optional Print progress of fits. Returns ------- hdr : `~astropy.io.fits.Header` Header with WCS-TAB information associated (to be used) with image data. hdulist : a list of `~astropy.io.fits.BinTableHDU` A Python list of binary table extensions containing the coordinate array for TAB extensions; one extension per separable axes group. Raises ------ ValueError When ``bounding_box`` is not defined either through the input ``bounding_box`` parameter or this object's ``bounding_box`` property. ValueError When ``sampling`` is a `tuple` of length larger than 1 that does not match the number of image axes. RuntimeError If the number of image axes (``~gwcs.WCS.pixel_n_dim``) is larger than the number of world axes (``~gwcs.WCS.world_n_dim``). """ if bounding_box is None: if self.bounding_box is None: raise ValueError( "Need a valid bounding_box to compute the footprint." ) bounding_box = self.bounding_box else: # validate user-supplied bounding box: frames = self.available_frames transform_0 = self.get_transform(frames[0], frames[1]) Bbox.validate(transform_0, bounding_box) if self.forward_transform.n_inputs == 1: bounding_box = [bounding_box] if self.pixel_n_dim > self.world_n_dim: raise RuntimeError( "The case when the number of input axes is larger than the " "number of output axes is not supported." ) try: sampling = np.broadcast_to(sampling, (self.pixel_n_dim, )) except ValueError: raise ValueError("Number of sampling values either must be 1 " "or it must match the number of pixel axes.") world_axes_groups, _, celestial_group = self._separable_groups( detect_celestial=True ) # Find celestial axes group and treat it separately from other axes: if celestial_group: # if world_axes_groups is empty, then we have only celestial axes # and so we can allow arbitrary degree for SIP. When there are # other axes types present, issue a warning and set 'degree' to 1 # because use of SIP when world_n_dim > 2 currently is not supported by # astropy.wcs.WCS - see https://github.com/astropy/astropy/pull/11452 if world_axes_groups and (degree is None or np.max(degree) != 2): if degree is not None: warnings.warn( "SIP distortion is not supported when the number\n" "of axes in WCS is larger than 2. Setting 'degree'\n" "to 1 and 'max_inv_pix_error' to None." ) degree = 1 max_inv_pix_error = None hdr = self._to_fits_sip( celestial_group=celestial_group, keep_axis_position=True, bounding_box=bounding_box, max_pix_error=max_pix_error, degree=degree, max_inv_pix_error=max_inv_pix_error, inv_degree=inv_degree, npoints=npoints, crpix=crpix, projection=projection, matrix_type='PC-CDELT1', verbose=verbose ) use_cd = 'A_ORDER' in hdr else: use_cd = False hdr = fits.Header() hdr['NAXIS'] = 0 hdr['WCSAXES'] = 0 # now handle non-celestial axes using -TAB convention for each # separable axes group: hdulist = [] for extver0, world_axes_group in enumerate(world_axes_groups): # For each subset of separable axes call _to_fits_tab to # convert that group to a single Bin TableHDU with a # coordinate array for this group of axes: hdr, bin_table_hdu = self._to_fits_tab( hdr=hdr, world_axes_group=world_axes_group, use_cd=use_cd, bounding_box=bounding_box, bin_ext=(bin_ext_name, extver0 + 1), coord_col_name=coord_col_name, sampling=sampling ) hdulist.append(bin_table_hdu) hdr.add_comment('FITS WCS created by approximating a gWCS') return hdr, hdulist def _to_fits_tab(self, hdr, world_axes_group, use_cd, bounding_box, bin_ext, coord_col_name, sampling): """ Construct a FITS WCS ``-TAB``-based approximation to the WCS in the form of a FITS header and a binary table extension. For the description of the FITS WCS ``-TAB`` convention, see "Representations of spectral coordinates in FITS" in `Greisen, E. W. et al. A&A 446 (2) 747-771 (2006) <https://doi.org/10.1051/0004-6361:20053818>`_ . Below we describe only parameters additional to the ones explained for `to_fits_tab`. .. warn:: For this helper function, parameters ``bounding_box`` and ``sampling`` (when provided as a tuple) are expected to have the same length as the number of input axes in the *full* WCS object. That is, the number of elements in ``bounding_box`` and ``sampling`` is not be affected by ``ignore_axes``. Other Parameters ---------------- hdr : astropy.io.fits.Header, None The first time this function is called, ``hdr`` should be set to `None` or be an empty :py:class:`~astropy.io.fits.Header` object. On subsequent calls, updated header from the previous iteration should be provided. world_axes_group : tuple of dict A list of world axes to represent through FITS' -TAB convention. This is a list of dictionaries with each dicti axes_mapping : dict A dictionary that maps output axis index to a tuple of input axis indices. In a typical scenario of two input image axes and two output celestial axes for a FITS-like WCS, this dictionary would look like ``{0: (0, 1), 1: (0, 1)}`` with the two non-separable input axes. fix_axes : dict A dictionary containing as keys image axes' indices to be fixed and as values - the values to which inputs should be kept fixed. For example, this dictionary may be used to indicate the celestial axes that should not be included into -TAB approximation because they will be approximated using -SIP. use_cd : bool When `True` - CD-matrix formalism will be used instead of the PC-matrix formalism. bin_ext : str, tuple of str and int Extension name and optionally version for the `~astropy.io.fits.BinTableHDU` HDU. When only a string extension name is provided, extension version will be set to 1. When ``bin_ext`` is a tuple, first element should be extension name and the second element is a positive integer extension version number. Returns ------- hdr : `~astropy.io.fits.Header` Header with WCS-TAB information associated (to be used) with image data. bin_table_hdu : `~astropy.io.fits.BinTableHDU` Binary table extension containing the coordinate array. Raises ------ ValueError When ``bounding_box`` is not defined either through the input ``bounding_box`` parameter or this object's ``bounding_box`` property. ValueError When ``sampling`` is a `tuple` of length larger than 1 that does not match the number of image axes. ValueError When extension version is smaller than 1. TypeError RuntimeError If the number of image axes (``~gwcs.WCS.pixel_n_dim``) is larger than the number of world axes (``~gwcs.WCS.world_n_dim``). """ if isinstance(bin_ext, str): bin_ext = (bin_ext, 1) if isinstance(bounding_box, Bbox): bounding_box = bounding_box.bounding_box(order='F') if isinstance(bounding_box, list): for index, bbox in enumerate(bounding_box): if isinstance(bbox, Bbox): bounding_box[index] = bbox.bounding_box(order='F') # identify input axes: input_axes = [] world_axes_idx = [] for ax in world_axes_group: world_axes_idx.append(ax.axis) input_axes.extend(ax.input_axes) input_axes = sorted(set(input_axes)) n_inputs = len(input_axes) n_outputs = len(world_axes_group) world_axes_idx.sort() # Create initial header and deal with non-degenerate axes if hdr is None: hdr = fits.Header() hdr['NAXIS'] = n_inputs, 'number of array dimensions' hdr['WCSAXES'] = n_outputs hdr.insert('WCSAXES', ('WCSNAME', f'{self.output_frame.name}'), after=True) else: hdr['NAXIS'] += n_inputs hdr['WCSAXES'] += n_outputs # see what axes have been already populated in the header: used_hdr_axes = [] for v in hdr['naxis*'].keys(): try: used_hdr_axes.append(int(v.split('NAXIS')[1]) - 1) except ValueError: continue degenerate_axis_start = max( self.pixel_n_dim + 1, max(used_hdr_axes) + 1 if used_hdr_axes else 1 ) # Deal with non-degenerate axes and add NAXISi to the header: offset = hdr.index('NAXIS') for iax in input_axes: iiax = int(np.searchsorted(used_hdr_axes, iax)) hdr.insert(iiax + offset + 1, (f'NAXIS{iax + 1:d}', int(max(bounding_box[iiax])) + 1)) # 1D grid coordinates: gcrds = [] cdelt = [] bb = [bounding_box[k] for k in input_axes] for (xmin, xmax), s in zip(bb, sampling): npix = max(2, 1 + int(np.ceil(abs((xmax - xmin) / s)))) gcrds.append(np.linspace(xmin, xmax, npix)) cdelt.append((npix - 1) / (xmax - xmin) if xmin != xmax else 1) # In the forward transformation, select only inputs and outputs # that we need given world_axes_group parameter: bb_center = np.mean(bounding_box, axis=1) fixi_dict = { k: bb_center[k] for k in set(range(self.pixel_n_dim)).difference(input_axes) } transform = _fix_transform_inputs(self.forward_transform, fixi_dict) transform = transform | Mapping(world_axes_idx, n_inputs=self.forward_transform.n_outputs) xyz = np.meshgrid(*gcrds[::-1], indexing='ij')[::-1] shape = xyz[0].shape xyz = [v.ravel() for v in xyz] coord = np.stack( transform(*xyz), axis=-1 ) coord = coord.reshape(shape + (len(world_axes_group), )) # create header with WCS info: if hdr is None: hdr = fits.Header() for m, axis_info in enumerate(world_axes_group): k = axis_info.axis widx = world_axes_idx.index(k) k1 = k + 1 ct = cf.get_ctype_from_ucd(self.world_axis_physical_types[k]) if len(ct) > 4: raise ValueError("Axis type name too long.") hdr[f'CTYPE{k1:d}'] = ct + (4 - len(ct)) * '-' + '-TAB' hdr[f'CUNIT{k1:d}'] = self.world_axis_units[k] hdr[f'PS{k1:d}_0'] = bin_ext[0] hdr[f'PV{k1:d}_1'] = bin_ext[1] hdr[f'PS{k1:d}_1'] = coord_col_name hdr[f'PV{k1:d}_3'] = widx + 1 hdr[f'CRVAL{k1:d}'] = 1 if widx < n_inputs: m1 = input_axes[widx] + 1 hdr[f'CRPIX{m1:d}'] = gcrds[widx][0] + 1 if use_cd: hdr[f'CD{k1:d}_{m1:d}'] = cdelt[widx] else: if k1 != m1: hdr[f'PC{k1:d}_{k1:d}'] = 0.0 hdr[f'PC{k1:d}_{m1:d}'] = 1.0 hdr[f'CDELT{k1:d}'] = cdelt[widx] else: m1 = degenerate_axis_start degenerate_axis_start += 1 hdr[f'CRPIX{m1:d}'] = 1 if use_cd: hdr[f'CD{k1:d}_{m1:d}'] = 1.0 else: if k1 != m1: hdr[f'PC{k1:d}_{k1:d}'] = 0.0 hdr[f'PC{k1:d}_{m1:d}'] = 1.0 hdr[f'CDELT{k1:d}'] = 1 # Uncomment 3 lines below to enable use of degenerate axes: # hdr['NAXIS'] = hdr['NAXIS'] + 1 # naxisi_max = max(int(k[5:]) for k in hdr['naxis*'] if k[5:].strip()) # hdr.insert(f'NAXIS{naxisi_max:d}', (f'NAXIS{m1:d}', 1), after=True) # NOTE: in this case make sure NAXIS=WCSAXES coord = coord[None, :] # structured array (data) for binary table HDU: arr = np.array( [(coord, )], dtype=[ (coord_col_name, np.float64, coord.shape), ] ) # create binary table HDU: bin_table_hdu = fits.BinTableHDU(arr, name=bin_ext[0], ver=bin_ext[1]) return hdr, bin_table_hdu def _calc_approx_inv(self, max_inv_pix_error=5, inv_degree=None, npoints=16): """ Compute polynomial fit for the inverse transformation to be used as initial aproximation/guess for the iterative solution. """ self._approx_inverse = None try: # try to use analytic inverse if available: self._approx_inverse = functools.partial(self.backward_transform, with_bounding_box=False) return except (NotImplementedError, KeyError): pass if not isinstance(self.output_frame, cf.CelestialFrame): # The _calc_approx_inv method only works with celestial frame transforms return # Determine reference points. if self.bounding_box is None: # A bounding_box is needed to proceed. return crpix = np.mean(self.bounding_box, axis=1) crval1, crval2 = self.forward_transform(*crpix) # Rotate to native system and deproject. Set center of the projection # transformation to the middle of the bounding box ("image") in order # to minimize projection effects across the entire image, # thus the initial shift. ntransform = ((Shift(crpix[0]) & Shift(crpix[1])) | self.forward_transform | RotateCelestial2Native(crval1, crval2, 180) | Sky2Pix_TAN()) # standard sampling: u, v = _make_sampling_grid(npoints, self.bounding_box, crpix=crpix) undist_x, undist_y = ntransform(u, v) # Double sampling to check if sampling is sufficient. ud, vd = _make_sampling_grid(2 * npoints, self.bounding_box, crpix=crpix) undist_xd, undist_yd = ntransform(ud, vd) fit_inv_poly_u, fit_inv_poly_v, max_inv_resid = _fit_2D_poly( None, max_inv_pix_error, 1, undist_x, undist_y, u, v, undist_xd, undist_yd, ud, vd, verbose=True ) self._approx_inverse = (RotateCelestial2Native(crval1, crval2, 180) | Sky2Pix_TAN() | Mapping((0, 1, 0, 1)) | (fit_inv_poly_u & fit_inv_poly_v) | (Shift(crpix[0]) & Shift(crpix[1])))
(forward_transform=None, input_frame='detector', output_frame=None, name='')
44,480
gwcs.wcs
__call__
Executes the forward transform. args : float or array-like Inputs in the input coordinate system, separate inputs for each dimension. with_units : bool If ``True`` returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object, by using the units of the output cooridnate frame. Optional, default=False. with_bounding_box : bool, optional If True(default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is np.nan).
def __call__(self, *args, **kwargs): """ Executes the forward transform. args : float or array-like Inputs in the input coordinate system, separate inputs for each dimension. with_units : bool If ``True`` returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object, by using the units of the output cooridnate frame. Optional, default=False. with_bounding_box : bool, optional If True(default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is np.nan). """ transform = self.forward_transform if transform is None: raise NotImplementedError("WCS.forward_transform is not implemented.") with_units = kwargs.pop("with_units", False) if 'with_bounding_box' not in kwargs: kwargs['with_bounding_box'] = True if 'fill_value' not in kwargs: kwargs['fill_value'] = np.nan if self.bounding_box is not None: # Currently compound models do not attempt to combine individual model # bounding boxes. Get the forward transform and assign the bounding_box to it # before evaluating it. The order Model.bounding_box is reversed. transform.bounding_box = self.bounding_box result = transform(*args, **kwargs) if with_units: if self.output_frame.naxes == 1: result = self.output_frame.coordinates(result) else: result = self.output_frame.coordinates(*result) return result
(self, *args, **kwargs)
44,481
gwcs.wcs
__init__
null
def __init__(self, forward_transform=None, input_frame='detector', output_frame=None, name=""): #self.low_level_wcs = self self._approx_inverse = None self._available_frames = [] self._pipeline = [] self._name = name self._initialize_wcs(forward_transform, input_frame, output_frame) self._pixel_shape = None pipe = [] for step in self._pipeline: if isinstance(step, Step): pipe.append(Step(step.frame, step.transform)) else: pipe.append(Step(*step)) self._pipeline = pipe
(self, forward_transform=None, input_frame='detector', output_frame=None, name='')
44,482
gwcs.wcs
__repr__
null
def __repr__(self): fmt = "<WCS(output_frame={0}, input_frame={1}, forward_transform={2})>".format( self.output_frame, self.input_frame, self.forward_transform) return fmt
(self)
44,483
gwcs.wcs
__str__
null
def __str__(self): from astropy.table import Table col1 = [step.frame for step in self._pipeline] col2 = [] for item in self._pipeline[: -1]: model = item.transform if model is None: col2.append(None) elif model.name is not None: col2.append(model.name) else: col2.append(model.__class__.__name__) col2.append(None) t = Table([col1, col2], names=['From', 'Transform']) return str(t)
(self)
44,484
gwcs.api
_add_units_input
null
def _add_units_input(self, arrays, transform, frame): if transform.uses_quantity: return tuple(u.Quantity(array, unit) for array, unit in zip(arrays, frame.unit)) return arrays
(self, arrays, transform, frame)
44,485
astropy.wcs.wcsapi.low_level_api
_as_mpl_axes
Compatibility hook for Matplotlib and WCSAxes. With this method, one can do:: from astropy.wcs import WCS import matplotlib.pyplot as plt wcs = WCS('filename.fits') fig = plt.figure() ax = fig.add_axes([0.15, 0.1, 0.8, 0.8], projection=wcs) ... and this will generate a plot with the correct WCS coordinates on the axes.
def _as_mpl_axes(self): """Compatibility hook for Matplotlib and WCSAxes. With this method, one can do:: from astropy.wcs import WCS import matplotlib.pyplot as plt wcs = WCS('filename.fits') fig = plt.figure() ax = fig.add_axes([0.15, 0.1, 0.8, 0.8], projection=wcs) ... and this will generate a plot with the correct WCS coordinates on the axes. """ from astropy.visualization.wcsaxes import WCSAxes return WCSAxes, {"wcs": self}
(self)
44,486
gwcs.wcs
_calc_approx_inv
Compute polynomial fit for the inverse transformation to be used as initial aproximation/guess for the iterative solution.
def _calc_approx_inv(self, max_inv_pix_error=5, inv_degree=None, npoints=16): """ Compute polynomial fit for the inverse transformation to be used as initial aproximation/guess for the iterative solution. """ self._approx_inverse = None try: # try to use analytic inverse if available: self._approx_inverse = functools.partial(self.backward_transform, with_bounding_box=False) return except (NotImplementedError, KeyError): pass if not isinstance(self.output_frame, cf.CelestialFrame): # The _calc_approx_inv method only works with celestial frame transforms return # Determine reference points. if self.bounding_box is None: # A bounding_box is needed to proceed. return crpix = np.mean(self.bounding_box, axis=1) crval1, crval2 = self.forward_transform(*crpix) # Rotate to native system and deproject. Set center of the projection # transformation to the middle of the bounding box ("image") in order # to minimize projection effects across the entire image, # thus the initial shift. ntransform = ((Shift(crpix[0]) & Shift(crpix[1])) | self.forward_transform | RotateCelestial2Native(crval1, crval2, 180) | Sky2Pix_TAN()) # standard sampling: u, v = _make_sampling_grid(npoints, self.bounding_box, crpix=crpix) undist_x, undist_y = ntransform(u, v) # Double sampling to check if sampling is sufficient. ud, vd = _make_sampling_grid(2 * npoints, self.bounding_box, crpix=crpix) undist_xd, undist_yd = ntransform(ud, vd) fit_inv_poly_u, fit_inv_poly_v, max_inv_resid = _fit_2D_poly( None, max_inv_pix_error, 1, undist_x, undist_y, u, v, undist_xd, undist_yd, ud, vd, verbose=True ) self._approx_inverse = (RotateCelestial2Native(crval1, crval2, 180) | Sky2Pix_TAN() | Mapping((0, 1, 0, 1)) | (fit_inv_poly_u & fit_inv_poly_v) | (Shift(crpix[0]) & Shift(crpix[1])))
(self, max_inv_pix_error=5, inv_degree=None, npoints=16)
44,487
gwcs.wcs
_get_axes_indices
null
def _get_axes_indices(self): try: axes_ind = np.argsort(self.input_frame.axes_order) except AttributeError: # the case of a frame being a string axes_ind = np.arange(self.forward_transform.n_inputs) return axes_ind
(self)
44,488
gwcs.wcs
_get_frame_index
Return the index in the pipeline where this frame is locate.
def _get_frame_index(self, frame): """ Return the index in the pipeline where this frame is locate. """ if isinstance(frame, cf.CoordinateFrame): frame = frame.name frame_names = [step.frame if isinstance(step.frame, str) else step.frame.name for step in self._pipeline] try: return frame_names.index(frame) except ValueError as e: raise CoordinateFrameError(f"Frame {frame} is not in the available frames") from e
(self, frame)
44,489
gwcs.wcs
_get_frame_name
Return the name of the frame and a ``CoordinateFrame`` object. Parameters ---------- frame : str, `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame. Returns ------- name : str The name of the frame frame_obj : `~gwcs.coordinate_frames.CoordinateFrame` Frame instance or None (if `frame` is str)
def _get_frame_name(self, frame): """ Return the name of the frame and a ``CoordinateFrame`` object. Parameters ---------- frame : str, `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame. Returns ------- name : str The name of the frame frame_obj : `~gwcs.coordinate_frames.CoordinateFrame` Frame instance or None (if `frame` is str) """ if isinstance(frame, str): name = frame frame_obj = None else: name = frame.name frame_obj = frame return name, frame_obj
(self, frame)
44,490
gwcs.wcs
_initialize_wcs
null
def _initialize_wcs(self, forward_transform, input_frame, output_frame): if forward_transform is not None: if isinstance(forward_transform, Model): if output_frame is None: raise CoordinateFrameError("An output_frame must be specified " "if forward_transform is a model.") _input_frame, inp_frame_obj = self._get_frame_name(input_frame) _output_frame, outp_frame_obj = self._get_frame_name(output_frame) super(WCS, self).__setattr__(_input_frame, inp_frame_obj) super(WCS, self).__setattr__(_output_frame, outp_frame_obj) self._pipeline = [(input_frame, forward_transform.copy()), (output_frame, None)] elif isinstance(forward_transform, list): for item in forward_transform: if isinstance(item, Step): name, frame_obj = self._get_frame_name(item.frame) else: name, frame_obj = self._get_frame_name(item[0]) super(WCS, self).__setattr__(name, frame_obj) #self._pipeline.append((name, item[1])) self._pipeline = forward_transform else: raise TypeError("Expected forward_transform to be a model or a " "(frame, transform) list, got {0}".format( type(forward_transform))) else: # Initialize a WCS without a forward_transform - allows building a WCS programmatically. if output_frame is None: raise CoordinateFrameError("An output_frame must be specified " "if forward_transform is None.") _input_frame, inp_frame_obj = self._get_frame_name(input_frame) _output_frame, outp_frame_obj = self._get_frame_name(output_frame) super(WCS, self).__setattr__(_input_frame, inp_frame_obj) super(WCS, self).__setattr__(_output_frame, outp_frame_obj) self._pipeline = [(_input_frame, None), (_output_frame, None)]
(self, forward_transform, input_frame, output_frame)
44,491
gwcs.api
_remove_quantity_output
null
def _remove_quantity_output(self, result, frame): if self.forward_transform.uses_quantity: if self.output_frame.naxes == 1: result = [result] result = tuple(r.to_value(unit) for r, unit in zip(result, frame.unit)) # If we only have one output axes, we shouldn't return a tuple. if self.output_frame.naxes == 1 and isinstance(result, tuple): return result[0] return result
(self, result, frame)
44,492
gwcs.api
_sanitize_pixel_inputs
null
def _sanitize_pixel_inputs(self, *pixel_arrays): pixels = [] if self.forward_transform.uses_quantity: for i, pixel in enumerate(pixel_arrays): if not isinstance(pixel, u.Quantity): pixel = u.Quantity(value=pixel, unit=self.input_frame.unit[i]) pixels.append(pixel) else: for i, pixel in enumerate(pixel_arrays): if isinstance(pixel, u.Quantity): if pixel.unit != self.input_frame.unit[i]: raise ValueError('Quantity input does not match the ' 'input_frame unit.') pixel = pixel.value pixels.append(pixel) return pixels
(self, *pixel_arrays)
44,493
gwcs.wcs
_separable_groups
This method finds sets (groups) of separable axes - axes that are dependent on other axes within a set/group but do not depend on axes from other groups. In other words, axes from different groups are separable. Parameters ---------- detect_celestial : bool If `True`, will return, as the third return value, the group of celestial axes separately from all other (groups of) axes. If no celestial frame is detected, then return value for the celestial axes group will be set to `None`. Returns ------- axes_groups : list of lists of ``_WorldAxisInfo`` Each inner list represents a group of non-separable (among themselves) axes and each axis in a group is independent of axes in *other* groups. Each axis in a group is represented through the `_WorldAxisInfo` class used to store relevant information about an axis. When ``detect_celestial`` is set to `True`, celestial axes group is not included in this list. world_axes : list of ``_WorldAxisInfo`` A flattened version of ``axes_groups``. Even though it is not difficult to flatten ``axes_groups``, this list is a by-product of other checks and returned here for efficiency. When ``detect_celestial`` is set to `True`, celestial axes group is not included in this list. celestial_group : list of ``_WorldAxisInfo`` A group of two celestial axes. This group is returned *only when* ``detect_celestial`` is set to `True`.
def _separable_groups(self, detect_celestial): """ This method finds sets (groups) of separable axes - axes that are dependent on other axes within a set/group but do not depend on axes from other groups. In other words, axes from different groups are separable. Parameters ---------- detect_celestial : bool If `True`, will return, as the third return value, the group of celestial axes separately from all other (groups of) axes. If no celestial frame is detected, then return value for the celestial axes group will be set to `None`. Returns ------- axes_groups : list of lists of ``_WorldAxisInfo`` Each inner list represents a group of non-separable (among themselves) axes and each axis in a group is independent of axes in *other* groups. Each axis in a group is represented through the `_WorldAxisInfo` class used to store relevant information about an axis. When ``detect_celestial`` is set to `True`, celestial axes group is not included in this list. world_axes : list of ``_WorldAxisInfo`` A flattened version of ``axes_groups``. Even though it is not difficult to flatten ``axes_groups``, this list is a by-product of other checks and returned here for efficiency. When ``detect_celestial`` is set to `True`, celestial axes group is not included in this list. celestial_group : list of ``_WorldAxisInfo`` A group of two celestial axes. This group is returned *only when* ``detect_celestial`` is set to `True`. """ def find_frame(axis_number): for frame in frames: if axis_number in frame.axes_order: return frame else: raise RuntimeError("Encountered an output axes that does not " "belong to any output coordinate frames.") # use correlation matrix to find separable axes: corr_mat = self.axis_correlation_matrix axes_sets = [set(np.flatnonzero(r)) for r in corr_mat.T] k = 0 while len(axes_sets) - 1 > k: for m in range(len(axes_sets) - 1, k, -1): if axes_sets[k].isdisjoint(axes_sets[m]): continue axes_sets[k] = axes_sets[k].union(axes_sets[m]) del axes_sets[m] k += 1 # create a mapping of output axes to input/image axes groups: mapping = {k: tuple(np.flatnonzero(r)) for k, r in enumerate(corr_mat)} axes_groups = [] world_axes = [] # flattened version of axes_groups input_axes = [] # all input axes if isinstance(self.output_frame, cf.CompositeFrame): frames = self.output_frame.frames else: frames = [self.output_frame] celestial_group = None # identify which separable group of axes belong for s in axes_sets: axis_info_group = [] # group of separable output axes info # Find the frame to which the first axis in the group belongs. # Most likely this frame will be the frame of all other axes in # this group; if not, we will update it later. s = sorted(s) frame = find_frame(s[0]) celestial = (detect_celestial and len(s) == 2 and len(frame.axes_order) == 2 and isinstance(frame, cf.CelestialFrame)) for axno in s: if axno not in frame.axes_order: frame = find_frame(axno) celestial = False # Celestial axes must belong to the same frame # index of the axis in this frame's fidx = frame.axes_order.index(axno) if hasattr(frame.unit[fidx], 'get_format_name'): cunit = frame.unit[fidx].get_format_name(u.format.Fits).upper() else: cunit = '' axis_info = _WorldAxisInfo( axis=axno, frame=frame, world_axis_order=self.output_frame.axes_order.index(axno), cunit=cunit, ctype=cf.get_ctype_from_ucd(self.world_axis_physical_types[axno]), input_axes=mapping[axno] ) axis_info_group.append(axis_info) input_axes.extend(mapping[axno]) world_axes.extend(axis_info_group) if celestial: celestial_group = axis_info_group else: axes_groups.append(axis_info_group) # sanity check: input_axes = set(sum((ax.input_axes for ax in world_axes), world_axes[0].input_axes.__class__())) n_inputs = len(input_axes) if (n_inputs != self.pixel_n_dim or max(input_axes) + 1 != n_inputs or min(input_axes) < 0): raise ValueError("Input axes indices are inconsistent with the " "forward transformation.") if detect_celestial: return axes_groups, world_axes, celestial_group else: return axes_groups, world_axes
(self, detect_celestial)
44,494
gwcs.wcs
_to_fits_sip
Construct a SIP-based approximation to the WCS for the axes corresponding to the `~gwcs.coordinate_frames.CelestialFrame` in the form of a FITS header. The default mode in using this attempts to achieve roughly 0.25 pixel accuracy over the whole image. Below we describe only parameters additional to the ones explained for `to_fits_sip`. Other Parameters ---------------- frame : gwcs.coordinate_frames.CelestialFrame A celestial frame. celestial_group : list of ``_WorldAxisInfo`` A group of two celestial axes to be represented using standard image FITS WCS and maybe ``-SIP`` polynomials. keep_axis_position : bool This parameter controls whether to keep/preserve output axes indices in this WCS object when creating FITS WCS and create a FITS header with ``CTYPE`` axes indices preserved from the ``frame`` object or whether to reset the indices of output celestial axes to 1 and 2 with ``CTYPE1``, ``CTYPE2``. Default is `False`. .. warning:: Returned header will have both ``NAXIS`` and ``WCSAXES`` set to 2. If ``max(axes_mapping) > 2`` this will lead to an invalid WCS. It is caller's responsibility to adjust NAXIS to a valid value. .. note:: The ``lon``/``lat`` order is still preserved regardless of this setting. matrix_type : {'CD', 'PC-CDELT1', 'PC-SUM1', 'PC-DET1', 'PC-SCALE'} Specifies formalism (``PC`` or ``CD``) to be used for the linear transformation matrix and normalization for the ``PC`` matrix *when non-linear polynomial terms are not required to achieve requested accuracy*. .. note:: ``CD`` matrix is always used when requested SIP approximation accuracy requires non-linear terms (when ``CTYPE`` ends in ``-SIP``). This parameter is ignored when non-linear polynomial terms are used. - ``'CD'``: use ``CD`` matrix; - ``'PC-CDELT1'``: set ``PC=CD`` and ``CDELTi=1``. This is the behavior of `~astropy.wcs.WCS.to_header` method; - ``'PC-SUM1'``: normalize ``PC`` matrix such that sum of its squared elements is 1: :math:`\Sigma PC_{ij}^2=1`; - ``'PC-DET1'``: normalize ``PC`` matrix such that :math:`|\det(PC)|=1`; - ``'PC-SCALE'``: normalize ``PC`` matrix such that ``CDELTi`` are estimates of the linear pixel scales. Returns ------- FITS header with all SIP WCS keywords Raises ------ ValueError If the WCS is not at least 2D, an exception will be raised. If the specified accuracy (both forward and inverse, both rms and maximum) is not achieved an exception will be raised.
def _to_fits_sip(self, celestial_group, keep_axis_position, bounding_box, max_pix_error, degree, max_inv_pix_error, inv_degree, npoints, crpix, projection, matrix_type, verbose): r""" Construct a SIP-based approximation to the WCS for the axes corresponding to the `~gwcs.coordinate_frames.CelestialFrame` in the form of a FITS header. The default mode in using this attempts to achieve roughly 0.25 pixel accuracy over the whole image. Below we describe only parameters additional to the ones explained for `to_fits_sip`. Other Parameters ---------------- frame : gwcs.coordinate_frames.CelestialFrame A celestial frame. celestial_group : list of ``_WorldAxisInfo`` A group of two celestial axes to be represented using standard image FITS WCS and maybe ``-SIP`` polynomials. keep_axis_position : bool This parameter controls whether to keep/preserve output axes indices in this WCS object when creating FITS WCS and create a FITS header with ``CTYPE`` axes indices preserved from the ``frame`` object or whether to reset the indices of output celestial axes to 1 and 2 with ``CTYPE1``, ``CTYPE2``. Default is `False`. .. warning:: Returned header will have both ``NAXIS`` and ``WCSAXES`` set to 2. If ``max(axes_mapping) > 2`` this will lead to an invalid WCS. It is caller's responsibility to adjust NAXIS to a valid value. .. note:: The ``lon``/``lat`` order is still preserved regardless of this setting. matrix_type : {'CD', 'PC-CDELT1', 'PC-SUM1', 'PC-DET1', 'PC-SCALE'} Specifies formalism (``PC`` or ``CD``) to be used for the linear transformation matrix and normalization for the ``PC`` matrix *when non-linear polynomial terms are not required to achieve requested accuracy*. .. note:: ``CD`` matrix is always used when requested SIP approximation accuracy requires non-linear terms (when ``CTYPE`` ends in ``-SIP``). This parameter is ignored when non-linear polynomial terms are used. - ``'CD'``: use ``CD`` matrix; - ``'PC-CDELT1'``: set ``PC=CD`` and ``CDELTi=1``. This is the behavior of `~astropy.wcs.WCS.to_header` method; - ``'PC-SUM1'``: normalize ``PC`` matrix such that sum of its squared elements is 1: :math:`\Sigma PC_{ij}^2=1`; - ``'PC-DET1'``: normalize ``PC`` matrix such that :math:`|\det(PC)|=1`; - ``'PC-SCALE'``: normalize ``PC`` matrix such that ``CDELTi`` are estimates of the linear pixel scales. Returns ------- FITS header with all SIP WCS keywords Raises ------ ValueError If the WCS is not at least 2D, an exception will be raised. If the specified accuracy (both forward and inverse, both rms and maximum) is not achieved an exception will be raised. """ if isinstance(matrix_type, str): matrix_type = matrix_type.upper() if matrix_type not in ['CD', 'PC-CDELT1', 'PC-SUM1', 'PC-DET1', 'PC-SCALE']: raise ValueError(f"Unsupported 'matrix_type' value: {repr(matrix_type)}.") if npoints < 8: raise ValueError("Number of sampling points is too small. 'npoints' must be >= 8.") if isinstance(projection, str): projection = projection.upper() try: sky2pix_proj = getattr(projections, f'Sky2Pix_{projection}')(name=projection) except AttributeError: raise ValueError("Unsupported FITS WCS sky projection: {projection}") elif isinstance(projection, projections.Sky2PixProjection): sky2pix_proj = projection projection = projection.name if not projection or not isinstance(projection, str) or len(projection) != 3: raise ValueError("Unsupported FITS WCS sky projection: {sky2pix_proj}") try: getattr(projections, f'Sky2Pix_{projection}')() except AttributeError: raise ValueError("Unsupported FITS WCS sky projection: {projection}") else: raise TypeError( "'projection' must be either a FITS WCS string projection code " "or an instance of astropy.modeling.projections.Pix2SkyProjection.") frame = celestial_group[0].frame lon_axis = frame.axes_order[0] lat_axis = frame.axes_order[1] # identify input axes: input_axes = [] for wax in celestial_group: input_axes.extend(wax.input_axes) input_axes = sorted(set(input_axes)) if len(input_axes) != 2: raise ValueError("Only CelestialFrame that correspond to two " "input axes are supported.") # Axis number for FITS axes. # iax? - image axes; nlon, nlat - celestial axes: if keep_axis_position: nlon = lon_axis + 1 nlat = lat_axis + 1 iax1, iax2 = (i + 1 for i in input_axes) else: nlon, nlat = (1, 2) if lon_axis < lat_axis else (2, 1) iax1 = 1 iax2 = 2 # Determine reference points. if bounding_box is None and self.bounding_box is None: raise ValueError("A bounding_box is needed to proceed.") if bounding_box is None: bounding_box = self.bounding_box bb_center = np.mean(bounding_box, axis=1) fixi_dict = { k: bb_center[k] for k in set(range(self.pixel_n_dim)).difference(input_axes) } # transform = fix_inputs(self.forward_transform, fixi_dict) # This is a workaround to the bug in https://github.com/astropy/astropy/issues/11360 # Once that bug is fixed, the code below can be replaced with fix_inputs # statement commented out immediately above. transform = _fix_transform_inputs(self.forward_transform, fixi_dict) transform = transform | Mapping((lon_axis, lat_axis), n_inputs=self.forward_transform.n_outputs) (xmin, xmax) = bounding_box[input_axes[0]] (ymin, ymax) = bounding_box[input_axes[1]] # 0-based crpix: if crpix is None: crpix1 = round(bb_center[input_axes[0]], 1) crpix2 = round(bb_center[input_axes[1]], 1) else: crpix1 = crpix[0] - 1 crpix2 = crpix[1] - 1 # check that the bounding box has some reasonable size: if (xmax - xmin) < 1 or (ymax - ymin) < 1: raise ValueError("Bounding box is too small for fitting a SIP polynomial") lon, lat = transform(crpix1, crpix2) # Now rotate to native system and deproject. Recall that transform # expects pixels in the original coordinate system, but the SIP # transform is relative to crpix coordinates, thus the initial shift. ntransform = ((Shift(crpix1) & Shift(crpix2)) | transform | RotateCelestial2Native(lon, lat, 180) | sky2pix_proj) # standard sampling: u, v = _make_sampling_grid( npoints, tuple(bounding_box[k] for k in input_axes), crpix=[crpix1, crpix2] ) undist_x, undist_y = ntransform(u, v) # Double sampling to check if sampling is sufficient. ud, vd = _make_sampling_grid( 2 * npoints, tuple(bounding_box[k] for k in input_axes), crpix=[crpix1, crpix2] ) undist_xd, undist_yd = ntransform(ud, vd) # Determine approximate pixel scale in order to compute error threshold # from the specified pixel error. Computed at the center of the array. x0, y0 = ntransform(0, 0) xx, xy = ntransform(1, 0) yx, yy = ntransform(0, 1) pixarea = np.abs((xx - x0) * (yy - y0) - (xy - y0) * (yx - x0)) plate_scale = np.sqrt(pixarea) # The fitting section. if verbose: print("\nFitting forward SIP ...") fit_poly_x, fit_poly_y, max_resid = _fit_2D_poly( degree, max_pix_error, plate_scale, u, v, undist_x, undist_y, ud, vd, undist_xd, undist_yd, verbose=verbose ) # The following is necessary to put the fit into the SIP formalism. cdmat, sip_poly_x, sip_poly_y = _reform_poly_coefficients(fit_poly_x, fit_poly_y) # cdmat = np.array([[fit_poly_x.c1_0.value, fit_poly_x.c0_1.value], # [fit_poly_y.c1_0.value, fit_poly_y.c0_1.value]]) det = cdmat[0][0] * cdmat[1][1] - cdmat[0][1] * cdmat[1][0] U = ( cdmat[1][1] * undist_x - cdmat[0][1] * undist_y) / det V = (-cdmat[1][0] * undist_x + cdmat[0][0] * undist_y) / det detd = cdmat[0][0] * cdmat[1][1] - cdmat[0][1] * cdmat[1][0] Ud = ( cdmat[1][1] * undist_xd - cdmat[0][1] * undist_yd) / detd Vd = (-cdmat[1][0] * undist_xd + cdmat[0][0] * undist_yd) / detd if max_inv_pix_error: if verbose: print("\nFitting inverse SIP ...") fit_inv_poly_u, fit_inv_poly_v, max_inv_resid = _fit_2D_poly( inv_degree, max_inv_pix_error, 1, U, V, u-U, v-V, Ud, Vd, ud-Ud, vd-Vd, verbose=verbose ) # create header with WCS info: w = celestial_frame_to_wcs(frame.reference_frame, projection=projection) w.wcs.crval = [lon, lat] w.wcs.crpix = [crpix1 + 1, crpix2 + 1] w.wcs.pc = cdmat if nlon < nlat else cdmat[::-1] w.wcs.set() hdr = w.to_header(True) # data array info: hdr.insert(0, ('NAXIS', 2, 'number of array dimensions')) hdr.insert(1, (f'NAXIS{iax1:d}', int(xmax) + 1)) hdr.insert(2, (f'NAXIS{iax2:d}', int(ymax) + 1)) assert len(hdr['NAXIS*']) == 3 # list of celestial axes related keywords: cel_kwd = ['CRVAL', 'CTYPE', 'CUNIT'] # Add SIP info: if fit_poly_x.degree > 1: mat_kind = 'CD' # CDELT is not used with CD matrix (PC->CD later): del hdr['CDELT?'] hdr['CTYPE1'] = hdr['CTYPE1'].strip() + '-SIP' hdr['CTYPE2'] = hdr['CTYPE2'].strip() + '-SIP' hdr['A_ORDER'] = fit_poly_x.degree hdr['B_ORDER'] = fit_poly_x.degree _store_2D_coefficients(hdr, sip_poly_x, 'A') _store_2D_coefficients(hdr, sip_poly_y, 'B') hdr['sipmxerr'] = (max_resid, 'Max diff from GWCS (equiv pix).') if max_inv_pix_error: hdr['AP_ORDER'] = fit_inv_poly_u.degree hdr['BP_ORDER'] = fit_inv_poly_u.degree _store_2D_coefficients(hdr, fit_inv_poly_u, 'AP', keeplinear=True) _store_2D_coefficients(hdr, fit_inv_poly_v, 'BP', keeplinear=True) hdr['sipiverr'] = (max_inv_resid, 'Max diff for inverse (pixels)') else: if matrix_type.startswith('PC'): mat_kind = 'PC' cel_kwd.append('CDELT') if matrix_type == 'PC-CDELT1': cdelt = [1.0, 1.0] elif matrix_type == 'PC-SUM1': norm = np.sqrt(np.sum(w.wcs.pc**2)) cdelt = [norm, norm] elif matrix_type == 'PC-DET1': det_pc = np.linalg.det(w.wcs.pc) norm = np.sqrt(np.abs(det_pc)) cdelt = [norm, np.sign(det_pc) * norm] elif matrix_type == 'PC-SCALE': cdelt = proj_plane_pixel_scales(w) for i in range(1, 3): s = cdelt[i - 1] hdr[f'CDELT{i}'] = s for j in range(1, 3): pc_kwd = f'PC{i}_{j}' if pc_kwd in hdr: hdr[pc_kwd] = w.wcs.pc[i - 1, j - 1] / s else: mat_kind = 'CD' del hdr['CDELT?'] hdr['sipmxerr'] = (max_resid, 'Max diff from GWCS (equiv pix).') # Construct CD matrix while remapping input axes. # We do not update comments to typical comments for CD matrix elements # (such as 'partial of second axis coordinate w.r.t. y'), because # when input frame has number of axes > 2, then imaging # axes arbitrary. old_nlon, old_nlat = (1, 2) if nlon < nlat else (2, 1) # Remap input axes (CRPIX) and output axes-related parameters # (CRVAL, CUNIT, CTYPE, CD/PC). This has to be done in two steps to avoid # name conflicts (i.e., swapping CRPIX1<->CRPIX2). # remap input axes: axis_rename = {} if iax1 != 1: axis_rename['CRPIX1'] = f'CRPIX{iax1}' if iax2 != 2: axis_rename['CRPIX2'] = f'CRPIX{iax2}' # CP/PC matrix: axis_rename[f'PC{old_nlon}_1'] = f'{mat_kind}{nlon}_{iax1}' axis_rename[f'PC{old_nlon}_2'] = f'{mat_kind}{nlon}_{iax2}' axis_rename[f'PC{old_nlat}_1'] = f'{mat_kind}{nlat}_{iax1}' axis_rename[f'PC{old_nlat}_2'] = f'{mat_kind}{nlat}_{iax2}' # remap celestial axes keywords: for kwd in cel_kwd: for iold, inew in [(1, nlon), (2, nlat)]: if iold != inew: axis_rename[f'{kwd:s}{iold:d}'] = f'{kwd:s}{inew:d}' # construct new header cards with remapped axes: new_cards = [] for c in hdr.cards: if c[0] in axis_rename: c = fits.Card(keyword=axis_rename[c.keyword], value=c.value, comment=c.comment) new_cards.append(c) hdr = fits.Header(new_cards) hdr['WCSAXES'] = 2 hdr.insert('WCSAXES', ('WCSNAME', f'{self.output_frame.name}'), after=True) # for PC matrix formalism, set diagonal elements to 0 if necessary # (by default, in PC formalism, diagonal matrix elements by default # are 0): if mat_kind == 'PC': if nlon not in [iax1, iax2]: hdr.insert( f'{mat_kind}{nlon}_{iax2}', (f'{mat_kind}{nlon}_{nlon}', 0.0, 'Coordinate transformation matrix element') ) if nlat not in [iax1, iax2]: hdr.insert( f'{mat_kind}{nlat}_{iax2}', (f'{mat_kind}{nlat}_{nlat}', 0.0, 'Coordinate transformation matrix element') ) return hdr
(self, celestial_group, keep_axis_position, bounding_box, max_pix_error, degree, max_inv_pix_error, inv_degree, npoints, crpix, projection, matrix_type, verbose)
44,495
gwcs.wcs
_to_fits_tab
Construct a FITS WCS ``-TAB``-based approximation to the WCS in the form of a FITS header and a binary table extension. For the description of the FITS WCS ``-TAB`` convention, see "Representations of spectral coordinates in FITS" in `Greisen, E. W. et al. A&A 446 (2) 747-771 (2006) <https://doi.org/10.1051/0004-6361:20053818>`_ . Below we describe only parameters additional to the ones explained for `to_fits_tab`. .. warn:: For this helper function, parameters ``bounding_box`` and ``sampling`` (when provided as a tuple) are expected to have the same length as the number of input axes in the *full* WCS object. That is, the number of elements in ``bounding_box`` and ``sampling`` is not be affected by ``ignore_axes``. Other Parameters ---------------- hdr : astropy.io.fits.Header, None The first time this function is called, ``hdr`` should be set to `None` or be an empty :py:class:`~astropy.io.fits.Header` object. On subsequent calls, updated header from the previous iteration should be provided. world_axes_group : tuple of dict A list of world axes to represent through FITS' -TAB convention. This is a list of dictionaries with each dicti axes_mapping : dict A dictionary that maps output axis index to a tuple of input axis indices. In a typical scenario of two input image axes and two output celestial axes for a FITS-like WCS, this dictionary would look like ``{0: (0, 1), 1: (0, 1)}`` with the two non-separable input axes. fix_axes : dict A dictionary containing as keys image axes' indices to be fixed and as values - the values to which inputs should be kept fixed. For example, this dictionary may be used to indicate the celestial axes that should not be included into -TAB approximation because they will be approximated using -SIP. use_cd : bool When `True` - CD-matrix formalism will be used instead of the PC-matrix formalism. bin_ext : str, tuple of str and int Extension name and optionally version for the `~astropy.io.fits.BinTableHDU` HDU. When only a string extension name is provided, extension version will be set to 1. When ``bin_ext`` is a tuple, first element should be extension name and the second element is a positive integer extension version number. Returns ------- hdr : `~astropy.io.fits.Header` Header with WCS-TAB information associated (to be used) with image data. bin_table_hdu : `~astropy.io.fits.BinTableHDU` Binary table extension containing the coordinate array. Raises ------ ValueError When ``bounding_box`` is not defined either through the input ``bounding_box`` parameter or this object's ``bounding_box`` property. ValueError When ``sampling`` is a `tuple` of length larger than 1 that does not match the number of image axes. ValueError When extension version is smaller than 1. TypeError RuntimeError If the number of image axes (``~gwcs.WCS.pixel_n_dim``) is larger than the number of world axes (``~gwcs.WCS.world_n_dim``).
def _to_fits_tab(self, hdr, world_axes_group, use_cd, bounding_box, bin_ext, coord_col_name, sampling): """ Construct a FITS WCS ``-TAB``-based approximation to the WCS in the form of a FITS header and a binary table extension. For the description of the FITS WCS ``-TAB`` convention, see "Representations of spectral coordinates in FITS" in `Greisen, E. W. et al. A&A 446 (2) 747-771 (2006) <https://doi.org/10.1051/0004-6361:20053818>`_ . Below we describe only parameters additional to the ones explained for `to_fits_tab`. .. warn:: For this helper function, parameters ``bounding_box`` and ``sampling`` (when provided as a tuple) are expected to have the same length as the number of input axes in the *full* WCS object. That is, the number of elements in ``bounding_box`` and ``sampling`` is not be affected by ``ignore_axes``. Other Parameters ---------------- hdr : astropy.io.fits.Header, None The first time this function is called, ``hdr`` should be set to `None` or be an empty :py:class:`~astropy.io.fits.Header` object. On subsequent calls, updated header from the previous iteration should be provided. world_axes_group : tuple of dict A list of world axes to represent through FITS' -TAB convention. This is a list of dictionaries with each dicti axes_mapping : dict A dictionary that maps output axis index to a tuple of input axis indices. In a typical scenario of two input image axes and two output celestial axes for a FITS-like WCS, this dictionary would look like ``{0: (0, 1), 1: (0, 1)}`` with the two non-separable input axes. fix_axes : dict A dictionary containing as keys image axes' indices to be fixed and as values - the values to which inputs should be kept fixed. For example, this dictionary may be used to indicate the celestial axes that should not be included into -TAB approximation because they will be approximated using -SIP. use_cd : bool When `True` - CD-matrix formalism will be used instead of the PC-matrix formalism. bin_ext : str, tuple of str and int Extension name and optionally version for the `~astropy.io.fits.BinTableHDU` HDU. When only a string extension name is provided, extension version will be set to 1. When ``bin_ext`` is a tuple, first element should be extension name and the second element is a positive integer extension version number. Returns ------- hdr : `~astropy.io.fits.Header` Header with WCS-TAB information associated (to be used) with image data. bin_table_hdu : `~astropy.io.fits.BinTableHDU` Binary table extension containing the coordinate array. Raises ------ ValueError When ``bounding_box`` is not defined either through the input ``bounding_box`` parameter or this object's ``bounding_box`` property. ValueError When ``sampling`` is a `tuple` of length larger than 1 that does not match the number of image axes. ValueError When extension version is smaller than 1. TypeError RuntimeError If the number of image axes (``~gwcs.WCS.pixel_n_dim``) is larger than the number of world axes (``~gwcs.WCS.world_n_dim``). """ if isinstance(bin_ext, str): bin_ext = (bin_ext, 1) if isinstance(bounding_box, Bbox): bounding_box = bounding_box.bounding_box(order='F') if isinstance(bounding_box, list): for index, bbox in enumerate(bounding_box): if isinstance(bbox, Bbox): bounding_box[index] = bbox.bounding_box(order='F') # identify input axes: input_axes = [] world_axes_idx = [] for ax in world_axes_group: world_axes_idx.append(ax.axis) input_axes.extend(ax.input_axes) input_axes = sorted(set(input_axes)) n_inputs = len(input_axes) n_outputs = len(world_axes_group) world_axes_idx.sort() # Create initial header and deal with non-degenerate axes if hdr is None: hdr = fits.Header() hdr['NAXIS'] = n_inputs, 'number of array dimensions' hdr['WCSAXES'] = n_outputs hdr.insert('WCSAXES', ('WCSNAME', f'{self.output_frame.name}'), after=True) else: hdr['NAXIS'] += n_inputs hdr['WCSAXES'] += n_outputs # see what axes have been already populated in the header: used_hdr_axes = [] for v in hdr['naxis*'].keys(): try: used_hdr_axes.append(int(v.split('NAXIS')[1]) - 1) except ValueError: continue degenerate_axis_start = max( self.pixel_n_dim + 1, max(used_hdr_axes) + 1 if used_hdr_axes else 1 ) # Deal with non-degenerate axes and add NAXISi to the header: offset = hdr.index('NAXIS') for iax in input_axes: iiax = int(np.searchsorted(used_hdr_axes, iax)) hdr.insert(iiax + offset + 1, (f'NAXIS{iax + 1:d}', int(max(bounding_box[iiax])) + 1)) # 1D grid coordinates: gcrds = [] cdelt = [] bb = [bounding_box[k] for k in input_axes] for (xmin, xmax), s in zip(bb, sampling): npix = max(2, 1 + int(np.ceil(abs((xmax - xmin) / s)))) gcrds.append(np.linspace(xmin, xmax, npix)) cdelt.append((npix - 1) / (xmax - xmin) if xmin != xmax else 1) # In the forward transformation, select only inputs and outputs # that we need given world_axes_group parameter: bb_center = np.mean(bounding_box, axis=1) fixi_dict = { k: bb_center[k] for k in set(range(self.pixel_n_dim)).difference(input_axes) } transform = _fix_transform_inputs(self.forward_transform, fixi_dict) transform = transform | Mapping(world_axes_idx, n_inputs=self.forward_transform.n_outputs) xyz = np.meshgrid(*gcrds[::-1], indexing='ij')[::-1] shape = xyz[0].shape xyz = [v.ravel() for v in xyz] coord = np.stack( transform(*xyz), axis=-1 ) coord = coord.reshape(shape + (len(world_axes_group), )) # create header with WCS info: if hdr is None: hdr = fits.Header() for m, axis_info in enumerate(world_axes_group): k = axis_info.axis widx = world_axes_idx.index(k) k1 = k + 1 ct = cf.get_ctype_from_ucd(self.world_axis_physical_types[k]) if len(ct) > 4: raise ValueError("Axis type name too long.") hdr[f'CTYPE{k1:d}'] = ct + (4 - len(ct)) * '-' + '-TAB' hdr[f'CUNIT{k1:d}'] = self.world_axis_units[k] hdr[f'PS{k1:d}_0'] = bin_ext[0] hdr[f'PV{k1:d}_1'] = bin_ext[1] hdr[f'PS{k1:d}_1'] = coord_col_name hdr[f'PV{k1:d}_3'] = widx + 1 hdr[f'CRVAL{k1:d}'] = 1 if widx < n_inputs: m1 = input_axes[widx] + 1 hdr[f'CRPIX{m1:d}'] = gcrds[widx][0] + 1 if use_cd: hdr[f'CD{k1:d}_{m1:d}'] = cdelt[widx] else: if k1 != m1: hdr[f'PC{k1:d}_{k1:d}'] = 0.0 hdr[f'PC{k1:d}_{m1:d}'] = 1.0 hdr[f'CDELT{k1:d}'] = cdelt[widx] else: m1 = degenerate_axis_start degenerate_axis_start += 1 hdr[f'CRPIX{m1:d}'] = 1 if use_cd: hdr[f'CD{k1:d}_{m1:d}'] = 1.0 else: if k1 != m1: hdr[f'PC{k1:d}_{k1:d}'] = 0.0 hdr[f'PC{k1:d}_{m1:d}'] = 1.0 hdr[f'CDELT{k1:d}'] = 1 # Uncomment 3 lines below to enable use of degenerate axes: # hdr['NAXIS'] = hdr['NAXIS'] + 1 # naxisi_max = max(int(k[5:]) for k in hdr['naxis*'] if k[5:].strip()) # hdr.insert(f'NAXIS{naxisi_max:d}', (f'NAXIS{m1:d}', 1), after=True) # NOTE: in this case make sure NAXIS=WCSAXES coord = coord[None, :] # structured array (data) for binary table HDU: arr = np.array( [(coord, )], dtype=[ (coord_col_name, np.float64, coord.shape), ] ) # create binary table HDU: bin_table_hdu = fits.BinTableHDU(arr, name=bin_ext[0], ver=bin_ext[1]) return hdr, bin_table_hdu
(self, hdr, world_axes_group, use_cd, bounding_box, bin_ext, coord_col_name, sampling)
44,496
gwcs.wcs
_vectorized_fixed_point
null
def _vectorized_fixed_point(self, pix0, world, tolerance, maxiter, adaptive, detect_divergence, quiet, with_bounding_box, fill_value): # ############################################################ # # INITIALIZE ITERATIVE PROCESS: ## # ############################################################ # make a copy of the initial approximation pix0 = np.atleast_2d(np.array(pix0)) # 0-order solution pix = np.array(pix0) world0 = np.atleast_2d(np.array(world)) world = np.array(world0) # estimate pixel scale using approximate algorithm # from https://trs.jpl.nasa.gov/handle/2014/40409 if self.bounding_box is None: crpix = np.ones(self.pixel_n_dim) else: crpix = np.mean(self.bounding_box, axis=-1) l1, phi1 = np.deg2rad(self.__call__(*(crpix - 0.5))) l2, phi2 = np.deg2rad(self.__call__(*(crpix + [-0.5, 0.5]))) l3, phi3 = np.deg2rad(self.__call__(*(crpix + 0.5))) l4, phi4 = np.deg2rad(self.__call__(*(crpix + [0.5, -0.5]))) area = np.abs(0.5 * ((l4 - l2) * (np.sin(phi1) - np.sin(phi3)) + (l1 - l3) * (np.sin(phi2) - np.sin(phi4)))) inv_pscale = 1 / np.rad2deg(np.sqrt(area)) # form equation: def f(x): w = np.array(self.__call__(*(x.T), with_bounding_box=False)).T dw = np.mod(np.subtract(w, world) - 180.0, 360.0) - 180.0 return np.add(inv_pscale * dw, x) def froot(x): return np.mod(np.subtract(self.__call__(*x, with_bounding_box=False), worldi) - 180.0, 360.0) - 180.0 # compute correction: def correction(pix): p1 = f(pix) p2 = f(p1) d = p2 - 2.0 * p1 + pix idx = np.where(d != 0) corr = pix - p2 corr[idx] = np.square(p1[idx] - pix[idx]) / d[idx] return corr # initial iteration: dpix = correction(pix) # Update initial solution: pix -= dpix # Norm (L2) squared of the correction: dn = np.sum(dpix * dpix, axis=1) dnprev = dn.copy() # if adaptive else dn tol2 = tolerance**2 # Prepare for iterative process k = 1 ind = None inddiv = None # Turn off numpy runtime warnings for 'invalid' and 'over': old_invalid = np.geterr()['invalid'] old_over = np.geterr()['over'] np.seterr(invalid='ignore', over='ignore') # ############################################################ # # NON-ADAPTIVE ITERATIONS: ## # ############################################################ if not adaptive: # Fixed-point iterations: while (np.nanmax(dn) >= tol2 and k < maxiter): # Find correction to the previous solution: dpix = correction(pix) # Compute norm (L2) squared of the correction: dn = np.sum(dpix * dpix, axis=1) # Check for divergence (we do this in two stages # to optimize performance for the most common # scenario when successive approximations converge): if detect_divergence: divergent = (dn >= dnprev) if np.any(divergent): # Find solutions that have not yet converged: slowconv = (dn >= tol2) inddiv, = np.where(divergent & slowconv) if inddiv.shape[0] > 0: # Update indices of elements that # still need correction: conv = (dn < dnprev) iconv = np.where(conv) # Apply correction: dpixgood = dpix[iconv] pix[iconv] -= dpixgood dpix[iconv] = dpixgood # For the next iteration choose # non-divergent points that have not yet # converged to the requested accuracy: ind, = np.where(slowconv & conv) world = world[ind] dnprev[ind] = dn[ind] k += 1 # Switch to adaptive iterations: adaptive = True break # Save current correction magnitudes for later: dnprev = dn # Apply correction: pix -= dpix k += 1 # ############################################################ # # ADAPTIVE ITERATIONS: ## # ############################################################ if adaptive: if ind is None: ind, = np.where(np.isfinite(pix).all(axis=1)) world = world[ind] # "Adaptive" fixed-point iterations: while (ind.shape[0] > 0 and k < maxiter): # Find correction to the previous solution: dpixnew = correction(pix[ind]) # Compute norm (L2) of the correction: dnnew = np.sum(np.square(dpixnew), axis=1) # Bookkeeping of corrections: dnprev[ind] = dn[ind].copy() dn[ind] = dnnew if detect_divergence: # Find indices of pixels that are converging: conv = np.logical_or(dnnew < dnprev[ind], dnnew < tol2) if not np.all(conv): conv = np.ones_like(dnnew, dtype=bool) iconv = np.where(conv) iiconv = ind[iconv] # Apply correction: dpixgood = dpixnew[iconv] pix[iiconv] -= dpixgood dpix[iiconv] = dpixgood # Find indices of solutions that have not yet # converged to the requested accuracy # AND that do not diverge: subind, = np.where((dnnew >= tol2) & conv) else: # Apply correction: pix[ind] -= dpixnew dpix[ind] = dpixnew # Find indices of solutions that have not yet # converged to the requested accuracy: subind, = np.where(dnnew >= tol2) # Choose solutions that need more iterations: ind = ind[subind] world = world[subind] k += 1 # ############################################################ # # FINAL DETECTION OF INVALID, DIVERGING, ## # # AND FAILED-TO-CONVERGE POINTS ## # ############################################################ # Identify diverging and/or invalid points: invalid = ((~np.all(np.isfinite(pix), axis=1)) & (np.all(np.isfinite(world0), axis=1))) # When detect_divergence is False, dnprev is outdated # (it is the norm of the very first correction). # Still better than nothing... inddiv, = np.where(((dn >= tol2) & (dn >= dnprev)) | invalid) if inddiv.shape[0] == 0: inddiv = None # If there are divergent points, attempt to find a solution using # scipy's 'hybr' method: if detect_divergence and inddiv is not None and inddiv.size: bad = [] for idx in inddiv: worldi = world0[idx] result = optimize.root( froot, pix0[idx], method='hybr', tol=tolerance / (np.linalg.norm(pix0[idx]) + 1), options={'maxfev': 2 * maxiter} ) if result['success']: pix[idx, :] = result['x'] invalid[idx] = False else: bad.append(idx) if bad: inddiv = np.array(bad, dtype=int) else: inddiv = None # Identify points that did not converge within 'maxiter' # iterations: if k >= maxiter: ind, = np.where((dn >= tol2) & (dn < dnprev) & (~invalid)) if ind.shape[0] == 0: ind = None else: ind = None # Restore previous numpy error settings: np.seterr(invalid=old_invalid, over=old_over) # ############################################################ # # RAISE EXCEPTION IF DIVERGING OR TOO SLOWLY CONVERGING ## # # DATA POINTS HAVE BEEN DETECTED: ## # ############################################################ if (ind is not None or inddiv is not None) and not quiet: if inddiv is None: raise NoConvergence( "'WCS.numerical_inverse' failed to " "converge to the requested accuracy after {:d} " "iterations.".format(k), best_solution=pix, accuracy=np.abs(dpix), niter=k, slow_conv=ind, divergent=None) else: raise NoConvergence( "'WCS.numerical_inverse' failed to " "converge to the requested accuracy.\n" "After {:d} iterations, the solution is diverging " "at least for one input point." .format(k), best_solution=pix, accuracy=np.abs(dpix), niter=k, slow_conv=ind, divergent=inddiv) if with_bounding_box and self.bounding_box is not None: # find points outside the bounding box and replace their values # with fill_value valid = np.logical_not(invalid) in_bb = np.ones_like(invalid, dtype=np.bool_) for c, (x1, x2) in zip(pix[valid].T, self.bounding_box): in_bb[valid] &= (c >= x1) & (c <= x2) pix[np.logical_not(in_bb)] = fill_value return pix
(self, pix0, world, tolerance, maxiter, adaptive, detect_divergence, quiet, with_bounding_box, fill_value)
44,497
gwcs.api
array_index_to_world
Convert array indices to world coordinates (represented by Astropy objects).
def array_index_to_world(self, *index_arrays): """ Convert array indices to world coordinates (represented by Astropy objects). """ pixel_arrays = index_arrays[::-1] pixels = self._sanitize_pixel_inputs(*pixel_arrays) return self(*pixels, with_units=True)
(self, *index_arrays)
44,498
gwcs.api
array_index_to_world_values
Convert array indices to world coordinates. This is the same as `~BaseLowLevelWCS.pixel_to_world_values` except that the indices should be given in ``(i, j)`` order, where for an image ``i`` is the row and ``j`` is the column (i.e. the opposite order to `~BaseLowLevelWCS.pixel_to_world_values`).
def array_index_to_world_values(self, *index_arrays): """ Convert array indices to world coordinates. This is the same as `~BaseLowLevelWCS.pixel_to_world_values` except that the indices should be given in ``(i, j)`` order, where for an image ``i`` is the row and ``j`` is the column (i.e. the opposite order to `~BaseLowLevelWCS.pixel_to_world_values`). """ pixel_arrays = index_arrays[::-1] return self.pixel_to_world_values(*pixel_arrays)
(self, *index_arrays)
44,499
gwcs.wcs
attach_compound_bounding_box
null
def attach_compound_bounding_box(self, cbbox, selector_args): frames = self.available_frames transform_0 = self.get_transform(frames[0], frames[1]) self.bounding_box = CompoundBoundingBox.validate(transform_0, cbbox, selector_args=selector_args, order='F')
(self, cbbox, selector_args)
44,500
gwcs.wcs
fix_inputs
Return a new unique WCS by fixing inputs to constant values. Parameters ---------- fixed : dict Keyword arguments with fixed values corresponding to ``self.selector``. Returns ------- new_wcs : `WCS` A new unique WCS corresponding to the values in ``fixed``. Examples -------- >>> w = WCS(pipeline, selector={"spectral_order": [1, 2]}) # doctest: +SKIP >>> new_wcs = w.set_inputs(spectral_order=2) # doctest: +SKIP >>> new_wcs.inputs # doctest: +SKIP ("x", "y")
def fix_inputs(self, fixed): """ Return a new unique WCS by fixing inputs to constant values. Parameters ---------- fixed : dict Keyword arguments with fixed values corresponding to ``self.selector``. Returns ------- new_wcs : `WCS` A new unique WCS corresponding to the values in ``fixed``. Examples -------- >>> w = WCS(pipeline, selector={"spectral_order": [1, 2]}) # doctest: +SKIP >>> new_wcs = w.set_inputs(spectral_order=2) # doctest: +SKIP >>> new_wcs.inputs # doctest: +SKIP ("x", "y") """ new_pipeline = [] step0 = self.pipeline[0] new_transform = fix_inputs(step0[1], fixed) new_pipeline.append((step0[0], new_transform)) new_pipeline.extend(self.pipeline[1:]) return self.__class__(new_pipeline)
(self, fixed)
44,501
gwcs.wcs
footprint
Return the footprint in world coordinates. Parameters ---------- bounding_box : tuple of floats: (start, stop) ``prop: bounding_box`` center : bool If `True` use the center of the pixel, otherwise use the corner. axis_type : str A supported ``output_frame.axes_type`` or ``"all"`` (default). One of [``'spatial'``, ``'spectral'``, ``'temporal'``] or a custom type. Returns ------- coord : ndarray Array of coordinates in the output_frame mapping corners to the output frame. For spatial coordinates the order is clockwise, starting from the bottom left corner.
def footprint(self, bounding_box=None, center=False, axis_type="all"): """ Return the footprint in world coordinates. Parameters ---------- bounding_box : tuple of floats: (start, stop) ``prop: bounding_box`` center : bool If `True` use the center of the pixel, otherwise use the corner. axis_type : str A supported ``output_frame.axes_type`` or ``"all"`` (default). One of [``'spatial'``, ``'spectral'``, ``'temporal'``] or a custom type. Returns ------- coord : ndarray Array of coordinates in the output_frame mapping corners to the output frame. For spatial coordinates the order is clockwise, starting from the bottom left corner. """ def _order_clockwise(v): return np.asarray([[v[0][0], v[1][0]], [v[0][0], v[1][1]], [v[0][1], v[1][1]], [v[0][1], v[1][0]]]).T if bounding_box is None: if self.bounding_box is None: raise TypeError("Need a valid bounding_box to compute the footprint.") bb = self.bounding_box else: bb = bounding_box all_spatial = all([t.lower() == "spatial" for t in self.output_frame.axes_type]) if all_spatial: vertices = _order_clockwise(bb) else: vertices = np.array(list(itertools.product(*bb))).T if center: vertices = utils._toindex(vertices) result = np.asarray(self.__call__(*vertices, **{'with_bounding_box': False})) axis_type = axis_type.lower() if axis_type == 'spatial' and all_spatial: return result.T if axis_type != "all": axtyp_ind = np.array([t.lower() for t in self.output_frame.axes_type]) == axis_type if not axtyp_ind.any(): raise ValueError('This WCS does not have axis of type "{}".'.format(axis_type)) result = np.asarray([(r.min(), r.max()) for r in result[axtyp_ind]]) if axis_type == "spatial": result = _order_clockwise(result) else: result.sort() result = np.squeeze(result) return result.T
(self, bounding_box=None, center=False, axis_type='all')
44,502
gwcs.wcs
get_transform
Return a transform between two coordinate frames. Parameters ---------- from_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Initial coordinate frame name of object. to_frame : str, or instance of `~gwcs.coordinate_frames.CoordinateFrame` End coordinate frame name or object. Returns ------- transform : `~astropy.modeling.Model` Transform between two frames.
def get_transform(self, from_frame, to_frame): """ Return a transform between two coordinate frames. Parameters ---------- from_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Initial coordinate frame name of object. to_frame : str, or instance of `~gwcs.coordinate_frames.CoordinateFrame` End coordinate frame name or object. Returns ------- transform : `~astropy.modeling.Model` Transform between two frames. """ if not self._pipeline: return None from_ind = self._get_frame_index(from_frame) to_ind = self._get_frame_index(to_frame) if to_ind < from_ind: transforms = [step.transform for step in self._pipeline[to_ind: from_ind]] transforms = [tr.inverse for tr in transforms[::-1]] elif to_ind == from_ind: return None else: transforms = [step.transform for step in self._pipeline[from_ind: to_ind]] return functools.reduce(lambda x, y: x | y, transforms)
(self, from_frame, to_frame)
44,503
gwcs.wcs
in_image
This method tests if one or more of the input world coordinates are contained within forward transformation's image and that it maps to the domain of definition of the forward transformation. In practical terms, this function tests that input world coordinate(s) can be converted to input frame and that it is within the forward transformation's ``bounding_box`` when defined. Parameters ---------- args : float, array like, `~astropy.coordinates.SkyCoord` or `~astropy.units.Unit` coordinates to be inverted kwargs : dict keyword arguments to be passed either to ``backward_transform`` (when defined) or to the iterative invert method. Returns ------- result : bool, numpy.ndarray A single boolean value or an array of boolean values with `True` indicating that the WCS footprint contains the coordinate and `False` if input is outside the footprint.
def in_image(self, *args, **kwargs): """ This method tests if one or more of the input world coordinates are contained within forward transformation's image and that it maps to the domain of definition of the forward transformation. In practical terms, this function tests that input world coordinate(s) can be converted to input frame and that it is within the forward transformation's ``bounding_box`` when defined. Parameters ---------- args : float, array like, `~astropy.coordinates.SkyCoord` or `~astropy.units.Unit` coordinates to be inverted kwargs : dict keyword arguments to be passed either to ``backward_transform`` (when defined) or to the iterative invert method. Returns ------- result : bool, numpy.ndarray A single boolean value or an array of boolean values with `True` indicating that the WCS footprint contains the coordinate and `False` if input is outside the footprint. """ kwargs['with_bounding_box'] = True kwargs['fill_value'] = np.nan coords = self.invert(*args, **kwargs) result = np.isfinite(coords) if self.input_frame.naxes > 1: result = np.all(result, axis=0) if self.bounding_box is None or not np.any(result): return result if self.input_frame.naxes == 1: x1, x2 = self.bounding_box.bounding_box() if len(np.shape(args[0])) > 0: result[result] = (coords[result] >= x1) & (coords[result] <= x2) elif result: result = (coords >= x1) and (coords <= x2) else: if len(np.shape(args[0])) > 0: for c, (x1, x2) in zip(coords, self.bounding_box): result[result] = (c[result] >= x1) & (c[result] <= x2) elif result: result = all([(c >= x1) and (c <= x2) for c, (x1, x2) in zip(coords, self.bounding_box)]) return result
(self, *args, **kwargs)
44,504
gwcs.wcs
insert_frame
Insert a new frame into an existing pipeline. This frame must be anchored to a frame already in the pipeline by a transform. This existing frame is identified solely by its name, although an entire `~gwcs.coordinate_frames.CoordinateFrame` can be passed (e.g., the `input_frame` or `output_frame` attribute). This frame is never modified. Parameters ---------- input_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame at start of new transform transform : `~astropy.modeling.Model` New transform to be inserted in the pipeline output_frame: str or `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame at end of new transform
def insert_frame(self, input_frame, transform, output_frame): """ Insert a new frame into an existing pipeline. This frame must be anchored to a frame already in the pipeline by a transform. This existing frame is identified solely by its name, although an entire `~gwcs.coordinate_frames.CoordinateFrame` can be passed (e.g., the `input_frame` or `output_frame` attribute). This frame is never modified. Parameters ---------- input_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame at start of new transform transform : `~astropy.modeling.Model` New transform to be inserted in the pipeline output_frame: str or `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame at end of new transform """ input_name, input_frame_obj = self._get_frame_name(input_frame) output_name, output_frame_obj = self._get_frame_name(output_frame) try: input_index = self._get_frame_index(input_frame) except CoordinateFrameError: input_index = None if input_frame_obj is None: raise ValueError(f"New coordinate frame {input_name} must " "be defined") try: output_index = self._get_frame_index(output_frame) except CoordinateFrameError: output_index = None if output_frame_obj is None: raise ValueError(f"New coordinate frame {output_name} must " "be defined") new_frames = [input_index, output_index].count(None) if new_frames == 0: raise ValueError("Could not insert frame as both frames " f"{input_name} and {output_name} already exist") elif new_frames == 2: raise ValueError("Could not insert frame as neither frame " f"{input_name} nor {output_name} exists") if input_index is None: self._pipeline = (self._pipeline[:output_index] + [Step(input_frame_obj, transform)] + self._pipeline[output_index:]) super(WCS, self).__setattr__(input_name, input_frame_obj) else: split_step = self._pipeline[input_index] self._pipeline = (self._pipeline[:input_index] + [Step(split_step.frame, transform), Step(output_frame_obj, split_step.transform)] + self._pipeline[input_index + 1:]) super(WCS, self).__setattr__(output_name, output_frame_obj)
(self, input_frame, transform, output_frame)
44,505
gwcs.wcs
insert_transform
Insert a transform before (default) or after a coordinate frame. Append (or prepend) a transform to the transform connected to frame. Parameters ---------- frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame which sets the point of insertion. transform : `~astropy.modeling.Model` New transform to be inserted in the pipeline after : bool If True, the new transform is inserted in the pipeline immediately after ``frame``.
def insert_transform(self, frame, transform, after=False): """ Insert a transform before (default) or after a coordinate frame. Append (or prepend) a transform to the transform connected to frame. Parameters ---------- frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame which sets the point of insertion. transform : `~astropy.modeling.Model` New transform to be inserted in the pipeline after : bool If True, the new transform is inserted in the pipeline immediately after ``frame``. """ name, _ = self._get_frame_name(frame) frame_ind = self._get_frame_index(name) if not after: current_transform = self._pipeline[frame_ind - 1].transform self._pipeline[frame_ind - 1].transform = current_transform | transform else: current_transform = self._pipeline[frame_ind].transform self._pipeline[frame_ind].transform = transform | current_transform
(self, frame, transform, after=False)
44,506
gwcs.wcs
invert
Invert coordinates from output frame to input frame using analytical or user-supplied inverse. When neither analytical nor user-supplied inverses are defined, a numerical solution will be attempted using :py:meth:`numerical_inverse`. .. note:: Currently numerical inverse is implemented only for 2D imaging WCS. Parameters ---------- args : float, array like, `~astropy.coordinates.SkyCoord` or `~astropy.units.Unit` Coordinates to be inverted. The number of arguments must be equal to the number of world coordinates given by ``world_n_dim``. with_bounding_box : bool, optional If `True` (default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is ``np.nan``). with_units : bool, optional If ``True`` returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object, by using the units of the output cooridnate frame. Default is `False`. Other Parameters ---------------- kwargs : dict Keyword arguments to be passed to :py:meth:`numerical_inverse` (when defined) or to the iterative invert method. Returns ------- result : tuple or value Returns a tuple of scalar or array values for each axis. Unless ``input_frame.naxes == 1`` when it shall return the value.
def invert(self, *args, **kwargs): """ Invert coordinates from output frame to input frame using analytical or user-supplied inverse. When neither analytical nor user-supplied inverses are defined, a numerical solution will be attempted using :py:meth:`numerical_inverse`. .. note:: Currently numerical inverse is implemented only for 2D imaging WCS. Parameters ---------- args : float, array like, `~astropy.coordinates.SkyCoord` or `~astropy.units.Unit` Coordinates to be inverted. The number of arguments must be equal to the number of world coordinates given by ``world_n_dim``. with_bounding_box : bool, optional If `True` (default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is ``np.nan``). with_units : bool, optional If ``True`` returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object, by using the units of the output cooridnate frame. Default is `False`. Other Parameters ---------------- kwargs : dict Keyword arguments to be passed to :py:meth:`numerical_inverse` (when defined) or to the iterative invert method. Returns ------- result : tuple or value Returns a tuple of scalar or array values for each axis. Unless ``input_frame.naxes == 1`` when it shall return the value. """ with_units = kwargs.pop('with_units', False) if not utils.isnumerical(args[0]): args = self.output_frame.coordinate_to_quantity(*args) if self.output_frame.naxes == 1: args = [args] try: if not self.backward_transform.uses_quantity: args = utils.get_values(self.output_frame.unit, *args) except (NotImplementedError, KeyError): args = utils.get_values(self.output_frame.unit, *args) if 'with_bounding_box' not in kwargs: kwargs['with_bounding_box'] = True if 'fill_value' not in kwargs: kwargs['fill_value'] = np.nan try: # remove iterative inverse-specific keyword arguments: akwargs = {k: v for k, v in kwargs.items() if k not in _ITER_INV_KWARGS} result = self.backward_transform(*args, **akwargs) except (NotImplementedError, KeyError): result = self.numerical_inverse(*args, **kwargs, with_units=with_units) if with_units and self.input_frame: if self.input_frame.naxes == 1: return self.input_frame.coordinates(result) else: return self.input_frame.coordinates(*result) else: return result
(self, *args, **kwargs)
44,507
gwcs.wcs
numerical_inverse
Invert coordinates from output frame to input frame using numerical inverse. .. note:: Currently numerical inverse is implemented only for 2D imaging WCS. .. note:: This method uses a combination of vectorized fixed-point iterations algorithm and `scipy.optimize.root`. The later is used for input coordinates for which vectorized algorithm diverges. Parameters ---------- args : float, array like, `~astropy.coordinates.SkyCoord` or `~astropy.units.Unit` Coordinates to be inverted. The number of arguments must be equal to the number of world coordinates given by ``world_n_dim``. with_bounding_box : bool, optional If `True` (default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is ``np.nan``). with_units : bool, optional If ``True`` returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object, by using the units of the output cooridnate frame. Default is `False`. tolerance : float, optional *Absolute tolerance* of solution. Iteration terminates when the iterative solver estimates that the "true solution" is within this many pixels current estimate, more specifically, when the correction to the solution found during the previous iteration is smaller (in the sense of the L2 norm) than ``tolerance``. Default ``tolerance`` is 1.0e-5. maxiter : int, optional Maximum number of iterations allowed to reach a solution. Default is 50. quiet : bool, optional Do not throw :py:class:`NoConvergence` exceptions when the method does not converge to a solution with the required accuracy within a specified number of maximum iterations set by ``maxiter`` parameter. Instead, simply return the found solution. Default is `True`. adaptive : bool, optional Specifies whether to adaptively select only points that did not converge to a solution within the required accuracy for the next iteration. Default (`True`) is recommended. .. note:: The :py:meth:`numerical_inverse` uses a vectorized implementation of the method of consecutive approximations (see ``Notes`` section below) in which it iterates over *all* input points *regardless* until the required accuracy has been reached for *all* input points. In some cases it may be possible that *almost all* points have reached the required accuracy but there are only a few of input data points for which additional iterations may be needed (this depends mostly on the characteristics of the geometric distortions for a given instrument). In this situation it may be advantageous to set ``adaptive`` = `True` in which case :py:meth:`numerical_inverse` will continue iterating *only* over the points that have not yet converged to the required accuracy. .. note:: When ``detect_divergence`` is `True`, :py:meth:`numerical_inverse` will automatically switch to the adaptive algorithm once divergence has been detected. detect_divergence : bool, optional Specifies whether to perform a more detailed analysis of the convergence to a solution. Normally :py:meth:`numerical_inverse` may not achieve the required accuracy if either the ``tolerance`` or ``maxiter`` arguments are too low. However, it may happen that for some geometric distortions the conditions of convergence for the the method of consecutive approximations used by :py:meth:`numerical_inverse` may not be satisfied, in which case consecutive approximations to the solution will diverge regardless of the ``tolerance`` or ``maxiter`` settings. When ``detect_divergence`` is `False`, these divergent points will be detected as not having achieved the required accuracy (without further details). In addition, if ``adaptive`` is `False` then the algorithm will not know that the solution (for specific points) is diverging and will continue iterating and trying to "improve" diverging solutions. This may result in ``NaN`` or ``Inf`` values in the return results (in addition to a performance penalties). Even when ``detect_divergence`` is `False`, :py:meth:`numerical_inverse`, at the end of the iterative process, will identify invalid results (``NaN`` or ``Inf``) as "diverging" solutions and will raise :py:class:`NoConvergence` unless the ``quiet`` parameter is set to `True`. When ``detect_divergence`` is `True` (default), :py:meth:`numerical_inverse` will detect points for which current correction to the coordinates is larger than the correction applied during the previous iteration **if** the requested accuracy **has not yet been achieved**. In this case, if ``adaptive`` is `True`, these points will be excluded from further iterations and if ``adaptive`` is `False`, :py:meth:`numerical_inverse` will automatically switch to the adaptive algorithm. Thus, the reported divergent solution will be the latest converging solution computed immediately *before* divergence has been detected. .. note:: When accuracy has been achieved, small increases in current corrections may be possible due to rounding errors (when ``adaptive`` is `False`) and such increases will be ignored. .. note:: Based on our testing using JWST NIRCAM images, setting ``detect_divergence`` to `True` will incur about 5-10% performance penalty with the larger penalty corresponding to ``adaptive`` set to `True`. Because the benefits of enabling this feature outweigh the small performance penalty, especially when ``adaptive`` = `False`, it is recommended to set ``detect_divergence`` to `True`, unless extensive testing of the distortion models for images from specific instruments show a good stability of the numerical method for a wide range of coordinates (even outside the image itself). .. note:: Indices of the diverging inverse solutions will be reported in the ``divergent`` attribute of the raised :py:class:`NoConvergence` exception object. Returns ------- result : tuple Returns a tuple of scalar or array values for each axis. Raises ------ NoConvergence The iterative method did not converge to a solution to the required accuracy within a specified number of maximum iterations set by the ``maxiter`` parameter. To turn off this exception, set ``quiet`` to `True`. Indices of the points for which the requested accuracy was not achieved (if any) will be listed in the ``slow_conv`` attribute of the raised :py:class:`NoConvergence` exception object. See :py:class:`NoConvergence` documentation for more details. NotImplementedError Numerical inverse has not been implemented for this WCS. ValueError Invalid argument values. Examples -------- >>> from astropy.utils.data import get_pkg_data_filename >>> from gwcs import NoConvergence >>> import asdf >>> import numpy as np >>> filename = get_pkg_data_filename('data/nircamwcs.asdf', package='gwcs.tests') >>> with asdf.open(filename, copy_arrays=True, lazy_load=False, ignore_missing_extensions=True) as af: ... w = af.tree['wcs'] >>> ra, dec = w([1,2,3], [1,1,1]) >>> assert np.allclose(ra, [5.927628, 5.92757069, 5.92751337]); >>> assert np.allclose(dec, [-72.01341247, -72.01341273, -72.013413]) >>> x, y = w.numerical_inverse(ra, dec) >>> assert np.allclose(x, [1.00000005, 2.00000005, 3.00000006]); >>> assert np.allclose(y, [1.00000004, 0.99999979, 1.00000015]); >>> x, y = w.numerical_inverse(ra, dec, maxiter=3, tolerance=1.0e-10, quiet=False) Traceback (most recent call last): ... gwcs.wcs.NoConvergence: 'WCS.numerical_inverse' failed to converge to the requested accuracy after 3 iterations. >>> w.numerical_inverse( ... *w([1, 300000, 3], [2, 1000000, 5], with_bounding_box=False), ... adaptive=False, ... detect_divergence=True, ... quiet=False, ... with_bounding_box=False ... ) Traceback (most recent call last): ... gwcs.wcs.NoConvergence: 'WCS.numerical_inverse' failed to converge to the requested accuracy. After 4 iterations, the solution is diverging at least for one input point. >>> # Now try to use some diverging data: >>> divra, divdec = w([1, 300000, 3], [2, 1000000, 5], with_bounding_box=False) >>> assert np.allclose(divra, [5.92762673, 148.21600848, 5.92750827]) >>> assert np.allclose(divdec, [-72.01339464, -7.80968079, -72.01334172]) >>> try: # doctest: +SKIP ... x, y = w.numerical_inverse(divra, divdec, maxiter=20, ... tolerance=1.0e-4, adaptive=True, ... detect_divergence=True, ... quiet=False) ... except NoConvergence as e: ... print(f"Indices of diverging points: {e.divergent}") ... print(f"Indices of poorly converging points: {e.slow_conv}") ... print(f"Best solution:\n{e.best_solution}") ... print(f"Achieved accuracy:\n{e.accuracy}") Indices of diverging points: None Indices of poorly converging points: [1] Best solution: [[1.00000040e+00 1.99999841e+00] [6.33507833e+17 3.40118820e+17] [3.00000038e+00 4.99999841e+00]] Achieved accuracy: [[2.75925982e-05 1.18471543e-05] [3.65405005e+04 1.31364188e+04] [2.76552923e-05 1.14789013e-05]]
def numerical_inverse(self, *args, tolerance=1e-5, maxiter=50, adaptive=True, detect_divergence=True, quiet=True, with_bounding_box=True, fill_value=np.nan, with_units=False, **kwargs): """ Invert coordinates from output frame to input frame using numerical inverse. .. note:: Currently numerical inverse is implemented only for 2D imaging WCS. .. note:: This method uses a combination of vectorized fixed-point iterations algorithm and `scipy.optimize.root`. The later is used for input coordinates for which vectorized algorithm diverges. Parameters ---------- args : float, array like, `~astropy.coordinates.SkyCoord` or `~astropy.units.Unit` Coordinates to be inverted. The number of arguments must be equal to the number of world coordinates given by ``world_n_dim``. with_bounding_box : bool, optional If `True` (default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is ``np.nan``). with_units : bool, optional If ``True`` returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object, by using the units of the output cooridnate frame. Default is `False`. tolerance : float, optional *Absolute tolerance* of solution. Iteration terminates when the iterative solver estimates that the "true solution" is within this many pixels current estimate, more specifically, when the correction to the solution found during the previous iteration is smaller (in the sense of the L2 norm) than ``tolerance``. Default ``tolerance`` is 1.0e-5. maxiter : int, optional Maximum number of iterations allowed to reach a solution. Default is 50. quiet : bool, optional Do not throw :py:class:`NoConvergence` exceptions when the method does not converge to a solution with the required accuracy within a specified number of maximum iterations set by ``maxiter`` parameter. Instead, simply return the found solution. Default is `True`. adaptive : bool, optional Specifies whether to adaptively select only points that did not converge to a solution within the required accuracy for the next iteration. Default (`True`) is recommended. .. note:: The :py:meth:`numerical_inverse` uses a vectorized implementation of the method of consecutive approximations (see ``Notes`` section below) in which it iterates over *all* input points *regardless* until the required accuracy has been reached for *all* input points. In some cases it may be possible that *almost all* points have reached the required accuracy but there are only a few of input data points for which additional iterations may be needed (this depends mostly on the characteristics of the geometric distortions for a given instrument). In this situation it may be advantageous to set ``adaptive`` = `True` in which case :py:meth:`numerical_inverse` will continue iterating *only* over the points that have not yet converged to the required accuracy. .. note:: When ``detect_divergence`` is `True`, :py:meth:`numerical_inverse` will automatically switch to the adaptive algorithm once divergence has been detected. detect_divergence : bool, optional Specifies whether to perform a more detailed analysis of the convergence to a solution. Normally :py:meth:`numerical_inverse` may not achieve the required accuracy if either the ``tolerance`` or ``maxiter`` arguments are too low. However, it may happen that for some geometric distortions the conditions of convergence for the the method of consecutive approximations used by :py:meth:`numerical_inverse` may not be satisfied, in which case consecutive approximations to the solution will diverge regardless of the ``tolerance`` or ``maxiter`` settings. When ``detect_divergence`` is `False`, these divergent points will be detected as not having achieved the required accuracy (without further details). In addition, if ``adaptive`` is `False` then the algorithm will not know that the solution (for specific points) is diverging and will continue iterating and trying to "improve" diverging solutions. This may result in ``NaN`` or ``Inf`` values in the return results (in addition to a performance penalties). Even when ``detect_divergence`` is `False`, :py:meth:`numerical_inverse`, at the end of the iterative process, will identify invalid results (``NaN`` or ``Inf``) as "diverging" solutions and will raise :py:class:`NoConvergence` unless the ``quiet`` parameter is set to `True`. When ``detect_divergence`` is `True` (default), :py:meth:`numerical_inverse` will detect points for which current correction to the coordinates is larger than the correction applied during the previous iteration **if** the requested accuracy **has not yet been achieved**. In this case, if ``adaptive`` is `True`, these points will be excluded from further iterations and if ``adaptive`` is `False`, :py:meth:`numerical_inverse` will automatically switch to the adaptive algorithm. Thus, the reported divergent solution will be the latest converging solution computed immediately *before* divergence has been detected. .. note:: When accuracy has been achieved, small increases in current corrections may be possible due to rounding errors (when ``adaptive`` is `False`) and such increases will be ignored. .. note:: Based on our testing using JWST NIRCAM images, setting ``detect_divergence`` to `True` will incur about 5-10% performance penalty with the larger penalty corresponding to ``adaptive`` set to `True`. Because the benefits of enabling this feature outweigh the small performance penalty, especially when ``adaptive`` = `False`, it is recommended to set ``detect_divergence`` to `True`, unless extensive testing of the distortion models for images from specific instruments show a good stability of the numerical method for a wide range of coordinates (even outside the image itself). .. note:: Indices of the diverging inverse solutions will be reported in the ``divergent`` attribute of the raised :py:class:`NoConvergence` exception object. Returns ------- result : tuple Returns a tuple of scalar or array values for each axis. Raises ------ NoConvergence The iterative method did not converge to a solution to the required accuracy within a specified number of maximum iterations set by the ``maxiter`` parameter. To turn off this exception, set ``quiet`` to `True`. Indices of the points for which the requested accuracy was not achieved (if any) will be listed in the ``slow_conv`` attribute of the raised :py:class:`NoConvergence` exception object. See :py:class:`NoConvergence` documentation for more details. NotImplementedError Numerical inverse has not been implemented for this WCS. ValueError Invalid argument values. Examples -------- >>> from astropy.utils.data import get_pkg_data_filename >>> from gwcs import NoConvergence >>> import asdf >>> import numpy as np >>> filename = get_pkg_data_filename('data/nircamwcs.asdf', package='gwcs.tests') >>> with asdf.open(filename, copy_arrays=True, lazy_load=False, ignore_missing_extensions=True) as af: ... w = af.tree['wcs'] >>> ra, dec = w([1,2,3], [1,1,1]) >>> assert np.allclose(ra, [5.927628, 5.92757069, 5.92751337]); >>> assert np.allclose(dec, [-72.01341247, -72.01341273, -72.013413]) >>> x, y = w.numerical_inverse(ra, dec) >>> assert np.allclose(x, [1.00000005, 2.00000005, 3.00000006]); >>> assert np.allclose(y, [1.00000004, 0.99999979, 1.00000015]); >>> x, y = w.numerical_inverse(ra, dec, maxiter=3, tolerance=1.0e-10, quiet=False) Traceback (most recent call last): ... gwcs.wcs.NoConvergence: 'WCS.numerical_inverse' failed to converge to the requested accuracy after 3 iterations. >>> w.numerical_inverse( ... *w([1, 300000, 3], [2, 1000000, 5], with_bounding_box=False), ... adaptive=False, ... detect_divergence=True, ... quiet=False, ... with_bounding_box=False ... ) Traceback (most recent call last): ... gwcs.wcs.NoConvergence: 'WCS.numerical_inverse' failed to converge to the requested accuracy. After 4 iterations, the solution is diverging at least for one input point. >>> # Now try to use some diverging data: >>> divra, divdec = w([1, 300000, 3], [2, 1000000, 5], with_bounding_box=False) >>> assert np.allclose(divra, [5.92762673, 148.21600848, 5.92750827]) >>> assert np.allclose(divdec, [-72.01339464, -7.80968079, -72.01334172]) >>> try: # doctest: +SKIP ... x, y = w.numerical_inverse(divra, divdec, maxiter=20, ... tolerance=1.0e-4, adaptive=True, ... detect_divergence=True, ... quiet=False) ... except NoConvergence as e: ... print(f"Indices of diverging points: {e.divergent}") ... print(f"Indices of poorly converging points: {e.slow_conv}") ... print(f"Best solution:\\n{e.best_solution}") ... print(f"Achieved accuracy:\\n{e.accuracy}") Indices of diverging points: None Indices of poorly converging points: [1] Best solution: [[1.00000040e+00 1.99999841e+00] [6.33507833e+17 3.40118820e+17] [3.00000038e+00 4.99999841e+00]] Achieved accuracy: [[2.75925982e-05 1.18471543e-05] [3.65405005e+04 1.31364188e+04] [2.76552923e-05 1.14789013e-05]] """ if not utils.isnumerical(args[0]): args = self.output_frame.coordinate_to_quantity(*args) if self.output_frame.naxes == 1: args = [args] args = utils.get_values(self.output_frame.unit, *args) args_shape = np.shape(args) nargs = args_shape[0] arg_dim = len(args_shape) - 1 if nargs != self.world_n_dim: raise ValueError("Number of input coordinates is different from " "the number of defined world coordinates in the " f"WCS ({self.world_n_dim:d})") if self.world_n_dim != self.pixel_n_dim: raise NotImplementedError( "Support for iterative inverse for transformations with " "different number of inputs and outputs was not implemented." ) # initial guess: if nargs == 2 and self._approx_inverse is None: self._calc_approx_inv(max_inv_pix_error=5, inv_degree=None) if self._approx_inverse is None: if self.bounding_box is None: x0 = np.ones(self.pixel_n_dim) else: x0 = np.mean(self.bounding_box, axis=-1) if arg_dim == 0: argsi = args if nargs == 2 and self._approx_inverse is not None: x0 = self._approx_inverse(*argsi) if not np.all(np.isfinite(x0)): return [np.array(np.nan) for _ in range(nargs)] result = tuple(self._vectorized_fixed_point( x0, argsi, tolerance=tolerance, maxiter=maxiter, adaptive=adaptive, detect_divergence=detect_divergence, quiet=quiet, with_bounding_box=with_bounding_box, fill_value=fill_value ).T.ravel().tolist()) else: arg_shape = args_shape[1:] nelem = np.prod(arg_shape) args = np.reshape(args, (nargs, nelem)) if self._approx_inverse is None: x0 = np.full((nelem, nargs), x0) else: x0 = np.array(self._approx_inverse(*args)).T result = self._vectorized_fixed_point( x0, args.T, tolerance=tolerance, maxiter=maxiter, adaptive=adaptive, detect_divergence=detect_divergence, quiet=quiet, with_bounding_box=with_bounding_box, fill_value=fill_value ).T result = tuple(np.reshape(result, args_shape)) if with_units and self.input_frame: if self.input_frame.naxes == 1: return self.input_frame.coordinates(result) else: return self.input_frame.coordinates(*result) else: return result
(self, *args, tolerance=1e-05, maxiter=50, adaptive=True, detect_divergence=True, quiet=True, with_bounding_box=True, fill_value=nan, with_units=False, **kwargs)
44,508
gwcs.api
pixel_to_world
Convert pixel values to world coordinates.
def pixel_to_world(self, *pixel_arrays): """ Convert pixel values to world coordinates. """ pixels = self._sanitize_pixel_inputs(*pixel_arrays) return self(*pixels, with_units=True)
(self, *pixel_arrays)
44,509
gwcs.api
pixel_to_world_values
Convert pixel coordinates to world coordinates. This method takes ``pixel_n_dim`` scalars or arrays as input, and pixel coordinates should be zero-based. Returns ``world_n_dim`` scalars or arrays in units given by ``world_axis_units``. Note that pixel coordinates are assumed to be 0 at the center of the first pixel in each dimension. If a pixel is in a region where the WCS is not defined, NaN can be returned. The coordinates should be specified in the ``(x, y)`` order, where for an image, ``x`` is the horizontal coordinate and ``y`` is the vertical coordinate.
def pixel_to_world_values(self, *pixel_arrays): """ Convert pixel coordinates to world coordinates. This method takes ``pixel_n_dim`` scalars or arrays as input, and pixel coordinates should be zero-based. Returns ``world_n_dim`` scalars or arrays in units given by ``world_axis_units``. Note that pixel coordinates are assumed to be 0 at the center of the first pixel in each dimension. If a pixel is in a region where the WCS is not defined, NaN can be returned. The coordinates should be specified in the ``(x, y)`` order, where for an image, ``x`` is the horizontal coordinate and ``y`` is the vertical coordinate. """ pixel_arrays = self._add_units_input(pixel_arrays, self.forward_transform, self.input_frame) result = self(*pixel_arrays, with_units=False) return self._remove_quantity_output(result, self.output_frame)
(self, *pixel_arrays)
44,510
gwcs.wcs
set_transform
Set/replace the transform between two coordinate frames. Parameters ---------- from_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Initial coordinate frame. to_frame : str, or instance of `~gwcs.coordinate_frames.CoordinateFrame` End coordinate frame. transform : `~astropy.modeling.Model` Transform between ``from_frame`` and ``to_frame``.
def set_transform(self, from_frame, to_frame, transform): """ Set/replace the transform between two coordinate frames. Parameters ---------- from_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Initial coordinate frame. to_frame : str, or instance of `~gwcs.coordinate_frames.CoordinateFrame` End coordinate frame. transform : `~astropy.modeling.Model` Transform between ``from_frame`` and ``to_frame``. """ from_name, from_obj = self._get_frame_name(from_frame) to_name, to_obj = self._get_frame_name(to_frame) if not self._pipeline: if from_name != self._input_frame: raise CoordinateFrameError( "Expected 'from_frame' to be {0}".format(self._input_frame)) if to_frame != self._output_frame: raise CoordinateFrameError( "Expected 'to_frame' to be {0}".format(self._output_frame)) try: from_ind = self._get_frame_index(from_name) except ValueError: raise CoordinateFrameError("Frame {0} is not in the available frames".format(from_name)) try: to_ind = self._get_frame_index(to_name) except ValueError: raise CoordinateFrameError("Frame {0} is not in the available frames".format(to_name)) if from_ind + 1 != to_ind: raise ValueError("Frames {0} and {1} are not in sequence".format(from_name, to_name)) self._pipeline[from_ind].transform = transform
(self, from_frame, to_frame, transform)
44,511
gwcs.wcs
to_fits
Construct a FITS WCS ``-TAB``-based approximation to the WCS in the form of a FITS header and a binary table extension. For the description of the FITS WCS ``-TAB`` convention, see "Representations of spectral coordinates in FITS" in `Greisen, E. W. et al. A&A 446 (2) 747-771 (2006) <https://doi.org/10.1051/0004-6361:20053818>`_ . If WCS contains celestial frame, PC/CD formalism will be used for the celestial axes. .. note:: SIP distortion fitting requires that the WCS object has only two celestial axes. When WCS does not contain celestial axes, SIP fitting parameters (``max_pix_error``, ``degree``, ``max_inv_pix_error``, ``inv_degree``, and ``projection``) are ignored. When a WCS, in addition to celestial frame, contains other types of axes, SIP distortion fitting is disabled (ony linear terms are fitted for celestial frame). Parameters ---------- bounding_box : tuple, optional Specifies the range of acceptable values for each input axis. The order of the axes is `~gwcs.coordinate_frames.CoordinateFrame.axes_order`. For two image axes ``bounding_box`` is of the form ``((xmin, xmax), (ymin, ymax))``. max_pix_error : float, optional Maximum allowed error over the domain of the pixel array. This error is the equivalent pixel error that corresponds to the maximum error in the output coordinate resulting from the fit based on a nominal plate scale. degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_pixel_error`` is ignored. .. note:: When WCS object has When ``degree`` is `None` and the WCS object has max_inv_pix_error : float, optional Maximum allowed inverse error over the domain of the pixel array in pixel units. If None, no inverse is generated. inv_degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_inv_pixel_error`` is ignored. npoints : int, optional The number of points in each dimension to sample the bounding box for use in the SIP fit. Minimum number of points is 3. crpix : list of float, None, optional Coordinates (1-based) of the reference point for the new FITS WCS. When not provided, i.e., when set to `None` (default) the reference pixel will be chosen near the center of the bounding box for axes corresponding to the celestial frame. projection : str, `~astropy.modeling.projections.Pix2SkyProjection`, optional Projection to be used for the created FITS WCS. It can be specified as a string of three characters specifying a FITS projection code from Table 13 in `Representations of World Coordinates in FITS <https://doi.org/10.1051/0004-6361:20021326>`_ (Paper I), Greisen, E. W., and Calabretta, M. R., A & A, 395, 1061-1075, 2002. Alternatively, it can be an instance of one of the `astropy's Pix2Sky_* <https://docs.astropy.org/en/stable/modeling/ reference_api.html#module-astropy.modeling.projections>`_ projection models inherited from :py:class:`~astropy.modeling.projections.Pix2SkyProjection`. bin_ext_name : str, optional Extension name for the `~astropy.io.fits.BinTableHDU` HDU for those axes groups that will be converted using FITW WCS' ``-TAB`` algorith. Extension version will be determined automatically based on the number of separable group of axes. coord_col_name : str, optional Field name of the coordinate array in the structured array stored in `~astropy.io.fits.BinTableHDU` data. This corresponds to ``TTYPEi`` field in the FITS header of the binary table extension. sampling : float, tuple, optional The target "density" of grid nodes per pixel to be used when creating the coordinate array for the ``-TAB`` FITS WCS convention. It is equal to ``1/step`` where ``step`` is the distance between grid nodes in pixels. ``sampling`` can be specified as a single number to be used for all axes or as a `tuple` of numbers that specify the sampling for each image axis. verbose : bool, optional Print progress of fits. Returns ------- hdr : `~astropy.io.fits.Header` Header with WCS-TAB information associated (to be used) with image data. hdulist : a list of `~astropy.io.fits.BinTableHDU` A Python list of binary table extensions containing the coordinate array for TAB extensions; one extension per separable axes group. Raises ------ ValueError When ``bounding_box`` is not defined either through the input ``bounding_box`` parameter or this object's ``bounding_box`` property. ValueError When ``sampling`` is a `tuple` of length larger than 1 that does not match the number of image axes. RuntimeError If the number of image axes (``~gwcs.WCS.pixel_n_dim``) is larger than the number of world axes (``~gwcs.WCS.world_n_dim``).
def to_fits(self, bounding_box=None, max_pix_error=0.25, degree=None, max_inv_pix_error=0.25, inv_degree=None, npoints=32, crpix=None, projection='TAN', bin_ext_name='WCS-TABLE', coord_col_name='coordinates', sampling=1, verbose=False): """ Construct a FITS WCS ``-TAB``-based approximation to the WCS in the form of a FITS header and a binary table extension. For the description of the FITS WCS ``-TAB`` convention, see "Representations of spectral coordinates in FITS" in `Greisen, E. W. et al. A&A 446 (2) 747-771 (2006) <https://doi.org/10.1051/0004-6361:20053818>`_ . If WCS contains celestial frame, PC/CD formalism will be used for the celestial axes. .. note:: SIP distortion fitting requires that the WCS object has only two celestial axes. When WCS does not contain celestial axes, SIP fitting parameters (``max_pix_error``, ``degree``, ``max_inv_pix_error``, ``inv_degree``, and ``projection``) are ignored. When a WCS, in addition to celestial frame, contains other types of axes, SIP distortion fitting is disabled (ony linear terms are fitted for celestial frame). Parameters ---------- bounding_box : tuple, optional Specifies the range of acceptable values for each input axis. The order of the axes is `~gwcs.coordinate_frames.CoordinateFrame.axes_order`. For two image axes ``bounding_box`` is of the form ``((xmin, xmax), (ymin, ymax))``. max_pix_error : float, optional Maximum allowed error over the domain of the pixel array. This error is the equivalent pixel error that corresponds to the maximum error in the output coordinate resulting from the fit based on a nominal plate scale. degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_pixel_error`` is ignored. .. note:: When WCS object has When ``degree`` is `None` and the WCS object has max_inv_pix_error : float, optional Maximum allowed inverse error over the domain of the pixel array in pixel units. If None, no inverse is generated. inv_degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_inv_pixel_error`` is ignored. npoints : int, optional The number of points in each dimension to sample the bounding box for use in the SIP fit. Minimum number of points is 3. crpix : list of float, None, optional Coordinates (1-based) of the reference point for the new FITS WCS. When not provided, i.e., when set to `None` (default) the reference pixel will be chosen near the center of the bounding box for axes corresponding to the celestial frame. projection : str, `~astropy.modeling.projections.Pix2SkyProjection`, optional Projection to be used for the created FITS WCS. It can be specified as a string of three characters specifying a FITS projection code from Table 13 in `Representations of World Coordinates in FITS \ <https://doi.org/10.1051/0004-6361:20021326>`_ (Paper I), Greisen, E. W., and Calabretta, M. R., A & A, 395, 1061-1075, 2002. Alternatively, it can be an instance of one of the `astropy's Pix2Sky_* <https://docs.astropy.org/en/stable/modeling/\ reference_api.html#module-astropy.modeling.projections>`_ projection models inherited from :py:class:`~astropy.modeling.projections.Pix2SkyProjection`. bin_ext_name : str, optional Extension name for the `~astropy.io.fits.BinTableHDU` HDU for those axes groups that will be converted using FITW WCS' ``-TAB`` algorith. Extension version will be determined automatically based on the number of separable group of axes. coord_col_name : str, optional Field name of the coordinate array in the structured array stored in `~astropy.io.fits.BinTableHDU` data. This corresponds to ``TTYPEi`` field in the FITS header of the binary table extension. sampling : float, tuple, optional The target "density" of grid nodes per pixel to be used when creating the coordinate array for the ``-TAB`` FITS WCS convention. It is equal to ``1/step`` where ``step`` is the distance between grid nodes in pixels. ``sampling`` can be specified as a single number to be used for all axes or as a `tuple` of numbers that specify the sampling for each image axis. verbose : bool, optional Print progress of fits. Returns ------- hdr : `~astropy.io.fits.Header` Header with WCS-TAB information associated (to be used) with image data. hdulist : a list of `~astropy.io.fits.BinTableHDU` A Python list of binary table extensions containing the coordinate array for TAB extensions; one extension per separable axes group. Raises ------ ValueError When ``bounding_box`` is not defined either through the input ``bounding_box`` parameter or this object's ``bounding_box`` property. ValueError When ``sampling`` is a `tuple` of length larger than 1 that does not match the number of image axes. RuntimeError If the number of image axes (``~gwcs.WCS.pixel_n_dim``) is larger than the number of world axes (``~gwcs.WCS.world_n_dim``). """ if bounding_box is None: if self.bounding_box is None: raise ValueError( "Need a valid bounding_box to compute the footprint." ) bounding_box = self.bounding_box else: # validate user-supplied bounding box: frames = self.available_frames transform_0 = self.get_transform(frames[0], frames[1]) Bbox.validate(transform_0, bounding_box) if self.forward_transform.n_inputs == 1: bounding_box = [bounding_box] if self.pixel_n_dim > self.world_n_dim: raise RuntimeError( "The case when the number of input axes is larger than the " "number of output axes is not supported." ) try: sampling = np.broadcast_to(sampling, (self.pixel_n_dim, )) except ValueError: raise ValueError("Number of sampling values either must be 1 " "or it must match the number of pixel axes.") world_axes_groups, _, celestial_group = self._separable_groups( detect_celestial=True ) # Find celestial axes group and treat it separately from other axes: if celestial_group: # if world_axes_groups is empty, then we have only celestial axes # and so we can allow arbitrary degree for SIP. When there are # other axes types present, issue a warning and set 'degree' to 1 # because use of SIP when world_n_dim > 2 currently is not supported by # astropy.wcs.WCS - see https://github.com/astropy/astropy/pull/11452 if world_axes_groups and (degree is None or np.max(degree) != 2): if degree is not None: warnings.warn( "SIP distortion is not supported when the number\n" "of axes in WCS is larger than 2. Setting 'degree'\n" "to 1 and 'max_inv_pix_error' to None." ) degree = 1 max_inv_pix_error = None hdr = self._to_fits_sip( celestial_group=celestial_group, keep_axis_position=True, bounding_box=bounding_box, max_pix_error=max_pix_error, degree=degree, max_inv_pix_error=max_inv_pix_error, inv_degree=inv_degree, npoints=npoints, crpix=crpix, projection=projection, matrix_type='PC-CDELT1', verbose=verbose ) use_cd = 'A_ORDER' in hdr else: use_cd = False hdr = fits.Header() hdr['NAXIS'] = 0 hdr['WCSAXES'] = 0 # now handle non-celestial axes using -TAB convention for each # separable axes group: hdulist = [] for extver0, world_axes_group in enumerate(world_axes_groups): # For each subset of separable axes call _to_fits_tab to # convert that group to a single Bin TableHDU with a # coordinate array for this group of axes: hdr, bin_table_hdu = self._to_fits_tab( hdr=hdr, world_axes_group=world_axes_group, use_cd=use_cd, bounding_box=bounding_box, bin_ext=(bin_ext_name, extver0 + 1), coord_col_name=coord_col_name, sampling=sampling ) hdulist.append(bin_table_hdu) hdr.add_comment('FITS WCS created by approximating a gWCS') return hdr, hdulist
(self, bounding_box=None, max_pix_error=0.25, degree=None, max_inv_pix_error=0.25, inv_degree=None, npoints=32, crpix=None, projection='TAN', bin_ext_name='WCS-TABLE', coord_col_name='coordinates', sampling=1, verbose=False)
44,512
gwcs.wcs
to_fits_sip
Construct a SIP-based approximation to the WCS for the axes corresponding to the `~gwcs.coordinate_frames.CelestialFrame` in the form of a FITS header. The default mode in using this attempts to achieve roughly 0.25 pixel accuracy over the whole image. Parameters ---------- bounding_box : tuple, optional A pair of tuples, each consisting of two numbers Represents the range of pixel values in both dimensions ((xmin, xmax), (ymin, ymax)) max_pix_error : float, optional Maximum allowed error over the domain of the pixel array. This error is the equivalent pixel error that corresponds to the maximum error in the output coordinate resulting from the fit based on a nominal plate scale. Ignored when ``degree`` is an integer or a list with a single degree. degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_pixel_error`` is ignored. max_inv_pix_error : float, optional Maximum allowed inverse error over the domain of the pixel array in pixel units. If None, no inverse is generated. Ignored when ``degree`` is an integer or a list with a single degree. inv_degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_inv_pixel_error`` is ignored. npoints : int, optional The number of points in each dimension to sample the bounding box for use in the SIP fit. Minimum number of points is 3. crpix : list of float, None, optional Coordinates (1-based) of the reference point for the new FITS WCS. When not provided, i.e., when set to `None` (default) the reference pixel will be chosen near the center of the bounding box for axes corresponding to the celestial frame. projection : str, `~astropy.modeling.projections.Pix2SkyProjection`, optional Projection to be used for the created FITS WCS. It can be specified as a string of three characters specifying a FITS projection code from Table 13 in `Representations of World Coordinates in FITS <https://doi.org/10.1051/0004-6361:20021326>`_ (Paper I), Greisen, E. W., and Calabretta, M. R., A & A, 395, 1061-1075, 2002. Alternatively, it can be an instance of one of the `astropy's Pix2Sky_* <https://docs.astropy.org/en/stable/modeling/ reference_api.html#module-astropy.modeling.projections>`_ projection models inherited from :py:class:`~astropy.modeling.projections.Pix2SkyProjection`. verbose : bool, optional Print progress of fits. Returns ------- FITS header with all SIP WCS keywords Raises ------ ValueError If the WCS is not at least 2D, an exception will be raised. If the specified accuracy (both forward and inverse, both rms and maximum) is not achieved an exception will be raised. Notes ----- Use of this requires a judicious choice of required accuracies. Attempts to use higher degrees (~7 or higher) will typically fail due to floating point problems that arise with high powers.
def to_fits_sip(self, bounding_box=None, max_pix_error=0.25, degree=None, max_inv_pix_error=0.25, inv_degree=None, npoints=32, crpix=None, projection='TAN', verbose=False): """ Construct a SIP-based approximation to the WCS for the axes corresponding to the `~gwcs.coordinate_frames.CelestialFrame` in the form of a FITS header. The default mode in using this attempts to achieve roughly 0.25 pixel accuracy over the whole image. Parameters ---------- bounding_box : tuple, optional A pair of tuples, each consisting of two numbers Represents the range of pixel values in both dimensions ((xmin, xmax), (ymin, ymax)) max_pix_error : float, optional Maximum allowed error over the domain of the pixel array. This error is the equivalent pixel error that corresponds to the maximum error in the output coordinate resulting from the fit based on a nominal plate scale. Ignored when ``degree`` is an integer or a list with a single degree. degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_pixel_error`` is ignored. max_inv_pix_error : float, optional Maximum allowed inverse error over the domain of the pixel array in pixel units. If None, no inverse is generated. Ignored when ``degree`` is an integer or a list with a single degree. inv_degree : int, iterable, None, optional Degree of the SIP polynomial. Default value `None` indicates that all allowed degree values (``[1...9]``) will be considered and the lowest degree that meets accuracy requerements set by ``max_pix_error`` will be returned. Alternatively, ``degree`` can be an iterable containing allowed values for the SIP polynomial degree. This option is similar to default `None` but it allows caller to restrict the range of allowed SIP degrees used for fitting. Finally, ``degree`` can be an integer indicating the exact SIP degree to be fit to the WCS transformation. In this case ``max_inv_pixel_error`` is ignored. npoints : int, optional The number of points in each dimension to sample the bounding box for use in the SIP fit. Minimum number of points is 3. crpix : list of float, None, optional Coordinates (1-based) of the reference point for the new FITS WCS. When not provided, i.e., when set to `None` (default) the reference pixel will be chosen near the center of the bounding box for axes corresponding to the celestial frame. projection : str, `~astropy.modeling.projections.Pix2SkyProjection`, optional Projection to be used for the created FITS WCS. It can be specified as a string of three characters specifying a FITS projection code from Table 13 in `Representations of World Coordinates in FITS \ <https://doi.org/10.1051/0004-6361:20021326>`_ (Paper I), Greisen, E. W., and Calabretta, M. R., A & A, 395, 1061-1075, 2002. Alternatively, it can be an instance of one of the `astropy's Pix2Sky_* <https://docs.astropy.org/en/stable/modeling/\ reference_api.html#module-astropy.modeling.projections>`_ projection models inherited from :py:class:`~astropy.modeling.projections.Pix2SkyProjection`. verbose : bool, optional Print progress of fits. Returns ------- FITS header with all SIP WCS keywords Raises ------ ValueError If the WCS is not at least 2D, an exception will be raised. If the specified accuracy (both forward and inverse, both rms and maximum) is not achieved an exception will be raised. Notes ----- Use of this requires a judicious choice of required accuracies. Attempts to use higher degrees (~7 or higher) will typically fail due to floating point problems that arise with high powers. """ _, _, celestial_group = self._separable_groups(detect_celestial=True) if celestial_group is None: raise ValueError("The to_fits_sip requires an output celestial frame.") hdr = self._to_fits_sip( celestial_group=celestial_group, keep_axis_position=False, bounding_box=bounding_box, max_pix_error=max_pix_error, degree=degree, max_inv_pix_error=max_inv_pix_error, inv_degree=inv_degree, npoints=npoints, crpix=crpix, projection=projection, matrix_type='CD', verbose=verbose ) return hdr
(self, bounding_box=None, max_pix_error=0.25, degree=None, max_inv_pix_error=0.25, inv_degree=None, npoints=32, crpix=None, projection='TAN', verbose=False)
44,513
gwcs.wcs
to_fits_tab
Construct a FITS WCS ``-TAB``-based approximation to the WCS in the form of a FITS header and a binary table extension. For the description of the FITS WCS ``-TAB`` convention, see "Representations of spectral coordinates in FITS" in `Greisen, E. W. et al. A&A 446 (2) 747-771 (2006) <https://doi.org/10.1051/0004-6361:20053818>`_ . Parameters ---------- bounding_box : tuple, optional Specifies the range of acceptable values for each input axis. The order of the axes is `~gwcs.coordinate_frames.CoordinateFrame.axes_order`. For two image axes ``bounding_box`` is of the form ``((xmin, xmax), (ymin, ymax))``. bin_ext_name : str, optional Extension name for the `~astropy.io.fits.BinTableHDU` HDU for those axes groups that will be converted using FITW WCS' ``-TAB`` algorith. Extension version will be determined automatically based on the number of separable group of axes. coord_col_name : str, optional Field name of the coordinate array in the structured array stored in `~astropy.io.fits.BinTableHDU` data. This corresponds to ``TTYPEi`` field in the FITS header of the binary table extension. sampling : float, tuple, optional The target "density" of grid nodes per pixel to be used when creating the coordinate array for the ``-TAB`` FITS WCS convention. It is equal to ``1/step`` where ``step`` is the distance between grid nodes in pixels. ``sampling`` can be specified as a single number to be used for all axes or as a `tuple` of numbers that specify the sampling for each image axis. Returns ------- hdr : `~astropy.io.fits.Header` Header with WCS-TAB information associated (to be used) with image data. bin_table_hdu : `~astropy.io.fits.BinTableHDU` Binary table extension containing the coordinate array. Raises ------ ValueError When ``bounding_box`` is not defined either through the input ``bounding_box`` parameter or this object's ``bounding_box`` property. ValueError When ``sampling`` is a `tuple` of length larger than 1 that does not match the number of image axes. RuntimeError If the number of image axes (``~gwcs.WCS.pixel_n_dim``) is larger than the number of world axes (``~gwcs.WCS.world_n_dim``).
def to_fits_tab(self, bounding_box=None, bin_ext_name='WCS-TABLE', coord_col_name='coordinates', sampling=1): """ Construct a FITS WCS ``-TAB``-based approximation to the WCS in the form of a FITS header and a binary table extension. For the description of the FITS WCS ``-TAB`` convention, see "Representations of spectral coordinates in FITS" in `Greisen, E. W. et al. A&A 446 (2) 747-771 (2006) <https://doi.org/10.1051/0004-6361:20053818>`_ . Parameters ---------- bounding_box : tuple, optional Specifies the range of acceptable values for each input axis. The order of the axes is `~gwcs.coordinate_frames.CoordinateFrame.axes_order`. For two image axes ``bounding_box`` is of the form ``((xmin, xmax), (ymin, ymax))``. bin_ext_name : str, optional Extension name for the `~astropy.io.fits.BinTableHDU` HDU for those axes groups that will be converted using FITW WCS' ``-TAB`` algorith. Extension version will be determined automatically based on the number of separable group of axes. coord_col_name : str, optional Field name of the coordinate array in the structured array stored in `~astropy.io.fits.BinTableHDU` data. This corresponds to ``TTYPEi`` field in the FITS header of the binary table extension. sampling : float, tuple, optional The target "density" of grid nodes per pixel to be used when creating the coordinate array for the ``-TAB`` FITS WCS convention. It is equal to ``1/step`` where ``step`` is the distance between grid nodes in pixels. ``sampling`` can be specified as a single number to be used for all axes or as a `tuple` of numbers that specify the sampling for each image axis. Returns ------- hdr : `~astropy.io.fits.Header` Header with WCS-TAB information associated (to be used) with image data. bin_table_hdu : `~astropy.io.fits.BinTableHDU` Binary table extension containing the coordinate array. Raises ------ ValueError When ``bounding_box`` is not defined either through the input ``bounding_box`` parameter or this object's ``bounding_box`` property. ValueError When ``sampling`` is a `tuple` of length larger than 1 that does not match the number of image axes. RuntimeError If the number of image axes (``~gwcs.WCS.pixel_n_dim``) is larger than the number of world axes (``~gwcs.WCS.world_n_dim``). """ if bounding_box is None: if self.bounding_box is None: raise ValueError( "Need a valid bounding_box to compute the footprint." ) bounding_box = self.bounding_box else: # validate user-supplied bounding box: frames = self.available_frames transform_0 = self.get_transform(frames[0], frames[1]) Bbox.validate(transform_0, bounding_box) if self.forward_transform.n_inputs == 1: bounding_box = [bounding_box] if self.pixel_n_dim > self.world_n_dim: raise RuntimeError( "The case when the number of input axes is larger than the " "number of output axes is not supported." ) try: sampling = np.broadcast_to(sampling, (self.pixel_n_dim, )) except ValueError: raise ValueError("Number of sampling values either must be 1 " "or it must match the number of pixel axes.") _, world_axes = self._separable_groups(detect_celestial=False) hdr, bin_table_hdu = self._to_fits_tab( hdr=None, world_axes_group=world_axes, use_cd=False, bounding_box=bounding_box, bin_ext=bin_ext_name, coord_col_name=coord_col_name, sampling=sampling ) return hdr, bin_table_hdu
(self, bounding_box=None, bin_ext_name='WCS-TABLE', coord_col_name='coordinates', sampling=1)
44,514
gwcs.wcs
transform
Transform positions between two frames. Parameters ---------- from_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Initial coordinate frame. to_frame : str, or instance of `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame into which to transform. args : float or array-like Inputs in ``from_frame``, separate inputs for each dimension. output_with_units : bool If ``True`` - returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object. with_bounding_box : bool, optional If True(default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is np.nan).
def transform(self, from_frame, to_frame, *args, **kwargs): """ Transform positions between two frames. Parameters ---------- from_frame : str or `~gwcs.coordinate_frames.CoordinateFrame` Initial coordinate frame. to_frame : str, or instance of `~gwcs.coordinate_frames.CoordinateFrame` Coordinate frame into which to transform. args : float or array-like Inputs in ``from_frame``, separate inputs for each dimension. output_with_units : bool If ``True`` - returns a `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.SpectralCoord` object. with_bounding_box : bool, optional If True(default) values in the result which correspond to any of the inputs being outside the bounding_box are set to ``fill_value``. fill_value : float, optional Output value for inputs outside the bounding_box (default is np.nan). """ transform = self.get_transform(from_frame, to_frame) if not utils.isnumerical(args[0]): inp_frame = getattr(self, from_frame) args = inp_frame.coordinate_to_quantity(*args) if not transform.uses_quantity: args = utils.get_values(inp_frame.unit, *args) with_units = kwargs.pop("with_units", False) if 'with_bounding_box' not in kwargs: kwargs['with_bounding_box'] = True if 'fill_value' not in kwargs: kwargs['fill_value'] = np.nan result = transform(*args, **kwargs) if with_units: to_frame_name, to_frame_obj = self._get_frame_name(to_frame) if to_frame_obj is not None: if to_frame_obj.naxes == 1: result = to_frame_obj.coordinates(result) else: result = to_frame_obj.coordinates(*result) else: raise TypeError("Coordinate objects could not be created because" "frame {0} is not defined.".format(to_frame_name)) return result
(self, from_frame, to_frame, *args, **kwargs)
44,515
gwcs.api
world_to_array_index
Convert world coordinates (represented by Astropy objects) to array indices.
def world_to_array_index(self, *world_objects): """ Convert world coordinates (represented by Astropy objects) to array indices. """ result = self.invert(*world_objects, with_units=True)[::-1] return tuple([utils._toindex(r) for r in result])
(self, *world_objects)
44,516
gwcs.api
world_to_array_index_values
Convert world coordinates to array indices. This is the same as `~BaseLowLevelWCS.world_to_pixel_values` except that the indices should be returned in ``(i, j)`` order, where for an image ``i`` is the row and ``j`` is the column (i.e. the opposite order to `~BaseLowLevelWCS.pixel_to_world_values`). The indices should be returned as rounded integers.
def world_to_array_index_values(self, *world_arrays): """ Convert world coordinates to array indices. This is the same as `~BaseLowLevelWCS.world_to_pixel_values` except that the indices should be returned in ``(i, j)`` order, where for an image ``i`` is the row and ``j`` is the column (i.e. the opposite order to `~BaseLowLevelWCS.pixel_to_world_values`). The indices should be returned as rounded integers. """ result = self.world_to_pixel_values(*world_arrays) if self.pixel_n_dim != 1: result = result[::-1] return result
(self, *world_arrays)
44,517
gwcs.api
world_to_pixel
Convert world coordinates to pixel values.
def world_to_pixel(self, *world_objects): """ Convert world coordinates to pixel values. """ result = self.invert(*world_objects, with_units=True) if self.input_frame.naxes > 1: first_res = result[0] if not utils.isnumerical(first_res): result = [i.value for i in result] else: if not utils.isnumerical(result): result = result.value return result
(self, *world_objects)
44,518
gwcs.api
world_to_pixel_values
Convert world coordinates to pixel coordinates. This method takes ``world_n_dim`` scalars or arrays as input in units given by ``world_axis_units``. Returns ``pixel_n_dim`` scalars or arrays. Note that pixel coordinates are assumed to be 0 at the center of the first pixel in each dimension. If a world coordinate does not have a matching pixel coordinate, NaN can be returned. The coordinates should be returned in the ``(x, y)`` order, where for an image, ``x`` is the horizontal coordinate and ``y`` is the vertical coordinate.
def world_to_pixel_values(self, *world_arrays): """ Convert world coordinates to pixel coordinates. This method takes ``world_n_dim`` scalars or arrays as input in units given by ``world_axis_units``. Returns ``pixel_n_dim`` scalars or arrays. Note that pixel coordinates are assumed to be 0 at the center of the first pixel in each dimension. If a world coordinate does not have a matching pixel coordinate, NaN can be returned. The coordinates should be returned in the ``(x, y)`` order, where for an image, ``x`` is the horizontal coordinate and ``y`` is the vertical coordinate. """ world_arrays = self._add_units_input(world_arrays, self.backward_transform, self.output_frame) result = self.invert(*world_arrays, with_units=False) return self._remove_quantity_output(result, self.input_frame)
(self, *world_arrays)
44,521
gwcs.wcstools
grid_from_bounding_box
Create a grid of input points from the WCS bounding_box. Note: If ``bbox`` is a tuple describing the range of an axis in ``bounding_box``, ``x.5`` is considered part of the next pixel in ``bbox[0]`` and part of the previous pixel in ``bbox[1]``. In this way if ``bbox`` describes the edges of an image the indexing includes only pixels within the image. Parameters ---------- bounding_box : tuple The bounding_box of a WCS object, `~gwcs.wcs.WCS.bounding_box`. step : scalar or tuple Step size for grid in each dimension. Scalar applies to all dimensions. center : bool The bounding_box is in order of X, Y [, Z] and the output will be in the same order. Examples -------- >>> bb = ((-1, 2.9), (6, 7.5)) >>> grid_from_bounding_box(bb, step=(1, .5), center=False) array([[[-1. , 0. , 1. , 2. , 3. ], [-1. , 0. , 1. , 2. , 3. ], [-1. , 0. , 1. , 2. , 3. ], [-1. , 0. , 1. , 2. , 3. ]], [[ 6. , 6. , 6. , 6. , 6. ], [ 6.5, 6.5, 6.5, 6.5, 6.5], [ 7. , 7. , 7. , 7. , 7. ], [ 7.5, 7.5, 7.5, 7.5, 7.5]]]) >>> bb = ((-1, 2.9), (6, 7.5)) >>> grid_from_bounding_box(bb) array([[[-1., 0., 1., 2., 3.], [-1., 0., 1., 2., 3.]], [[ 6., 6., 6., 6., 6.], [ 7., 7., 7., 7., 7.]]]) Returns ------- x, y [, z]: ndarray Grid of points.
def grid_from_bounding_box(bounding_box, step=1, center=True): """ Create a grid of input points from the WCS bounding_box. Note: If ``bbox`` is a tuple describing the range of an axis in ``bounding_box``, ``x.5`` is considered part of the next pixel in ``bbox[0]`` and part of the previous pixel in ``bbox[1]``. In this way if ``bbox`` describes the edges of an image the indexing includes only pixels within the image. Parameters ---------- bounding_box : tuple The bounding_box of a WCS object, `~gwcs.wcs.WCS.bounding_box`. step : scalar or tuple Step size for grid in each dimension. Scalar applies to all dimensions. center : bool The bounding_box is in order of X, Y [, Z] and the output will be in the same order. Examples -------- >>> bb = ((-1, 2.9), (6, 7.5)) >>> grid_from_bounding_box(bb, step=(1, .5), center=False) array([[[-1. , 0. , 1. , 2. , 3. ], [-1. , 0. , 1. , 2. , 3. ], [-1. , 0. , 1. , 2. , 3. ], [-1. , 0. , 1. , 2. , 3. ]], [[ 6. , 6. , 6. , 6. , 6. ], [ 6.5, 6.5, 6.5, 6.5, 6.5], [ 7. , 7. , 7. , 7. , 7. ], [ 7.5, 7.5, 7.5, 7.5, 7.5]]]) >>> bb = ((-1, 2.9), (6, 7.5)) >>> grid_from_bounding_box(bb) array([[[-1., 0., 1., 2., 3.], [-1., 0., 1., 2., 3.]], [[ 6., 6., 6., 6., 6.], [ 7., 7., 7., 7., 7.]]]) Returns ------- x, y [, z]: ndarray Grid of points. """ def _bbox_to_pixel(bbox): return (np.floor(bbox[0] + 0.5), np.ceil(bbox[1] - 0.5)) # 1D case if np.isscalar(bounding_box[0]): nd = 1 bounding_box = (bounding_box, ) else: nd = len(bounding_box) if center: bb = tuple([_bbox_to_pixel(bb) for bb in bounding_box]) else: bb = bounding_box step = np.atleast_1d(step) if nd > 1 and len(step) == 1: step = np.repeat(step, nd) if len(step) != len(bb): raise ValueError('`step` must be a scalar, or tuple with length ' 'matching `bounding_box`') slices = [] for d, s in zip(bb, step): slices.append(slice(d[0], d[1] + s, s)) grid = np.mgrid[slices[::-1]][::-1] if nd == 1: return grid[0] return grid
(bounding_box, step=1, center=True)
44,527
gwcs.wcstools
wcs_from_fiducial
Create a WCS object from a fiducial point in a coordinate frame. If an additional transform is supplied it is prepended to the projection. Parameters ---------- fiducial : `~astropy.coordinates.SkyCoord` or tuple of float One of: A location on the sky in some standard coordinate system. A Quantity with spectral units. A list of the above. coordinate_frame : ~gwcs.coordinate_frames.CoordinateFrame` The output coordinate frame. If fiducial is not an instance of `~astropy.coordinates.SkyCoord`, ``coordinate_frame`` is required. projection : `~astropy.modeling.projections.Projection` Projection instance - required if there is a celestial component in the fiducial. transform : `~astropy.modeling.Model` (optional) An optional tranform to be prepended to the transform constructed by the fiducial point. The number of outputs of this transform must equal the number of axes in the coordinate frame. name : str Name of this WCS. bounding_box : tuple The bounding box over which the WCS is valid. It is a tuple of tuples of size 2 where each tuple represents a range of (low, high) values. The ``bounding_box`` is in the order of the axes, `~gwcs.coordinate_frames.CoordinateFrame.axes_order`. For two inputs and axes_order(0, 1) the bounding box is ((xlow, xhigh), (ylow, yhigh)). input_frame : ~gwcs.coordinate_frames.CoordinateFrame` The input coordinate frame.
def wcs_from_fiducial(fiducial, coordinate_frame=None, projection=None, transform=None, name='', bounding_box=None, input_frame=None): """ Create a WCS object from a fiducial point in a coordinate frame. If an additional transform is supplied it is prepended to the projection. Parameters ---------- fiducial : `~astropy.coordinates.SkyCoord` or tuple of float One of: A location on the sky in some standard coordinate system. A Quantity with spectral units. A list of the above. coordinate_frame : ~gwcs.coordinate_frames.CoordinateFrame` The output coordinate frame. If fiducial is not an instance of `~astropy.coordinates.SkyCoord`, ``coordinate_frame`` is required. projection : `~astropy.modeling.projections.Projection` Projection instance - required if there is a celestial component in the fiducial. transform : `~astropy.modeling.Model` (optional) An optional tranform to be prepended to the transform constructed by the fiducial point. The number of outputs of this transform must equal the number of axes in the coordinate frame. name : str Name of this WCS. bounding_box : tuple The bounding box over which the WCS is valid. It is a tuple of tuples of size 2 where each tuple represents a range of (low, high) values. The ``bounding_box`` is in the order of the axes, `~gwcs.coordinate_frames.CoordinateFrame.axes_order`. For two inputs and axes_order(0, 1) the bounding box is ((xlow, xhigh), (ylow, yhigh)). input_frame : ~gwcs.coordinate_frames.CoordinateFrame` The input coordinate frame. """ from .wcs import WCS if transform is not None: if not isinstance(transform, Model): raise UnsupportedTransformError("Expected transform to be an instance" "of astropy.modeling.Model") # transform_outputs = transform.n_outputs if isinstance(fiducial, coord.SkyCoord): coordinate_frame = CelestialFrame(reference_frame=fiducial.frame, unit=(fiducial.spherical.lon.unit, fiducial.spherical.lat.unit)) fiducial_transform = _sky_transform(fiducial, projection) elif isinstance(coordinate_frame, CompositeFrame): trans_from_fiducial = [] for item in coordinate_frame.frames: ind = coordinate_frame.frames.index(item) try: model = frame2transform[item.__class__](fiducial[ind], projection=projection) except KeyError: raise TypeError("Coordinate frame {0} is not supported".format(item)) trans_from_fiducial.append(model) fiducial_transform = functools.reduce(lambda x, y: x & y, [tr for tr in trans_from_fiducial]) else: # The case of one coordinate frame with more than 1 axes. try: fiducial_transform = frame2transform[coordinate_frame.__class__](fiducial, projection=projection) except KeyError: raise TypeError("Coordinate frame {0} is not supported".format(coordinate_frame)) if transform is not None: forward_transform = transform | fiducial_transform else: forward_transform = fiducial_transform if bounding_box is not None: if len(bounding_box) != forward_transform.n_outputs: raise ValueError("Expected the number of items in 'bounding_box' to be equal to the " "number of outputs of the forawrd transform.") forward_transform.bounding_box = bounding_box[::-1] if input_frame is None: input_frame = 'detector' return WCS(output_frame=coordinate_frame, input_frame=input_frame, forward_transform=forward_transform, name=name)
(fiducial, coordinate_frame=None, projection=None, transform=None, name='', bounding_box=None, input_frame=None)
44,528
gwcs.wcstools
wcs_from_points
Given two matching sets of coordinates on detector and sky, compute the WCS. Notes ----- This function implements the following algorithm: ``world_coords`` are transformed to a projection plane using the specified projection. A polynomial fits ``xy`` and the projected coordinates. The fitted polynomials and the projection transforms are combined into a tranform from detector to sky. The input coordinate frame is set to ``detector``. The output coordinate frame is initialized based on the frame in the fiducial. Parameters ---------- xy : tuple of 2 ndarrays Points in the input cooridnate frame - x, y inputs. world_coords : `~astropy.coordinates.SkyCoord` Points in the output coordinate frame. The order matches the order of ``xy``. proj_point : `~astropy.coordinates.SkyCoord` A fiducial point in the output coordinate frame. If set to 'center' (default), the geometric center of input world coordinates will be used as the projection point. To specify an exact point for the projection, a Skycoord object with a coordinate pair can be passed in. projection : `~astropy.modeling.projections.Projection` A projection type. One of the projections in `~astropy.modeling.projections.projcodes`. Defaults to TAN projection (`astropy.modeling.projections.Sky2Pix_TAN`). poly_degree : int Degree of polynomial model to be fit to data. Defaults to 4. polynomial_type : str one of "polynomial", "chebyshev", "legendre". Defaults to "polynomial". Returns ------- wcsobj : `~gwcs.wcs.WCS` a WCS object for this observation.
def wcs_from_points(xy, world_coords, proj_point='center', projection=projections.Sky2Pix_TAN(), poly_degree=4, polynomial_type='polynomial'): """ Given two matching sets of coordinates on detector and sky, compute the WCS. Notes ----- This function implements the following algorithm: ``world_coords`` are transformed to a projection plane using the specified projection. A polynomial fits ``xy`` and the projected coordinates. The fitted polynomials and the projection transforms are combined into a tranform from detector to sky. The input coordinate frame is set to ``detector``. The output coordinate frame is initialized based on the frame in the fiducial. Parameters ---------- xy : tuple of 2 ndarrays Points in the input cooridnate frame - x, y inputs. world_coords : `~astropy.coordinates.SkyCoord` Points in the output coordinate frame. The order matches the order of ``xy``. proj_point : `~astropy.coordinates.SkyCoord` A fiducial point in the output coordinate frame. If set to 'center' (default), the geometric center of input world coordinates will be used as the projection point. To specify an exact point for the projection, a Skycoord object with a coordinate pair can be passed in. projection : `~astropy.modeling.projections.Projection` A projection type. One of the projections in `~astropy.modeling.projections.projcodes`. Defaults to TAN projection (`astropy.modeling.projections.Sky2Pix_TAN`). poly_degree : int Degree of polynomial model to be fit to data. Defaults to 4. polynomial_type : str one of "polynomial", "chebyshev", "legendre". Defaults to "polynomial". Returns ------- wcsobj : `~gwcs.wcs.WCS` a WCS object for this observation. """ from .wcs import WCS supported_poly_types = {"polynomial": models.Polynomial2D, "chebyshev": models.Chebyshev2D, "legendre": models.Legendre2D } x, y = xy if not isinstance(world_coords, coord.SkyCoord): raise TypeError('`world_coords` must be an `~astropy.coordinates.SkyCoord`') try: lon, lat = world_coords.data.lon.deg, world_coords.data.lat.deg except AttributeError: unit_sph = world_coords.unit_spherical lon, lat = unit_sph.lon.deg, unit_sph.lat.deg if isinstance(proj_point, coord.SkyCoord): assert proj_point.size == 1 proj_point.transform_to(world_coords) crval = (proj_point.data.lon, proj_point.data.lat) frame = proj_point.frame elif proj_point == 'center': # use center of input points sc1 = coord.SkyCoord(lon.min()*u.deg, lat.max()*u.deg) sc2 = coord.SkyCoord(lon.max()*u.deg, lat.min()*u.deg) pa = sc1.position_angle(sc2) sep = sc1.separation(sc2) midpoint_sc = sc1.directional_offset_by(pa, sep/2) crval = (midpoint_sc.data.lon, midpoint_sc.data.lat) frame = sc1.frame else: raise ValueError("`proj_point` must be set to 'center', or an" + "`~astropy.coordinates.SkyCoord` object with " + "a pair of points.") if not isinstance(projection, projections.Projection): raise UnsupportedProjectionError("Unsupported projection code {0}".format(projection)) if polynomial_type not in supported_poly_types.keys(): raise ValueError("Unsupported polynomial_type: {}. " "Only one of {} is supported.".format(polynomial_type, supported_poly_types.keys())) skyrot = models.RotateCelestial2Native(crval[0], crval[1], 180*u.deg) trans = (skyrot | projection) projection_x, projection_y = trans(lon, lat) poly = supported_poly_types[polynomial_type](poly_degree) fitter = fitting.LevMarLSQFitter() with warnings.catch_warnings(): warnings.simplefilter("ignore") poly_x = fitter(poly, x, y, projection_x) poly_y = fitter(poly, x, y, projection_y) distortion = models.Mapping((0, 1, 0, 1)) | poly_x & poly_y poly_x_inverse = fitter(poly, projection_x, projection_y, x) poly_y_inverse = fitter(poly, projection_x, projection_y, y) distortion.inverse = models.Mapping((0, 1, 0, 1)) | poly_x_inverse & poly_y_inverse transform = distortion | projection.inverse | skyrot.inverse skyframe = CelestialFrame(reference_frame=frame) detector = Frame2D(name="detector") pipeline = [(detector, transform), (skyframe, None)] return WCS(pipeline)
(xy, world_coords, proj_point='center', projection=<Sky2Pix_Gnomonic()>, poly_degree=4, polynomial_type='polynomial')
44,530
sock
AbstractPwnlibTubes
null
class AbstractPwnlibTubes(object): def recvall(self, *args, **kwargs): return self.read_all(*args, **kwargs) def recvline(self, *args, **kwargs): return self.read_line(*args, **kwargs) def recvuntil(self, *args, **kwargs): return self.read_until(*args, **kwargs) def recvregex(self, *args, **kwargs): return self.read_until_re(*args, **kwargs) def sendline(self, *args, **kwargs): return self.send_line(*args, **kwargs) def readall(self, *args, **kwargs): return self.recvall(*args, **kwargs) def readline(self, *args, **kwargs): return self.recvline(*args, **kwargs) def readuntil(self, *args, **kwargs): return self.recvuntil(*args, **kwargs) def readregex(self, *args, **kwargs): return self.recvregex(*args, **kwargs) def interactive(self, *args, **kwargs): return self.interact(*args, **kwargs)
()
44,531
sock
interactive
null
def interactive(self, *args, **kwargs): return self.interact(*args, **kwargs)
(self, *args, **kwargs)
44,532
sock
readall
null
def readall(self, *args, **kwargs): return self.recvall(*args, **kwargs)
(self, *args, **kwargs)
44,533
sock
readline
null
def readline(self, *args, **kwargs): return self.recvline(*args, **kwargs)
(self, *args, **kwargs)
44,534
sock
readregex
null
def readregex(self, *args, **kwargs): return self.recvregex(*args, **kwargs)
(self, *args, **kwargs)
44,535
sock
readuntil
null
def readuntil(self, *args, **kwargs): return self.recvuntil(*args, **kwargs)
(self, *args, **kwargs)
44,536
sock
recvall
null
def recvall(self, *args, **kwargs): return self.read_all(*args, **kwargs)
(self, *args, **kwargs)
44,537
sock
recvline
null
def recvline(self, *args, **kwargs): return self.read_line(*args, **kwargs)
(self, *args, **kwargs)
44,538
sock
recvregex
null
def recvregex(self, *args, **kwargs): return self.read_until_re(*args, **kwargs)
(self, *args, **kwargs)
44,539
sock
recvuntil
null
def recvuntil(self, *args, **kwargs): return self.read_until(*args, **kwargs)
(self, *args, **kwargs)
44,540
sock
sendline
null
def sendline(self, *args, **kwargs): return self.send_line(*args, **kwargs)
(self, *args, **kwargs)
44,541
sock
AbstractSock
SomeSock("127.0.0.1", 3123, timeout=15) - timeout should be given using explicit keyword
class AbstractSock(object): """ SomeSock("127.0.0.1", 3123, timeout=15) - timeout should be given using explicit keyword """ SOCKET_FAMILY = NotImplemented SOCKET_TYPE = NotImplemented RECV_SIZE = 4096 def __init__(self, *addr, **timeout_dict): self.addr = parse_addr(*addr) # python2 does not allow (*args, timeout=None) :( self.timeout = float(timeout_dict.pop("timeout", DEFAULT_TIMEOUT)) if timeout_dict: raise TypeError("Only timeout should be given through keyword args") self.buf = b"" self.eof = False self._init_sock() self._connect() return @classmethod def from_socket(cls, sock, timeout=None): self = object.__new__(cls) self.addr = sock.getpeername() self.sock = sock assert self.SOCKET_TYPE == sock.type if timeout is not None: self.timeout = float(timeout) self.sock.settimeout(self.timeout) else: self.timeout = self.sock.gettimeout() self.buf = b"" self.eof = False return self def _init_sock(self): self.sock = socket.socket(self.SOCKET_FAMILY, self.SOCKET_TYPE) self.sock.settimeout(self.timeout) def _connect(self): return NotImplemented def recv(self): return NotImplemented def send(self): return NotImplemented def send_line(self, line): return self.send(Bytes(line) + b"\n") def read_line(self, timeout=None): return self.read_until(b"\n", timeout=timeout) def read_one(self, timeout=None): """ Read something from socket timeout = -1 - wait until something new in socket timeout = 0 - return cached buffer + socket buffer immediately timeout = N - wait N seconds until something new in socket, else raise TimeoutError """ self._fill_one(timeout) if not self.buf and timeout != 0: raise EOFError("Connection closed") res = self.buf self.buf = b"" return res def read_all(self, timeout=None): """ Read everything from socket (the other side should close socket before timeout) """ self.read_cond(lambda x: x.eof, timeout) res = self.buf self.buf = b"" return res def skip_until(self, s, timeout=None): """ Skip everything until first occurence of string @s, stop before occurence. Return nothing. """ s = Bytes(s) self.read_cond(lambda x: s in x.buf, timeout) start = self.buf.find(s) self.buf = self.buf[start:] return def skip_until_re(self, r, flags=0, timeout=None): """ Skip everything until first match of regexp @r, stop before match. Return match. """ r = Bytes(r) match = self.read_cond( lambda x: re.search(r, x.buf, flags=flags), timeout) self.buf = self.buf[match.start():] return match if len(match.groups()) > 1 else match.group(len(match.groups())) def read_until(self, s, timeout=None): """ Read everything until first occurence of string @s, stop after occurence. Return everything before @s, and @s. """ s = Bytes(s) self.read_cond(lambda x: s in x.buf, timeout) end = self.buf.find(s) + len(s) res = self.buf[:end] self.buf = self.buf[end:] return res def read_until_re(self, r, flags=0, timeout=None): """ Read everything until first match of regexp @r, stop after match. Return match. Note: if you need the data before match, you can make group (.*?) for that: r1 = r"(\d) coins" r2 = r"(.*?)(\d coins)" """ r = Bytes(r) match = self.read_cond(lambda x: re.search(r, x.buf, flags=flags), timeout) self.buf = self.buf[match.end():] return match if len(match.groups()) > 1 else match.group(len(match.groups())) def read_nbytes(self, n, timeout=None): self.read_cond(lambda x: len(x.buf) >= n, timeout) self.buf, res = self.buf[n:], self.buf[:n] return res def read_cond(self, cond, timeout=None): """ Read bytes while @cond(self) is False. Return @cond return. self.buf should be analyzed/cropped by caller, if needed (this is rather low-level function, helper) """ time_start = time() remaining = timeout if timeout is None: timeout = self.timeout if self.eof and not self.buf: raise EOFError("Connection closed") res = cond(self) while not res: self._fill_one(remaining) res = cond(self) if res: break if self.eof: raise EOFError("Connection closed") if timeout == -1: remaining = -1 elif timeout == 0: raise Timeout("read_cond timeout") else: remaining = time_start + timeout - time() if remaining <= 0: raise Timeout("read_cond timeout") return res def _fill_one(self, timeout=None): """Read something from socket. timeout = -1 - blocking until read timeout = 0 - non-blocking timeout = N - blocking until read or timeout """ if timeout is None: timeout = self.timeout if timeout == 0: self.sock.setblocking(False) try: self.buf += self.recv(self.RECV_SIZE) except SocketError: # WHAT? pass return if timeout == -1: self.sock.settimeout(None) # blocking, infinity timeout else: self.sock.setblocking(True) # it's overriden by settimeout, but for clarity self.sock.settimeout(timeout) buf = self.recv(self.RECV_SIZE) self.eof = (not buf) self.buf += buf return @property def socket(self): return self.sock @property def fileno(self): return self.sock.fileno() def write(self, s): return self.send(s) def shut_wr(self): self.sock.shutdown(socket.SHUT_WR) def shut_rd(self): self.sock.shutdown(socket.SHUT_RD) def close(self): return self.sock.close() def __del__(self): self.sock.close() def interact_telnet(self): sys.stdout.buffer.write(self.buf) self.buf = b"" t = telnetlib.Telnet() t.sock = self.sock return t.interact() def interact(self): # copied from Telnetlib with minor fixes import selectors _TelnetSelector = selectors.SelectSelector sys.stdout.buffer.write(self.buf) self.buf = b"" with _TelnetSelector() as selector: selector.register(self.sock, selectors.EVENT_READ) selector.register(sys.stdin, selectors.EVENT_READ) while True: for key, events in selector.select(): if key.fileobj is self.sock: try: text = self.read_one() except EOFError: print('*** Connection closed by remote host ***') return if text: sys.stdout.buffer.write(text) sys.stdout.flush() elif key.fileobj is sys.stdin: line = sys.stdin.readline() if not line: return self.send(line)
(*addr, **timeout_dict)
44,542
sock
__del__
null
def __del__(self): self.sock.close()
(self)
44,543
sock
__init__
null
def __init__(self, *addr, **timeout_dict): self.addr = parse_addr(*addr) # python2 does not allow (*args, timeout=None) :( self.timeout = float(timeout_dict.pop("timeout", DEFAULT_TIMEOUT)) if timeout_dict: raise TypeError("Only timeout should be given through keyword args") self.buf = b"" self.eof = False self._init_sock() self._connect() return
(self, *addr, **timeout_dict)
44,544
sock
_connect
null
def _connect(self): return NotImplemented
(self)
44,545
sock
_fill_one
Read something from socket. timeout = -1 - blocking until read timeout = 0 - non-blocking timeout = N - blocking until read or timeout
def _fill_one(self, timeout=None): """Read something from socket. timeout = -1 - blocking until read timeout = 0 - non-blocking timeout = N - blocking until read or timeout """ if timeout is None: timeout = self.timeout if timeout == 0: self.sock.setblocking(False) try: self.buf += self.recv(self.RECV_SIZE) except SocketError: # WHAT? pass return if timeout == -1: self.sock.settimeout(None) # blocking, infinity timeout else: self.sock.setblocking(True) # it's overriden by settimeout, but for clarity self.sock.settimeout(timeout) buf = self.recv(self.RECV_SIZE) self.eof = (not buf) self.buf += buf return
(self, timeout=None)
44,546
sock
_init_sock
null
def _init_sock(self): self.sock = socket.socket(self.SOCKET_FAMILY, self.SOCKET_TYPE) self.sock.settimeout(self.timeout)
(self)
44,547
sock
close
null
def close(self): return self.sock.close()
(self)
44,548
sock
interact
null
def interact(self): # copied from Telnetlib with minor fixes import selectors _TelnetSelector = selectors.SelectSelector sys.stdout.buffer.write(self.buf) self.buf = b"" with _TelnetSelector() as selector: selector.register(self.sock, selectors.EVENT_READ) selector.register(sys.stdin, selectors.EVENT_READ) while True: for key, events in selector.select(): if key.fileobj is self.sock: try: text = self.read_one() except EOFError: print('*** Connection closed by remote host ***') return if text: sys.stdout.buffer.write(text) sys.stdout.flush() elif key.fileobj is sys.stdin: line = sys.stdin.readline() if not line: return self.send(line)
(self)
44,549
sock
interact_telnet
null
def interact_telnet(self): sys.stdout.buffer.write(self.buf) self.buf = b"" t = telnetlib.Telnet() t.sock = self.sock return t.interact()
(self)