int main(int argc, char **argv) { float *f,*g; int npx=2046,npy=4090; float pixignr=0; double xcen=5000,ycen=4192; double xpos=-FLT_MAX,ypos=-FLT_MAX; double a[N+1]={0,A1,A2,A3,A4,A5,A6,A7}; int npx2,npy2,xshift,yshift; int i; char fnamin[255]=""; char fnamout[255]=""; char dtype[255]; int ignor=INT_MIN; float bzero=0.0,bscale=1.0; FILE *fpin,*fpout; struct imh imhin={""}, imhout={""}; struct icom icomin={0}, icomout={0}; int quickmode=0; /* default slow */ int keepSB=1; /* default keep SB, not flux */ /* for WCS */ double b11=1.0,b12=0.0,b21=0.0,b22=1.0; /***** parse options ******/ getargopt opts[20]; char *files[3]={NULL}; int n=0; int keepflux=0; files[0]=fnamin; files[1]=fnamout; n=0; setopts(&opts[n++],"-x=", OPTTYP_DOUBLE , &xpos, "chip offset x from the center(always needed)"); setopts(&opts[n++],"-y=", OPTTYP_DOUBLE , &ypos, "chip offset y from the center(always needed)"); setopts(&opts[n++],"-quick", OPTTYP_FLAG , &quickmode, "quick mode (default: quick)",1); setopts(&opts[n++],"-keepflux", OPTTYP_FLAG , &keepflux, "keep flux mode (default:keep SB)",1); /* setopts(&opts[n++],"-reverse", OPTTYP_FLAG , &reverse, "reverse(not impl.)",1); */ setopts(&opts[n++],"-a2=", OPTTYP_DOUBLE , &(a[2]), "a2(default:0)"); setopts(&opts[n++],"-a3=", OPTTYP_DOUBLE , &(a[3]), "a3(default:4.15755e-10)"); setopts(&opts[n++],"-a4=", OPTTYP_DOUBLE , &(a[4]), "a4(default:0)"); setopts(&opts[n++],"-a5=", OPTTYP_DOUBLE , &(a[5]), "a5(default:1.64531e-18)"); setopts(&opts[n++],"-a6=", OPTTYP_DOUBLE , &(a[6]), "a6(default:0)"); setopts(&opts[n++],"-a7=", OPTTYP_DOUBLE , &(a[7]), "a7(default:-4.62459e-26)"); setopts(&opts[n++],"-b11=", OPTTYP_DOUBLE , &(b11), "b11(default:1.0)"); setopts(&opts[n++],"-b12=", OPTTYP_DOUBLE , &(b12), "b12(default:0.0)"); setopts(&opts[n++],"-b21=", OPTTYP_DOUBLE , &(b21), "b21(defaulr:0.0)"); setopts(&opts[n++],"-b22=", OPTTYP_DOUBLE , &(b22), "b22(default:1.0)"); setopts(&opts[n++],"-bzero=", OPTTYP_FLOAT , &bzero, "bzero"); setopts(&opts[n++],"-bscale=", OPTTYP_FLOAT , &bscale, "bscale"); setopts(&opts[n++],"-dtype=", OPTTYP_STRING , dtype, "data_typ(FITSFLOAT,FITSSHORT...)"); setopts(&opts[n++],"-pixignr=", OPTTYP_FLOAT , &ignor, "pixignr value"); setopts(&opts[n++],"",0,NULL,NULL); if(parsearg(argc,argv,opts,files,NULL) ||fnamout[0]=='\0' || xpos==-FLT_MAX || ypos==-FLT_MAX) { print_help("Usage: distcorr5 [options] filein fileout", opts,""); exit(-1); } if(keepflux==1) keepSB=0; xcen=-xpos; ycen=-ypos; /* read FILE */ if ((fpin= imropen(fnamin,&imhin,&icomin))==NULL) { printf("File %s not found !!\n",fnamin); exit(-1); } pixignr=(float)imget_pixignr( &imhin ); npx=imhin.npx; npy=imhin.npy; f=(float*)malloc(npx*npy*sizeof(float)); imrall_tor(&imhin,fpin,f,npx*npy); /* 2002/04/01 */ /* only "quick" version is implemented */ g=distcorr_lintrans_quick3(f,npx,npy,pixignr,xcen,ycen,N,a, b11,b12,b21,b22, &npx2,&npy2,&xshift,&yshift,keepSB); /* replace pixignr */ imh_inherit(&imhin,&icomin,&imhout,&icomout,fnamout); imclose(fpin,&imhin,&icomin); imc_fits_set_dtype(&imhout,FITSFLOAT,bzero,bscale); if(ignor!=INT_MIN) { for(i=0;i<npx2*npy2;i++) if (g[i]==pixignr) g[i]=(float)ignor; imset_pixignr(&imhout,&icomout,ignor); } else { imset_pixignr(&imhout,&icomout,(int)pixignr); } imhout.npx=npx2; imhout.npy=npy2; imhout.ndatx=npx2; imhout.ndaty=npy2; imaddhistf(&icomout,"Distortion corrected with center=(%f,%f)",xcen,ycen); if (keepSB==1) imaddhistf(&icomout," keep-SB mode."); else imaddhistf(&icomout," keep-flux mode."); imaddhistf(&icomout," matrix=(%f,%f)(%f,%f)",b11,b12,b21,b22); imaddhistf(&icomout," coeff:a2=%e a3=%e a4=%e",a[2],a[3],a[4]); imaddhistf(&icomout," coeff:a5=%e a6=%e a7=%e",a[5],a[6],a[7]); /* WCS revise */ imc_shift_WCS(&icomout,(float)xshift,(float)yshift); /* write FILE */ if ((fpout= imwopen(fnamout,&imhout,&icomout))==NULL) { printf("Cannot open file %s !!\n",fnamout); exit(-1); } imwall_rto( &imhout, fpout, g ); imclose(fpout,&imhout,&icomout); free(g); return 0; }
int main(int argc, char *argv[]) { /* ----- declarations */ int i; /* do loop */ int ifx,ify; /* do loop for filter */ int xdim, ydim; /* x,y-pixel data size */ int pdim; /* total-pixel data size */ int fsize; /* filter size (pixel)*/ float *ipix; /* input pixel array */ float *opix; /* output pixel array */ float *gf; /* gaussian filter array */ struct imh imhin={""}; /* imh header structure */ struct icom icomin={0}; /* imh comment structure */ struct imh imhout={""}; /* imh header structure */ struct icom icomout={0}; /* imh comment structure */ /* for empty pixels (WHT) */ int pixignr; /* define struct for file pointers */ FILE *fpin,*fpout; char *fnamin,*fnamout; float s1in,s2in,a1in,a2in; float s1out,s2out,a1out,a2out; double A1,A2,A3,A4,S1,S2,S3,S4,d,v; if(argc!=9) { printf("Usage: dsmth file_in s1in s2in a1in file_out s1out s2out a2out \n"); exit(-1); } else { fnamin=argv[1]; s1in=atof(argv[2]); s2in=atof(argv[3]); a1in=atof(argv[4]); a2in=1.0-a1in; fnamout=argv[5]; s1out=atof(argv[6]); s2out=atof(argv[7]); a1out=atof(argv[8]); a2out=1.0-a1out; } if ((fpin = imropen(fnamin, &imhin, &icomin)) == NULL) { fprintf(stderr,"\a\n Cannot open input file %s\n",fnamin); exit(1); } xdim = imhin.npx; ydim = imhin.npy; pdim = xdim * ydim; ipix = (float*)calloc(pdim, sizeof(float)); printf("\n> %s reading\n",fnamin); if (imrall_tor(&imhin, fpin, ipix, pdim) == 0) { fprintf(stderr,"\a\n Cannot read file \n"); exit(1); } pixignr = imget_pixignr( &imhin ); printf("smth: %s PixIgnor= %d\n",fnamin,pixignr); /* ----- make a smoothing filter */ /************ FILTER making up! **************/ /* Function is \Sum An/sqrt(Sn)*exp(-0.5*r^2/Sn) */ A1=(a1out*s1out)/(a1in*s1in); A2=(a2out*s2out)/(a1in*s1in); A3=-(a1out*s1out)/(a1in*s1in)*(a2in*s2in)/(a1in*s1in); A4=-(a2out*s2out)/(a1in*s1in)*(a2in*s2in)/(a1in*s1in); S1=(s1out*s1out-s1in*s1in); S2=(s2out*s2out-s1in*s1in); S3=(s1out*s1out+s2in*s2in)-2.0*(s1in*s1in); S4=(s1out*s1out+s2in*s2in)-2.0*(s1in*s1in); d=(a2in*s2in)/(a1in*s1in)*exp(-M_PI*S1); if(S1<0 || S2<0 || S3<0 || S4<0 || d*d>0.05 /* <---- */ ) { printf("Cannot smooth, we need deconvolution! \n"); exit(-1); } printf("%f %f %f %f %f\n",A1,A2,A3,A4,A1+A2+A3+A4); printf("%f %f %f %f \n",S1,S2,S3,S4); /* renormalize */ v=A1+A2+A3+A4; A1/=v; A2/=v; A3/=v; A4/=v; printf("%f %f %f %f %f\n",A1,A2,A3,A4,A1+A2+A3+A4); printf("%f %f %f %f \n",S1,S2,S3,S4); /* fsize(=2n+1), gf[] */ for(i=1;i<101;i++) { v=gauss4(A1,S1,A2,S2,A3,S3,A4,S4,(double)i); if (v*PTPARAM<1.) break; } fsize=2*i+1; imh_inherit(&imhin,&icomin,&imhout,&icomout,fnamout); imclose(fpin,&imhin,&icomin); if (fsize == 1) { printf("\a\n *input seeing is near to output one \n"); printf(" there is no need to smooth the image \n"); opix=ipix; } else { printf(" size = %d pix\n",fsize); /* Make filter */ gf = (float*) gauss4flt(A1,S1,A2,S2,A3,S3,A4,S4,fsize); printf("\n> filter map \n"); for (ify=0; ify<fsize; ify++) { for (ifx=0; ifx<fsize; ifx++) { printf(" %7.5f",gf[ify*fsize+ifx]); } printf("\n"); } /* ----- smoothing the file */ printf("\n> %s smoothing\n",fnamin); opix = (float*) calloc(pdim, sizeof(float)); /* Convolve */ /* 2002/06/06 replaced with convolve2 */ convolve2(xdim,ydim,ipix, opix, fsize, gf,pixignr); free(ipix); free(gf); /* ----- write output file */ /* 2002/06/06 updated */ /* (void) sprintf( line, "DSMTH: filter size=%d",fsize); (void) imaddcom( &icomout, line ); */ imaddhistf(&icomout,"DSMTH: (%f %f %f) => (%f %f %f)", s1in,s2in,a1in,s1out,s2out,a1out); imaddhistf(&icomout,"DSMTH: filter size=%d",fsize); } if( (fpout=imwopen(fnamout, &imhout, &icomout)) == NULL ) { fprintf(stderr,"Cannot open output file \n"); exit(1); } printf("\n> %s writing --------------\n",fnamout); imwall_rto(&imhout, fpout, opix); imclose(fpout,&imhout,&icomout); free(opix); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ return 0; }
int main( int argc, char *argv[] ) { FILE *fp_in, *fp_out; float *data; float *frame; int i, j, /* do loop for (px,py) */ ncol, /* image size (X) (pix) */ nrow; /* image size (Y) (pix) */ char *input,*output; double x1,y1,x2,y2,width; double a,b,d; double b_low,b_up; float blank=-32768.; struct imh imhin={""},imhout={""}; struct icom icomin={0},icomout={0}; /*---- get argv[] ------------------------------------------*/ if( argc != 9 ) { (void) printf("usage: line_blank [input image] "); (void) printf("[x1] [y1] [x2] [y2] [width] "); (void) printf("[blank_value(SPCAM=-32768)] [output image]\n"); (void) exit(-1); } /* (void) strcpy( input, argv[1] ); (void) strcpy( output, argv[8] ); */ input=argv[1]; output=argv[8]; x1 = atoi(argv[2]); y1 = atoi(argv[3]); x2 = atoi(argv[4]); y2 = atoi(argv[5]); width = atoi(argv[6]); blank = atof(argv[7]); /*---- read the image ---------------------------------------------*/ if( (fp_in = imropen( input, &imhin, &icomin )) == NULL ) { (void) printf(" ERROR: Can't open %s.\n", input ); (void) printf(" Suffix of image must be either .pix or .fits.\n"); (void) exit(-1); } ncol = imhin.npx; nrow = imhin.npy; #if 0 (void) printf(" nrow = %4d\n", nrow ); (void) printf(" ncol = %4d\n", ncol ); #endif /*---- malloc -----------------------------------------------*/ /* data = (float *)malloc( ncol*sizeof(float) ); */ frame = (float *)malloc( ncol*nrow*sizeof(float) ); /*---- processing the data ----------------------------------*/ /* store the data */ /* for( j=0; j<nrow; j++ ) { imrl_tor( &imhin, fp_in, data, j ); for( i=0; i<ncol; i++ ) { frame[ncol*j+i] = data[i]; } } */ imrall_tor(&imhin,fp_in,frame,ncol*nrow); /* calculate coefficients of lines which surround the region */ if( (x1-x2)==0 ) { fprintf(stderr,"ERROR: cannot define a, since x1-x2=0 \n"); exit(-1); } a=(y1-y2)/(x1-x2); b=y1-a*x1; d=width/2.; b_low=b-d*sqrt(a*a+1.); b_up=b+d*sqrt(a*a+1.); /* substitute the blank value in the specified region */ for( j=0; j<nrow; j++ ) { for( i=0; i<ncol; i++ ) { if ( ( (j+1)-(a*(i+1)+b_low) >= 0 ) && ( (j+1)-(a*(i+1)+b_up) <= 0)) frame[ncol*j+i] = (float)blank; } } /* for( k=0; k<rec; k++) { for( j=0; j<nrow; j++ ) { for( i=0; i<ncol; i++ ) { if ( ( i+1 >= ncol_min[k] ) && ( i+1 <= ncol_max[k] ) && ( j+1 >= nrow_min[k] ) && ( j+1 <= nrow_max[k] ) && ( frame[ncol*j+i] <= thres ) ) frame[ncol*j+i] = blank; } } } */ /*---- write an output image ----------------------------------*/ imh_inherit(&imhin,&icomin,&imhout,&icomout,output); imclose(fp_in,&imhin,&icomin); imhout.npx = ncol; imhout.npy = nrow; imhout.dtype = DTYPFITS; (void) imc_fits_set_dtype( &imhout, FITSFLOAT, 0.0, 1.0 ); if( (fp_out = imwopen_fits( output, &imhout, &icomout )) == NULL ) { (void) printf(" ERROR: Can't open %s.\n", output ); (void) exit(-1); } /* for( j=0; j<nrow; j++ ) { for( i=0; i<ncol; i++ ) { data[i] = (float)(frame[ncol*j+i]); } imc_fits_wl( &imhout, fp_out, data, j ); } */ imwall_rto(&imhout, fp_out, frame); (void) free(frame); imclose(fp_out,&imhout,&icomout); return 0 ; }
int main(int argc,char **argv) { float fact,fsigm; float gmax,gmean,gmin,gsd; int npx,npy,meshsiz_x,meshsiz_y; int nmesh_x=0,nmesh_y=0,mesh=0; int ncycle; char fnamin[BUFSIZ]=""; char fnamout[BUFSIZ]=""; struct icom icomin={0},icomout={0},icomref={0}; struct imh imhin={""},imhout={""},imhref={""}; int i,j; float *g,*g2; float *rmesh; float *sgmesh; char fnamref[BUFSIZ]=""; char buffer[BUFSIZ]; char skymap[BUFSIZ]=""; float bzero=0.,bscale=1.; float bzero_new=FLT_MIN,bscale_new=FLT_MIN; int bzeroset=0; int fitsdtype; FILE *fpin,*fpout,*fpref; FILE *fmap; int mode; char dtype[80]=""; float msky,msigm; int dtypein; int binflag; float ignor=(float)INT_MIN; float imax=(float)INT_MIN; float imin=(float)INT_MAX; int pixignr; getargopt opts[20]; char *files[3]={NULL}; int n=0; int helpflag; /***************************************/ files[0]=fnamin; files[1]=fnamout; setopts(&opts[n++],"-mesh=", OPTTYP_INT , &mesh, "mesh size (default:full image)"); setopts(&opts[n++],"-meshx=", OPTTYP_INT , &nmesh_x, "mesh x-size (default:not used)"); setopts(&opts[n++],"-meshy=", OPTTYP_INT , &nmesh_y, "mesh y-size (default:not used)"); setopts(&opts[n++],"-skyref=", OPTTYP_STRING , &fnamref, "sky determination reference (default:use input image)"); setopts(&opts[n++],"-imax=", OPTTYP_FLOAT , &imax, "flux upper limit to estimate sky(default:unlimited)"); setopts(&opts[n++],"-imin=", OPTTYP_FLOAT , &imin, "flux lower limit to estimate sky(default:unlimited)"); /* */ setopts(&opts[n++],"-bzero=", OPTTYP_FLOAT , &bzero_new, "bzero"); setopts(&opts[n++],"-bscale=", OPTTYP_FLOAT , &bscale_new, "bscale"); setopts(&opts[n++],"-dtype=", OPTTYP_STRING , dtype, "datatyp(FITSFLOAT,FITSSHORT...)"); setopts(&opts[n++],"-pixignr=", OPTTYP_FLOAT , &ignor, "pixignr value"); /* To be implemented in next major version */ /* setopts(&opts[n++],"-fit", OPTTYP_FLAG , &fit_param, "fitting (default:no)",1); */ setopts(&opts[n++],"",0,NULL,NULL); helpflag=parsearg(argc,argv,opts,files,NULL); if(fnamout[0]=='\0') { fprintf(stderr,"Error: No input file specified!!\n"); helpflag=1; } if(helpflag==1) { print_help("Usage: skysb3b <options> [filein] [fileout]", opts, ""); exit(-1); } if(nmesh_x==0) nmesh_x=mesh; if(nmesh_y==0) nmesh_y=mesh; if(bzero_new!=FLT_MIN) { bzero=bzero_new; bzeroset=1; } if(bscale_new!=FLT_MIN) { bscale=bscale_new; bzeroset=1; } /* printf("debug:nmesh_x=%d nmesh_y=%d\n",nmesh_x,nmesh_y); */ mode=1; /* default */ fsigm=70.0; /* default */ /* *** DATA INPUT *** */ /**** read image ****/ fprintf(stderr,"%s\n",fnamin); if ((fpin= imropen(fnamin,&imhin,&icomin))==NULL) { printf("File %s not found !!\n",fnamin); exit(-1); } pixignr=(int)imget_pixignr( &imhin ); printf("pixignr_in=%f\n",(float)pixignr); if(imhin.dtype==DTYPFITS) { imc_fits_get_dtype( &imhin, &dtypein, NULL, NULL, NULL); if(dtypein==FITSFLOAT) binflag=0; else binflag=1; } else { if(imhin.dtype==DTYPR4 || imhin.dtype==DTYPR8) binflag=0; else binflag=1; } npx=imhin.npx; npy=imhin.npy; if(nmesh_x<1) nmesh_x=npx; if(nmesh_y<1) nmesh_y=npy; g=(float*)malloc(sizeof(float)*npx*npy); g2=(float*)malloc(sizeof(float)*npx*npy); if (g==NULL||g2==NULL) { printf("Error cannot allocate image buffer"); exit(-1);} if(imrall_tor(&imhin,fpin,g2,npx*npy)) printf("npx, npy =%d %d \n",npx,npy); else { printf("Error. cannot read data.\n"); exit(-1); } /* 2002/05/21 */ if(fnamref[0]=='\0') { memcpy(g,g2,sizeof(float)*npx*npy); } else { if ((fpref=imropen(fnamref,&imhref,&icomref))==NULL) { printf("Reference File %s not found !!\n",fnamref); exit(-1); } if (npx!=imhref.npx||npy!=imhref.npy) { printf("Error: Ref. size (%dx%d) differ from input (%dx%d)\n", imhref.npx,imhref.npy,npx,npy); exit(-1); } if(! imrall_tor(&imhref,fpref,g,npx*npy)) { printf("Error: cannot read ref data.\n"); exit(-1); } imclose(fpref,&imhref,&icomref); } /* 2001/07/19 preformat (masking) */ if(imax>(float)INT_MIN||imin<(float)INT_MAX) for(i=0;i<npx*npy;i++) if(g[i]>imax||g[i]<imin) g[i]=pixignr; /* *** SATAISTICS */ printf("Calculating statistics \n"); /* TODO */ /* to be tunable ?*/ fact=2.0; ncycle=2; statsk(g,npx,npy,fact,ncycle,&gmax,&gmin,&gmean,&gsd,pixignr); printf("\n"); printf(" => GMAX GMIN GMEAN GSD= %f %f %f %f \n",gmax,gmin,gmean,gsd); /* *** SKY DETERMINATION AND SUBTRACTION *** */ printf("Sky subtraction under way\n"); meshsiz_x=(int)((npx*2.)/nmesh_x)-1; /* meshsiz_x > 0 */ if(meshsiz_x<1) meshsiz_x=1; meshsiz_y=(int)((npy*2.)/nmesh_y)-1; /* meshsiz_y > 0 */ if(meshsiz_y<1) meshsiz_y=1; rmesh=(float*)malloc(sizeof(float)*meshsiz_x*meshsiz_y); sgmesh=(float*)malloc(sizeof(float)*meshsiz_x*meshsiz_y); /* 97/09/12 Yagi */ skydet2b(g,npx,npy,rmesh,meshsiz_x,meshsiz_y, nmesh_x,nmesh_y,gmean,gsd,fsigm,sgmesh,binflag,pixignr); if(meshsiz_x*meshsiz_y>2) { /* 3X3 median filter */ /* msky=skysigmadet(rmesh,meshsiz_x,meshsiz_y); */ msky=medfilter_skysb (rmesh,meshsiz_x,meshsiz_y,1); msigm=skysigmadet(sgmesh,meshsiz_x,meshsiz_y); } else if (meshsiz_x*meshsiz_y==2) { msky=0.5*(rmesh[0]+rmesh[1]); msigm=0.5*(sgmesh[0]+sgmesh[1]); } else if(meshsiz_x*meshsiz_y==1) { msky=rmesh[0]; msigm=sgmesh[0]; } else { msky=0;/* Error */ msigm=-1.0; /* Error */ } printf("SSB-EST %g\n",msky); printf("SSGM-EST %g\n",msigm); /* Write skymap to file*/ if(skymap[0]!='\0') { if(skymap[0]=='-') { fmap=stdout; printf("mapfile:stdout\n"); fprintf(stderr,"mapfile:stdout\n"); } else { printf("mapfile:%s\n",skymap); fprintf(stderr,"mapfile:%s\n",skymap); fmap=fopen(skymap,"a"); } if(fmap==NULL) { printf("Cannot write to \"%s\" -- writeing to stdout.\n",skymap); fprintf(stderr, "Cannot write to \"%s\" -- writeing to stdout.\n",skymap); fmap=stdout; } fprintf(fmap,"# %s\n",fnamin); fprintf(fmap,"# %d %d # (mesh size)\n",nmesh_x,nmesh_y); fprintf(fmap,"# %d %d # meshsiz_x meshsiz_y\n",meshsiz_x,meshsiz_y); fprintf(fmap,"# %g %g # msky msigma\n",msky,msigm); for(j=0;j<meshsiz_y;j++) { for(i=0;i<meshsiz_x;i++) { fprintf(fmap,"%d %d %6.2f\n",i,j,rmesh[i+meshsiz_x*j]); } } if(fmap!=stdout) fclose(fmap); } /* *** prepare for output *** */ /* Now sky subtract */ skysub(g2,npx,npy,rmesh,meshsiz_x,meshsiz_y, nmesh_x,nmesh_y,mode,pixignr); free(rmesh); free(sgmesh); statsk(g2,npx,npy,fact,ncycle,&gmax,&gmin,&gmean,&gsd,pixignr); /***** set output format *******/ fprintf(stderr,"%s\n",fnamout); imh_inherit(&imhin,&icomin,&imhout,&icomout,fnamout); imclose(fpin,&imhin,&icomin); if (imhout.dtype==DTYPFITS) { if(dtype[0]=='\0'||(fitsdtype=imc_fits_dtype_string(dtype))==-1) (void)imc_fits_get_dtype( &imhout, &fitsdtype, NULL, NULL, NULL); /* 2000/07/02 */ if(bzeroset!=1) (void)imc_fits_get_dtype( &imhout, NULL, &bzero, &bscale, NULL); /* re-set bzero & bscale */ if(imc_fits_set_dtype(&imhout,fitsdtype,bzero,bscale)==0) { printf("Error\nCannot set FITS %s\n",fnamout); printf("Type %d BZERO %f BSCALE %f\n",fitsdtype,bzero,bscale); exit(-1); } } else { if (strstr(dtype,"I2")) { imhout.dtype=DTYPI2; } else if (strstr(dtype,"INT")) { imhout.dtype=DTYPINT; } else if (strstr(dtype,"I4")) { imhout.dtype=DTYPI4; } else if (strstr(dtype,"R4")) { imhout.dtype=DTYPR4; } else if (strstr(dtype,"R8")) { imhout.dtype=DTYPR8; } else { imhout.dtype=DTYPU2; imset_u2off(&imhout,&icomout,500); } } if(ignor!=(float)INT_MIN) { imset_pixignr(&imhout, &icomout, (int)ignor); /* replace pixignr */ for(i=0;i<imhout.npx*imhout.npy;i++) if(g2[i]==(float)pixignr) g2[i]=(float)ignor; pixignr=ignor; } else { imset_pixignr(&imhout, &icomout, pixignr); } /**** output image ****/ imaddhistf(&icomout, "Sky subtracted by skysb3b, mesh %3dx%3d sky%7.2f+%7.2f", nmesh_x,nmesh_y,msky,msigm); if (imget_fits_value(&icomout, "SSBMSH-I", buffer )==0) { imupdate_fitsf(&icomout, "SSBMSH-I", "%20d / %-46.46s", nmesh_x , "SKY MESH X PIXEL" ); imupdate_fitsf(&icomout, "SSBMSH-J", "%20d / %-46.46s", nmesh_y , "SKY MESH Y PIXEL" ); imupdate_fitsf(&icomout, "SSB-EST", "%20.4f / %-46.46s", msky, "TYPICAL SKY ADC ESTIMATED" ); imupdate_fitsf(&icomout, "SSGM-EST", "%20.4f / %-46.46s", msigm, "TYPICAL SKY SIGMA ADC ESTIMATED" ); } if ((fpout= imwopen(fnamout,&imhout,&icomout))==NULL) { printf("Cannot open file %s !!\n",fnamout); exit(-1); } imwall_rto( &imhout, fpout, g2 ); free(g); imclose(fpout,&imhout,&icomout); /* printf("\n"); printf(" --- skysb3b PROGRAM ENDED ---\n"); */ return 0; }
int main(int argc,char *argv[]) { struct imh imhin={""},imhout={""}; struct icom icomin={0},icomout={0}; char fnamin[100]=""; char fnamout[100]=""; char tmp[100]=""; FILE *fp,*fp2; float *f,*g,*h; int i; int xmin=-1,xmax=-1,ymin=-1,ymax=-1; int ymin2,ymax2; int offst; float bzero=0,bscale=1.0,bzero_new=FLT_MIN,bscale_new=FLT_MIN; float blank_new=FLT_MIN; float pixignr=INT_MIN; int fitsdtype=-1; int npx,npy,npx2,npy2,npxos,npyos,npxef[NREGION],npyef[NREGION]; int npxef0=0; int xflip,yflip; int region; char regid[2],key[10],comment[80]; int efminx[NREGION],efmaxx[NREGION],efminy[NREGION],efmaxy[NREGION]; int osminx[NREGION],osmaxx[NREGION],osminy[NREGION],osmaxy[NREGION]; int j=0; char dtype[BUFSIZ]=""; int notrim=0; int no_y=0; /* test */ int flag_norm=0; float gain[NREGION]={0}; int fit_param=-1; getargopt opts[20]; char *files[3]={NULL}; int nopt=0; int helpflag; files[0]=fnamin; files[1]=fnamout; /* only trim version is supported now */ setopts(&opts[nopt++],"-notrim", OPTTYP_FLAG , ¬rim, "not trim off(default:trim)",1); setopts(&opts[nopt++],"-no-y", OPTTYP_FLAG , &no_y, "not subtract y-overscan(default:subtract)",1); setopts(&opts[nopt++],"-bzero=", OPTTYP_FLOAT , &bzero_new, "bzero"); setopts(&opts[nopt++],"-bscale=", OPTTYP_FLOAT , &bscale_new, "bscale"); setopts(&opts[nopt++],"-dtype=", OPTTYP_STRING , dtype, "datatyp(FITSFLOAT,FITSSHORT...)"); setopts(&opts[nopt++],"-pixignr=", OPTTYP_FLOAT , &blank_new, "pixignr value"); setopts(&opts[nopt++],"",0,NULL,NULL); helpflag=parsearg(argc,argv,opts,files,NULL); if(fnamout[0]=='\0') { fprintf(stderr,"Error: No input file specified!!\n"); helpflag=1; } if(helpflag==1) { print_help("Usage: osmed4 <options> [filein] [fileout]", opts, ""); exit(-1); } /* * ... Open Input */ (void)printf("\n Input = %s\n",fnamin ); if( (fp = imropen ( fnamin, &imhin, &icomin )) == NULL ) { print_help("Usage: %s <options> [filein] [fileout]", opts, ""); exit(-1); } npx=imhin.npx; npy=imhin.npy; /* get information here */ npx2=0; npy2=0; npxos=0; npyos=0; imget_fits_value(&icomin,"S_XFLIP",tmp); printf("debug: xflip [%s]\n",tmp); if (tmp[0]=='T') xflip=1; else xflip=0; imget_fits_value(&icomin,"S_YFLIP",tmp); if (tmp[0]=='T') yflip=1; else yflip=0; xmin=npx; xmax=-1; ymin=npy; ymax=-1; ymin2=npy; ymax2=-1; for(region=0;region<NREGION;region++) { if (xflip==0) sprintf(regid,"%1d",region+1); else sprintf(regid,"%1d",NREGION-region); sprintf(key,"S_EFMN%c1",regid[0]); imget_fits_int_value(&icomin,key,&(efminx[region])); sprintf(key,"S_EFMX%c1",regid[0]); imget_fits_int_value(&icomin,key,&(efmaxx[region])); sprintf(key,"S_EFMN%c2",regid[0]); imget_fits_int_value(&icomin,key,&(efminy[region])); sprintf(key,"S_EFMX%c2",regid[0]); imget_fits_int_value(&icomin,key,&(efmaxy[region])); efminx[region]--; efmaxx[region]--; efminy[region]--; efmaxy[region]--; printf("debug:EF: %d %d %d %d\n", efminx[region],efmaxx[region],efminy[region],efmaxy[region]); if(efmaxx[region]<efminx[region]) exit(-1); if(efmaxy[region]<efminy[region]) exit(-1); npxef[region]=efmaxx[region]-efminx[region]+1; npyef[region]=efmaxy[region]-efminy[region]+1; if(xmin>efminx[region]) xmin=efminx[region]; if(xmax<efmaxx[region]) xmax=efmaxx[region]; if(ymin>efminy[region]) ymin=efminy[region]; if(ymax<efmaxy[region]) ymax=efmaxy[region]; if(ymin2>efminy[region]) ymin2=efminy[region]; if(ymax2<efmaxy[region]) ymax2=efmaxy[region]; printf("debug: npx2=%d\n",npx2); npx2+=npxef[region]; printf("debug: region %d: %d x %d \n", region,npxef[region],npyef[region]); sprintf(key,"S_OSMN%c1",regid[0]); imget_fits_int_value(&icomin,key,&(osminx[region])); sprintf(key,"S_OSMX%c1",regid[0]); imget_fits_int_value(&icomin,key,&(osmaxx[region])); sprintf(key,"S_OSMN%c2",regid[0]); imget_fits_int_value(&icomin,key,&(osminy[region])); sprintf(key,"S_OSMX%c2",regid[0]); imget_fits_int_value(&icomin,key,&(osmaxy[region])); osminx[region]--; osmaxx[region]--; osminy[region]--; osmaxy[region]--; printf("debug:OS: %d %d %d %d\n", osminx[region],osmaxx[region],osminy[region],osmaxy[region]); if(ymin2>osminy[region]) ymin2=osminy[region]; if(ymax2<osmaxy[region]) ymax2=osmaxy[region]; if (npxos<osmaxx[region]-osminx[region]+1) npxos=osmaxx[region]-osminx[region]+1; if (flag_norm==1) { sprintf(key,"S_GAIN%c",regid[0]); imget_fits_float_value(&icomin,key,&(gain[region])); } if (npyos<osmaxy[region]-osminy[region]+1) npyos=osmaxy[region]-osminy[region]+1; } npy2=ymax-ymin+1; printf("debug: y %d-%d\n",ymin,ymax); printf("debug: effective size: %dx%d\n",npx2,npy2); /* output */ imh_inherit(&imhin,&icomin,&imhout,&icomout,fnamout); if(!notrim) { imhout.npx = npx2; imhout.npy = npy2; imhout.ndatx = imhout.npx; imhout.ndaty = imhout.npy; } else { imhout.npx = npx; imhout.npy = npy; imhout.ndatx = imhout.npx; imhout.ndaty = imhout.npy; } if(dtype[0]=='\0') { imc_fits_get_dtype( &imhout, &fitsdtype, &bzero, &bscale, &offst ); /** force FLOAT **/ fitsdtype=FITSFLOAT; } else if ((fitsdtype=imc_fits_dtype_string(dtype))==-1) { printf("\nWarning: Unknown fits dtype %s. Inheriting original.\n",dtype); imc_fits_get_dtype( &imhout, &fitsdtype, &bzero, &bscale, &offst ); } if(bzero_new!=FLT_MIN) { bzero=bzero_new; } else bzero=0; if(bscale_new!=FLT_MIN) { bscale=bscale_new; } else bscale=1.0; (void)imc_fits_set_dtype( &imhout, fitsdtype, bzero, bscale ); if(blank_new!=FLT_MIN) imset_pixignr(&imhout,&icomout,(int)blank_new); else { pixignr=(float)imget_pixignr(&imhin); imset_pixignr(&imhout,&icomout,(int)pixignr); } if (fit_param>0) { imaddhist(&icomout,"OSMED5: Overscan value is smoothed"); } else { imaddhist(&icomout,"OSMED5: X-Overscan median is subtracted line by line."); if(no_y==0) imaddhist(&icomout,"OSMED5: Y-Overscan median is subtracted row by row."); } if (!notrim) { imaddhistf(&icomout,"OSMED5: And trimmed"); } (void)printf("\n Output = %s\n",fnamout ); if(!notrim) { imupdate_fitsf(&icomout, "EFP-MIN1",IMC_FITS_INTFORM, 1, "Start position of effective frame in axis-1"); imupdate_fitsf(&icomout, "EFP-MIN2",IMC_FITS_INTFORM, 1, "Start position of effective frame in axis-2"); imupdate_fitsf(&icomout, "EFP-RNG1",IMC_FITS_INTFORM, npx2, "Range of effective frame in axis-1"); imupdate_fitsf(&icomout, "EFP-RNG2",IMC_FITS_INTFORM, npy2, "Range of effective frame in axis-2"); imc_shift_WCS(&icomout,(float)xmin,(float)ymin); /* 2008/08/28 header revision more */ /* tenuki kimeuchi */ /* ch1->ch4 ? ch4->ch1?? */ /* tenuki again */ npxef0=0; for (i=0;i<NREGION;i++) { if (xflip==0) j=i+1; else j=NREGION-i; sprintf(key,"S_EFMN%d1",j); sprintf(comment,"MIN pixel of x-effective range for ch%d",j); imupdate_fitsf(&icomout,key,IMC_FITS_INTFORM, npxef0+1,comment); sprintf(key,"S_EFMX%d1",j); sprintf(comment,"MAX pixel of x-effective range for ch%d",j); imupdate_fitsf(&icomout,key,IMC_FITS_INTFORM, npxef0+npxef[i],comment); npxef0+=npxef[i]; sprintf(key,"S_EFMN%d2",j); sprintf(comment,"MIN pixel of y-effective range for ch%d",j); imupdate_fitsf(&icomout,key,IMC_FITS_INTFORM, 1,comment); sprintf(key,"S_EFMX%d2",j); sprintf(comment,"MAX pixel of y-effective range for ch%d",j); imupdate_fitsf(&icomout,key,IMC_FITS_INTFORM, npy2,comment); } } f=(float*)malloc(npx*npy*sizeof(float)); imrall_tor(&imhin, fp, f, npx*npy ); imclose(fp,&imhin,&icomin); g=(float*)calloc(npx*npy,sizeof(float)); /* overscan estimation and subtract */ for(region=0;region<NREGION;region++) { overscansub_x(npx,npy,f,g, efminx[region],efmaxx[region], osminx[region],osmaxx[region], ymin2,ymax2); if(no_y==0) { /* 2008/07/26 Overscan Y added*/ overscansub_y(npx,npy,g,f, efminy[region],efmaxy[region], osminy[region],osmaxy[region], efminx[region],efmaxx[region]); } } if(no_y!=0) memcpy(f,g,npx*npy*sizeof(float)); if( (fp2 = imwopen( fnamout, &imhout, &icomout )) == NULL ) { print_help("Usage: %s <options> [filein] [fileout]", opts, ""); exit(-1); } if(notrim) { imwall_rto( &imhout, fp2, h); } else { /* copy effective regions to g */ i=0; for(region=0;region<NREGION;region++) { for(j=ymin;j<=ymax;j++) { memcpy(g+i+(j-ymin)*npx2,f+efminx[region]+j*npx, (efmaxx[region]-efminx[region]+1)*sizeof(float)); } i+=(efmaxx[region]-efminx[region]+1); } imwall_rto( &imhout, fp2, g); } (void) imclose(fp2,&imhout,&icomout); return 0; }
int main(int argc, char *argv[]) { /* ----- declarations */ char line[LMAX]; /* input line*/ int ifx,ify; /* do loop for filter */ int ix,iy; /* do loop for image */ int ip; /* iy*xdim+ix */ int xdim, ydim; /* x,y-pixel data size */ int pdim; /* total-pixel data size */ int fsize; /* filter size (pixel)*/ float *ipix; /* input pixel array */ float *opix=NULL; /* output pixel array */ float *opix2=NULL; /* output pixel array */ float gsgm; /* gaussian sigma */ float *gf; /* gaussian filter array */ struct imh imhin={""}; /* imh header structure */ struct icom icomin={0}; /* imh comment structure */ struct imh imhout={""}; /* imh header structure */ struct icom icomout={0}; /* imh comment structure */ /* For empty pixels (WHT) */ int pixignr; FILE *fpin, *fpout; char *fnamin,*fnamout; if(argc!=4) { printf("Usage: smth2 file_in filtersize file_out\n"); exit(-1); } else { fnamin=argv[1]; gsgm=atof(argv[2]); fnamout=argv[3]; } if ((fpin = imropen(fnamin, &imhin, &icomin)) == NULL) { fprintf(stderr,"\7\n Cannot open input file %s\n",fnamin); exit(1); } xdim = imhin.npx; ydim = imhin.npy; pdim = xdim * ydim; ipix = (float*)calloc(pdim, sizeof(float)); if(ipix==NULL) { fprintf(stderr,"Cannot allocate ipix\n"); exit(-1); } printf("\n> %s reading\n",fnamin); if (imrall_tor(&imhin, fpin, ipix, pdim) == 0) { fprintf(stderr,"\7\n Cannot read file \n"); exit(1); } pixignr = imget_pixignr( &imhin ); printf("smth: %s PixIgnor= %d\n",fnamin,pixignr); /* ----- make a smoothing filter */ fsize= 2*(int)ceil(gsgm*sqrt(log(PTPARAM)))-1; imh_inherit(&imhin,&icomin,&imhout,&icomout,fnamout); imclose(fpin,&imhin,&icomin); if (fsize == 1) { printf("\7\n *input seeing is near to output one \n"); printf(" there is no need to smooth the image \n"); opix2=ipix; } else { printf("\n> gaussian filter sgm = %f pix\n",gsgm); printf(" size = %d pix\n",fsize); gf = (float*) gausflt_1d(gsgm, fsize); printf("\n> filter map \n"); for (ifx=0; ifx<fsize; ifx++) { printf(" %7.5f",gf[ifx]); } /* ----- smoothing the file */ /* Y axis */ printf("\n> %s smoothing\n",fnamin); opix=(float*) calloc(pdim, sizeof(float)); if(opix==NULL) { fprintf(stderr,"Cannot allocate opix\n"); exit(-1); } for (iy=0; iy<ydim; iy++) { for (ix=0; ix<xdim; ix++) { ip=iy*xdim+ix; if (ipix[ip]==pixignr) { opix[ip]=ipix[ip]; } else if ((iy-(fsize-1)/2) < 0 || (iy+(fsize-1)/2) >= ydim || ipix[ip-(fsize-1)/2*xdim]==pixignr || ipix[ip+(fsize-1)/2*xdim]==pixignr) { opix[ip]=(float) edge_y_1d(ix,iy,fsize,xdim,ydim,ipix,gf,pixignr); } else { for (ify=0; ify<fsize; ify++) { opix[ip]+=ipix[(iy-(fsize-1)/2+ify)*xdim+ix]*gf[ify]; } } } } opix2=(float*)calloc(pdim, sizeof(float)); if(opix2==NULL) { fprintf(stderr,"Cannot allocate opix2\n"); exit(-1); } for (iy=0; iy<ydim; iy++) { for (ix=0; ix<xdim; ix++) { ip=iy*xdim+ix; if (opix[ip]==pixignr) opix2[ip]=pixignr; else if ((ix-(fsize-1)/2) < 0 || (ix+(fsize-1)/2) >= xdim || opix[ip-(fsize-1)/2]==pixignr || opix[ip+(fsize-1)/2]==pixignr) { opix2[ip]=(float) edge_x_1d(ix,iy,fsize,xdim,ydim,opix,gf,pixignr); } else for (ifx=0; ifx<fsize; ifx++) { opix2[ip]+=opix[iy*xdim+(ix-(fsize-1)/2+ifx)]*gf[ifx]; } } } free(ipix); free(gf); /* ----- write output file */ (void) sprintf( line, "SMTH2_quick: filter sgm=%4.2f: size=%d",gsgm,fsize); (void) imaddcom( &icomin, line ); } /* (void) sprintf( line, " original sgm=%4.2f: resulted sgm=%4.2f", isee[nflm],osee); (void) imaddcom( &icomin, line ); */ if( (fpout=imwopen(fnamout, &imhout, &icomout)) == NULL ) { fprintf(stderr,"Cannot open output file \n"); exit(1); } printf("\n> %s writing --------------\n",fnamout); imwall_rto(&imhout, fpout, opix2); fclose(fpout); if(opix!=NULL) { free(opix); free(opix2); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ return 0; }