SpectralCoordinate.h

Classes

SpectralCoordinate -- Interconvert pixel and frequency values. (full description)

class SpectralCoordinate : public Coordinate

Interface

Public Members
SpectralCoordinate()
SpectralCoordinate(MFrequency::Types type, Double f0, Double inc, Double refPix, Double restFrequency = 0.0)
SpectralCoordinate(MFrequency::Types type, const type<Double>& f0, const type<Double>& inc, Double refPix, const type<Double>& restFrequency = type<Double>(0.0,"Hz"))
SpectralCoordinate(MFrequency::Types type, const Vector<Double> &freqs, Double restFrequency = 0.0)
SpectralCoordinate(MFrequency::Types type, const type<Quantum<Double> >& freqs, const type<Double>& restFrequency = type<Double>(0.0,"Hz"))
SpectralCoordinate(MFrequency::Types freqType, MDoppler::Types velType, const MDoppler<Double>& velocities, const Vector& velUnit, Double restFrequency = 0.0)
SpectralCoordinate(const SpectralCoordinate &other)
SpectralCoordinate &operator=(const SpectralCoordinate &other)
virtual ~SpectralCoordinate()
virtual Coordinate::Type type() const
virtual String showType() const
virtual uInt nPixelAxes() const
virtual uInt nWorldAxes() const
Bool setReferenceConversion (MFrequency::Types type, const MEpoch& epoch, const MEpoch& position, const MPosition& direction)
void getReferenceConversion (MFrequency::Types& type, MEpoch& epoch, MEpoch& position, MPosition& direction) const
virtual Bool toWorld(Vector<Double> &world, const Vector<Double> &pixel) const
virtual Bool toPixel(Vector<Double> &pixel, const Vector<Double> &world) const
Bool toWorld(Double& world, const Double& pixel) const
Bool toPixel(Double& pixel, const Double& world) const
Bool toWorld(MFrequency &world, Double pixel) const
Bool toPixel(Double& pixel, const MFrequency &world) const
Bool toWorld(MVFrequency &world, Double pixel) const
Bool toPixel(Double& pixel, const MVFrequency &world) const
virtual Bool toWorldMany(Matrix<Double>& world, const Matrix<Double>& pixel, Vector<Bool>& failures) const
virtual Bool toPixelMany(Matrix<Double>& pixel, const Matrix<Double>& world, Vector<Bool>& failures) const
virtual void makePixelRelative (Vector<Double>& pixel) const
virtual void makePixelAbsolute (Vector<Double>& pixel) const
virtual void makeWorldRelative (Vector<Double>& world) const
virtual void makeWorldAbsolute (Vector<Double>& world) const
Bool setVelocity (const String& velUnit=String("km/s"), MDoppler::Types velType=MDoppler::RADIO)
MDoppler::Types velocityDoppler () const
String velocityUnit () const
Bool pixelToVelocity (Quantum<Double>& velocity, Double pixel) const
Bool pixelToVelocity (Double& velocity, Double pixel) const
Bool pixelToVelocity (Vector<Double>& velocity, const Vector<Double>& pixel) const
Bool frequencyToVelocity (Quantum<Double>& velocity, Double frequency) const
Bool frequencyToVelocity (Quantum<Double>& velocity, const MFrequency& frequency) const
Bool frequencyToVelocity (Quantum<Double>& velocity, const MVFrequency& frequency) const
Bool frequencyToVelocity (Double& velocity, Double frequency) const
Bool frequencyToVelocity (Vector<Double>& velocity, const Vector<Double>& frequency) const
Bool velocityToPixel (Double& pixel, Double velocity) const
Bool velocityToPixel (Vector<Double>& pixel, const Vector<Double>& velocity) const
Bool velocityToFrequency (Double& frequency, Double velocity) const
Bool velocityToFrequency (Vector<Double>& frequency, const Vector<Double>& velocity) const
Double restFrequency() const
const Vector<Double>& restFrequencies() const
Bool setRestFrequency(Double newFrequency, Bool append=False)
void setRestFrequencies(const Vector<Double>& newFrequencies, uInt which=0, Bool append=False)
void selectRestFrequency(uInt which)
void selectRestFrequency(Double frequency)
String formatRestFrequencies () const
MFrequency::Types frequencySystem() const
void setFrequencySystem(MFrequency::Types type)
virtual Vector<String> worldAxisNames() const
virtual Vector<Double> referencePixel() const
virtual Matrix<Double> linearTransform() const
virtual Vector<Double> increment() const
virtual Vector<Double> referenceValue() const
virtual Bool setWorldAxisNames(const Vector<String> &names)
virtual Bool setReferencePixel(const Vector<Double> &refPix)
virtual Bool setLinearTransform(const Matrix<Double> &xform)
virtual Bool setIncrement(const Vector<Double> &inc)
virtual Bool setReferenceValue(const Vector<Double> &refval)
Vector<Double> pixelValues() const
Vector<Double> worldValues() const
virtual Bool setWorldAxisUnits(const Vector<String> &units)
virtual Vector<String> worldAxisUnits() const
virtual Bool setWorldMixRanges (const IPosition& shape)
virtual void setDefaultWorldMixRanges ()
virtual Vector<Double> worldMixMin () const
virtual Vector<Double> worldMixMax () const
virtual Bool near(const Coordinate& other, Double tol=1e-6) const
virtual Bool near(const Coordinate& other, const Vector<Int>& excludeAxes, Double tol=1e-6) const
virtual Coordinate* makeFourierCoordinate (const Vector<Bool>& axes, const Vector<Int>& shape) const
virtual String format(String& unit, Coordinate::formatType format, Double worldValue, uInt worldAxis, Bool isAbsolute=True, Bool showAsAbsolute=True, Int precision=-1)
String formatUnit () const
Bool setFormatUnit (const String& unit)
void toFITS(RecordInterface &header, uInt whichAxis, LogIO &logger, Bool oneRelative=True, Bool preferVelocity=True, Bool opticalVelDef=True) const
static Bool fromFITS(SpectralCoordinate &out, String &error, const RecordInterface &header, uInt whichAxis, LogIO &logger, Bool oneRelative=True)
virtual Bool save(RecordInterface &container, const String &fieldName) const
static SpectralCoordinate* restore(const RecordInterface &container, const String &fieldName)
virtual Coordinate *clone() const
Private Members
Int makeConversionMachines (MFrequency::Types type, MFrequency::Types conversionType, const MEpoch& epoch, const MEpoch& position, const MPosition& direction)
void makeVelocityMachine (const String& velUnit, MDoppler::Types velType, const String& freqUnit, MFrequency::Types freqType, Double restFreq)
void deleteVelocityMachine ()
void deleteConversionMachines ()
void checkFormat(Coordinate::formatType& format, const Bool ) const
void convertTo (Vector<Double>& world) const
void convertFrom (Vector<Double>& world) const
void convertToMany (Matrix<Double>& world) const
void convertFromMany (Matrix<Double>& world) const
void updateVelocityMachine (const String& velUnit, MDoppler::Types velType)

Description

Review Status

Reviewed By:
Peter Barnes
Date Reviewed:
1999/12/24
Programs:
Tests:

Prerequisite

Synopsis

This class performs the mapping from pixel to frequency.

Caution All pixels coordinates are zero relative.

Example

Let us make a linear SpectralCoordinate first
   Double restfreq = 1.420405752E9;
   Double crpix = 10.0;
   Double crval = 1.4e9;
   Double cdelt = 1.0e6;
   SpectralCoordinate sc(MFrequency::TOPO, crval, cdelt, crpix, restfreq);

   Double world, pixel;
   pixel = 12.1;
   if (!sc.toWorld(world, pixel)) {
      cerr << "Error : " << sc.errorMessage() << endl;
   } else {
      cerr << "pixel, world = " << pixel << ", " << world << endl;
   }

Example

Now we make a non-linear SpectralCoordinate
   Vector<Double> freqs(5);
   freqs(0) = 1.4e9; freqs(1) = 1.41e9;
   freqs(2) = 1.43e9; freqs(3) = 1.44e9;
   freqs(4) = 1.47e9;
   SpectralCoordinate sc(MFrequency::LSRK, freqs, restfreq);

   Double world, pixel;
   world = 1.42e9;
   if (!sc.toPixel(pixel, world)) {
      cerr << "Error : " << sc.errorMessage() << endl;
   } else {
      cerr << "world, pixel = " << world << ", " << pixel << endl;
   }

Motivation

Spectral-line astronomy requires a specialized SpectralCoordinate.

To Do

Member Description

SpectralCoordinate()

Default constructor. It is equivalent to doing SpectralCoordinate(MFrequency::TOPO, 0.0, 1.0, 0.0)

SpectralCoordinate(MFrequency::Types type, Double f0, Double inc, Double refPix, Double restFrequency = 0.0)

Create a linear frequency axis SpectralCoordinate f0 is the frequency of the reference pixel, inc is the pixel increment, refPix is the reference pixel. You can optionally store the rest frequency for later use in calculating radial velocities. Use 0 for restFrequency if continuum.

Frequencies and increments initially in Hz.

SpectralCoordinate(MFrequency::Types type, const type<Double>& f0, const type<Double>& inc, Double refPix, const type<Double>& restFrequency = type<Double>(0.0,"Hz"))

Create linear frequency axis SpectralCoordinate with Quantum-based interface. Parameters are the same as above. Regardless of the units of the Quanta, the initial units of the SpectralCoordinate will be Hz. You can change it to something else with the setWorldAxisUnits method later if you want. Use 0 for restFrequency if continuum.

SpectralCoordinate(MFrequency::Types type, const Vector<Double> &freqs, Double restFrequency = 0.0)

Construct a SpectralCoordinate with the specified frequencies (in Hz). This axis can be nonlinear; the increments and related functions return the average values (calculated from the first and last pixel's frequencies).

A linear interpolation/extrapolation is used for pixels which are not supplied. The reference pixel is chosen to be 0. The frequencies must increase or decrease monotonically (otherwise the toPixel lookup would not be possible). Use 0 for restFrequency if continuum.

SpectralCoordinate(MFrequency::Types type, const type<Quantum<Double> >& freqs, const type<Double>& restFrequency = type<Double>(0.0,"Hz"))

Construct a SpectralCoordinate with the specified frequencies with Quantum-based interface. Parameters are the same as above. Regardless of the units of the Quanta, the initial units of the SpectralCoordinate will be Hz. Use 0 for restFrequency if continuum.

SpectralCoordinate(MFrequency::Types freqType, MDoppler::Types velType, const MDoppler<Double>& velocities, const Vector& velUnit, Double restFrequency = 0.0)

Construct a SpectralCoordinate with the specified velocities (in km/s). They will be converted to Hz and the SpectralCoordinate constructed. This axis can be nonlinear; the increments and related functions return the average values (calculated from the first and last pixel's frequencies).

A linear interpolation/extrapolation is used for pixels which are not supplied. The reference pixel is chosen to be 0. The velocities must increase or decrease monotonically (otherwise the toPixel lookup would not be possible).

SpectralCoordinate(const SpectralCoordinate &other)

Copy constructor (copy semantics).

SpectralCoordinate &operator=(const SpectralCoordinate &other)

Assignment (copy semantics).

virtual ~SpectralCoordinate()

Destructor.

virtual Coordinate::Type type() const

Always returns Coordinate::SPECTRAL.

virtual String showType() const

Always returns the String "Spectral".

virtual uInt nPixelAxes() const
virtual uInt nWorldAxes() const

Always returns 1.

Bool setReferenceConversion (MFrequency::Types type, const MEpoch& epoch, const MEpoch& position, const MPosition& direction)
void getReferenceConversion (MFrequency::Types& type, MEpoch& epoch, MEpoch& position, MPosition& direction) const

Set extra conversion layer. Whenever a conversion from pixel to world is done, the world value is then further converted to this MFrequency::Types value. For example, your SpectralCoordinate may be defined in LSRK. You can use this to get the world values out in say BARY. You must specify the position on earth, the epoch and the direction for the conversions and it is your responsibility to ensure they are viable. Similarly, whenever you convert from world to pixel, the world value is assumed to be that appropriate to the setReferenceConversion type. It is first converted to the MFrequency::Types with which the SpectralCoordinate was constructed and from there to pixel. If you don't call this function, or you set the same type for which the SpectralCoordinate was constructed, no extra conversions occur. Some conversions will fail. These are the ones that require extra frame information (radial velocity) such as to REST. This will be added later. In this case this function returns False (and the conversion parameters are all left as they were), else it returns True.

virtual Bool toWorld(Vector<Double> &world, const Vector<Double> &pixel) const
virtual Bool toPixel(Vector<Double> &pixel, const Vector<Double> &world) const
Bool toWorld(Double& world, const Double& pixel) const
Bool toPixel(Double& pixel, const Double& world) const

Convert a pixel to a world coordinate or vice versa. Returns True if the conversion succeeds, otherwise it returns False and errorMessage() contains an error message. The input vectors must be of length one and the output vectors are resized if they are not already of length one.

Bool toWorld(MFrequency &world, Double pixel) const
Bool toPixel(Double& pixel, const MFrequency &world) const
Bool toWorld(MVFrequency &world, Double pixel) const
Bool toPixel(Double& pixel, const MVFrequency &world) const

Convert a pixel (channel number) into an MFrequency or MVFrequency and vice versa. Usually you will do this for calculating velocities or converting frequencies from one frame to another.

virtual Bool toWorldMany(Matrix<Double>& world, const Matrix<Double>& pixel, Vector<Bool>& failures) const
virtual Bool toPixelMany(Matrix<Double>& pixel, const Matrix<Double>& world, Vector<Bool>& failures) const

Batch up a lot of transformations. The first (most rapidly varying) axis of the matrices contain the coordinates. Returns False if any conversion failed and errorMessage() will hold a message. The failures array (True for fail, False for success) is the length of the number of conversions and holds an error status for each conversion.

virtual void makePixelRelative (Vector<Double>& pixel) const
virtual void makePixelAbsolute (Vector<Double>& pixel) const
virtual void makeWorldRelative (Vector<Double>& world) const
virtual void makeWorldAbsolute (Vector<Double>& world) const

Make absolute coordinates relative and vice-versa (with respect to the reference value). Vectors must be length nPixelAxes() or nWorldAxes() or memory access errors will occur

Bool setVelocity (const String& velUnit=String("km/s"), MDoppler::Types velType=MDoppler::RADIO)
MDoppler::Types velocityDoppler () const
String velocityUnit () const

Set the state that is used for conversions from pixel and frequency to velocity. The SpectralCoordinate is constructed with MDoppler::RADIO and km/s as the velocity conversion state. The functions in this class which use this state are those that convert to or from velocity. Also, function format uses the Doppler state set here. If the function returns False it means the unit was not valid. There will be an error message in function errorMessage

Bool pixelToVelocity (Quantum<Double>& velocity, Double pixel) const
Bool pixelToVelocity (Double& velocity, Double pixel) const
Bool pixelToVelocity (Vector<Double>& velocity, const Vector<Double>& pixel) const
Bool frequencyToVelocity (Quantum<Double>& velocity, Double frequency) const
Bool frequencyToVelocity (Quantum<Double>& velocity, const MFrequency& frequency) const
Bool frequencyToVelocity (Quantum<Double>& velocity, const MVFrequency& frequency) const
Bool frequencyToVelocity (Double& velocity, Double frequency) const
Bool frequencyToVelocity (Vector<Double>& velocity, const Vector<Double>& frequency) const

Functions to convert to velocity (uses the current active rest frequency). There is no reference frame change but you can specify the velocity Doppler and the output units of the velocity with function setVelocity. When the input is a frequency stored as a Double it must be in the current units of the SpectralCoordinate.

Note that the extra conversion layer (see function setReferenceConversion) is active in the pixelToVelocity functions (because internally the use toWorld) but not in the frequencyToVelocity functions.

Bool velocityToPixel (Double& pixel, Double velocity) const
Bool velocityToPixel (Vector<Double>& pixel, const Vector<Double>& velocity) const
Bool velocityToFrequency (Double& frequency, Double velocity) const
Bool velocityToFrequency (Vector<Double>& frequency, const Vector<Double>& velocity) const

Functions to convert from velocity (uses the current active rest frequency). There is no reference frame change but you can specify the velocity Doppler and the output units of the velocity with function setVelocity. When the input is a frequency stored as a Double it must be in the current units of the SpectralCoordinate.

Note that the extra conversion layer (see function setReferenceConversion) is active in the pixelToVelocity functions (because internally the use toPixel) but not in the frequencyToVelocity functions.

Double restFrequency() const
const Vector<Double>& restFrequencies() const
Bool setRestFrequency(Double newFrequency, Bool append=False)
void setRestFrequencies(const Vector<Double>& newFrequencies, uInt which=0, Bool append=False)
void selectRestFrequency(uInt which)
void selectRestFrequency(Double frequency)
String formatRestFrequencies () const

The SpectralCoordinate can maintain a list of rest frequencies (e.g. multiple lines within a band). However, only one of them is active (e.g. for velocity conversions) at any one time. Function restFrequency returns that frequency. Function restFrequencies returns all of the possible restfrequencies.

When you construct the SpectralCoordinate, you give it one rest frequency and it is the active one. Thereafter you can add a new restfrequency with function setRestFrequency (append=True) and that frequency will become the active one. With this function and append=False, the current active restfrequency will be replaced by the one you give.

You can change the list of restfrequencies with function setRestFrequencies. When you do so, you can either replace the list of rest frequencies or append to it. You specify which frequency of the new (appended) list becomes active.

You can also select the active rest frequency either by an index into the current list (exception if out of range) given by restFrequencies() or by the value in the list nearest to the frequency you give.

Whenever you change the active rest frequency, the class internals are adjusted (e.g. the velocity machine is updated).

MFrequency::Types frequencySystem() const
void setFrequencySystem(MFrequency::Types type)

Retrieve/set the frequency system. Note that setting the frequency system just changes the internal value of the frequency system. In addition, it will reset the internal conversion frequency system to the new type and delete any conversion machines.

virtual Vector<String> worldAxisNames() const
virtual Vector<Double> referencePixel() const
virtual Matrix<Double> linearTransform() const
virtual Vector<Double> increment() const
virtual Vector<Double> referenceValue() const

Report the value of the requested attribute.

virtual Bool setWorldAxisNames(const Vector<String> &names)
virtual Bool setReferencePixel(const Vector<Double> &refPix)
virtual Bool setLinearTransform(const Matrix<Double> &xform)
virtual Bool setIncrement(const Vector<Double> &inc)
virtual Bool setReferenceValue(const Vector<Double> &refval)

Set the value of the requested attribute. Note that these just change the internal values, they do not cause any recomputation.

Vector<Double> pixelValues() const
Vector<Double> worldValues() const

Get the table, i.e. the pixel and world values. The length of these Vectors will be zero if this axis is pure linear (i.e. if the channel and frequencies are related through an increment and offset).

virtual Bool setWorldAxisUnits(const Vector<String> &units)
virtual Vector<String> worldAxisUnits() const

Set/get the unit. Adjust the increment and reference value by the ratio of the old and new units. The unit must be compatible with frequency.

virtual Bool setWorldMixRanges (const IPosition& shape)
virtual void setDefaultWorldMixRanges ()
virtual Vector<Double> worldMixMin () const
virtual Vector<Double> worldMixMax () const

Set the world min and max ranges, for use in function toMix, for a lattice of the given shape (for this coordinate). The implementation here gives world coordinates dangling 25% off the edges of the image. The output vectors are resized. Returns False if fails (and then setDefaultWorldMixRanges generates the ranges) with a reason in errorMessage(). The setDefaultWorldMixRanges function gives you [-1e99->1e99].

virtual Bool near(const Coordinate& other, Double tol=1e-6) const
virtual Bool near(const Coordinate& other, const Vector<Int>& excludeAxes, Double tol=1e-6) const

Comparison function. Any private Double data members are compared with the specified fractional tolerance. Don't compare on the specified axes in the Coordinate. If the comparison returns False, errorMessage() contains a message about why.

virtual Coordinate* makeFourierCoordinate (const Vector<Bool>& axes, const Vector<Int>& shape) const

Find the Coordinate for when we Fourier Transform ourselves. This pointer must be deleted by the caller. Axes specifies which axes of the Coordinate you wish to transform. Shape specifies the shape of the image associated with all the axes of the Coordinate. Currently the output reference pixel is always shape/2.

virtual String format(String& unit, Coordinate::formatType format, Double worldValue, uInt worldAxis, Bool isAbsolute=True, Bool showAsAbsolute=True, Int precision=-1)

Format a SpectralCoordinate coordinate world value nicely through the common format interface. See Coordinate for basics.

The world value must always be given in native frequency units. Use argument unit to determine what it will be converted to for formatting. If unit is given, it must be dimensionally consistent with Hz or m/s. If you give a unit consistent with m/s then the appropriate velocity Doppler type is taken from that set by function setVelocity. There is no frame conversion. If unit is empty, the unit given by setFormatUnit is used. If this is turn empty, then native units are used.

String formatUnit () const
Bool setFormatUnit (const String& unit)

Set the default formatter unit (which is initialized to empty). Must be consistent with Hz or km/s. If the given unit is illegal, False is returned and the internal state unchanged. This unit is used by the function format when the given unit is empty.

void toFITS(RecordInterface &header, uInt whichAxis, LogIO &logger, Bool oneRelative=True, Bool preferVelocity=True, Bool opticalVelDef=True) const
static Bool fromFITS(SpectralCoordinate &out, String &error, const RecordInterface &header, uInt whichAxis, LogIO &logger, Bool oneRelative=True)

Convert to and from a FITS header record. When writing the FITS record, the fields "ctype, crval, crpix", and "cdelt" must already be created. Other header words are created as needed. Use oneRelative=True to convert zero-relative SpectralCoordinate pixel coordinates to one-relative FITS coordinates, and vice-versa. If preferVelocity=False the primary axis type will be Frequency, else velocity. For a velocity axis, if opticalVelDef=False, the radio velocity definition will be used, else optical definition.

virtual Bool save(RecordInterface &container, const String &fieldName) const

Save the SpectralCoordinate into the supplied record using the supplied field name. The field must not exist, otherwise False is returned.

static SpectralCoordinate* restore(const RecordInterface &container, const String &fieldName)

Recover the SpectralCoordinate from a record. A null pointer means that the restoration did not succeed.

virtual Coordinate *clone() const

Make a copy of the SpectralCoordinate using new. The caller is responsible for calling delete.

Int makeConversionMachines (MFrequency::Types type, MFrequency::Types conversionType, const MEpoch& epoch, const MEpoch& position, const MPosition& direction)

They are only private so we can save their state

Set up pixel<->world conversion machines Returns: 3 (machines were noOPs, machines deleted) 2 (types the same, machines deleted), 1 (machines created and functioning) -1 (machines could not make trial conversion, machines deleted)

void makeVelocityMachine (const String& velUnit, MDoppler::Types velType, const String& freqUnit, MFrequency::Types freqType, Double restFreq)

Create velocity<->frequency machine

void deleteVelocityMachine ()

Deletes and sets pointer to 0

void deleteConversionMachines ()

Deletes and sets pointers to 0

void checkFormat(Coordinate::formatType& format, const Bool ) const

Format checker

void convertTo (Vector<Double>& world) const

Convert to and from conversion type

void convertFrom (Vector<Double>& world) const

void convertToMany (Matrix<Double>& world) const

void convertFromMany (Matrix<Double>& world) const

void updateVelocityMachine (const String& velUnit, MDoppler::Types velType)

Update Velocity Machine