World-coordinate systems
An array of a FITS file can have a description of the world coordinates of its pixels, which is the keywords of Sect. 8 of the FITS Standard 4.0 and of the papers of the world-coordinate systems (WCS) that it incorporates. A header can have more than one description: the primary description, and the alternative descriptions, which have a letter from A to Z (Sect. 8.2.1). When an HDU is made, e.g., when it is read, a WCS is made for each version of the header, with the default values of the standard, and checked. A WCS has the keywords as its fields, so that it does not need the cards, and the transformation of the pixel coordinates to the world coordinates, and its inverse. hdu.wcs is a NamedTuple of them: hdu.wcs.default is the primary description, and hdu.wcs.A to hdu.wcs.Z are the alternative descriptions. It is empty if the header has no description, and it does not have default if the header has only alternative descriptions. wcsversions says which versions a header has.
julia> hdu = fits("image.fits")[1];
julia> hdu.wcs
(default = WCS(version ' ', 2 axes: "RA---TAN", "DEC--TAN"), A = WCS(version 'A', 2 axes: "GLON-CAR", "GLAT-CAR"))
julia> w = hdu.wcs.default;
julia> w.ctype, w.crpix
(["RA---TAN", "DEC--TAN"], [256.0, 257.0])
julia> linearmatrix(w) # the matrix of the pixel to the intermediate world coordinates
2×2 Matrix{Float64}:
-0.003 -0.0
0.0 0.003
julia> hdu.wcs.A.ctype
2-element Vector{String}:
"GLON-CAR"
"GLAT-CAR"The header is always read: a value that is not of its type is a warning, and the keyword has its default value, and a value that does not satisfy the standard is a warning, and is read as it is. A version that cannot be made, e.g., a projection that has no parameter, is a warning, and is not in hdu.wcs. The same checks are made when an HDU is written, where a problem is an error, unless the keyword check of write is false. The WCSs are made from the cards of the HDU when the HDU is made, and they are not made again if the cards are changed afterward.
The transformation
A WCS is the transformation of the pixel coordinates of an array to its world coordinates, made with WorldCoordinates, which has no knowledge of FITS, and the inverse transformation (inverse). The pixel coordinates are the FITS pixel coordinates, so the first pixel of an axis is 1.0, as the first index of an array is. A position that the transformation does not define is NaN, and not an error, as it is in WCSLIB: a pixel outside of the projection (the boundary of the sphere, the empty corners of a cube, a polar facet), a position that the projection cannot see (the far side of the sphere for TAN), a latitude of more than 90°, a value that is not a number or is infinite, and a pixel outside of a -TAB table or of a lookup of a distortion. The transformation does not change the data array: it gives the world coordinates of a position, e.g., of a pixel.
julia> w = hdu.wcs.default;
julia> w([1.0, 2.0]) # the pixel coordinates to the world coordinates
2-element Vector{Float64}:
47.50326377236703
62.79511082956175
julia> inverse(w)(w([1.0, 2.0])) # and back
2-element Vector{Float64}:
1.0000000000034
2.0000000000045
julia> hdu.wcs.A([1.0, 2.0]) # the alternative description A
2-element Vector{Float64}:
10.0
0.0Writing
The cards of a header are the written form of the descriptions of an HDU, as the keywords of the data are the written form of its format. So a WCS can be made without a header, from its keywords, and given to an HDU with the keyword wcs. Its cards (see wcscards) are made when the HDU is written, and they are not in the cards of the HDU.
julia> w = WCS(ctype = ["RA---TAN", "DEC--TAN"], crpix = [256.0, 257.0],
cdelt = [-0.003, 0.003], crval = [45.83, 63.57], lonpole = 180.0);
julia> hdu = HDU(Image, data; wcs = w);
julia> hdu.wcs.default === w
true
julia> write("image.fits", [HDU(), hdu]); # WCSAXES, CTYPE1, CRPIX1, ... are writtenA version that is given replaces the cards of that version of the header, at the place of the first of them, and the cards of the other versions stay. A version that an HDU has from its cards is written with them, as they are, if they describe the same keywords as its WCS, so a file that is read is written as it is. The cards that are made are checked as the cards of a header are, which is an error unless the keyword check of write is false. The deprecated CROTAi is not written.
The linear transformation of the pixel to the intermediate world coordinates is made from CRPIX, CDELT, and the PC or CD matrix, or the CROTA of an old header, the world coordinates of a linear axis are the intermediate coordinates and CRVAL, and a celestial pair of axes, e.g., RA---TAN and DEC--TAN, is the projection of the algorithm code of CTYPE (Table 23 of the standard), which has the parameters PVim, and the rotation of the native coordinates to the celestial coordinates, whose parameters are CRVAL, LONPOLE, LATPOLE, and PVim.
Spectral axes
A spectral axis has a coordinate type of Table 25 of the standard (FREQ, ENER, WAVN, VRAD, WAVE, VOPT, ZOPT, AWAV, VELO, or BETA) and, if it is not linear, an algorithm code of Table 26 (Greisen et al. 2006). FREQ-W2F is a frequency that is regularly sampled in wavelength, and WAVE-LOG is a wavelength that is regularly sampled in its logarithm. The axis is linearly sampled in the variable of the first letter of the code, X, which is converted to the variable of the third letter, P, that the type of the coordinate is linearly related to, and then to the coordinate (Eqs. 42 to 47 of the paper), with the speed of light and the rest frequency or wavelength, RESTFRQ or RESTWAV, which the conversions of velocities need. The reference value CRVAL, the increment CDELT (or the matrix), and the world coordinates are in the units of CUNIT, whose prefixes and alternative units are converted, e.g., MHz, Angstrom, km/s, and eV, and the dimensionless types, ZOPT and BETA, are not scaled.
julia> w = hdu.wcs.default; # CTYPE1 = 'FREQ-W2F', CUNIT1 = 'MHz', CRVAL1 = 1400.0, CDELT1 = 10.0
julia> w([3.0]) # the frequency of pixel 3, in MHz
1-element Vector{Float64}:
1420.2898550724638The detector algorithms -GRI, for a grating, prism, or grism that is operated in vacuum, and -GRA, for one that is operated in dry air, are valid for any coordinate type of Table 25 (Sect. 5 of the paper). The parameters of the disperser are the keywords PVi_m of the spectral axis i (Table 6 of the paper): the grating density PVi_0 (m⁻¹), the order PVi_1, the angle of incidence PVi_2 (degrees), the index of refraction PVi_3 (default 1) and its derivative PVi_4 (m⁻¹), the angle of the normal to the dispersion plane PVi_5 (degrees), and the angle of the detector PVi_6 (degrees). The coordinate is converted from the wavelength of the grism equation as above, and a disperser that has no dispersion is a warning, and the axis is linear.
julia> w = hdu.wcs.default; # CTYPE1 = 'AWAV-GRA', CUNIT1 = 'Angstrom', CRVAL1 = 5225.2, CRPIX1 = 1801.7,
# CDELT1 = -0.4334, PV1_0 = 3.16e5, PV1_1 = 1, PV1_2 = 13.9 (Fig. 3 of the paper)
julia> w([1.0]) # the first pixel, which is not 5225.2 + 0.4334 * 1800.7 = 6005.7 of the linear axis
1-element Vector{Float64}:
6006.11140235981A code or a coordinate type that is not valid for the other, e.g., FREQ-F2W, whose coordinate is related to frequency and not to wavelength, an axis that needs a rest frequency or wavelength that the header does not have, and a unit that is not one of the type, are warnings, and the axis is linear. The other conventions of the distortions, e.g., the -SIP suffix of another projection, and the conversions of the spectral reference frames (SPECSYS) are not implemented yet, and the axes are linear, with a warning.
Reference frames of spectral axes
The spectral coordinate of an image is that of a frame of rest, SPECSYS (Table 12 of Paper III: TOPOCENT, GEOCENTR, BARYCENT, HELIOCEN, LSRK, LSRD, GALACTOC, LOCALGRP, CMBDIPOL, and SOURCE), and the plane of the image has the frequency of the frame of the observation, SSYSOBS (TOPOCENT if it is not given), over the field, so the coordinate of a pixel in the frame of SPECSYS differs from the one that the reference value and the increment give by the Doppler shift of the two frames in the direction of the pixel (Sect. 7 and 10.2 of Paper III). reframe is the WCS of the spectral coordinate in another frame, which applies that shift to every pixel:
julia> w = hdu.wcs.default; # CTYPE3 = 'VRAD', SPECSYS = 'TOPOCENT', OBSGEO-X, -Y, and -Z, DATE-OBS = '2020-06-01T00:00:00'
julia> lsrk = reframe(w, "LSRK"); # the radio velocity in the kinematic local standard of rest
julia> world = lsrk([10.0, 20.0, 5.0]); # the right ascension, the declination, and the radio velocity of the pixel in the frame
julia> inverse(lsrk)(world) # the pixel of the world coordinates in the frame
3-element Vector{Float64}:
10.0
20.0
5.0The frequency of the frame is that of the special relativistic Doppler shift, ν_F = ν γ_F (1 + v_F · n / c), of the velocity v_F of the frame with respect to the barycenter, in the ICRS, for the unit vector n of the direction of the pixel, so the ratio of the frequency in two frames is that of their factors, whatever the velocities are. The velocity of the Earth, and of the Sun, which the heliocentric frame has, are those of the SOFA ephemeris (epv00), and the topocentric frame adds the rotation of the Earth at the observatory (OBSGEO-X, -Y, and -Z), with the Earth rotation angle and the celestial-to-intermediate matrix of SOFA, for the date of the observation (MJD-AVG, DATE-AVG, MJD-OBS, or DATE-OBS, in the time scale TIMESYS, which is UTC if it is not given, and the UTC is the UT1, which differs by less than a second, and the polar motion is not applied: the error is a meter per second at most). The velocities of the other frames are those of Table 12 with respect to the Sun: the kinematic local standard of rest is 20 km/s toward 18h03m50.29s +30°00'16.8" (the position 18h +30° of 1900 in J2000), the dynamic one is (9, 12, 7) km/s in galactic Cartesian coordinates (Paper III, erratum 3.1), the Galactic center is 220 km/s from the dynamic one toward l = 90°, b = 0, the Local Group is 300 km/s toward the same direction, and the cosmic microwave background dipole is 368 km/s toward l = 263.85°, b = 48.25° with respect to the barycenter. The frame SOURCE is the one of the redshift ZSOURCE in the frame SSYSSRC, which has the frequency 1 + z times that of the frame. The celestial axes are equatorial (RA and DEC of the ICRS, or of FK5 of the equinox 2000) or galactic (GLON and GLAT). All of the spectral types are shifted through the frequency, with the rest frequency or wavelength of the type, in the units of CUNIT.
The keywords of the result are those of the original WCS, except specsys, and are not those of the transformation, which includes the frame, so its cards cannot be made, and a reframed WCS cannot be given to an HDU.
Time
A time axis (Sect. 9 of the standard, and Paper VII) is an axis whose CTYPE is TIME, or one of the time scales of Table 30 (TAI, TT, TDT, ET, IAT, UT1, UTC, GMT, GPS, TCG, TCB, TDB, and LOCAL), which may have a realization, e.g., TT(TAI); an image has at most one of them in a description. The axis is linear: its world coordinate is the elapsed time since the reference time, in CUNIT or, if the header has none, TIMEUNIT (default s), so CRVAL is not an ISO-8601 time, even if the reference time is DATEREF. timeaxes gives the time axes of a WCS. The coordinates PHASE, TIMELAG, and FREQUENCY (Sect. 9.6) are linear as well, and a phase axis has its zero point CZPHSia and its period CPERIia in the phase of the WCS.
The global keywords of the time, which have no version code, are in the time of the WCS, only if the header has them: TIMESYS (the time scale of the keywords, UTC if it is absent), MJDREF, JDREF, DATEREF, and the parts MJDREFI, MJDREFF, JDREFI, and JDREFF of the reference time, TREFPOS (the position where the times are valid, TOPOCENTER if it is absent, of which only the first three characters count), TREFDIR, PLEPHEM, TIMEUNIT, TIMSYER and TIMRDER (the errors), DATE-BEG, DATE-END, MJD-BEG, MJD-END, TSTART, TSTOP, XPOSURE, TELAPSE, JEPOCH, BEPOCH, the geodetic position of the observatory OBSGEO-B, OBSGEO-L, OBSGEO-H, and OBSORBIT. A value that is not of its type is a warning and is not used, and so is a reference position that is not one of Table 31, a unit that is not one of Table 34, and the TIMEOFFS, TIMEDEL, and TIMEPIXR keywords, which are for tables, in an image. The keywords that are already fields of the WCS, DATE-OBS, MJD-OBS, DATE-AVG, MJD-AVG, and OBSGEO-X, -Y, and -Z, stay where they are. The reference time is the single value if there is one, else the parts, and MJDREF has precedence over JDREF, which has it over DATEREF; timereference gives it in MJD, as the integer part and the fractional part, so that the precision is not lost.
julia> w = hdu.wcs.default; # TIMESYS = 'UTC', MJDREF = 54746.0, CTYPE3 = 'UTC', CUNIT3 = 's', CRVAL3 = 2375.341, CDELT3 = 13.3629
julia> timeaxes(w), timereference(w)
([3], (54746, 0.0))
julia> w([1.0, 1.0, 12.0])[3] # the time of the 12th frame, in seconds since MJD 54746.0
2522.3299Time scales and units
The world coordinate of a time axis is the time since the reference time, which is the same number of days in every scale (Sect. 4.1.2 of Paper VII), in the unit CUNIT, or TIMEUNIT. The scale of the axis is its CTYPE, or TIMESYS (UTC) if CTYPE is TIME. retime is the WCS whose time axis has another scale, and unit: the instant of the elapsed time in the scale of the keywords is converted to the instant in the other scale, and the result is the time of that instant since the same reference time.
julia> w = hdu.wcs.default; # CTYPE3 = 'UTC', CUNIT3 = 's', MJDREF = 58849.0
julia> retime(w; scale = "TT")([1.0, 1.0, 1.0])[3] # TT is 69.184 s ahead of UTC in 2020 (37 s of leap seconds and 32.184 s)
69.184
julia> retime(w; scale = "TDB", unit = "d")([1.0, 1.0, 1.0])[3] # the same instant in days of TDB since the reference timeThe scales are TAI (IAT), TT (TDT, ET), GPS, UTC (GMT), UT1, TCG, TDB, and TCB, with the conversions of SOFA through TAI, and the units are those of Table 34. The leap seconds are those of the release of SOFA, the difference of TDB and TT is the SOFA model for the observatory (OBSGEO-X, -Y, and -Z) or the geocenter, and the UT1 needs UT1 − UTC (dut1, in seconds). LOCAL, which has no relation to the others, is an error.
The reference position of the time (TREFPOS, Table 3 of Paper VII) is where the time is valid, and the time at another position is the time plus the light time between the two along the reference direction: retime(w; position = "BARYCENTER", direction = (ra, dec)) is the time at the barycenter, for TOPOCENTER (the default), GEOCENTER, BARYCENTER, HELIOCENTER, or EMBARYCENTER, whose first three characters are the significant ones, and can be combined with the scale (scale = "TDB", position = "BARYCENTER"). The light time is (r_A - r_B) · n / c for the unit vector n to the source, direction in degrees of the ICRS, or the celestial reference value of the WCS if it is not given (TREFDIR refers to keywords that the WCS does not have), with the positions of the SOFA ephemeris (the Earth and the Sun), its model of the Moon (the Earth-Moon barycenter), and the observatory. The relativistic delay of the Sun, which is microseconds, is not applied. The positions of the other bodies, CUSTOM, RELOCATABLE, and GALACTIC have no position, and are errors.
julia> retime(w; scale = "TDB", position = "BARYCENTER", direction = (83.63, 22.01))([1.0, 1.0, 1.0])[3] # the time at the barycenter, in TDBLike reframe, the keywords of the result are those of the original WCS, except the unit, the scale, and TREFPOS, and are not those of its transformation, so it cannot be given to an HDU.
Table columns
A binary table can have an image in the cell of a column, and a binary or ASCII table can be a list of pixels, whose coordinates are columns (Sect. 8.2 and Table 22 of the standard, and Table 2 of Paper I). Their WCS keywords have the names of the keywords of an image, abbreviated to eight characters, and the column in them. The axis is a digit before the keyword for the image in the cell of a column, and the column n is a number after it: 1CRVL3 is CRVAL1 of the image of column 3, 12PC3 is PC1_2, 2V3_4 and 2PV3_4 are PV2_4, WCAX3 is WCSAXES, and an alternative description, 1CRV3A for CRVAL1A, has the version code in place of a character of the keyword. The coordinates of a pixel list are columns, so the axis is the column: TCRVL3 is CRVAL of the axis that column 3 has, TPC3_5 and TP3_5 are the matrix element of the columns 3 and 5, and TCRV3A is its alternative description. The keywords that are shared by the two, LONPna, LATPna, EQUIna, RADEna, RFRQna, RWAVna, SPECna, SOBSna, SSRCna, VSYSna, ZSOUna, and VANGna, have the column of the image or of an axis, and the name of the description is WCSNna for both, which the pixel list had as TWCSna (Paper I, erratum 3), and the observation keywords OBSGXn, OBSGYn, OBSGZn, DOBSn, MJDOBn, DAVGn, and MJDAn have it too. The time keywords of a column are TRPOSn and TRDIRn, which override TREFPOS and TREFDIR of the header (Sect. 9.2.3 and 9.2.4) in the time of its WCS, and the zero point and the period of a phase axis, iCZPHn and iCPERn (iCZPna and iCPRna of an alternative description) for the image of a column, and TCZPHn and TCPERn (TCZPna and TCPRna) for the pixel list, which are in its phase. The column of a pixel list that TRPOSn and TRDIRn refer to is that of its first time axis, or of its first axis if it has none. The global keywords of the time, TIMEOFFS, TIMEDEL, and TIMEPIXR among them, are those of a table, so they are not a warning, and a pixel list may have more than one time axis, since each column has a time frame. The image of a column may refer to the coordinate system of the image of another column (Paper I, Sect. 3.5): WCSTna of the target has a value that WCSXna of the reference has, and the WCS of the version and the column of the reference has the keywords of the target, so hdu.wcs.noise.A of WCSX4A = 'XREF2' is the WCS of the target WCST2B = 'XREF2' of column 2 with the version 'A' and the column 4, and the cards stay as they are. The reference must not have other keywords of its version, and a target that is itself a reference, a value that no target has, or more than one has, is a warning, and the WCS is not used. A WCS that is given to the column replaces the reference, and its cards, which have no WCSXna, are written.
A keyword of a table may be a column that has its value in each row (the Green Bank convention, Appendix L of the standard), e.g., 1CRVL2, a column of the reference values of the first axis of the images in column 2, which has precedence over the keyword of the header. The images of a column have the values of their row in WCS(hdu, column, row, version = ' '), the column by its number or its name: the keywords of the axes, the matrix, the parameters, and the description, the keywords of the observation and the time (DATE-OBS, OBSGEO-X, TIMESYS, ...), and the array of the parameters iVn_Xa, whose cell has the parameters PVi_0, PVi_1, ... of the axis i of the image in column n. A version that has a keyword that is a column is not a WCS of the header, so it is not in hdu.wcs, which has the WCSs that are the same in every row.
julia> hdu = fits("spectra.fits")[2]; # the column 1CRVL2 has the reference values of the images of the column 2, which is `spec`
julia> WCS(hdu, :spec, 3)([1.0]) # the world coordinates of the first pixel of the image of the third row
1-element Vector{Float64}:
3.0e9hdu.wcs of a table has the WCSs of the pixel list as hdu.wcs.default and hdu.wcs.A, as for an image, whose axes are the columns of the coordinates in order of the column number, so hdu.wcs.default.columns gives them, and a WCS is called with the vector of the values of those columns. The image of a column of a binary table is the NamedTuple of its versions, under the name of the column (TTYPEn, or column and its number if it has none): hdu.wcs.flux.default, and hdu.wcs.flux.A. The cards of a table are written as they were read, unless a WCS differs from its cards.
A WCS is given to a table with the keyword wcs of HDU as hdu.wcs has it, and its cards are made when the table is written, in the form of the keywords of a table: a WCS, or a tuple, vector, or NamedTuple of them, is the pixel list, and the WCSs of the image of a column are under its name, wcs = (; flux = (w, wA)). The columns are the keyword columns of the WCS: the axes of a pixel list, WCS(; columns = [3, 5], ...), which are different and in order, and the column of the image, which the name gives. The cards that are replaced are those of the version, at the place of the first of them, and the others stay. The names have at most eight characters, so a column number that is too long for a keyword, e.g., 2V123_12A, is an error, and so are the distortions (SIP, WAT, and Paper IV), which have no keywords in a table. The reference position and direction of the time are TRPOSn and TRDIRn, the keywords of the observation (OBSGEO-X, DATE-OBS, ...) have the column (OBSGXn, DOBSn, ...), and the other global keywords of the time have one value in a table, so two WCSs that differ in one are an error. wcscards(w; form = FITSFiles.BintableForm(n)) gives the cards of a WCS in the form of the image of column n, and FITSFiles.PixelListForm(columns) that of a pixel list. A form gives the key of a keyword, FITSFiles.key(form, "CTYPE", 1, 0, 'A'), and reads the keyword of a key, parse(form, "1CTY2A"), which is nothing if the key is not a keyword of the form.
julia> w = WCS(; columns = [3, 5], ctype = ["RA---TAN", "DEC--TAN"], crpix = [100.0, 100.0], crval = [80.0, -14.0], cdelt = [-0.001, 0.001]);
julia> hdu = HDU(Bintable, (a = [1], b = [2], x = [3.0], c = [4], y = [5.0]); wcs = w); # TCTYP3 = 'RA---TAN', TCTYP5 = 'DEC--TAN', TCRPX3 = 100, ...julia> hdu = fits("events.fits")[2]; # TCTYP1 = 'RA---TAN', TCTYP2 = 'DEC--TAN', TCRPX1 = 100, TCRVL1 = 80, TCDLT1 = -0.001, ...
julia> hdu.wcs.default.columns
2-element Vector{Int64}:
1
2
julia> hdu.wcs.default([100.0, 100.0]) # the world coordinates of an event with the values (100, 100) of the columns
2-element Vector{Float64}:
80.0
-14.0Table lookup axes
The coordinate of an axis whose CTYPE ends in -TAB, e.g., WAVE-TAB, is not a function of the pixel, but is looked up in a table (Sect. 6 of Paper III), e.g., the wavelengths of a spectrograph that has no simple form. The table is a binary table extension of the same file that has one row, and a column whose only cell is the coordinate array, which has the dimensions (M, K1, ..., KM) for M axes that are not separable, e.g., (1, K) for one axis, with the dimensions in its TDIMn. The keywords of the axis i say which table and column:
| Keyword | Value |
|---|---|
PSi_0 | EXTNAME of the table, which has no default; the name should begin with WCS- |
PVi_1, PVi_2 | EXTVER and EXTLEVEL of the table (default 1) |
PSi_1 | TTYPE of the column of the coordinate array, which has no default, and is the same for the M axes of an array |
PSi_2 | TTYPE of the column of the indexing vector of the axis (default none, so the index is 1, 2, ..., K) |
PVi_3 | the axis m of the array that is the axis i (default 1) |
The index of the axis is ψ = x + CRVAL, where x is the intermediate world coordinate, which is the pixel for the default CRPIX, CDELT, and PC (Eq. 87); the position in the indexing vector is interpolated linearly (Eq. 88), and so is the coordinate in the coordinate array (Eq. 89), which is extrapolated to the edge of the first and last pixels, and not beyond: the coordinate of a pixel that is more than half a pixel outside of the table is not defined, which is NaN, and so is the pixel of a coordinate that is not in the table. inverse(wcs) finds the index of a coordinate by scanning the coordinate array. No unit conversion is made: the coordinates are in the units of the table, which are CUNIT.
The table is in another HDU, which may follow the image, so the HDUs of a file that are read by fits have their -TAB WCSs made when all of the HDUs are read. A WCS that is made apart from the file, or of an HDU that is made in memory, is given the HDUs that have the tables with the keyword tables, and its cards are written with the HDU (see Writing), while the table is an HDU of the file that the user makes. The -TAB axes of the WCS of a table, of the image of a column (1CTYP2 = 'WAVE-TAB', 1S2_0 = 'WCS-TAB', 1S2_1 = 'WAVELENGTH') or of a pixel list (TCTYP3, TS3_0, TS3_1), are made the same way (see Table columns).
julia> table = HDU(Bintable, (WAVELENGTH = [reshape([4000.0, 4010.0, 4025.0, 4045.0], 1, :)],), Card[Card("EXTNAME", "WCS-TAB")]);
julia> w = WCS(; ctype = ["WAVE-TAB"], cunit = ["Angstrom"], crpix = [1.0], crval = [1.0],
ps = [(1, 0) => "WCS-TAB", (1, 1) => "WAVELENGTH"], tables = table);
julia> w([2.5]) # between the wavelengths of the pixels 2 and 3
1-element Vector{Float64}:
4017.5
julia> image = HDU(Image, rand(Float32, 4); wcs = w); # written with CTYPE1, PS1_0, and PS1_1
julia> write("spectrum.fits", [HDU(Primary), image, table]);
julia> fits("spectrum.fits")[2].wcs.default([2.5])
1-element Vector{Float64}:
4017.5A table that the file does not have, or has more than once, a column that the table does not have, a PVi_3 that are not the M axes of the array, and a coordinate array that does not have the dimensions of the axes, are warnings when the HDU is read, and the WCS is not used, and they are errors for the constructor of a WCS. The axes of a binary table or of a pixel list that have their own table-lookup keywords (Sect. 6.1.4) are not implemented yet.
Distortions
The distortion functions of Paper IV (Calabretta et al.) are implemented, and the conventions of a distortion of the celestial axes that are polynomials (SIP, TPV, TNX, and ZPX), and the plate solution of the DSS. None of them is part of the standard.
Paper IV has the keyword CPDISja for a correction of the pixel coordinates of the axis j before the matrix (prior, p' = p + δ(p)), and CQDISia for a correction of the intermediate pixel coordinates q of the axis i after the matrix and before CDELT scales them (sequent, q' = q + δ(q)). The code is Polynomial or Lookup. The parameters are the record-valued keywords DPja and DQia, which are repeated: each has the string 'field-specifier: float', e.g., DP1 = 'NAXES: 2' and DP1 = 'TERM.1.COEFF: 3.0', and the parameters that are not given have the defaults of the paper (Table 3). CPERRja and CQERRia have the largest correction of an axis, and DVERRa the largest error of all of them, in pixels, which are kept and are not used. A distortion that has no NAXES is no correction; the codes Cubic-spline and B-spline (Sects. 3.2 and 3.3), which the paper does not define, are a warning, and are not used. The WCS has them in distortions (see WCS).
Polynomial(Sect. 3.1) is a function ofNAXESpixel coordinates,AXIS.n(defaultn), which are renormalized,p̂ = (p - OFFSET.n) SCALE.n;NAUXauxiliary variablesρk = (AUX.k.COEFF.0 + Σ AUX.k.COEFF.n p̂ₙ^AUX.k.POWER.n)^AUX.k.POWER.0; andNTERMStermsTERM.m.COEFF Π p̂ₙ^TERM.m.VAR.n Π ρk^TERM.m.AUX.k, which is zero if a factor is.Lookup(Sect. 3.4) interpolates linearly in the array of the image extension that has theEXTNAMEWCSDVARRand theEXTVERofEXTVER(default 1), whose axesnare the axesAXIS.nof the image, with its ownCRPIXn,CDELTn, andCRVALn: the pixelwof the image of the array pixelr̂isCRVALn, and the step isCDELTn. The array must cover the image, since the corrections are not extrapolated, so the pixel outside of it is not defined (NaN). The WCSs that have a lookup are made like those of-TAB: when all of the HDUs of a file are read byfits, or with the keywordtablesofWCS.
The inverse of a WCS that has a distortion is found by Newton's method from the pixel that the corrected one is, so a lookup that is inverted near the edge of the array can have an iterate outside of it, which is NaN.
TNX and ZPX (NOAO/IRAF) are the algorithm codes in place of TAN and ZPN, e.g., RA---TNX, which have the linear transformation CDi_j and a correction of the intermediate world coordinates in degrees, ξ' = ξ + lngcor(ξ, η) and η' = η + latcor(ξ, η), before the projection. The surfaces are in the cards WATi_nnn, which are joined in the order of nnn without anything between them, since a number or a name is divided between the cards, to the string name = value pairs: lngcor = "..." of the longitude axis and latcor = "..." of the latitude axis, whose value is the type (1 Chebyshev, 2 Legendre, 3 simple polynomial), the orders in ξ and η, the cross terms (0 none, 1 all, 2 half), the limits ξmin ξmax ηmin ηmax that normalize the variables of the Chebyshev and Legendre polynomials, and the coefficients C_mn with m the faster index; and, for ZPX, projp0 to projp5, the parameters PVi_m of ZPN. An axis that has no surface is not corrected. The cards are kept as they are in the wat of the WCS.
DSS is the plate solution of the Digitized Sky Survey, whose header has no WCS keywords: the geometry of the plate (PPO3, PPO6, XPIXELSZ, YPIXELSZ, CNPIX1, CNPIX2), its center (PLTRAH, PLTRAM, PLTRAS, PLTDECSN, PLTDECD, PLTDECM, PLTDECS), and the coefficients of the polynomials AMDX1 to AMDX13 and AMDY1 to AMDY13 (the color and magnitude terms AMDX14 to AMDX20 are not read). If a header has all of them and no CTYPE, CRPIX, CRVAL, CDELT, PC, or CD keyword, its primary description is the translation of Paper IV (Sect. 5.2): the tangent plane RA---TAN and DEC--TAN of FK5 and the equinox 2000, whose reference pixel, matrix, and scale are those of the linear part of the plate solution, and whose residual terms are two sequent polynomials with auxiliary variables, which is the sequent distortion of Paper IV. The translation is the WCS of the header, so it is not written, and the cards of the plate are kept as they are.
SIP (Shupe et al. 2005) is the suffix -SIP of the coordinate types of both axes of the celestial system, e.g., RA---TAN-SIP and DEC--TAN-SIP. The pixel offsets from the reference pixel, u = p₁ - CRPIX₁ and v = p₂ - CRPIX₂, are corrected before the linear transformation: u + f(u, v) and v + g(u, v), where f and g are the polynomials of the keywords A_p_q and B_p_q of the powers u^p v^q with 2 ≤ p + q ≤ A_ORDER (or B_ORDER), which are zero if the header does not have them. A_ORDER and B_ORDER are required. The keywords have no version code, so every WCS that has the suffix has them, and the sip field of the WCS has them (see WCS). The inverse polynomials AP_p_q and BP_p_q are kept as they are, but are not used: the inverse of the WCS solves for the pixel, which is exact.
TPV is the algorithm code TPV in place of TAN, e.g., RA---TPV. The parameters PVi_m for m from 0 to 39 of the axes are the coefficients of the polynomial of the terms 1, x, y, r, x², xy, y², x³, ..., r⁷, which corrects the intermediate world coordinates (in degrees) before the tangent-plane projection: the polynomial of the latitude axis has x and y the other way round, and the coefficient of x (PVi_1) is 1 if it is not given. The parameters are not those of a projection, so the fiducial point and the pole of the TAN projection have their defaults.
julia> w = hdu.wcs.default; # CTYPE1 = 'RA---TAN-SIP', CTYPE2 = 'DEC--TAN-SIP', A_ORDER = 2, B_ORDER = 2, ...
julia> w([50.0, 200.0]) # the pixel after the polynomials correct it
2-element Vector{Float64}:
45.02621176338889
30.0207463255214FITSFiles.WCS — Type
WCS(; version = ' ', <keywords>)A world-coordinate system (WCS) that is made from its keywords, which have the default values of the standard (Sect. 8), instead of from a header. It is given to an HDU with the keyword wcs of HDU, and its cards are made when the HDU is written (see wcscards).
The keywords are the fields of WCS: name, naxes, ctype, cunit, cname, crpix, crval, cdelt, crder, csyer, pc, cd, pv, ps, sip, wat, distortions, lonpole, latpole, radesys, equinox, restfrq, restwav, specsys, ssysobs, ssyssrc, velosys, zsource, velangl, dverr, obsgeo, mjdobs, dateobs, mjdavg, and dateavg, and the global time keywords time, e.g., time = ["TIMESYS" => "TT", "MJDREF" => 55197.0], and phase. The vectors have an element for each axis, and the number of axes, naxes, is the length of the longest one, if it is not given. pv and ps are dictionaries, or vectors of pairs, whose keys are the tuples of the axis and of the number of the parameter, e.g., pv = [(2, 1) => 45.0] for PV21, sip is the dictionary, or the vector of pairs, of the keywords of the SIP convention, e.g., `sip = ["AORDER" => 2, "A20" => 3.3e-5, "B_ORDER" => 2], andwatis that of the cards of the WAT convention, e.g.,wat = [(1, 1) => "wtype=tnx lngcor = ..."]. The deprecatedcrotais not a keyword. The keywordtablesis the HDU, or the collection of HDUs, that have the coordinate arrays of the axes of the table lookup (-TAB), and the distortion arrays of the lookups of Paper IV.distortionsis a dictionary, or a vector of pairs, of the key,(:prior, j)or(:sequent, i), and aNamedTupleofcode, and optionallyparametersanderror, e.g.,distortions = [(:prior, 1) => (code = "Polynomial", parameters = ["NAXES" => 1, "NTERMS" => 1, "TERM.1.COEFF" => 1e-5, "TERM.1.VAR.1" => 2])]`.
WCS(ctype = ["RA---TAN", "DEC--TAN"], crpix = [256.0, 257.0], cdelt = [-0.003, 0.003], crval = [45.83, 63.57], lonpole = 180.0)WCS(hdu::HDU, column, row, version = ' '; tables = nothing)The WCS of the image in the cell of the row of the column of a binary table (a Bintable), which is given by its number or its name (TTYPEn), for a version. The keywords of the WCS that are columns of the table have their value in the row, and have precedence over the keywords of the header (the Green Bank convention, Appendix L of the standard): a column named as the keyword of the table, e.g., 1CRVL2 for the reference value of the first axis of the image in column 2, and the column 2V3_X of the array of the parameters of the second axis of the image in column 3, whose cell has the parameters PV2_0, PV2_1, ... Their values are those of the observation too: the columns DATE-OBS, OBSGEO-X, TIMESYS, ... A version whose keywords have a value in each row is not in hdu.wcs.
julia> hdu = fits("spectra.fits")[2]; # the column 1CRVL2 has the reference value of the images of the column 2 in each row
julia> WCS(hdu, :flux, 3)([1.0])FITSFiles.wcscards — Function
wcscards(wcs::WCS; form = FITSFiles.ImageForm())The cards of the keywords of a WCS, which an HDU writes when the HDU does not have them as they are, i.e., when the WCS was given to the HDU, or is not the one that its cards describe (Sect. 8). The keywords that have their default values are not written, except WCSAXESa, CRPIXia, CRVALia, and CDELTia, and the keywords of the observation, which do not have the version code (OBSGEO-X, OBSGEO-Y, OBSGEO-Z, DATE-OBS, MJD-OBS, MJD-AVG, and DATE-AVG), are those of every WCS. The deprecated CROTAi is not written, and a WCS that has it is an error: it has the PCi_ja matrix.
The keywords have the names of the form (see FITSFiles.key): the keywords of an image, ImageForm(), the default, the image in the column n of a binary table, BintableForm(n), which is the column of the WCS, and the pixel list, PixelListForm(columns), whose axes are the columns of the WCS (Table 22 of the standard). The SIP, WAT, and Paper IV distortions have no keywords in a table, and the names of the keywords of a table have at most eight characters, so a column number that is too long for one is an error.
wcscards(WCS(; ctype = ["FREQ"], columns = [3]); form = FITSFiles.BintableForm(3)) # 1CTYP3, 1CRPX3, 1CRVL3, 1CDLT3, and WCAX3FITSFiles.reframe — Function
reframe(wcs::WCS, frame)The WCS whose spectral coordinate is that of the reference frame, a value of Table 12 of Paper III ("LSRK", "BARYCENT", "TOPOCENT", ...), instead of its SPECSYS. The coordinates of the pixels of the image are those of the frame of the observation, SSYSOBS (TOPOCENT if it is not given), which have no variation over the image, and the reference value and the increment are those of SPECSYS at the reference point (Sect. 7 and 10.2 of Paper III), so the new frequency, wavelength, or velocity of a pixel is that of the frame, which differs by the Doppler shift of the velocity of the frames in the direction of the celestial coordinates of the pixel, and varies over the field. The inverse of the result is the transformation of the world coordinates in the new frame to the pixels.
The velocity of the Earth and the Sun are those of the SOFA ephemeris, and the frames that are defined with respect to the Sun are those of Table 12 (the velocity of the kinematic local standard of rest, LSRK, is 20 km/s toward 18h03m50.29s +30°00'16.8", that of the dynamic one, LSRD, is (9, 12, 7) km/s in galactic Cartesian coordinates, that of the Galactic center, GALACTOC, is 220 km/s from it, that of the Local Group, LOCALGRP, is 300 km/s toward the same direction, and the cosmic microwave background, CMBDIPOL, is 368 km/s toward l = 263.85°, b = 48.25°). The frames of the Earth and the Sun need the date of the observation, MJD-AVG, DATE-AVG, MJD-OBS, or DATE-OBS, in the time scale TIMESYS (UTC if it is not given), and the topocentric frame needs the position of the observatory, OBSGEO-X, -Y, and -Z. The frame of the source, SOURCE, is that of the redshift ZSOURCE in the frame SSYSSRC. The celestial coordinates are equatorial (ICRS, or FK5 of the equinox 2000) or galactic. The frequency, and the velocity, are those of the special relativistic Doppler shift.
The keywords of the result are those of the WCS, except specsys, and not those of its transformation, so its cards are not made: it is an error to give it to an HDU.
julia> w = hdu.wcs.default; # SPECSYS = 'TOPOCENT', CTYPE3 = 'VRAD', OBSGEO-X, -Y, -Z, and DATE-OBS
julia> reframe(w, "LSRK")([10.0, 20.0, 5.0]) # the radio velocity of the pixel in the kinematic local standard of restFITSFiles.retime — Function
retime(wcs::WCS; scale = nothing, unit = nothing, position = nothing, direction = nothing, dut1 = nothing)The WCS whose time axis has the time scale, the unit, and the reference position, instead of those of the keywords: a time scale of Table 30 of the standard, e.g., "TT", "UTC", "TDB", and a unit of Table 34, "s", "min", "h", "d", "a", "yr", "cy", "ta", or "Ba". The world coordinate of the axis is the time since the reference time (MJDREF, JDREF, or DATEREF), which is the same time in every scale (Sect. 4.1.2 of Paper VII): the instant of the time since it in the scale of the keywords is converted to the instant in the other scale, and the result is the time of that instant since the reference time, in the unit. The scale of the keywords is that of CTYPE of the axis, or TIMESYS (UTC) if CTYPE is TIME, and the unit is CUNIT, or TIMEUNIT (s). The conversions are those of SOFA, through TAI: TAI (IAT), TT (TDT, ET), GPS (TAI − 19 s), UTC (GMT), UT1, TCG, TDB, and TCB. The leap seconds of the UTC are those of SOFA, and the difference of the TDB and the TT, which depends on the position, is that of the SOFA model for the observatory (OBSGEO-X, -Y, and -Z), or the geocenter if the WCS has none. UT1 − UTC in seconds, dut1, is needed for UT1.
The reference position (Table 3 of Paper VII) is where the time is valid: "TOPOCENTER" (TREFPOS of the WCS if it has none, the default), "GEOCENTER", "BARYCENTER", "HELIOCENTER", or "EMBARYCENTER" (the first three characters are the significant ones). The time at another position is the time plus the light time between the positions along the reference direction, (r_A - r_B) · n / c, with the positions of the SOFA ephemeris (the observatory is OBSGEO-X, -Y, and -Z, and the Earth-Moon barycenter is from the SOFA model of the Moon), whose unit vector n to the source is the direction, (longitude, latitude) in degrees of the ICRS, or the celestial reference value of the WCS if it is not given (TREFDIR refers to keywords that the WCS does not have). The relativistic delays of the Sun are not applied. The inverse of the result is the transformation of the world coordinates in the new scale, unit, and position to the pixels.
The keywords of the result are those of the WCS, except the CUNIT and the CTYPE or TIMESYS of the time axis, and the TREFPOS, and not those of its transformation, so its cards are not made: it is an error to give it to an HDU.
julia> w = hdu.wcs.default; # CTYPE3 = 'UTC', CUNIT3 = 's', MJDREF = 54746.0
julia> retime(w; scale = "TT", unit = "d")([1.0, 1.0, 12.0])[3] # the time of the 12th frame in days of TT since the reference timeFITSFiles.wcsversions — Function
wcsversions(hdu::HDU)
wcsversions(cards)The versions of the world-coordinate system (WCS) that a header has, sorted: ' ' for the primary description, if it has any of its keywords, and the letters 'A' to 'Z' of the alternative descriptions (Sect. 8.2.1). WCS reads a version.
FITSFiles.linearmatrix — Function
linearmatrix(wcs::WCS)The matrix of the linear transformation of the pixel coordinates p to the intermediate world coordinates x, which is x = M (p - r) for the reference pixel r (crpix) (Eqs. 9 to 11 of the standard): the cd matrix, which has the scale, if the keywords are in the CDija form, and the product of cdelt and the pc matrix, M[i, j] = cdelt[i] pc[i, j], if they are in the PCija form.
FITSFiles.timeaxes — Function
timeaxes(wcs::WCS)The axes of a WCS that are time axes, whose CTYPE is TIME or one of the time scales of Table 30 of the standard, e.g., UTC or TT, and may have a realization in parentheses (Sect. 9.2.1). An image has at most one.
FITSFiles.timereference — Function
timereference(wcs::WCS)The reference time of the time keywords of a WCS in Modified Julian Date, as the integer part and the fractional part, so that the precision is not lost (Sect. 9.2.2): MJDREF, or MJDREFI and MJDREFF, if the WCS has them, else JDREF, or JDREFI and JDREFF, else DATEREF, else 0.0. The single value has precedence over the parts. The time offset TIMEOFFS is not added.