/* initialize check-image. */ checkstruct *initcheck(char *filename, checkenum check_type, int next) { catstruct *fitscat; checkstruct *check; QCALLOC(check, checkstruct, 1); strcpy(check->filename, filename); check->type = check_type; if (next>1) /*-- Create a "pure" primary HDU */ { fitscat = new_cat(1); init_cat(fitscat); strcpy(fitscat->filename, filename); fitsadd(fitscat->tab->headbuf, "NEXTEND ", "Number of extensions"); fitswrite(fitscat->tab->headbuf, "NEXTEND ", &next, H_INT, T_LONG); if (open_cat(fitscat, WRITE_ONLY) != RETURN_OK) error(EXIT_FAILURE,"*Error*: cannot open for writing ", filename); save_tab(fitscat, fitscat->tab); check->file = fitscat->file; fitscat->file = NULL; free_cat(&fitscat, 1); } else if (!(check->file = fopen(check->filename, "wb"))) error(EXIT_FAILURE, "*Error*: Cannot open for output ", check->filename); return check; }
/****** save_cat ************************************************************** PROTO void save_cat(catstruct *cat, char *filename) PURPOSE Save a FITS catalog with name filename. INPUT catalog structure, filename. OUTPUT -. NOTES Any preexisting file with name filename is overwritten. AUTHOR E. Bertin (IAP) VERSION 09/09/2003 ***/ void save_cat(catstruct *cat, char *filename) { tabstruct *tab; int i; strcpy(cat->filename, filename); if (open_cat(cat, WRITE_ONLY) != RETURN_OK) error(EXIT_FAILURE, "*Error*: cannot open for writing ", filename); tab = cat->tab; /*Go through each segment in the right order to save data*/ for (i=0; i<cat->ntab; i++) { /*-- Make sure that the tab header is primary or extension! */ if (i) ext_head(tab); else prim_head(tab); save_tab(cat, tab); while (!((tab=tab->nexttab)->nseg)); } if (close_cat(cat) != RETURN_OK) error(EXIT_FAILURE, "*Error*: Problem while closing", cat->filename); return; }
/****** read_cats ************************************************************** PROTO read_cats(char **filenames, int ncat) PURPOSE ``Read'' several FITS catalogs. INPUT A pointer to pointers of char, The number of catalogs. OUTPUT catstruct pointer. NOTES -. AUTHOR E. Bertin (IAP & Leiden observatory) VERSION 25/04/97 ***/ catstruct *read_cats(char **filenames, int ncat) { catstruct *cat, *ccat; int i; if (!(cat = new_cat(ncat))) error (EXIT_FAILURE, "Not enough memory to read ", "catalogs"); for (i=ncat, ccat = cat; i--; ccat++, filenames++) { strcpy(ccat->filename, *filenames); if (open_cat(ccat, READ_ONLY) != RETURN_OK) error (EXIT_FAILURE, "Cannot open ", *filenames); if (map_cat(ccat) != RETURN_OK) error (EXIT_FAILURE, "Cannot map ", *filenames); close_cat(ccat); } return cat; }
/****** read_cat *************************************************************** PROTO catstruct read_cat(char *filename) PURPOSE ``Read'' a FITS catalog with name filename. INPUT Filename, OUTPUT catstruct pointer. NOTES Returns NULL if no file with name \<filename\> is found. AUTHOR E. Bertin (IAP & Leiden observatory) VERSION 07/05/2002 ***/ catstruct *read_cat(char *filename) { catstruct *cat; if (!(cat = new_cat(1))) error (EXIT_FAILURE, "Not enough memory to read ", filename); strcpy(cat->filename, filename); if (open_cat(cat, READ_ONLY) != RETURN_OK) { free_cat(&cat, 1); return NULL; } if (map_cat(cat) != RETURN_OK) { free_cat(&cat, 1); return NULL; } return cat; }
/****** check_write ******************************************************** PROTO void check_write(fieldstruct *field, char *checkname, checkenum checktype, int ext, int next, int cubeflag) PURPOSE Write a FITS image for check. INPUT Pointer to the field, Pointer to the set, Check-image filename, Check-image type, Extension number, Number of extensions, Datacube flag. OUTPUT -. NOTES Check-image is written as a datacube if cubeflag!=0. AUTHOR E. Bertin (IAP) VERSION 21/09/2015 ***/ void check_write(fieldstruct *field, setstruct *set, char *checkname, checkenum checktype, int ext, int next, int cubeflag) { psfstruct *psf; catstruct *cat; tabstruct *tab; samplestruct **gridsample, *sample,*osample; char filename[MAXCHAR], str[82], *head, *pstr,*pstr2; static double dpos[POLY_MAXDIM], *dpost; double dstep,dstart, dval1,dval2, scalefac, dstepx,dstepy; float *pix,*pix0, *vig,*vig0, *fpix,*fpixsym, val; int i,g,j,l,x,y, w,h,n, npc,nt, nw,nh,np, npos,npos2, step,step2, ipos, inpos, ival1,ival2, npix; /* Create the new cat (well it is not a "cat", but simply a FITS table */ if (!ext) { cat = new_cat(1); init_cat(cat); strcpy(cat->filename, checkname); if (!(pstr = strrchr(cat->filename, '.'))) pstr = cat->filename+strlen(cat->filename); strcpy(filename, field->rcatname); if (!(pstr2 = strrchr(filename, '.'))) pstr2 = filename+strlen(filename); *pstr2 = '\0'; sprintf(pstr, "_%s.fits", filename); if (open_cat(cat, WRITE_ONLY) != RETURN_OK) error(EXIT_FAILURE, "*Error*: cannot open for writing ", cat->filename); if (next>1) { addkeywordto_head(cat->tab, "NEXTEND ", "Number of extensions"); fitswrite(cat->tab->headbuf, "NEXTEND", &next, H_INT, T_LONG); save_tab(cat, cat->tab); } field->ccat[checktype] = cat; } else cat = field->ccat[checktype]; sprintf(str, "chip%02d", ext+1); psf = field->psf[ext]; tab = new_tab(str); head = tab->headbuf; tab->bitpix = BP_FLOAT; tab->bytepix = t_size[T_FLOAT]; switch(checktype) { case PSF_BASIS: /*---- View basis vectors as small vignets */ if (cubeflag) { tab->naxis = 3; QREALLOC(tab->naxisn, int, tab->naxis); tab->naxisn[0] = psf->size[0]; tab->naxisn[1] = psf->size[1]; tab->naxisn[2] = psf->nbasis; npix = tab->naxisn[0]*tab->naxisn[1]*tab->naxisn[2]; tab->tabsize = tab->bytepix*npix; QCALLOC(pix0, float, tab->tabsize); tab->bodybuf = (char *)pix0; pix = pix0; fpix = psf->basis; for (i=npix; i--;) *(pix++) = *(fpix++); } else { nw = (int)sqrt((double)psf->nbasis); nw = ((nw-1)/10+1)*10; nh = (psf->nbasis-1)/nw + 1; w = psf->size[0]; h = psf->dim>1? psf->size[1] : 1; tab->naxisn[0] = nw*w; tab->naxisn[1] = nh*h; step = (nw-1)*w; tab->tabsize = tab->bytepix*tab->naxisn[0]*tab->naxisn[1]; QCALLOC(pix0, float, tab->tabsize); tab->bodybuf = (char *)pix0; fpix = psf->basis; for (n=0; n<psf->nbasis; n++) { pix = pix0 + ((n%nw) + (n/nw)*nw*h)*w; for (y=h; y--; pix += step) for (x=w; x--;) *(pix++) = *(fpix++); } } break; case PSF_CHI: /*---- sqrt(chi2) map in PSF pixel-space nw = 1; nh = 1; w = psf->size[0]; h = psf->size[1]; tab->naxisn[0] = nw*w; tab->naxisn[1] = nh*h; tab->tabsize = tab->bytepix*tab->naxisn[0]*tab->naxisn[1]; QCALLOC(pix0, float, tab->tabsize); tab->bodybuf = (char *)pix0; pix = pix0; fpix = psf->resi; for (i=w*h; i--;) *(pix++) = *(fpix++); break; */ if (cubeflag) { tab->naxis = 3; QREALLOC(tab->naxisn, int, tab->naxis); tab->naxisn[0] = set->vigsize[0]; tab->naxisn[1] = set->vigsize[1]; tab->naxisn[2] = set->ngood? set->ngood : 1; npix = tab->naxisn[0]*tab->naxisn[1]; tab->tabsize = tab->bytepix*npix*tab->naxisn[2]; QCALLOC(pix0, float, tab->tabsize); tab->bodybuf = (char *)pix0; pix = pix0; sample = set->sample; for (n=set->nsample; n--; sample++) { if (sample->badflag) continue; fpix = sample->vigchi; for (i=npix; i--;) *(pix++) = *(fpix++); } } else {
/****** save_tab ************************************************************** PROTO void save_tab(catstruct *cat, tabstruct *tab) PURPOSE Save a FITS table. INPUT pointer to the catalog structure, pointer to the table structure. OUTPUT -. NOTES -. AUTHOR E. Bertin (IAP & Leiden observatory) VERSION 09/09/2003 ***/ void save_tab(catstruct *cat, tabstruct *tab) { catstruct *tabcat; keystruct *key; tabstruct *keytab; KINGSIZE_T tabsize; KINGLONG size; int b,j,k,o, nbytes,nkey,nobj,spoonful, tabflag, larrayin,larrayout; char *buf, *inbuf, *outbuf, *fptr,*ptr; int esize; /* Make the table parameters reflect its content*/ update_tab(tab); /* The header itself*/ tabflag = save_head(cat, tab)==RETURN_OK?1:0; /* Allocate memory for the output buffer */ tabsize = 0; tabcat = NULL; /* to satisfy gcc -Wall */ inbuf = NULL; /* to satisfy gcc -Wall */ if (tabflag) { /*-- If segment is a binary table, save it row by row */ QMALLOC(outbuf, char, (larrayout = tab->naxisn[0])); nkey = tab->nkey; tabsize = larrayin = 0; for (j=tab->nseg; j--;) { update_tab(tab); /*---- Scan keys to find the reference tab and other things*/ keytab = NULL; key = tab->key; for (k=nkey; k--; key = key->nextkey) if (!key->ptr) { keytab = key->tab; tabcat = keytab->cat; } /*---- If table contains some keys with no ptrs, we have to access a file */ if (keytab) { QMALLOC(inbuf, char, (larrayin = keytab->naxisn[0])); if (open_cat(tabcat, READ_ONLY) != RETURN_OK) error(EXIT_FAILURE, "*Error*: Cannot access ", tabcat->filename); QFSEEK(tabcat->file, keytab->bodypos, SEEK_SET, tabcat->filename); } nobj = tab->naxisn[1]; for (o=0; o<nobj; o++) { if (keytab) QFREAD(inbuf, larrayin, tabcat->file, tabcat->filename); fptr = outbuf; for (k=nkey; k--; key = key->nextkey) { nbytes = key->nbytes; ptr = key->ptr? (char *)key->ptr+nbytes*o:inbuf+key->pos; for (b=nbytes; b--;) *(fptr++) = *(ptr++); if (bswapflag) if (key->ptr) { esize = t_size[key->ttype]; swapbytes(fptr-nbytes, esize, nbytes/esize); } } QFWRITE(outbuf, larrayout, cat->file, cat->filename); } if (keytab) { free(inbuf); if (close_cat(tabcat) != RETURN_OK) error(EXIT_FAILURE, "*Error*: Problem while closing", tabcat->filename); } tabsize += tab->tabsize; tab = tab->nexttab; } free(outbuf); } else {