Commit 8df06b27 authored by Fang Yuedong's avatar Fang Yuedong
Browse files

Merge branch 'sim_scheduler' into develop

parents 81e2570f 93270bbf
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nrutil.h"
#include <time.h>
float poidev(float x, long *idum);
void creattraps(long *seed,int ntrap,int ny,int nmax,float c,float beta,float **sp);
void addCTI(int *a0,int ny,int noverscan,int nsp,float beta,float w, float c,float *t, float ***ntrap, int *acti, int release_seed,float *randv);
//int read_fits_2D(const char *argv,float *galaxy,int imagesize);
//int write_fits_2D(const char *argv,float **stars,int *dim);
float ran1(long *idum);
void CTI_simul(int **image, int nx, int ny, int noverscan, int nsp, float *rho_trap, float *t, float beta, float w, float c, int nmax, long *trap_seeds, int release_seed,int **imagecti){
int ntotal;
printf("image parameters: nx=%i ny=%i noverscan=%i\n",nx,ny,noverscan);
printf("input image: image[0][0]=%i image[50][60]=%i image[1000][20]=%i image[20][1000]=%i\n",image[0][0],image[50][60],image[1000][20],image[20][1000]);
printf("rho_trap rho1=%f rho2=%f rho3=%f\n",rho_trap[0],rho_trap[1],rho_trap[2]);
printf("t t1=%f t2=%f t3=%f\n",t[0],t[1],t[2]);
printf("volume parameter beta=%f,w=%f,c=%f\n",beta,w,c);
printf("nsp=%i,nmax=%i\n",nsp,nmax);
//printf("trap_seeds = %i,%i,%i\n",trap_seeds[0],trap_seeds[1],trap_seeds[2]);
printf("release_seed = %i\n",release_seed);
printf("begin CTI simulation\n");
float ***ntrap;
float *randv;
float tmp;
int ntrap_tmp,nrandv=100000;
int *a0,*acti,dim[2];
int i,j,k,l;
long iseed=-1;
ntotal=ny+noverscan;
ntrap = f3tensor(0,nsp-1,0,ny,0,nmax+1);
a0=ivector(0,ny);
acti=ivector(0,ntotal);
randv=vector(0,nrandv);
for(i=0;i<nrandv;i++)randv[i]=ran1(&iseed);
printf("column k/%i, k=", nx);
for(k=0;k<nx;k++){
if(k % 8 == 0) fprintf(stdout, "%i ",k); fflush(stdout);
for(l=0;l<nsp;l++){
tmp = rho_trap[l]*ny;
ntrap_tmp = poidev(tmp,&trap_seeds[l]);
creattraps(&trap_seeds[l],ntrap_tmp,ny,nmax,c,beta,ntrap[l]);
}
for(i=0;i<ny;i++){a0[i]=image[k][i];}
//for(i=0;i<ntotal;i++)acti[i]=0;
addCTI(a0,ny,noverscan,nsp,beta,w,c,t,ntrap,acti,release_seed,randv);
for(i=0;i<ntotal;i++){
imagecti[k][i]=acti[i];
}
}
printf("\nCTI simulation finished!\n");
free_ivector(a0,0,ny);
free_ivector(acti,0,ntotal);
free_f3tensor(ntrap,0,nsp-1,0,ny,0,nmax+1);
free_vector(randv,0,nrandv);
return;
}
void addCTI(int *a0,int ny,int noverscan, int nsp, float beta,float w, float c,float *t, float ***ntrap, int *acti, int release_seed,float *randv)
{
void trap_release(int *flow, int *traped,int ntraped,float *randv,int *ncumran,float f);
//float ran1(long *idum);
float *tmpntrap,*f,tmph[50];
float height,wre;
int flowt,ntraptot,ntraped,topoftrap,nrelease,ntraped0,tmptrapedt,ntrapsum;
int ntotal,i,j,k,nmove,isum,ntmp,isp,*tmptraped,**traped,*flow;
int ncumran,ncumran0,jc,*jcindx;
long hotseed=-1;
wre=1/w;
ntotal=ny+noverscan;
f=vector(0,nsp-1);
traped = imatrix(0,nsp-1,0,ntotal);
flow=ivector(0,ntotal);
jcindx=ivector(0,ntotal);
for(i=0;i<nsp;i++){f[i]=1-exp(-1./t[i]);}
for(i=ny;i<ntotal;i++)flow[i]=0;
for(i=0;i<nsp;i++){for(j=ny;j<ntotal;j++){traped[i][j] = 0;}}
//initialize
for(i=0;i<ny;i++){
flow[i]=a0[i];
for(j=0;j<nsp;j++){
ntraptot = ntrap[j][i][0];
height=flow[i]*wre;
if(ntraptot>0){
ntraped=0;
tmpntrap=ntrap[j][i];
while(tmpntrap[ntraped+1]<=height){ntraped++;}
if(flow[i]<ntraped)ntraped=flow[i];
flow[i]-=ntraped;
traped[j][i]=ntraped;
}
else{traped[j][i]=0;}
}
}
// rearrange trapes
jc=-1;
for(i=0;i<ny;i++){
ntrapsum=ntrap[0][i][0]+ntrap[1][i][0]+ntrap[2][i][0];
//if((ntrapsum!=0 || hotpixflag){ if there is none-zero trap or it is a hot pix
if(ntrapsum!=0){
jc++;
for(j=0;j<nsp;j++){
traped[j][jc]=traped[j][i];
ntraptot=ntrap[j][i][0];
for(k=0;k<=ntraptot+1;k++)ntrap[j][jc][k]=ntrap[j][i][k];
}
}
jcindx[i+1]=jc;
}
for(i=ny;i<ntotal;i++)jcindx[i]=jc;
// add CTI effects
acti[0]=flow[0];
for(i=1;i<ntotal;i++){
nmove=jcindx[i];
for(j=nmove;j>=0;j--){
//flow[i]+=poidev(0.01, &hotseed);
ncumran=randv[j+i]*10000;
height=flow[i]*wre;
isp=0;
tmpntrap=ntrap[isp][j];
ntraped=0;
tmptraped=traped[isp];
while(tmpntrap[ntraped+1]<=height){ntraped++;}
topoftrap=tmptraped[j]-ntraped;
if(topoftrap!=0){trap_release(&flow[i], &tmptraped[j],topoftrap,randv,&ncumran,f[isp]);}
isp=1;
tmpntrap=ntrap[isp][j];
ntraped=0;
//height=flow[j]*wre; ;
tmptraped=traped[isp];
while(tmpntrap[ntraped+1]<=height){ntraped++;}
topoftrap=tmptraped[j]-ntraped;
if(topoftrap!=0){trap_release(&flow[i], &tmptraped[j],topoftrap,randv,&ncumran,f[isp]);}
isp=2;
tmpntrap=ntrap[isp][j];
ntraped=0;
//height=flow[j]*wre;
tmptraped=traped[isp];
while(tmpntrap[ntraped+1]<=height){ntraped++;}
topoftrap=tmptraped[j]-ntraped;
if(topoftrap!=0){trap_release(&flow[i], &tmptraped[j],topoftrap,randv,&ncumran,f[isp]);}
//isp=3...
}
acti[i]=flow[i];
}
free_ivector(flow,0,ntotal);
free_ivector(jcindx,0,ntotal);
free_imatrix(traped,0,nsp-1,0,ntotal);
free_vector(f,0,nsp-1);
return;
}
void trap_release(int *flow, int *traped,int topoftrap,float *randv,int *ncumran,float f){
int ntmp,k;
if (topoftrap>0){ // release
for(k=0;k<topoftrap;k++){
*ncumran+=1;
if(randv[*ncumran]<f){
*flow+=1;
*traped-=1;
}
}
return;
}
else{ //trap
ntmp = -topoftrap;
if(*flow<ntmp)ntmp=*flow;
*flow-=ntmp;
*traped += ntmp;
}
return;
}
/*int main(){
int read_fits_2D(const char *argv,float *galaxy,int imagesize);
int write_fits_2D(const char *argv,float **stars,int *dim);
int **image,nimg,kct,i,j;
float *image1,**imagecti;
int nx=4608,ny=4616,noverscan=84,nmax=12,nsp=3;
int ntotal;
float beta=0.478,w=84700,c=0;
float t[3] = {0.74,7.7,37};
float rho_trap[3] = {0.6,1.6,1.4};
char fname1[300];
int dim[2];
long trap_seeds[3] = {1,100,1000};
int release_seed = 10000;
clock_t start, end;
ntotal=ny+noverscan;
nimg=nx*ny;
image=imatrix(0,nx,0,ny);
image1=vector(0,nimg);
imagecti=matrix(0,nx,0,ntotal);
strcpy(fname1,"inputdata/image.fits");
read_fits_2D(fname1,image1,nimg);kct=0;
for (j=0; j<ny; j++){
for(i=0;i<nx;i++){
image[i][j]=image1[kct];
kct++;
}}
free_vector(image1,0,nimg);
start = clock();
CTI_simul(image, nx, ny, noverscan, nsp, rho_trap, t, beta, w, c, nmax, trap_seeds, release_seed, imagecti);
end = clock();
float time;
time = (end-start)/CLOCKS_PER_SEC;
printf("running time %f ms\n",time);
// output final image
strcpy(fname1,"output/image_CTIfinal.fits");
dim[0]=nx;dim[1]=ntotal;
write_fits_2D(fname1,imagecti,dim);
free_matrix(imagecti,0,nx,0,ntotal);
free_imatrix(image,0,nx,0,ny);
return 0;
}*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nrutil.h"
void creattraps(long *seed,int ntrap,int ny,int nmax,float c,float beta,float **sp)
{
//creat ntrap traps along one column
//sp[i][0] the number of trap in the ith pixle;
//sp[i][j] (c,1+c) the height of the jth trap in the ith pixle;
float ran1(long *idum);
void sort(unsigned long n, float arr[]);
float tmp,betare,height;
int i,nyt,ntmp,j;
float *tmpv;
tmpv = vector(1,100);
if(nmax>50){printf(" the upper limite of trap in each pixe is too large, nmax=%d\n",nmax); getchar();}
betare=1./beta;
for(i=0;i<ny;i++)sp[i][0]=0;
for(i=0;i<ntrap;i++){
nyt=ran1(seed)*ny;
sp[nyt][0]++;
}
for(i=0;i<ny;i++){
ntmp=sp[i][0];
if(ntmp==1){
height=ran1(seed)-c;
if(height<=0){sp[i][1]=0;}
else{
sp[i][1]=pow(height,betare);
}
}
if(ntmp>1){
if(ntmp>nmax)sp[i][0]=ntmp=nmax; // upper limite of trap in each pixel is nmax
for(j=1;j<=ntmp;j++)tmpv[j]=ran1(seed)-c;
sort(ntmp, tmpv);
for(j=1;j<=ntmp;j++){
height=tmpv[j];
if(height<=0){sp[i][j]=0;}
else{sp[i][j]=pow(height,betare);}
}
}
sp[i][ntmp+1]=999999;
}
free_vector(tmpv,1,100);
}
#include <math.h>
float gammln(float xx)
{
double x,y,tmp,ser;
static double cof[6]={76.18009172947146,-86.50532032941677,
24.01409824083091,-1.231739572450155,
0.1208650973866179e-2,-0.5395239384953e-5};
int j;
y=x=xx;
tmp=x+5.5;
tmp -= (x+0.5)*log(tmp);
ser=1.000000000190015;
for (j=0;j<=5;j++) ser += cof[j]/++y;
return -tmp+log(2.5066282746310005*ser/x);
}
/* (C) Copr. 1986-92 Numerical Recipes Software )1!. */
#include <math.h>
float gasdev(long *idum)
{
float ran1(long *idum);
static int iset=0;
static float gset;
float fac,rsq,v1,v2;
if (iset == 0) {
do {
v1=2.0*ran1(idum)-1.0;
v2=2.0*ran1(idum)-1.0;
rsq=v1*v1+v2*v2;
} while (rsq >= 1.0 || rsq == 0.0);
fac=sqrt(-2.0*log(rsq)/rsq);
gset=v1*fac;
iset=1;
return v2*fac;
} else {
iset=0;
return gset;
}
}
/* (C) Copr. 1986-92 Numerical Recipes Software )1!. */
This diff is collapsed.
/* CAUTION: This is the ANSI C (only) version of the Numerical Recipes
utility file nrutil.h. Do not confuse this file with the same-named
file nrutil.h that may be supplied in a 'misc' subdirectory.
*That* file is the one from the book, and contains both ANSI and
traditional K&R versions, along with #ifdef macros to select the
correct version. *This* file contains only ANSI C. */
#ifndef _NR_UTILS_H_
#define _NR_UTILS_H_
static float sqrarg;
#define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 : sqrarg*sqrarg)
static double dsqrarg;
#define DSQR(a) ((dsqrarg=(a)) == 0.0 ? 0.0 : dsqrarg*dsqrarg)
static double dmaxarg1,dmaxarg2;
#define DMAX(a,b) (dmaxarg1=(a),dmaxarg2=(b),(dmaxarg1) > (dmaxarg2) ?\
(dmaxarg1) : (dmaxarg2))
static double dminarg1,dminarg2;
#define DMIN(a,b) (dminarg1=(a),dminarg2=(b),(dminarg1) < (dminarg2) ?\
(dminarg1) : (dminarg2))
static float maxarg1,maxarg2;
#define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1) > (maxarg2) ?\
(maxarg1) : (maxarg2))
static float minarg1,minarg2;
#define FMIN(a,b) (minarg1=(a),minarg2=(b),(minarg1) < (minarg2) ?\
(minarg1) : (minarg2))
static long lmaxarg1,lmaxarg2;
#define LMAX(a,b) (lmaxarg1=(a),lmaxarg2=(b),(lmaxarg1) > (lmaxarg2) ?\
(lmaxarg1) : (lmaxarg2))
static long lminarg1,lminarg2;
#define LMIN(a,b) (lminarg1=(a),lminarg2=(b),(lminarg1) < (lminarg2) ?\
(lminarg1) : (lminarg2))
static int imaxarg1,imaxarg2;
#define IMAX(a,b) (imaxarg1=(a),imaxarg2=(b),(imaxarg1) > (imaxarg2) ?\
(imaxarg1) : (imaxarg2))
static int iminarg1,iminarg2;
#define IMIN(a,b) (iminarg1=(a),iminarg2=(b),(iminarg1) < (iminarg2) ?\
(iminarg1) : (iminarg2))
#define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
void nrerror(char error_text[]);
float *vector(long nl, long nh);
int *ivector(long nl, long nh);
unsigned char *cvector(long nl, long nh);
long *lvector(long nl, long nh);
double *dvector(long nl, long nh);
float **matrix(long nrl, long nrh, long ncl, long nch);
double **dmatrix(long nrl, long nrh, long ncl, long nch);
int **imatrix(long nrl, long nrh, long ncl, long nch);
float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
long newrl, long newcl);
float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch);
float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
void free_vector(float *v, long nl, long nh);
void free_ivector(int *v, long nl, long nh);
void free_cvector(unsigned char *v, long nl, long nh);
void free_lvector(long *v, long nl, long nh);
void free_dvector(double *v, long nl, long nh);
void free_matrix(float **m, long nrl, long nrh, long ncl, long nch);
void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch);
void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch);
void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch);
void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch);
void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh);
int ***i3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
void free_i3tensor(int ***t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh);
unsigned char ***b3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
void free_b3tensor(unsigned char ***t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh);
double ***d3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
void free_d3tensor(double ***t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh);
char **cmatrix(long nrl, long nrh, long ncl, long nch);
void free_cmatrix(char **m, long nrl, long nrh, long ncl, long nch);
#endif /* _NR_UTILS_H_ */
#include <math.h>
#define PI 3.141592654
float poidev(float xm, int *idum)
{
float gammln(float xx);
float ran1(int *idum);
static float sq,alxm,g,oldm=(-1.0);
float em,t,y;
if (xm < 12.0) {
if (xm != oldm) {
oldm=xm;
g=exp(-xm);
}
em = -1;
t=1.0;
do {
++em;
t *= ran1(idum);
} while (t > g);
} else {
if (xm != oldm) {
oldm=xm;
sq=sqrt(2.0*xm);
alxm=log(xm);
g=xm*alxm-gammln(xm+1.0);
}
do {
do {
y=tan(PI*ran1(idum));
em=sq*y+xm;
} while (em < 0.0);
em=floor(em);
t=0.9*(1.0+y*y)*exp(em*alxm-gammln(em+1.0)-g);
} while (ran1(idum) > t);
}
return em;
}
float poidev_copy(float xm, int *idum)
{
float gammln(float xx);
float ran1_copy(int *idum);
static float sq1,alxm1,g1,oldm1=(-1.0);
float em,t,y;
if (xm < 12.0) {
if (xm != oldm1) {
oldm1=xm;
g1=exp(-xm);
}
em = -1;
t=1.0;
do {
++em;
t *= ran1_copy(idum);
} while (t > g1);
} else {
if (xm != oldm1) {
oldm1=xm;
sq1=sqrt(2.0*xm);
alxm1=log(xm);
g1=xm*alxm1-gammln(xm+1.0);
}
do {
do {
y=tan(PI*ran1_copy(idum));
em=sq1*y+xm;
} while (em < 0.0);
em=floor(em);
t=0.9*(1.0+y*y)*exp(em*alxm1-gammln(em+1.0)-g1);
} while (ran1_copy(idum) > t);
}
return em;
}
#undef PI
/* (C) Copr. 1986-92 Numerical Recipes Software )1!. */
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -2,17 +2,24 @@ import galsim
import numpy as np
class FocalPlane(object):
def __init__(self, config=None, survey_type='Photometric', bad_chips=None):
def __init__(self, config=None, chip_list=None, survey_type='Photometric', bad_chips=None):
"""Get the focal plane layout
"""
self.nchips = 42
self.ignore_chips = []
if bad_chips == None:
self.bad_chips = []
else:
self.bad_chips = bad_chips
for chip_id in bad_chips:
self.ignore_chips.append(chip_id)
self.ignore_chips = []
if survey_type == 'Photometric':
if chip_list is not None:
for i in range(42):
if not (i+1 in chip_list):
self.ignore_chips.append(i+1)
elif survey_type == 'Photometric':
for i in range(5):
self.ignore_chips.append(i+1)
self.ignore_chips.append(i+26)
......
......@@ -14,6 +14,10 @@ VC_A = 2.99792458e+18 # speed of light: A/s
VC_M = 2.99792458e+8 # speed of light: m/s
H_PLANK = 6.626196e-27 # Plank constant: erg s
ALL_FILTERS = ["NUV","u", "g", "r", "i","z","y","GU", "GV", "GI", "FGS"]
PHOT_FILTERS = ["NUV", "u", "g", 'r', 'i', 'z', 'y', 'FGS']
SPEC_FILTERS = ["GI", "GV", "GU"]
def rotate_conterclockwise(x0, y0, x, y, angle):
"""
Rotate a point counterclockwise by a given angle around a given origin.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment