Reading OIFITS files

Basic usage

readoifits loads an OIFITS file and returns a 2D array of OIdata{T} indexed as [nwavbin, ntimebin]. The simplest call reads everything into a single bin:

using OITOOLS
data = readoifits("mystar.oifits")        # returns Array{OIdata{Float32}, 2}
data[1]                                   # the single OIdata bin

The observables are Float32 by default; T=Float64 gives double precision:

data = readoifits("mystar.oifits"; T=Float64)

MJDs (v2_mjd, t3_mjd, vis_mjd, flux_mjd, mean_mjd, and uv_mjd, the time a $MJD model is evaluated at) are Float64 for any T.

Keyword reference

KeywordDefaultDescription
Target selection
targetname""Select a single target by name (all targets if empty)
insname""Select one instrument (an OI_WAVELENGTH table's INSNAME; all if empty)
Spectral / temporal binning
spectralbin[[]]Vector of [λ_min, λ_max] windows (metres)
temporalbin[[]]Vector of [mjd_min, mjd_max] windows
polychromaticfalseOne spectral bin per channel (overrides spectralbin)
merge_oi_wavelengthfalseDeduplicate compatible OI_WAVELENGTH tables (polychromatic mode)
splittingfalseForce multi-bin mode even with default bin vectors
get_specbin_filetrueAuto-derive spectral bins from the file
get_timebin_filetrueAuto-derive temporal bins from the file
Averaging channels
binning0Average every N channels into one (an integer), or the channels in given λ windows; 0 keeps every channel
binmode:block:block (1/σ² weights, circular-mean phases, scatter check) or :pmoired (PMOIRED's binning)
bincorr1Correlation between adjacent channels: averaged errors are multiplied by √bincorr
binstatsnothingA Dict that receives, per observable, the χ²/dof of the averaged channels about a line in λ
Observable selection
use_vistrueLoad complex visibilities
use_v2trueLoad squared visibilities
use_t3trueLoad triple products
use_fluxtrueLoad flux spectra
Quality filtering
filter_bad_datatrueApply quality cuts (flags, NaN, SNR, range) on load
ignore_flagsfalseKeep points the file flags
force_full_visfalseRequire both visamp and visphi to be valid
force_full_t3falseRequire both t3amp and t3phi to be valid
cutoff_minv2-1Minimum V² (points below are discarded)
cutoff_maxv22.0Maximum V²
cutoff_mint3amp-1.0Minimum T3 amplitude
cutoff_maxt3amp1.5Maximum T3 amplitude
filter_v2_snr_threshold0.01Minimum |V²/σ| to keep
special_filter_diffvisfalseKeep only vis points common across all spectral bins
UV deduplication
redundance_removetrueMerge UV points closer than uvtol
uvtol200.0Merge radius in cycles/rad (B/λ)
Output
warntruePrint warnings about non-standard files
verbosetruePrint summary of loaded tables
TFloat32Element type of the observables

The OIdata struct

Each OIdata{T} bin holds flat vectors for every observable:

FieldDescription
v2, v2_err, v2_baseline, v2_lam, v2_mjdSquared visibilities
t3phi, t3phi_err, t3amp, t3amp_errClosure phases and triple amplitudes
visamp, visamp_err, visphi, visphi_errComplex visibility amplitude/phase
flux, flux_err, flux_lam, flux_sta_indexFlux spectra (0 = calibrated)
uv, uv_lam, uv_baselineUV coverage (2×nuv matrix, λ in metres)
indx_v2, indx_t3_1/2/3, indx_visIndices mapping observables → UV points
sta_name, tel_name, sta_indexStation / telescope names
mean_mjdMean MJD of the bin (always Float64)
nv2, nt3phi, nt3amp, nvisamp, nvisphi, nflux, nuvCounts of the points holding a measurement
tablesOne entry per table read: (extname, insname, arrname, amptyp, phityp)
v2_tab, t3_tab, vis_tab, flux_tabFor each point, the index of its table in tables

Call display(data) or display(data[1]) to print a compact summary.

Correlated data (OI_CORR)

OI_CORR tables are stored in the OIdata as correlation matrices and per-point indices:

FieldDescription
v2_corr, v2_corr_idxV² correlation matrix and per-point index
t3phi_corr, t3phi_corr_idxClosure-phase correlation
t3amp_corr, t3amp_corr_idxTriple-amplitude correlation
visamp_corr, visamp_corr_idxVisibility amplitude correlation
visphi_corr, visphi_corr_idxVisibility phase correlation
flux_corr, flux_corr_idxFlux correlation

*_corr_idx[i] == 0 means point i has no associated correlation row.

Correlation blocks covering several baselines (GRAVITY, where NDATA > nwave) are mapped to each baseline's slice of the matrix. example_chi2_correlated.jl computes a whitened χ² from GRAVITY data.

Selecting a target

Files with multiple targets require a targetname filter:

data = readoifits("multi_target.oifits"; targetname="Betelgeuse")

Use list_oifits_targets("multi_target.oifits") to see all available targets.

Instruments

Each point records the table it was read from (tables, v2_tab, …), so a dataset knows the instrument of every point. insname reads one instrument; split_by_instrument divides a dataset already read, merged or filtered:

sc = readoifits("gravity.fits"; insname = "GRAVITY_SC")
parts = split_by_instrument(readoifits("gravity.fits")[1])   # ["GRAVITY_SC" => …, "GRAVITY_FT" => …]

write_oifits writes one OI_WAVELENGTH table per instrument, under its name and with its own channels, so a written file reads back by instrument as the original does.

Polychromatic (per-channel) mode

polychromatic=true makes one bin per spectral channel, with boundaries at the midpoints between adjacent channels:

data = readoifits("my_spectrum.oifits"; polychromatic=true)
# data is nchannels×1; data[k,1] is the k-th channel bin

A file with several OI_WAVELENGTH tables gets one bin per distinct channel: tables sharing a grid share their bins. merge_oi_wavelength=true checks that the tables' channels overlap and uses the first table's grid.

To specify bins manually:

spectralbin = [[1.6e-6, 1.8e-6], [2.0e-6, 2.4e-6]]   # two K-band bins
data = readoifits("my_spectrum.oifits"; spectralbin)

Similarly for time bins:

temporalbin = [[58000.0, 58100.0], [58200.0, 58300.0]]
data = readoifits("mystar.oifits"; temporalbin)

Averaging channels

binning averages adjacent channels into one when the file is read; spectralbin and polychromatic group channels into bins and keep each one.

data = readoifits("gravity.fits"; binning = 20)                   # every 20 channels → 1
data = readoifits("gravity.fits"; binning = 20, bincorr = 2)      # adjacent channels correlated
data = readoifits("gravity.fits"; binning = 20, binmode = :pmoired)
stats = Dict{Symbol,Float64}()
readoifits("gravity.fits"; binning = 20, binstats = stats)        # χ²/dof of each average

In :block mode the average is 1/σ²-weighted, a phase is averaged as a weighted circular mean, and the error is the error of the weighted mean times √bincorr. binstats reports the scatter of the averaged channels about a straight line in λ within each group: near 1 when the errors describe the scatter, above 1 when they are underestimated, below 1 when adjacent channels are correlated. OI_CORR describes the file's own channels and is not used when averaging.

Quality filtering

filter_bad_data=true (the default) removes flagged points and applies range and SNR cuts:

data = readoifits("mystar.oifits";
    filter_bad_data        = true,
    cutoff_minv2           = -0.05,
    cutoff_maxv2           = 1.05,
    cutoff_mint3amp        = -0.1,
    cutoff_maxt3amp        = 1.5,
    filter_v2_snr_threshold = 1.0)   # require SNR > 1 for V²

To skip observable types:

data = readoifits("mystar.oifits"; use_flux=false, use_vis=false)

Output

readoifits prints a summary of the tables (including OI_VIS's AMPTYP/PHITYP) and warns about non-standard files. verbose=false and warn=false turn these off:

data = readoifits("mystar.oifits"; verbose=false, warn=false)

UV deduplication

Redundant UV points (within uvtol cycles/rad, i.e. B/λ; default 200) are merged:

data = readoifits("mystar.oifits"; redundance_remove=true, uvtol=100.0)

Disable with redundance_remove=false.

Post-load filtering

set_data_filter and filter_data apply further cuts to data already read:

idx = set_data_filter(data[1];
    filter_bad_data      = true,
    baseline_range       = [10.0e6, 300.0e6],
    wav_range            = [1.6e-6, 1.8e-6])
data_filtered = filter_data(data[1], idx)

drop removes observables. A T3 amplitude and its closure phase are one point, as are a VIS amplitude and its phase: dropping one half keeps the point for the other, the dropped half "not measured" (0 ± Inf: not counted, ignored by χ², not drawn, written as NaN).

no_t3amp = filter_data(data[1]; drop = [:t3amp])
v2_only  = filter_data(data[1]; drop = [:t3amp, :t3phi, :visamp, :visphi, :flux])

split_by_time divides a dataset into epochs, from the times of every observable:

parts, mjd = split_by_time(data[1])                    # a new epoch after a 0.5-day gap
parts, mjd = split_by_time(data[1]; binwidth = 1/24)   # hourly windows

Reading multiple files

Several files as one dataset, stations matched by name:

data = readoifits(["night1.oifits", "night2.oifits"])

One file per epoch, as a Matrix{OIdata}:

files = ["epoch1.oifits", "epoch2.oifits", "epoch3.oifits"]
data = readoifits_multiepochs(files)   # 1×3 Matrix{OIdata}

One file per band, also with readoifits_multiepochs:

files = ["H_band.oifits", "K_band.oifits"]
data = readoifits_multiepochs(files)   # 1×2 Matrix — one column per file

Error inflation

oifits_prep inflates error bars in place:

oifits_prep(data[1];
    min_v2_err_add   = 0.01,    # additive floor for V² errors
    min_v2_err_rel   = 0.02,    # relative floor (2% of |V²|)
    v2_err_mult      = 1.0,     # multiplicative scaling
    min_t3phi_err_add = 1.0,    # additive floor for T3φ errors (degrees)
    t3phi_err_mult   = 1.0,
    quad             = false)   # true = combine in quadrature

By default each error bar becomes the larger of the scaled error and the floor; quad=true adds them in quadrature.

ASPRO-compatible FITS headers

updatefits_aspro adds the WCS keywords (pixel scale, reference pixel) ASPRO reads to a FITS image:

updatefits_aspro("input.fits", "output.fits", 0.1)   # pixsize in mas

Non-standard files

Files are read table by table (load_oifits). A table declaring a revision whose columns it lacks, or a revision that does not exist, is read at the revision that fits; GRAVITY's FLUX column is read as FLUXDATA; an OI_VIS with amplitudes only has its phases "not measured"; rows whose TARGET_ID is not in OI_TARGET are left out; a ZIP archive holding one FITS file is read through that file. readoifits lists these in one warning per file:

data = readoifits("2022-08-03-calibrated_cleaned.fits")   # a ZIP archive

validate_oifits(file) reports the same, along with references to missing stations or targets and values with unusable errors; repair_oifits(file, out) writes a standard OIFITS v2 copy; oifits_headers(file) lists every HDU and its header cards.

OIFITSv1 compatibility

OIFITS v1 files with 0-based station indices are read with the stations renumbered from 1, and a warning.