Sample source code:
/****************************************************************/
/* read_fitsidi.c */
/* sample main program to read SMA FITS-IDI data file */
/* read SMA FITS-IDI tables(AG,FQ,SU,AN,UV) */
/* */
/****************************************************************/
#include <stdio.h>
#include <sys/stat.h>
#include <math.h>
#include "../lib/fits_reader.h"
#include "../lib/smadb.h"
int n_bytes_read = 2880;
int n_bytes_per_block = 28800;
main (argc, argv)
int argc;
char *argv[];
{
struct tbl_header table_hd;
struct basic_header basic_hd;
struct ag_FITS array_table[20];
struct su_FITS source_table[10];
struct fq_FITS freq_table[50];
struct an_FITS an_table[10];
struct run_log Run_Log;
struct fits_key Fits_Key;
struct arr_geo Arr_Geo;
struct freq db_freq[50];
int i, j, k, nsrcs;
int fd, iread, irow, no_pol;
int nrows, nread, iband, nband;
int n0 = 0, nstns;
double ref_freq;
char filename[64];
char bbside[3];
FILE *flist;
if ((flist = fopen ("readfits.log", "w")) == NULL)
{
printf ("cannot open output data file\n");
perror ("open error:");
exit (0);
}
/* open input file */
if (argc > 1)
strcpy (filename, argv[1]);
if ((fd = open (filename, n0)) == -1)
{
printf ("Cannot open %s, error=%d\n", filename, fd);
exit (0);
}
printf("opened file : %s\n", filename);
table_hd.xtension[0] = '\0';
/* read fits basic header */
n_bytes_read = n_bytes_per_block;
if ((nread = read_BasicHdr (fd, flist, &Run_Log)) == 0)
{
close (fd);
exit (0);
}
printf ("\n");
printf ("=== Finished reading basic header ===\n");
printf ("obscode =%s\n", Run_Log.obscode);
table_hd.xtension[0] = '\0';
table_hd.extname[0] = '\0';
nread = 0;
no_pol = 2;
/* read AG table */
nstns = readAG (fd, flist, nread, &table_hd, &Fits_Key, &Arr_Geo,
&array_table[0]);
if(Fits_Key.no_stkd == 1) no_pol = 1;
printf ("\n");
printf ("=== Finished reading AG table ===\n");
printf("<< Station offset from array origin >>\n");
printf(" Antenna stabx staby stabz\n");
for (i = 0; i < nstns; i++)
{
printf(" %d %-8s %14.4f %14.4f %14.4f\n", array_table[i].stnid,
array_table[i].anname, array_table[i].stabxyz[0],
array_table[i].stabxyz[1], array_table[i].stabxyz[2]);
}
/* read FQ table */
nrows = readFQ (fd, flist, nread, &table_hd, &freq_table[0],
&db_freq[0]);
printf ("\n");
printf ("=== Finished reading FQ table ===\n");
printf ("band, bb_freq(Hz), ch_width(Hz), total_BW(Hz), bb_chan,
freq(GH z), SB, POLI\n");
nband = Fits_Key.no_band;
ref_freq = Fits_Key.ref_freq;
printf ("ref_freq = %f\n", ref_freq);
for (iread = 0; iread < table_hd.naxis2; iread++)
{
for (iband = 0; iband < nband; iband++)
{
if (freq_table[iread].sideband[iband] == -1)
strcpy (bbside, "L");
if (freq_table[iread].sideband[iband] == 1)
strcpy (bbside, "U");
printf (" %d %14.1f %10.1f %10.1f %2d | %10.6f %s\n",
iband + 1,
freq_table[iread].bandfreq[iband],
freq_table[iread].ch_width[iband],
freq_table[iread].total_bandwidth[iband],
freq_table[iread].bb_chan[iband],
(ref_freq + freq_table[iread].bandfreq[iband])/1.0e+9,
bbside);
}
}
/* read SU table */
nsrcs = readSU (fd, flist, nread, &table_hd, &source_table);
printf ("\n");
printf ("=== Finished reading SU table ===\n");
printf ("ID Source Calcode RA Dec Epoch\n");
for (j = 0; j < nsrcs; j++)
{
printf ("%2d %-16s %-s %14.4f %14.4f %6.1f\n",
source_table[j].sou_ id, source_table[j].source,
source_table[j].calcode, source_table[j].ra,
source_table[j].dec, source_table[j].epoch);
for (k=0; k< nband; k++)
{
printf (" band = %d, Vsys = %f\n",
k+1,source_table[j].sysvel[k]);
}
}
/* read AN table */
nrows = readAN (fd, flist, nread, &table_hd, &an_table);
printf ("\n");
printf ("=== Finished reading AN table ===\n");
printf ("MJD at mid-datapoints: %f \n", an_table[0].time);
printf ("time span of data (day): %f \n", an_table[0].time_intvl);
printf ("no of pol : %d \n", no_pol);
/* read UV table */
printf ("\n");
printf ("=== Finished reading UV table ===\n");
nrows = readUV (fd, flist, nread, &table_hd);
}
Compilation:
gcc -o read_fits read_fitsidi.c -lm -L/home/tsutsumi/sniffer/lib -lrdsmafits
Execution of read_fits yields following output on console:
read_fits /home/tsutsumi/00010002.FIT
opened file : /home/tsutsumi/00010002.FIT
=== Finished reading basic header ===
obscode =TEST0002
=== Finished reading AG table ===
<< Station offset from array origin >>
Antenna stabx staby stabz
1 SMA_p2 -0.0000 0.0000 0.0000
2 SMA_p3 -0.5398 15.4366 -0.0558
3 0.0000 0.0000 0.0000
4 0.0000 0.0000 0.0000
5 0.0000 0.0000 0.0000
6 0.0000 0.0000 0.0000
7 0.0000 0.0000 0.0000
8 0.0000 0.0000 0.0000
=== Finished reading FQ table ===
band, bb_freq(Hz), ch_width(Hz), total_BW(Hz), bb_chan,
freq(GH z), SB, POLI
ref_freq = 230438000000.000000
1 0.0 2000000.0 256000000.0 0 | 230.438000 U
=== Finished reading SU table ===
ID Source Calcode RA Dec Epoch
1 IRC10216 C 146.9891 13.2789 2000.0
band = 1, Vsys = 0.000000
=== Finished reading AN table ===
MJD at mid-datapoints: 51163.403287
time span of data (day): 0.375625
no of pol : 1
=== Finished reading UV table ===
real and imaginary part of visibility
first 10 records (channel = 1)
record1 Re = -0.201737, Im = -0.067500
record2 Re = -0.053258, Im = -0.048156
record3 Re = -0.075533, Im = 0.005921
record4 Re = -0.052813, Im = -0.440354
record5 Re = 0.158620, Im = -0.027928
record6 Re = 0.177189, Im = -0.129926
record7 Re = 0.099060, Im = -0.100129
record8 Re = 0.146315, Im = -0.064175
record9 Re = 0.197210, Im = -0.074321
record10 Re = -0.396971, Im = 0.063831