o_pixsource.c 6.11 KB
Newer Older
1
#include<stdio.h>
2
#include<stdlib.h>
3
4
5
6
7
#include<math.h>
#include<fonction.h>
#include<constant.h>
#include<dimension.h>
#include<structure.h>
8
#include<lt.h>
9
10
11
12
13
14
15
/****************************************************************/
/*      nom:        o_pixsource              */
/*      auteur:     Johan Richard         */
/*      date:       02/01/14            */
/*      place:      Lyon                */
/****************************************************************/

16
void precompsource(){
17
18
    FILE *fp;
    struct point Pi, Ps;
19
   const extern  struct  pot lens[];
20
    const extern struct g_source  S;
21
22
23
24
   extern struct g_mode M; // M.iclean
   const extern struct g_observ  O;
   extern struct g_pixel imFrame,ps,PSF;
   extern double **fluxmap;
25
    double **tmpimage ;
26
27
   int ni=imFrame.nx*imFrame.ny;
   int ns=ps.nx*ps.ny;
28
29
    double stepx,stepxs,stepy,stepys;
    double  ra, dec, width, height;
30
31
32
   fluxmap=(double **)alloc_square_double(ni,ns);
   double dlsds = dratio(lens[0].z, M.zclean);
   double sizeratio=imFrame.pixelx/ps.pixelx; // pixel size ratio between image and source plane
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  double xs,ys;
    
    wcsfull(ps.wcsinfo, &ra, &dec, &width, &height);
    ps.xmin -= 3600.*cos(M.ref_dec * DTR) * (ra - M.ref_ra);
    ps.xmax -= 3600.*cos(M.ref_dec * DTR) * (ra - M.ref_ra);
    ps.ymin -= -3600.*(dec - M.ref_dec);
    ps.ymax -= -3600.*(dec - M.ref_dec);
 /*   fprintf(stderr,"ps.nx=%d\t ps.ny=%d\n",ps.nx,ps.ny);
    fprintf(stderr,"imFrame.pixelx=%lf\t imFrame.pixely=%lf\n",imFrame.pixelx,imFrame.pixely);
    fprintf(stderr,"ps.pixelx=%lf\t ps.pixely=%lf\n",ps.pixelx,ps.pixely);
    fprintf(stderr,"imFrame.xmax=%lf\t imFrame.ymax=%lf\n",imFrame.xmax,imFrame.ymax);
    fprintf(stderr,"imFrame.xmin=%lf\t imFrame.ymin=%lf\n",imFrame.xmin,imFrame.ymin);
    fprintf(stderr,"ps.xmax=%lf\t ps.ymax=%lf\n",ps.xmax,ps.ymax);
    fprintf(stderr,"ps.xmin=%lf\t ps.ymin=%lf\n",ps.xmin,ps.ymin); */
    fprintf(stderr, "sizeratio= %lf\n", sizeratio);
   tmpimage=(double **)alloc_square_double(imFrame.ny,imFrame.nx);
49
   int is,js,i,j;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    double z;
    stepx=(imFrame.xmax-imFrame.xmin)/(imFrame.nx-1);
    stepy=(imFrame.ymax-imFrame.ymin)/(imFrame.ny-1);
    stepxs=(ps.xmax-ps.xmin)/(ps.nx-1);
    stepys=(ps.ymax-ps.ymin)/(ps.ny-1);
    fprintf(stderr,"stepxs=%.8lf\t stepys=%.8lf\n",stepxs,stepys);
    fprintf(stderr,"stepx=%.8lf\t stepy=%.8lf\n",stepx,stepy);
    for (i=0;i<imFrame.nx;i++)
    for(j=0;j<imFrame.ny;j++)
        tmpimage[j][i]=0.0;
    for (i=0;i<ni;i++)
        for(j=0;j<ns;j++)
            fluxmap[i][j]=0.0;
    xs= ps.xmin;
  //  ys=ps.ymin;
    fp = fopen("fluxmap.dat","w");
     for(is=0;is<ps.nx;is++)
   { fprintf(stderr, "is=%d\n",is);
      // xs = is * (ps.xmax - ps.xmin) / (ps.nx - 1) + ps.xmin;
       ys=ps.ymin;
70
      for(js=0;js<ps.ny;js++)
71
72
73
74
75
      {//fprintf(stderr, "js=%d\t ys=%lf\n",js,ys);
     //   ys = js * (ps.ymax - ps.ymin) / (ps.ny - 1) + ps.ymin;
         
          Pi.x= imFrame.xmin;
          
76
77
        for(i=0;i<imFrame.nx;i++)
        {
78
79
            Pi.y=imFrame.ymin;
         //   Pi.x = i * (imFrame.xmax - imFrame.xmin) / (imFrame.nx - 1) + imFrame.xmin;
80
81
          for(j=0;j<imFrame.ny;j++)
          {
82
83
84
85
86
87
88
89
90
91
92
93
94
95
           //   fprintf(fp, "is=%d\t xs=%lf\t js=%d\t ys=%lf\t i=%d\t Pi.x=%lf\t j=%d\t Pi.y=%lf\n",is,xs,js,ys,i,Pi.x,j,Pi.y);
           // Pi.y = j * (imFrame.ymax - imFrame.ymin) / (imFrame.ny - 1) + imFrame.ymin;
           
            e_dpl(&Pi, dlsds,S.zs, &Ps);
              z= (Ps.x-xs)*(Ps.x-xs)/stepxs/stepxs+(Ps.y-ys)*(Ps.y-ys)/stepys/stepys;
         // fprintf(fp, "Pi.x=%lf\t Pi.y=%lf\t Ps.x=%lf \t Ps.y=%lf \t xs=%lf \t ys=%lf\n",Pi.x,Pi.y,Ps.x,Ps.y,xs,ys);
              if(z<0.25)
              {  
               tmpimage[j][i]=sizeratio;
          //  fprintf(stderr, "sizeratio\n");
              }
            else
            tmpimage[j][i]=0.0;
              Pi.y +=stepy;
96
          }
97
            Pi.x +=stepx;
98
        }
99
100
        
      /*  convolve tmpimage with PSF */
101
102
103
        if (O.setseeing)
               d_seeing_omp(tmpimage, imFrame.nx, imFrame.ny, imFrame.pixelx);

104
       /* store tmpimage in flux matrix */
105
106
107
108
        for(i=0;i<imFrame.nx;i++)
        {
          for(j=0;j<imFrame.ny;j++)
          {
109
110
111
112
           fluxmap[i*imFrame.ny+j][is*ps.ny+js]=tmpimage[j][i];
       if(fluxmap[i*imFrame.ny+j][is*ps.ny+js]>1.e-15)
           fprintf(fp,"fluxmap[%d][%d]=%e\t tmpimage[%d][%d]=%e\n",(i*imFrame.ny+j),(is*ps.ny+js),fluxmap[i*imFrame.ny+j][is*ps.ny+js],j,i,tmpimage[j][i]);
          }
113
        }
114
        ys+=stepys;
115
     }
116
       xs+=stepxs;
117
   }
118
119
      fclose(fp);
   
120
   free_square_double(tmpimage,imFrame.ny);
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
   
}

void testpixel(double **zz,double *sflux)
{
    const extern struct g_observ  O;
    extern struct g_mode    M;
    extern struct g_pixel   ps,imFrame;
    extern double **fluxmap;
    double xmin, ymin, xmax, ymax, dx, dy;
    double f, scale;
    int nx, ny;
    FILE *fp;
    int i,j,k;
double  ra, dec, width, height;
    xmin = imFrame.xmin;
    xmax = imFrame.xmax;
    ymin = imFrame.ymin;
    ymax = imFrame.ymax;

    dx = xmax - xmin;
    dy = ymax - ymin;
    nx = imFrame.nx;// default: assign np to nx and adjust ny
   // ny = imFrame.ny;
    scale = dx / (nx-1);
    ny = dy / scale + 1;  // warning: trunc(ny) behavior

    dx = (nx - 1) * scale;
    dy = (ny - 1) * scale;
    xmax = xmin + dx;
    ymax = ymin + dy;
    int ni=imFrame.nx*imFrame.ny;
    int ns=ps.nx*ps.ny;
  fprintf(stderr, "\timage (%d,%d) s=%.3lf [%.3lf,%.3lf] [%.3lf,%.3lf]\n",nx, ny, scale, xmin, xmax, ymin, ymax);

    zz = (double **) alloc_square_double(ny, nx);
   
    fp = fopen("final_image1.dat","w");
    for(i=0;i<imFrame.nx;i++)
    {
       for(j=0;j<imFrame.ny;j++)
       {  zz[j][i]=0.0;
          for(k=0;k<ns;k++)
          zz[j][i]+=fluxmap[i*ny+j][k]*sflux[k];
           fprintf(fp,"zz[%d][%d]=%e\n",j,i,zz[j][i]);
       }
    }
    fclose(fp);
  //  if (O.setseeing)
    //    d_seeing(zz, nx, ny, scale);

  //  if ( M.iref > 0 )
    wcsfull(imFrame.wcsinfo, &ra, &dec, &width, &height);
     wrf_fits_abs("testpixel_abs.fits", zz, nx, ny, xmin, xmax, ymin, ymax, ra, dec);
        
   // else
        wrf_fits("testpixel.fits", zz, nx, ny, xmin, xmax, ymin, ymax);
   
   // free_square_double(zz, ny);

181
}