Beispiel #1
0
setrange(real *rval, string rexp)
{
    char *cptr;

    cptr = strchr(rexp, ':');
    if (cptr) {
        rval[0] = natof(rexp);
        rval[1] = natof(cptr+1);
    } else {
        rval[0] = 0.0;
        rval[1] = natof(rexp);
    	warning("Range taken from 0 - %g",rval[1]);
    }
}
Beispiel #2
0
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);
}
Beispiel #3
0
void nemo_main(void)
{
    char   fmt1[20], fmt2[20], *cp;
    double dms[32];
    double *x;
    int    *ix;
    int    i,nret, nx, seed;
    bool   Qnl, Qint,Qdms;
    stream outstr;

    if (hasvalue("tab"))
        outstr = stropen(getparam("tab"),"w");
    else
        outstr = stdout;                /* is that really ok? */
    cp = getparam("format");            /* get format string to print with */
    if (strchr(cp,'%')==NULL)
      warning("%s badly formed printf-conversion specification ???",cp);
    if (strchr(cp,'d')) {
      dprintf(1,"Using integer math\n");
      Qint = TRUE;
    } else {
      dprintf(1,"Using floating point math\n");
      Qint = FALSE;
    }
    seed = init_xrandom(getparam("seed"));
    dprintf(1,"init_xrandom: seed=%d\n",seed);
    Qdms = getbparam("dms");
    if (Qdms) {
      nret = nemoinpx(getparam("expression"),dms,32);
      if (nret < 0) error("Parsing dms expression");
      for (i=0; i<nret; i++)
	printf("%g\n",dms[i]);
      return;  /* for now */
    }

    strcpy (fmt1,cp);                   /* store it in 'fmt' */
    strcpy (fmt2,cp);                   /* and here */
    cp = getparam("separ");             
    if (hasvalue("separ"))              /* separator between numbers ? */
        strcat(fmt2,getparam("separ"));
    else
        strcat (fmt2," ");              /* else use blank as separator */

    Qnl = getbparam("newline");         /* use newlines also ? */

    if (hasvalue("atof")) {
      printf(fmt1,natof(getparam("atof")));
      printf("\n");
      return;
    }

    if (!hasvalue("expression")) {      /* extra help if nothing given */
    	/* this somewhat unusual exit is because aliens often use this program */
	/* what really should have been done is make "expression=???" default */
        dprintf(0,"Usage: %s <expression>\n",getargv0());
        dprintf(0,"\n<expr> can be of form:  start[:end][:incr][,start::repeat]...\n");
        dprintf(0,"Also try keyword 'help= or help=h'\n");
        stop(0);
    }
    nx = getiparam("nmax");

    if (Qint) {
        ix = (int *) allocate(nx * sizeof(int));
   
        nret = nemoinpi(getparam("expression"),ix,nx);
        if (nret == -23)
            error("Too many items in list, use a bigger nmax=%d",nx);
        else if (nret < 0)
            error("nemoinp (%s) parsing error (%d)",cp,nret);

        for (i=0; i< nret-1; i++) {
            fprintf(outstr,fmt2,ix[i]);
            if (Qnl)
                fprintf(outstr,"\n");
        }
        fprintf (outstr,fmt1,ix[nret-1]);
        if (Qnl)
            fprintf (outstr,"\n");
    } else {
        x = (double *) allocate(nx * sizeof(double));
   
        nret = nemoinpd(getparam("expression"),x,nx);
        if (nret == -23)
            error("Too many items in list, use a bigger nmax=%d",nx);
        else if (nret < 0)
            error("nemoinp (%s) parsing error (%d)",cp,nret);

        for (i=0; i< nret-1; i++) {
            fprintf(outstr,fmt2,x[i]);
            if (Qnl)
                fprintf(outstr,"\n");
        }
        fprintf (outstr,fmt1,x[nret-1]);
        if (Qnl)
            fprintf (outstr,"\n");
    }
    if (!Qnl) fprintf(outstr,"\n");  /* always make sure to add 1 newline */

}
Beispiel #4
0
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);
}
Beispiel #5
0
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);
}