1
0
Fork 0

Indent qfits to GNU style, perhaps

giza-pure
Jeff Moe 2022-08-06 22:20:48 -06:00
parent 7621d31783
commit db0415128c
49 changed files with 13259 additions and 11539 deletions

File diff suppressed because it is too large Load Diff

View File

@ -66,14 +66,14 @@
Function prototypes
-----------------------------------------------------------------------------*/
void * xmemory_malloc(size_t, const char *, int) ;
void * xmemory_calloc(size_t, size_t, const char *, int) ;
void * xmemory_realloc(void *, size_t, const char *, int) ;
void xmemory_free(void *, const char *, int) ;
char * xmemory_strdup(const char *, const char *, int) ;
char * xmemory_falloc(char *, size_t, size_t *, const char *, int) ;
void xmemory_fdealloc(void *, size_t, size_t, const char *, int) ;
void *xmemory_malloc (size_t, const char *, int);
void *xmemory_calloc (size_t, size_t, const char *, int);
void *xmemory_realloc (void *, size_t, const char *, int);
void xmemory_free (void *, const char *, int);
char *xmemory_strdup (const char *, const char *, int);
char *xmemory_falloc (char *, size_t, size_t *, const char *, int);
void xmemory_fdealloc (void *, size_t, size_t, const char *, int);
void xmemory_status_(const char * filename, int lineno) ;
void xmemory_status_ (const char *filename, int lineno);
#endif

View File

@ -45,275 +45,307 @@
Function prototypes
-----------------------------------------------------------------------------*/
void usage(char * pname) ;
void parse_cmd_line(int, char **, int *, int *, int *) ;
int dump_fits_filter(FILE *, int) ;
int dump_fits(char *, int) ;
char * rstrip(char *) ;
void usage (char *pname);
void parse_cmd_line (int, char **, int *, int *, int *);
int dump_fits_filter (FILE *, int);
int dump_fits (char *, int);
char *rstrip (char *);
/*-----------------------------------------------------------------------------
Main
-----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
int
main (int argc, char *argv[])
{
int xtnum ;
int c_arg ;
int filter ;
int err ;
int xtnum;
int c_arg;
int filter;
int err;
/* No arguments prints out a usage message */
if (argc<2) usage(argv[0]);
/* No arguments prints out a usage message */
if (argc < 2)
usage (argv[0]);
/* Parse command-line options */
parse_cmd_line(argc, argv, &xtnum, &filter, &c_arg);
/* Parse command-line options */
parse_cmd_line (argc, argv, &xtnum, &filter, &c_arg);
/* Filter mode: process data received from stdin */
if (filter) {
/* Filter mode: process data received from stdin */
if (filter)
{
#if HAVE_ZLIB
printf("filter mode does not support gzipped files\n");
printf("use: gunzip -c file.fits | dfits -\n");
return 1 ;
printf ("filter mode does not support gzipped files\n");
printf ("use: gunzip -c file.fits | dfits -\n");
return 1;
#else
return dump_fits_filter(stdin, xtnum);
return dump_fits_filter (stdin, xtnum);
#endif
}
/* Normal mode: loop on all file names given on command-line */
err = 0 ;
while (c_arg < argc) {
err += dump_fits(argv[c_arg], xtnum);
c_arg++;
}
return err ; /* Returns number of errors during process */
/* Normal mode: loop on all file names given on command-line */
err = 0;
while (c_arg < argc)
{
err += dump_fits (argv[c_arg], xtnum);
c_arg++;
}
return err; /* Returns number of errors during process */
}
void usage(char * pname)
void
usage (char *pname)
{
printf(
"\n\n"
"usage: %s [-x xtnum] <list of FITS files>\n"
"usage: %s [-x xtnum] -\n"
"\n"
"The former version expects file names.\n"
"The latter expects data coming in from stdin.\n"
"\n"
"-x xtnum specifies the extension header to print\n"
"-x 0 specifies main header + all extensions\n"
"\n\n",
pname, pname);
printf ("\n\n"
"usage: %s [-x xtnum] <list of FITS files>\n"
"usage: %s [-x xtnum] -\n"
"\n"
"The former version expects file names.\n"
"The latter expects data coming in from stdin.\n"
"\n"
"-x xtnum specifies the extension header to print\n"
"-x 0 specifies main header + all extensions\n"
"\n\n", pname, pname);
#if HAVE_ZLIB
printf(
"This program was compiled against zlib %s\n"
"This means you can use it with gzipped FITS files\n"
"as with uncompressed FITS files.\n"
"NB: this does not apply to the '-' option (input from stdin)\n"
"\n\n", ZLIB_VERSION);
printf ("This program was compiled against zlib %s\n"
"This means you can use it with gzipped FITS files\n"
"as with uncompressed FITS files.\n"
"NB: this does not apply to the '-' option (input from stdin)\n"
"\n\n", ZLIB_VERSION);
#endif
exit(1) ;
exit (1);
}
void parse_cmd_line(
int argc,
char ** argv,
int * xtnum,
int * filter,
int * c_arg)
void
parse_cmd_line (int argc, char **argv, int *xtnum, int *filter, int *c_arg)
{
*filter = 0 ;
*xtnum = -1 ;
*c_arg = argc-1 ;
*filter = 0;
*xtnum = -1;
*c_arg = argc - 1;
/* If '-' is on the command-line, it must be the last argument */
if (!strcmp(argv[argc-1], "-")) *filter = 1 ;
/* If -x xtnum is on the command-line, it must be the first two arguments */
if (!strcmp(argv[1], "-x")) {
*xtnum = atoi(argv[2]);
*c_arg = 3 ;
} else {
*c_arg = 1 ;
}
return ;
/* If '-' is on the command-line, it must be the last argument */
if (!strcmp (argv[argc - 1], "-"))
*filter = 1;
/* If -x xtnum is on the command-line, it must be the first two arguments */
if (!strcmp (argv[1], "-x"))
{
*xtnum = atoi (argv[2]);
*c_arg = 3;
}
else
{
*c_arg = 1;
}
return;
}
/* Strip off all blank characters in a string from the right-side. */
char * rstrip(char * s)
char *
rstrip (char *s)
{
int len ;
if (s==NULL) return s ;
len = strlen(s);
if (len<1) return s ;
len -- ;
while (s[len]== ' ') {
s[len]=(char)0 ;
len --;
if (len<0) break ;
int len;
if (s == NULL)
return s;
len = strlen (s);
if (len < 1)
return s;
len--;
while (s[len] == ' ')
{
s[len] = (char) 0;
len--;
if (len < 0)
break;
}
return s ;
return s;
}
/* Dump the requested header (main or extension) from a filename. */
int dump_fits(char * name, int xtnum)
int
dump_fits (char *name, int xtnum)
{
FILE * in ;
int err ;
FILE *in;
int err;
if ((in=fopen(name, "r"))==NULL) {
fprintf(stderr, "error: cannot open file [%s]\n", name);
return 1 ;
}
if ((in = fopen (name, "r")) == NULL)
{
fprintf (stderr, "error: cannot open file [%s]\n", name);
return 1;
}
printf("====> file %s (main) <====\n", name) ;
err = dump_fits_filter(in, xtnum);
fclose(in);
return err ;
printf ("====> file %s (main) <====\n", name);
err = dump_fits_filter (in, xtnum);
fclose (in);
return err;
}
/* Dump the requested header (main or extension) from a FILE * */
int dump_fits_filter(FILE * in, int xtnum)
int
dump_fits_filter (FILE * in, int xtnum)
{
int n_xt ;
char buf[LGTH+1];
int err ;
int data_bytes, naxis ;
char * read_val ;
int skip_blocks ;
int seeked ;
int n_xt;
char buf[LGTH + 1];
int err;
int data_bytes, naxis;
char *read_val;
int skip_blocks;
int seeked;
/* Try getting the first 80 chars */
memset(buf, 0, LGTH+1);
if (fread(buf, sizeof(char), LGTH, in)!=LGTH) {
fprintf(stderr, "error reading input\n");
return 1;
}
/* Check that it is indeed FITS */
if (strncmp(buf, MAGIC, strlen(MAGIC))) {
fprintf(stderr, "not a FITS file\n");
return 1 ;
}
naxis = 0 ;
data_bytes = 1 ;
if (xtnum<1) {
/* Output main header */
printf("%s\n", rstrip(buf));
data_bytes = 1 ;
naxis = 0 ;
while ((err=fread(buf, sizeof(char), LGTH, in))==LGTH) {
printf("%s\n", rstrip(buf));
/* Look for BITPIX keyword */
if (buf[0]=='B' &&
buf[1]=='I' &&
buf[2]=='T' &&
buf[3]=='P' &&
buf[4]=='I' &&
buf[5]=='X' &&
buf[6]==' ') {
read_val = qfits_getvalue(buf);
data_bytes *= (int)atoi(read_val) / 8 ;
if (data_bytes<0) data_bytes *= -1 ;
} else
/* Look for NAXIS keyword */
if (buf[0]=='N' &&
buf[1]=='A' &&
buf[2]=='X' &&
buf[3]=='I' &&
buf[4]=='S') {
/* Try getting the first 80 chars */
memset (buf, 0, LGTH + 1);
if (fread (buf, sizeof (char), LGTH, in) != LGTH)
{
fprintf (stderr, "error reading input\n");
return 1;
}
/* Check that it is indeed FITS */
if (strncmp (buf, MAGIC, strlen (MAGIC)))
{
fprintf (stderr, "not a FITS file\n");
return 1;
}
naxis = 0;
data_bytes = 1;
if (xtnum < 1)
{
/* Output main header */
printf ("%s\n", rstrip (buf));
data_bytes = 1;
naxis = 0;
while ((err = fread (buf, sizeof (char), LGTH, in)) == LGTH)
{
printf ("%s\n", rstrip (buf));
/* Look for BITPIX keyword */
if (buf[0] == 'B' &&
buf[1] == 'I' &&
buf[2] == 'T' &&
buf[3] == 'P' &&
buf[4] == 'I' && buf[5] == 'X' && buf[6] == ' ')
{
read_val = qfits_getvalue (buf);
data_bytes *= (int) atoi (read_val) / 8;
if (data_bytes < 0)
data_bytes *= -1;
}
else
/* Look for NAXIS keyword */
if (buf[0] == 'N' &&
buf[1] == 'A' &&
buf[2] == 'X' && buf[3] == 'I' && buf[4] == 'S')
{
if (buf[5]==' ') {
/* NAXIS keyword */
read_val = qfits_getvalue(buf);
naxis = (int)atoi(read_val);
} else {
/* NAXIS?? keyword (axis size) */
read_val = qfits_getvalue(buf);
data_bytes *= (int)atoi(read_val);
}
} else
/* Look for END keyword */
if (buf[0]=='E' &&
buf[1]=='N' &&
buf[2]=='D') {
break ;
}
if (buf[5] == ' ')
{
/* NAXIS keyword */
read_val = qfits_getvalue (buf);
naxis = (int) atoi (read_val);
}
if (err!=LGTH) return 1 ;
}
if (xtnum<0) return 0 ;
n_xt=0 ;
while (1) {
/*
* Skip the previous data section if pixels were declared
*/
if (naxis>0) {
/* Skip as many blocks as there are declared pixels */
skip_blocks = data_bytes/BLOCK_SIZE ;
if ((data_bytes % BLOCK_SIZE)!=0) skip_blocks ++ ;
seeked = fseek(in, skip_blocks*BLOCK_SIZE, SEEK_CUR);
if (seeked<0) return -1 ;
}
/* Look for next XTENSION keyword */
while ((err=fread(buf, sizeof(char), LGTH, in))==LGTH) {
if (buf[0]=='X' &&
buf[1]=='T' &&
buf[2]=='E' &&
buf[3]=='N' &&
buf[4]=='S' &&
buf[5]=='I' &&
buf[6]=='O' &&
buf[7]=='N') break ;
else
{
/* NAXIS?? keyword (axis size) */
read_val = qfits_getvalue (buf);
data_bytes *= (int) atoi (read_val);
}
if (err==0) break ;
if (err!=LGTH) return 1 ;
n_xt++ ;
if (xtnum==0 || xtnum==n_xt) {
printf("===> xtension %d\n", n_xt) ;
printf("%s\n", rstrip(buf));
}
data_bytes = 1 ;
naxis = 0 ;
while ((err=fread(buf, sizeof(char), LGTH, in))==LGTH) {
if (xtnum==0 || xtnum==n_xt) printf("%s\n", rstrip(buf));
/* Look for BITPIX keyword */
if (buf[0]=='B' &&
buf[1]=='I' &&
buf[2]=='T' &&
buf[3]=='P' &&
buf[4]=='I' &&
buf[5]=='X' &&
buf[6]==' ') {
read_val = qfits_getvalue(buf);
data_bytes *= (int)atoi(read_val) / 8 ;
if (data_bytes<0) data_bytes *= -1 ;
} else
/* Look for NAXIS keyword */
if (buf[0]=='N' &&
buf[1]=='A' &&
buf[2]=='X' &&
buf[3]=='I' &&
buf[4]=='S') {
if (buf[5]==' ') {
/* NAXIS keyword */
read_val = qfits_getvalue(buf);
naxis = (int)atoi(read_val);
} else {
/* NAXIS?? keyword (axis size) */
read_val = qfits_getvalue(buf);
data_bytes *= (int)atoi(read_val);
}
} else
/* Look for END keyword */
if (buf[0]=='E' &&
buf[1]=='N' &&
buf[2]=='D') break ;
}
if (n_xt==xtnum) break ;
}
else
/* Look for END keyword */
if (buf[0] == 'E' && buf[1] == 'N' && buf[2] == 'D')
{
break;
}
}
return 0 ;
if (err != LGTH)
return 1;
}
if (xtnum < 0)
return 0;
n_xt = 0;
while (1)
{
/*
* Skip the previous data section if pixels were declared
*/
if (naxis > 0)
{
/* Skip as many blocks as there are declared pixels */
skip_blocks = data_bytes / BLOCK_SIZE;
if ((data_bytes % BLOCK_SIZE) != 0)
skip_blocks++;
seeked = fseek (in, skip_blocks * BLOCK_SIZE, SEEK_CUR);
if (seeked < 0)
return -1;
}
/* Look for next XTENSION keyword */
while ((err = fread (buf, sizeof (char), LGTH, in)) == LGTH)
{
if (buf[0] == 'X' &&
buf[1] == 'T' &&
buf[2] == 'E' &&
buf[3] == 'N' &&
buf[4] == 'S' &&
buf[5] == 'I' && buf[6] == 'O' && buf[7] == 'N')
break;
}
if (err == 0)
break;
if (err != LGTH)
return 1;
n_xt++;
if (xtnum == 0 || xtnum == n_xt)
{
printf ("===> xtension %d\n", n_xt);
printf ("%s\n", rstrip (buf));
}
data_bytes = 1;
naxis = 0;
while ((err = fread (buf, sizeof (char), LGTH, in)) == LGTH)
{
if (xtnum == 0 || xtnum == n_xt)
printf ("%s\n", rstrip (buf));
/* Look for BITPIX keyword */
if (buf[0] == 'B' &&
buf[1] == 'I' &&
buf[2] == 'T' &&
buf[3] == 'P' &&
buf[4] == 'I' && buf[5] == 'X' && buf[6] == ' ')
{
read_val = qfits_getvalue (buf);
data_bytes *= (int) atoi (read_val) / 8;
if (data_bytes < 0)
data_bytes *= -1;
}
else
/* Look for NAXIS keyword */
if (buf[0] == 'N' &&
buf[1] == 'A' &&
buf[2] == 'X' && buf[3] == 'I' && buf[4] == 'S')
{
if (buf[5] == ' ')
{
/* NAXIS keyword */
read_val = qfits_getvalue (buf);
naxis = (int) atoi (read_val);
}
else
{
/* NAXIS?? keyword (axis size) */
read_val = qfits_getvalue (buf);
data_bytes *= (int) atoi (read_val);
}
}
else
/* Look for END keyword */
if (buf[0] == 'E' && buf[1] == 'N' && buf[2] == 'D')
break;
}
if (n_xt == xtnum)
break;
}
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -37,132 +37,149 @@
* Swap pixels between position p1 and p2, regardless of the pixel
* type and endian-ness of the local host.
*/
static void swap_pix(char * buf, int p1, int p2, int psize)
static void
swap_pix (char *buf, int p1, int p2, int psize)
{
int i ;
char c ;
int i;
char c;
for (i=0 ; i<psize ; i++) {
c = buf[p1+i] ;
buf[p1+i] = buf[p2+i];
buf[p2+i] = c ;
}
for (i = 0; i < psize; i++)
{
c = buf[p1 + i];
buf[p1 + i] = buf[p2 + i];
buf[p2 + i] = c;
}
}
/*
* Main processing function. It expects one only file name
* and will flip pixels on the input frame.
*/
static int fits_flip(char * pname, char * filename)
static int
fits_flip (char *pname, char *filename)
{
char * sval ;
int dstart;
int lx, ly ;
int bpp ;
int i, j ;
char * buf ;
char * fbuf ;
int psize;
struct stat fileinfo ;
int fd ;
char *sval;
int dstart;
int lx, ly;
int bpp;
int i, j;
char *buf;
char *fbuf;
int psize;
struct stat fileinfo;
int fd;
printf("%s: processing %s\n", pname, filename);
printf ("%s: processing %s\n", pname, filename);
if (stat(filename, &fileinfo)!=0) {
return -1 ;
}
if (fileinfo.st_size<1) {
printf("cannot stat file\n");
return -1 ;
}
if (stat (filename, &fileinfo) != 0)
{
return -1;
}
if (fileinfo.st_size < 1)
{
printf ("cannot stat file\n");
return -1;
}
/* Retrieve image attributes */
if (is_fits_file(filename)!=1) {
printf("not a FITS file\n");
return -1 ;
}
/* Retrieve image attributes */
if (is_fits_file (filename) != 1)
{
printf ("not a FITS file\n");
return -1;
}
sval = qfits_query_hdr(filename, "NAXIS1");
if (sval==NULL) {
printf("cannot read NAXIS1\n");
return -1 ;
}
lx = atoi(sval);
sval = qfits_query_hdr(filename, "NAXIS2");
if (sval==NULL) {
printf("cannot read NAXIS2\n");
return -1 ;
}
ly = atoi(sval);
sval = qfits_query_hdr(filename, "BITPIX");
if (sval==NULL) {
printf("cannot read BITPIX\n");
return -1 ;
}
bpp = atoi(sval);
sval = qfits_query_hdr (filename, "NAXIS1");
if (sval == NULL)
{
printf ("cannot read NAXIS1\n");
return -1;
}
lx = atoi (sval);
sval = qfits_query_hdr (filename, "NAXIS2");
if (sval == NULL)
{
printf ("cannot read NAXIS2\n");
return -1;
}
ly = atoi (sval);
sval = qfits_query_hdr (filename, "BITPIX");
if (sval == NULL)
{
printf ("cannot read BITPIX\n");
return -1;
}
bpp = atoi (sval);
psize = bpp/8 ;
if (psize<0) psize=-psize ;
psize = bpp / 8;
if (psize < 0)
psize = -psize;
/* Retrieve start of first data section */
if (qfits_get_hdrinfo(filename, 0, &dstart, NULL)!=0) {
printf("reading header information\n");
return -1 ;
}
/* Retrieve start of first data section */
if (qfits_get_hdrinfo (filename, 0, &dstart, NULL) != 0)
{
printf ("reading header information\n");
return -1;
}
/* Map the input file in read/write mode (input file is modified) */
if ((fd=open(filename, O_RDWR))==-1) {
perror("open");
printf("reading file\n");
return -1 ;
}
fbuf = (char*)mmap(0,
fileinfo.st_size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0);
if (fbuf==(char*)-1) {
perror("mmap");
printf("mapping file\n");
return -1 ;
}
buf = fbuf + dstart ;
/* Map the input file in read/write mode (input file is modified) */
if ((fd = open (filename, O_RDWR)) == -1)
{
perror ("open");
printf ("reading file\n");
return -1;
}
fbuf = (char *) mmap (0,
fileinfo.st_size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (fbuf == (char *) -1)
{
perror ("mmap");
printf ("mapping file\n");
return -1;
}
buf = fbuf + dstart;
/* Double loop */
for (j=0 ; j<ly ; j++) {
for (i=0 ; i<lx/2 ; i++) {
/* Swap bytes */
swap_pix(buf, i*psize, (lx-i-1)*psize, psize);
}
buf += lx * psize ;
/* Double loop */
for (j = 0; j < ly; j++)
{
for (i = 0; i < lx / 2; i++)
{
/* Swap bytes */
swap_pix (buf, i * psize, (lx - i - 1) * psize, psize);
}
if (munmap(fbuf, fileinfo.st_size)!=0) {
printf("unmapping file\n");
return -1 ;
}
return 0 ;
buf += lx * psize;
}
if (munmap (fbuf, fileinfo.st_size) != 0)
{
printf ("unmapping file\n");
return -1;
}
return 0;
}
/*-----------------------------------------------------------------------------
Main
-----------------------------------------------------------------------------*/
int main(int argc, char * argv[])
int
main (int argc, char *argv[])
{
int i ;
int err ;
int i;
int err;
if (argc<2) {
printf("use: %s <list of FITS files...>\n", argv[0]);
return 1 ;
}
err=0 ;
for (i=1 ; i<argc ; i++) {
err += fits_flip(argv[0], argv[i]) ;
}
if (err>0) {
fprintf(stderr, "%s: %d error(s) occurred\n", argv[0], err);
return -1 ;
}
return 0 ;
if (argc < 2)
{
printf ("use: %s <list of FITS files...>\n", argv[0]);
return 1;
}
err = 0;
for (i = 1; i < argc; i++)
{
err += fits_flip (argv[0], argv[i]);
}
if (err > 0)
{
fprintf (stderr, "%s: %d error(s) occurred\n", argv[0], err);
return -1;
}
return 0;
}

View File

@ -33,31 +33,33 @@
-----------------------------------------------------------------------------*/
/* Frame informations needed to classify it */
typedef struct _framei {
char * name ;
char * tplid ;
char * origfile ;
int expno ;
int nexp ;
typedef struct _framei
{
char *name;
char *tplid;
char *origfile;
int expno;
int nexp;
struct _framei * next ;
} framei ;
struct _framei *next;
} framei;
/* Frame queue: is mostly a pointer to a list of frame information objects */
typedef struct _frameq {
framei * first ;
int n ;
} frameq ;
typedef struct _frameq
{
framei *first;
int n;
} frameq;
/*-----------------------------------------------------------------------------
Global variables
-----------------------------------------------------------------------------*/
/* List of strings to identify unwanted templates. */
char * tpl_filter[] = {
"_acq_",
"_CheckAoCorrection",
NULL
char *tpl_filter[] = {
"_acq_",
"_CheckAoCorrection",
NULL
};
/*-----------------------------------------------------------------------------
@ -71,43 +73,56 @@ char * tpl_filter[] = {
@return A newly allocated framei obj. NULL in error case
*/
/*----------------------------------------------------------------------------*/
static framei * framei_new(char * filename)
static framei *
framei_new (char *filename)
{
framei * fi ;
char * sval ;
char * sval2 ;
int i ;
framei *fi;
char *sval;
char *sval2;
int i;
fi = malloc(sizeof(framei));
fi->name = strdup(filename);
sval = qfits_query_hdr(filename, "tpl.id");
if (sval!=NULL) {
/* Filter out unwanted template IDs */
i=0 ;
while (tpl_filter[i]!=NULL) {
if (strstr(sval, tpl_filter[i])!=NULL) return NULL ;
i++ ;
}
fi->tplid = strdup(qfits_pretty_string(sval)) ;
} else {
fi->tplid = NULL ;
fi = malloc (sizeof (framei));
fi->name = strdup (filename);
sval = qfits_query_hdr (filename, "tpl.id");
if (sval != NULL)
{
/* Filter out unwanted template IDs */
i = 0;
while (tpl_filter[i] != NULL)
{
if (strstr (sval, tpl_filter[i]) != NULL)
return NULL;
i++;
}
fi->tplid = strdup (qfits_pretty_string (sval));
}
sval = qfits_query_hdr(filename, "origfile");
if (sval!=NULL) {
sval2 = qfits_pretty_string(sval) ;
fi->origfile = strdup(qfits_get_root_name(sval2)) ;
} else {
fi->origfile = NULL ;
else
{
fi->tplid = NULL;
}
sval = qfits_query_hdr(filename, "tpl.expno");
if (sval!=NULL) fi->expno = (int)atoi(sval);
else fi->expno = -1 ;
sval = qfits_query_hdr(filename, "tpl.nexp");
if (sval!=NULL) fi->nexp = (int)atoi(sval);
else fi->nexp = -1 ;
sval = qfits_query_hdr (filename, "origfile");
if (sval != NULL)
{
sval2 = qfits_pretty_string (sval);
fi->origfile = strdup (qfits_get_root_name (sval2));
}
else
{
fi->origfile = NULL;
}
sval = qfits_query_hdr (filename, "tpl.expno");
if (sval != NULL)
fi->expno = (int) atoi (sval);
else
fi->expno = -1;
sval = qfits_query_hdr (filename, "tpl.nexp");
if (sval != NULL)
fi->nexp = (int) atoi (sval);
else
fi->nexp = -1;
fi->next = NULL ;
return fi ;
fi->next = NULL;
return fi;
}
/*----------------------------------------------------------------------------*/
@ -116,18 +131,22 @@ static framei * framei_new(char * filename)
@param fi Object to delete
*/
/*----------------------------------------------------------------------------*/
static void framei_del(framei * fi)
static void
framei_del (framei * fi)
{
if (fi==NULL) return ;
if (fi->name!=NULL)
free(fi->name);
if (fi->tplid!=NULL) {
free(fi->tplid);
if (fi == NULL)
return;
if (fi->name != NULL)
free (fi->name);
if (fi->tplid != NULL)
{
free (fi->tplid);
}
if (fi->origfile!=NULL) {
free(fi->origfile);
if (fi->origfile != NULL)
{
free (fi->origfile);
}
free(fi);
free (fi);
}
/*----------------------------------------------------------------------------*/
@ -136,14 +155,15 @@ static void framei_del(framei * fi)
@return A newly allocated empty frameq object
*/
/*----------------------------------------------------------------------------*/
frameq * frameq_new(void)
frameq *
frameq_new (void)
{
frameq * fq ;
frameq *fq;
fq = malloc(sizeof(frameq));
fq->first = NULL ;
fq->n = 0 ;
return fq ;
fq = malloc (sizeof (frameq));
fq->first = NULL;
fq->n = 0;
return fq;
}
/*----------------------------------------------------------------------------*/
@ -152,21 +172,24 @@ frameq * frameq_new(void)
@param fq The object to delete
*/
/*----------------------------------------------------------------------------*/
void frameq_del(frameq * fq)
void
frameq_del (frameq * fq)
{
framei * fi ;
framei * fin ;
framei *fi;
framei *fin;
if (fq==NULL) return ;
if (fq == NULL)
return;
fi = fq->first ;
while (fi!=NULL) {
fin = fi->next ;
framei_del(fi);
fi = fin ;
fi = fq->first;
while (fi != NULL)
{
fin = fi->next;
framei_del (fi);
fi = fin;
}
free(fq);
return ;
free (fq);
return;
}
/*----------------------------------------------------------------------------*/
@ -176,28 +199,31 @@ void frameq_del(frameq * fq)
@param filename The file to append name
*/
/*----------------------------------------------------------------------------*/
void frameq_append(frameq * fq, char * filename)
void
frameq_append (frameq * fq, char *filename)
{
framei * fi ;
framei * fn ;
framei *fi;
framei *fn;
if (fq==NULL || filename==NULL) return ;
fi = framei_new(filename);
if (fi==NULL)
return ;
if (fq->n==0) {
fq->first = fi ;
fq->n = 1 ;
return ;
if (fq == NULL || filename == NULL)
return;
fi = framei_new (filename);
if (fi == NULL)
return;
if (fq->n == 0)
{
fq->first = fi;
fq->n = 1;
return;
}
fn = fq->first ;
while (fn->next!=NULL)
fn = fn->next ;
fn->next = fi ;
fq->n ++ ;
return ;
fn = fq->first;
while (fn->next != NULL)
fn = fn->next;
fn->next = fi;
fq->n++;
return;
}
/*----------------------------------------------------------------------------*/
@ -206,14 +232,15 @@ void frameq_append(frameq * fq, char * filename)
@param fq The frame queue
*/
/*----------------------------------------------------------------------------*/
void frameq_pop(frameq * fq)
void
frameq_pop (frameq * fq)
{
framei * first ;
framei *first;
first = fq->first->next ;
framei_del(fq->first);
fq->first = first ;
fq->n -- ;
first = fq->first->next;
framei_del (fq->first);
fq->first = first;
fq->n--;
}
/*----------------------------------------------------------------------------*/
@ -223,20 +250,18 @@ void frameq_pop(frameq * fq)
@param out File where the queue is dumped
*/
/*----------------------------------------------------------------------------*/
void frameq_dump(frameq * fq, FILE * out)
void
frameq_dump (frameq * fq, FILE * out)
{
int i ;
framei * fi ;
int i;
framei *fi;
fi = fq->first ;
for (i=0 ; i<fq->n ; i++) {
fprintf(out,
"%s %s %02d/%02d\n",
fi->name,
fi->tplid,
fi->expno,
fi->nexp);
fi = fi->next ;
fi = fq->first;
for (i = 0; i < fq->n; i++)
{
fprintf (out,
"%s %s %02d/%02d\n", fi->name, fi->tplid, fi->expno, fi->nexp);
fi = fi->next;
}
}
@ -247,42 +272,46 @@ void frameq_dump(frameq * fq, FILE * out)
@return A newly allocated frame queue
*/
/*----------------------------------------------------------------------------*/
frameq * frameq_load(char * dirname)
frameq *
frameq_load (char *dirname)
{
frameq * fq ;
int i ;
glob_t pglob ;
char filename[FILENAMESZ];
frameq *fq;
int i;
glob_t pglob;
char filename[FILENAMESZ];
/* Look for *.fits *.FITS *.fits.gz *.FITS.gz */
sprintf(filename, "%s/*.fits", dirname);
glob(filename, GLOB_MARK, NULL, &pglob);
sprintf(filename, "%s/*.FITS", dirname);
glob(filename, GLOB_APPEND, NULL, &pglob);
sprintf(filename, "%s/*.fits.gz", dirname);
glob(filename, GLOB_APPEND, NULL, &pglob);
sprintf(filename, "%s/*.FITS.gz", dirname);
glob(filename, GLOB_APPEND, NULL, &pglob);
if (pglob.gl_pathc<1) {
printf("found no frame\n");
return NULL ;
/* Look for *.fits *.FITS *.fits.gz *.FITS.gz */
sprintf (filename, "%s/*.fits", dirname);
glob (filename, GLOB_MARK, NULL, &pglob);
sprintf (filename, "%s/*.FITS", dirname);
glob (filename, GLOB_APPEND, NULL, &pglob);
sprintf (filename, "%s/*.fits.gz", dirname);
glob (filename, GLOB_APPEND, NULL, &pglob);
sprintf (filename, "%s/*.FITS.gz", dirname);
glob (filename, GLOB_APPEND, NULL, &pglob);
if (pglob.gl_pathc < 1)
{
printf ("found no frame\n");
return NULL;
}
/* Build frame queue */
fq = frameq_new();
for (i=0 ; i<pglob.gl_pathc ; i++) {
printf("\radding %d of %d", i+1, pglob.gl_pathc);
fflush(stdout);
frameq_append(fq, pglob.gl_pathv[i]);
/* Build frame queue */
fq = frameq_new ();
for (i = 0; i < pglob.gl_pathc; i++)
{
printf ("\radding %d of %d", i + 1, pglob.gl_pathc);
fflush (stdout);
frameq_append (fq, pglob.gl_pathv[i]);
}
printf("\n");
globfree(&pglob);
return fq ;
printf ("\n");
globfree (&pglob);
return fq;
}
static int stringsort(const void * e1, const void * e2)
static int
stringsort (const void *e1, const void *e2)
{
return strcmp(*(char**)e1, *(char**)e2);
return strcmp (*(char **) e1, *(char **) e2);
}
/*----------------------------------------------------------------------------*/
@ -293,53 +322,64 @@ static int stringsort(const void * e1, const void * e2)
@return List of n strings
*/
/*----------------------------------------------------------------------------*/
char ** frameq_get_tplid(frameq * fq, int * n)
char **
frameq_get_tplid (frameq * fq, int *n)
{
framei * fn ;
char ** tplid_all ;
char ** tplid ;
int i, j ;
int ntplid ;
framei *fn;
char **tplid_all;
char **tplid;
int i, j;
int ntplid;
/* Get all possible values for tplid */
tplid_all = malloc(fq->n * sizeof(char*));
fn = fq->first ;
for (i=0 ; i<fq->n ; i++) {
if (fn->tplid==NULL) {
tplid_all[i] = strdup("none");
} else {
tplid_all[i] = strdup(fn->tplid);
}
fn = fn->next ;
/* Get all possible values for tplid */
tplid_all = malloc (fq->n * sizeof (char *));
fn = fq->first;
for (i = 0; i < fq->n; i++)
{
if (fn->tplid == NULL)
{
tplid_all[i] = strdup ("none");
}
else
{
tplid_all[i] = strdup (fn->tplid);
}
fn = fn->next;
}
/* Sort all tplid's */
qsort(tplid_all, fq->n, sizeof(char*), stringsort);
/* Sort all tplid's */
qsort (tplid_all, fq->n, sizeof (char *), stringsort);
/* Compute how many different tplid's can be found */
ntplid=1 ;
for (i=1 ; i<fq->n ; i++) {
if (strcmp(tplid_all[i], tplid_all[i-1])) {
ntplid++ ;
}
/* Compute how many different tplid's can be found */
ntplid = 1;
for (i = 1; i < fq->n; i++)
{
if (strcmp (tplid_all[i], tplid_all[i - 1]))
{
ntplid++;
}
}
tplid = malloc(ntplid * sizeof(char*));
tplid[0] = tplid_all[0] ;
tplid_all[0] = NULL ;
j=0 ;
for (i=1 ; i<fq->n ; i++) {
if (strcmp(tplid_all[i], tplid[j])) {
j++ ;
tplid[j] = tplid_all[i] ;
} else {
free(tplid_all[i]);
}
tplid_all[i] = NULL ;
tplid = malloc (ntplid * sizeof (char *));
tplid[0] = tplid_all[0];
tplid_all[0] = NULL;
j = 0;
for (i = 1; i < fq->n; i++)
{
if (strcmp (tplid_all[i], tplid[j]))
{
j++;
tplid[j] = tplid_all[i];
}
else
{
free (tplid_all[i]);
}
tplid_all[i] = NULL;
}
free(tplid_all);
free (tplid_all);
*n = ntplid ;
return tplid ;
*n = ntplid;
return tplid;
}
/*----------------------------------------------------------------------------*/
@ -349,29 +389,34 @@ char ** frameq_get_tplid(frameq * fq, int * n)
@return Setting number
*/
/*----------------------------------------------------------------------------*/
int frameq_getsetnum(char * dirname)
int
frameq_getsetnum (char *dirname)
{
char pattern[FILENAMESZ];
glob_t pglob ;
int i ;
int max ;
int num ;
char pattern[FILENAMESZ];
glob_t pglob;
int i;
int max;
int num;
sprintf(pattern, "%s/set*", dirname);
glob(pattern, GLOB_MARK, NULL, &pglob);
if (pglob.gl_pathc<1) {
max=0 ;
} else {
sprintf(pattern, "%s/set%%02d", dirname);
max=0 ;
for (i=0 ; i<pglob.gl_pathc ; i++) {
sscanf(pglob.gl_pathv[i], pattern, &num);
if (num>max)
max=num ;
}
sprintf (pattern, "%s/set*", dirname);
glob (pattern, GLOB_MARK, NULL, &pglob);
if (pglob.gl_pathc < 1)
{
max = 0;
}
globfree(&pglob);
return max+1 ;
else
{
sprintf (pattern, "%s/set%%02d", dirname);
max = 0;
for (i = 0; i < pglob.gl_pathc; i++)
{
sscanf (pglob.gl_pathv[i], pattern, &num);
if (num > max)
max = num;
}
}
globfree (&pglob);
return max + 1;
}
/*----------------------------------------------------------------------------*/
@ -380,82 +425,93 @@ int frameq_getsetnum(char * dirname)
@param fq Frame queue
*/
/*----------------------------------------------------------------------------*/
void frameq_makelists(frameq * fq)
void
frameq_makelists (frameq * fq)
{
FILE * list ;
char filename[FILENAMESZ];
framei * fi ;
int setnum ;
int count ;
int batches ;
FILE *list;
char filename[FILENAMESZ];
framei *fi;
int setnum;
int count;
int batches;
/* Count # of batches in input */
fi = fq->first ;
batches=0 ;
while (fi!=NULL) {
if (fi->expno==1)
batches++ ;
fi = fi->next ;
/* Count # of batches in input */
fi = fq->first;
batches = 0;
while (fi != NULL)
{
if (fi->expno == 1)
batches++;
fi = fi->next;
}
fi = fq->first ;
count=0 ;
list=NULL ;
while (fi!=NULL) {
printf("\rclassifying batches: %d of %d", count, batches);
fflush(stdout);
if (fi->expno<0) {
fi=fi->next ;
continue ;
}
if (fi->expno==1) {
count++ ;
if (list!=NULL) {
fclose(list);
}
if (fi->tplid == NULL) {
printf("No TPL ID - abort\n") ;
return ;
}
if (fi->origfile == NULL) {
printf("No ORIGFILE - abort\n") ;
return ;
}
mkdir(fi->tplid, 0755);
setnum = frameq_getsetnum(fi->tplid);
sprintf(filename, "%s/%s_%02d", fi->tplid, fi->origfile, fi->nexp);
mkdir(filename, 0755);
sprintf(filename, "%s/%s_%02d/IN", fi->tplid,fi->origfile,fi->nexp);
list = fopen(filename, "w");
fprintf(list,"# TPL.ID= %s\n", fi->tplid);
fprintf(list,"# NEXP = %02d\n", fi->nexp);
}
if (list) fprintf(list, "%s\n", fi->name);
fi = fi->next ;
fi = fq->first;
count = 0;
list = NULL;
while (fi != NULL)
{
printf ("\rclassifying batches: %d of %d", count, batches);
fflush (stdout);
if (fi->expno < 0)
{
fi = fi->next;
continue;
}
if (fi->expno == 1)
{
count++;
if (list != NULL)
{
fclose (list);
}
if (fi->tplid == NULL)
{
printf ("No TPL ID - abort\n");
return;
}
if (fi->origfile == NULL)
{
printf ("No ORIGFILE - abort\n");
return;
}
mkdir (fi->tplid, 0755);
setnum = frameq_getsetnum (fi->tplid);
sprintf (filename, "%s/%s_%02d", fi->tplid, fi->origfile, fi->nexp);
mkdir (filename, 0755);
sprintf (filename, "%s/%s_%02d/IN", fi->tplid, fi->origfile,
fi->nexp);
list = fopen (filename, "w");
fprintf (list, "# TPL.ID= %s\n", fi->tplid);
fprintf (list, "# NEXP = %02d\n", fi->nexp);
}
if (list)
fprintf (list, "%s\n", fi->name);
fi = fi->next;
}
printf("\n");
return ;
printf ("\n");
return;
}
/*-----------------------------------------------------------------------------
Main
-----------------------------------------------------------------------------*/
int main(int argc, char * argv[])
int
main (int argc, char *argv[])
{
frameq * fq ;
frameq *fq;
if (argc<2) {
printf("use: %s <dirname>\n", argv[0]);
return 1 ;
}
printf("loading frames from %s...\n", argv[1]);
fq = frameq_load(argv[1]);
printf("processing lists...\n");
frameq_makelists(fq);
frameq_del(fq);
printf("done\n");
if (argc < 2)
{
printf ("use: %s <dirname>\n", argv[0]);
return 1;
}
return 0 ;
printf ("loading frames from %s...\n", argv[1]);
fq = frameq_load (argv[1]);
printf ("processing lists...\n");
frameq_makelists (fq);
frameq_del (fq);
printf ("done\n");
return 0;
}

View File

@ -5,7 +5,7 @@
@date Nov, 2002
@version $Revision: 1.5 $
@brief Extract a FITS extension and write it in a new FITS file
*/
*/
/*----------------------------------------------------------------------------*/
/*
@ -19,249 +19,275 @@
Includes
-----------------------------------------------------------------------------*/
#include "qfits.h"
#include "qfits.h"
/*-----------------------------------------------------------------------------
Function prototypes
-----------------------------------------------------------------------------*/
static int textract_write_ext(char *, int) ;
static int iextract_write_ext(char *, int) ;
static char prog_desc[] = "Extract and write FITS extensions" ;
static void usage(char *) ;
static int textract_write_ext (char *, int);
static int iextract_write_ext (char *, int);
static char prog_desc[] = "Extract and write FITS extensions";
static void usage (char *);
/*-----------------------------------------------------------------------------
Main
-----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
int
main (int argc, char *argv[])
{
char * name_in ;
int ext ;
int nb_ext ;
int ext_type ;
if (argc<3) usage(argv[0]) ;
/* Get input file name and extension number */
name_in = strdup(argv[1]) ;
ext = (int)atoi(argv[2]) ;
/* Check if the file is a FITS file */
if (!is_fits_file(name_in)) {
printf("[%s] is not a FITS file\n", name_in) ;
free(name_in) ;
return -1 ;
}
/* Check if the extension is valid */
nb_ext = qfits_query_n_ext(name_in) ;
if (nb_ext < ext) {
printf("Only %d extensions in this file\n", nb_ext) ;
free(name_in) ;
return -1 ;
char *name_in;
int ext;
int nb_ext;
int ext_type;
if (argc < 3)
usage (argv[0]);
/* Get input file name and extension number */
name_in = strdup (argv[1]);
ext = (int) atoi (argv[2]);
/* Check if the file is a FITS file */
if (!is_fits_file (name_in))
{
printf ("[%s] is not a FITS file\n", name_in);
free (name_in);
return -1;
}
/* Check if it's a table or an image */
ext_type = qfits_is_table(name_in, ext) ;
switch (ext_type) {
case QFITS_BINTABLE:
case QFITS_ASCIITABLE:
if (textract_write_ext(name_in, ext) == -1) {
printf("cannot read-write extension no %d\n", ext) ;
free(name_in) ;
return -1 ;
}
break ;
case QFITS_INVALIDTABLE:
if (iextract_write_ext(name_in, ext) == -1) {
printf("cannot read-write extension no %d\n", ext) ;
free(name_in) ;
return -1 ;
}
break ;
default:
printf("Unrecognized FITS type\n") ;
free(name_in) ;
return -1 ;
/* Check if the extension is valid */
nb_ext = qfits_query_n_ext (name_in);
if (nb_ext < ext)
{
printf ("Only %d extensions in this file\n", nb_ext);
free (name_in);
return -1;
}
free(name_in) ;
return 0 ;
/* Check if it's a table or an image */
ext_type = qfits_is_table (name_in, ext);
switch (ext_type)
{
case QFITS_BINTABLE:
case QFITS_ASCIITABLE:
if (textract_write_ext (name_in, ext) == -1)
{
printf ("cannot read-write extension no %d\n", ext);
free (name_in);
return -1;
}
break;
case QFITS_INVALIDTABLE:
if (iextract_write_ext (name_in, ext) == -1)
{
printf ("cannot read-write extension no %d\n", ext);
free (name_in);
return -1;
}
break;
default:
printf ("Unrecognized FITS type\n");
free (name_in);
return -1;
}
free (name_in);
return 0;
}
/*-----------------------------------------------------------------------------
Functions code
-----------------------------------------------------------------------------*/
static void usage(char * pname)
static void
usage (char *pname)
{
printf("%s : %s\n", pname, prog_desc) ;
printf(
"use : %s <in> <extension>\n"
"\n", pname) ;
exit(0) ;
printf ("%s : %s\n", pname, prog_desc);
printf ("use : %s <in> <extension>\n" "\n", pname);
exit (0);
}
static int textract_write_ext(
char * in,
int ext)
static int
textract_write_ext (char *in, int ext)
{
qfits_table * th ;
void ** array ;
qfits_header * fh ;
int array_size ;
int i ;
qfits_table *th;
void **array;
qfits_header *fh;
int array_size;
int i;
/* Get the table infos */
if ((th = qfits_table_open(in, ext)) == NULL) {
printf("cannot read extension: %d\n", ext) ;
return -1 ;
}
/* Compute array_size */
array_size = 0 ;
for (i=0 ; i<th->nc ; i++) {
switch (th->col[i].atom_type) {
case TFITS_ASCII_TYPE_A:
case TFITS_ASCII_TYPE_I:
case TFITS_ASCII_TYPE_E:
case TFITS_ASCII_TYPE_F:
case TFITS_ASCII_TYPE_D:
case TFITS_BIN_TYPE_A:
case TFITS_BIN_TYPE_L:
case TFITS_BIN_TYPE_B:
case TFITS_BIN_TYPE_X:
array_size += sizeof(char*) ;
break ;
case TFITS_BIN_TYPE_I:
array_size += sizeof(short*) ;
break ;
case TFITS_BIN_TYPE_J:
case TFITS_BIN_TYPE_E:
array_size += sizeof(int*) ;
break ;
case TFITS_BIN_TYPE_C:
case TFITS_BIN_TYPE_P:
array_size += sizeof(float*) ;
break ;
case TFITS_BIN_TYPE_D:
case TFITS_BIN_TYPE_M:
array_size += sizeof(double*) ;
break ;
default:
return -1 ;
}
}
/* Allocate memory for array */
array = malloc(array_size) ;
/* Load columns in array */
for (i=0 ; i<th->nc ; i++) {
array[i] = qfits_query_column_data(th, i, NULL, NULL) ;
if (array[i] == NULL) {
printf("cannot read column %d\n", i+1) ;
}
/* Get the table infos */
if ((th = qfits_table_open (in, ext)) == NULL)
{
printf ("cannot read extension: %d\n", ext);
return -1;
}
/* Update th : filename */
sprintf(th->filename, "ext%d.tfits", ext) ;
/* Compute array_size */
array_size = 0;
for (i = 0; i < th->nc; i++)
{
switch (th->col[i].atom_type)
{
case TFITS_ASCII_TYPE_A:
case TFITS_ASCII_TYPE_I:
case TFITS_ASCII_TYPE_E:
case TFITS_ASCII_TYPE_F:
case TFITS_ASCII_TYPE_D:
case TFITS_BIN_TYPE_A:
case TFITS_BIN_TYPE_L:
case TFITS_BIN_TYPE_B:
case TFITS_BIN_TYPE_X:
array_size += sizeof (char *);
break;
/* Get fits primary header */
if ((fh = qfits_header_read(in)) == NULL) {
for (i=0 ; i<th->nc ; i++) if (array[i] != NULL) free(array[i]) ;
qfits_table_close(th) ;
free(array) ;
printf("cannot read fits header\n") ;
return -1 ;
}
if (ext != 0) {
/* No data in primary HDU */
qfits_header_mod(fh, "NAXIS", "0", NULL) ;
qfits_header_del(fh, "NAXIS1") ;
qfits_header_del(fh, "NAXIS2") ;
}
case TFITS_BIN_TYPE_I:
array_size += sizeof (short *);
break;
/* Write the tfits file */
if (qfits_save_table_hdrdump((void **)array, th, fh) == -1) {
qfits_header_destroy(fh) ;
for (i=0 ; i<th->nc ; i++) if (array[i] != NULL) free(array[i]) ;
qfits_table_close(th) ;
free(array) ;
printf("cannot write fits table\n") ;
return -1 ;
case TFITS_BIN_TYPE_J:
case TFITS_BIN_TYPE_E:
array_size += sizeof (int *);
break;
case TFITS_BIN_TYPE_C:
case TFITS_BIN_TYPE_P:
array_size += sizeof (float *);
break;
case TFITS_BIN_TYPE_D:
case TFITS_BIN_TYPE_M:
array_size += sizeof (double *);
break;
default:
return -1;
}
}
/* Free and return */
qfits_header_destroy(fh) ;
for (i=0 ; i<th->nc ; i++) if (array[i] != NULL) free(array[i]) ;
qfits_table_close(th) ;
free(array) ;
return 0 ;
/* Allocate memory for array */
array = malloc (array_size);
/* Load columns in array */
for (i = 0; i < th->nc; i++)
{
array[i] = qfits_query_column_data (th, i, NULL, NULL);
if (array[i] == NULL)
{
printf ("cannot read column %d\n", i + 1);
}
}
/* Update th : filename */
sprintf (th->filename, "ext%d.tfits", ext);
/* Get fits primary header */
if ((fh = qfits_header_read (in)) == NULL)
{
for (i = 0; i < th->nc; i++)
if (array[i] != NULL)
free (array[i]);
qfits_table_close (th);
free (array);
printf ("cannot read fits header\n");
return -1;
}
if (ext != 0)
{
/* No data in primary HDU */
qfits_header_mod (fh, "NAXIS", "0", NULL);
qfits_header_del (fh, "NAXIS1");
qfits_header_del (fh, "NAXIS2");
}
/* Write the tfits file */
if (qfits_save_table_hdrdump ((void **) array, th, fh) == -1)
{
qfits_header_destroy (fh);
for (i = 0; i < th->nc; i++)
if (array[i] != NULL)
free (array[i]);
qfits_table_close (th);
free (array);
printf ("cannot write fits table\n");
return -1;
}
/* Free and return */
qfits_header_destroy (fh);
for (i = 0; i < th->nc; i++)
if (array[i] != NULL)
free (array[i]);
qfits_table_close (th);
free (array);
return 0;
}
static int iextract_write_ext(
char * in,
int ext)
static int
iextract_write_ext (char *in, int ext)
{
qfitsloader ql ;
qfitsdumper qd ;
qfits_header * fh ;
char outname[1024] ;
FILE * out ;
sprintf(outname, "ext%d.fits", ext) ;
/* Initialize a FITS loader */
ql.filename = in ;
ql.xtnum = ext ;
ql.pnum = 0 ;
ql.map = 1 ;
ql.ptype = PTYPE_DOUBLE ;
if (qfitsloader_init(&ql)!=0) return -1 ;
/* Load the primary header */
if ((fh = qfits_header_read(ql.filename)) == NULL) return -1 ;
qfitsloader ql;
qfitsdumper qd;
qfits_header *fh;
char outname[1024];
FILE *out;
if (ext != 0) {
/* No data in primary HDU */
qfits_header_mod(fh, "NAXIS", "0", NULL) ;
qfits_header_del(fh, "NAXIS1") ;
qfits_header_del(fh, "NAXIS2") ;
}
/* Dump the primary header */
if ((out = fopen(outname, "w")) == NULL) return -1 ;
qfits_header_dump(fh, out);
qfits_header_destroy(fh) ;
sprintf (outname, "ext%d.fits", ext);
if (ext != 0) {
/* Load the extension header */
if ((fh = qfits_header_readext(ql.filename, ext)) == NULL) return -1 ;
/* Dump the extension header */
qfits_header_dump(fh, out);
qfits_header_destroy(fh) ;
/* Initialize a FITS loader */
ql.filename = in;
ql.xtnum = ext;
ql.pnum = 0;
ql.map = 1;
ql.ptype = PTYPE_DOUBLE;
if (qfitsloader_init (&ql) != 0)
return -1;
/* Load the primary header */
if ((fh = qfits_header_read (ql.filename)) == NULL)
return -1;
if (ext != 0)
{
/* No data in primary HDU */
qfits_header_mod (fh, "NAXIS", "0", NULL);
qfits_header_del (fh, "NAXIS1");
qfits_header_del (fh, "NAXIS2");
}
fclose(out) ;
/* Load the FITS image */
if (qfits_loadpix(&ql)!=0) return -1 ;
/* Write the FITS image */
qd.filename = outname ;
qd.npix = ql.lx * ql.ly ;
qd.ptype = PTYPE_DOUBLE ;
qd.dbuf = ql.dbuf ;
qd.out_ptype = ql.bitpix ;
if (qfits_pixdump(&qd)!=0) return -1 ;
/* qfits_zeropad(outname) ; */
free(ql.dbuf) ;
return 0 ;
/* Dump the primary header */
if ((out = fopen (outname, "w")) == NULL)
return -1;
qfits_header_dump (fh, out);
qfits_header_destroy (fh);
if (ext != 0)
{
/* Load the extension header */
if ((fh = qfits_header_readext (ql.filename, ext)) == NULL)
return -1;
/* Dump the extension header */
qfits_header_dump (fh, out);
qfits_header_destroy (fh);
}
fclose (out);
/* Load the FITS image */
if (qfits_loadpix (&ql) != 0)
return -1;
/* Write the FITS image */
qd.filename = outname;
qd.npix = ql.lx * ql.ly;
qd.ptype = PTYPE_DOUBLE;
qd.dbuf = ql.dbuf;
qd.out_ptype = ql.bitpix;
if (qfits_pixdump (&qd) != 0)
return -1;
/* qfits_zeropad(outname) ; */
free (ql.dbuf);
return 0;
}

View File

@ -19,7 +19,7 @@
Include
-----------------------------------------------------------------------------*/
#include "qfits.h"
#include "qfits.h"
/*-----------------------------------------------------------------------------
Define
@ -31,182 +31,231 @@
Functions prototypes
-----------------------------------------------------------------------------*/
static char prog_desc[] = "replace keyword in a FITS header" ;
static void usage(char *) ;
static char prog_desc[] = "replace keyword in a FITS header";
static void usage (char *);
/*-----------------------------------------------------------------------------
Main
-----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
int
main (int argc, char *argv[])
{
char * name_in ;
char * card ;
char * place ;
char * key ;
char * key_tmp ;
char * val ;
char * val_tmp ;
char * com ;
char * com_tmp ;
int keep_com ;
int numeric ;
char * stmp ;
int i ;
char card_tmp[NM_SIZ] ;
char *name_in;
char *card;
char *place;
char *key;
char *key_tmp;
char *val;
char *val_tmp;
char *com;
char *com_tmp;
int keep_com;
int numeric;
char *stmp;
int i;
if (argc<2) usage(argv[0]) ;
/* Initialize */
name_in = NULL ;
card = NULL ;
place = NULL ;
key = NULL ;
val = NULL ;
com = NULL ;
keep_com = 0 ;
numeric = 0 ;
char card_tmp[NM_SIZ];
/* Command line handling */
i=1 ;
while (i<argc) {
if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
usage(argv[0]);
} else if (!strcmp(argv[i], "-p")) {
if ((i+1)>=argc) {
fprintf(stderr, "option -p needs an argument\n");
return -1 ;
}
i++ ;
place = strdup(argv[i]);
} else if (!strcmp(argv[i], "-k")) {
if ((i+1)>=argc) {
fprintf(stderr, "option -k needs an argument\n");
return -1 ;
}
i++ ;
key = strdup(argv[i]);
} else if (!strcmp(argv[i], "-v")) {
if ((i+1)>=argc) {
fprintf(stderr, "option -v needs an argument\n");
return -1 ;
}
i++ ;
val = strdup(argv[i]);
} else if (!strcmp(argv[i], "-c")) {
if ((i+1)>=argc) {
fprintf(stderr, "option -c needs an argument\n");
return -1 ;
}
i++ ;
com = strdup(argv[i]);
} else if (!strcmp(argv[i], "-C")) {
keep_com = 1 ;
} else if (!strcmp(argv[i], "-n")) {
numeric = 1 ;
} else {
break ;
}
i++ ;
}
/* Check options coherence */
if ((keep_com == 1) && (com != NULL)) {
fprintf(stderr, "options -c and -C should not be used together\n") ;
if (place) free(place) ;
if (key) free(key) ;
if (val) free(val) ;
free(com) ;
return -1 ;
}
if (place == NULL) {
fprintf(stderr, "options -p has to be used\n") ;
if (key) free(key) ;
if (val) free(val) ;
if (com) free(com) ;
return -1 ;
if (argc < 2)
usage (argv[0]);
/* Initialize */
name_in = NULL;
card = NULL;
place = NULL;
key = NULL;
val = NULL;
com = NULL;
keep_com = 0;
numeric = 0;
/* Command line handling */
i = 1;
while (i < argc)
{
if (!strcmp (argv[i], "--help") || !strcmp (argv[i], "-h"))
{
usage (argv[0]);
}
else if (!strcmp (argv[i], "-p"))
{
if ((i + 1) >= argc)
{
fprintf (stderr, "option -p needs an argument\n");
return -1;
}
i++;
place = strdup (argv[i]);
}
else if (!strcmp (argv[i], "-k"))
{
if ((i + 1) >= argc)
{
fprintf (stderr, "option -k needs an argument\n");
return -1;
}
i++;
key = strdup (argv[i]);
}
else if (!strcmp (argv[i], "-v"))
{
if ((i + 1) >= argc)
{
fprintf (stderr, "option -v needs an argument\n");
return -1;
}
i++;
val = strdup (argv[i]);
}
else if (!strcmp (argv[i], "-c"))
{
if ((i + 1) >= argc)
{
fprintf (stderr, "option -c needs an argument\n");
return -1;
}
i++;
com = strdup (argv[i]);
}
else if (!strcmp (argv[i], "-C"))
{
keep_com = 1;
}
else if (!strcmp (argv[i], "-n"))
{
numeric = 1;
}
else
{
break;
}
i++;
}
/* Get input file name */
if ((argc-i)<1) {
fprintf(stderr, "missing input file name\n");
return -1 ;
/* Check options coherence */
if ((keep_com == 1) && (com != NULL))
{
fprintf (stderr, "options -c and -C should not be used together\n");
if (place)
free (place);
if (key)
free (key);
if (val)
free (val);
free (com);
return -1;
}
/* Loop on the input files */
while (argc-i >= 1) {
name_in = strdup(argv[i]) ;
/* Set keyword to write */
key_tmp = NULL ;
if (key==NULL) key_tmp = strdup(place) ;
else key_tmp = strdup(key) ;
/* Set value to write */
val_tmp = NULL ;
if (val==NULL) {
card = qfits_query_card(name_in, place) ;
if (card!= NULL) {
stmp = qfits_getvalue(card) ;
val_tmp = strdup(stmp) ;
}
} else val_tmp = strdup(val) ;
com_tmp = NULL ;
/* Set comment to write */
if ((com == NULL) && (keep_com == 1)) {
if (card == NULL) card = qfits_query_card(name_in, place) ;
if (card != NULL) {
stmp = qfits_getcomment(card) ;
com_tmp = strdup(stmp) ;
}
} else if (com != NULL) com_tmp = strdup(com) ;
if (card != NULL) free(card) ;
card = NULL ;
printf("DEBUG: %s\n", key_tmp) ;
/* Create the card */
keytuple2str(card_tmp, key_tmp, val_tmp, com_tmp) ;
if (key_tmp) free(key_tmp) ;
if (val_tmp) free(val_tmp) ;
if (com_tmp) free(com_tmp) ;
card = strdup(card_tmp) ;
/* Display what will be written where */
printf("File %s\n", name_in) ;
printf("\tcard : \n\t\t%s\n", card) ;
printf("\tplace : \n\t\t%s\n", place) ;
/* Try to replace the first key */
if (qfits_replace_card(name_in, place, card) == -1) {
fprintf(stderr, "cannot replace the key %s\n", place) ;
}
free(name_in);
free(card) ;
card = NULL ;
i++ ;
if (place == NULL)
{
fprintf (stderr, "options -p has to be used\n");
if (key)
free (key);
if (val)
free (val);
if (com)
free (com);
return -1;
}
if (val) free(val) ;
if (com) free(com) ;
if (key) free(key) ;
free(place) ;
/* Free and return */
return 0 ;
/* Get input file name */
if ((argc - i) < 1)
{
fprintf (stderr, "missing input file name\n");
return -1;
}
/* Loop on the input files */
while (argc - i >= 1)
{
name_in = strdup (argv[i]);
/* Set keyword to write */
key_tmp = NULL;
if (key == NULL)
key_tmp = strdup (place);
else
key_tmp = strdup (key);
/* Set value to write */
val_tmp = NULL;
if (val == NULL)
{
card = qfits_query_card (name_in, place);
if (card != NULL)
{
stmp = qfits_getvalue (card);
val_tmp = strdup (stmp);
}
}
else
val_tmp = strdup (val);
com_tmp = NULL;
/* Set comment to write */
if ((com == NULL) && (keep_com == 1))
{
if (card == NULL)
card = qfits_query_card (name_in, place);
if (card != NULL)
{
stmp = qfits_getcomment (card);
com_tmp = strdup (stmp);
}
}
else if (com != NULL)
com_tmp = strdup (com);
if (card != NULL)
free (card);
card = NULL;
printf ("DEBUG: %s\n", key_tmp);
/* Create the card */
keytuple2str (card_tmp, key_tmp, val_tmp, com_tmp);
if (key_tmp)
free (key_tmp);
if (val_tmp)
free (val_tmp);
if (com_tmp)
free (com_tmp);
card = strdup (card_tmp);
/* Display what will be written where */
printf ("File %s\n", name_in);
printf ("\tcard : \n\t\t%s\n", card);
printf ("\tplace : \n\t\t%s\n", place);
/* Try to replace the first key */
if (qfits_replace_card (name_in, place, card) == -1)
{
fprintf (stderr, "cannot replace the key %s\n", place);
}
free (name_in);
free (card);
card = NULL;
i++;
}
if (val)
free (val);
if (com)
free (com);
if (key)
free (key);
free (place);
/* Free and return */
return 0;
}
static void usage(char * pname)
static void
usage (char *pname)
{
printf("%s : %s\n", pname, prog_desc) ;
printf(
"use : %s [options] <in>\n"
"options are:\n"
"\t-p place gives the keyword to write over (required).\n"
"\t-k key gives the new keyword name (optional).\n"
"\t-v val gives the value to write (optional).\n"
"\t-c com gives the comment to write (optional).\n"
"\t-C flag to keep comment\n"
"\n", pname) ;
exit(0) ;
printf ("%s : %s\n", pname, prog_desc);
printf ("use : %s [options] <in>\n"
"options are:\n"
"\t-p place gives the keyword to write over (required).\n"
"\t-k key gives the new keyword name (optional).\n"
"\t-v val gives the value to write (optional).\n"
"\t-c com gives the comment to write (optional).\n"
"\t-C flag to keep comment\n" "\n", pname);
exit (0);
}

File diff suppressed because it is too large Load Diff

View File

@ -52,8 +52,8 @@
Define
-----------------------------------------------------------------------------*/
#define MAX_STRING 128
#define MAX_KEY 512
#define MAX_STRING 128
#define MAX_KEY 512
#define FMT_STRING "%%-%ds\t"
/*-----------------------------------------------------------------------------
@ -61,147 +61,178 @@
-----------------------------------------------------------------------------*/
/* This holds a keyword value and a flag to indicate its presence */
typedef struct _KEYWORD_ {
char value[MAX_STRING] ;
int present ;
} keyword ;
typedef struct _KEYWORD_
{
char value[MAX_STRING];
int present;
} keyword;
/* Each detected file in input has such an associated structure */
typedef struct _RECORD_ {
char filename[MAX_STRING] ;
keyword listkw[MAX_KEY] ;
} record ;
typedef struct _RECORD_
{
char filename[MAX_STRING];
keyword listkw[MAX_KEY];
} record;
/*-----------------------------------------------------------------------------
Function prototypes
-----------------------------------------------------------------------------*/
static int isfilename(char *string) ;
static void getfilename(char *line, char *word) ;
static char * expand_hierarch_keyword(char *, char *) ;
static int isdetectedkeyword(char *line, char *keywords[], int nkeys) ;
static void getkeywordvalue(char *line, char *word) ;
static int isfilename (char *string);
static void getfilename (char *line, char *word);
static char *expand_hierarch_keyword (char *, char *);
static int isdetectedkeyword (char *line, char *keywords[], int nkeys);
static void getkeywordvalue (char *line, char *word);
/*-----------------------------------------------------------------------------
Main
-----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
int
main (int argc, char *argv[])
{
char curline[MAX_STRING] ;
char word[MAX_STRING] ;
int i, j ;
int nfiles ;
record *allrecords ;
int kwnum ;
int len ;
int max_width[MAX_KEY] ;
int max_filnam ;
char fmt[8] ;
int flag ;
int printnames ;
int print_hdr ;
char curline[MAX_STRING];
char word[MAX_STRING];
int i, j;
int nfiles;
record *allrecords;
int kwnum;
int len;
int max_width[MAX_KEY];
int max_filnam;
char fmt[8];
int flag;
int printnames;
int print_hdr;
if (argc<2) {
printf("\n\nuse : %s [-d] KEY1 KEY2 ... KEYn\n", argv[0]) ;
printf("Input data is received from stdin\n") ;
printf("See man page for more details and examples\n\n") ;
return 0 ;
if (argc < 2)
{
printf ("\n\nuse : %s [-d] KEY1 KEY2 ... KEYn\n", argv[0]);
printf ("Input data is received from stdin\n");
printf ("See man page for more details and examples\n\n");
return 0;
}
/* Initialize */
printnames = 0 ;
print_hdr = 1 ;
nfiles = 0 ;
allrecords = (record*)calloc(1, sizeof(record));
if (!strcmp(argv[1], "-d")) {
print_hdr = 0;
argv++ ;
argc-- ;
}
argv++ ;
/* Uppercase all inputs */
for (i=0 ; i<(argc-1) ; i++) {
j=0 ;
while (argv[i][j]!=0) {
argv[i][j] = toupper(argv[i][j]);
j++ ;
}
}
while (fgets(curline, MAX_STRING, stdin) != (char*)NULL) {
flag=isfilename(curline) ;
if (flag == 1) {
/* New file name is detected, get the new file name */
printnames = 1 ;
getfilename(curline, allrecords[nfiles].filename) ;
nfiles++ ;
/* Initialize a new record structure to store data for this file. */
allrecords = (record*)realloc(allrecords,(nfiles+1)*sizeof(record));
for (i=0 ; i<MAX_KEY ; i++) allrecords[nfiles].listkw[i].present=0;
} else if (flag==0) {
/* Is not a file name, is it a searched keyword? */
if ((kwnum = isdetectedkeyword( curline, argv, argc-1)) != -1) {
/* Is there anything allocated yet to store this? */
if (nfiles>0) {
/* It has been detected as a searched keyword. */
/* Get its value, store it, present flag up */
getkeywordvalue(curline, word) ;
strcpy(allrecords[nfiles-1].listkw[kwnum].value, word) ;
allrecords[nfiles-1].listkw[kwnum].present ++ ;
}
}
}
/* Initialize */
printnames = 0;
print_hdr = 1;
nfiles = 0;
allrecords = (record *) calloc (1, sizeof (record));
if (!strcmp (argv[1], "-d"))
{
print_hdr = 0;
argv++;
argc--;
}
for (i=0 ; i<argc-1 ; i++) max_width[i] = (int)strlen(argv[i]) ;
argv++;
/* Record the maximum width for each column */
max_filnam = 0 ;
for (i=0 ; i<nfiles ; i++) {
len = (int)strlen(allrecords[i].filename) ;
if (len>max_filnam) max_filnam=len ;
for (kwnum=0 ; kwnum<argc-1 ; kwnum++) {
if (allrecords[i].listkw[kwnum].present) {
len = (int)strlen(allrecords[i].listkw[kwnum].value) ;
} else {
len = 0 ;
}
if (len>max_width[kwnum]) max_width[kwnum] = len ;
}
/* Uppercase all inputs */
for (i = 0; i < (argc - 1); i++)
{
j = 0;
while (argv[i][j] != 0)
{
argv[i][j] = toupper (argv[i][j]);
j++;
}
/* Print out header line */
if (print_hdr) {
sprintf(fmt, FMT_STRING, max_filnam) ;
if (printnames) printf(fmt, "FILE");
for (i=0 ; i<argc-1 ; i++) {
sprintf(fmt, FMT_STRING, max_width[i]) ;
printf(fmt, argv[i]) ;
}
printf("\n") ;
}
/* Now print out stored data */
if (nfiles<1) {
printf("*** error: no input data corresponding to dfits output\n");
return -1 ;
}
for (i=0 ; i<nfiles ; i++) {
if (printnames) {
sprintf(fmt, FMT_STRING, max_filnam) ;
printf(fmt, allrecords[i].filename) ;
}
for (kwnum=0 ; kwnum<argc-1 ; kwnum++) {
sprintf(fmt, FMT_STRING, max_width[kwnum]);
if (allrecords[i].listkw[kwnum].present)
printf(fmt, allrecords[i].listkw[kwnum].value) ;
else printf(fmt, " ");
}
printf("\n") ;
}
free(allrecords) ;
return 0 ;
while (fgets (curline, MAX_STRING, stdin) != (char *) NULL)
{
flag = isfilename (curline);
if (flag == 1)
{
/* New file name is detected, get the new file name */
printnames = 1;
getfilename (curline, allrecords[nfiles].filename);
nfiles++;
/* Initialize a new record structure to store data for this file. */
allrecords =
(record *) realloc (allrecords, (nfiles + 1) * sizeof (record));
for (i = 0; i < MAX_KEY; i++)
allrecords[nfiles].listkw[i].present = 0;
}
else if (flag == 0)
{
/* Is not a file name, is it a searched keyword? */
if ((kwnum = isdetectedkeyword (curline, argv, argc - 1)) != -1)
{
/* Is there anything allocated yet to store this? */
if (nfiles > 0)
{
/* It has been detected as a searched keyword. */
/* Get its value, store it, present flag up */
getkeywordvalue (curline, word);
strcpy (allrecords[nfiles - 1].listkw[kwnum].value, word);
allrecords[nfiles - 1].listkw[kwnum].present++;
}
}
}
}
for (i = 0; i < argc - 1; i++)
max_width[i] = (int) strlen (argv[i]);
/* Record the maximum width for each column */
max_filnam = 0;
for (i = 0; i < nfiles; i++)
{
len = (int) strlen (allrecords[i].filename);
if (len > max_filnam)
max_filnam = len;
for (kwnum = 0; kwnum < argc - 1; kwnum++)
{
if (allrecords[i].listkw[kwnum].present)
{
len = (int) strlen (allrecords[i].listkw[kwnum].value);
}
else
{
len = 0;
}
if (len > max_width[kwnum])
max_width[kwnum] = len;
}
}
/* Print out header line */
if (print_hdr)
{
sprintf (fmt, FMT_STRING, max_filnam);
if (printnames)
printf (fmt, "FILE");
for (i = 0; i < argc - 1; i++)
{
sprintf (fmt, FMT_STRING, max_width[i]);
printf (fmt, argv[i]);
}
printf ("\n");
}
/* Now print out stored data */
if (nfiles < 1)
{
printf ("*** error: no input data corresponding to dfits output\n");
return -1;
}
for (i = 0; i < nfiles; i++)
{
if (printnames)
{
sprintf (fmt, FMT_STRING, max_filnam);
printf (fmt, allrecords[i].filename);
}
for (kwnum = 0; kwnum < argc - 1; kwnum++)
{
sprintf (fmt, FMT_STRING, max_width[kwnum]);
if (allrecords[i].listkw[kwnum].present)
printf (fmt, allrecords[i].listkw[kwnum].value);
else
printf (fmt, " ");
}
printf ("\n");
}
free (allrecords);
return 0;
}
/*-----------------------------------------------------------------------------
@ -218,11 +249,14 @@ int main(int argc, char *argv[])
extension, 0 otherwise.
*/
/*----------------------------------------------------------------------------*/
static int isfilename(char * string)
static int
isfilename (char *string)
{
if (!strncmp(string, "====>", 5)) return 1 ;
if (!strncmp(string, "===>", 4)) return 2 ;
return 0 ;
if (!strncmp (string, "====>", 5))
return 1;
if (!strncmp (string, "===>", 4))
return 2;
return 0;
}
/*----------------------------------------------------------------------------*/
@ -234,13 +268,14 @@ static int isfilename(char * string)
This is dfits dependent.
*/
/*----------------------------------------------------------------------------*/
static void getfilename(char * line, char * word)
static void
getfilename (char *line, char *word)
{
/* get filename from a dfits output */
sscanf(line, "%*s %*s %s", word) ;
return ;
/* get filename from a dfits output */
sscanf (line, "%*s %*s %s", word);
return;
}
/*----------------------------------------------------------------------------*/
/**
@brief detects a if a keyword is present in a FITS line
@ -254,41 +289,45 @@ static void getfilename(char * line, char * word)
otherwise, -1 is returned.
*/
/*----------------------------------------------------------------------------*/
static int isdetectedkeyword(
char * line,
char * keywords[],
int nkeys)
static int
isdetectedkeyword (char *line, char *keywords[], int nkeys)
{
char kw[MAX_STRING] ;
char esokw[MAX_STRING] ;
int i ;
char kw[MAX_STRING];
char esokw[MAX_STRING];
int i;
/* The keyword is up to the equal character, with trailing blanks removed */
strcpy(kw, line) ;
strtok(kw, "=") ;
/* Now remove all trailing blanks (if any) */
i = (int)strlen(kw) -1 ;
while (kw[i] == ' ') i -- ;
kw[i+1] = (char)0 ;
/* Now compare what we got with what's available */
for (i=0 ; i<nkeys ; i++) {
if (strstr(keywords[i], ".")!=NULL) {
/*
* keyword contains a dot, it is a hierarchical keyword that
* must be expanded. Pattern is:
* A.B.C... becomes HIERARCH ESO A B C ...
*/
expand_hierarch_keyword(keywords[i], esokw) ;
if (!strcmp(kw, esokw)) {
return i ;
}
} else if (!strcmp(kw, keywords[i])) {
return i ;
}
/* The keyword is up to the equal character, with trailing blanks removed */
strcpy (kw, line);
strtok (kw, "=");
/* Now remove all trailing blanks (if any) */
i = (int) strlen (kw) - 1;
while (kw[i] == ' ')
i--;
kw[i + 1] = (char) 0;
/* Now compare what we got with what's available */
for (i = 0; i < nkeys; i++)
{
if (strstr (keywords[i], ".") != NULL)
{
/*
* keyword contains a dot, it is a hierarchical keyword that
* must be expanded. Pattern is:
* A.B.C... becomes HIERARCH ESO A B C ...
*/
expand_hierarch_keyword (keywords[i], esokw);
if (!strcmp (kw, esokw))
{
return i;
}
}
else if (!strcmp (kw, keywords[i]))
{
return i;
}
}
/* Keyword not found */
return -1 ;
/* Keyword not found */
return -1;
}
/*----------------------------------------------------------------------------*/
@ -299,22 +338,22 @@ static int isdetectedkeyword(
@return char *, pointer to second input string (modified)
*/
/*----------------------------------------------------------------------------*/
static char * expand_hierarch_keyword(
char * dotkey,
char * hierarchy)
static char *
expand_hierarch_keyword (char *dotkey, char *hierarchy)
{
char * token ;
char ws[MAX_STRING] ;
char *token;
char ws[MAX_STRING];
sprintf(hierarchy, "HIERARCH ESO");
strcpy(ws, dotkey) ;
token = strtok(ws, ".") ;
while (token!=NULL) {
strcat(hierarchy, " ") ;
strcat(hierarchy, token) ;
token = strtok(NULL, ".");
}
return hierarchy ;
sprintf (hierarchy, "HIERARCH ESO");
strcpy (ws, dotkey);
token = strtok (ws, ".");
while (token != NULL)
{
strcat (hierarchy, " ");
strcat (hierarchy, token);
token = strtok (NULL, ".");
}
return hierarchy;
}
/*----------------------------------------------------------------------------*/
@ -326,48 +365,54 @@ static char * expand_hierarch_keyword(
No complex value is recognized
*/
/*----------------------------------------------------------------------------*/
static void getkeywordvalue(
char * line,
char * word)
static void
getkeywordvalue (char *line, char *word)
{
int c, w ;
char tmp[MAX_STRING] ;
char *begin, *end ;
int length ;
int quote = 0 ;
int search = 1 ;
int c, w;
char tmp[MAX_STRING];
char *begin, *end;
int length;
int quote = 0;
int search = 1;
memset(tmp, (char)0, MAX_STRING) ;
memset(word, (char)0, MAX_STRING) ;
c = w = 0;
memset (tmp, (char) 0, MAX_STRING);
memset (word, (char) 0, MAX_STRING);
c = w = 0;
/* Parse the line till the equal '=' sign is found */
while (line[c] != '=') c++ ;
c++ ;
/* Parse the line till the equal '=' sign is found */
while (line[c] != '=')
c++;
c++;
/* Copy the line till the slash '/' sign or the end of data is found. */
while (search == 1) {
if (c>=80) search = 0 ;
else if ((line[c] == '/') && (quote == 0)) search = 0 ;
if (line[c] == '\'') quote = !quote ;
tmp[w++] = line[c++] ;
/* Copy the line till the slash '/' sign or the end of data is found. */
while (search == 1)
{
if (c >= 80)
search = 0;
else if ((line[c] == '/') && (quote == 0))
search = 0;
if (line[c] == '\'')
quote = !quote;
tmp[w++] = line[c++];
}
/* NULL termination of the string */
tmp[--w] = (char)0 ;
/* Return the keyword only : a diff is made between text fields and nbs. */
if ((begin = strchr(tmp, '\'')) != (char*)NULL) {
/* A quote has been found: it is a string value */
begin++ ;
end = strrchr(tmp, '\'') ;
length = (int)strlen(begin) - (int)strlen(end) ;
strncpy(word, begin, length) ;
} else {
/* No quote, just get the value (only one, no complex supported) */
sscanf(tmp, "%s", word) ;
/* NULL termination of the string */
tmp[--w] = (char) 0;
/* Return the keyword only : a diff is made between text fields and nbs. */
if ((begin = strchr (tmp, '\'')) != (char *) NULL)
{
/* A quote has been found: it is a string value */
begin++;
end = strrchr (tmp, '\'');
length = (int) strlen (begin) - (int) strlen (end);
strncpy (word, begin, length);
}
return ;
else
{
/* No quote, just get the value (only one, no complex supported) */
sscanf (tmp, "%s", word);
}
return;
}

View File

@ -40,136 +40,136 @@
Function prototypes
-----------------------------------------------------------------------------*/
static char prog_desc[] = "header conversion from ESO to standard FITS" ;
static void usage(char *) ;
static int convert_eso_to_std_FITS(char *, char *) ;
static void free_keys(char **, int n) ;
static void strip_beg_end(char *) ;
static int search_and_replace_kw(char *, int, char **, char **, int) ;
static void search_rep(char *, char *, char *);
static void generate_default_convtab(void);
static char * convert_deg_to_str(double deg) ;
static char prog_desc[] = "header conversion from ESO to standard FITS";
static void usage (char *);
static int convert_eso_to_std_FITS (char *, char *);
static void free_keys (char **, int n);
static void strip_beg_end (char *);
static int search_and_replace_kw (char *, int, char **, char **, int);
static void search_rep (char *, char *, char *);
static void generate_default_convtab (void);
static char *convert_deg_to_str (double deg);
/*-----------------------------------------------------------------------------
Static variables
-----------------------------------------------------------------------------*/
static char CONVTAB_DEFAULT1[] =
"#\n"
"# Example of conversion table for hierarch28\n"
"#\n"
"# A note about this file's format:\n"
"# Any blank line or line starting with a hash is ignored.\n"
"# Declare the keyword names to search and replace with:\n"
"#\n"
"# OLDKEYWORD = NEWKEYWORD\n"
"#\n"
"# Spaces are allowed within keyword names, to allow e.g.:\n"
"#\n"
"# HIERARCH ESO DET NDIT = DET NDIT\n"
"#\n"
"# The most important restriction is that new keywords shall not be\n"
"# longer than the keywords they replace.\n"
"#\n" ;
"#\n"
"# Example of conversion table for hierarch28\n"
"#\n"
"# A note about this file's format:\n"
"# Any blank line or line starting with a hash is ignored.\n"
"# Declare the keyword names to search and replace with:\n"
"#\n"
"# OLDKEYWORD = NEWKEYWORD\n"
"#\n"
"# Spaces are allowed within keyword names, to allow e.g.:\n"
"#\n"
"# HIERARCH ESO DET NDIT = DET NDIT\n"
"#\n"
"# The most important restriction is that new keywords shall not be\n"
"# longer than the keywords they replace.\n" "#\n";
static char CONVTAB_DEFAULT2[] =
"#\n"
"# Translation table for basic keywords used by IRAF\n"
"# -------------------------------------------------\n"
"#\n"
"# Note: hierarch28 will replace keywords in the main header\n"
"# and also in extensions.\n"
"#\n"
"# Disclaimer:\n"
"# this table has been compiled to best knowledge of present\n"
"# IRAF packages. Please let us know of any addition/change.\n"
"#\n"
"\n" ;
"#\n"
"# Translation table for basic keywords used by IRAF\n"
"# -------------------------------------------------\n"
"#\n"
"# Note: hierarch28 will replace keywords in the main header\n"
"# and also in extensions.\n"
"#\n"
"# Disclaimer:\n"
"# this table has been compiled to best knowledge of present\n"
"# IRAF packages. Please let us know of any addition/change.\n"
"#\n" "\n";
static char CONVTAB_DEFAULT3[] =
"UTC = UT\n"
"LST = ST\n"
"RA = RA\n"
"DEC = DEC\n"
"\n"
"HIERARCH ESO TEL AIRM START = AIRMASS\n"
"HIERARCH ESO DPR TYPE = IMAGETYP\n"
"HIERARCH ESO INS FILT1 NAME = FILTER1\n"
"HIERARCH ESO INS SLIT2 NAME = SLIT\n"
"HIERARCH ESO INS GRIS1 NAME = GRISM\n"
"HIERARCH ESO INS GRAT NAME = GRAT\n"
"HIERARCH ESO INS GRAT1 NAME = GRAT1\n"
"HIERARCH ESO INS GRAT2 NAME = GRAT2\n"
"HIERARCH ESO INS GRAT WLEN = WLEN\n"
"HIERARCH ESO INS GRAT1 WLEN = WLEN1\n"
"HIERARCH ESO INS GRAT2 WLEN = WLEN2\n"
"HIERARCH ESO INS GRAT ORDER = ORDER\n"
"\n" ;
"UTC = UT\n"
"LST = ST\n"
"RA = RA\n"
"DEC = DEC\n"
"\n"
"HIERARCH ESO TEL AIRM START = AIRMASS\n"
"HIERARCH ESO DPR TYPE = IMAGETYP\n"
"HIERARCH ESO INS FILT1 NAME = FILTER1\n"
"HIERARCH ESO INS SLIT2 NAME = SLIT\n"
"HIERARCH ESO INS GRIS1 NAME = GRISM\n"
"HIERARCH ESO INS GRAT NAME = GRAT\n"
"HIERARCH ESO INS GRAT1 NAME = GRAT1\n"
"HIERARCH ESO INS GRAT2 NAME = GRAT2\n"
"HIERARCH ESO INS GRAT WLEN = WLEN\n"
"HIERARCH ESO INS GRAT1 WLEN = WLEN1\n"
"HIERARCH ESO INS GRAT2 WLEN = WLEN2\n"
"HIERARCH ESO INS GRAT ORDER = ORDER\n" "\n";
static char CONVTAB_DEFAULT4[] =
"#\n"
"# A note for IRAF users:\n"
"# Be aware also that the ESO convention names the keywords UTC and\n"
"# LST, whereas the IRAF convention is 'UT' and 'ST'.\n"
"#\n"
"# The ESO standard (see http://archive.eso.org/dicb) defines these\n"
"# keywords as floating point values with the units degrees for RA/DEC\n"
"# and elapsed seconds since midnight for UT/ST.\n"
"#\n"
"# In order to have this tranlation performed, add\n"
"# RA = RA\n"
"# DEC = DEC\n"
"# UTC = UT\n"
"# LST = ST\n"
"# to the conversion table.\n"
"#\n";
"#\n"
"# A note for IRAF users:\n"
"# Be aware also that the ESO convention names the keywords UTC and\n"
"# LST, whereas the IRAF convention is 'UT' and 'ST'.\n"
"#\n"
"# The ESO standard (see http://archive.eso.org/dicb) defines these\n"
"# keywords as floating point values with the units degrees for RA/DEC\n"
"# and elapsed seconds since midnight for UT/ST.\n"
"#\n"
"# In order to have this tranlation performed, add\n"
"# RA = RA\n"
"# DEC = DEC\n"
"# UTC = UT\n" "# LST = ST\n" "# to the conversion table.\n" "#\n";
/*-----------------------------------------------------------------------------
Main
-----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
int
main (int argc, char *argv[])
{
char name_conv[NM_SIZ] ;
char name_in[NM_SIZ] ;
char name_conv[NM_SIZ];
char name_in[NM_SIZ];
if (argc<2) usage(argv[0]) ;
if (!strcmp(argv[1], "-g")) {
generate_default_convtab() ;
return 0 ;
}
strcpy(name_in, argv[1]) ;
if (argc==3) {
strcpy(name_conv, argv[2]) ;
} else {
strcpy(name_conv, "table.conv") ;
if (argc < 2)
usage (argv[0]);
if (!strcmp (argv[1], "-g"))
{
generate_default_convtab ();
return 0;
}
if (convert_eso_to_std_FITS(name_in, name_conv) != 0) {
fprintf(stderr, "error during conversion: aborting\n") ;
strcpy (name_in, argv[1]);
if (argc == 3)
{
strcpy (name_conv, argv[2]);
}
return 0 ;
else
{
strcpy (name_conv, "table.conv");
}
if (convert_eso_to_std_FITS (name_in, name_conv) != 0)
{
fprintf (stderr, "error during conversion: aborting\n");
}
return 0;
}
static void usage(char * pname)
static void
usage (char *pname)
{
printf(
"\n\n"
"hierarch28 (hierarch-to-eight)\n"
"%s : %s\n"
"use : %s [options] <in> [table]\n"
"options are:\n"
"\t-g generates a generic table\n"
"\n"
"default conversion table name is 'table.conv'\n"
"\n"
"More help can be found in the comments included in the default\n"
"conversion table. Generate one with the -g option and read it.\n"
"\n\n",
pname, prog_desc, pname);
exit(0) ;
printf ("\n\n"
"hierarch28 (hierarch-to-eight)\n"
"%s : %s\n"
"use : %s [options] <in> [table]\n"
"options are:\n"
"\t-g generates a generic table\n"
"\n"
"default conversion table name is 'table.conv'\n"
"\n"
"More help can be found in the comments included in the default\n"
"conversion table. Generate one with the -g option and read it.\n"
"\n\n", pname, prog_desc, pname);
exit (0);
}
@ -185,132 +185,140 @@ static void usage(char * pname)
keyword values are also modified to follow the IRAF convention.
*/
/*----------------------------------------------------------------------------*/
static int convert_eso_to_std_FITS(char * name_in, char * name_conv)
static int
convert_eso_to_std_FITS (char *name_in, char *name_conv)
{
FILE * convtab ;
int nkeys ;
int i ;
char ** key_in ;
char ** key_out ;
int fd ;
char * buf ;
char line[NM_SIZ] ;
char kw1[FITS_LINE],
kw2[FITS_LINE] ;
int lineno ;
int fs ;
struct stat fileinfo ;
FILE *convtab;
int nkeys;
int i;
char **key_in;
char **key_out;
int fd;
char *buf;
char line[NM_SIZ];
char kw1[FITS_LINE], kw2[FITS_LINE];
int lineno;
int fs;
struct stat fileinfo;
/* Read conversion table and translate it to key_in, key_out */
if ((convtab = fopen(name_conv, "r")) == NULL) {
fprintf(stderr, "cannot open conversion table: %s\n", name_conv) ;
return -1 ;
/* Read conversion table and translate it to key_in, key_out */
if ((convtab = fopen (name_conv, "r")) == NULL)
{
fprintf (stderr, "cannot open conversion table: %s\n", name_conv);
return -1;
}
/* First, count how many keywords we need to translate */
nkeys = 0 ;
while (fgets(line, FITS_LINE, convtab)!=NULL) {
if ((line[0] != '#') && (line[0] != '\n')) {
nkeys ++ ;
}
/* First, count how many keywords we need to translate */
nkeys = 0;
while (fgets (line, FITS_LINE, convtab) != NULL)
{
if ((line[0] != '#') && (line[0] != '\n'))
{
nkeys++;
}
}
rewind(convtab) ;
rewind (convtab);
/* Allocate space to store keyword info */
key_in = malloc(nkeys * sizeof(char*)) ;
key_out = malloc(nkeys * sizeof(char*)) ;
/* Allocate space to store keyword info */
key_in = malloc (nkeys * sizeof (char *));
key_out = malloc (nkeys * sizeof (char *));
/* Now read the file through and get the keywords */
i = 0 ;
lineno = 0 ;
while (fgets(line, FITS_LINE, convtab)!=NULL) {
lineno++ ;
if ((line[0]!='#') && (line[0]!='\n')) {
if (sscanf(line, "%[^=] = %[^;#]", kw1, kw2)!=2) {
fprintf(stderr,
"*** error parsing table file %s\n", name_conv);
fprintf(stderr, "line: %d\n", lineno) ;
free_keys(key_in, i) ;
free_keys(key_out, i) ;
fclose(convtab) ;
return -1 ;
}
strip_beg_end(kw1) ;
strip_beg_end(kw2) ;
if (strlen(kw2)>strlen(kw1)) {
fprintf(stderr,
"*** error in conversion table %s (line %d)\n",
name_conv, lineno);
fprintf(stderr,
"*** error: dest keyword is longer than original\n");
fprintf(stderr, "orig: [%s] dest: [%s]\n", kw1, kw2);
fclose(convtab) ;
free_keys(key_in, i) ;
free_keys(key_out, i) ;
return -1 ;
}
key_in[i] = strdup(kw1) ;
key_out[i] = strdup(kw2) ;
i++ ;
}
/* Now read the file through and get the keywords */
i = 0;
lineno = 0;
while (fgets (line, FITS_LINE, convtab) != NULL)
{
lineno++;
if ((line[0] != '#') && (line[0] != '\n'))
{
if (sscanf (line, "%[^=] = %[^;#]", kw1, kw2) != 2)
{
fprintf (stderr,
"*** error parsing table file %s\n", name_conv);
fprintf (stderr, "line: %d\n", lineno);
free_keys (key_in, i);
free_keys (key_out, i);
fclose (convtab);
return -1;
}
strip_beg_end (kw1);
strip_beg_end (kw2);
if (strlen (kw2) > strlen (kw1))
{
fprintf (stderr,
"*** error in conversion table %s (line %d)\n",
name_conv, lineno);
fprintf (stderr,
"*** error: dest keyword is longer than original\n");
fprintf (stderr, "orig: [%s] dest: [%s]\n", kw1, kw2);
fclose (convtab);
free_keys (key_in, i);
free_keys (key_out, i);
return -1;
}
key_in[i] = strdup (kw1);
key_out[i] = strdup (kw2);
i++;
}
}
fclose(convtab) ;
fclose (convtab);
/* Print out some information about what is being done */
printf("\n\n") ;
printf("*** hierarch28\n") ;
printf("\n") ;
printf("searching %s and replacing the following keywords:\n", name_in) ;
for (i=0 ; i<nkeys ; i++) {
printf("\t[%s]\t=>\t[%s]\n", key_in[i], key_out[i]) ;
/* Print out some information about what is being done */
printf ("\n\n");
printf ("*** hierarch28\n");
printf ("\n");
printf ("searching %s and replacing the following keywords:\n", name_in);
for (i = 0; i < nkeys; i++)
{
printf ("\t[%s]\t=>\t[%s]\n", key_in[i], key_out[i]);
}
printf("\n\n") ;
printf ("\n\n");
/* mmap the input file entirely */
if (stat(name_in, &fileinfo)!=0) {
fprintf(stderr, "*** error: accessing file [%s]\n", name_in);
free_keys(key_in, nkeys) ;
free_keys(key_out, nkeys) ;
return -1 ;
/* mmap the input file entirely */
if (stat (name_in, &fileinfo) != 0)
{
fprintf (stderr, "*** error: accessing file [%s]\n", name_in);
free_keys (key_in, nkeys);
free_keys (key_out, nkeys);
return -1;
}
fs = (int)fileinfo.st_size ;
if (fs < 1) {
fprintf(stderr, "error getting FITS header size for %s\n", name_in);
free_keys(key_in, nkeys) ;
free_keys(key_out, nkeys) ;
return -1 ;
fs = (int) fileinfo.st_size;
if (fs < 1)
{
fprintf (stderr, "error getting FITS header size for %s\n", name_in);
free_keys (key_in, nkeys);
free_keys (key_out, nkeys);
return -1;
}
fd = open(name_in, O_RDWR) ;
if (fd == -1) {
fprintf(stderr, "cannot open %s: aborting\n", name_in) ;
free_keys(key_in, nkeys) ;
free_keys(key_out, nkeys) ;
return -1 ;
fd = open (name_in, O_RDWR);
if (fd == -1)
{
fprintf (stderr, "cannot open %s: aborting\n", name_in);
free_keys (key_in, nkeys);
free_keys (key_out, nkeys);
return -1;
}
buf = (char*)mmap(0,
fs,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0) ;
if (buf == (char*)-1) {
perror("mmap") ;
fprintf(stderr, "cannot mmap file: %s\n", name_in) ;
free_keys(key_in, nkeys) ;
free_keys(key_out, nkeys) ;
close(fd) ;
return -1 ;
buf = (char *) mmap (0, fs, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == (char *) -1)
{
perror ("mmap");
fprintf (stderr, "cannot mmap file: %s\n", name_in);
free_keys (key_in, nkeys);
free_keys (key_out, nkeys);
close (fd);
return -1;
}
/* Apply search and replace for the input keyword lists */
if (search_and_replace_kw(buf, fs, key_in, key_out, nkeys) != 0) {
fprintf(stderr, "error while doing search and replace\n") ;
/* Apply search and replace for the input keyword lists */
if (search_and_replace_kw (buf, fs, key_in, key_out, nkeys) != 0)
{
fprintf (stderr, "error while doing search and replace\n");
}
free_keys(key_in, nkeys) ;
free_keys(key_out, nkeys) ;
close(fd) ;
munmap(buf, fs) ;
return 0 ;
free_keys (key_in, nkeys);
free_keys (key_out, nkeys);
close (fd);
munmap (buf, fs);
return 0;
}
/*----------------------------------------------------------------------------*/
@ -325,19 +333,22 @@ static int convert_eso_to_std_FITS(char * name_in, char * name_conv)
characters (like FITS keywords).
*/
/*----------------------------------------------------------------------------*/
static void strip_beg_end(char * s)
static void
strip_beg_end (char *s)
{
int beg, len ;
int beg, len;
beg = 0 ;
while (!isalnum((unsigned char)(s[beg]))) beg++ ;
beg = 0;
while (!isalnum ((unsigned char) (s[beg])))
beg++;
len = (int)strlen(s) -1 ;
while (!isalnum((unsigned char)(s[len]))) len -- ;
len = (int) strlen (s) - 1;
while (!isalnum ((unsigned char) (s[len])))
len--;
strncpy(s, s+beg, len-beg+1) ;
s[len-beg+1] = (char)0 ;
return ;
strncpy (s, s + beg, len - beg + 1);
s[len - beg + 1] = (char) 0;
return;
}
/*----------------------------------------------------------------------------*/
@ -351,13 +362,16 @@ static void strip_beg_end(char * s)
keys and the master table pointer.
*/
/*----------------------------------------------------------------------------*/
static void free_keys(char ** keyt, int n)
static void
free_keys (char **keyt, int n)
{
int i ;
int i;
if (n<1) return ;
for (i=0 ; i<n ; i++) free(keyt[i]) ;
free(keyt) ;
if (n < 1)
return;
for (i = 0; i < n; i++)
free (keyt[i]);
free (keyt);
}
/*----------------------------------------------------------------------------*/
@ -374,74 +388,82 @@ static void free_keys(char ** keyt, int n)
keyword changes in extensions too. Heavily optimized for speed.
*/
/*----------------------------------------------------------------------------*/
static int search_and_replace_kw(
char * buf,
int bufsize,
char ** key_in,
char ** key_out,
int nk)
static int
search_and_replace_kw (char *buf,
int bufsize, char **key_in, char **key_out, int nk)
{
char * w ;
int i, j ;
int in_header ;
int match_flag ;
int * keysizes ;
char *w;
int i, j;
int in_header;
int match_flag;
int *keysizes;
/* Pre-compute key sizes to gain time */
keysizes = malloc(nk * sizeof(int));
for (i=0 ; i<nk ; i++) keysizes[i] = (int)strlen(key_in[i]);
/* Pre-compute key sizes to gain time */
keysizes = malloc (nk * sizeof (int));
for (i = 0; i < nk; i++)
keysizes[i] = (int) strlen (key_in[i]);
/* Browse through file line by line */
w = buf ;
in_header=1 ;
while ((w-buf+FITS_LINE)<bufsize) {
if (in_header) { /* Currently browsing a header */
if (w[0]=='E' &&
w[1]=='N' &&
w[2]=='D' &&
w[3]==' ') {
/* Found an END keyword: exit from header */
in_header=0 ;
} else {
/* Compare the current line with all searched keys */
for (i=0 ; i<nk ; i++) {
match_flag=1 ;
for (j=0 ; j<=keysizes[i] ; j++) {
if (j<keysizes[i]) {
if (key_in[i][j]!=w[j]) {
match_flag=0 ;
break ;
}
} else {
if ((w[j] != '=') && (w[j] != ' ')) {
match_flag=0 ;
break ;
}
}
}
if (match_flag) {
search_rep(w, key_in[i], key_out[i]);
}
}
/* Browse through file line by line */
w = buf;
in_header = 1;
while ((w - buf + FITS_LINE) < bufsize)
{
if (in_header)
{ /* Currently browsing a header */
if (w[0] == 'E' && w[1] == 'N' && w[2] == 'D' && w[3] == ' ')
{
/* Found an END keyword: exit from header */
in_header = 0;
}
else
{
/* Compare the current line with all searched keys */
for (i = 0; i < nk; i++)
{
match_flag = 1;
for (j = 0; j <= keysizes[i]; j++)
{
if (j < keysizes[i])
{
if (key_in[i][j] != w[j])
{
match_flag = 0;
break;
}
}
} else {
/* Currently out of header, look for next extension */
if (w[0]=='X' &&
w[1]=='T' &&
w[2]=='E' &&
w[3]=='N' &&
w[4]=='S' &&
w[5]=='I' &&
w[6]=='O' &&
w[7]=='N') {
/* Found a header start */
in_header=1 ;
else
{
if ((w[j] != '=') && (w[j] != ' '))
{
match_flag = 0;
break;
}
}
}
if (match_flag)
{
search_rep (w, key_in[i], key_out[i]);
}
}
w+=FITS_LINE ;
}
}
free(keysizes);
return 0 ;
else
{
/* Currently out of header, look for next extension */
if (w[0] == 'X' &&
w[1] == 'T' &&
w[2] == 'E' &&
w[3] == 'N' &&
w[4] == 'S' && w[5] == 'I' && w[6] == 'O' && w[7] == 'N')
{
/* Found a header start */
in_header = 1;
}
}
w += FITS_LINE;
}
free (keysizes);
return 0;
}
/*----------------------------------------------------------------------------*/
@ -457,92 +479,114 @@ static int search_and_replace_kw(
conventions.
*/
/*----------------------------------------------------------------------------*/
static void search_rep(char * line, char * key_i, char * key_o)
static void
search_rep (char *line, char *key_i, char *key_o)
{
int i, j ;
char * equal ;
int to_copy ;
char * p ;
char tmp[FITS_LINE+1];
char comment[FITS_LINE+1];
int i, j;
char *equal;
int to_copy;
char *p;
char tmp[FITS_LINE + 1];
char comment[FITS_LINE + 1];
equal = strstr(line, "=");
to_copy = FITS_LINE - (equal-line);
for (i=0 ; i<(int)strlen(key_o) ; i++) {
line[i] = key_o[i] ;
equal = strstr (line, "=");
to_copy = FITS_LINE - (equal - line);
for (i = 0; i < (int) strlen (key_o); i++)
{
line[i] = key_o[i];
}
if (strlen (key_o) <= 8)
{
/* Blank-pad until equal sign is reached */
for (; i < 8; i++)
{
line[i] = ' ';
}
if (strlen(key_o)<=8) {
/* Blank-pad until equal sign is reached */
for ( ; i<8 ; i++) {
line[i]=' ';
}
/* Add equal sign */
line[i] = '=' ;
i++ ;
/* Add equal sign */
line[i] = '=';
i++;
/* Handle special cases: the value also needs conversion */
if(!strcmp(key_o, "RA")) {
if (*(equal+2)!='\'') {
/* out key is RA, translate to ' HH:MM:SS.SSS' */
p = strchr(line+i, '/');
if (p)
strncpy(comment, p, line+FITS_LINE-p);
sprintf(tmp, " %-29.29s %-40.40s",
convert_deg_to_str(atof(equal+1)/15.),
(p)? comment : "/ Right Ascension");
memcpy(line+i, tmp, 71);
}
} else if(!strcmp(key_o, "DEC")) {
if( *(equal+2)!='\'') {
/* out key is DEC, translate to '+DD:MM:SS.SSS' */
p = strchr(line+i, '/');
if (p)
strncpy(comment, p, line+FITS_LINE-p);
sprintf(tmp, " %-29.29s %-40.40s",
convert_deg_to_str(atof(equal+1)),
(p)? comment : "/ Declination");
memcpy(line+i, tmp, 71);
}
} else if(!strcmp(key_o, "UT")) {
if( *(equal+2)!='\'') {
/* out key is UT, translate to ' HH:MM:SS.SSS' */
p = strchr(line+i, '/');
if (p)
strncpy(comment, p, line+FITS_LINE-p);
sprintf(tmp, " %-29.29s %-40.40s",
convert_deg_to_str(atof(equal+1)/3600.),
(p)? comment : "/ UT");
memcpy(line+i, tmp, 71);
}
} else if(!strcmp(key_o, "ST")) {
if( *(equal+2)!='\'') {
/* out key is ST, translate to ' HH:MM:SS.SSS' */
p = strchr(line+i, '/');
if (p)
strncpy(comment, p, line+FITS_LINE-p);
sprintf(tmp, " %-29.29s %-40.40s",
convert_deg_to_str(atof(equal+1)/3600.),
(p)? comment : "/ ST");
memcpy(line+i, tmp, 71);
}
} else {
/* Copy line from first char after real equal sign */
for (j=0 ; j<to_copy ; j++) {
line[i+j] = equal[j+1];
}
i+=to_copy-1 ;
/* Blank padding */
for ( ; i<FITS_LINE ; i++) {
line[i]=' ';
}
}
} else {
/* Blank padding */
for (i=(int)strlen(key_o) ; i<(int)strlen(key_i) ; i++) {
line[i]=' ';
}
/* Handle special cases: the value also needs conversion */
if (!strcmp (key_o, "RA"))
{
if (*(equal + 2) != '\'')
{
/* out key is RA, translate to ' HH:MM:SS.SSS' */
p = strchr (line + i, '/');
if (p)
strncpy (comment, p, line + FITS_LINE - p);
sprintf (tmp, " %-29.29s %-40.40s",
convert_deg_to_str (atof (equal + 1) / 15.),
(p) ? comment : "/ Right Ascension");
memcpy (line + i, tmp, 71);
}
}
return ;
else if (!strcmp (key_o, "DEC"))
{
if (*(equal + 2) != '\'')
{
/* out key is DEC, translate to '+DD:MM:SS.SSS' */
p = strchr (line + i, '/');
if (p)
strncpy (comment, p, line + FITS_LINE - p);
sprintf (tmp, " %-29.29s %-40.40s",
convert_deg_to_str (atof (equal + 1)),
(p) ? comment : "/ Declination");
memcpy (line + i, tmp, 71);
}
}
else if (!strcmp (key_o, "UT"))
{
if (*(equal + 2) != '\'')
{
/* out key is UT, translate to ' HH:MM:SS.SSS' */
p = strchr (line + i, '/');
if (p)
strncpy (comment, p, line + FITS_LINE - p);
sprintf (tmp, " %-29.29s %-40.40s",
convert_deg_to_str (atof (equal + 1) / 3600.),
(p) ? comment : "/ UT");
memcpy (line + i, tmp, 71);
}
}
else if (!strcmp (key_o, "ST"))
{
if (*(equal + 2) != '\'')
{
/* out key is ST, translate to ' HH:MM:SS.SSS' */
p = strchr (line + i, '/');
if (p)
strncpy (comment, p, line + FITS_LINE - p);
sprintf (tmp, " %-29.29s %-40.40s",
convert_deg_to_str (atof (equal + 1) / 3600.),
(p) ? comment : "/ ST");
memcpy (line + i, tmp, 71);
}
}
else
{
/* Copy line from first char after real equal sign */
for (j = 0; j < to_copy; j++)
{
line[i + j] = equal[j + 1];
}
i += to_copy - 1;
/* Blank padding */
for (; i < FITS_LINE; i++)
{
line[i] = ' ';
}
}
}
else
{
/* Blank padding */
for (i = (int) strlen (key_o); i < (int) strlen (key_i); i++)
{
line[i] = ' ';
}
}
return;
}
/*----------------------------------------------------------------------------*/
@ -555,20 +599,22 @@ static void search_rep(char * line, char * key_i, char * key_o)
instrument is specified (ins==NULL) a default table is generated.
*/
/*----------------------------------------------------------------------------*/
static void generate_default_convtab(void)
static void
generate_default_convtab (void)
{
FILE * convtab ;
FILE *convtab;
if ((convtab = fopen("table.conv", "w")) == NULL) {
fprintf(stderr, "*** error: cannot create table.conv: aborting\n") ;
return ;
if ((convtab = fopen ("table.conv", "w")) == NULL)
{
fprintf (stderr, "*** error: cannot create table.conv: aborting\n");
return;
}
fprintf(convtab, CONVTAB_DEFAULT1);
fprintf(convtab, CONVTAB_DEFAULT2);
fprintf(convtab, CONVTAB_DEFAULT3);
fprintf(convtab, CONVTAB_DEFAULT4);
fclose(convtab) ;
return ;
fprintf (convtab, CONVTAB_DEFAULT1);
fprintf (convtab, CONVTAB_DEFAULT2);
fprintf (convtab, CONVTAB_DEFAULT3);
fprintf (convtab, CONVTAB_DEFAULT4);
fclose (convtab);
return;
}
/*----------------------------------------------------------------------------*/
@ -582,22 +628,24 @@ static void generate_default_convtab(void)
string.
*/
/*----------------------------------------------------------------------------*/
static char * convert_deg_to_str( double deg )
static char *
convert_deg_to_str (double deg)
{
int d, m;
double s;
int sign;
static char buf[13];
int d, m;
double s;
int sign;
static char buf[13];
sign = 1;
if(deg < 0.) sign = -1;
sign = 1;
if (deg < 0.)
sign = -1;
deg *= sign;
d = (int)deg;
m = (int)( (deg - d) * 60);
s = (deg - d) * 3600. - m * 60;
deg *= sign;
d = (int) deg;
m = (int) ((deg - d) * 60);
s = (deg - d) * 3600. - m * 60;
sprintf(buf, "'%c%02d:%02d:%06.3f'", (sign<0)? '-' : ' ', d, m, s);
sprintf (buf, "'%c%02d:%02d:%06.3f'", (sign < 0) ? '-' : ' ', d, m, s);
return(buf);
return (buf);
}

File diff suppressed because it is too large Load Diff

View File

@ -53,32 +53,35 @@
Function prototypes
-----------------------------------------------------------------------------*/
static int dump_pix(char*, char*);
static int get_FITS_header_size(char *) ;
static int filesize(char*) ;
static int get_bitpix(char*) ;
static int get_npix(char*) ;
static int dump_pix (char *, char *);
static int get_FITS_header_size (char *);
static int filesize (char *);
static int get_bitpix (char *);
static int get_npix (char *);
/*-----------------------------------------------------------------------------
Main
-----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
int
main (int argc, char *argv[])
{
if (argc<3) {
printf("\n\n") ;
printf("use: %s <in> <out>\n", argv[0]) ;
printf("\n") ;
printf("\t<in> is a valid FITS file in the current directory\n") ;
printf("\t<out> is the name of the output file\n") ;
printf("\n\n") ;
return 0 ;
if (argc < 3)
{
printf ("\n\n");
printf ("use: %s <in> <out>\n", argv[0]);
printf ("\n");
printf ("\t<in> is a valid FITS file in the current directory\n");
printf ("\t<out> is the name of the output file\n");
printf ("\n\n");
return 0;
}
if (!strcmp(argv[1], argv[2])) {
fprintf(stderr, "cannot convert a file to itself\n") ;
fprintf(stderr, "specify another name for the output\n") ;
return -1 ;
if (!strcmp (argv[1], argv[2]))
{
fprintf (stderr, "cannot convert a file to itself\n");
fprintf (stderr, "specify another name for the output\n");
return -1;
}
return dump_pix(argv[1], argv[2]);
return dump_pix (argv[1], argv[2]);
}
/*----------------------------------------------------------------------------*/
@ -90,108 +93,117 @@ int main(int argc, char *argv[])
Heavy use of mmap() to speed up the process
*/
/*----------------------------------------------------------------------------*/
static int dump_pix(
char * name_in,
char * name_out)
static int
dump_pix (char *name_in, char *name_out)
{
int fd_in,
fd_out ;
char * buf_in ;
char * buf_out ;
int fsize_in,
fsize_out,
header_size ;
int npix ;
/*
* Open input file and get information we need:
* - pixel depth
* - number of pixels
*/
int fd_in, fd_out;
char *buf_in;
char *buf_out;
int fsize_in, fsize_out, header_size;
int npix;
fsize_in = filesize(name_in) ;
header_size = get_FITS_header_size(name_in) ;
if ((fd_in = open(name_in, O_RDONLY)) == -1) {
fprintf(stderr, "cannot open file %s: aborting\n", name_in) ;
return -1 ;
/*
* Open input file and get information we need:
* - pixel depth
* - number of pixels
*/
fsize_in = filesize (name_in);
header_size = get_FITS_header_size (name_in);
if ((fd_in = open (name_in, O_RDONLY)) == -1)
{
fprintf (stderr, "cannot open file %s: aborting\n", name_in);
return -1;
}
buf_in = (char*)mmap(0, fsize_in, PROT_READ, MAP_SHARED, fd_in, 0) ;
if (buf_in == (char*)-1) {
perror("mmap") ;
fprintf(stderr, "cannot mmap file: %s\n", name_in) ;
close(fd_in) ;
return -1 ;
buf_in = (char *) mmap (0, fsize_in, PROT_READ, MAP_SHARED, fd_in, 0);
if (buf_in == (char *) -1)
{
perror ("mmap");
fprintf (stderr, "cannot mmap file: %s\n", name_in);
close (fd_in);
return -1;
}
if (get_bitpix(buf_in) != -32) {
fprintf(stderr, "only 32-bit IEEE floating point format supported\n");
close(fd_in) ; munmap(buf_in, fsize_in) ;
return -1 ;
if (get_bitpix (buf_in) != -32)
{
fprintf (stderr, "only 32-bit IEEE floating point format supported\n");
close (fd_in);
munmap (buf_in, fsize_in);
return -1;
}
/*
* Compute the size of the output file
* same header size, + pixel area + blank padding
*/
npix = get_npix(buf_in) ;
if (npix < 1) {
fprintf(stderr, "cannot compute number of pixels\n");
close(fd_in) ;
munmap(buf_in, fsize_in) ;
return -1 ;
/*
* Compute the size of the output file
* same header size, + pixel area + blank padding
*/
npix = get_npix (buf_in);
if (npix < 1)
{
fprintf (stderr, "cannot compute number of pixels\n");
close (fd_in);
munmap (buf_in, fsize_in);
return -1;
}
fsize_out = npix * 4 ;
/*
* Now create the output file and fill it with zeros, then mmap it.
* The permissions are rw-rw-r-- by default.
*/
if ((fd_out=creat(name_out,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH))==-1){
perror("creat") ;
fprintf(stderr, "cannot create file %s: aborting\n", name_out) ;
close(fd_in) ; munmap(buf_in, fsize_in) ;
return -1 ;
fsize_out = npix * 4;
/*
* Now create the output file and fill it with zeros, then mmap it.
* The permissions are rw-rw-r-- by default.
*/
if ((fd_out =
creat (name_out,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)) == -1)
{
perror ("creat");
fprintf (stderr, "cannot create file %s: aborting\n", name_out);
close (fd_in);
munmap (buf_in, fsize_in);
return -1;
}
buf_out = malloc(fsize_out) ;
if (buf_out == NULL) {
fprintf(stderr, "not enough memory\n");
fprintf(stderr, "failed to allocate %d bytes\n", fsize_out);
close(fd_in) ; munmap(buf_in, fsize_in) ;
return -1;
buf_out = malloc (fsize_out);
if (buf_out == NULL)
{
fprintf (stderr, "not enough memory\n");
fprintf (stderr, "failed to allocate %d bytes\n", fsize_out);
close (fd_in);
munmap (buf_in, fsize_in);
return -1;
}
write(fd_out, buf_out, fsize_out);
close(fd_out);
free(buf_out);
write (fd_out, buf_out, fsize_out);
close (fd_out);
free (buf_out);
fd_out = open(name_out, O_RDWR);
if (fd_out==-1) {
fprintf(stderr, "error opening file %s\n", name_out);
close(fd_in) ; munmap(buf_in, fsize_in) ;
return -1;
fd_out = open (name_out, O_RDWR);
if (fd_out == -1)
{
fprintf (stderr, "error opening file %s\n", name_out);
close (fd_in);
munmap (buf_in, fsize_in);
return -1;
}
buf_out = (char*)mmap(0,
fsize_out,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd_out,
0) ;
if (buf_out == (char*)-1) {
perror("mmap") ;
fprintf(stderr, "cannot mmap file: %s\n", name_out) ;
munmap(buf_in, fsize_in) ; close(fd_in) ; close(fd_out) ;
return -1 ;
buf_out = (char *) mmap (0,
fsize_out,
PROT_READ | PROT_WRITE, MAP_SHARED, fd_out, 0);
if (buf_out == (char *) -1)
{
perror ("mmap");
fprintf (stderr, "cannot mmap file: %s\n", name_out);
munmap (buf_in, fsize_in);
close (fd_in);
close (fd_out);
return -1;
}
/*
* Copy FITS header from input to output, modify BITPIX
*/
memcpy(buf_out, buf_in+header_size, fsize_out) ;
/*
* Close, unmap, goodbye
*/
close(fd_in) ;
close(fd_out) ;
munmap(buf_in, fsize_in) ;
munmap(buf_out, fsize_out) ;
return 0 ;
/*
* Copy FITS header from input to output, modify BITPIX
*/
memcpy (buf_out, buf_in + header_size, fsize_out);
/*
* Close, unmap, goodbye
*/
close (fd_in);
close (fd_out);
munmap (buf_in, fsize_in);
munmap (buf_out, fsize_out);
return 0;
}
/*----------------------------------------------------------------------------*/
@ -203,40 +215,47 @@ static int dump_pix(
80 characters are found per line.
*/
/*----------------------------------------------------------------------------*/
static int get_FITS_header_size(char * name)
static int
get_FITS_header_size (char *name)
{
FILE * in ;
char line[81] ;
int found = 0 ;
int count ;
int hs ;
FILE *in;
char line[81];
int found = 0;
int count;
int hs;
if ((in = fopen(name, "r")) == NULL) {
fprintf(stderr, "cannot open %s: aborting\n", name) ;
return 0 ;
if ((in = fopen (name, "r")) == NULL)
{
fprintf (stderr, "cannot open %s: aborting\n", name);
return 0;
}
count = 0 ;
while (!found) {
if (fread(line, 1, 80, in)!=80) {
break ;
}
count ++ ;
if (!strncmp(line, "END ", 4)) {
found = 1 ;
}
count = 0;
while (!found)
{
if (fread (line, 1, 80, in) != 80)
{
break;
}
count++;
if (!strncmp (line, "END ", 4))
{
found = 1;
}
}
fclose(in);
fclose (in);
if (!found) return 0 ;
/*
* The size is the number of found cards times 80,
* rounded to the closest higher multiple of 2880.
*/
hs = count * 80 ;
if ((hs % FITS_BLSZ) != 0) {
hs = (1+(hs/FITS_BLSZ)) * FITS_BLSZ ;
if (!found)
return 0;
/*
* The size is the number of found cards times 80,
* rounded to the closest higher multiple of 2880.
*/
hs = count * 80;
if ((hs % FITS_BLSZ) != 0)
{
hs = (1 + (hs / FITS_BLSZ)) * FITS_BLSZ;
}
return hs ;
return hs;
}
/*----------------------------------------------------------------------------*/
@ -247,18 +266,22 @@ static int get_FITS_header_size(char * name)
Strongly non portable. Only on Unix systems!
*/
/*----------------------------------------------------------------------------*/
static int filesize(char *filename)
static int
filesize (char *filename)
{
int size ;
struct stat fileinfo ;
int size;
struct stat fileinfo;
/* POSIX compliant */
if (stat(filename, &fileinfo) != 0) {
size = (int)0 ;
} else {
size = (int)fileinfo.st_size ;
/* POSIX compliant */
if (stat (filename, &fileinfo) != 0)
{
size = (int) 0;
}
return size ;
else
{
size = (int) fileinfo.st_size;
}
return size;
}
/*----------------------------------------------------------------------------*/
@ -268,28 +291,28 @@ static int filesize(char *filename)
@return int 8 16 32 -32 or -64 or 0 if cannot find it
*/
/*----------------------------------------------------------------------------*/
static int get_bitpix(char * buf)
static int
get_bitpix (char *buf)
{
int bitpix ;
char * where ;
int bitpix;
char *where;
where = strstr(buf, "BITPIX") ;
if (where == NULL) {
fprintf(stderr, "cannot find BITPIX in header: aborting\n") ;
return 0 ;
where = strstr (buf, "BITPIX");
if (where == NULL)
{
fprintf (stderr, "cannot find BITPIX in header: aborting\n");
return 0;
}
sscanf(where, "%*[^=] = %d", &bitpix) ;
/*
* Check the returned value makes sense
*/
if ((bitpix != 8) &&
(bitpix != 16) &&
(bitpix != 32) &&
(bitpix != -32) &&
(bitpix != -64)) {
bitpix = 0 ;
sscanf (where, "%*[^=] = %d", &bitpix);
/*
* Check the returned value makes sense
*/
if ((bitpix != 8) &&
(bitpix != 16) && (bitpix != 32) && (bitpix != -32) && (bitpix != -64))
{
bitpix = 0;
}
return bitpix ;
return bitpix;
}
/*----------------------------------------------------------------------------*/
@ -300,41 +323,45 @@ static int get_bitpix(char * buf)
Does not support extensions!
*/
/*----------------------------------------------------------------------------*/
static int get_npix(char * buf)
static int
get_npix (char *buf)
{
int naxes ;
int npix ;
int naxis ;
char * where ;
char lookfor[80] ;
int i ;
int naxes;
int npix;
int naxis;
char *where;
char lookfor[80];
int i;
where = strstr(buf, "NAXIS") ;
if (where == NULL) {
fprintf(stderr, "cannot find NAXIS in header: aborting\n") ;
return 0 ;
where = strstr (buf, "NAXIS");
if (where == NULL)
{
fprintf (stderr, "cannot find NAXIS in header: aborting\n");
return 0;
}
sscanf(where, "%*[^=] = %d", &naxes) ;
if ((naxes<1) || (naxes>999)) {
fprintf(stderr, "illegal value for %s: %d\n", lookfor, naxes) ;
return 0 ;
sscanf (where, "%*[^=] = %d", &naxes);
if ((naxes < 1) || (naxes > 999))
{
fprintf (stderr, "illegal value for %s: %d\n", lookfor, naxes);
return 0;
}
npix = 1 ;
for (i=1 ; i<=naxes ; i++) {
sprintf(lookfor, "NAXIS%d", i) ;
where = strstr(buf, lookfor) ;
if (where == NULL) {
fprintf(stderr, "cannot find %s in header: aborting\n",
lookfor) ;
return 0 ;
}
sscanf(where, "%*[^=] = %d", &naxis) ;
if (naxis<1) {
fprintf(stderr, "error: found %s=%d\n", lookfor, naxis);
return 0 ;
}
npix *= (int)naxis ;
npix = 1;
for (i = 1; i <= naxes; i++)
{
sprintf (lookfor, "NAXIS%d", i);
where = strstr (buf, lookfor);
if (where == NULL)
{
fprintf (stderr, "cannot find %s in header: aborting\n", lookfor);
return 0;
}
sscanf (where, "%*[^=] = %d", &naxis);
if (naxis < 1)
{
fprintf (stderr, "error: found %s=%d\n", lookfor, naxis);
return 0;
}
npix *= (int) naxis;
}
return npix ;
return npix;
}

View File

@ -51,15 +51,16 @@
modifying the passed argument. Assembler included for x86 architectures.
*/
/*----------------------------------------------------------------------------*/
unsigned short __NOTRACE__ swap_bytes_16(unsigned short w)
unsigned short __NOTRACE__
swap_bytes_16 (unsigned short w)
{
#ifdef CPU_X86
__asm("xchgb %b0,%h0" :
"=q" (w) :
"0" (w));
return w ;
__asm ("xchgb %b0,%h0":
"=q" (w):
"0" (w));
return w;
#else
return (((w) & 0x00ff) << 8 | ((w) & 0xff00) >> 8);
return (((w) & 0x00ff) << 8 | ((w) & 0xff00) >> 8);
#endif
}
@ -74,23 +75,22 @@ unsigned short __NOTRACE__ swap_bytes_16(unsigned short w)
and optimized for processors above 386.
*/
/*----------------------------------------------------------------------------*/
unsigned int __NOTRACE__ swap_bytes_32(unsigned int dw)
unsigned int __NOTRACE__
swap_bytes_32 (unsigned int dw)
{
#ifdef CPU_X86
#if CPU_X86 > 386
__asm("bswap %0":
"=r" (dw) :
__asm ("bswap %0":
"=r" (dw):
#else
__asm("xchgb %b0,%h0\n"
" rorl $16,%0\n"
" xchgb %b0,%h0":
"=q" (dw) :
__asm ("xchgb %b0,%h0\n" " rorl $16,%0\n" " xchgb %b0,%h0":
"=q" (dw):
#endif
"0" (dw));
return dw ;
"0" (dw));
return dw;
#else
return ((((dw) & 0xff000000) >> 24) | (((dw) & 0x00ff0000) >> 8) |
(((dw) & 0x0000ff00) << 8) | (((dw) & 0x000000ff) << 24));
return ((((dw) & 0xff000000) >> 24) | (((dw) & 0x00ff0000) >> 8) |
(((dw) & 0x0000ff00) << 8) | (((dw) & 0x000000ff) << 24));
#endif
}
@ -107,17 +107,19 @@ unsigned int __NOTRACE__ swap_bytes_32(unsigned int dw)
everywhere this file compiles.
*/
/*----------------------------------------------------------------------------*/
void __NOTRACE__ swap_bytes(void * p, int s)
void __NOTRACE__
swap_bytes (void *p, int s)
{
unsigned char tmp, *a, *b ;
unsigned char tmp, *a, *b;
a = (unsigned char*)p ;
b = a + s ;
a = (unsigned char *) p;
b = a + s;
while (a<b) {
tmp = *a ;
*a++ = *--b ;
*b = tmp ;
while (a < b)
{
tmp = *a;
*a++ = *--b;
*b = tmp;
}
}
@ -131,10 +133,11 @@ void __NOTRACE__ swap_bytes(void * p, int s)
MOTOROLA-like one does not.
*/
/*----------------------------------------------------------------------------*/
int need_byteswapping(void)
int
need_byteswapping (void)
{
short ps = 0xFF ;
return ((*((char*)(&ps))) ? 1 : 0 ) ;
short ps = 0xFF;
return ((*((char *) (&ps))) ? 1 : 0);
}
/* vim: set ts=4 et sw=4 tw=75 */

File diff suppressed because it is too large Load Diff

View File

@ -56,7 +56,7 @@
fairly small, you should not need to care too much about this.
*/
/*----------------------------------------------------------------------------*/
void qfits_cache_purge(void);
void qfits_cache_purge (void);
/* </dox> */
/*----------------------------------------------------------------------------*/
@ -116,7 +116,7 @@ void qfits_cache_purge(void);
of the FITS file.
*/
/*----------------------------------------------------------------------------*/
int qfits_query(char * filename, int what);
int qfits_query (char *filename, int what);
#endif
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -40,20 +40,23 @@
@return string
*/
/*----------------------------------------------------------------------------*/
static char * expkey_strupc(const char * s)
static char *
expkey_strupc (const char *s)
{
static char l[ASCIILINESZ+1];
int i ;
static char l[ASCIILINESZ + 1];
int i;
if (s==NULL) return NULL ;
memset(l, 0, ASCIILINESZ+1);
i=0 ;
while (s[i] && i<ASCIILINESZ) {
l[i] = (char)toupper((int)s[i]);
i++ ;
if (s == NULL)
return NULL;
memset (l, 0, ASCIILINESZ + 1);
i = 0;
while (s[i] && i < ASCIILINESZ)
{
l[i] = (char) toupper ((int) s[i]);
i++;
}
l[ASCIILINESZ]=(char)0;
return l ;
l[ASCIILINESZ] = (char) 0;
return l;
}
/*----------------------------------------------------------------------------*/
@ -76,28 +79,33 @@ static char * expkey_strupc(const char * s)
not dots '.') the result is identical to the input.
*/
/*----------------------------------------------------------------------------*/
char * qfits_expand_keyword(const char * keyword)
char *
qfits_expand_keyword (const char *keyword)
{
static char expanded[81];
char ws[81];
char * token ;
static char expanded[81];
char ws[81];
char *token;
/* Bulletproof entries */
if (keyword==NULL) return NULL ;
/* If regular keyword, copy the uppercased input and return */
if (strstr(keyword, ".")==NULL) {
strcpy(expanded, expkey_strupc(keyword));
return expanded ;
}
/* Regular shortFITS keyword */
sprintf(expanded, "HIERARCH ESO");
strcpy(ws, expkey_strupc(keyword));
token = strtok(ws, ".");
while (token!=NULL) {
strcat(expanded, " ");
strcat(expanded, token);
token = strtok(NULL, ".");
}
return expanded ;
/* Bulletproof entries */
if (keyword == NULL)
return NULL;
/* If regular keyword, copy the uppercased input and return */
if (strstr (keyword, ".") == NULL)
{
strcpy (expanded, expkey_strupc (keyword));
return expanded;
}
/* Regular shortFITS keyword */
sprintf (expanded, "HIERARCH ESO");
strcpy (ws, expkey_strupc (keyword));
token = strtok (ws, ".");
while (token != NULL)
{
strcat (expanded, " ");
strcat (expanded, token);
token = strtok (NULL, ".");
}
return expanded;
}
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -46,7 +46,7 @@
not dots '.') the result is identical to the input.
*/
/*----------------------------------------------------------------------------*/
char * qfits_expand_keyword(const char * keyword);
char *qfits_expand_keyword (const char *keyword);
/* </dox> */
#endif

File diff suppressed because it is too large Load Diff

View File

@ -23,7 +23,8 @@
#define FITS_HEADER_H
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
/*-----------------------------------------------------------------------------
@ -50,11 +51,12 @@ extern "C" {
accessor functions.
*/
/*----------------------------------------------------------------------------*/
typedef struct qfits_header {
void * first ; /* Pointer to list start */
void * last ; /* Pointer to list end */
int n ; /* Number of cards in list */
} qfits_header ;
typedef struct qfits_header
{
void *first; /* Pointer to list start */
void *last; /* Pointer to list end */
int n; /* Number of cards in list */
} qfits_header;
/*-----------------------------------------------------------------------------
Function ANSI prototypes
@ -69,7 +71,7 @@ typedef struct qfits_header {
an allocated linked-list handler with an empty card list.
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_header_new(void) ;
qfits_header *qfits_header_new (void);
/*----------------------------------------------------------------------------*/
/**
@ -81,7 +83,7 @@ qfits_header * qfits_header_new(void) ;
(SIMPLE=T) and the last one (END).
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_header_default(void) ;
qfits_header *qfits_header_default (void);
/*----------------------------------------------------------------------------*/
/**
@ -99,12 +101,8 @@ qfits_header * qfits_header_default(void) ;
are allowed to get NULL values.
*/
/*----------------------------------------------------------------------------*/
void qfits_header_add(
qfits_header * hdr,
char * key,
char * val,
char * com,
char * lin) ;
void qfits_header_add (qfits_header * hdr,
char *key, char *val, char *com, char *lin);
/*----------------------------------------------------------------------------*/
/**
@ -122,13 +120,9 @@ void qfits_header_add(
can be NULL, except after and key.
*/
/*----------------------------------------------------------------------------*/
void qfits_header_add_after(
qfits_header * hdr,
char * after,
char * key,
char * val,
char * com,
char * lin) ;
void qfits_header_add_after (qfits_header * hdr,
char *after,
char *key, char *val, char *com, char *lin);
/*----------------------------------------------------------------------------*/
/**
@ -144,12 +138,10 @@ void qfits_header_add_after(
NULL except key.
*/
/*----------------------------------------------------------------------------*/
void qfits_header_append(
qfits_header * hdr,
const char * key,
const char * val,
const char * com,
const char * lin) ;
void qfits_header_append (qfits_header * hdr,
const char *key,
const char *val,
const char *com, const char *lin);
/*----------------------------------------------------------------------------*/
/**
@ -162,7 +154,7 @@ void qfits_header_append(
the key is removed.
*/
/*----------------------------------------------------------------------------*/
void qfits_header_del(qfits_header * hdr, char * key) ;
void qfits_header_del (qfits_header * hdr, char *key);
/*----------------------------------------------------------------------------*/
/**
@ -178,7 +170,7 @@ void qfits_header_del(qfits_header * hdr, char * key) ;
line is set to NULL in the card.
*/
/*----------------------------------------------------------------------------*/
void qfits_header_mod(qfits_header * hdr, char * key, char * val, char * com) ;
void qfits_header_mod (qfits_header * hdr, char *key, char *val, char *com);
/*----------------------------------------------------------------------------*/
/**
@ -187,7 +179,7 @@ void qfits_header_mod(qfits_header * hdr, char * key, char * val, char * com) ;
@return -1 in error case, 0 otherwise
*/
/*----------------------------------------------------------------------------*/
int qfits_header_sort(qfits_header ** hdr) ;
int qfits_header_sort (qfits_header ** hdr);
/*----------------------------------------------------------------------------*/
/**
@ -199,7 +191,7 @@ int qfits_header_sort(qfits_header ** hdr) ;
header. The returned header must be freed using qfits_header_destroy.
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_header_copy(qfits_header * src) ;
qfits_header *qfits_header_copy (qfits_header * src);
/*----------------------------------------------------------------------------*/
/**
@ -211,7 +203,7 @@ qfits_header * qfits_header_copy(qfits_header * src) ;
freed and set to NULL. Useful when a header needs to be reformatted.
*/
/*----------------------------------------------------------------------------*/
void qfits_header_touchall(qfits_header * hdr) ;
void qfits_header_touchall (qfits_header * hdr);
/*----------------------------------------------------------------------------*/
/**
@ -222,7 +214,7 @@ void qfits_header_touchall(qfits_header * hdr) ;
Dump a FITS header to stdout. Mostly for debugging purposes.
*/
/*----------------------------------------------------------------------------*/
void qfits_header_consoledump(qfits_header * hdr) ;
void qfits_header_consoledump (qfits_header * hdr);
/*----------------------------------------------------------------------------*/
/**
@ -233,7 +225,7 @@ void qfits_header_consoledump(qfits_header * hdr) ;
Frees all memory associated to a given qfits_header object.
*/
/*----------------------------------------------------------------------------*/
void qfits_header_destroy(qfits_header * hdr) ;
void qfits_header_destroy (qfits_header * hdr);
/*----------------------------------------------------------------------------*/
/**
@ -249,7 +241,7 @@ void qfits_header_destroy(qfits_header * hdr) ;
Returns NULL if no matching key is found or no value is attached.
*/
/*----------------------------------------------------------------------------*/
char * qfits_header_getstr(qfits_header * hdr, const char * key) ;
char *qfits_header_getstr (qfits_header * hdr, const char *key);
/*----------------------------------------------------------------------------*/
/**
@ -273,7 +265,7 @@ char * qfits_header_getstr(qfits_header * hdr, const char * key) ;
@endverbatim
*/
/*----------------------------------------------------------------------------*/
char * qfits_header_findmatch(qfits_header * hdr, char * key) ;
char *qfits_header_findmatch (qfits_header * hdr, char *key);
/*----------------------------------------------------------------------------*/
/**
@ -312,13 +304,9 @@ char * qfits_header_findmatch(qfits_header * hdr, char * key) ;
available in this module.
*/
/*----------------------------------------------------------------------------*/
int qfits_header_getitem(
qfits_header * hdr,
int idx,
char * key,
char * val,
char * com,
char * lin) ;
int qfits_header_getitem (qfits_header * hdr,
int idx,
char *key, char *val, char *com, char *lin);
/*----------------------------------------------------------------------------*/
/**
@ -334,7 +322,7 @@ int qfits_header_getitem(
Returns NULL if no matching key is found or no line is attached.
*/
/*----------------------------------------------------------------------------*/
char * qfits_header_getline(qfits_header * hdr, char * key) ;
char *qfits_header_getline (qfits_header * hdr, char *key);
/*----------------------------------------------------------------------------*/
/**
@ -351,7 +339,7 @@ char * qfits_header_getline(qfits_header * hdr, char * key) ;
Returns NULL if no matching key is found or no comment is attached.
*/
/*----------------------------------------------------------------------------*/
char * qfits_header_getcom(qfits_header * hdr, char * key) ;
char *qfits_header_getcom (qfits_header * hdr, char *key);
/*----------------------------------------------------------------------------*/
/**
@ -366,7 +354,7 @@ char * qfits_header_getcom(qfits_header * hdr, char * key) ;
attached.
*/
/*----------------------------------------------------------------------------*/
int qfits_header_getint(qfits_header * hdr, const char * key, int errval) ;
int qfits_header_getint (qfits_header * hdr, const char *key, int errval);
/*----------------------------------------------------------------------------*/
/**
@ -381,7 +369,8 @@ int qfits_header_getint(qfits_header * hdr, const char * key, int errval) ;
attached.
*/
/*----------------------------------------------------------------------------*/
double qfits_header_getdouble(qfits_header * hdr, const char * key, double errval) ;
double qfits_header_getdouble (qfits_header * hdr, const char *key,
double errval);
/*----------------------------------------------------------------------------*/
/**
@ -405,7 +394,8 @@ double qfits_header_getdouble(qfits_header * hdr, const char * key, double errva
beginning with a 'n' (no), a 'f' (false) or the digit '0'.
*/
/*----------------------------------------------------------------------------*/
int qfits_header_getboolean(qfits_header * hdr, const char * key, int errval) ;
int qfits_header_getboolean (qfits_header * hdr, const char *key,
int errval);
/*----------------------------------------------------------------------------*/
/**
@ -421,7 +411,7 @@ int qfits_header_getboolean(qfits_header * hdr, const char * key, int errval) ;
Formatting is done according to FITS standard.
*/
/*----------------------------------------------------------------------------*/
void keytuple2str(char * line, char * key, char * val, char * com) ;
void keytuple2str (char *line, char *key, char *val, char *com);
/*----------------------------------------------------------------------------*/
/**
@ -433,7 +423,7 @@ void keytuple2str(char * line, char * key, char * val, char * com) ;
Dumps a FITS header to an opened file pointer.
*/
/*----------------------------------------------------------------------------*/
int qfits_header_dump(qfits_header * hdr, FILE * out) ;
int qfits_header_dump (qfits_header * hdr, FILE * out);
/*----------------------------------------------------------------------------*/
/**
@ -446,7 +436,7 @@ int qfits_header_dump(qfits_header * hdr, FILE * out) ;
This function is meant to create hdr files.
*/
/*----------------------------------------------------------------------------*/
int qfits_header_dump_hdr(qfits_header * hdr, FILE * out) ;
int qfits_header_dump_hdr (qfits_header * hdr, FILE * out);
/*----------------------------------------------------------------------------*/
/**
@ -464,7 +454,7 @@ int qfits_header_dump_hdr(qfits_header * hdr, FILE * out) ;
'hsize'. The returned block must be deallocated using free().
*/
/*----------------------------------------------------------------------------*/
char * qfits_header_to_memblock(qfits_header * fh, int * hsize) ;
char *qfits_header_to_memblock (qfits_header * fh, int *hsize);
/* </dox> */
#ifdef __cplusplus

View File

@ -58,121 +58,128 @@
of error.
*/
/*----------------------------------------------------------------------------*/
char * qfits_datamd5(char * filename)
char *
qfits_datamd5 (char *filename)
{
static char datamd5[MD5HASHSZ+1] ;
struct MD5Context ctx ;
unsigned char digest[16] ;
FILE * in ;
char buf[FITS_BLOCK_SIZE];
char * buf_c ;
int i ;
int in_header ;
int check_fits ;
static char datamd5[MD5HASHSZ + 1];
struct MD5Context ctx;
unsigned char digest[16];
FILE *in;
char buf[FITS_BLOCK_SIZE];
char *buf_c;
int i;
int in_header;
int check_fits;
/* Check entries */
if (filename==NULL) return NULL ;
/* Open input file */
if ((in=fopen(filename, "r"))==NULL) {
qfits_error("cannot open file %s", filename);
return NULL ;
/* Check entries */
if (filename == NULL)
return NULL;
/* Open input file */
if ((in = fopen (filename, "r")) == NULL)
{
qfits_error ("cannot open file %s", filename);
return NULL;
}
/* Initialize all variables */
MD5Init(&ctx);
in_header=1 ;
check_fits=0 ;
/* Loop over input file */
while (fread(buf, 1, FITS_BLOCK_SIZE, in)==FITS_BLOCK_SIZE) {
/* First time in the loop: check the file is FITS */
if (check_fits==0) {
/* Examine first characters in block */
if (buf[0]!='S' ||
buf[1]!='I' ||
buf[2]!='M' ||
buf[3]!='P' ||
buf[4]!='L' ||
buf[5]!='E' ||
buf[6]!=' ' ||
buf[7]!=' ' ||
buf[8]!='=') {
qfits_error("file [%s] is not FITS\n", filename);
fclose(in);
return NULL ;
} else {
check_fits=1 ;
}
}
if (in_header) {
buf_c = buf ;
for (i=0 ; i<FITS_NCARDS ; i++) {
if (buf_c[0]=='E' &&
buf_c[1]=='N' &&
buf_c[2]=='D' &&
buf_c[3]==' ') {
in_header=0 ;
break ;
}
buf_c += FITS_LINESZ ;
}
} else {
/* If current block is a data block */
/* Try to locate an extension header */
if (buf[0]=='X' &&
buf[1]=='T' &&
buf[2]=='E' &&
buf[3]=='N' &&
buf[4]=='S' &&
buf[5]=='I' &&
buf[6]=='O' &&
buf[7]=='N' &&
buf[8]=='=') {
in_header=1 ;
buf_c = buf ;
for (i=0 ; i<FITS_NCARDS ; i++) {
/* Try to find an END marker in this block */
if (buf_c[0]=='E' &&
buf_c[1]=='N' &&
buf_c[2]=='D' &&
buf_c[3]==' ') {
/* Found END marker in same block as XTENSION */
in_header=0;
break ;
}
buf_c += FITS_LINESZ ;
}
} else {
MD5Update(&ctx, (unsigned char *)buf, FITS_BLOCK_SIZE);
}
}
}
fclose(in);
if (check_fits==0) {
/* Never went through the read loop: file is not FITS */
qfits_error("file [%s] is not FITS", filename);
return NULL ;
/* Initialize all variables */
MD5Init (&ctx);
in_header = 1;
check_fits = 0;
/* Loop over input file */
while (fread (buf, 1, FITS_BLOCK_SIZE, in) == FITS_BLOCK_SIZE)
{
/* First time in the loop: check the file is FITS */
if (check_fits == 0)
{
/* Examine first characters in block */
if (buf[0] != 'S' ||
buf[1] != 'I' ||
buf[2] != 'M' ||
buf[3] != 'P' ||
buf[4] != 'L' ||
buf[5] != 'E' ||
buf[6] != ' ' || buf[7] != ' ' || buf[8] != '=')
{
qfits_error ("file [%s] is not FITS\n", filename);
fclose (in);
return NULL;
}
else
{
check_fits = 1;
}
}
/* Got to the end of file: summarize */
MD5Final(digest, &ctx);
/* Write digest into a string */
sprintf(datamd5,
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
digest[ 0],
digest[ 1],
digest[ 2],
digest[ 3],
digest[ 4],
digest[ 5],
digest[ 6],
digest[ 7],
digest[ 8],
digest[ 9],
digest[10],
digest[11],
digest[12],
digest[13],
digest[14],
digest[15]);
return datamd5 ;
if (in_header)
{
buf_c = buf;
for (i = 0; i < FITS_NCARDS; i++)
{
if (buf_c[0] == 'E' &&
buf_c[1] == 'N' && buf_c[2] == 'D' && buf_c[3] == ' ')
{
in_header = 0;
break;
}
buf_c += FITS_LINESZ;
}
}
else
{
/* If current block is a data block */
/* Try to locate an extension header */
if (buf[0] == 'X' &&
buf[1] == 'T' &&
buf[2] == 'E' &&
buf[3] == 'N' &&
buf[4] == 'S' &&
buf[5] == 'I' &&
buf[6] == 'O' && buf[7] == 'N' && buf[8] == '=')
{
in_header = 1;
buf_c = buf;
for (i = 0; i < FITS_NCARDS; i++)
{
/* Try to find an END marker in this block */
if (buf_c[0] == 'E' &&
buf_c[1] == 'N' && buf_c[2] == 'D' && buf_c[3] == ' ')
{
/* Found END marker in same block as XTENSION */
in_header = 0;
break;
}
buf_c += FITS_LINESZ;
}
}
else
{
MD5Update (&ctx, (unsigned char *) buf, FITS_BLOCK_SIZE);
}
}
}
fclose (in);
if (check_fits == 0)
{
/* Never went through the read loop: file is not FITS */
qfits_error ("file [%s] is not FITS", filename);
return NULL;
}
/* Got to the end of file: summarize */
MD5Final (digest, &ctx);
/* Write digest into a string */
sprintf (datamd5,
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
digest[0],
digest[1],
digest[2],
digest[3],
digest[4],
digest[5],
digest[6],
digest[7],
digest[8],
digest[9],
digest[10],
digest[11], digest[12], digest[13], digest[14], digest[15]);
return datamd5;
}
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -49,7 +49,7 @@
of error.
*/
/*----------------------------------------------------------------------------*/
char * qfits_datamd5(char * filename);
char *qfits_datamd5 (char *filename);
/* </dox> */
#endif

View File

@ -50,63 +50,73 @@
allocated in this function, so do not modify or try to free it.
*/
/*----------------------------------------------------------------------------*/
char * qfits_getkey(char * line)
char *
qfits_getkey (char *line)
{
static char key[81];
int i ;
static char key[81];
int i;
if (line==NULL) {
if (line == NULL)
{
#ifdef DEBUG_FITSHEADER
printf("qfits_getkey: NULL input line\n");
printf ("qfits_getkey: NULL input line\n");
#endif
return NULL ;
}
return NULL;
}
/* Special case: blank keyword */
if (!strncmp(line, " ", 8)) {
strcpy(key, " ");
return key ;
}
/* Sort out special cases: HISTORY, COMMENT, END do not have = in line */
if (!strncmp(line, "HISTORY ", 8)) {
strcpy(key, "HISTORY");
return key ;
}
if (!strncmp(line, "COMMENT ", 8)) {
strcpy(key, "COMMENT");
return key ;
}
if (!strncmp(line, "END ", 4)) {
strcpy(key, "END");
return key ;
}
/* Special case: blank keyword */
if (!strncmp (line, " ", 8))
{
strcpy (key, " ");
return key;
}
/* Sort out special cases: HISTORY, COMMENT, END do not have = in line */
if (!strncmp (line, "HISTORY ", 8))
{
strcpy (key, "HISTORY");
return key;
}
if (!strncmp (line, "COMMENT ", 8))
{
strcpy (key, "COMMENT");
return key;
}
if (!strncmp (line, "END ", 4))
{
strcpy (key, "END");
return key;
}
memset(key, 0, 81);
/* General case: look for the first equal sign */
i=0 ;
while (line[i]!='=' && i<80) i++ ;
if (i>=80) {
memset (key, 0, 81);
/* General case: look for the first equal sign */
i = 0;
while (line[i] != '=' && i < 80)
i++;
if (i >= 80)
{
#ifdef DEBUG_FITSHEADER
printf("qfits_getkey: cannot find equal sign\n");
printf ("qfits_getkey: cannot find equal sign\n");
#endif
return NULL ;
}
i-- ;
/* Equal sign found, now backtrack on blanks */
while (line[i]==' ' && i>=0) i-- ;
if (i<=0) {
return NULL;
}
i--;
/* Equal sign found, now backtrack on blanks */
while (line[i] == ' ' && i >= 0)
i--;
if (i <= 0)
{
#ifdef DEBUG_FITSHEADER
printf("qfits_getkey: error backtracking on blanks\n");
printf ("qfits_getkey: error backtracking on blanks\n");
#endif
return NULL ;
}
i++ ;
return NULL;
}
i++;
/* Copy relevant characters into output buffer */
strncpy(key, line, i) ;
/* Null-terminate the string */
key[i+1] = (char)0;
return key ;
/* Copy relevant characters into output buffer */
strncpy (key, line, i);
/* Null-terminate the string */
key[i + 1] = (char) 0;
return key;
}
/*----------------------------------------------------------------------------*/
@ -121,124 +131,145 @@ char * qfits_getkey(char * line)
modify or try to free it.
*/
/*----------------------------------------------------------------------------*/
char * qfits_getvalue(char * line)
char *
qfits_getvalue (char *line)
{
static char value[81] ;
int i ;
int from, to ;
int inq ;
static char value[81];
int i;
int from, to;
int inq;
if (line==NULL) {
if (line == NULL)
{
#ifdef DEBUG_FITSHEADER
printf("qfits_getvalue: NULL input line\n");
printf ("qfits_getvalue: NULL input line\n");
#endif
return NULL ;
}
/* Special cases */
/* END has no associated value */
if (!strncmp(line, "END ", 4)) {
return NULL ;
}
/*
* HISTORY has for value everything else on the line, stripping
* blanks before and after. Blank HISTORY is also accepted.
*/
memset(value, 0, 81);
if (!strncmp(line, "HISTORY ", 8) || !strncmp(line, " ", 8)) {
i=7 ;
/* Strip blanks from the left side */
while (line[i]==' ' && i<80) i++ ;
if (i>=80) return NULL ; /* Blank HISTORY */
from=i ;
/* Strip blanks from the right side */
to=79 ;
while (line[to]==' ') to-- ;
/* Copy relevant characters into output buffer */
strncpy(value, line+from, to-from+1);
/* Null-terminate the string */
value[to-from+1] = (char)0;
return value ;
} else if (!strncmp(line, "COMMENT ", 8)) {
/* COMMENT is like HISTORY */
/* Strip blanks from the left side */
i=7 ;
while (line[i]==' ' && i<80) i++ ;
if (i>=80) return NULL ;
from=i ;
/* Strip blanks from the right side */
to=79 ;
while (line[to]==' ') to-- ;
if (to<from) {
#ifdef DEBUG_FITSHEADER
printf("qfits_getvalue: inconsistent value search in COMMENT\n");
#endif
return NULL ;
}
/* Copy relevant characters into output buffer */
strncpy(value, line+from, to-from+1);
/* Null-terminate the string */
value[to-from+1] = (char)0;
return value ;
}
/* General case - Get past the keyword */
i=0 ;
while (line[i]!='=' && i<80) i++ ;
if (i>80) {
#ifdef DEBUG_FITSHEADER
printf("qfits_getvalue: no equal sign found on line\n");
#endif
return NULL ;
}
i++ ;
while (line[i]==' ' && i<80) i++ ;
if (i>80) {
#ifdef DEBUG_FITSHEADER
printf("qfits_getvalue: no value past the equal sign\n");
#endif
return NULL ;
}
from=i;
/* Now value section: Look for the first slash '/' outside a string */
inq = 0 ;
while (i<80) {
if (line[i]=='\'')
inq=!inq ;
if (line[i]=='/')
if (!inq)
break ;
i++;
return NULL;
}
i-- ;
/* Backtrack on blanks */
while (line[i]==' ' && i>=0) i-- ;
if (i<0) {
#ifdef DEBUG_FITSHEADER
printf("qfits_getvalue: error backtracking on blanks\n");
#endif
return NULL ;
}
to=i ;
/* Special cases */
if (to<from) {
/* END has no associated value */
if (!strncmp (line, "END ", 4))
{
return NULL;
}
/*
* HISTORY has for value everything else on the line, stripping
* blanks before and after. Blank HISTORY is also accepted.
*/
memset (value, 0, 81);
if (!strncmp (line, "HISTORY ", 8) || !strncmp (line, " ", 8))
{
i = 7;
/* Strip blanks from the left side */
while (line[i] == ' ' && i < 80)
i++;
if (i >= 80)
return NULL; /* Blank HISTORY */
from = i;
/* Strip blanks from the right side */
to = 79;
while (line[to] == ' ')
to--;
/* Copy relevant characters into output buffer */
strncpy (value, line + from, to - from + 1);
/* Null-terminate the string */
value[to - from + 1] = (char) 0;
return value;
}
else if (!strncmp (line, "COMMENT ", 8))
{
/* COMMENT is like HISTORY */
/* Strip blanks from the left side */
i = 7;
while (line[i] == ' ' && i < 80)
i++;
if (i >= 80)
return NULL;
from = i;
/* Strip blanks from the right side */
to = 79;
while (line[to] == ' ')
to--;
if (to < from)
{
#ifdef DEBUG_FITSHEADER
printf("qfits_getvalue: from>to?\n");
printf("line=[%s]\n", line);
printf ("qfits_getvalue: inconsistent value search in COMMENT\n");
#endif
return NULL ;
return NULL;
}
/* Copy relevant characters into output buffer */
strncpy(value, line+from, to-from+1);
/* Null-terminate the string */
value[to-from+1] = (char)0;
return value ;
/* Copy relevant characters into output buffer */
strncpy (value, line + from, to - from + 1);
/* Null-terminate the string */
value[to - from + 1] = (char) 0;
return value;
}
/* General case - Get past the keyword */
i = 0;
while (line[i] != '=' && i < 80)
i++;
if (i > 80)
{
#ifdef DEBUG_FITSHEADER
printf ("qfits_getvalue: no equal sign found on line\n");
#endif
return NULL;
}
i++;
while (line[i] == ' ' && i < 80)
i++;
if (i > 80)
{
#ifdef DEBUG_FITSHEADER
printf ("qfits_getvalue: no value past the equal sign\n");
#endif
return NULL;
}
from = i;
/* Now value section: Look for the first slash '/' outside a string */
inq = 0;
while (i < 80)
{
if (line[i] == '\'')
inq = !inq;
if (line[i] == '/')
if (!inq)
break;
i++;
}
i--;
/* Backtrack on blanks */
while (line[i] == ' ' && i >= 0)
i--;
if (i < 0)
{
#ifdef DEBUG_FITSHEADER
printf ("qfits_getvalue: error backtracking on blanks\n");
#endif
return NULL;
}
to = i;
if (to < from)
{
#ifdef DEBUG_FITSHEADER
printf ("qfits_getvalue: from>to?\n");
printf ("line=[%s]\n", line);
#endif
return NULL;
}
/* Copy relevant characters into output buffer */
strncpy (value, line + from, to - from + 1);
/* Null-terminate the string */
value[to - from + 1] = (char) 0;
return value;
}
/*----------------------------------------------------------------------------*/
@ -253,74 +284,87 @@ char * qfits_getvalue(char * line)
modify or try to free it.
*/
/*----------------------------------------------------------------------------*/
char * qfits_getcomment(char * line)
char *
qfits_getcomment (char *line)
{
static char comment[81];
int i ;
int from, to ;
int inq ;
static char comment[81];
int i;
int from, to;
int inq;
if (line==NULL) {
if (line == NULL)
{
#ifdef DEBUG_FITSHEADER
printf("qfits_getcomment: null line in input\n");
printf ("qfits_getcomment: null line in input\n");
#endif
return NULL ;
}
return NULL;
}
/* Special cases: END, HISTORY, COMMENT and blank have no comment */
if (!strncmp(line, "END ", 4)) return NULL ;
if (!strncmp(line, "HISTORY ", 8)) return NULL ;
if (!strncmp(line, "COMMENT ", 8)) return NULL ;
if (!strncmp(line, " ", 8)) return NULL ;
/* Special cases: END, HISTORY, COMMENT and blank have no comment */
if (!strncmp (line, "END ", 4))
return NULL;
if (!strncmp (line, "HISTORY ", 8))
return NULL;
if (!strncmp (line, "COMMENT ", 8))
return NULL;
if (!strncmp (line, " ", 8))
return NULL;
memset(comment, 0, 81);
/* Get past the keyword */
i=0 ;
while (line[i]!='=' && i<80) i++ ;
if (i>=80) {
memset (comment, 0, 81);
/* Get past the keyword */
i = 0;
while (line[i] != '=' && i < 80)
i++;
if (i >= 80)
{
#ifdef DEBUG_FITSHEADER
printf("qfits_getcomment: no equal sign on line\n");
printf ("qfits_getcomment: no equal sign on line\n");
#endif
return NULL ;
}
i++ ;
/* Get past the value until the slash */
inq = 0 ;
while (i<80) {
if (line[i]=='\'')
inq = !inq ;
if (line[i]=='/')
if (!inq)
break ;
i++ ;
}
if (i>=80) {
#ifdef DEBUG_FITSHEADER
printf("qfits_getcomment: no slash found on line\n");
#endif
return NULL ;
}
i++ ;
/* Get past the first blanks */
while (line[i]==' ') i++ ;
from=i ;
return NULL;
}
i++;
/* Now backtrack from the end of the line to the first non-blank char */
to=79 ;
while (line[to]==' ') to-- ;
if (to<from) {
/* Get past the value until the slash */
inq = 0;
while (i < 80)
{
if (line[i] == '\'')
inq = !inq;
if (line[i] == '/')
if (!inq)
break;
i++;
}
if (i >= 80)
{
#ifdef DEBUG_FITSHEADER
printf("qfits_getcomment: from>to?\n");
printf ("qfits_getcomment: no slash found on line\n");
#endif
return NULL ;
}
/* Copy relevant characters into output buffer */
strncpy(comment, line+from, to-from+1);
/* Null-terminate the string */
comment[to-from+1] = (char)0;
return comment ;
return NULL;
}
i++;
/* Get past the first blanks */
while (line[i] == ' ')
i++;
from = i;
/* Now backtrack from the end of the line to the first non-blank char */
to = 79;
while (line[to] == ' ')
to--;
if (to < from)
{
#ifdef DEBUG_FITSHEADER
printf ("qfits_getcomment: from>to?\n");
#endif
return NULL;
}
/* Copy relevant characters into output buffer */
strncpy (comment, line + from, to - from + 1);
/* Null-terminate the string */
comment[to - from + 1] = (char) 0;
return comment;
}
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -22,7 +22,8 @@
#define FITSEP_H
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
/* <dox> */
@ -41,7 +42,7 @@ extern "C" {
allocated in this function, so do not modify or try to free it.
*/
/*----------------------------------------------------------------------------*/
char * qfits_getkey(char * line) ;
char *qfits_getkey (char *line);
/*----------------------------------------------------------------------------*/
/**
@ -55,7 +56,7 @@ char * qfits_getkey(char * line) ;
modify or try to free it.
*/
/*----------------------------------------------------------------------------*/
char * qfits_getvalue(char * line) ;
char *qfits_getvalue (char *line);
/*----------------------------------------------------------------------------*/
/**
@ -69,7 +70,7 @@ char * qfits_getvalue(char * line) ;
modify or try to free it.
*/
/*----------------------------------------------------------------------------*/
char * qfits_getcomment(char * line) ;
char *qfits_getcomment (char *line);
/* </dox> */
#ifdef __cplusplus

View File

@ -35,14 +35,17 @@
/*-----------------------------------------------------------------------------
Private to this module
-----------------------------------------------------------------------------*/
static int is_blank_line(char * s)
static int
is_blank_line (char *s)
{
int i ;
int i;
for (i=0 ; i<(int)strlen(s) ; i++) {
if (s[i]!=' ') return 0 ;
for (i = 0; i < (int) strlen (s); i++)
{
if (s[i] != ' ')
return 0;
}
return 1 ;
return 1;
}
/*-----------------------------------------------------------------------------
@ -70,10 +73,11 @@ static int is_blank_line(char * s)
Value, comment, and original line might be NULL pointers.
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_header_read(char * filename)
qfits_header *
qfits_header_read (char *filename)
{
/* Forward job to readext */
return qfits_header_readext(filename, 0);
/* Forward job to readext */
return qfits_header_readext (filename, 0);
}
/*----------------------------------------------------------------------------*/
@ -88,76 +92,86 @@ qfits_header * qfits_header_read(char * filename)
a hdr file.
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_header_read_hdr(char * filename)
qfits_header *
qfits_header_read_hdr (char *filename)
{
qfits_header * hdr ;
FILE * in ;
char line[81];
char * key,
* val,
* com ;
int i, j ;
qfits_header *hdr;
FILE *in;
char line[81];
char *key, *val, *com;
int i, j;
/* Check input */
if (filename==NULL) return NULL ;
/* Check input */
if (filename == NULL)
return NULL;
/* Initialise */
key = val = com = NULL ;
/* Initialise */
key = val = com = NULL;
/* Open the file */
if ((in=fopen(filename, "r"))==NULL) {
qfits_error("cannot read [%s]", filename) ;
return NULL ;
/* Open the file */
if ((in = fopen (filename, "r")) == NULL)
{
qfits_error ("cannot read [%s]", filename);
return NULL;
}
/* Create the header */
hdr = qfits_header_new() ;
/* Go through the file */
while (fgets(line, 81, in)!=NULL) {
for (i=0 ; i<81 ; i++) {
if (line[i] == '\n') {
for (j=i ; j<81 ; j++) line[j] = ' ' ;
line[80] = (char)0 ;
break ;
}
}
if (!strcmp(line, "END")) {
line[3] = ' ';
line[4] = (char)0 ;
}
/* Rule out blank lines */
if (!is_blank_line(line)) {
/* Get key, value, comment for the current line */
key = qfits_getkey(line);
val = qfits_getvalue(line);
com = qfits_getcomment(line);
/* Create the header */
hdr = qfits_header_new ();
/* If key or value cannot be found, trigger an error */
if (key==NULL) {
qfits_header_destroy(hdr);
fclose(in) ;
return NULL ;
}
/* Append card to linked-list */
qfits_header_append(hdr, key, val, com, NULL);
}
/* Go through the file */
while (fgets (line, 81, in) != NULL)
{
for (i = 0; i < 81; i++)
{
if (line[i] == '\n')
{
for (j = i; j < 81; j++)
line[j] = ' ';
line[80] = (char) 0;
break;
}
}
if (!strcmp (line, "END"))
{
line[3] = ' ';
line[4] = (char) 0;
}
/* Rule out blank lines */
if (!is_blank_line (line))
{
/* Get key, value, comment for the current line */
key = qfits_getkey (line);
val = qfits_getvalue (line);
com = qfits_getcomment (line);
/* If key or value cannot be found, trigger an error */
if (key == NULL)
{
qfits_header_destroy (hdr);
fclose (in);
return NULL;
}
/* Append card to linked-list */
qfits_header_append (hdr, key, val, com, NULL);
}
}
fclose(in) ;
fclose (in);
/* The last key should be 'END' */
if (strlen(key)!=3) {
qfits_header_destroy(hdr);
return NULL ;
}
if (key[0]!='E' || key[1]!='N' || key[2]!='D') {
qfits_header_destroy(hdr);
return NULL ;
/* The last key should be 'END' */
if (strlen (key) != 3)
{
qfits_header_destroy (hdr);
return NULL;
}
return hdr ;
if (key[0] != 'E' || key[1] != 'N' || key[2] != 'D')
{
qfits_header_destroy (hdr);
return NULL;
}
return hdr;
}
/*----------------------------------------------------------------------------*/
@ -171,74 +185,81 @@ qfits_header * qfits_header_read_hdr(char * filename)
object.
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_header_read_hdr_string(
unsigned char * hdr_str,
int nb_char)
qfits_header *
qfits_header_read_hdr_string (unsigned char *hdr_str, int nb_char)
{
qfits_header * hdr ;
char line[81];
char * key,
* val,
* com ;
int ind ;
int i, j ;
qfits_header *hdr;
char line[81];
char *key, *val, *com;
int ind;
int i, j;
/* Check input */
if (hdr_str==NULL) return NULL ;
/* Check input */
if (hdr_str == NULL)
return NULL;
/* Initialise */
key = val = com = NULL ;
/* Initialise */
key = val = com = NULL;
/* Create the header */
hdr = qfits_header_new() ;
/* Go through the file */
ind = 0 ;
while (ind <= nb_char - 80) {
strncpy(line, (char*)hdr_str + ind, 80) ;
line[80] = (char)0 ;
for (i=0 ; i<81 ; i++) {
if (line[i] == '\n') {
for (j=i ; j<81 ; j++) line[j] = ' ' ;
line[80] = (char)0 ;
break ;
}
}
if (!strcmp(line, "END")) {
line[3] = ' ';
line[4] = (char)0 ;
}
/* Rule out blank lines */
if (!is_blank_line(line)) {
/* Create the header */
hdr = qfits_header_new ();
/* Get key, value, comment for the current line */
key = qfits_getkey(line);
val = qfits_getvalue(line);
com = qfits_getcomment(line);
/* Go through the file */
ind = 0;
while (ind <= nb_char - 80)
{
strncpy (line, (char *) hdr_str + ind, 80);
line[80] = (char) 0;
for (i = 0; i < 81; i++)
{
if (line[i] == '\n')
{
for (j = i; j < 81; j++)
line[j] = ' ';
line[80] = (char) 0;
break;
}
}
if (!strcmp (line, "END"))
{
line[3] = ' ';
line[4] = (char) 0;
}
/* If key or value cannot be found, trigger an error */
if (key==NULL) {
qfits_header_destroy(hdr);
return NULL ;
}
/* Append card to linked-list */
qfits_header_append(hdr, key, val, com, NULL);
}
ind += 80 ;
/* Rule out blank lines */
if (!is_blank_line (line))
{
/* Get key, value, comment for the current line */
key = qfits_getkey (line);
val = qfits_getvalue (line);
com = qfits_getcomment (line);
/* If key or value cannot be found, trigger an error */
if (key == NULL)
{
qfits_header_destroy (hdr);
return NULL;
}
/* Append card to linked-list */
qfits_header_append (hdr, key, val, com, NULL);
}
ind += 80;
}
/* The last key should be 'END' */
if (strlen(key)!=3) {
qfits_header_destroy(hdr);
return NULL ;
}
if (key[0]!='E' || key[1]!='N' || key[2]!='D') {
qfits_header_destroy(hdr);
return NULL ;
/* The last key should be 'END' */
if (strlen (key) != 3)
{
qfits_header_destroy (hdr);
return NULL;
}
return hdr ;
if (key[0] != 'E' || key[1] != 'N' || key[2] != 'D')
{
qfits_header_destroy (hdr);
return NULL;
}
return hdr;
}
/*----------------------------------------------------------------------------*/
@ -255,80 +276,85 @@ qfits_header * qfits_header_read_hdr_string(
Returns NULL in case of error.
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_header_readext(char * filename, int xtnum)
qfits_header *
qfits_header_readext (char *filename, int xtnum)
{
qfits_header* hdr ;
int n_ext ;
char line[81];
char * where ;
char * start ;
char * key,
* val,
* com ;
int seg_start ;
int seg_size ;
size_t size ;
qfits_header *hdr;
int n_ext;
char line[81];
char *where;
char *start;
char *key, *val, *com;
int seg_start;
int seg_size;
size_t size;
/* Check input */
if (filename==NULL || xtnum<0)
return NULL ;
/* Check input */
if (filename == NULL || xtnum < 0)
return NULL;
/* Check that there are enough extensions */
if (xtnum>0) {
n_ext = qfits_query_n_ext(filename);
if (xtnum>n_ext) {
return NULL ;
}
/* Check that there are enough extensions */
if (xtnum > 0)
{
n_ext = qfits_query_n_ext (filename);
if (xtnum > n_ext)
{
return NULL;
}
}
/* Get offset to the extension header */
if (qfits_get_hdrinfo(filename, xtnum, &seg_start, &seg_size)!=0) {
return NULL ;
/* Get offset to the extension header */
if (qfits_get_hdrinfo (filename, xtnum, &seg_start, &seg_size) != 0)
{
return NULL;
}
/* Memory-map the input file */
start = falloc(filename, seg_start, &size) ;
if (start==NULL) return NULL ;
/* Memory-map the input file */
start = falloc (filename, seg_start, &size);
if (start == NULL)
return NULL;
hdr = qfits_header_new() ;
where = start ;
while (1) {
memcpy(line, where, 80);
line[80] = (char)0;
hdr = qfits_header_new ();
where = start;
while (1)
{
memcpy (line, where, 80);
line[80] = (char) 0;
/* Rule out blank lines */
if (!is_blank_line(line)) {
/* Rule out blank lines */
if (!is_blank_line (line))
{
/* Get key, value, comment for the current line */
key = qfits_getkey(line);
val = qfits_getvalue(line);
com = qfits_getcomment(line);
/* Get key, value, comment for the current line */
key = qfits_getkey (line);
val = qfits_getvalue (line);
com = qfits_getcomment (line);
/* If key or value cannot be found, trigger an error */
if (key==NULL) {
qfits_header_destroy(hdr);
hdr = NULL ;
break ;
}
/* Append card to linked-list */
qfits_header_append(hdr, key, val, com, line);
/* Check for END keyword */
if (strlen(key)==3)
if (key[0]=='E' &&
key[1]=='N' &&
key[2]=='D')
break ;
}
where += 80 ;
/* If reaching the end of file, trigger an error */
if ((int)(where-start)>=(int)(seg_size+80)) {
qfits_header_destroy(hdr);
hdr = NULL ;
break ;
}
/* If key or value cannot be found, trigger an error */
if (key == NULL)
{
qfits_header_destroy (hdr);
hdr = NULL;
break;
}
/* Append card to linked-list */
qfits_header_append (hdr, key, val, com, line);
/* Check for END keyword */
if (strlen (key) == 3)
if (key[0] == 'E' && key[1] == 'N' && key[2] == 'D')
break;
}
where += 80;
/* If reaching the end of file, trigger an error */
if ((int) (where - start) >= (int) (seg_size + 80))
{
qfits_header_destroy (hdr);
hdr = NULL;
break;
}
}
fdealloc(start, seg_start, size) ;
return hdr ;
fdealloc (start, seg_start, size);
return hdr;
}
/*----------------------------------------------------------------------------*/
@ -341,34 +367,38 @@ qfits_header * qfits_header_readext(char * filename, int xtnum)
for the file size to reach a multiple of 2880, as required by FITS.
*/
/*----------------------------------------------------------------------------*/
void qfits_zeropad(char * filename)
void
qfits_zeropad (char *filename)
{
struct stat sta ;
int size ;
int remaining;
FILE * out ;
char * buf;
struct stat sta;
int size;
int remaining;
FILE *out;
char *buf;
if (filename==NULL) return ;
if (filename == NULL)
return;
/* Get file size in bytes */
if (stat(filename, &sta)!=0) {
return ;
/* Get file size in bytes */
if (stat (filename, &sta) != 0)
{
return;
}
size = (int)sta.st_size ;
/* Compute number of zeros to pad */
remaining = size % FITS_BLOCK_SIZE ;
if (remaining==0) return ;
remaining = FITS_BLOCK_SIZE - remaining ;
size = (int) sta.st_size;
/* Compute number of zeros to pad */
remaining = size % FITS_BLOCK_SIZE;
if (remaining == 0)
return;
remaining = FITS_BLOCK_SIZE - remaining;
/* Open file, dump zeros, exit */
if ((out=fopen(filename, "a"))==NULL)
return ;
buf = calloc(remaining, sizeof(char));
fwrite(buf, 1, remaining, out);
fclose(out);
free(buf);
return ;
/* Open file, dump zeros, exit */
if ((out = fopen (filename, "a")) == NULL)
return;
buf = calloc (remaining, sizeof (char));
fwrite (buf, 1, remaining, out);
fclose (out);
free (buf);
return;
}
/*----------------------------------------------------------------------------*/
@ -381,28 +411,31 @@ void qfits_zeropad(char * filename)
0 else. If the file does not exist, returns -1.
*/
/*----------------------------------------------------------------------------*/
int is_fits_file(char *filename)
int
is_fits_file (char *filename)
{
FILE * fp ;
char * magic ;
int isfits ;
FILE *fp;
char *magic;
int isfits;
if (filename==NULL) return -1 ;
if ((fp = fopen(filename, "r"))==NULL) {
qfits_error("cannot open file [%s]", filename) ;
return -1 ;
if (filename == NULL)
return -1;
if ((fp = fopen (filename, "r")) == NULL)
{
qfits_error ("cannot open file [%s]", filename);
return -1;
}
magic = calloc(FITS_MAGIC_SZ+1, sizeof(char)) ;
fread(magic, 1, FITS_MAGIC_SZ, fp) ;
fclose(fp) ;
magic[FITS_MAGIC_SZ] = (char)0 ;
if (strstr(magic, FITS_MAGIC)!=NULL)
isfits = 1 ;
else
isfits = 0 ;
free(magic) ;
return isfits ;
magic = calloc (FITS_MAGIC_SZ + 1, sizeof (char));
fread (magic, 1, FITS_MAGIC_SZ, fp);
fclose (fp);
magic[FITS_MAGIC_SZ] = (char) 0;
if (strstr (magic, FITS_MAGIC) != NULL)
isfits = 1;
else
isfits = 0;
free (magic);
return isfits;
}
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -19,7 +19,8 @@
#define FITS_RW_H
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
/*-----------------------------------------------------------------------------
@ -55,7 +56,7 @@ extern "C" {
Value, comment, and original line might be NULL pointers.
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_header_read(char * filename) ;
qfits_header *qfits_header_read (char *filename);
/*----------------------------------------------------------------------------*/
/**
@ -69,7 +70,7 @@ qfits_header * qfits_header_read(char * filename) ;
a hdr file.
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_header_read_hdr(char * filename) ;
qfits_header *qfits_header_read_hdr (char *filename);
/*----------------------------------------------------------------------------*/
/**
@ -82,9 +83,8 @@ qfits_header * qfits_header_read_hdr(char * filename) ;
object.
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_header_read_hdr_string(
unsigned char * hdr_str,
int nb_char) ;
qfits_header *qfits_header_read_hdr_string (unsigned char *hdr_str,
int nb_char);
/*----------------------------------------------------------------------------*/
/**
@ -100,7 +100,7 @@ qfits_header * qfits_header_read_hdr_string(
Returns NULL in case of error.
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_header_readext(char * filename, int xtnum) ;
qfits_header *qfits_header_readext (char *filename, int xtnum);
/*----------------------------------------------------------------------------*/
/**
@ -112,7 +112,7 @@ qfits_header * qfits_header_readext(char * filename, int xtnum) ;
for the file size to reach a multiple of 2880, as required by FITS.
*/
/*----------------------------------------------------------------------------*/
void qfits_zeropad(char * filename) ;
void qfits_zeropad (char *filename);
/*----------------------------------------------------------------------------*/
/**
@ -124,7 +124,7 @@ void qfits_zeropad(char * filename) ;
0 else. If the file does not exist, returns -1.
*/
/*----------------------------------------------------------------------------*/
int is_fits_file(char *filename) ;
int is_fits_file (char *filename);
/* </dox> */
#ifdef __cplusplus

View File

@ -63,7 +63,7 @@
((x) == BPP_16_SIGNED) ? 2 : \
((x) == BPP_32_SIGNED) ? 4 : \
((x) == BPP_IEEE_FLOAT) ? 4 : \
((x) == BPP_IEEE_DOUBLE) ? 8 : 0 )
((x) == BPP_IEEE_DOUBLE) ? 8 : 0 )
/* </dox> */
#endif
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -89,33 +89,35 @@
@endverbatim
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_dir_name(char * filename)
char *
qfits_get_dir_name (char *filename)
{
static char path[MAXNAMESZ];
char *last_slash;
static char path[MAXNAMESZ];
char *last_slash;
if (strlen(filename)>MAXNAMESZ) return NULL ;
strcpy(path, filename);
/* Find last '/'. */
last_slash = path != NULL ? strrchr (path, '/') : NULL;
if (strlen (filename) > MAXNAMESZ)
return NULL;
strcpy (path, filename);
/* Find last '/'. */
last_slash = path != NULL ? strrchr (path, '/') : NULL;
if (last_slash == path)
if (last_slash == path)
/* The last slash is the first character in the string. We have to
return "/". */
++last_slash;
else if (last_slash != NULL && last_slash[1] == '\0')
/* The '/' is the last character, we have to look further. */
last_slash = memchr (path, last_slash - path, '/');
return "/". */
++last_slash;
else if (last_slash != NULL && last_slash[1] == '\0')
/* The '/' is the last character, we have to look further. */
last_slash = memchr (path, last_slash - path, '/');
if (last_slash != NULL)
/* Terminate the path. */
last_slash[0] = '\0';
else
/* This assignment is ill-designed but the XPG specs require to
return a string containing "." in any case no directory part is
found and so a static and constant string is required. */
strcpy(path, ".");
return path;
if (last_slash != NULL)
/* Terminate the path. */
last_slash[0] = '\0';
else
/* This assignment is ill-designed but the XPG specs require to
return a string containing "." in any case no directory part is
found and so a static and constant string is required. */
strcpy (path, ".");
return path;
}
/*----------------------------------------------------------------------------*/
@ -138,11 +140,12 @@ char * qfits_get_dir_name(char * filename)
@endverbatim
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_base_name(const char *filename)
char *
qfits_get_base_name (const char *filename)
{
char *p ;
p = strrchr (filename, '/');
return p ? p + 1 : (char *) filename;
char *p;
p = strrchr (filename, '/');
return p ? p + 1 : (char *) filename;
}
/*----------------------------------------------------------------------------*/
@ -176,26 +179,29 @@ char * qfits_get_base_name(const char *filename)
to free it or modify its contents.
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_root_name(char * filename)
char *
qfits_get_root_name (char *filename)
{
static char path[MAXNAMESZ+1];
char * lastdot ;
static char path[MAXNAMESZ + 1];
char *lastdot;
if (strlen(filename)>MAXNAMESZ) return NULL ;
memset(path, MAXNAMESZ, 0);
strcpy(path, filename);
lastdot = strrchr(path, '.');
if (lastdot == NULL) return path ;
if ((!strcmp(lastdot, ".fits")) || (!strcmp(lastdot, ".FITS")) ||
(!strcmp(lastdot, ".paf")) || (!strcmp(lastdot, ".PAF")) ||
(!strcmp(lastdot, ".dat")) || (!strcmp(lastdot, ".DAT")) ||
(!strcmp(lastdot, ".txt")) || (!strcmp(lastdot, ".TXT")) ||
(!strcmp(lastdot, ".tfits")) || (!strcmp(lastdot, ".TFITS")) ||
(!strcmp(lastdot, ".ascii")) || (!strcmp(lastdot, ".ASCII")))
if (strlen (filename) > MAXNAMESZ)
return NULL;
memset (path, MAXNAMESZ, 0);
strcpy (path, filename);
lastdot = strrchr (path, '.');
if (lastdot == NULL)
return path;
if ((!strcmp (lastdot, ".fits")) || (!strcmp (lastdot, ".FITS")) ||
(!strcmp (lastdot, ".paf")) || (!strcmp (lastdot, ".PAF")) ||
(!strcmp (lastdot, ".dat")) || (!strcmp (lastdot, ".DAT")) ||
(!strcmp (lastdot, ".txt")) || (!strcmp (lastdot, ".TXT")) ||
(!strcmp (lastdot, ".tfits")) || (!strcmp (lastdot, ".TFITS")) ||
(!strcmp (lastdot, ".ascii")) || (!strcmp (lastdot, ".ASCII")))
{
lastdot[0] = (char)0;
lastdot[0] = (char) 0;
}
return path ;
return path;
}
/*----------------------------------------------------------------------------*/
@ -219,11 +225,12 @@ char * qfits_get_root_name(char * filename)
@endverbatim
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_ext_name(char * filename)
char *
qfits_get_ext_name (char *filename)
{
char * p;
p = strrchr(filename, '.');
return p ? p+1 : NULL ;
char *p;
p = strrchr (filename, '.');
return p ? p + 1 : NULL;
}
/*----------------------------------------------------------------------------*/
@ -240,17 +247,20 @@ char * qfits_get_ext_name(char * filename)
string which first character is a null character.
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_login_name(void)
char *
qfits_get_login_name (void)
{
struct passwd * pw ;
static char name[32];
struct passwd *pw;
static char name[32];
pw = getpwuid(getuid());
if (pw!=NULL) {
strcpy(name, pw->pw_name);
} else {
name[0]=0 ;
pw = getpwuid (getuid ());
if (pw != NULL)
{
strcpy (name, pw->pw_name);
}
return name ;
else
{
name[0] = 0;
}
return name;
}

View File

@ -20,7 +20,8 @@
#define GET_NAME_H
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
@ -51,7 +52,7 @@ extern "C" {
@endverbatim
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_dir_name(char * filename) ;
char *qfits_get_dir_name (char *filename);
/*----------------------------------------------------------------------------*/
/**
@ -73,7 +74,7 @@ char * qfits_get_dir_name(char * filename) ;
@endverbatim
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_base_name(const char *filename) ;
char *qfits_get_base_name (const char *filename);
/*----------------------------------------------------------------------------*/
/**
@ -105,7 +106,7 @@ char * qfits_get_base_name(const char *filename) ;
to free it or modify its contents.
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_root_name(char * filename) ;
char *qfits_get_root_name (char *filename);
/*----------------------------------------------------------------------------*/
/**
@ -128,7 +129,7 @@ char * qfits_get_root_name(char * filename) ;
@endverbatim
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_ext_name(char * filename) ;
char *qfits_get_ext_name (char *filename);
/*----------------------------------------------------------------------------*/
/**
@ -144,7 +145,7 @@ char * qfits_get_ext_name(char * filename) ;
point to a string which first character is a null character.
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_login_name(void) ;
char *qfits_get_login_name (void);
/* </dox> */
#ifdef __cplusplus

View File

@ -38,85 +38,94 @@
#ifndef WORDS_BIGENDIAN
/* Little endian ordering */
typedef union _ieee_double_pattern_ {
double d ;
struct {
unsigned int lsw ;
unsigned int msw ;
} p ;
} ieee_double_pattern ;
typedef union _ieee_double_pattern_
{
double d;
struct
{
unsigned int lsw;
unsigned int msw;
} p;
} ieee_double_pattern;
#else
/* Big endian ordering */
typedef union _ieee_double_pattern_ {
double d ;
struct {
unsigned int msw ;
unsigned int lsw ;
} p ;
} ieee_double_pattern ;
typedef union _ieee_double_pattern_
{
double d;
struct
{
unsigned int msw;
unsigned int lsw;
} p;
} ieee_double_pattern;
#endif
typedef union _ieee_float_pattern_ {
float f ;
int i ;
} ieee_float_pattern ;
typedef union _ieee_float_pattern_
{
float f;
int i;
} ieee_float_pattern;
/*-----------------------------------------------------------------------------
Function codes
-----------------------------------------------------------------------------*/
int _qfits_isnanf(float f)
int
_qfits_isnanf (float f)
{
ieee_float_pattern ip ;
int ix ;
ieee_float_pattern ip;
int ix;
ip.f = f ;
ix = ip.i ;
ix &= 0x7fffffff ;
ix = 0x7f800000 - ix ;
return (int)(((unsigned int)(ix))>>31);
ip.f = f;
ix = ip.i;
ix &= 0x7fffffff;
ix = 0x7f800000 - ix;
return (int) (((unsigned int) (ix)) >> 31);
}
int _qfits_isinff(float f)
int
_qfits_isinff (float f)
{
ieee_float_pattern ip ;
int ix, t ;
ieee_float_pattern ip;
int ix, t;
ip.f = f ;
ix = ip.i ;
t = ix & 0x7fffffff;
t ^= 0x7f800000;
t |= -t;
return ~(t >> 31) & (ix >> 30);
ip.f = f;
ix = ip.i;
t = ix & 0x7fffffff;
t ^= 0x7f800000;
t |= -t;
return ~(t >> 31) & (ix >> 30);
}
int _qfits_isnand(double d)
int
_qfits_isnand (double d)
{
ieee_double_pattern id ;
int hx, lx ;
ieee_double_pattern id;
int hx, lx;
id.d = d ;
lx = id.p.lsw ;
hx = id.p.msw ;
id.d = d;
lx = id.p.lsw;
hx = id.p.msw;
hx &= 0x7fffffff;
hx |= (unsigned int)(lx|(-lx))>>31;
hx = 0x7ff00000 - hx;
return (int)(((unsigned int)hx)>>31);
hx &= 0x7fffffff;
hx |= (unsigned int) (lx | (-lx)) >> 31;
hx = 0x7ff00000 - hx;
return (int) (((unsigned int) hx) >> 31);
}
int _qfits_isinfd(double d)
int
_qfits_isinfd (double d)
{
ieee_double_pattern id ;
int hx, lx ;
ieee_double_pattern id;
int hx, lx;
id.d = d ;
lx = id.p.lsw ;
hx = id.p.msw ;
id.d = d;
lx = id.p.lsw;
hx = id.p.msw;
lx |= (hx & 0x7fffffff) ^ 0x7ff00000;
lx |= -lx;
return ~(lx >> 31) & (hx >> 30);
lx |= (hx & 0x7fffffff) ^ 0x7ff00000;
lx |= -lx;
return ~(lx >> 31) & (hx >> 30);
}
@ -133,88 +142,99 @@ int _qfits_isinfd(double d)
#ifndef WORDS_BIGENDIAN
/* Little endian patterns */
static unsigned char fnan_pat[] = {0, 0, 0xc0, 0x7f};
static unsigned char dnan_pat[] = {0, 0, 0, 0, 0, 0, 0xf8, 0x7f};
static unsigned char finf_pat[] = {0, 0, 0x80, 0x7f};
static unsigned char dinf_pat[] = {0, 0, 0, 0, 0, 0, 0xf0, 0x7f};
static unsigned char fminf_pat[] = {0, 0, 0x80, 0xff};
static unsigned char fnan_pat[] = { 0, 0, 0xc0, 0x7f };
static unsigned char dnan_pat[] = { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f };
static unsigned char finf_pat[] = { 0, 0, 0x80, 0x7f };
static unsigned char dinf_pat[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f };
static unsigned char fminf_pat[] = { 0, 0, 0x80, 0xff };
/* static unsigned char dminf_pat[] = {0, 0, 0, 0, 0, 0, 0xf0, 0xff}; */
static unsigned char dminf_pat[] = {0, 0, 0, 0, 0, 0, 0xf0, 0x7f};
static unsigned char dminf_pat[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f };
#else
/* Big endian patterns */
static unsigned char fnan_pat[] = {0x7f, 0xc0, 0, 0};
static unsigned char dnan_pat[] = {0x7f, 0xf8, 0, 0, 0, 0, 0, 0};
static unsigned char finf_pat[] = {0x7f, 0x80, 0, 0};
static unsigned char dinf_pat[] = {0x7f, 0xf0, 0, 0, 0, 0, 0, 0};
static unsigned char fminf_pat[] = {0xff, 0x80, 0, 0};
static unsigned char dminf_pat[] = {0xff, 0xf0, 0, 0, 0, 0, 0, 0};
static unsigned char fnan_pat[] = { 0x7f, 0xc0, 0, 0 };
static unsigned char dnan_pat[] = { 0x7f, 0xf8, 0, 0, 0, 0, 0, 0 };
static unsigned char finf_pat[] = { 0x7f, 0x80, 0, 0 };
static unsigned char dinf_pat[] = { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 };
static unsigned char fminf_pat[] = { 0xff, 0x80, 0, 0 };
static unsigned char dminf_pat[] = { 0xff, 0xf0, 0, 0, 0, 0, 0, 0 };
#endif
static void hexdump(void * p, int s)
static void
hexdump (void *p, int s)
{
unsigned char * c ;
int i ;
unsigned char *c;
int i;
c=(unsigned char*)p ;
c = (unsigned char *) p;
#ifndef WORDS_BIGENDIAN
for (i=s-1 ; i>=0 ; i--) {
for (i = s - 1; i >= 0; i--)
{
#else
for (i=0 ; i<s ; i++) {
for (i = 0; i < s; i++)
{
#endif
printf("%02x", c[i]);
}
printf("\n");
printf ("%02x", c[i]);
}
printf ("\n");
}
int main(void)
int
main (void)
{
float f ;
double d ;
float f;
double d;
printf("Testing Nan...\n");
memcpy(&f, fnan_pat, 4);
memcpy(&d, dnan_pat, 8);
printf("f=%g d=%g\n", f, d);
hexdump(&f, sizeof(float));
hexdump(&d, sizeof(double));
printf ("Testing Nan...\n");
memcpy (&f, fnan_pat, 4);
memcpy (&d, dnan_pat, 8);
printf ("f=%g d=%g\n", f, d);
hexdump (&f, sizeof (float));
hexdump (&d, sizeof (double));
if (qfits_isnan(f)) {
printf("f is NaN\n");
if (qfits_isnan (f))
{
printf ("f is NaN\n");
}
if (qfits_isnan(d)) {
printf("d is NaN\n");
if (qfits_isnan (d))
{
printf ("d is NaN\n");
}
printf("Testing +Inf...\n");
memcpy(&f, finf_pat, 4);
memcpy(&d, dinf_pat, 8);
printf("f=%g d=%g\n", f, d);
hexdump(&f, sizeof(float));
hexdump(&d, sizeof(double));
printf ("Testing +Inf...\n");
memcpy (&f, finf_pat, 4);
memcpy (&d, dinf_pat, 8);
printf ("f=%g d=%g\n", f, d);
hexdump (&f, sizeof (float));
hexdump (&d, sizeof (double));
if (qfits_isinf(f)) {
printf("f is Inf\n");
if (qfits_isinf (f))
{
printf ("f is Inf\n");
}
if (qfits_isinf(d)) {
printf("d is Inf\n");
if (qfits_isinf (d))
{
printf ("d is Inf\n");
}
printf("Testing -Inf...\n");
memcpy(&f, fminf_pat, 4);
memcpy(&d, dminf_pat, 8);
printf("f=%g d=%g\n", f, d);
hexdump(&f, sizeof(float));
hexdump(&d, sizeof(double));
printf ("Testing -Inf...\n");
memcpy (&f, fminf_pat, 4);
memcpy (&d, dminf_pat, 8);
printf ("f=%g d=%g\n", f, d);
hexdump (&f, sizeof (float));
hexdump (&d, sizeof (double));
if (qfits_isinf(f)) {
printf("f is (-)Inf\n");
if (qfits_isinf (f))
{
printf ("f is (-)Inf\n");
}
if (qfits_isinf(d)) {
printf("d is (-)Inf\n");
if (qfits_isinf (d))
{
printf ("d is (-)Inf\n");
}
return 0 ;
return 0;
}
#endif
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -82,22 +82,22 @@
* Test a float variable for NaN value.
* Do not call directly, call qfits_isnan().
*/
int _qfits_isnanf(float f);
int _qfits_isnanf (float f);
/**
* Test a float variable for Inf value.
* Do not call directly, call qfits_isinf().
*/
int _qfits_isinff(float f);
int _qfits_isinff (float f);
/**
* Test a double variable for NaN value.
* Do not call directly, call qfits_isnan().
*/
int _qfits_isnand(double d);
int _qfits_isnand (double d);
/**
* Test a double variable for Inf value.
* Do not call directly, call qfits_isinf().
*/
int _qfits_isinfd(double d);
int _qfits_isinfd (double d);
/* </dox> */
#endif

View File

@ -31,13 +31,13 @@
-----------------------------------------------------------------------------*/
#include "md5.h"
#include <string.h>
#include <string.h>
/*-----------------------------------------------------------------------------
Function prototypes
-----------------------------------------------------------------------------*/
static void byteReverse(unsigned char *buf, unsigned longs) ;
static void byteReverse (unsigned char *buf, unsigned longs);
/*-----------------------------------------------------------------------------
Function codes
@ -46,123 +46,135 @@ static void byteReverse(unsigned char *buf, unsigned longs) ;
/*
* Note: this code is harmless on little-endian machines.
*/
static void byteReverse(unsigned char *buf, unsigned longs)
static void
byteReverse (unsigned char *buf, unsigned longs)
{
word32 t;
do {
t = (word32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
((unsigned) buf[1] << 8 | buf[0]);
*(word32 *) buf = t;
buf += 4;
} while (--longs);
word32 t;
do
{
t = (word32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
((unsigned) buf[1] << 8 | buf[0]);
*(word32 *) buf = t;
buf += 4;
}
while (--longs);
}
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.
*/
void MD5Init(struct MD5Context *ctx)
void
MD5Init (struct MD5Context *ctx)
{
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
ctx->buf[2] = 0x98badcfe;
ctx->buf[3] = 0x10325476;
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
ctx->buf[2] = 0x98badcfe;
ctx->buf[3] = 0x10325476;
ctx->bits[0] = 0;
ctx->bits[1] = 0;
ctx->bits[0] = 0;
ctx->bits[1] = 0;
}
/*
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
void
MD5Update (struct MD5Context *ctx, unsigned char const *buf, unsigned len)
{
register word32 t;
register word32 t;
/* Update bitcount */
/* Update bitcount */
t = ctx->bits[0];
if ((ctx->bits[0] = t + ((word32) len << 3)) < t)
ctx->bits[1]++; /* Carry from low to high */
ctx->bits[1] += len >> 29;
t = ctx->bits[0];
if ((ctx->bits[0] = t + ((word32) len << 3)) < t)
ctx->bits[1]++; /* Carry from low to high */
ctx->bits[1] += len >> 29;
t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
/* Handle any leading odd-sized chunks */
/* Handle any leading odd-sized chunks */
if (t) {
unsigned char *p = (unsigned char *) ctx->in + t;
if (t)
{
unsigned char *p = (unsigned char *) ctx->in + t;
t = 64 - t;
if (len < t) {
memmove(p, buf, len);
return;
t = 64 - t;
if (len < t)
{
memmove (p, buf, len);
return;
}
memmove(p, buf, t);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (word32 *) ctx->in);
buf += t;
len -= t;
memmove (p, buf, t);
byteReverse (ctx->in, 16);
MD5Transform (ctx->buf, (word32 *) ctx->in);
buf += t;
len -= t;
}
/* Process data in 64-byte chunks */
/* Process data in 64-byte chunks */
while (len >= 64) {
memmove(ctx->in, buf, 64);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (word32 *) ctx->in);
buf += 64;
len -= 64;
while (len >= 64)
{
memmove (ctx->in, buf, 64);
byteReverse (ctx->in, 16);
MD5Transform (ctx->buf, (word32 *) ctx->in);
buf += 64;
len -= 64;
}
/* Handle any remaining bytes of data. */
/* Handle any remaining bytes of data. */
memmove(ctx->in, buf, len);
memmove (ctx->in, buf, len);
}
/*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
void
MD5Final (unsigned char digest[16], struct MD5Context *ctx)
{
unsigned int count;
unsigned char *p;
unsigned int count;
unsigned char *p;
/* Compute number of bytes mod 64 */
count = (ctx->bits[0] >> 3) & 0x3F;
/* Compute number of bytes mod 64 */
count = (ctx->bits[0] >> 3) & 0x3F;
/* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */
p = ctx->in + count;
*p++ = 0x80;
/* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */
p = ctx->in + count;
*p++ = 0x80;
/* Bytes of padding needed to make 64 bytes */
count = 64 - 1 - count;
/* Bytes of padding needed to make 64 bytes */
count = 64 - 1 - count;
/* Pad out to 56 mod 64 */
if (count < 8) {
/* Two lots of padding: Pad the first block to 64 bytes */
memset(p, 0, count);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (word32 *) ctx->in);
/* Pad out to 56 mod 64 */
if (count < 8)
{
/* Two lots of padding: Pad the first block to 64 bytes */
memset (p, 0, count);
byteReverse (ctx->in, 16);
MD5Transform (ctx->buf, (word32 *) ctx->in);
/* Now fill the next block with 56 bytes */
memset(ctx->in, 0, 56);
} else {
/* Pad block to 56 bytes */
memset(p, 0, count - 8);
/* Now fill the next block with 56 bytes */
memset (ctx->in, 0, 56);
}
byteReverse(ctx->in, 14);
else
{
/* Pad block to 56 bytes */
memset (p, 0, count - 8);
}
byteReverse (ctx->in, 14);
/* Append length in bits and transform */
((word32 *) ctx->in)[14] = ctx->bits[0];
((word32 *) ctx->in)[15] = ctx->bits[1];
/* Append length in bits and transform */
((word32 *) ctx->in)[14] = ctx->bits[0];
((word32 *) ctx->in)[15] = ctx->bits[1];
MD5Transform(ctx->buf, (word32 *) ctx->in);
byteReverse((unsigned char *) ctx->buf, 4);
memmove(digest, ctx->buf, 16);
memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
MD5Transform (ctx->buf, (word32 *) ctx->in);
byteReverse ((unsigned char *) ctx->buf, 4);
memmove (digest, ctx->buf, 16);
memset (ctx, 0, sizeof (ctx)); /* In case it's sensitive */
}
/* The four core functions - F1 is optimized somewhat */
@ -182,86 +194,88 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
* reflect the addition of 16 longwords of new data. MD5Update blocks
* the data and converts bytes into longwords for this routine.
*/
void MD5Transform(word32 buf[4], word32 const in[16])
void
MD5Transform (word32 buf[4], word32 const in[16])
{
register word32 a, b, c, d;
register word32 a, b, c, d;
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
MD5STEP (F1, a, b, c, d, in[0] + 0xd76aa478, 7);
MD5STEP (F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
MD5STEP (F1, c, d, a, b, in[2] + 0x242070db, 17);
MD5STEP (F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
MD5STEP (F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
MD5STEP (F1, d, a, b, c, in[5] + 0x4787c62a, 12);
MD5STEP (F1, c, d, a, b, in[6] + 0xa8304613, 17);
MD5STEP (F1, b, c, d, a, in[7] + 0xfd469501, 22);
MD5STEP (F1, a, b, c, d, in[8] + 0x698098d8, 7);
MD5STEP (F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
MD5STEP (F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
MD5STEP (F1, b, c, d, a, in[11] + 0x895cd7be, 22);
MD5STEP (F1, a, b, c, d, in[12] + 0x6b901122, 7);
MD5STEP (F1, d, a, b, c, in[13] + 0xfd987193, 12);
MD5STEP (F1, c, d, a, b, in[14] + 0xa679438e, 17);
MD5STEP (F1, b, c, d, a, in[15] + 0x49b40821, 22);
MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
MD5STEP (F2, a, b, c, d, in[1] + 0xf61e2562, 5);
MD5STEP (F2, d, a, b, c, in[6] + 0xc040b340, 9);
MD5STEP (F2, c, d, a, b, in[11] + 0x265e5a51, 14);
MD5STEP (F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
MD5STEP (F2, a, b, c, d, in[5] + 0xd62f105d, 5);
MD5STEP (F2, d, a, b, c, in[10] + 0x02441453, 9);
MD5STEP (F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
MD5STEP (F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
MD5STEP (F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
MD5STEP (F2, d, a, b, c, in[14] + 0xc33707d6, 9);
MD5STEP (F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
MD5STEP (F2, b, c, d, a, in[8] + 0x455a14ed, 20);
MD5STEP (F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
MD5STEP (F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
MD5STEP (F2, c, d, a, b, in[7] + 0x676f02d9, 14);
MD5STEP (F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
MD5STEP (F3, a, b, c, d, in[5] + 0xfffa3942, 4);
MD5STEP (F3, d, a, b, c, in[8] + 0x8771f681, 11);
MD5STEP (F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
MD5STEP (F3, b, c, d, a, in[14] + 0xfde5380c, 23);
MD5STEP (F3, a, b, c, d, in[1] + 0xa4beea44, 4);
MD5STEP (F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
MD5STEP (F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
MD5STEP (F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
MD5STEP (F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
MD5STEP (F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
MD5STEP (F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
MD5STEP (F3, b, c, d, a, in[6] + 0x04881d05, 23);
MD5STEP (F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
MD5STEP (F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
MD5STEP (F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
MD5STEP (F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
MD5STEP (F4, a, b, c, d, in[0] + 0xf4292244, 6);
MD5STEP (F4, d, a, b, c, in[7] + 0x432aff97, 10);
MD5STEP (F4, c, d, a, b, in[14] + 0xab9423a7, 15);
MD5STEP (F4, b, c, d, a, in[5] + 0xfc93a039, 21);
MD5STEP (F4, a, b, c, d, in[12] + 0x655b59c3, 6);
MD5STEP (F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
MD5STEP (F4, c, d, a, b, in[10] + 0xffeff47d, 15);
MD5STEP (F4, b, c, d, a, in[1] + 0x85845dd1, 21);
MD5STEP (F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
MD5STEP (F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
MD5STEP (F4, c, d, a, b, in[6] + 0xa3014314, 15);
MD5STEP (F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
MD5STEP (F4, a, b, c, d, in[4] + 0xf7537e82, 6);
MD5STEP (F4, d, a, b, c, in[11] + 0xbd3af235, 10);
MD5STEP (F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
MD5STEP (F4, b, c, d, a, in[9] + 0xeb86d391, 21);
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -1,19 +1,20 @@
#ifndef MD5_H
#define MD5_H
typedef unsigned int word32 ;
typedef unsigned int word32;
struct MD5Context {
word32 buf[4];
word32 bits[2];
unsigned char in[64];
struct MD5Context
{
word32 buf[4];
word32 bits[2];
unsigned char in[64];
};
void MD5Init(struct MD5Context *context);
void MD5Update(struct MD5Context *context, unsigned char const *buf,
unsigned len);
void MD5Final(unsigned char digest[16], struct MD5Context *context);
void MD5Transform(word32 buf[4], word32 const in[16]);
void MD5Init (struct MD5Context *context);
void MD5Update (struct MD5Context *context, unsigned char const *buf,
unsigned len);
void MD5Final (unsigned char digest[16], struct MD5Context *context);
void MD5Transform (word32 buf[4], word32 const in[16]);
/*
* This is needed to make RSAREF happy on some MS-DOS compilers.

View File

@ -33,7 +33,7 @@
/*-----------------------------------------------------------------------------
Function prototypes
-----------------------------------------------------------------------------*/
static char * qfits_strcrop(char * s) ;
static char *qfits_strcrop (char *s);
/*-----------------------------------------------------------------------------
Function codes
@ -65,32 +65,31 @@ static char * qfits_strcrop(char * s) ;
the file.
*/
/*----------------------------------------------------------------------------*/
FILE * qfits_paf_print_header(
char * filename,
char * paf_id,
char * paf_desc,
char * login_name,
char * datetime)
FILE *
qfits_paf_print_header (char *filename,
char *paf_id,
char *paf_desc, char *login_name, char *datetime)
{
FILE * paf ;
if ((paf=fopen(filename, "w"))==NULL) {
qfits_error("cannot create PAF file [%s]", filename);
return NULL ;
}
fprintf(paf, "PAF.HDR.START ;# start of header\n");
fprintf(paf, "PAF.TYPE \"pipeline product\" ;\n");
fprintf(paf, "PAF.ID \"%s\"\n", paf_id);
fprintf(paf, "PAF.NAME \"%s\"\n", filename);
fprintf(paf, "PAF.DESC \"%s\"\n", paf_desc);
fprintf(paf, "PAF.CRTE.NAME \"%s\"\n", login_name) ;
fprintf(paf, "PAF.CRTE.DAYTIM \"%s\"\n", datetime) ;
fprintf(paf, "PAF.LCHG.NAME \"%s\"\n", login_name) ;
fprintf(paf, "PAF.LCHG.DAYTIM \"%s\"\n", datetime) ;
fprintf(paf, "PAF.CHCK.CHECKSUM \"\"\n");
fprintf(paf, "PAF.HDR.END ;# end of header\n");
fprintf(paf, "\n");
return paf ;
FILE *paf;
if ((paf = fopen (filename, "w")) == NULL)
{
qfits_error ("cannot create PAF file [%s]", filename);
return NULL;
}
fprintf (paf, "PAF.HDR.START ;# start of header\n");
fprintf (paf, "PAF.TYPE \"pipeline product\" ;\n");
fprintf (paf, "PAF.ID \"%s\"\n", paf_id);
fprintf (paf, "PAF.NAME \"%s\"\n", filename);
fprintf (paf, "PAF.DESC \"%s\"\n", paf_desc);
fprintf (paf, "PAF.CRTE.NAME \"%s\"\n", login_name);
fprintf (paf, "PAF.CRTE.DAYTIM \"%s\"\n", datetime);
fprintf (paf, "PAF.LCHG.NAME \"%s\"\n", login_name);
fprintf (paf, "PAF.LCHG.DAYTIM \"%s\"\n", datetime);
fprintf (paf, "PAF.CHCK.CHECKSUM \"\"\n");
fprintf (paf, "PAF.HDR.END ;# end of header\n");
fprintf (paf, "\n");
return paf;
}
/*----------------------------------------------------------------------------*/
@ -107,57 +106,65 @@ FILE * qfits_paf_print_header(
If the key is not found, this function returns NULL.
*/
/*----------------------------------------------------------------------------*/
char * qfits_paf_query(
char * filename,
char * key)
char *
qfits_paf_query (char *filename, char *key)
{
static char value[ASCIILINESZ];
FILE * paf ;
char line[ASCIILINESZ+1];
char val[ASCIILINESZ+1];
char head[ASCIILINESZ+1];
int found ;
int len ;
static char value[ASCIILINESZ];
FILE *paf;
char line[ASCIILINESZ + 1];
char val[ASCIILINESZ + 1];
char head[ASCIILINESZ + 1];
int found;
int len;
/* Check inputs */
if (filename==NULL || key==NULL) return NULL ;
/* Check inputs */
if (filename == NULL || key == NULL)
return NULL;
/* Check PAF validity */
if (qfits_is_paf_file(filename)!=1) {
qfits_error("not a PAF file: [%s]", filename);
return NULL ;
}
/* Check PAF validity */
if (qfits_is_paf_file (filename) != 1)
{
qfits_error ("not a PAF file: [%s]", filename);
return NULL;
}
/* Open file and read it */
paf = fopen(filename, "r");
if (paf==NULL) {
qfits_error("opening [%s]", filename);
return NULL ;
/* Open file and read it */
paf = fopen (filename, "r");
if (paf == NULL)
{
qfits_error ("opening [%s]", filename);
return NULL;
}
found = 0;
while (fgets (line, ASCIILINESZ, paf) != NULL)
{
sscanf (line, "%[^ ]", head);
if (!strcmp (head, key))
{
/* Get value */
sscanf (line, "%*[^ ] %[^;]", value);
found++;
break;
}
found = 0 ;
while (fgets(line, ASCIILINESZ, paf)!=NULL) {
sscanf(line, "%[^ ]", head);
if (!strcmp(head, key)) {
/* Get value */
sscanf(line, "%*[^ ] %[^;]", value);
found ++ ;
break ;
}
}
if (!found) return NULL ;
/* Remove trailing blanks */
strcpy(val, qfits_strcrop(value));
/* Get rid of possible quotes */
len = strlen(val);
if (val[0]=='\"' && val[len-1]=='\"') {
strncpy(value, val+1, len-2);
value[len-2]=(char)0;
} else {
strcpy(value, val);
}
return value ;
}
if (!found)
return NULL;
/* Remove trailing blanks */
strcpy (val, qfits_strcrop (value));
/* Get rid of possible quotes */
len = strlen (val);
if (val[0] == '\"' && val[len - 1] == '\"')
{
strncpy (value, val + 1, len - 2);
value[len - 2] = (char) 0;
}
else
{
strcpy (value, val);
}
return value;
}
/*----------------------------------------------------------------------------*/
@ -170,34 +177,40 @@ char * qfits_paf_query(
is checked with the presence of PAF.HDR.START at the beginning
*/
/*----------------------------------------------------------------------------*/
int qfits_is_paf_file(char * filename)
int
qfits_is_paf_file (char *filename)
{
FILE * fp ;
int is_paf ;
char line[ASCIILINESZ] ;
if (filename==NULL) return -1 ;
FILE *fp;
int is_paf;
char line[ASCIILINESZ];
/* Initialize is_paf */
is_paf = 0 ;
if (filename == NULL)
return -1;
/* Open file */
if ((fp = fopen(filename, "r"))==NULL) {
qfits_error("cannot open file [%s]", filename) ;
return -1 ;
/* Initialize is_paf */
is_paf = 0;
/* Open file */
if ((fp = fopen (filename, "r")) == NULL)
{
qfits_error ("cannot open file [%s]", filename);
return -1;
}
/* Parse file */
while (fgets (line, ASCIILINESZ, fp) != NULL)
{
if (line[0] != '#')
{
if (!strncmp (line, PAF_MAGIC, PAF_MAGIC_SZ))
is_paf = 1;
(void) fclose (fp);
return is_paf;
}
}
/* Parse file */
while (fgets(line, ASCIILINESZ, fp) != NULL) {
if (line[0] != '#') {
if (!strncmp(line, PAF_MAGIC, PAF_MAGIC_SZ)) is_paf = 1 ;
(void)fclose(fp) ;
return is_paf ;
}
}
(void)fclose(fp) ;
return is_paf ;
(void) fclose (fp);
return is_paf;
}
/*----------------------------------------------------------------------------*/
@ -214,22 +227,25 @@ int qfits_is_paf_file(char * filename)
(not re-entrant).
*/
/*----------------------------------------------------------------------------*/
static char * qfits_strcrop(char * s)
static char *
qfits_strcrop (char *s)
{
static char l[ASCIILINESZ+1];
char * last ;
static char l[ASCIILINESZ + 1];
char *last;
if (s==NULL) return NULL ;
memset(l, 0, ASCIILINESZ+1);
strcpy(l, s);
last = l + strlen(l);
while (last > l) {
if (!isspace((int)*(last-1)))
break ;
last -- ;
if (s == NULL)
return NULL;
memset (l, 0, ASCIILINESZ + 1);
strcpy (l, s);
last = l + strlen (l);
while (last > l)
{
if (!isspace ((int) *(last - 1)))
break;
last--;
}
*last = (char)0;
return l ;
*last = (char) 0;
return l;
}
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -21,7 +21,8 @@
#define PAFS_H
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
/*-----------------------------------------------------------------------------
@ -65,12 +66,10 @@ extern "C" {
the file.
*/
/*----------------------------------------------------------------------------*/
FILE * qfits_paf_print_header(
char * filename,
char * paf_id,
char * paf_desc,
char * login_name,
char * datetime) ;
FILE *qfits_paf_print_header (char *filename,
char *paf_id,
char *paf_desc,
char *login_name, char *datetime);
/*----------------------------------------------------------------------------*/
/**
@ -86,9 +85,7 @@ FILE * qfits_paf_print_header(
If the key is not found, this function returns NULL.
*/
/*----------------------------------------------------------------------------*/
char * qfits_paf_query(
char * filename,
char * key) ;
char *qfits_paf_query (char *filename, char *key);
/*----------------------------------------------------------------------------*/
/**
@ -100,7 +97,7 @@ char * qfits_paf_query(
is checked with the presence of PAF.HDR.START at the beginning
*/
/*----------------------------------------------------------------------------*/
int qfits_is_paf_file(char * filename) ;
int qfits_is_paf_file (char *filename);
/* </dox> */
#ifdef __cplusplus

File diff suppressed because it is too large Load Diff

View File

@ -53,7 +53,7 @@
for readability.
*/
/*----------------------------------------------------------------------------*/
typedef unsigned char byte ;
typedef unsigned char byte;
/*----------------------------------------------------------------------------*/
/**
@ -126,49 +126,50 @@ int main(int argc, char * argv[])
@endcode
*/
/*----------------------------------------------------------------------------*/
typedef struct qfitsloader {
typedef struct qfitsloader
{
/** Private field to see if structure has been initialized */
int _init ;
int _init;
/** input: Name of the file you want to read pixels from */
char * filename ;
char *filename;
/** input: xtension number you want to read */
int xtnum ;
int xtnum;
/** input: Index of the plane you want, from 0 to np-1 */
int pnum ;
int pnum;
/** input: Pixel type you want (PTYPE_FLOAT, PTYPE_INT or PTYPE_DOUBLE) */
int ptype ;
int ptype;
/** input: Guarantee file copy or allow file mapping */
int map ;
int map;
/** output: Total number of extensions found in file */
int exts ;
int exts;
/** output: Size in X of the requested plane */
int lx ;
int lx;
/** output: Size in Y of the requested plane */
int ly ;
int ly;
/** output: Number of planes present in this extension */
int np ;
int np;
/** output: BITPIX for this extension */
int bitpix ;
int bitpix;
/** output: Start of the data segment (in bytes) for your request */
int seg_start ;
int seg_start;
/** output: Size of the data segment (in bytes) for your request */
int seg_size ;
int seg_size;
/** output: BSCALE found for this extension */
double bscale ;
double bscale;
/** output: BZERO found for this extension */
double bzero ;
double bzero;
/** output: Pointer to pixel buffer loaded as integer values */
int * ibuf ;
int *ibuf;
/** output: Pointer to pixel buffer loaded as float values */
float * fbuf ;
float *fbuf;
/** output: Pointer to pixel buffer loaded as double values */
double * dbuf ;
double *dbuf;
} qfitsloader ;
} qfitsloader;
/*----------------------------------------------------------------------------*/
@ -215,25 +216,26 @@ typedef struct qfitsloader {
could have been re-directed).
*/
/*----------------------------------------------------------------------------*/
typedef struct qfitsdumper {
typedef struct qfitsdumper
{
/** Name of the file to dump to, "STDOUT" to dump to stdout */
char * filename ;
char *filename;
/** Number of pixels in the buffer to dump */
int npix ;
int npix;
/** Buffer type: PTYPE_FLOAT, PTYPE_INT or PTYPE_DOUBLE */
int ptype ;
int ptype;
/** Pointer to input integer pixel buffer */
int * ibuf ;
int *ibuf;
/** Pointer to input float pixel buffer */
float * fbuf ;
float *fbuf;
/** Pointer to input double pixel buffer */
double * dbuf ;
double *dbuf;
/** Requested BITPIX in output FITS file */
int out_ptype ;
} qfitsdumper ;
int out_ptype;
} qfitsdumper;
/*-----------------------------------------------------------------------------
@ -265,7 +267,7 @@ typedef struct qfitsdumper {
is performed by qfits_loadpix() afterwards.
*/
/*----------------------------------------------------------------------------*/
int qfitsloader_init(qfitsloader * ql);
int qfitsloader_init (qfitsloader * ql);
/*----------------------------------------------------------------------------*/
/**
@ -287,7 +289,7 @@ int qfitsloader_init(qfitsloader * ql);
code before you make calls to the pixel loader here.
*/
/*----------------------------------------------------------------------------*/
int qfits_loadpix(qfitsloader * ql);
int qfits_loadpix (qfitsloader * ql);
/*----------------------------------------------------------------------------*/
/**
@ -310,7 +312,7 @@ int qfits_loadpix(qfitsloader * ql);
free on the returned pointer.
*/
/*----------------------------------------------------------------------------*/
float * qfits_pixin_float (byte *, int, int, double, double);
float *qfits_pixin_float (byte *, int, int, double, double);
/*----------------------------------------------------------------------------*/
/**
@ -333,7 +335,7 @@ float * qfits_pixin_float (byte *, int, int, double, double);
free on the returned pointer.
*/
/*----------------------------------------------------------------------------*/
int * qfits_pixin_int (byte *, int, int, double, double);
int *qfits_pixin_int (byte *, int, int, double, double);
/*----------------------------------------------------------------------------*/
/**
@ -354,9 +356,9 @@ int * qfits_pixin_int (byte *, int, int, double, double);
The returned buffer must be deallocated using the free() offered
by xmemory. It is enough to #include "xmemory.h" before calling
free on the returned pointer.
*/
*/
/*----------------------------------------------------------------------------*/
double * qfits_pixin_double(byte *, int, int, double, double);
double *qfits_pixin_double (byte *, int, int, double, double);
/*----------------------------------------------------------------------------*/
/**
@ -393,7 +395,7 @@ double * qfits_pixin_double(byte *, int, int, double, double);
will be performed to stdout.
*/
/*----------------------------------------------------------------------------*/
int qfits_pixdump(qfitsdumper * qd) ;
int qfits_pixdump (qfitsdumper * qd);
/*----------------------------------------------------------------------------*/
/**
@ -407,9 +409,9 @@ int qfits_pixdump(qfitsdumper * qd) ;
suitable for dumping to a FITS file (i.e. big-endian, in the
requested pixel type). The returned pointer must be deallocated
using the free() function offered by xmemory.
*/
*/
/*----------------------------------------------------------------------------*/
byte * qfits_pixdump_float(float * buf, int npix, int ptype);
byte *qfits_pixdump_float (float *buf, int npix, int ptype);
/*----------------------------------------------------------------------------*/
/**
@ -425,7 +427,7 @@ byte * qfits_pixdump_float(float * buf, int npix, int ptype);
using the free() function offered by xmemory.
*/
/*----------------------------------------------------------------------------*/
byte * qfits_pixdump_int(int * buf, int npix, int ptype);
byte *qfits_pixdump_int (int *buf, int npix, int ptype);
/*----------------------------------------------------------------------------*/
/**
@ -441,7 +443,7 @@ byte * qfits_pixdump_int(int * buf, int npix, int ptype);
using the free() function offered by xmemory.
*/
/*----------------------------------------------------------------------------*/
byte * qfits_pixdump_double(double * buf, int npix, int ptype);
byte *qfits_pixdump_double (double *buf, int npix, int ptype);
/* </dox> */
#endif

View File

@ -41,16 +41,21 @@
-----------------------------------------------------------------------------*/
/* Type of a display function only defined for legibility here */
typedef void (*qfits_err_dispfunc)(char *) ;
typedef void (*qfits_err_dispfunc) (char *);
/* Default display function prints out msg to stderr */
static void qfits_err_display_stderr(char * s)
{ fprintf(stderr, "qfits: %s\n", s); }
static void
qfits_err_display_stderr (char *s)
{
fprintf (stderr, "qfits: %s\n", s);
}
/* Static control structure, completely private */
static struct {
qfits_err_dispfunc disp[QFITS_ERR_MAXERRDISP] ;
int n ;
int active ;
} qfits_err_control = {{qfits_err_display_stderr}, 1, 0} ;
static struct
{
qfits_err_dispfunc disp[QFITS_ERR_MAXERRDISP];
int n;
int active;
} qfits_err_control = { {qfits_err_display_stderr}, 1, 0 };
/*-----------------------------------------------------------------------------
Function codes
@ -64,21 +69,24 @@ static struct {
It calls registered display functions one after another.
*/
/*----------------------------------------------------------------------------*/
static void qfits_err_main_display(char * msg)
static void
qfits_err_main_display (char *msg)
{
int i ;
int i;
/* Check if there is a message in input */
if (msg==NULL)
return ;
/* Check if there is a message in input */
if (msg == NULL)
return;
/* Loop on all registered functions and call them */
for (i=0 ; i<qfits_err_control.n ; i++) {
if (qfits_err_control.disp[i]) {
qfits_err_control.disp[i](msg);
}
/* Loop on all registered functions and call them */
for (i = 0; i < qfits_err_control.n; i++)
{
if (qfits_err_control.disp[i])
{
qfits_err_control.disp[i] (msg);
}
return ;
}
return;
}
/*----------------------------------------------------------------------------*/
@ -91,9 +99,10 @@ static void qfits_err_main_display(char * msg)
messages using the registered functions, otherwise they do nothing.
*/
/*----------------------------------------------------------------------------*/
int qfits_err_statget(void)
int
qfits_err_statget (void)
{
return qfits_err_control.active ;
return qfits_err_control.active;
}
/*----------------------------------------------------------------------------*/
@ -114,12 +123,13 @@ int qfits_err_statget(void)
@endcode
*/
/*----------------------------------------------------------------------------*/
int qfits_err_statset(int sta)
int
qfits_err_statset (int sta)
{
int prev ;
prev = qfits_err_control.active ;
qfits_err_control.active=sta ;
return prev ;
int prev;
prev = qfits_err_control.active;
qfits_err_control.active = sta;
return prev;
}
/*----------------------------------------------------------------------------*/
@ -146,53 +156,60 @@ int qfits_err_statset(int sta)
returning -1.
*/
/*----------------------------------------------------------------------------*/
int qfits_err_register(qfits_err_dispfunc dispfn)
int
qfits_err_register (qfits_err_dispfunc dispfn)
{
if (qfits_err_control.n==QFITS_ERR_MAXERRDISP) {
/* Cannot register any more function */
return -1 ;
}
qfits_err_control.disp[qfits_err_control.n] = dispfn ;
qfits_err_control.n ++ ;
return 0 ;
if (qfits_err_control.n == QFITS_ERR_MAXERRDISP)
{
/* Cannot register any more function */
return -1;
}
qfits_err_control.disp[qfits_err_control.n] = dispfn;
qfits_err_control.n++;
return 0;
}
/* Public warning/error functions */
void qfits_warning(const char *fmt, ...)
void
qfits_warning (const char *fmt, ...)
{
char msg[QFITS_ERR_MSGSIZE] ;
char all[QFITS_ERR_MSGSIZE] ;
va_list ap ;
char msg[QFITS_ERR_MSGSIZE];
char all[QFITS_ERR_MSGSIZE];
va_list ap;
/* Check if display is activated */
if (qfits_err_control.active==0) {
return ;
}
va_start(ap, fmt) ;
vsprintf(msg, fmt, ap) ;
va_end(ap);
/* Check if display is activated */
if (qfits_err_control.active == 0)
{
return;
}
va_start (ap, fmt);
vsprintf (msg, fmt, ap);
va_end (ap);
sprintf(all, "*** %s", msg);
qfits_err_main_display(all);
return ;
sprintf (all, "*** %s", msg);
qfits_err_main_display (all);
return;
}
void qfits_error(const char *fmt, ...)
void
qfits_error (const char *fmt, ...)
{
char msg[QFITS_ERR_MSGSIZE] ;
char all[QFITS_ERR_MSGSIZE] ;
va_list ap ;
char msg[QFITS_ERR_MSGSIZE];
char all[QFITS_ERR_MSGSIZE];
va_list ap;
/* Check if display is activated */
if (qfits_err_control.active==0) {
return ;
}
va_start(ap, fmt) ;
vsprintf(msg, fmt, ap) ;
va_end(ap);
/* Check if display is activated */
if (qfits_err_control.active == 0)
{
return;
}
va_start (ap, fmt);
vsprintf (msg, fmt, ap);
va_end (ap);
sprintf(all, "error: %s", msg);
qfits_err_main_display(all);
return ;
sprintf (all, "error: %s", msg);
qfits_err_main_display (all);
return;
}
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -40,7 +40,7 @@
messages using the registered functions, otherwise they do nothing.
*/
/*----------------------------------------------------------------------------*/
int qfits_err_statget(void);
int qfits_err_statget (void);
/*----------------------------------------------------------------------------*/
/**
@ -60,7 +60,7 @@ int qfits_err_statget(void);
\end{verbatim}
*/
/*----------------------------------------------------------------------------*/
int qfits_err_statset(int sta);
int qfits_err_statset (int sta);
/*----------------------------------------------------------------------------*/
/**
@ -86,12 +86,12 @@ int qfits_err_statset(int sta);
returning -1.
*/
/*----------------------------------------------------------------------------*/
int qfits_err_register( void (*dispfn)(char*) ) ;
int qfits_err_register (void (*dispfn) (char *));
/* </dox> */
/* Public warning/error functions */
void qfits_warning(const char *fmt, ...);
void qfits_error(const char *fmt, ...);
void qfits_warning (const char *fmt, ...);
void qfits_error (const char *fmt, ...);
#endif
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -50,14 +50,14 @@
*/
/** A regular expression matching a floating-point number */
static char regex_float[] =
"^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?$";
"^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?$";
/** A regular expression matching an integer */
static char regex_int[] = "^[+-]?[0-9]+$";
/** A regular expression matching a complex number (int or float) */
static char regex_cmp[] =
"^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?[ ]+[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?$";
"^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?[ ]+[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?$";
/*-----------------------------------------------------------------------------
Function codes
@ -90,9 +90,10 @@ static char regex_cmp[] =
Returns NULL in case the requested keyword cannot be found.
*/
/*----------------------------------------------------------------------------*/
char * qfits_query_hdr(char * filename, const char * keyword)
char *
qfits_query_hdr (char *filename, const char *keyword)
{
return qfits_query_ext(filename, keyword, 0);
return qfits_query_ext (filename, keyword, 0);
}
/*----------------------------------------------------------------------------*/
@ -108,90 +109,99 @@ char * qfits_query_hdr(char * filename, const char * keyword)
strictly identical to qfits_query_hdr().
*/
/*----------------------------------------------------------------------------*/
char * qfits_query_ext(char * filename, const char * keyword, int xtnum)
char *
qfits_query_ext (char *filename, const char *keyword, int xtnum)
{
char * exp_key ;
char * where ;
char * start ;
char * value ;
char test1, test2 ;
int i ;
int len ;
int different ;
int seg_start ;
int seg_size ;
long bufcount ;
size_t size ;
char *exp_key;
char *where;
char *start;
char *value;
char test1, test2;
int i;
int len;
int different;
int seg_start;
int seg_size;
long bufcount;
size_t size;
/* Bulletproof entries */
if (filename==NULL || keyword==NULL || xtnum<0) return NULL ;
/* Bulletproof entries */
if (filename == NULL || keyword == NULL || xtnum < 0)
return NULL;
/* Expand keyword */
exp_key = qfits_expand_keyword(keyword);
/* Expand keyword */
exp_key = qfits_expand_keyword (keyword);
/*
* Find out offsets to the required extension
* Record the xtension start and stop offsets
*/
if (qfits_get_hdrinfo(filename, xtnum, &seg_start, &seg_size)==-1) {
return NULL ;
/*
* Find out offsets to the required extension
* Record the xtension start and stop offsets
*/
if (qfits_get_hdrinfo (filename, xtnum, &seg_start, &seg_size) == -1)
{
return NULL;
}
/*
* Get a hand on requested buffer
*/
/*
* Get a hand on requested buffer
*/
start = falloc(filename, seg_start, &size);
if (start==NULL) return NULL ;
start = falloc (filename, seg_start, &size);
if (start == NULL)
return NULL;
/*
* Look for keyword in header
*/
/*
* Look for keyword in header
*/
bufcount=0 ;
where = start ;
len = (int)strlen(exp_key);
while (1) {
different=0 ;
for (i=0 ; i<len ; i++) {
if (where[i]!=exp_key[i]) {
different++ ;
break ;
}
}
if (!different) {
/* Get 2 chars after keyword */
test1=where[len];
test2=where[len+1];
/* If first subsequent character is the equal sign, bingo. */
if (test1=='=') break ;
/* If subsequent char is equal sign, bingo */
if (test1==' ' && (test2=='=' || test2==' '))
break ;
}
/* Watch out for header end */
if ((where[0]=='E') &&
(where[1]=='N') &&
(where[2]=='D') &&
(where[3]==' ')) {
/* Detected header end */
fdealloc(start, seg_start, size) ;
return NULL ;
}
/* Forward one line */
where += 80 ;
bufcount += 80 ;
if (bufcount>seg_size) {
/* File is damaged or not FITS: bailout */
fdealloc(start, seg_start, size) ;
return NULL ;
}
bufcount = 0;
where = start;
len = (int) strlen (exp_key);
while (1)
{
different = 0;
for (i = 0; i < len; i++)
{
if (where[i] != exp_key[i])
{
different++;
break;
}
}
if (!different)
{
/* Get 2 chars after keyword */
test1 = where[len];
test2 = where[len + 1];
/* If first subsequent character is the equal sign, bingo. */
if (test1 == '=')
break;
/* If subsequent char is equal sign, bingo */
if (test1 == ' ' && (test2 == '=' || test2 == ' '))
break;
}
/* Watch out for header end */
if ((where[0] == 'E') &&
(where[1] == 'N') && (where[2] == 'D') && (where[3] == ' '))
{
/* Detected header end */
fdealloc (start, seg_start, size);
return NULL;
}
/* Forward one line */
where += 80;
bufcount += 80;
if (bufcount > seg_size)
{
/* File is damaged or not FITS: bailout */
fdealloc (start, seg_start, size);
return NULL;
}
}
/* Found the keyword, now get its value */
value = qfits_getvalue(where);
fdealloc(start, seg_start, size) ;
return value;
/* Found the keyword, now get its value */
value = qfits_getvalue (where);
fdealloc (start, seg_start, size);
return value;
}
/*----------------------------------------------------------------------------*/
@ -203,9 +213,10 @@ char * qfits_query_ext(char * filename, const char * keyword, int xtnum)
extension is found, and -1 if an error occurred.
*/
/*----------------------------------------------------------------------------*/
int qfits_query_n_ext(char * filename)
int
qfits_query_n_ext (char *filename)
{
return qfits_query(filename, QFITS_QUERY_N_EXT);
return qfits_query (filename, QFITS_QUERY_N_EXT);
}
/*----------------------------------------------------------------------------*/
@ -218,45 +229,54 @@ int qfits_query_n_ext(char * filename)
and -1 if an error occurred.
*/
/*----------------------------------------------------------------------------*/
int qfits_query_nplanes(char * filename, int extnum)
int
qfits_query_nplanes (char *filename, int extnum)
{
char * sval ;
int next ;
int naxes ;
int nplanes ;
char *sval;
int next;
int naxes;
int nplanes;
/* Check file existence */
if (filename == NULL) return -1 ;
/* Check validity of extnum */
next = qfits_query_n_ext(filename) ;
if (extnum>next) {
qfits_error("invalid extension specified") ;
return -1 ;
/* Check file existence */
if (filename == NULL)
return -1;
/* Check validity of extnum */
next = qfits_query_n_ext (filename);
if (extnum > next)
{
qfits_error ("invalid extension specified");
return -1;
}
/* Find the number of axes */
naxes = 0;
if ((sval = qfits_query_ext (filename, "NAXIS", extnum)) == NULL)
{
qfits_error ("missing key in header: NAXIS");
return -1;
}
naxes = atoi (sval);
/* Check validity of naxes */
if ((naxes < 2) || (naxes > 3))
return -1;
/* Two dimensions cube */
if (naxes == 2)
nplanes = 1;
else
{
/* For 3D cubes, get the third dimension size */
if ((sval = qfits_query_ext (filename, "NAXIS3", extnum)) == NULL)
{
qfits_error ("missing key in header: NAXIS3");
return -1;
}
/* Find the number of axes */
naxes = 0 ;
if ((sval = qfits_query_ext(filename, "NAXIS", extnum)) == NULL) {
qfits_error("missing key in header: NAXIS");
return -1 ;
}
naxes = atoi(sval) ;
/* Check validity of naxes */
if ((naxes < 2) || (naxes > 3)) return -1 ;
/* Two dimensions cube */
if (naxes == 2) nplanes = 1 ;
else {
/* For 3D cubes, get the third dimension size */
if ((sval = qfits_query_ext(filename, "NAXIS3", extnum))==NULL) {
qfits_error("missing key in header: NAXIS3");
return -1 ;
}
nplanes = atoi(sval);
if (nplanes < 1) nplanes = 0 ;
}
return nplanes ;
nplanes = atoi (sval);
if (nplanes < 1)
nplanes = 0;
}
return nplanes;
}
/*----------------------------------------------------------------------------*/
@ -278,51 +298,61 @@ int qfits_query_nplanes(char * filename, int extnum)
*/
/*----------------------------------------------------------------------------*/
#define PRETTY_STRING_STATICBUFS 8
char * qfits_pretty_string(char * s)
char *
qfits_pretty_string (char *s)
{
static char pretty_buf[PRETTY_STRING_STATICBUFS][81] ;
static int flip=0 ;
char * pretty ;
int i,j ;
static char pretty_buf[PRETTY_STRING_STATICBUFS][81];
static int flip = 0;
char *pretty;
int i, j;
/* bulletproof */
if (s==NULL) return NULL ;
/* bulletproof */
if (s == NULL)
return NULL;
/* Switch between static buffers */
pretty = pretty_buf[flip];
flip++ ;
if (flip==PRETTY_STRING_STATICBUFS)
flip=0 ;
pretty[0] = (char)0 ;
if (s[0]!='\'') return s ;
/* Switch between static buffers */
pretty = pretty_buf[flip];
flip++;
if (flip == PRETTY_STRING_STATICBUFS)
flip = 0;
/* skip first quote */
i=1 ;
j=0 ;
/* trim left-side blanks */
while (s[i]==' ') {
if (i==(int)strlen(s)) break ;
i++ ;
pretty[0] = (char) 0;
if (s[0] != '\'')
return s;
/* skip first quote */
i = 1;
j = 0;
/* trim left-side blanks */
while (s[i] == ' ')
{
if (i == (int) strlen (s))
break;
i++;
}
if (i>=(int)(strlen(s)-1)) return pretty ;
/* copy string, changing double quotes to single ones */
while (i<(int)strlen(s)) {
if (s[i]=='\'') {
i++ ;
}
pretty[j]=s[i];
i++ ;
j++ ;
}
/* NULL-terminate the pretty string */
pretty[j+1]=(char)0;
/* trim right-side blanks */
j = (int)strlen(pretty)-1;
while (pretty[j]==' ') j-- ;
pretty[j+1]=(char)0;
if (i >= (int) (strlen (s) - 1))
return pretty;
/* copy string, changing double quotes to single ones */
while (i < (int) strlen (s))
{
if (s[i] == '\'')
{
i++;
}
pretty[j] = s[i];
i++;
j++;
}
/* NULL-terminate the pretty string */
pretty[j + 1] = (char) 0;
/* trim right-side blanks */
j = (int) strlen (pretty) - 1;
while (pretty[j] == ' ')
j--;
pretty[j + 1] = (char) 0;
return pretty;
}
#undef PRETTY_STRING_STATICBUFS
/*----------------------------------------------------------------------------*/
@ -334,13 +364,18 @@ char * qfits_pretty_string(char * s)
Identifies if a FITS value is boolean.
*/
/*----------------------------------------------------------------------------*/
int qfits_is_boolean(char * s)
int
qfits_is_boolean (char *s)
{
if (s==NULL) return 0 ;
if (s[0]==0) return 0 ;
if ((int)strlen(s)>1) return 0 ;
if (s[0]=='T' || s[0]=='F') return 1 ;
return 0 ;
if (s == NULL)
return 0;
if (s[0] == 0)
return 0;
if ((int) strlen (s) > 1)
return 0;
if (s[0] == 'T' || s[0] == 'F')
return 1;
return 0;
}
/*----------------------------------------------------------------------------*/
@ -352,20 +387,24 @@ int qfits_is_boolean(char * s)
Identifies if a FITS value is an integer.
*/
/*----------------------------------------------------------------------------*/
int qfits_is_int(char * s)
int
qfits_is_int (char *s)
{
regex_t re_int ;
int status ;
regex_t re_int;
int status;
if (s==NULL) return 0 ;
if (s[0]==0) return 0 ;
if (regcomp(&re_int, &regex_int[0], REG_EXTENDED|REG_NOSUB)!=0) {
qfits_error("internal error: compiling int rule");
exit(-1);
if (s == NULL)
return 0;
if (s[0] == 0)
return 0;
if (regcomp (&re_int, &regex_int[0], REG_EXTENDED | REG_NOSUB) != 0)
{
qfits_error ("internal error: compiling int rule");
exit (-1);
}
status = regexec(&re_int, s, 0, NULL, 0) ;
regfree(&re_int) ;
return (status) ? 0 : 1 ;
status = regexec (&re_int, s, 0, NULL, 0);
regfree (&re_int);
return (status) ? 0 : 1;
}
/*----------------------------------------------------------------------------*/
@ -377,20 +416,24 @@ int qfits_is_int(char * s)
Identifies if a FITS value is float.
*/
/*----------------------------------------------------------------------------*/
int qfits_is_float(char * s)
int
qfits_is_float (char *s)
{
regex_t re_float;
int status ;
regex_t re_float;
int status;
if (s==NULL) return 0 ;
if (s[0]==0) return 0 ;
if (regcomp(&re_float, &regex_float[0], REG_EXTENDED|REG_NOSUB)!=0) {
qfits_error("internal error: compiling float rule");
exit(-1);
if (s == NULL)
return 0;
if (s[0] == 0)
return 0;
if (regcomp (&re_float, &regex_float[0], REG_EXTENDED | REG_NOSUB) != 0)
{
qfits_error ("internal error: compiling float rule");
exit (-1);
}
status = regexec(&re_float, s, 0, NULL, 0) ;
regfree(&re_float) ;
return (status) ? 0 : 1 ;
status = regexec (&re_float, s, 0, NULL, 0);
regfree (&re_float);
return (status) ? 0 : 1;
}
/*----------------------------------------------------------------------------*/
@ -402,20 +445,24 @@ int qfits_is_float(char * s)
Identifies if a FITS value is complex.
*/
/*----------------------------------------------------------------------------*/
int qfits_is_complex(char * s)
int
qfits_is_complex (char *s)
{
regex_t re_cmp ;
int status ;
regex_t re_cmp;
int status;
if (s==NULL) return 0 ;
if (s[0]==0) return 0 ;
if (regcomp(&re_cmp, &regex_cmp[0], REG_EXTENDED|REG_NOSUB)!=0) {
qfits_error("internal error: compiling complex rule");
exit(-1);
if (s == NULL)
return 0;
if (s[0] == 0)
return 0;
if (regcomp (&re_cmp, &regex_cmp[0], REG_EXTENDED | REG_NOSUB) != 0)
{
qfits_error ("internal error: compiling complex rule");
exit (-1);
}
status = regexec(&re_cmp, s, 0, NULL, 0) ;
regfree(&re_cmp) ;
return (status) ? 0 : 1 ;
status = regexec (&re_cmp, s, 0, NULL, 0);
regfree (&re_cmp);
return (status) ? 0 : 1;
}
/*----------------------------------------------------------------------------*/
@ -427,11 +474,14 @@ int qfits_is_complex(char * s)
Identifies if a FITS value is a string.
*/
/*----------------------------------------------------------------------------*/
int qfits_is_string(char * s)
int
qfits_is_string (char *s)
{
if (s==NULL) return 0 ;
if (s[0]=='\'') return 1 ;
return 0 ;
if (s == NULL)
return 0;
if (s[0] == '\'')
return 1;
return 0;
}
/*----------------------------------------------------------------------------*/
@ -450,14 +500,20 @@ int qfits_is_string(char * s)
- QFITS_STRING (5) for a FITS string.
*/
/*----------------------------------------------------------------------------*/
int qfits_get_type(char * s)
int
qfits_get_type (char *s)
{
if (s==NULL) return QFITS_UNKNOWN ;
if (qfits_is_boolean(s)) return QFITS_BOOLEAN ;
if (qfits_is_int(s)) return QFITS_INT ;
if (qfits_is_float(s)) return QFITS_FLOAT ;
if (qfits_is_complex(s)) return QFITS_COMPLEX ;
return QFITS_STRING ;
if (s == NULL)
return QFITS_UNKNOWN;
if (qfits_is_boolean (s))
return QFITS_BOOLEAN;
if (qfits_is_int (s))
return QFITS_INT;
if (qfits_is_float (s))
return QFITS_FLOAT;
if (qfits_is_complex (s))
return QFITS_COMPLEX;
return QFITS_STRING;
}
/*----------------------------------------------------------------------------*/
@ -479,26 +535,27 @@ int qfits_get_type(char * s)
main header in the file.
*/
/*----------------------------------------------------------------------------*/
int qfits_get_hdrinfo(
char * filename,
int xtnum,
int * seg_start,
int * seg_size)
int
qfits_get_hdrinfo (char *filename, int xtnum, int *seg_start, int *seg_size)
{
if (filename==NULL || xtnum<0 || (seg_start==NULL && seg_size==NULL)) {
return -1 ;
if (filename == NULL || xtnum < 0
|| (seg_start == NULL && seg_size == NULL))
{
return -1;
}
if (seg_start!=NULL) {
*seg_start = qfits_query(filename, QFITS_QUERY_HDR_START | xtnum);
if (*seg_start<0)
return -1 ;
if (seg_start != NULL)
{
*seg_start = qfits_query (filename, QFITS_QUERY_HDR_START | xtnum);
if (*seg_start < 0)
return -1;
}
if (seg_size!=NULL) {
*seg_size = qfits_query(filename, QFITS_QUERY_HDR_SIZE | xtnum);
if (*seg_size<0)
return -1 ;
if (seg_size != NULL)
{
*seg_size = qfits_query (filename, QFITS_QUERY_HDR_SIZE | xtnum);
if (*seg_size < 0)
return -1;
}
return 0 ;
return 0;
}
/*----------------------------------------------------------------------------*/
@ -520,26 +577,27 @@ int qfits_get_hdrinfo(
main header in the file.
*/
/*----------------------------------------------------------------------------*/
int qfits_get_datinfo(
char * filename,
int xtnum,
int * seg_start,
int * seg_size)
int
qfits_get_datinfo (char *filename, int xtnum, int *seg_start, int *seg_size)
{
if (filename==NULL || xtnum<0 || (seg_start==NULL && seg_size==NULL)) {
return -1 ;
if (filename == NULL || xtnum < 0
|| (seg_start == NULL && seg_size == NULL))
{
return -1;
}
if (seg_start!=NULL) {
*seg_start = qfits_query(filename, QFITS_QUERY_DAT_START | xtnum);
if (*seg_start<0)
return -1 ;
if (seg_start != NULL)
{
*seg_start = qfits_query (filename, QFITS_QUERY_DAT_START | xtnum);
if (*seg_start < 0)
return -1;
}
if (seg_size!=NULL) {
*seg_size = qfits_query(filename, QFITS_QUERY_DAT_SIZE | xtnum);
if (*seg_size<0)
return -1 ;
if (seg_size != NULL)
{
*seg_size = qfits_query (filename, QFITS_QUERY_DAT_SIZE | xtnum);
if (*seg_size < 0)
return -1;
}
return 0 ;
return 0;
}
/*----------------------------------------------------------------------------*/
@ -550,72 +608,74 @@ int qfits_get_datinfo(
@return Allocated string containing the card or NULL
*/
/*----------------------------------------------------------------------------*/
char * qfits_query_card(
char * filename,
char * keyword)
char *
qfits_query_card (char *filename, char *keyword)
{
char * exp_key ;
int fd ;
char * buf ;
char * buf2 ;
char * where ;
int hs ;
char * card ;
char *exp_key;
int fd;
char *buf;
char *buf2;
char *where;
int hs;
char *card;
/* Bulletproof entries */
if (filename==NULL || keyword==NULL) return NULL ;
/* Bulletproof entries */
if (filename == NULL || keyword == NULL)
return NULL;
/* Expand keyword */
exp_key = qfits_expand_keyword(keyword) ;
/* Expand keyword */
exp_key = qfits_expand_keyword (keyword);
/* Memory-map the FITS header of the input file */
qfits_get_hdrinfo(filename, 0, NULL, &hs) ;
if (hs < 1) {
qfits_error("error getting FITS header size for %s", filename);
return NULL ;
/* Memory-map the FITS header of the input file */
qfits_get_hdrinfo (filename, 0, NULL, &hs);
if (hs < 1)
{
qfits_error ("error getting FITS header size for %s", filename);
return NULL;
}
fd = open(filename, O_RDWR) ;
if (fd == -1) return NULL ;
buf = (char*)mmap(0,
hs,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0) ;
if (buf == (char*)-1) {
perror("mmap") ;
close(fd) ;
return NULL ;
fd = open (filename, O_RDWR);
if (fd == -1)
return NULL;
buf = (char *) mmap (0, hs, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == (char *) -1)
{
perror ("mmap");
close (fd);
return NULL;
}
/* Apply search for the input keyword */
buf2 = malloc(hs+1) ;
memcpy(buf2, buf, hs) ;
buf2[hs] = (char)0 ;
where = buf2 ;
do {
where = strstr(where, exp_key);
if (where == NULL) {
close(fd);
munmap(buf,hs);
free(buf2) ;
return NULL ;
}
if ((where-buf2)%80) where++ ;
} while ((where-buf2)%80) ;
where = buf + (int)(where - buf2) ;
/* Create the card */
card = malloc(81*sizeof(char)) ;
strncpy(card, where, 80) ;
card[80] = (char)0 ;
/* Apply search for the input keyword */
buf2 = malloc (hs + 1);
memcpy (buf2, buf, hs);
buf2[hs] = (char) 0;
where = buf2;
do
{
where = strstr (where, exp_key);
if (where == NULL)
{
close (fd);
munmap (buf, hs);
free (buf2);
return NULL;
}
if ((where - buf2) % 80)
where++;
}
while ((where - buf2) % 80);
/* Free and return */
close(fd) ;
munmap(buf, hs) ;
free(buf2) ;
return card ;
where = buf + (int) (where - buf2);
/* Create the card */
card = malloc (81 * sizeof (char));
strncpy (card, where, 80);
card[80] = (char) 0;
/* Free and return */
close (fd);
munmap (buf, hs);
free (buf2);
return card;
}
/*----------------------------------------------------------------------------*/
@ -637,76 +697,77 @@ char * qfits_query_card(
Returns 0 if everything worked Ok, -1 otherwise.
*/
/*----------------------------------------------------------------------------*/
int qfits_replace_card(
char * filename,
const char * keyword,
char * substitute)
int
qfits_replace_card (char *filename, const char *keyword, char *substitute)
{
char * exp_key ;
int fd ;
char * buf ;
char * buf2 ;
char * where ;
int hs ;
char *exp_key;
int fd;
char *buf;
char *buf2;
char *where;
int hs;
/* Bulletproof entries */
if (filename==NULL || keyword==NULL || substitute==NULL) return -1 ;
/* Bulletproof entries */
if (filename == NULL || keyword == NULL || substitute == NULL)
return -1;
/* Expand keyword */
exp_key = qfits_expand_keyword(keyword);
/*
* Memory-map the FITS header of the input file
*/
/* Expand keyword */
exp_key = qfits_expand_keyword (keyword);
/*
* Memory-map the FITS header of the input file
*/
qfits_get_hdrinfo(filename, 0, NULL, &hs) ;
if (hs < 1) {
qfits_error("error getting FITS header size for %s", filename);
return -1 ;
qfits_get_hdrinfo (filename, 0, NULL, &hs);
if (hs < 1)
{
qfits_error ("error getting FITS header size for %s", filename);
return -1;
}
fd = open(filename, O_RDWR) ;
if (fd == -1) {
return -1 ;
fd = open (filename, O_RDWR);
if (fd == -1)
{
return -1;
}
buf = (char*)mmap(0,
hs,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0) ;
if (buf == (char*)-1) {
perror("mmap") ;
close(fd) ;
return -1 ;
buf = (char *) mmap (0, hs, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == (char *) -1)
{
perror ("mmap");
close (fd);
return -1;
}
/* Apply search and replace for the input keyword lists */
buf2 = malloc(hs+1) ;
memcpy(buf2, buf, hs) ;
buf2[hs] = (char)0 ;
where = buf2 ;
do {
where = strstr(where, exp_key);
if (where == NULL) {
close(fd);
munmap(buf,hs);
free(buf2) ;
return -1 ;
}
if ((where-buf2)%80) where++ ;
} while ((where-buf2)%80) ;
where = buf + (int)(where - buf2) ;
/* Replace current placeholder by blanks */
memset(where, ' ', 80);
/* Copy substitute into placeholder */
memcpy(where, substitute, strlen(substitute));
/* Apply search and replace for the input keyword lists */
buf2 = malloc (hs + 1);
memcpy (buf2, buf, hs);
buf2[hs] = (char) 0;
where = buf2;
do
{
where = strstr (where, exp_key);
if (where == NULL)
{
close (fd);
munmap (buf, hs);
free (buf2);
return -1;
}
if ((where - buf2) % 80)
where++;
}
while ((where - buf2) % 80);
close(fd) ;
munmap(buf, hs) ;
free(buf2) ;
return 0 ;
where = buf + (int) (where - buf2);
/* Replace current placeholder by blanks */
memset (where, ' ', 80);
/* Copy substitute into placeholder */
memcpy (where, substitute, strlen (substitute));
close (fd);
munmap (buf, hs);
free (buf2);
return 0;
}
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -22,7 +22,8 @@
#define SIMPLE_H
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
/*-----------------------------------------------------------------------------
@ -82,7 +83,7 @@ extern "C" {
Returns NULL in case the requested keyword cannot be found.
*/
/*----------------------------------------------------------------------------*/
char * qfits_query_hdr(char * filename, const char * keyword) ;
char *qfits_query_hdr (char *filename, const char *keyword);
/*----------------------------------------------------------------------------*/
/**
@ -97,7 +98,7 @@ char * qfits_query_hdr(char * filename, const char * keyword) ;
strictly identical to qfits_query_hdr().
*/
/*----------------------------------------------------------------------------*/
char * qfits_query_ext(char * filename, const char * keyword, int xtnum) ;
char *qfits_query_ext (char *filename, const char *keyword, int xtnum);
/*----------------------------------------------------------------------------*/
/**
@ -109,7 +110,7 @@ char * qfits_query_ext(char * filename, const char * keyword, int xtnum) ;
extension is found, and -1 if an error occurred.
*/
/*----------------------------------------------------------------------------*/
int qfits_query_n_ext(char * filename) ;
int qfits_query_n_ext (char *filename);
/*----------------------------------------------------------------------------*/
/**
@ -121,7 +122,7 @@ int qfits_query_n_ext(char * filename) ;
and -1 if an error occurred.
*/
/*----------------------------------------------------------------------------*/
int qfits_query_nplanes(char * filename, int extnum) ;
int qfits_query_nplanes (char *filename, int extnum);
/*----------------------------------------------------------------------------*/
/**
@ -141,7 +142,7 @@ int qfits_query_nplanes(char * filename, int extnum) ;
*/
/*----------------------------------------------------------------------------*/
char * qfits_pretty_string(char * s) ;
char *qfits_pretty_string (char *s);
/*----------------------------------------------------------------------------*/
/**
@ -152,7 +153,7 @@ char * qfits_pretty_string(char * s) ;
Identifies if a FITS value is boolean.
*/
/*----------------------------------------------------------------------------*/
int qfits_is_boolean(char * s) ;
int qfits_is_boolean (char *s);
/*----------------------------------------------------------------------------*/
/**
@ -163,7 +164,7 @@ int qfits_is_boolean(char * s) ;
Identifies if a FITS value is an integer.
*/
/*----------------------------------------------------------------------------*/
int qfits_is_int(char * s) ;
int qfits_is_int (char *s);
/*----------------------------------------------------------------------------*/
/**
@ -174,7 +175,7 @@ int qfits_is_int(char * s) ;
Identifies if a FITS value is float.
*/
/*----------------------------------------------------------------------------*/
int qfits_is_float(char * s) ;
int qfits_is_float (char *s);
/*----------------------------------------------------------------------------*/
/**
@ -185,7 +186,7 @@ int qfits_is_float(char * s) ;
Identifies if a FITS value is complex.
*/
/*----------------------------------------------------------------------------*/
int qfits_is_complex(char * s) ;
int qfits_is_complex (char *s);
/*----------------------------------------------------------------------------*/
/**
@ -196,7 +197,7 @@ int qfits_is_complex(char * s) ;
Identifies if a FITS value is a string.
*/
/*----------------------------------------------------------------------------*/
int qfits_is_string(char * s) ;
int qfits_is_string (char *s);
/*----------------------------------------------------------------------------*/
/**
@ -214,7 +215,7 @@ int qfits_is_string(char * s) ;
- QFITS_STRING (5) for a FITS string.
*/
/*----------------------------------------------------------------------------*/
int qfits_get_type(char * s) ;
int qfits_get_type (char *s);
/*----------------------------------------------------------------------------*/
/**
@ -235,11 +236,8 @@ int qfits_get_type(char * s) ;
main header in the file.
*/
/*----------------------------------------------------------------------------*/
int qfits_get_hdrinfo(
char * filename,
int xtnum,
int * seg_start,
int * seg_size) ;
int qfits_get_hdrinfo (char *filename,
int xtnum, int *seg_start, int *seg_size);
/*----------------------------------------------------------------------------*/
/**
@ -260,11 +258,8 @@ int qfits_get_hdrinfo(
main header in the file.
*/
/*----------------------------------------------------------------------------*/
int qfits_get_datinfo(
char * filename,
int xtnum,
int * seg_start,
int * seg_size) ;
int qfits_get_datinfo (char *filename,
int xtnum, int *seg_start, int *seg_size);
/*----------------------------------------------------------------------------*/
/**
@ -274,9 +269,7 @@ int qfits_get_datinfo(
@return Allocated string containing the card or NULL
*/
/*----------------------------------------------------------------------------*/
char * qfits_query_card(
char * filename,
char * keyword) ;
char *qfits_query_card (char *filename, char *keyword);
/*----------------------------------------------------------------------------*/
/**
@ -297,10 +290,8 @@ char * qfits_query_card(
Returns 0 if everything worked Ok, -1 otherwise.
*/
/*----------------------------------------------------------------------------*/
int qfits_replace_card(
char * filename,
const char * keyword,
char * substitute) ;
int qfits_replace_card (char *filename,
const char *keyword, char *substitute);
/* </dox> */
#ifdef __cplusplus

View File

@ -19,7 +19,8 @@
#define STATIC_SZ_H
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
/* <dox> */

View File

@ -75,8 +75,8 @@
Private to this module
-----------------------------------------------------------------------------*/
static long timer_to_date(time_t time_secs) ;
static long timer_to_time(time_t time_secs) ;
static long timer_to_date (time_t time_secs);
static long timer_to_time (time_t time_secs);
/*-----------------------------------------------------------------------------
Static Function code
@ -95,9 +95,10 @@ static long timer_to_time(time_t time_secs) ;
Example: 19 Oct 2000 is returned as 20001019
*/
/*----------------------------------------------------------------------------*/
long qfits_date_now (void)
long
qfits_date_now (void)
{
return (timer_to_date (time (NULL)));
return (timer_to_date (time (NULL)));
}
/*----------------------------------------------------------------------------*/
@ -111,13 +112,13 @@ long qfits_date_now (void)
Example: 15:36:12.84 is returned as 15361284
*/
/*----------------------------------------------------------------------------*/
long qfits_time_now(void)
long
qfits_time_now (void)
{
struct timeval time_struct;
struct timeval time_struct;
gettimeofday (&time_struct, 0);
return (timer_to_time (time_struct.tv_sec)
+ time_struct.tv_usec / 10000);
gettimeofday (&time_struct, 0);
return (timer_to_time (time_struct.tv_sec) + time_struct.tv_usec / 10000);
}
/*----------------------------------------------------------------------------*/
@ -133,25 +134,31 @@ long qfits_time_now(void)
(GMT).
*/
/*----------------------------------------------------------------------------*/
static long timer_to_date(time_t time_secs)
static long
timer_to_date (time_t time_secs)
{
struct tm *time_struct;
struct tm *time_struct;
if (time_secs == 0) {
return 0;
} else {
/* Convert into a long value CCYYMMDD */
time_struct = localtime (&time_secs);
if (time_struct) {
time_struct-> tm_year += 1900;
return (MAKE_DATE ( time_struct-> tm_year / 100,
time_struct-> tm_year % 100,
time_struct-> tm_mon + 1,
time_struct-> tm_mday));
} else {
return (19700101);
}
if (time_secs == 0)
{
return 0;
}
else
{
/* Convert into a long value CCYYMMDD */
time_struct = localtime (&time_secs);
if (time_struct)
{
time_struct->tm_year += 1900;
return (MAKE_DATE (time_struct->tm_year / 100,
time_struct->tm_year % 100,
time_struct->tm_mon + 1, time_struct->tm_mday));
}
else
{
return (19700101);
}
}
}
/*----------------------------------------------------------------------------*/
@ -167,24 +174,29 @@ static long timer_to_date(time_t time_secs)
(GMT).
*/
/*----------------------------------------------------------------------------*/
static long timer_to_time(time_t time_secs)
static long
timer_to_time (time_t time_secs)
{
struct tm *time_struct;
struct tm *time_struct;
if (time_secs == 0) {
return 0;
} else {
/* Convert into a long value HHMMSS00 */
time_struct = localtime (&time_secs);
if (time_struct) {
return (MAKE_TIME (time_struct-> tm_hour,
time_struct-> tm_min,
time_struct-> tm_sec,
0));
} else {
return 0;
}
if (time_secs == 0)
{
return 0;
}
else
{
/* Convert into a long value HHMMSS00 */
time_struct = localtime (&time_secs);
if (time_struct)
{
return (MAKE_TIME (time_struct->tm_hour,
time_struct->tm_min, time_struct->tm_sec, 0));
}
else
{
return 0;
}
}
}
/*-----------------------------------------------------------------------------
@ -201,17 +213,16 @@ static long timer_to_time(time_t time_secs)
in the function, so no need to free it.
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_date_iso8601(void)
char *
qfits_get_date_iso8601 (void)
{
static char date_iso8601[20] ;
long curdate ;
static char date_iso8601[20];
long curdate;
curdate = qfits_date_now() ;
sprintf(date_iso8601, "%04d-%02d-%02d",
GET_CCYEAR(curdate),
GET_MONTH(curdate),
GET_DAY(curdate));
return date_iso8601 ;
curdate = qfits_date_now ();
sprintf (date_iso8601, "%04d-%02d-%02d",
GET_CCYEAR (curdate), GET_MONTH (curdate), GET_DAY (curdate));
return date_iso8601;
}
/*----------------------------------------------------------------------------*/
@ -224,38 +235,37 @@ char * qfits_get_date_iso8601(void)
statically allocated string in the function, so no need to free it.
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_datetime_iso8601(void)
char *
qfits_get_datetime_iso8601 (void)
{
static char date_iso8601[20] ;
long curdate ;
long curtime ;
static char date_iso8601[20];
long curdate;
long curtime;
curdate = qfits_date_now() ;
curtime = qfits_time_now() ;
curdate = qfits_date_now ();
curtime = qfits_time_now ();
sprintf(date_iso8601, "%04d-%02d-%02dT%02d:%02d:%02d",
GET_CCYEAR(curdate),
GET_MONTH(curdate),
GET_DAY(curdate),
GET_HOUR(curtime),
GET_MINUTE(curtime),
GET_SECOND(curtime));
return date_iso8601 ;
sprintf (date_iso8601, "%04d-%02d-%02dT%02d:%02d:%02d",
GET_CCYEAR (curdate),
GET_MONTH (curdate),
GET_DAY (curdate),
GET_HOUR (curtime), GET_MINUTE (curtime), GET_SECOND (curtime));
return date_iso8601;
}
#ifdef TEST
int main(int argc, char *argv[])
int
main (int argc, char *argv[])
{
printf( "date now %ld\n"
"time now %ld\n"
"date iso8601 %s\n"
"date/time iso 8601 %s\n",
qfits_date_now(),
qfits_time_now(),
qfits_get_date_iso8601(),
qfits_get_datetime_iso8601());
return 0 ;
printf ("date now %ld\n"
"time now %ld\n"
"date iso8601 %s\n"
"date/time iso 8601 %s\n",
qfits_date_now (),
qfits_time_now (),
qfits_get_date_iso8601 (), qfits_get_datetime_iso8601 ());
return 0;
}
#endif
/* vim: set ts=4 et sw=4 tw=75 */

View File

@ -23,7 +23,8 @@
#define T_ISO8601_H
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
@ -46,7 +47,7 @@ extern "C" {
Example: 19 Oct 2000 is returned as 20001019
*/
/*----------------------------------------------------------------------------*/
long qfits_date_now (void);
long qfits_date_now (void);
/*----------------------------------------------------------------------------*/
/**
@ -58,7 +59,7 @@ long qfits_date_now (void);
Example: 15:36:12.84 is returned as 15361284
*/
/*----------------------------------------------------------------------------*/
long qfits_time_now(void);
long qfits_time_now (void);
/*----------------------------------------------------------------------------*/
/**
@ -70,7 +71,7 @@ long qfits_time_now(void);
in the function, so no need to free it.
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_date_iso8601(void);
char *qfits_get_date_iso8601 (void);
/*----------------------------------------------------------------------------*/
/**
@ -82,7 +83,7 @@ char * qfits_get_date_iso8601(void);
statically allocated string in the function, so no need to free it.
*/
/*----------------------------------------------------------------------------*/
char * qfits_get_datetime_iso8601(void);
char *qfits_get_datetime_iso8601 (void);
/* </dox> */
#ifdef __cplusplus

File diff suppressed because it is too large Load Diff

View File

@ -19,7 +19,8 @@
#define TFITS_H
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
/*-----------------------------------------------------------------------------
@ -30,7 +31,7 @@ extern "C" {
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "fits_h.h"
#include "static_sz.h"
@ -53,27 +54,28 @@ extern "C" {
/*----------------------------------------------------------------------------*/
/**
@brief Column data type
*/
*/
/*----------------------------------------------------------------------------*/
typedef enum _TFITS_DATA_TYPE_ {
TFITS_ASCII_TYPE_A,
TFITS_ASCII_TYPE_D,
TFITS_ASCII_TYPE_E,
TFITS_ASCII_TYPE_F,
TFITS_ASCII_TYPE_I,
TFITS_BIN_TYPE_A,
TFITS_BIN_TYPE_B,
TFITS_BIN_TYPE_C,
TFITS_BIN_TYPE_D,
TFITS_BIN_TYPE_E,
TFITS_BIN_TYPE_I,
TFITS_BIN_TYPE_J,
TFITS_BIN_TYPE_L,
TFITS_BIN_TYPE_M,
TFITS_BIN_TYPE_P,
TFITS_BIN_TYPE_X,
TFITS_BIN_TYPE_UNKNOWN
} tfits_type ;
typedef enum _TFITS_DATA_TYPE_
{
TFITS_ASCII_TYPE_A,
TFITS_ASCII_TYPE_D,
TFITS_ASCII_TYPE_E,
TFITS_ASCII_TYPE_F,
TFITS_ASCII_TYPE_I,
TFITS_BIN_TYPE_A,
TFITS_BIN_TYPE_B,
TFITS_BIN_TYPE_C,
TFITS_BIN_TYPE_D,
TFITS_BIN_TYPE_E,
TFITS_BIN_TYPE_I,
TFITS_BIN_TYPE_J,
TFITS_BIN_TYPE_L,
TFITS_BIN_TYPE_M,
TFITS_BIN_TYPE_P,
TFITS_BIN_TYPE_X,
TFITS_BIN_TYPE_UNKNOWN
} tfits_type;
/*----------------------------------------------------------------------------*/
/**
@ -87,8 +89,8 @@ typedef enum _TFITS_DATA_TYPE_ {
generate a FITS table.
*/
/*----------------------------------------------------------------------------*/
typedef struct qfits_col
{
typedef struct qfits_col
{
/**
Number of atoms in one field.
In ASCII tables, it is the number of characters in the field as defined
@ -97,13 +99,13 @@ typedef struct qfits_col
it is the number of characters. A field with two complex object will
have atom_nb = 4.
*/
int atom_nb ;
int atom_nb;
/**
Number of decimals in a ASCII field.
This value is always 0 for BIN tables
*/
int atom_dec_nb ;
int atom_dec_nb;
/**
Size of one element in bytes. In ASCII tables, atom_size is the size
@ -124,44 +126,44 @@ typedef struct qfits_col
conversion of the field is atom_size.
In BIN tables, the size in bytes of a field is always atom_nb*atom_size.
*/
int atom_size ;
int atom_size;
/**
Type of data in the column as specified in TFORM keyword
In ASCII tables : TFITS_ASCII_TYPE_* with *=A, I, F, E or D
In BIN tables : TFITS_BIN_TYPE_* with *=L, X, B, I, J, A, E, D, C, M or P
*/
tfits_type atom_type ;
tfits_type atom_type;
/** Label of the column */
char tlabel[FITSVALSZ] ;
char tlabel[FITSVALSZ];
/** Unit of the data */
char tunit[FITSVALSZ] ;
char tunit[FITSVALSZ];
/** Null value */
char nullval[FITSVALSZ] ;
char nullval[FITSVALSZ];
/** Display format */
char tdisp[FITSVALSZ] ;
char tdisp[FITSVALSZ];
/**
zero and scale are used when the quantity in the field does not
represent a true physical quantity. Basically, thez should be used
when they are present: physical_value = zero + scale * field_value
They are read from TZERO and TSCAL in the header
*/
int zero_present ;
float zero ;
int scale_present ;
float scale ;
int zero_present;
float zero;
int scale_present;
float scale;
/** Offset between the beg. of the table and the beg. of the column. */
int off_beg ;
int off_beg;
/** Flag to know if the column is readable. An empty col is not readable */
int readable ;
} qfits_col ;
int readable;
} qfits_col;
/*----------------------------------------------------------------------------*/
@ -195,26 +197,26 @@ typedef struct qfits_col
@endcode
*/
/*----------------------------------------------------------------------------*/
typedef struct qfits_table
{
typedef struct qfits_table
{
/**
Name of the file the table comes from or it is intended to end to
*/
char filename[FILENAMESZ] ;
char filename[FILENAMESZ];
/**
Table type.
Possible values: QFITS_INVALIDTABLE, QFITS_BINTABLE, QFITS_ASCIITABLE
*/
int tab_t ;
int tab_t;
/** Width in bytes of the table */
int tab_w ;
int tab_w;
/** Number of columns */
int nc ;
int nc;
/** Number of raws */
int nr ;
int nr;
/** Array of qfits_col objects */
qfits_col * col ;
} qfits_table ;
qfits_col *col;
} qfits_table;
/*-----------------------------------------------------------------------------
Function prototypes
@ -231,7 +233,7 @@ typedef struct qfits_table
FITS table.
*/
/*----------------------------------------------------------------------------*/
int qfits_is_table(char * filename, int xtnum) ;
int qfits_is_table (char *filename, int xtnum);
/*----------------------------------------------------------------------------*/
/**
@ -239,7 +241,7 @@ int qfits_is_table(char * filename, int xtnum) ;
@return the header object
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_table_prim_header_default(void) ;
qfits_header *qfits_table_prim_header_default (void);
/*----------------------------------------------------------------------------*/
/**
@ -247,7 +249,7 @@ qfits_header * qfits_table_prim_header_default(void) ;
@return the header object
*/
/*----------------------------------------------------------------------------*/
qfits_header * qfits_table_ext_header_default(qfits_table *) ;
qfits_header *qfits_table_ext_header_default (qfits_table *);
/*----------------------------------------------------------------------------*/
/**
@ -262,12 +264,9 @@ qfits_header * qfits_table_ext_header_default(qfits_table *) ;
qfits_table_close()
*/
/*----------------------------------------------------------------------------*/
qfits_table * qfits_table_new(
char * filename,
int table_type,
int table_width,
int nb_cols,
int nb_raws) ;
qfits_table *qfits_table_new (char *filename,
int table_type,
int table_width, int nb_cols, int nb_raws);
/*----------------------------------------------------------------------------*/
/**
@ -293,21 +292,18 @@ qfits_table * qfits_table_new(
@return -1 in error case, 0 otherwise
*/
/*----------------------------------------------------------------------------*/
int qfits_col_fill(
qfits_col * qc,
int atom_nb,
int atom_dec_nb,
int atom_size,
tfits_type atom_type,
char * label,
char * unit,
char * nullval,
char * disp,
int zero_present,
float zero,
int scale_present,
float scale,
int offset_beg) ;
int qfits_col_fill (qfits_col * qc,
int atom_nb,
int atom_dec_nb,
int atom_size,
tfits_type atom_type,
char *label,
char *unit,
char *nullval,
char *disp,
int zero_present,
float zero,
int scale_present, float scale, int offset_beg);
/*----------------------------------------------------------------------------*/
/**
@ -320,9 +316,7 @@ int qfits_col_fill(
newly allocated qfits_table structure.
*/
/*----------------------------------------------------------------------------*/
qfits_table * qfits_table_open(
char * filename,
int xtnum) ;
qfits_table *qfits_table_open (char *filename, int xtnum);
/*----------------------------------------------------------------------------*/
/**
@ -332,7 +326,7 @@ qfits_table * qfits_table_open(
Frees all memory associated to a qfits_table structure.
*/
/*----------------------------------------------------------------------------*/
void qfits_table_close(qfits_table * t) ;
void qfits_table_close (qfits_table * t);
/*----------------------------------------------------------------------------*/
/**
@ -363,10 +357,8 @@ void qfits_table_close(qfits_table * t) ;
the pixel loader here.
*/
/*----------------------------------------------------------------------------*/
unsigned char * qfits_query_column(
qfits_table * th,
int colnum,
int * selection) ;
unsigned char *qfits_query_column (qfits_table * th,
int colnum, int *selection);
/*----------------------------------------------------------------------------*/
/**
@ -380,11 +372,9 @@ unsigned char * qfits_query_column(
Spares the overhead of the selection object allocation
*/
/*----------------------------------------------------------------------------*/
unsigned char * qfits_query_column_seq(
qfits_table * th,
int colnum,
int start_ind,
int nb_rows) ;
unsigned char *qfits_query_column_seq (qfits_table * th,
int colnum,
int start_ind, int nb_rows);
/*----------------------------------------------------------------------------*/
/**
@ -393,7 +383,7 @@ unsigned char * qfits_query_column_seq(
@return the width (-1 in error case)
*/
/*----------------------------------------------------------------------------*/
int qfits_compute_table_width(qfits_table * th) ;
int qfits_compute_table_width (qfits_table * th);
/*----------------------------------------------------------------------------*/
/**
@ -420,11 +410,9 @@ int qfits_compute_table_width(qfits_table * th) ;
the pixel loader here.
*/
/*----------------------------------------------------------------------------*/
void * qfits_query_column_data(
qfits_table * th,
int colnum,
int * selection,
void * null_value) ;
void *qfits_query_column_data (qfits_table * th,
int colnum,
int *selection, void *null_value);
/*----------------------------------------------------------------------------*/
/**
@ -439,12 +427,10 @@ void * qfits_query_column_data(
of rows. Spares the overhead of the selection object allocation
*/
/*----------------------------------------------------------------------------*/
void * qfits_query_column_seq_data(
qfits_table * th,
int colnum,
int start_ind,
int nb_rows,
void * null_value) ;
void *qfits_query_column_seq_data (qfits_table * th,
int colnum,
int start_ind,
int nb_rows, void *null_value);
/*----------------------------------------------------------------------------*/
/**
@ -457,12 +443,9 @@ void * qfits_query_column_seq_data(
@return array with 1 for NULLs and 0 for non-NULLs
*/
/*----------------------------------------------------------------------------*/
int * qfits_query_column_nulls(
qfits_table * th,
int colnum,
int * selection,
int * nb_vals,
int * nb_nulls) ;
int *qfits_query_column_nulls (qfits_table * th,
int colnum,
int *selection, int *nb_vals, int *nb_nulls);
/*----------------------------------------------------------------------------*/
/**
@ -473,10 +456,8 @@ int * qfits_query_column_nulls(
@return -1 in error case, 0 otherwise
*/
/*----------------------------------------------------------------------------*/
int qfits_save_table_hdrdump(
void ** array,
qfits_table * table,
qfits_header * fh) ;
int qfits_save_table_hdrdump (void **array,
qfits_table * table, qfits_header * fh);
/*----------------------------------------------------------------------------*/
/**
@ -494,10 +475,8 @@ int qfits_save_table_hdrdump(
Notice that no main header is produced, only the extension part.
*/
/*----------------------------------------------------------------------------*/
int qfits_table_append_xtension(
FILE * outfile,
qfits_table * t,
void ** data) ;
int qfits_table_append_xtension (FILE * outfile,
qfits_table * t, void **data);
/*----------------------------------------------------------------------------*/
/**
@ -515,11 +494,9 @@ int qfits_table_append_xtension(
Notice that no main header is produced, only the extension part.
*/
/*----------------------------------------------------------------------------*/
int qfits_table_append_xtension_hdr(
FILE * outfile,
qfits_table * t,
void ** data,
qfits_header * hdr) ;
int qfits_table_append_xtension_hdr (FILE * outfile,
qfits_table * t,
void **data, qfits_header * hdr);
/*----------------------------------------------------------------------------*/
/**
@ -531,11 +508,9 @@ int qfits_table_append_xtension_hdr(
@return the string to write
*/
/*----------------------------------------------------------------------------*/
char * qfits_table_field_to_string(
qfits_table * table,
int col_id,
int row_id,
int use_zero_scale) ;
char *qfits_table_field_to_string (qfits_table * table,
int col_id,
int row_id, int use_zero_scale);
/* </dox> */
#ifdef __cplusplus

View File

@ -1,9 +1,9 @@
/* Library version */
static const char *
_qfits_version = "5.4.0" ;
static const char *_qfits_version = "5.4.0";
const char * qfits_version(void)
const char *
qfits_version (void)
{
return _qfits_version ;
return _qfits_version;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff