/** Sanity checking on key string. If key non-existent or whitespace * then use the abbreviated form as the key. * @param key_ptr The key, which may be modified in this function if * necessary. * @param abbrev_short The abbreviated form. */ void key_sanity_check(char **key_ptr, const char *abbrev_short) { if(*key_ptr == NULL) { *key_ptr = malloc(sizeof(char) * (strlen(abbrev_short) + 1)); if(*key_ptr == NULL) errmess(ERRMESS_NO_MEM); strcpy(*key_ptr, abbrev_short); /* strdup not POSIX key = strdup(abbrev_short); */ } else { int i; int blank = 1; for(i = 0; i < strlen(*key_ptr); ++i) if(!isspace(*key_ptr[i])) { blank = 0; break; } if(blank) { free(*key_ptr); *key_ptr = malloc(sizeof(char) * (strlen(abbrev_short) + 1)); if(*key_ptr == NULL) errmess(ERRMESS_NO_MEM); strcpy(*key_ptr, abbrev_short); } } return; }
long get_headlen(char *inpfname) { char head[RECORD_SIZE], eoh; int i; long ofs; FILE *inpf; if (!(inpf=fopen(inpfname, "r"))) errmess(inpfname); eoh=0; ofs=0L; while (!eoh) { if (fread(head, RECORD_SIZE, 1, inpf) != 1 ) errmess("get_headlen: reading header"); for (i=0; i<RECORD_SIZE; i+=CARD_SIZE) if (strncmp(head+i, "END ", 8) == 0) eoh=1; ofs+=RECORD_SIZE; } fclose(inpf); return(ofs); }
/*--------------------------------------------------------*/ int readfnames(char *iname, char ***diffiles, char ***imfiles, char ***kerfiles) { char **dif, **imf, **kef, line[501], s1[201], s2[201], s3[201]; int i; FILE *inf; if (!(dif=(char **)malloc(sizeof(char *)))) errmess("malloc(dif)"); if (!(imf=(char **)malloc(sizeof(char *)))) errmess("malloc(imf)"); if (!(kef=(char **)malloc(sizeof(char *)))) errmess("malloc(kef)"); if (!(inf = fopen(iname, "r"))) errmess(iname); for (i=0; !feof(inf); i++) { fgets(line, 500, inf); if (feof(inf)) break; sscanf(line, "%s %s %s", s1, s2, s3); if (!(dif=(char **)realloc(dif, (i+1)*sizeof(char *)))) errmess("realloc(dif)"); if (!(dif[i]=(char *)calloc(strlen(s1)+1, sizeof(char)))) errmess("calloc(dif[i])"); if (!(imf=(char **)realloc(imf, (i+1)*sizeof(char *)))) errmess("realloc(imf)"); if (!(imf[i]=(char *)calloc(strlen(s2)+1, sizeof(char)))) errmess("calloc(imf[i])"); if (!(kef=(char **)realloc(kef, (i+1)*sizeof(char *)))) errmess("realloc(kef)"); if (!(kef[i]=(char *)calloc(strlen(s3)+1, sizeof(char)))) errmess("calloc(kef[i])"); strcpy(dif[i], s1); strcpy(imf[i], s2); strcpy(kef[i], s3); } fclose(inf); *diffiles=dif; *imfiles=imf; *kerfiles=kef; return(i); }
/*--------------------------------------------------------*/ int read_inp_list(char *listname, char ***difnames, char ***imnames, char ***kernames) { char **ldifnames, **limnames, **lkernames, line[501], s1[201], s2[201], s3[201]; int i; FILE *inf; if (!(ldifnames=(char **)calloc(1, sizeof(char *)))) errmess("calloc(ldifnames)"); if (!(limnames=(char **)calloc(1, sizeof(char *)))) errmess("calloc(limnames)"); if (!(lkernames=(char **)calloc(1, sizeof(char *)))) errmess("calloc(lkernames)"); if (!(inf=fopen(listname, "r"))) errmess(listname); for (i=0; !feof(inf); i++) { fgets(line, 200, inf); if (feof(inf)) break; sscanf(line, "%s %s %s", s1, s2, s3); if (!(ldifnames=(char **)realloc(ldifnames, (i+1)*sizeof(char *)))) errmess("realloc(ldifnames)"); if (!(limnames=(char **)realloc(limnames, (i+1)*sizeof(char *)))) errmess("realloc(limnames)"); if (!(lkernames=(char **)realloc(lkernames, (i+1)*sizeof(char *)))) errmess("realloc(lkernames)"); if (!(ldifnames[i]=(char *)calloc(strlen(s1)+1, sizeof(char)))) errmess("calloc(ldifnames[i])"); if (!(limnames[i]=(char *)calloc(strlen(s2)+1, sizeof(char)))) errmess("calloc(limnames[i])"); if (!(lkernames[i]=(char *)calloc(strlen(s3)+1, sizeof(char)))) errmess("calloc(lkernames[i])"); strcpy(ldifnames[i], s1); strcpy(limnames[i], s2); strcpy(lkernames[i], s3); } fclose(inf); *difnames=ldifnames; *imnames=limnames; *kernames=lkernames; return(i); }
/*--------------------------------------------------------*/ void writedba(char *oname, char **diffiles, int nim, int npsf, int *vnum, STAR **obj) { int i, j; FILE *outf; STAR *objp; if (!(outf=fopen(oname, "w"))) errmess(oname); fprintf(outf, "image var_num num_bad_pix profile_flux flux_err"); fprintf(outf, " ap_flux flux_err bkg psf_chi^2(s) fwhm\n"); for (j=0; j<nim; j++) { for (i=0; i<npsf; i++) { objp = &obj[i][j]; fprintf(outf, "%s %5d %3d ", diffiles[j], vnum[i], objp->nbad); fprintf(outf, "%9g %8g ", objp->p_flux, objp->p_err); fprintf(outf, "%9g %8g ", objp->a_flux, objp->a_err); fprintf(outf, "%8g %8g %g ", objp->bg, objp->chi2_n, objp->corr); fprintf(outf, "%9g %8g\n", objp->ker_chi2, objp->fwhm); } } fclose(outf); return; }
/*--------------------------------------------------------*/ int read_FITS_image(FILE *inf, char **header, int hsize, void **buf) { void *ptr; /* data table pointer */ char flag[9], /* header entry flag */ hline[72]; int i, /* loop numerator */ naxes, /* image dimension */ *naxis, /* table of sizes of axes */ pixnum, /* number of image pixels */ bitpix, bytepix; if (get_FITS_key(hsize, header, "NAXIS", hline) == -1) return(0); sscanf(hline, "%d", &naxes); if (!(naxis=(int *)calloc(naxes, sizeof(int)))) errmess("pfitsio: read_FITS_image - calloc(naxis)"); for (i=0; i<naxes; i++) { sprintf(flag, "NAXIS%d", i+1); if (get_FITS_key(hsize, header, flag, hline) == -1) return(0); sscanf(hline, "%d", &naxis[i]); } pixnum=1; for (i=0; i<naxes; i++) pixnum*=naxis[i]; free(naxis); if (get_FITS_key(hsize, header, "BITPIX", hline) == -1) return(0); sscanf(hline, "%d", &bitpix); bytepix=abs(bitpix)/8; if (!(ptr=(void *)malloc(pixnum*bytepix))) errmess("pfitsio: read_FITS_image - malloc(ptr)"); if (fread(ptr, bytepix, pixnum, inf) != pixnum) errmess("pfitsio: read_FITS_image - reading data"); if (needswap()) swap_bytes(ptr, pixnum, bytepix); *buf=ptr; return(pixnum); }
/*--------------------------------------------------------*/ int read_FITS_header(FILE *inf, char ***header) { char **lheader, eoh; // end of header int i, hc, // number of header cards hr; // number of header records eoh=0; if (!(lheader=(char **)calloc(RECORD_CARDS, sizeof(char *)))) errmess("read_FITS_header(): calloc(lheader)"); hc=0; for (hr=1; !eoh; hr++) { if (!(lheader=(char **)realloc(lheader, hr*RECORD_CARDS*sizeof(char *)))) errmess("read_FITS_header(): realloc(lheader)"); for (i=0; i<RECORD_CARDS; i++) { if (!(lheader[hc+i]=(char *)calloc(CARD_SIZE, sizeof(char)))) errmess("read_FITS_header(): calloc(lheader[hc+i])"); if (fread(lheader[hc+i], sizeof(char), CARD_SIZE, inf) != CARD_SIZE) { printf("ERROR! read_FITS_header(): header corrupted\n"); return(0); } } for (i=0; i<RECORD_CARDS; i++) if (!strncmp(lheader[hc+i], "END ", KEYWORD_SIZE)) eoh=1; hc+=RECORD_CARDS; } *header=lheader; return(hc); }
void adjust(void) { // Adjust for baseline drift. cls(); cout << endl << "Adjust spectra" << endl << endl; if(!ynquery("NOTE: This routine will replace the first letter\n" \ " of the filename with ! (exlamation point)!\n" \ " Do you wish to continue? (y/n): ")) return; cout << endl; vector<string> fnames; if(!getfiles(&fnames, prmpt.c_str(), ".spc")) return; //if(!getinpvecnoerr(&fnames, prmpt.c_str())) return; float longw, shortw; do{ cout << endl << "Enter adjustment range:" << endl; if(!getinpnoerr(&longw, " Long wavelength: ")) return; if(!getinpnoerr(&shortw, " Short wavelength: ")) return; cout << endl; } while(longw <= shortw); for(size_t i=0; i<fnames.size(); i++) { ifstream ifile((fnames[i]+".spc").c_str()); //if(!ifileopen(fnames[i], ".spc", ifile)) return; string temp = fnames[i]; temp[0] = '!'; ofstream ofile; if(!ofileopen(temp, ".spc", ofile)) return; cout << "Adjusting " << fnames[i] << endl; spectrum spec = readspc(ifile); if (!(spec.inrange(longw) && spec.inrange(shortw))) { errmess("ERROR: The wavelength adjust limit is out-of-bounds!"); return; } long start = spec.which(longw); long stop = spec.which(shortw); float adj = 0.0; for(long j=start; j <= stop; j++) adj += spec[j]; adj /= stop - start + 1; spec.sub(adj); writespc(spec, ofile); } return; }
/*--------------------------------------------------------*/ int readcoor(char *inpfname, float **x, float **y, float **zx, float **zy) { char buf[256]; int i; float *lx, *ly, *lzx, *lzy; FILE *inpf; if (!(lx=(float *)calloc(1, sizeof(float)))) errmess("readcoor: calloc(lx)"); if (!(ly=(float *)calloc(1, sizeof(float)))) errmess("readcoor: calloc(ly)"); if (!(lzx=(float *)calloc(1, sizeof(float)))) errmess("readcoor: calloc(lzx)"); if (!(lzy=(float *)calloc(1, sizeof(float)))) errmess("readcoor: calloc(lzy)"); if (!(inpf=fopen(inpfname, "r"))) errmess(inpfname); for (i=0; !feof(inpf); i++) { fgets(buf, 256, inpf); if (feof(inpf)) break; if (!(lx=(float *)realloc(lx, (i+1)*sizeof(float)))) errmess("readcoor: realloc(lx)"); if (!(ly=(float *)realloc(ly, (i+1)*sizeof(float)))) errmess("readcoor: realloc(ly)"); if (!(lzx=(float *)realloc(lzx, (i+1)*sizeof(float)))) errmess("readcoor: realloc(lzx)"); if (!(lzy=(float *)realloc(lzy, (i+1)*sizeof(float)))) errmess("readcoor: realloc(lzy)"); sscanf(buf, "%f %f %f %f", &lx[i], &ly[i], &lzx[i], &lzy[i]); } fclose(inpf); *x=lx; *y=ly; *zx=lzx; *zy=lzy; return(i); }
/*--------------------------------------------------------*/ int reject_saturated(int nobj, float **x, float **y, short *sat) { int i, nn; float *nx, *ny, *lx, *ly; lx=*x; ly=*y; if (!(nx=(float *)calloc(1, sizeof(float)))) errmess("reject_saturated: calloc(nx)"); if (!(ny=(float *)calloc(1, sizeof(float)))) errmess("reject_saturated: calloc(ny)"); nn=0; for (i=0; i<nobj; i++) { if (!sat[i]) { if (!(nx=(float *)realloc(nx, (nn+1)*sizeof(float)))) errmess("reject_saturated: realloc(nx)"); if (!(ny=(float *)realloc(ny, (nn+1)*sizeof(float)))) errmess("reject_saturated: realloc(ny)"); nx[nn]=lx[i]; ny[nn]=ly[i]; nn++; } } free(lx); free(ly); *x=nx; *y=ny; return(nn); }
/*--------------------------------------------------------*/ int make_header(char ***header, int nx, int ny) { char **lheader, card[CARD_SIZE+1]; int i; if (!(lheader=(char **)calloc(6, sizeof(char *)))) errmess("calloc(lheader)"); for (i=0; i<6; i++) if (!(lheader[i]=(char *)calloc(CARD_SIZE, sizeof(char)))) errmess("calloc(lheader[i])"); strcpy(card, "SIMPLE = T / "); strcat(card, "FITS STANDARD "); memcpy(lheader[0], card, CARD_SIZE); strcpy(card, "BITPIX = -32 / "); strcat(card, "FITS BITS/PIXEL "); memcpy(lheader[1], card, CARD_SIZE); strcpy(card, "NAXIS = 2 / "); strcat(card, "NUMBER OF AXES "); memcpy(lheader[2], card, CARD_SIZE); sprintf(card, "NAXIS1 = %6d / ", nx); strcat(card, " "); memcpy(lheader[3], card, CARD_SIZE); sprintf(card, "NAXIS2 = %6d / ", ny); strcat(card, " "); memcpy(lheader[4], card, CARD_SIZE); strcpy(card, "END "); strcat(card, " "); memcpy(lheader[5], card, CARD_SIZE); *header=lheader; return(6); }
/*--------------------------------------------------------*/ void read_params(char *iname, PARAMS *par) { char line[201], key[200], val[200]; int i; FILE *inf; if (!(inf=fopen(iname, "r"))) errmess(iname); for (i=0; !feof(inf); i++) { fgets(line, 200, inf); if (feof(inf)) break; sscanf(line, "%s = %s", key, val); if (!strcasecmp(key, "END")) break; else if (!strcasecmp(key, "NSUB")) sscanf(val, "%d", &par->nsub); else if (!strcasecmp(key, "LLIM")) sscanf(val, "%f", &par->llim); else if (!strcasecmp(key, "RLIM")) sscanf(val, "%f", &par->rlim); else if (!strcasecmp(key, "FVNO")) sscanf(val, "%f", &par->fvno); else if (!strcasecmp(key, "LTOL")) sscanf(val, "%f", &par->ltol); else if (!strcasecmp(key, "RTOL")) sscanf(val, "%f", &par->rtol); else if (!strcasecmp(key, "CTOL")) sscanf(val, "%f", &par->ctol); else if (!strcasecmp(key, "PTOL")) sscanf(val, "%f", &par->ptol); else if (!strcasecmp(key, "VERBOSE")) sscanf(val, "%hd", &par->verbose); else printf("WARNING: Unknown parameter %s\n", key); } fclose(inf); if (par->verbose > 1) { printf("Reading %s:\n", iname); printf("-----------\n"); printf("NSUB = %d\n", par->nsub); printf("LLIM = %g\n", par->llim); printf("RLIM = %g\n", par->rlim); printf("FVNO = %g\n", par->fvno); printf("LTOL = %g\n", par->ltol); printf("RTOL = %g\n", par->rtol); printf("CTOL = %g\n", par->ctol); printf("PTOL = %g\n", par->ptol); printf("VERBOSE = %hd\n", par->verbose); printf("-----------\n"); } return; }
/*--------------------------------------------------------*/ int read_objects(char *iname, float **x, float **y, float **mag, short **sat) { char buf[256]; short *ls; int i; float *lx, *ly, *lm; FILE *inf; if (!(lx=(float *)calloc(1, sizeof(float)))) errmess("calloc(lx)"); if (!(ly=(float *)calloc(1, sizeof(float)))) errmess("calloc(ly)"); if (!(lm=(float *)calloc(1, sizeof(float)))) errmess("calloc(lm)"); if (!(ls=(short *)calloc(1, sizeof(short)))) errmess("calloc(ls)"); if (!(inf=fopen(iname, "r"))) errmess(iname); for (i=0; !feof(inf); i++) { fgets(buf, 256, inf); if (feof(inf)) break; if (!(lx=(float *)realloc(lx, (i+1)*sizeof(float)))) errmess("realloc(lx)"); if (!(ly=(float *)realloc(ly, (i+1)*sizeof(float)))) errmess("realloc(ly)"); if (!(lm=(float *)realloc(lm, (i+1)*sizeof(float)))) errmess("realloc(lm)"); if (!(ls=(short *)realloc(ls, (i+1)*sizeof(short)))) errmess("realloc(ls)"); sscanf(buf, "%f %f %f %*s %hd", &lx[i], &ly[i], &lm[i], &ls[i]); if (lm[i] < -999.99) i--; // this means wrong photometry from sfind } fclose(inf); *x=lx; *y=ly; *mag=lm; *sat=ls; return(i); }
/** Insert a node into the linked list. Insert (in increasing * alphabetical order) only if abbrev is unique. * @param abbrev_short The abbreviated form. * @param abbrev_full The abbreviation written out in full. * @param key The key to use for sorting the list. * @return none */ void insert_node(char *abbrev_short, char *abbrev_full, char *key) { int cmp; node *new_node; node *current_node = list_start; node *previous_node = NULL; /* create new node */ new_node = malloc(sizeof(node)); if(new_node == NULL) errmess(ERRMESS_NO_MEM); new_node->abbrevitem = make_abbrevitem(abbrev_short, abbrev_full); new_node->key = key; new_node->abbrev_short = abbrev_short; new_node->abbrev_full = abbrev_full; /* Loop through term until insertion point found */ while(current_node != NULL && (cmp = nodecasecmp(new_node, current_node)) >= 0) { if(!cmp) { /* already exists in list */ free_node(new_node); return; } /* go to next node */ previous_node = current_node; current_node = current_node->next; } /* insert new node */ new_node->next = current_node; if(previous_node == NULL) list_start = new_node; else previous_node->next = new_node; return; }
/*--------------------------------------------------------*/ void writedbb(char *oname, char **diffiles, int nim, int npsf, int *vnum, STAR **obj) { char record[10*sizeof(float)]; int i, j, rs, ofs; FILE *outf; STAR *objp; rs=sizeof(float); if (!(outf=fopen(oname, "w"))) errmess(oname); for(j=0; j<nim; j++) { for (i=0; i<npsf; i++) { objp = &obj[i][j]; ofs=0; memcpy(&record[ofs], &objp->p_flux, rs); ofs+=rs; memcpy(&record[ofs], &objp->p_err, rs); ofs+=rs; memcpy(&record[ofs], &objp->a_flux, rs); ofs+=rs; memcpy(&record[ofs], &objp->a_err, rs); ofs+=rs; memcpy(&record[ofs], &objp->bg, rs); ofs+=rs; memcpy(&record[ofs], &objp->chi2_n, rs); ofs+=rs; memcpy(&record[ofs], &objp->corr, rs); ofs+=rs; memcpy(&record[ofs], &objp->ker_chi2, rs); ofs+=rs; memcpy(&record[ofs], &objp->fwhm, rs); ofs+=rs; memcpy(&record[ofs], &objp->nbad, sizeof(int)); fwrite(record, 1, sizeof(record), outf); } } fclose(outf); return; }
/*--------------------------------------------------------*/ void readpar(char *parfname, PARAMS *par) { char line[201], key[200], val[200]; FILE *inpf; if (!(inpf = fopen(parfname, "r"))) errmess(parfname); while (!feof(inpf)) { fgets(line, 200, inpf); if (feof(inpf)) break; sscanf(line, "%s = %s", key, val); if (!strcasecmp(key, "END")) break; else if (!strcasecmp(key, "NDEG")) sscanf(val, "%d", &par->ndeg); else if (!strcasecmp(key, "MAX_NITER")) sscanf(val, "%d", &par->maxniter); else if (!strcasecmp(key, "SIGMA_F")) sscanf(val, "%f", &par->sigmaf); else if (!strcasecmp(key, "VERBOSE")) sscanf(val, "%hd", &par->verbose); else printf("WARNING: Unknown parameter %s\n", key); } fclose(inpf); if (par->verbose > 1) { printf("Reading %s\n", parfname); printf("----------\n"); printf("NDEG = %d\n", par->ndeg); printf("MAX_NITER = %d\n", par->maxniter); printf("SIGMA_F = %g\n", par->sigmaf); printf("VERBOSE = %d\n", par->verbose); printf("----------\n"); } return; }
/** Read a LaTeX parameter (ie surronded by {}). The return string is * created by malloc(), calling function to free() memory. * @param str line of LaTeX text. * @parameter_num number of parameter to find (starting at 1). * @return The parameter value, without braces. */ char* read_param(const char *str, int parameter_num) { int p_num = 0; int depth = 0; int p_len; char *p_str = NULL; const char *str_end = str + strlen(str) - 1; const char *cptr = str; const char *p_start = NULL; const char *p_end = NULL; while(p_end == NULL && cptr <= str_end) { switch(*cptr) { case '{': if((++depth) == 1) if((++p_num) == parameter_num) /* found start of a new parameter */ p_start = cptr + 1; break; case '}': if((--depth) == 0 && p_num == parameter_num) p_end = cptr; break; } cptr++; } if(p_start && p_end) { p_len = (p_end - p_start); p_str = malloc(sizeof(char) * (p_len+1)); if(p_str == NULL) errmess(ERRMESS_NO_MEM); if(p_len) strncpy(p_str, p_start, p_len); p_str[p_len] = '\0'; } return p_str; }
bool DbSelectionPanel::DoValidation() { //the data has not been transferred from the window to our members yet, so get them from the controls if (wxWindow::Validate()) { wxFileName wxfn(m_filepicker->GetPath()); //Did the user enter a valid file path if (!wxfn.FileExists()) { wxMessageBox( _("File or path not found."), _("Error"), wxOK | wxICON_EXCLAMATION, this); return false; } //Did he enter the same file that's currently open? if (wxfn.SameAs(wxFileName(towxstring(m_core->GetCurFile())))) { // It is the same damn file wxMessageBox(_("That file is already open."), _("Error"), wxOK | wxICON_WARNING, this); return false; } StringX combination = m_sc->GetCombination(); //Does the combination match? if (m_core->CheckPasskey(tostringx(wxfn.GetFullPath()), combination) != PWScore::SUCCESS) { wxString errmess(_("Incorrect passkey, not a PasswordSafe database, or a corrupt database. (Backup database has same name as original, ending with '~')")); wxMessageBox(errmess, _("Error"), wxOK | wxICON_ERROR, this); SelectCombinationText(); return false; } return true; } else { return false; } }
/*--------------------------------------------------------*/ int readcoo(char *iname, int **vnum, double **x, double **y) { char line[201]; int i, *lvnum; double *lx, *ly; FILE *inf; if (!(lvnum=(int *)malloc(sizeof(int)))) errmess("malloc(lvnum)"); if (!(lx=(double *)malloc(sizeof(double)))) errmess("malloc(lx)"); if (!(ly=(double *)malloc(sizeof(double)))) errmess("malloc(ly)"); if (!(inf = fopen(iname, "r"))) errmess(iname); for (i=0; !feof(inf); i++) { fgets(line, 200, inf); if (feof(inf)) break; if (!(lx=(double *)realloc(lx, (i+1)*sizeof(double)))) errmess("realloc(lx)"); if (!(ly=(double *)realloc(ly, (i+1)*sizeof(double)))) errmess("realloc(ly)"); if (!(lvnum=(int *)realloc(lvnum, (i+1)*sizeof(int)))) errmess("realloc(vnum)"); sscanf(line, "%d %lf %lf", &lvnum[i], &lx[i], &ly[i]); } fclose(inf); *vnum=lvnum; *x=lx; *y=ly; return(i); }
void fitpsf(OBJ_LIST *psf_objects, PSF_STRUCT *psf) { int i, j, k, l, m, n, psfhw, ipsf, cx, cy, igauss, icomp, ispat, npsf, ldeg, sdeg, ncomp, nspat, ngauss, size, cxpsf, cypsf, *indx, ntot; double xpsf, ypsf, a1, a2, dx, dy, *v, *wxy, bg, norm, weight, rad2, **mat, *vec, **mat0, *vec0, *psf_buf, f, rr, x1, y1, x2, y2, parity, sigma2_inc, sin_psf, cos_psf, ax_psf, ay_psf, pix, x_orig, y_orig; OBJECT *obj; sigma2_inc = psf->sigma_inc*psf->sigma_inc; obj = psf_objects->list; npsf = psf_objects->nobj; psfhw = psf->hw; psf_buf = psf->buf; sdeg = psf->ndeg_spat; ldeg = psf->ndeg_local; cos_psf = psf->cos; sin_psf = psf->sin; ax_psf = psf->ax; ay_psf = psf->ay; x_orig = psf->x_orig; y_orig = psf->y_orig; ngauss = psf->ngauss; ncomp = ngauss*(ldeg+1)*(ldeg+2)/2; nspat = (sdeg+1)*(sdeg+2)/2; ntot = ncomp*nspat; size = 2*psfhw+1; rad2 = psf->fitrad*psf->fitrad; /* get memory */ if (!(wxy = (double *)malloc(nspat*sizeof(double)))) errmess("malloc(wxy)"); if (!(v = (double *)malloc(ncomp*sizeof(double)))) errmess("malloc(v)"); if (!(vec0 = (double *)malloc(ncomp*sizeof(double)))) errmess("malloc(vec0)"); if (!(mat0 = (double **)malloc(ncomp*sizeof(double *)))) errmess("malloc(mat0)"); for (i=0; i<ncomp; i++) if (!(mat0[i] = (double *)malloc(ncomp*sizeof(double)))) errmess("malloc(mat0[i])"); if (!(vec = (double *)malloc(ntot*sizeof(double)))) errmess("malloc(vec)"); if (!(indx = (int *)malloc(ntot*sizeof(int)))) errmess("malloc(indx)"); if (!(mat = (double **)malloc(ntot*sizeof(double *)))) errmess("malloc(mat)"); for (i=0; i<ntot; i++) { if (!(mat[i] = (double *)malloc(ntot*sizeof(double)))) errmess("malloc(mat[i])"); mat[i]--; } mat--; for (i=0; i<ntot; i++) { vec[i] = 0.0; for(j=0; j<ntot; j++) mat[i+1][j+1] = 0.0; } /* main loop over all psf stars */ for (ipsf=0; ipsf<npsf; ipsf++) { if (obj->corr < 0.0) { ++obj; psf_buf += size*size; continue; } xpsf = obj->xfit; ypsf = obj->yfit; cxpsf = obj->x; cypsf = obj->y; dx = xpsf - (double)cxpsf; dy = ypsf - (double)cypsf; bg = obj->bg; norm = obj->norm; /********************************/ /*** Local Fit ***/ /********************************/ for (i=0; i<ncomp; i++) { vec0[i] = 0.0; for (j=0; j<ncomp; j++) mat0[i][j] = 0.0; } for (cx=-psfhw; cx<=psfhw; cx++) { for (cy=-psfhw; cy<=psfhw; cy++) { pix = psf_buf[cx+psfhw+size*(cy+psfhw)]; weight = pix/(norm*norm); pix -= bg; x1 = (double)cx - dx; y1 = (double)cy - dy; x2 = cos_psf*x1 - sin_psf*y1; y2 = sin_psf*x1 + cos_psf*y1; rr = ax_psf*x2*x2 + ay_psf*y2*y2; if (rr <= rad2) { icomp = 0; for (igauss=0; igauss<ngauss; igauss++) { f = exp(rr); a1 = 1.0; for (m=0; m<=ldeg; m++) { a2 = 1.0; for (n=0; n<=ldeg-m; n++) { v[icomp++] = f*a1*a2; a2 *= y1; } a1 *= x1; } rr *= sigma2_inc; } if (icomp != ncomp) fprintf(stderr, "fitpsf: warning: wrong number of components!\n"); /***********************/ for (i=0; i<ncomp; i++) { vec0[i] += v[i]*pix/norm/weight; for (j=0; j<=i; j++) mat0[i][j] += v[i]*v[j]/weight; } } } } for (i=0; i<ncomp; i++) for (j=0; j<i; j++) mat0[j][i] = mat0[i][j]; /********************************/ /*** Global Fit ***/ /********************************/ ispat = 0; a1 = 1.0; for (m=0; m<=sdeg; m++) { a2 = 1.0; for (n=0; n<=sdeg-m; n++) { wxy[ispat++] = a1*a2; a2 *= ypsf - y_orig; } a1 *= xpsf - x_orig; } if (ispat != nspat) fprintf(stderr, "fitpsf: warning: wrong number of spatial components!\n"); m = 0; for (i=0; i<nspat; i++) { for (j=0; j<ncomp; j++) { vec[m] += wxy[i]*vec0[j]; n = 0; for (k=0; k<nspat && n<=m; k++) { for (l=0; l<ncomp && n<=m; l++) { mat[m+1][n+1] += wxy[i]*wxy[k]*mat0[j][l]; n++; } } m++; } } psf_buf += size*size; ++obj; } for (i=1; i<=ntot; i++) for (j=1; j<i; j++) mat[j][i] = mat[i][j]; ludcmp(mat, ntot, indx-1, &parity); lubksb(mat, ntot, indx-1, vec-1); for(i=0; i<ntot; i++) psf->vec[i] = vec[i]; /* done with memory ! */ free(v); free(vec); free(vec0); free(wxy); free(indx); mat++; for (i=0; i<ntot; i++) free(++mat[i]); free(mat); for(i=0; i<ncomp; i++) free(mat0[i]); free(mat0); return; }
/*--------------------------------------------------------*/ int read_list(PARAMS par, char *iname, char ***inpfname, char ***outfname, char ***kerfname) { char **linpfname, **loutfname, **lkerfname, *kerptr, line[MAX_FNAME]; int i; FILE *inf; if (!(linpfname=(char **)calloc(1, sizeof(char *)))) errmess("calloc(linpfname)"); if (!(loutfname=(char **)calloc(1, sizeof(char *)))) errmess("calloc(loutfname)"); if (!(lkerfname=(char **)calloc(1, sizeof(char *)))) errmess("calloc(lkerfname)"); if (!(inf=fopen(iname, "r"))) errmess(iname); for (i=0; !feof(inf); i++) { fgets(line, MAX_FNAME, inf); if (feof(inf)) break; if (!(linpfname=(char **)realloc(linpfname, (i+1)*sizeof(char *)))) errmess("realloc(linpfname)"); if (!(loutfname=(char **)realloc(loutfname, (i+1)*sizeof(char *)))) errmess("realloc(loutfname)"); if (!(lkerfname=(char **)realloc(lkerfname, (i+1)*sizeof(char *)))) errmess("realloc(lkerfname)"); if (par.verbose > 5) printf("line: %s\n", line); sscanf(line, "%s", line); if (par.verbose > 5) printf("line: %s\n", line); if (!(linpfname[i]=(char *)calloc(strlen(line)+1, sizeof(char)))) errmess("calloc(linpfname[i])"); strcpy(linpfname[i], line); if (!(loutfname[i]=(char *)calloc(strlen(linpfname[i])+3, sizeof(char)))) errmess("calloc(loutfname[i])"); sprintf(loutfname[i], "s_%s", linpfname[i]); if (!(lkerfname[i]=(char *)calloc(strlen(linpfname[i])+1, sizeof(char)))) errmess("calloc(lkerfnamefname[i])"); strcpy(lkerfname[i], linpfname[i]); if (!(kerptr=strstr(lkerfname[i], par.fits_ext))) { printf("aga: strstr(lkerfname[i], par.fits_ext) failure\n"); printf("lkerfname[%d]: %s\n", i, lkerfname[i]); printf("par.fits_ext: %s\n", par.fits_ext); exit(6); } if (kerptr == lkerfname[i]) { printf("aga: '%s' not found in '%s'\n", par.fits_ext, lkerfname[i]); printf("lkerfname[%d]: %s\n", i, lkerfname[i]); printf("par.fits_ext: %s\n", par.fits_ext); exit(7); } sprintf(kerptr, "ker"); if (par.verbose > 4) { printf("linpfname[%d]= %s\n", i, linpfname[i]); printf("loutfname[%d]= %s\n", i, loutfname[i]); printf("lkerfname[%d]= %s\n", i, lkerfname[i]); } } fclose(inf); *inpfname=linpfname; *outfname=loutfname; *kerfname=lkerfname; return(i); }
/*--------------------------------------------------------*/ int main(int argc, char *argv[]) { char *parfname, *instrname, *maskname, *reffname, *imgfname, **inpfname, **outfname, **kerfname; unsigned short *mask0, *mask1, *flag0, *flag1; int *indx, nsec_x, nsec_y, isec_x, isec_y, x_off, y_off, mode, deg[MAX_NCOMP], i, k, nclip, npix, nim, ntab, nkeep; long int imglen, buflen, veclen, intlen, matlen, usblen, lomlen, lovlen, tablen, domlen, srtlen, *headlen, mheadlen, rheadlen; float *im, *imref, *imdif, sig[MAX_NCOMP], *sort_buf; double **vecs, **mat, *vec, **tab00, **wxy, *ker_norm, chi2_n, good_area, total_area, parity; DOM_TABS *domp; PARAMS par; /**************************************************************************/ imref = imdif = NULL; mask0 = mask1 = NULL; flag0 = flag1 = NULL; indx = NULL; vec = NULL; vecs = mat = NULL; tab00 = wxy = NULL; sort_buf = NULL; good_area = 0.0; domp = 0; /*** argument handling ***/ if (argc != 6) usage(); parfname = argv[1]; instrname= argv[2]; maskname = argv[3]; reffname = argv[4]; imgfname = argv[5]; /**************************************************************************/ /*** get parameters ***/ /**************************************************************************/ par.deg = deg; par.sig = sig; get_params(parfname, instrname, &par); if (par.verbose > 3) { printf("parfname: %s\n", parfname); printf("instrname: %s\n", instrname); printf("maskname: %s\n", maskname); printf("reffname: %s\n", reffname); printf("imgfname: %s\n", imgfname); printf("--------------\n"); } /*** get the list of input, output and kernel files ***/ if (par.verbose > 2) printf("Reading '%s'\n", imgfname); nim=read_list(par, imgfname, &inpfname, &outfname, &kerfname); if (par.verbose > 2) printf("%d file-names read read\n", nim); for (i=1; i<par.ncomp; i++) { par.deg[i] = par.deg[i-1] + par.deg_inc; par.sig[i] = par.sig[i-1] * par.sig_inc; } par.nx += 2*par.kerhw; par.ny += 2*par.kerhw; par.sdeg = par.wdeg; if (par.bdeg > par.wdeg) par.sdeg = par.bdeg; par.nwxy = (par.wdeg+1)*(par.wdeg+2)/2; par.nspat = (par.sdeg+1)*(par.sdeg+2)/2; par.nbkg = (par.bdeg+1)*(par.bdeg+2)/2; par.nvecs = par.nbkg; for (i=0; i<par.ncomp; i++) par.nvecs += (par.deg[i]+1)*(par.deg[i]+2)/2; par.ntot = par.nvecs + (par.nvecs - par.nbkg - 1)*(par.nwxy - 1); par.ndom = par.ndom_x*par.ndom_y; ntab = par.nvecs-par.nbkg+par.nspat; if (par.verbose) printf("\t %d image(s) to process\n\n", nim); /**************************************************************************/ /*** get memory ***/ /**************************************************************************/ imglen = par.nx*par.ny*sizeof(float); buflen = par.nx*par.ny*sizeof(double); matlen = par.ntot*sizeof(double *); veclen = par.ntot*sizeof(double); usblen = par.nx*par.ny*sizeof(unsigned short); tablen = ntab*sizeof(double *); lomlen = par.nvecs*sizeof(double *); lovlen = par.nvecs*sizeof(double); domlen = par.ndom*sizeof(DOM_TABS); srtlen = par.ndom*sizeof(float); intlen = par.ndom*sizeof(int); if (par.ntot > par.ndom) intlen = par.ntot*sizeof(int); if (!(im = (float *)malloc(imglen))) errmess("malloc(im)"); if (!(imref = (float *)malloc(imglen))) errmess("malloc(imref)"); if (!(imdif = (float *)malloc(imglen))) errmess("malloc(imdif)"); if (!(mat = (double **)malloc(matlen))) errmess("malloc(mat)"); if (!(tab00 = (double **)malloc(tablen))) errmess("malloc(tab00)"); if (!(vecs = (double **)malloc(tablen))) errmess("malloc(vecs)"); if (!(wxy = (double **)malloc(tablen))) errmess("malloc(wxy)"); if (!(domp = (DOM_TABS *)malloc(domlen))) errmess("malloc(DOM_TABS)"); if (!(sort_buf = (float *)malloc(srtlen))) errmess("malloc(sort_buf)"); for (i=0; i<ntab; i++) if (!(tab00[i] = (double *)malloc(buflen))) errmess("malloc(tab00[i])"); for (k=0; k<par.ndom; k++) { if (!(domp[k].mat0 = (double **)malloc(lomlen))) errmess("malloc(domp[k].mat0)"); if (!(domp[k].mat1 = (double **)malloc(lomlen))) errmess("malloc(domp[k].mat1)"); if (!(domp[k].mat = (double **)malloc(lomlen))) errmess("malloc(domp[k].mat)"); if (!(domp[k].vec0 = (double *)malloc(lovlen))) errmess("malloc(domp[k].vec0)"); if (!(domp[k].vec = (double *)malloc(lovlen))) errmess("malloc(domp[k].vec)"); for (i=0; i<par.nvecs; i++) { if (!(domp[k].mat0[i] = (double *)malloc(lovlen))) errmess("malloc(domp[k].mat0[i])"); if (!(domp[k].mat1[i] = (double *)malloc(lovlen))) errmess("malloc(domp[k].mat1[i])"); if (!(domp[k].mat[i] = (double *)malloc(lovlen))) errmess("malloc(domp[k].mat[i])"); } } /* mat is indexed from 1 to n for use with numerical recipes ! */ for (i=0; i<par.ntot; i++) { if (!(mat[i] = (double *)malloc(veclen))) errmess("malloc(mat[i])"); mat[i]--; } mat--; if (!(vec = (double *)malloc(veclen))) errmess("malloc(vec)"); if (!(indx = (int *)malloc(intlen))) errmess("malloc(indx)"); if (!(mask0 = (unsigned short *)malloc(usblen))) errmess("malloc(mask0)"); if (!(mask1 = (unsigned short *)malloc(usblen))) errmess("malloc(mask1)"); if (!(flag0 = (unsigned short *)malloc(usblen))) errmess("malloc(flag0)"); if (!(flag1 = (unsigned short *)malloc(usblen))) errmess("malloc(flag1)"); if (!(headlen=(long *)calloc(nim, sizeof(long)))) errmess("calloc(headlen)"); if (!(ker_norm=(double *)calloc(nim, sizeof(double)))) errmess("calloc(ker_norm)"); /**************************************************************************/ /**************************************************************************/ /* get information about header sizes */ mheadlen=get_headlen(maskname); rheadlen=get_headlen(reffname); init_difimages(inpfname, outfname, headlen, nim, par); /**************************************************************************/ if (par.verbose > 4) { printf("par.nx0= %d par.ny0 = %d\n", par.nx0, par.ny0); printf("par.kerhw= %d\n", par.kerhw); printf("par.nx= %d par.ny = %d\n", par.nx, par.ny); } nsec_x = (par.nx0 - 2*par.kerhw)/(par.nx - 2*par.kerhw); nsec_y = (par.ny0 - 2*par.kerhw)/(par.ny - 2*par.kerhw); /**************************************************************************/ /*** main loop over sections of each image ***/ /**************************************************************************/ if (par.verbose > 4) printf("main loop over sections: nsec_x= %d nsec_y= %d\n", nsec_x, nsec_y); for (isec_x=0; isec_x<nsec_x; isec_x++) { for (isec_y=0; isec_y<nsec_y; isec_y++) { y_off = isec_y*(par.ny - 2*par.kerhw); x_off = isec_x*(par.nx - 2*par.kerhw); mode = (int)( isec_x || isec_y ); if (par.verbose > 4) printf("isec_x= %d isec_y= %d mode= %d\n", isec_x, isec_y, mode); read_sector(maskname, mheadlen, par.nx0, par.ny0, x_off, y_off, (char *)mask0, sizeof(unsigned short), par.nx, par.ny); read_sector(reffname, rheadlen, par.nx0, par.ny0, x_off, y_off, (char *)imref, sizeof(float), par.nx, par.ny); make_vectors(imref, tab00, vecs, wxy, par); mask_badpix(imref, mask0, flag0, 0, par); get_domains(imref, mask0, domp, par, &par.ndom); make_domains(imref, mask0, vecs, domp, par); total_area = (2*par.domhw + 1); total_area *= total_area*par.ndom; /**********************************************/ /*** loop over images for a given section ***/ /**********************************************/ for (i=0; i<nim; i++) { if (par.verbose >= VERB_MED) printf("\nSection [%d,%d] of image %d : %s\n", isec_x+1, isec_y+1, i+1, inpfname[i]); read_sector(inpfname[i], headlen[i], par.nx0, par.ny0, x_off, y_off, (char *)im, sizeof(float), par.nx, par.ny); mask_badpix(im, mask1, flag1, 1, par); npix = clean_domains(im, imref, mask0, mask1, vecs, domp, par); /*** sigma clipping ***/ chi2_n = BIG_FLOAT; if (par.verbose >= VERB_MED) { printf("\nLocal sigma clipping of domains:\n"); printf(" iteration chi2/dof N(clip) N(fit)\n"); } for (k=0; k<=par.n_iter+1; k++) { good_area = npix/total_area; if (good_area < par.min_area) break; clone_domains(domp, par.ndom, par.nvecs); /**************************/ if (k > 0) { nclip = local_clip(im, imref, mask1, vecs, wxy, vec, domp, par, &chi2_n, &npix); if (par.verbose >= VERB_MED) printf("%6d\t%14.4f\t%9d\t%7d\n", k-1, par.gain*chi2_n, nclip, npix); } /**************************/ expand_matrix(mat, vec, wxy, domp, par); ludcmp(mat, par.ntot, indx-1, &parity); lubksb(mat, par.ntot, indx-1, vec-1); } if (par.verbose >= VERB_MED) { printf("\nSigma clipping of domain distribution:\n"); printf("iteration median chi2/dof sigma N(good)\n"); } nkeep = par.ndom; for (k=1; k<=par.n_iter_dom && nkeep>=par.min_nkeep; k++) nkeep = clip_domains(domp, sort_buf, indx, k, &chi2_n, par); /*** sigma clipping ends ***/ chi2_n *= par.gain; /*** final solution ***/ if (good_area < par.min_area) { printf("\nFailed for section [%d,%d] of image %d : %s\n", isec_x+1, isec_y+1, i+1, inpfname[i]); printf("*** [ good_area= %f < %f ] ***\n", good_area, par.min_area); } else if (chi2_n > par.max_chi2) { printf("\nFailed for section [%d,%d] of image %d : %s\n", isec_x+1, isec_y+1, i+1, inpfname[i]); printf("*** [ chi2_n= %f > %f ] ***\n", chi2_n, par.max_chi2); } else if (nkeep < par.min_nkeep) { printf("\nFailed for section [%d,%d] of image %d : %s\n", isec_x+1, isec_y+1, i+1, inpfname[i]); printf("*** [ nkeep= %d < %d ] ***\n", nkeep, par.min_nkeep); } else { expand_matrix(mat, vec, wxy, domp, par); ludcmp(mat, par.ntot, indx-1, &parity); lubksb(mat, par.ntot, indx-1, vec-1); spatial_convolve(im, imdif, vecs, wxy, vec, par); /*** output ***/ ker_norm[i] = vec[par.nbkg]; apply_norm(imdif, flag0, flag1, ker_norm[i], par.nx, par.ny, par.bad_value); write_sector(outfname[i], headlen[i], par.nx0, par.ny0, x_off, y_off, (char *)imdif, sizeof(float), par.nx, par.ny, par.kerhw); } if (par.verbose) write_kernel(kerfname[i], vec, nsec_x, nsec_y, mode, par, &chi2_n); } /**********************************************/ /*** loop over images ends ***/ /**********************************************/ } } /**********************************************/ /*** loop over sections ends ***/ /**********************************************/ if (par.verbose >= VERB_HIGH) { printf("\nKernel norms:\n\n"); for (i=0; i<nim; i++) printf("%s \t %8.5f\n", kerfname[i], ker_norm[i]); printf("\n"); } for (i=0; i<nim; i++) { free(inpfname[i]); free(outfname[i]); free(kerfname[i]); } free(inpfname); free(outfname); free(kerfname); free(headlen); free(ker_norm); return(0); }
rom::ROM rom::ROMFromFile(const std::string& filepath) { Eigen::MatrixXd res; std::ifstream myfile (filepath.c_str()); std::string line; double maxRadius_; int size_; double minx, miny, minz; double maxx, maxy, maxz; int axe1, axe2 , axe3; if (myfile.is_open()) { if(myfile.good()) { char r[255]; getline (myfile, line); sscanf(line.c_str(),"%s",r); maxRadius_ = (double) (strtod (r, NULL)); } else { std::string errmess("In file: " + filepath + "; file ended before finding radius"); throw(new ROMException(errmess)); } if(myfile.good()) { char s[255]; getline (myfile, line); sscanf(line.c_str(),"%s",s); size_ = (int) (strtod (s, NULL)); getline (myfile, line); if(line.size()> 0) { line = remplacerVirgule(line); char caxe1[255],caxe2[255],caxe3[255]; sscanf(line.c_str(),"%s %s %s",caxe1, caxe2, caxe3); axe1 = (int) strtod (caxe1, NULL); axe2 = (int) strtod (caxe2, NULL); axe3 = (int) strtod (caxe3, NULL); } getline (myfile, line); if(line.size()> 0) { line = remplacerVirgule(line); char cminx[255],cminy[255],cminz[255]; char cmaxx[255],cmaxy[255],cmaxz[255]; sscanf(line.c_str(),"%s %s %s %s %s %s",cminx, cmaxx, cminy, cmaxy, cminz, cmaxz); minx = (double) strtod (cminx, NULL); miny = (double) strtod (cminy, NULL); minz = (double) strtod (cminz, NULL); maxx = (double) strtod (cmaxx, NULL); maxy = (double) strtod (cmaxy, NULL); maxz = (double) strtod (cmaxz, NULL); } } res = ReadMatrix(size_, myfile); myfile.close(); } else { std::string errmess("file not found: " + filepath); throw(new ROMException(errmess)); } Eigen::MatrixXd A_ = res.block(0,0,size_,3); Eigen::VectorXd b_ = res.block(0,3,size_,1); return ROM(A_,b_,maxRadius_,minx, miny, minz, maxx, maxy, maxz, axe1, axe2, axe3); }
void local_max(IMAGE *im, FITPAR par, OBJ_LIST *objects) { int nobj, is_max, i, j, k, l, nx, ny, *obj_index; float *tmp_flux; double nsig2, nsig2_detect, bg, tmp_max; OBJECT *obj; BKG_STRUCT bkg; nx = im->nx; ny = im->ny; bkg.frac = par.bkg_frac; nobj = 0; if (!(obj=(OBJECT *)calloc(1,sizeof(OBJECT)))) errmess("calloc(obj)"); /* prepare some background stuff that is not changing to speed things up */ init_bkg(im, &bkg, par.anrad1, par.anrad2); /* loop over all pixels */ nsig2_detect = par.nsig_detect*par.nsig_detect; for (i=par.psfhw; i<nx-par.psfhw; i++) { for (j=par.psfhw; j<ny-par.psfhw; j++) { tmp_max = im->pix[i+nx*j]; is_max = 1; for (k=-par.maxhw; k<=par.maxhw && is_max; k++) for (l=-par.maxhw; l<=par.maxhw && is_max; l++) if (im->pix[i+k+nx*(j+l)] > tmp_max) is_max = 0; if (is_max && is_peak(im, i, j, par.peakhw, par.contrast)) { bg = getbkg(im, &bkg, i, j); nsig2 = (tmp_max - bg)*(tmp_max - bg)*im->gain/tmp_max; if (!(obj = (OBJECT *)realloc(obj, (nobj+1)*sizeof(OBJECT)))) errmess("realloc(obj)"); if ((nsig2 >= nsig2_detect) && (bg > 0.0)) { obj[nobj].x = i; obj[nobj].y = j; obj[nobj].max = tmp_max; obj[nobj].bg = bg; obj[nobj].flux = silly_phot(im, &obj[nobj], (int)(par.aprad+0.5)); obj[nobj].norm = obj[nobj].flux; nobj++; } } /* end of loop over all pixels */ } } if (par.verbose) printf("Found %d good local maxima\n", nobj); objects->list = obj; objects->nobj = nobj; if (!(tmp_flux=(float *)malloc(nobj*sizeof(float)))) errmess("malloc(tmp_flux)"); if (!(obj_index = (int *)malloc(nobj*sizeof(int)))) errmess("malloc(obj_index)"); for (i=0; i<nobj; i++) tmp_flux[i] = obj[i].flux; quick_sort(tmp_flux, obj_index, nobj); objects->index = obj_index; free(tmp_flux); return; }
void make_vectors(KER_STRUCT *ker) { int *deg, nvecs, mode, i, j, m, n, k, kern, kerhw, idx, ncomp; float *sig; double *ker_x, *ker_y, **vecs; sig = ker->sig; deg = ker->deg; vecs = ker->vecs; ncomp = ker->ncomp; kerhw = ker->hw; kern = 2*kerhw + 1; if (!(ker_x=(double *)malloc(kern*sizeof(double)))) errmess("malloc(ker_x)"); if (!(ker_y=(double *)malloc(kern*sizeof(double)))) errmess("malloc(ker_y)"); nvecs = 0; for (k=0; k<ncomp; k++) { for (m=0; m<=deg[k]; m++) { for (n=0; n<=deg[k]-m; n++) { mode = ((m/2)*2-m+1)*((n/2)*2-n+1); base_func(sig[k], m, n, ker_x, ker_y, kerhw, mode); /* the first vector is normalized to 1.0 */ /* the rest must have zero sums to enforce */ /* flux conservation */ for (i=0; i<kern; i++) for (j=0; j<kern; j++) vecs[nvecs][i+kern*j] = ker_x[i]*ker_y[j]; if ((nvecs > 0) && (mode)) for(idx=0;idx<kern*kern;idx++) { vecs[nvecs][idx] -= vecs[0][idx]; //printf("%f ",vecs[nvecs][idx]); } nvecs++; } } } if (nvecs != ker->nvecs) { fprintf(stderr, "make_vectors: warning: wrong number of "); fprintf(stderr, "kernel vectors - shouldn't happen!\n"); fflush(stderr); } free(ker_x); free(ker_y); return; }
/*--------------------------------------------------------*/ float *read_FITS_1Dfile(char *iname, char datatype, int *hsize, char ***header, int *n1) { void *buf; char hline[72], **lhead; int i, hs, bitpix, naxes, nx; float *ldata; double FITS_bzero, FITS_bscale; FILE *inf; if (!(inf=fopen(iname, "r"))) errmess(iname); hs=read_FITS_header(inf, &lhead); read_FITS_image(inf, lhead, hs, &buf); fclose(inf); if (get_FITS_key(hs, lhead, "NAXIS", hline) == -1) errmess("pfitsio: NAXIS not found in header"); sscanf(hline, "%d", &naxes); if (naxes != 1) errmess("Image must be 1 dimensional"); if (get_FITS_key(hs, lhead, "NAXIS1", hline) == -1) errmess("pfitsio: NAXIS1 not found in header"); sscanf(hline, "%d", &nx); if (get_FITS_key(hs, lhead, "BITPIX", hline) == -1) errmess("pfitsio: BITPIX not found in header"); sscanf(hline, "%d", &bitpix); if (!(ldata=(float *)calloc(nx, sizeof(long)))) errmess("calloc(ldata)"); if (get_FITS_key(hs, lhead, "BZERO", hline) == -1) FITS_bzero=0.0; else sscanf(hline, "%lg", &FITS_bzero); if (get_FITS_key(hs, lhead, "BSCALE", hline) == -1) FITS_bscale=1.0; else sscanf(hline, "%lg", &FITS_bscale); switch(bitpix) { case 8: if (datatype == 'u') for (i=0; i<nx; i++) ldata[i]=(float)((unsigned char *)buf)[i]; else for (i=0; i<nx; i++) ldata[i]=(float)((char *)buf)[i]; break; case 16: if (datatype == 'u') for (i=0; i<nx; i++) ldata[i]=(float)((unsigned short *)buf)[i]; else for (i=0; i<nx; i++) ldata[i]=(float)((short *)buf)[i]; break; case 32: if (datatype == 'u') for (i=0; i<nx; i++) ldata[i]=(float)((unsigned *)buf)[i]; else for (i=0; i<nx; i++) ldata[i]=(float)((int *)buf)[i]; break; case -32: for (i=0; i<nx; i++) ldata[i]=((float *)buf)[i]; break; case -64: for (i=0; i<nx; i++) ldata[i]=(float)((double *)buf)[i]; break; default: printf("bitpix=%d\n", bitpix); errmess("Not supported"); } free(buf); if (FITS_bscale != 1.0) for (i=0; i<nx; i++) ldata[i]*=FITS_bscale; if (FITS_bzero != 0.0) for (i=0; i<nx; i++) ldata[i]+=FITS_bzero; *n1=nx; *header=lhead; *hsize=hs; return(ldata); }
/*--------------------------------------------------------*/ int read_FITS_headers(FILE *inf, int **hsize, char ****header, long **offsets) { char ***lheader, value[VALUE_SIZE], val[VALUE_SIZE], tmp, eoF; int p, hen, // header extension number (0=primary header) *lhsize; // size of the header extension long imsize, offset, *loffsets; /* read primary header */ if (!(lheader=(char ***)calloc(1, sizeof(char **)))) errmess("read_FITS_headers(): calloc(lheader)"); if (!(lhsize=(int *)calloc(1, sizeof(int)))) errmess("read_FITS_headers(): calloc(lhsize)"); if (!(loffsets=(long *)calloc(1, sizeof(long)))) errmess("read_FITS_headers(): calloc(loffsets)"); loffsets[0]=0L; if (!(lhsize[0]=read_FITS_header(inf, &lheader[0]))) return(-1); /* check conformance with the FITS standard */ if ((p=get_FITS_key(lhsize[0], lheader[0], "SIMPLE", value)) == -1) { printf("ERROR! read_FITS_headers(): SIMPLE not found in the header\n"); return(-1); } if (p != 0) printf("WARNING! Header does not conform the FITS standard (SIMPLE)\n"); sscanf(value, "%s", val); if ((strcmp(val, "T")) && (strncmp(val, "T/", 2))) printf("WARNING! File does not conform the FITS standard (SIMPLE)\n"); /* check for possible extensions */ if ((p=get_FITS_key(lhsize[0], lheader[0], "EXTEND", value)) == -1) { *hsize=lhsize; *header=lheader; *offsets=loffsets; return(0); } if (p < 3) printf("WARNING! Header does not conform the FITS standard (EXTEND)\n"); sscanf(value, "%s", val); if ((strcmp(val, "T")) && (strncmp(val, "T/", 2))) { *hsize=lhsize; *header=lheader; *offsets=loffsets; return(0); } /* read extension headers */ hen=0; eoF=0; while (!eoF) { /* skip the image */ imsize=FITS_image_size(lhsize[hen], lheader[hen]); offset=imsize; if (imsize%RECORD_SIZE) offset+=(RECORD_SIZE-imsize%RECORD_SIZE); fseek(inf, offset, SEEK_CUR); /* check for the existence of another extension */ fread(&tmp, 1, 1, inf); if (feof(inf)) break; fseek(inf, -1, SEEK_CUR); /* read another header */ hen++; if (!(lheader=(char ***)realloc(lheader, (hen+1)*sizeof(char **)))) errmess("read_FITS_headers(): realloc(lheader)"); if (!(lhsize=(int *)realloc(lhsize, (hen+1)*sizeof(int)))) errmess("read_FITS_headers(): realloc(lhsize)"); if (!(loffsets=(long *)realloc(loffsets, (hen+1)*sizeof(long)))) errmess("calloc(loffsets)"); loffsets[hen]=offset; if (!(lhsize[hen]=read_FITS_header(inf, &lheader[hen]))) return(-1); /* check the XTENSION value - only IMAGE supported */ if ((p=get_FITS_key(lhsize[hen], lheader[hen], "XTENSION", value) == -1)) { printf("ERROR! read_FITS_headers(): XTENSION not found in the header\n"); return(-1); } if (p != 0) printf("WARNING! Header does not conform the FITS standard (XTENSION)\n"); if (strncmp(value, "'IMAGE '", 10)) printf("WARNING! Unsupported extension type: %s\n", value); if (!lhsize[hen]) eoF=1; } *hsize=lhsize; *header=lheader; *offsets=loffsets; return(hen); }
/*--------------------------------------------------------*/ int main(int argc, char *argv[]) { char *inp1name, *inp2name, *parfname, *outfname, *corrname; short *sat1, *sat2; int i, nobj1, nobj2, nsub1, nsub2, nmatch, maxobj, *index; float *x1, *y1, *x2, *y2, *mag1, *mag2, *xs1, *ys1, *xs2, *ys2, *xm1, *ym1, *xm2, *ym2, coeffx[3], coeffy[3]; FILE *outf; PARAMS par; /* IO stuff */ if (argc != 6) usage(); parfname = argv[1]; inp1name = argv[2]; inp2name = argv[3]; outfname = argv[4]; corrname = argv[5]; read_params(parfname, &par); nobj1=read_objects(inp1name, &x1, &y1, &mag1, &sat1); if (par.verbose) printf("%d objects read from %s\n", nobj1, inp1name); if (par.nsub > nobj1) par.nsub = nobj1; nsub1=bright_end(nobj1, x1, y1, mag1, par.nsub, &xs1, &ys1, par.verbose); nobj2=read_objects(inp2name, &x2, &y2, &mag2, &sat2); if (par.verbose) printf("%d objects read from %s\n", nobj2, inp2name); if (par.nsub > nobj2) par.nsub = nobj2; nsub2=bright_end(nobj2, x2, y2, mag2, par.nsub, &xs2, &ys2, par.verbose); printf("nsub1= %d\n", nsub1); printf("nsub2= %d\n", nsub2); par.nsub=(nsub1 < nsub2 ? nsub1 : nsub2); if (par.verbose > 1) printf("par.nsub= %d\n", par.nsub); free(mag1); free(mag2); if (par.verbose > 2) { printf("Coordinates of the brighest objects:\n"); printf(" X1 Y1 X2 Y2\n"); printf("--------------------------\n"); for (i=0; i<par.nsub; i++) printf("%8.2f %8.2f %8.2f %8.2f\n", xs1[i], ys1[i], xs2[i], ys2[i]); printf("--------------------------\n\n"); } /* match nsub brightest stars for approximate transformation */ maxobj=(nobj1 > nobj2 ? nobj1 : nobj2); if (par.verbose > 2) printf("maxobj= %d\n", maxobj); if (!(index=(int *)calloc(maxobj, sizeof(int)))) errmess("calloc(index)"); triangles(xs1, ys1, xs2, ys2, par.nsub, par.nsub, index, par); if (!(xm1=(float *)malloc(sizeof(float)))) errmess("malloc(xm1)"); if (!(ym1=(float *)malloc(sizeof(float)))) errmess("malloc(ym1)"); if (!(xm2=(float *)malloc(sizeof(float)))) errmess("malloc(xm2)"); if (!(ym2=(float *)malloc(sizeof(float)))) errmess("malloc(ym2)"); nmatch=0; for (i=0; i<par.nsub; i++) { if (index[i] != -1) { if (!(xm1=(float *)realloc(xm1, (nmatch+1)*sizeof(float)))) errmess("realloc(xm1)"); if (!(ym1=(float *)realloc(ym1, (nmatch+1)*sizeof(float)))) errmess("realloc(ym1)"); if (!(xm2=(float *)realloc(xm2, (nmatch+1)*sizeof(float)))) errmess("realloc(xm2)"); if (!(ym2=(float *)realloc(ym2, (nmatch+1)*sizeof(float)))) errmess("realloc(ym2)"); xm1[nmatch]=xs1[i]; ym1[nmatch]=ys1[i]; xm2[nmatch]=xs2[index[i]]; ym2[nmatch]=ys2[index[i]]; nmatch++; } } free(xs1); free(ys1); free(xs2); free(ys2); if (nmatch < 2) { printf("ERROR: nmatch < 2\n"); exit(2); } if (par.verbose) printf("%d objects matched by triangles()\n", nmatch); /* linear fit to nmatch stars indentified by triangles */ xy_lin(xm1, ym1, xm2, ym2, nmatch, coeffx, coeffy); free(xm1); free(ym1); free(xm2); free(ym2); if (par.verbose > 1) { printf("Linear transformation data:\n"); printf("----------------------\n"); for (i=0; i<3; i++) printf("coeffx[%d]= %12g coeffy[%d]= %12g\n", i, coeffx[i], i, coeffy[i]); printf("----------------------\n"); } nobj1=reject_saturated(nobj1, &x1, &y1, sat1); nobj2=reject_saturated(nobj2, &x2, &y2, sat2); if (par.verbose > 1) { printf("%d objects from %s left after reject_saturated()\n", nobj1, inp1name); printf("%d objects from %s left after reject_saturated()\n", nobj2, inp2name); } free(sat1); free(sat2); /* using linear fit transform one list and look for close neighbors */ for (i=0; i<nobj1; i++) maxobj=refine(coeffx, coeffy, x1, y1, x2, y2, nobj1, nobj2, index, par.ptol); if (par.verbose) { printf("%d objects left in the template list after refine()\n", maxobj); printf("Writing matched list to %s\n\n", outfname); } if (!(outf=fopen(outfname, "w"))) errmess("outfname"); for (i=0; i<nobj1; i++) if (index[i] != -1) fprintf(outf, "%9.3f %10.3f %10.3f %10.3f\n", x1[i], y1[i], x2[index[i]], y2[index[i]]); fclose(outf); free(index); free(x1); free(y1); free(x2); free(y2); if (!(outf=fopen(corrname, "w"))) errmess(corrname); fprintf(outf, "%d %d %s", (int)(coeffx[2]+0.5), (int)(coeffy[2]+0.5), inp2name); fclose(outf); return(0); }
int main(int argc, char * argv[]) { if (argc < 4) { printf("Usage: %s option dimension textfile\n" \ "\toption: 0 (display diagonal entries of result from Strassen), \n"\ "\t\t1 (display full result from Strassen), \n"\ "\t\t2 (display full result from Regular), \n"\ "\t\t3 (test to find best crossover point)\n", argv[0]); exit(1); } else { const long n = strtol(argv[2], NULL, 10); int i, j, k; long long c, d; float ** m1, ** m2, ** m3; FILE * fp = fopen(argv[3], "r"); long a1 = strtol(argv[1], NULL, 10); n0 = N0; m1 = malloc(n * sizeof(float *)); m2 = malloc(n * sizeof(float *)); m3 = malloc(n * sizeof(float *)); for (i = 0; i < n; i++) { m1[i] = malloc(n * sizeof(float)); m2[i] = malloc(n * sizeof(float)); m3[i] = malloc(n * sizeof(float)); } for (i = 0; i < n; i++) for (j = 0; j < n; j++) { c = fscanf(fp, "%f", &(m1[i][j])); if (c == EOF) errmess(1, argv[0], "reached end of file before end of m1"); m3[i][j] = 0; } for (i = 0; i < n; i++) for (j = 0; j < n; j++) { c = fscanf(fp, "%f", &(m2[i][j])); if (c == EOF) errmess(2, argv[0], "reached end of file before end of m2"); } fclose(fp); switch (a1) { case 0: c = strassmult(m1, 0, 0, m2, 0, 0, m3, 0, 0, n); for (i = 0; i < n; i++) printf("%5.2f\n", (m3[i][i])); break; case 1: c = strassmult(m1, 0, 0, m2, 0, 0, m3, 0, 0, n); display(m3, 0, 0, n); printf("number of operations: %lld\n", c); break; case 2: c = regmult(0, m1, 0, 0, n, n, m2, 0, 0, n, n, m3, 0, 0); display(m3, 0, 0, n); printf("number of operations: %lld\n", c); break; default: { for (k = 1; k <= n; k*=2){ n0 = 2; c = strassmult(m1, 0, 0, m2, 0, 0, m3, 0, 0, k); d = c; while ((c <= d) && (n0 <= k)){ d = c; n0++; c = strassmult(m1, 0, 0, m2, 0, 0, m3, 0, 0, k); } printf("%d %lld\n", n0 - 1, d); } exit(0); } } for (i = 0; i < n; i++) { free(m1[i]); free(m2[i]); free(m3[i]); } free(m1); free(m2); free(m3); return(0); } }
/*--------------------------------------------------------*/ int main(int argc, char *argv[]) { char *parfname, *inpfname, *outfname; int i, nobj, nterm, ik, ik0, delta, niter; float *x, *nx, *nzx, *zx, *y, *ny, *nzy, *zy, dx, dy, rr, thresh, sx, sy; double *coeffx, *coeffy; FILE *outf; PARAMS par; /* IO stuff */ if (argc != 4) { printf("\n\tUSAGE: xygrid parameter_file input_list coeff_file\n\n"); exit(1); } parfname = argv[1]; inpfname = argv[2]; outfname = argv[3]; readpar(parfname, &par); nterm = (par.ndeg+1)*(par.ndeg+2)/2; nobj=readcoor(inpfname, &x, &y, &zx, &zy); if (!(coeffx=(double *)calloc(nterm, sizeof(double)))) errmess("calloc(coeffx)"); if (!(coeffy=(double *)calloc(nterm, sizeof(double)))) errmess("calloc(coeffy)"); /* make initial fit */ fit(x, y, zx, par.ndeg, nobj, coeffx); fit(x, y, zy, par.ndeg, nobj, coeffy); sx = sigma(x, y, zx, par.ndeg, nobj, coeffx); sy = sigma(x, y, zy, par.ndeg, nobj, coeffy); thresh = par.sigmaf*par.sigmaf*(sx*sx + sy*sy); ik = nobj; delta = 1; niter = 0; if (!(nx=(float *)calloc(nobj, sizeof(float)))) errmess("readcoor: calloc(nx)"); if (!(ny=(float *)calloc(nobj, sizeof(float)))) errmess("readcoor: calloc(ny)"); if (!(nzx=(float *)calloc(nobj, sizeof(float)))) errmess("readcoor: calloc(nzx)"); if (!(nzy=(float *)calloc(nobj, sizeof(float)))) errmess("readcoor: calloc(nzy)"); /* sigma clipping of the fit until MAX_NITER reached or nothing changes */ while ((delta > 0) && (niter < par.maxniter)) { ik0 = ik; ik = 0; for (i=0; i<nobj; i++) { dx = poly(x[i], y[i], par.ndeg, coeffx) - zx[i]; dy = poly(x[i], y[i], par.ndeg, coeffy) - zy[i]; rr = dx*dx + dy*dy; nx[ik] = x[i]; ny[ik] = y[i]; nzx[ik] = zx[i]; nzy[ik] = zy[i]; if (rr < thresh) ++ik; } delta = ik0 - ik; fit(nx, ny, nzx, par.ndeg, ik, coeffx); fit(nx, ny, nzy, par.ndeg, ik, coeffy); sx = sigma(nx, ny, nzx, par.ndeg, ik, coeffx); sy = sigma(nx, ny, nzy, par.ndeg, ik, coeffy); niter++; } free(x); free(y); free(zx); free(zy); free(nx); free(ny); free(nzx); free(nzy); /* print results and store coefficients in binary file */ if (par.verbose) { printf("\n"); for (i=0; i<nterm; i++) { printf("coeffx[%d] = %9.6f ", i, coeffx[i]); printf("coeffy[%d] = %9.6f \n", i, coeffy[i]); } } printf("%s: sigmax= %.4f sigmay= %.4f ndata= %d nleft= %d\n", outfname, sx, sy, nobj, ik); if (!(outf=fopen(outfname, "w"))) errmess(outfname); fwrite(&nterm, sizeof(int), 1, outf); fwrite(coeffx, sizeof(double), nterm, outf); fwrite(coeffy, sizeof(double), nterm, outf); fclose(outf); free(coeffx); free(coeffy); return(0); }