int nemoinpx( char *expr, double *a, int na) { int nret, ncomp; string *vals, *comp; vals = burststring(expr,","); for (nret=0; vals[nret] != NULL; nret++) { if (nret>=na) return -23; comp = burststring(vals[nret],":"); ncomp = xstrlen(comp,sizeof(string))-1; if (ncomp < 1 || ncomp > 3) return -13; a[nret] = atof(comp[0]); if (ncomp==1) continue; a[nret] += atof(comp[1])/60.0; if (ncomp==2) continue; a[nret] += atof(comp[2])/3600.0; freestrings(comp); } freestrings(vals); return nret; }
/* * return the full path to a filter based on a short name * or, if the shortname is the full path, return itself */ string filtername(string shortname) { int nsp; static char fullname[256]; char line[MAX_LINELEN]; string *sp, fpath; stream fstr; if (nemo_file_size(shortname) > 0) return shortname; fpath = getenv("NEMODAT"); if (fpath == 0) error("NEMODAT does not exist"); sprintf(fullname,"%s/filter/Fnames",fpath); dprintf(1,"Alias table %s\n",fullname); fstr = stropen(fullname,"r"); while (fgets(line,MAX_LINELEN,fstr)) { if (line[0] == '#') continue; sp = burststring(line," \n"); nsp = xstrlen(sp,sizeof(sp))-1; if (nsp > 1) { if (streq(shortname,sp[0])) { sprintf(fullname,"%s/filter/%s",fpath,sp[1]); dprintf(1,"Matching %s\n",fullname); freestrings(sp); return fullname; } } freestrings(sp); } return shortname; }
int string_index(string options, string s) { int i=0; string *sa = burststring(options,","); while (sa[i]) { if (streq(sa[i],s)) { freestrings(sa); return i; } i++; } freestrings(sa); return -1; }
static void writesnps(int ncid, int nsnps) { size_t start = 0; size_t count = nsnps; int snpid, stat; stat = nc_inq_varid(ncid, "snp", &snpid); check_err(stat,__LINE__,__FILE__); stat = nc_put_vara_string(ncid, snpid, &start, &count, (const char **) snps); check_err(stat,__LINE__,__FILE__); freestrings(snps, nsnps); }
convert(stream instr, stream outstr) { char line[MAX_LINELEN]; /* input linelength */ double dval; int nval, i, nlines, sign, decimalval,nhms,ndms; string *outv, *hms, *dms; /* pointer to vector of strings to write */ string seps=", \t"; /* column separators */ real dd, mm, ss; nlines=0; /* count lines read so far */ for(;;) { /* loop over all lines in file(s) */ if (get_line(instr, line) < 0) return 0; dprintf(3,"LINE: (%s)\n",line); if(line[0] == '#') continue; /* don't use comment lines */ nlines++; outv = burststring(line,seps); i=0; while (outv[i]) { if (colmode[i+1] == 0) { /* no special mode: just copy */ if (fromhms[i+1]) { hms = burststring(outv[i],sep); nhms = xstrlen(hms,sizeof(string))-1; dval = atof(hms[0])*3600; if (nhms > 1) dval += atof(hms[1])*60; if (nhms > 2) dval += atof(hms[2]); if (nhms > 3) error("HMS string %s too many %s",outv[i],sep); fprintf(outstr,fmt,dval/3600.0); fputs(" ",outstr); freestrings(hms); } else if (fromdms[i+1]) { dms = burststring(outv[i],sep); ndms = xstrlen(dms,sizeof(string))-1; dval = atof(dms[0])*3600; if (ndms > 1) dval += atof(dms[1])*60; if (ndms > 2) dval += atof(dms[2]); if (ndms > 3) error("DMS string %s too many %s",outv[i],sep); fprintf(outstr,fmt,dval*15/3600.0); fputs(" ",outstr); freestrings(dms); } else { fputs(outv[i],outstr); fputs(" ",outstr); } } else { if (nemoinpd(outv[i],&dval,1)<0) error("syntax error decoding %s",outv[i]); if (colmode[i+1] < 0) { sign = SGN(dval); dval = ABS(dval); /* do we allow < 0 ??? */ decimalval = (int)floor(dval); fprintf(outstr,"%d",decimalval*sign); fprintf(outstr,"%s",sep); dval -= decimalval; dval *= ABS(colmode[i+1]); } sign = SGN(dval); dval = ABS(dval); dd = floor(dval); dval = (dval-dd)*60.0; mm = floor(dval); ss = (dval-mm)*60.0; fprintf(outstr,"%02d",(int)dd*sign); fprintf(outstr,"%s",sep); fprintf(outstr,"%02d",(int)mm); fprintf(outstr,"%s",sep); fprintf(outstr,"%06.3f ",ss); } i++; } fputs("\n",outstr); freestrings(outv); } /* for(;;) */ }
void nemo_main(void){ int i,numwritten=0,nummissed=0; int numimages=0; stream fp_in,fp,fp2; string *sp,logic; int count,n; int edge_dist; /* exclude sources this many pixels from the edge */ string inFile; /* name of input file */ char line[MAX_LINELEN]; /* a line of text from the inFile */ int racol,deccol,maxcol; /* column numbers for ra/dec in file */ double ra,dec; /* the values of ra/dec for a given line */ int nentries; /* number of valid entries, used for progress tracking */ inFile = getparam("in"); edge_dist = getiparam("edge"); logic = getparam("logic"); sp = burststring(getparam("col"),", \n"); n = xstrlen(sp,sizeof(string)) - 1; if (n != 2) error("You must specify ra,dec columns for col keyword!"); racol = natoi(sp[0]) - 1; deccol = natoi(sp[1]) - 1; freestrings(sp); if (racol >= deccol) maxcol = racol; else maxcol = deccol; sp = burststring(getparam("fits"),", \n"); n = xstrlen(sp,sizeof(string))-1; for (i=0; i<n; i++){ ini_mask(sp[i],i); numimages++; } freestrings(sp); sp = burststring(getparam("out"),", \n"); n = xstrlen(sp,sizeof(string))-1; if (n == 1){ fp = stropen(sp[0],"w"); fp2 = stropen("/dev/null","w!"); } else if (n == 2){ fp = stropen(sp[0],"w"); fp2 = stropen(sp[1],"w"); }else error("You must give one or two output files!\n"); freestrings(sp); nentries = nemo_file_lines(inFile,0); progress_init("overlap percent complete:",nentries); nentries = 0; fp_in = stropen(inFile,"r"); while (fgets(line,MAX_LINELEN,fp_in)){ progress_update(nentries); if (line[0] != '#'){ nentries += 1; sp = burststring(line," \t\n"); n = xstrlen(sp,sizeof(string)) - 2; /* zero based */ if (n < maxcol) error("Insufficient columns on line!"); ra = natof(sp[racol]); dec = natof(sp[deccol]); freestrings(sp); count = 0; for (i=0; i< numimages; i++) count+= is_edge(ra,dec,edge_dist,i); if(streq(logic,"or") && (count<numimages)){ fprintf(fp,line); numwritten++; } else if(streq(logic,"and") && (count==0) && (numimages > 0)){ fprintf(fp,line); numwritten++; } else { fprintf(fp2,line); nummissed++; } } } strclose(fp_in); strclose(fp); strclose(fp2); progress_finish(); printf("Sources outside regions: %d\n",nummissed); printf("Sources inside regions: %d\n",numwritten); printf("Number of images: %d\n",numimages); }
void nemo_main() { int i,j,nentries,ncol,nsp; FILE *fp; /* File pointer for output file */ string outfile; /* name of output file */ string *sp; /* used to read the command line argument "col" */ string wcs_type; /* WCS projection type (can be XXX or -XXX) */ char type[5]; /* the real WCS for worldpos() ; needs to be '-XXX' */ int nx,ny; /* size of map in pixels for x,y directions */ double index,rzero; /* parameters for powerlaw */ double crval1,crval2; /* center RA and DEC */ double cdelt1,cdelt2; /* delta RA and delta DEC for pixels */ double crpix1,crpix2; /* reference pixel */ double xpix, ypix; /* current x and y pixel position in map */ double ra,dec; /* ra,dec of current pixel position in map */ double flux; /* computed flux for each pixel */ double radius; /* radius, in pixels */ /* Read command line */ index = getdparam("index"); rzero = getdparam("rzero"); outfile = getparam("out"); wcs_type = getparam("type"); if (strlen(wcs_type)==3) { /* make sure type is '-XXX' */ type[0] = '-'; strcpy(&type[1],wcs_type); } else if (strlen(wcs_type)==4) { if (*wcs_type == '-') strcpy(type,wcs_type); else error("type=%s probably not legal, should be -XXX",wcs_type); } else error("type=%s must be a valid -XXX or XXX WCS descriptor",wcs_type); sp = burststring(getparam("crval")," ,"); nsp = xstrlen(sp,sizeof(string))-1; if (nsp != 2) error("You must specify two values for the crval keyword!"); crval1 = natof(sp[0]); crval2 = natof(sp[1]); freestrings(sp); sp = burststring(getparam("naxis")," ,"); nsp = xstrlen(sp,sizeof(string))-1; if (nsp != 2) error("You must specify two values for the naxis keyword!"); nx = natoi(sp[0]); ny = natoi(sp[1]); freestrings(sp); crpix1 = nx/2.0+1; crpix2 = ny/2.0+1; cdelt2 = getdparam("cdelt")/3600.; cdelt1 = -cdelt2; /* Test to make sure we can write to output file */ fp = stropen(outfile,"w"); strclose(fp); fprintf(stderr,"nx,ny = %d,%d\n",nx,ny); fprintf(stderr,"crval1,crval2=%lf,%lf\n",crval1,crval2); fprintf(stderr,"crpix1,crpix2=%lf,%lf\n",crpix1,crpix2); fprintf(stderr,"wcs: %s\n",type); xpix=1.0; ypix=1.0; worldpos(xpix,ypix,crval1,crval2,crpix1,crpix2,cdelt1,cdelt2,0.0,type,&ra,&dec); dprintf(1,"LL corner: %g %g\n",ra,dec); xpix=nx; worldpos(xpix,ypix,crval1,crval2,crpix1,crpix2,cdelt1,cdelt2,0.0,type,&ra,&dec); dprintf(1,"LR corner: %g %g\n",ra,dec); ypix=ny; worldpos(xpix,ypix,crval1,crval2,crpix1,crpix2,cdelt1,cdelt2,0.0,type,&ra,&dec); dprintf(1,"UR corner: %g %g\n",ra,dec); xpix=1.0; worldpos(xpix,ypix,crval1,crval2,crpix1,crpix2,cdelt1,cdelt2,0.0,type,&ra,&dec); dprintf(1,"UL corner: %g %g\n",ra,dec); /* Write header for TABFITS to file */ fp = stropen(getparam("out"),"w!"); fprintf(fp,"# NAXIS1 = %d\n",nx); fprintf(fp,"# NAXIS2 = %d\n",ny); fprintf(fp,"# NAXIS3 = 1\n"); fprintf(fp,"# CDELT1 = %lf\n",cdelt1); fprintf(fp,"# CDELT2 = %lf\n",cdelt2); fprintf(fp,"# CRVAL1 = %lf\n",crval1); fprintf(fp,"# CRVAL2 = %lf\n",crval2); fprintf(fp,"# CRPIX1 = %f\n",crpix1); fprintf(fp,"# CRPIX2 = %f\n",crpix2); fprintf(fp,"# CTYPE1 = 'RA--%s'\n",type); fprintf(fp,"# CTYPE2 = 'DEC-%s'\n",type); fprintf(fp,"# COMMENT index=%g / powerlaw index\n",index); fprintf(fp,"# COMMENT rzero=%g / rzero parameter\n",rzero); fprintf(fp,"# COMMENT out=%s / output table\n",outfile); fprintf(fp,"# COMMENT crval=%f,%f / Center WCS coordinates\n",crval1,crval2); fprintf(fp,"# COMMENT naxis=%d,%d / Image size in pixels\n",nx,ny); fprintf(fp,"# COMMENT cdelt=%f / pixel size in arcseconds\n",cdelt2*3600); fprintf(fp,"# COMMENT type=%s / Projection type\n",type); fprintf(fp,"# COMMENT VERSION=%s / Version number\n",getparam("VERSION")); for (i=0; i< ny; i++){ ypix = i + 1.0; for (j=0; j< nx; j++){ xpix = j + 1.0; worldpos(xpix,ypix,crval1,crval2,crpix1,crpix2,cdelt1,cdelt2,0.0, type,&ra,&dec); radius = sqrt((xpix-crpix1)*(xpix-crpix1) + (ypix-crpix2)*(ypix-crpix2)); flux = 1.0/(1.0 + pow(radius/rzero,index)); /* if (radius <= 4) flux = 1.0; else flux = 1.0/(1.0 + pow(radius-4,index)); */ fprintf(fp,"%lf %lf %4.2e\n",ra,dec,flux); } } strclose(fp); }
void nemo_main() { string *sp; /* string pointer for output of burststring */ int nsp; /* number of tokens in sp after using burststring */ double magj,magh,magks; /* JHKs magnitudes */ double sigmaj,sigmah,sigmaks; /* JHKs uncertainties in magnitudes */ double sum_jh,sum_jh2; /* Sums for J-H weighted averages */ double sum_hks,sum_hks2; /* Sums for H-Ks weighted averages */ double w_jh,w_hks,w_jhhks; /* sums of weights for J-H,H-Ks, and covariance */ double w_jh2,w_hks2,w_jhhks2; /* sums of square of weights for J-H,H-Ks, and cov. */ double sum_jhhks; /* for covariance matrix */ double sum_jhalt,sum_hksalt; /* for covariance matrix */ double tmp1,tmp2,tmp3; /* temp variables */ double avg_jh,avg_hks; /* average values of J-H,H-Ks */ double std_jh,std_hks; /* Std. deviations of J-H,H-Ks*/ double cov; /* covariance of J-H and H-Ks */ double no_jh,no_hks,no_jhhks; /* unweighted sums */ double no_jh2,no_hks2; /* unweighted sums */ int num; /* number of sources read */ stream fp; /* File pointer */ char line[MAX_LINELEN]; /* Character string for reading from file */ /* Initialize variables */ sum_jh = 0; sum_jh2 = 0; sum_hks = 0; sum_hks2 = 0; sum_jhhks = 0; sum_jhalt = 0; sum_hksalt= 0; w_jh = 0; w_hks = 0; w_jhhks = 0; w_jh2 = 0; w_hks2 = 0; w_jhhks2 = 0; num = 0; no_jh = 0; no_hks = 0; no_jhhks = 0; no_jh2 = 0; no_hks2 = 0; fp = stropen(getparam("in"),"r"); while (fgets(line,MAX_LINELEN,fp)){ if (line[0] != '#'){ /* lines starting with # are comments */ sp = burststring(line," \t\n"); nsp = xstrlen(sp,sizeof(string))-1; if (nsp < 8) warning("Skipping line: %s",line); magj = natof(sp[2]); sigmaj = natof(sp[3]); magh = natof(sp[4]); sigmah = natof(sp[5]); magks = natof(sp[6]); sigmaks = natof(sp[7]); num += 1; tmp1 = 1.0/(sigmaj*sigmaj + sigmah*sigmah); tmp2 = 1.0/(sigmah*sigmah + sigmaks*sigmaks); tmp3 = sqrt(tmp1*tmp2); sum_jh += tmp1*(magj - magh); sum_jh2 += tmp1*(magj - magh)*(magj - magh); sum_hks += tmp2*(magh - magks); sum_hks2 += tmp2*(magh - magks)*(magh - magks); sum_jhhks += tmp3*(magj - magh)*(magh - magks); sum_jhalt += tmp3*(magj - magh); sum_hksalt += tmp3*(magh - magks); no_jh += (magj - magh); no_jh2 += (magj - magh)*(magj - magh); no_hks += (magh - magks); no_hks2 += (magh - magks)*(magh - magks); no_jhhks += (magj - magh)*(magh - magks); w_jh += tmp1; w_hks += tmp2; w_jhhks += tmp3; w_jh2 += tmp1*tmp1; w_hks2 += tmp2*tmp2; w_jhhks2 += tmp3*tmp3; freestrings(sp); } } strclose(fp); avg_jh = sum_jh/w_jh; avg_hks = sum_hks/w_hks; std_jh = sqrt(w_jh/(w_jh*w_jh - w_jh2)*(sum_jh2 - avg_jh*avg_jh*w_jh)); std_hks = sqrt(w_hks/(w_hks*w_hks - w_hks2)*(sum_hks2 - avg_hks*avg_hks*w_hks)); cov = sum_jhhks - avg_hks*sum_jhalt - avg_jh*sum_hksalt + avg_jh*avg_hks*w_jhhks; cov = w_jhhks/(w_jhhks*w_jhhks - w_jhhks2)*cov; fprintf(stdout,"Weighted:\n"); fprintf(stdout,"Average J-H = %5.3e +- %5.3e\n",avg_jh,std_jh); fprintf(stdout,"Average H-Ks = %5.3e +- %5.3e\n",avg_hks,std_hks); fprintf(stdout,"Covariance = %5.3e\n",cov); avg_jh = no_jh/num; avg_hks = no_hks/num; std_jh = sqrt((no_jh2 - avg_jh*avg_jh*num)/(num - 1)); std_hks = sqrt((no_hks2 - avg_hks*avg_hks*num)/(num - 1)); no_jhhks = (no_jhhks- avg_jh*avg_hks*num)/(num - 1); fprintf(stdout,"Unweighted:\n"); fprintf(stdout,"Average J-H = %5.3e +- %5.3e\n",avg_jh,std_jh); fprintf(stdout,"Average H-Ks = %5.3e +- %5.3e\n",avg_hks,std_hks); fprintf(stdout,"Covariance = %5.3e\n",no_jhhks); }
convert(stream instr, stream outstr) { char line[MAX_LINELEN]; /* input linelength */ int nlines, nwords, nskip; long long nt0, nt; real dt; struct tm tm, tm0; string *bp; if (time0) { tm0.tm_sec= tm0.tm_min = tm0.tm_hour = 0; strptime(time0,format1,&tm0); strftime(line,MAX_LINELEN,"%s",&tm0); nt0 = atoll(line); dprintf(0,"Using time0=%lld sec since 1970.0 using %s on %s \n", nt0,format1,time0); } nlines=0; /* count lines read so far */ nskip=0; for(;;) { /* loop over all lines in file(s) */ if (get_line(instr, line) < 0) return 0; dprintf(3,"LINE: (%s)\n",line); if(line[0] == '#') continue; /* don't use comment lines */ nlines++; tm.tm_sec= tm.tm_min = tm.tm_hour = 0; if (dcol>0) { bp = burststring(line,", "); /* split line in words */ nwords = xstrlen(bp,sizeof(string))-1; if (nwords<dcol) { nskip++; continue; } strcpy(line,bp[dcol-1]); /* only pick out 1 column now */ freestrings(bp); } strptime(line,format1,&tm); if (need_time0) { dprintf(0,"First line: %s\n",line); strftime(line,MAX_LINELEN,"%s",&tm); time0 = strdup(line); nt0 = atoll(time0); dprintf(0,"First line uses: time0=%lld sec since 1970.0\n",nt0); need_time0 = FALSE; } strftime(line,MAX_LINELEN,format2,&tm); if (time0) { nt = atoll(line); if (use_scale) { dt = (nt-nt0)/scale; sprintf(line,"%g",dt); } else sprintf(line,"%lld",nt-nt0); } fputs(line,outstr); fputs("\n",outstr); } /* for(;;) */ dprintf(1,"Processed %d lines\n",nlines); if (nskip) warning("%d/%d lines skipped because dcol too large",nskip,nlines); }