00001
00002 00003 00004 00005 00006 00007 00008 00009 00010 00011
00012
00013
00014 #include<stdio.h>
00015 #include<math.h>
00016 #include<time.h>
00017 #include<stdlib.h>
00018 #include<string.h>
00019 #include<iostream.h>
00020 #include "cfitsio/fitsio.h"
00021
00022 #define MAXSTRING 50
00023 #define NATOTMAX 500
00024 #define NDIAMMAX 10
00025 #define NANTVISIMAX 210000
00026 #define MAXMAPSIZE 600 // max size of map along one axis
00027 #define MAXDENSITYSIZE 50 // max size of density matrix along one axis
00028 #define NCONFMAX 30 // max # of configurations
00029 #define NPTSMAX 5000 // max # of visibilities for 1 antenna pair
00030 #define NPADSMAX 500// max # of pads
00031 #define PI 3.14159
00032 #define HA2RAD 0.261799 // to transform hour angles into radian
00033 #define RAD2ASEC 206.265e3 // to transform rad into arcsec
00034 #define MINELEVATION 15.0 //minimum source elevation in deg
00035
00036
00037 00038 00039
00040 class Visi;
00041 class APO;
00042 class Ant;
00043 class Instrume;
00044 class Obs;
00045 class Site;
00046 class Shadow;
00047 class Transf;
00048 class Map;
00049 class Grid;
00050 class Model;
00051 class Configfile;
00052 class Padslist;
00053
00054 extern void checkimplemented(APO *vapo, int napo,
00055 Instrume *pinstr[], Obs *pobs[], Site *psite[],
00056 Grid *pgrid[], Transf *ptransf[],
00057 int *sameinstr, int *sameobs,
00058 int *samesite, int *samegrid);
00059 extern int move(APO *vapo, Instrume **pinstr, Obs **pobs, Transf **ptransf,
00060 Site **psite,
00061 int napo, int iapo, int iant, float dx, float dy,float dl,
00062 int sameinstr, int sameobs, int samesite, int samegrid);
00063 extern void optimize(APO *,int,float,float);
00064 extern void optimize_shad(APO *,int,float,float, float gsh);
00065 extern void optimize(APO *,int,float,float, int conf);
00066 extern void shake(APO *vapo,int napo, float dptacc);
00067 extern void correct(APO *vapo,int napo, float dptacc);
00068 extern void correct(APO *vapo,int napo, float dptacc, int conf);
00069 extern float solveFWHM(float B, float Bmax);
00070 extern float bilinear(float, float, float **);
00071 extern float **matrix(int, int);
00072 extern void delete_matrix(float **);
00073 extern int compi(const void *, const void *);
00074 extern int compf(const void *, const void *);
00075 extern int compvec(const void *v1, const void *v2);
00076 extern int compdist(const void *v1, const void *v2);
00077 extern void printfitserror( int status);
00078
00079 00080 00081
00082
00084
00091 class Visi{
00092 float u,v,r,t,ix,iy,wght;
00093 public:
00094 inline Visi();
00095 inline void set(float, float, float);
00096 inline void set(float, float, float, float, float);
00097 inline void set(float, float, float, float, float, float, float);
00098 inline void operator=(Visi);
00099 inline void operator=(float);
00100 inline void getijwght(int *, int *, float *);
00101 inline void getuvwght(float *u, float *v, float *wght);
00102 inline void sym(Grid *);
00103
00104 void print(){
00105 printf("u=%7.1f v=%7.1f r=%7.1f t=%7.4f wght=%5.3f\n",u,v,r,t,wght);
00106 printf(" ix=%7.2f iy=%7.2f\n",ix,iy);
00107 };
00108 };
00109
00110
00112
00127 class APO{
00128
00129 long nvisi, nvisitot;
00130 Grid *pgrid;
00131 Instrume *pinstr;
00132 Obs *pobs;
00133 Site *psite;
00134 Transf *ptransf;
00135 float **pweights;
00137 float **pdensity;
00139 float **pgradx;
00141 float **pgrady;
00143 float **dpt;
00145 Visi *pvisi;
00146
00147 inline void getvisi(int);
00148 inline void initweights(int);
00149 inline void addallvisi();
00150
00151 public:
00152 APO();
00153 ~APO();
00154 void set(Instrume *, Obs *, Transf *, Site *, Grid *);
00155 void set(Grid *);
00156 void reset();
00157 void correct(float dl);
00158 void writeuvcov(char* fitsfile, long npix, float du);
00159 inline void addvisi(int);
00160 inline void subtractvisi(int);
00161 inline void getgrad();
00162 inline int check(int iant, float x, float y,int nconf1,int *iconf1);
00163 float getstdev();
00164 void shake(float dptacc);
00165 inline int move(int, float, float, float);
00166 void optimize(float, float);
00167 void optimize(float, float, int);
00168 void printvisi();
00169 void printdensity();
00170 void printforce(int iant, float fa, char *filename);
00171 void printforce(int iant);
00172 inline void getforce(float *, float *,int, float);
00173 inline void getforce_shad(float *, float *,int, float, float gsh);
00174 Instrume *getpinstr(){return pinstr;};
00175 Obs *getpobs(){return pobs;};
00176 Transf *gettransf(){return ptransf;};
00177 Site *getpsite(){return psite;};
00178 Grid *getpgrid(){return pgrid;};
00179
00180 };
00181
00182
00183
00185
00191 class Ant{
00192 float x;
00193 float y;
00194 float diam;
00195 int fixed;
00196 int moved;
00197 int nconf;
00198 int iconf[3];
00199 public:
00200 Ant(){;};
00201 void set(float a, float b, int fixed1, float c, int d, int e[]){
00202 x=a; y=b; fixed=fixed1; diam=c; nconf=d;
00203 for (int i=0; i<nconf; i++) iconf[i]=e[i]; moved=0;
00204 };
00205 void set(float a, float b){
00206 x=a; y=b;
00207 };
00208 void operator=(Ant ant2){
00209 int i;
00210 x=ant2.x; y=ant2.y; diam=ant2.diam; nconf=ant2.nconf;
00211 for(i=0;i<nconf;i++)iconf[i]=ant2.iconf[i];
00212 };
00213 void get(float *p,float *q, int *pfixed, float *r){
00214 *p=x; *q=y; *pfixed=fixed; *r=diam;
00215 };
00216 void get(float *p,float *q){*p=x; *q=y;};
00217 void get(float *p,float *q, int *pfixed){*p=x; *q=y; *pfixed=fixed;};
00218 void get(float *p, float *q, int *pnconf1, int *piconf1){
00219 int i;
00220 *p=x; *q=y; *pnconf1=nconf;
00221 for(i=0;i<nconf;i++) *(piconf1+i)=iconf[i];
00222 };
00223 void move(float dx, float dy){
00224 x+=dx; y+=dy; moved=(moved+1) % 2;
00225 };
00226 int getmoved(){return moved;};
00227 int getfixed(){return fixed;};
00228 float getdiam(){return diam;};
00229 void resetmoved(){moved=0;};
00230 void freeant(){fixed=0;};
00231 inline int check(Site *);
00232 void fixant(float x1, float y1){x=x1;y=y1;fixed=1;};
00233 void fixant(){fixed=1;};
00234 void offset(float xoff, float yoff);
00235 friend void APO::getvisi(int);
00236 void print(){
00237 int i;
00238 printf("x= %7.1f y=%7.1f diam=%4.1f conf:",x,y,diam);
00239 for(i=0; i<nconf; i++) printf(" %d",iconf[i]);
00240 printf(" fixed=%d\n",fixed);
00241 };
00242 };
00243
00244
00245
00247
00251 class Instrume{
00252 Ant *AntList;
00253 int na, natot, ndiam, nsh, nconf, nmoved, notmoved;
00255 float dspec;
00256 float diamlist[NDIAMMAX];
00257 float *vdmax;
00258 float *vdmin;
00259
00260 public:
00261 Instrume(){
00262 AntList=NULL; vdmax=NULL; vdmin=NULL, nmoved=0;
00263 };
00264 ~Instrume(){
00265 delete AntList; delete vdmax; delete vdmin;
00266 AntList=NULL; vdmax=NULL; vdmin=NULL;
00267 };
00268 void set(float *, float *, int *fixed1, float *, int, int, int, float,
00269 float *, float *);
00270 void set(float *, float *, int na);
00271 void setvdmax(float *vdmax1, int nconf1);
00272 void random(Site *psite, Obs *pobs, Model *pmod);
00273 void sort();
00274 void offset(float xoff, float yoff);
00275 void fixant(int iant){AntList[iant].fixant();};
00276 inline void get(float *, float *, int *fixed1, float *, int *,
00277 int *, int *, float *, float *, float *);
00278 void getpositions(float *xx, float *yy);
00279 void getcenter(float *xc, float *yc);
00280 inline void getant(float *p, float *q, int *pnconf1, int *piconf1,int iant){
00281 AntList[iant].get(p, q, pnconf1, piconf1);
00282 };
00283 inline Ant getant(int iant){return AntList[iant];};
00284 void getcorrection(Transf *ptransf, Site *psite, int iant0,
00285 int *iant1, float *dx, float *dy);
00286 void getcorrection(Transf *ptransf, Site *psite, int iant0, int conf,
00287 int *iant1, float *dx, float *dy);
00288 int getnatot(){return natot;};
00289 int getna(){return na;};
00290 int getnconf(){return nconf;};
00291 int getnsh(){return nsh;};
00292 int getiant(int conf, int i){return conf*(na-nsh)+i;};
00293 int getmoved(int iant){return AntList[iant].getmoved();};
00294 int getfixed(int iant){return AntList[iant].getfixed();};
00295 int getndiam(){return ndiam;};
00296 float getdiam(int i);
00297 float getdmax();
00298 inline float getdptmax(int iant,float x1, float y1, float dx ,float dy,
00299 float dl, int nconf1,int iconf1[3], Transf *ptransf);
00300 int getBSL_FWHM_ObsDur(float obsdurmax, char *filename, int iconf,
00301 float *baseline, float *fwhm, float *obsdur);
00302 inline int check(int, float, float, int, int *, Transf *);
00303 inline int check(int, float, float, int, int *);
00304 inline int check(Transf *);
00305 inline int check(Site *);
00306 inline void move(int iant, float dx, float dy);
00307 inline void resetmoved();
00308 void freeants();
00309
00310 void addants2pads(Padslist *pads, int iconf);
00311 int loadants2pads(Padslist *pads, int nloadmax, float dist,
00312 float ff,int iconf);
00313 void print();
00314 friend APO;
00315 };
00316
00317
00318
00319
00321
00326 class Obs{
00327 float dec;
00328 float dt;
00329 int nconf;
00330 float *vhamin;
00331 float *vintime;
00332 int *vnpts;
00333 public:
00334 Obs(){
00335 vhamin=NULL; vnpts=NULL;
00336 };
00337 ~Obs(){
00338 delete vhamin;
00339 delete vnpts;
00340 vhamin=NULL; vnpts=NULL;
00341 };
00342 void Obs::operator=(Obs obs2);
00343 void set(float, float *, float, float *, int);
00344 void set(float dec1);
00345 void get(float *, float *, float *, float *, int *);
00346 void get(float *hamin,float *hamax,int ind){
00347 *hamin=vhamin[ind]*HA2RAD;
00348 if(vnpts[ind] > 1) *hamax=(vhamin[ind]+vintime[ind])*HA2RAD;
00349 else *hamax=*hamin;
00350 };
00351 float getdec(){return dec;};
00352 float getobsdur(int conf){return vintime[conf];};
00353 int getnpts(int conf){return vnpts[conf];};
00354 void print();
00355 friend void APO::getvisi(int);
00356 friend void APO::initweights(int);
00357 };
00358
00360
00372 class Shadow{
00373 float sinlat, sindec, cosdl,sindl;
00374 float *pcosha;
00375 float *psinha;
00376 int npts;
00377 public:
00378 Shadow();
00379 ~Shadow();
00380 void set(float dec,float lat, float elevmin);
00381 inline float getruvmin(float blx, float bly);
00382 };
00383
00385
00390 class Transf{
00391 Obs *pobs;
00392 Site *psite;
00393 int nconf;
00394 float coslat, sinlat, cosdec, sindec, cosdl,sindl, cosd_l;
00395 float **pcosha;
00396 float **psinha;
00397 int *vnpts;
00398 int shadowing;
00399 Shadow shad;
00400 public:
00401 Transf();
00402 ~Transf();
00403 void set(Obs *pobs, Site *psite);
00404 void setshadow(float dec);
00405 inline void xy2uv(float blx, float bly, int conf, int isample,
00406 float *pu, float *pv);
00407 inline void uv2xy(float u, float v, int conf, int isample,
00408 float *px, float *py);
00409 inline float getruvmax(float blx, float bly, int conf);
00410 inline float getruvmin(float blx, float bly, int conf);
00411 float getcosha(int isample,int conf){return pcosha[isample][conf];};
00412 void get(float *coslat1, float *sinlat1, float *cosdec1, float *sindec1,
00413 float *cosdl1, float *sindl1, float *cosd_l1);
00414 };
00415
00417
00425 class Map{
00426
00427 long naxis1, naxis2;
00428 float xcenter, ycenter;
00429 float cdelt1, cdelt2;
00430 float **parray;
00431 char filename[MAXSTRING];
00432
00433 public:
00434 Map(){parray=NULL;};
00435 ~Map(){if(parray!=NULL) delete_matrix(parray);};
00436 inline int check(float x, float y);
00437 void set(char *filename1, float xcenter1,float ycenter1,
00438 float cdelt11, float cdelt21);
00439 void get(char *filename1,float *xcenter1,float *ycenter1,
00440 float *cdelt11,float *cdelt21){
00441 strcpy(filename1,filename);
00442 *xcenter1=xcenter; *ycenter1=ycenter;
00443 *cdelt11=cdelt1; *cdelt21=cdelt2;
00444 };
00445 };
00446
00447
00449
00453 class Site{
00454
00455 Map ground;
00456 int nomap;
00457 float lat;
00458 float tau;
00459 float rmin, rmax;
00460 float elevmin;
00461 public:
00462 Site(){;};
00463 void set(char *filename1, float xcenter1,float ycenter1,
00464 float cdelt11, float cdelt21,
00465 float lat1, float tau1,float rmin1,float rmax1);
00466 void set(float lat1, float tau1, float rmin1,float rmax1){
00467 lat=lat1; tau=tau1; nomap=1; rmin=rmin1; rmax=rmax1;
00468 printf("nomap=%d\n",nomap);
00469 elevmin=MINELEVATION;
00470 };
00471 void get(float *lat1, float *tau1,
00472 float *rmin1, float *rmax1, int *nomap1){
00473 printf("nomap=%d\n",nomap);
00474 *lat1=lat; *tau1=tau; *nomap1=nomap; *rmin1=rmin; *rmax1=rmax;
00475 };
00476 void get(char *filename1,float *xcenter1,float *ycenter1,
00477 float *cdelt11,float *cdelt21){
00478 ground.get(filename1, xcenter1, ycenter1, cdelt11, cdelt21);
00479 };
00480 inline int check(float, float);
00481 float getlat(){return lat;};
00482 float getelevmin(){return elevmin;};
00483 void print();
00484 friend void APO::getvisi(int);
00485 friend void APO::initweights(int);
00486 };
00487
00488
00490
00496 class Model{
00497 float fwhm, sigsq, dmax, dmin, norm;
00498 public:
00499 Model(){;};
00500 void set(float, float, float);
00501 float integral(float r0,float r1,float daz){
00502 return (norm*daz*sigsq*(exp(-r0*r0/(2*sigsq))
00503 -exp(-r1*r1/(2*sigsq))));
00504 };
00505 void setfwhm(float fwhm1){fwhm=fwhm1;};
00506 float getfwhm(){return fwhm;};
00507 void get(float *fwhm1, float *dminmod1,float *dmaxmod1){
00508 *fwhm1=fwhm; *dminmod1=dmin; *dmaxmod1=dmax;
00509 };
00510 float getdmax(){return dmax;};
00511 float getdmin(){return dmin;};
00512 float getnextr(float r0,float integr, float daz){
00513 return sqrt(-2*sigsq*log(exp(-r0*r0/(2*sigsq))-integr/(daz*norm*sigsq)));
00514 };
00515 };
00516
00517
00519
00523 class Grid{
00524 int size;
00525 float integr;
00526 float daz;
00527 float radius[MAXDENSITYSIZE+1];
00528 float az[2*MAXDENSITYSIZE+1];
00529 Model *mod;
00530 public:
00531 Grid(){;};
00532 void set(int, float, float, Model *);
00533 void setrmax(float rmax);
00534 inline void getixiy(float *, float *, float, float, float, float);
00535 inline float getdr(float iy);
00536 int getsize(){return size;};
00537 float getdaz(){return daz;};
00538 float getintegr(){return integr;};
00539 float getfwhm(){return mod->getfwhm();};
00540 void print();
00541 void print(char *name);
00542 };
00543
00544
00545
00547
00553 class Configfile{
00554 FILE *fp;
00555 char name[40];
00556 void read(int *tmp);
00557 void read(long *tmp);
00558 void read(float *tmp);
00559 void read(char *s);
00560 void read(float *pv,int n);
00561 void read(int *pv,int n);
00562 public:
00563 Configfile(){
00564 fp=NULL;
00565 };
00566 void operator=(char *name1){
00567 strcpy(name,name1);
00568 };
00569 void read(Instrume *, Site *, Obs *, Model *);
00570 void write(Instrume *, Site *, Obs *, Model *);
00571 };
00572
00573
00574
00576
00582 class Padslist{
00583 char filename[MAXSTRING];
00584 long npads;
00585 int nconf;
00586 float **pos;
00587 int **conf;
00588
00589 public:
00590 Padslist();
00591 ~Padslist();
00592 void setnconf(int nconf1){nconf=nconf1;};
00593 char *getfilename(){return filename;};
00594 void operator=(char *filename1){strcpy(filename,filename1);};
00595 float getdist2pad(float x, float y, int iconf,
00596 float *xpad, float *ypad, int *ipad);
00597 int load(int ipad, int iconf);
00598
00599
00600 void add(float x, float y, int iconf);
00601 void read();
00602 void write();
00603 void write(char *filename2);
00604 void write2(char *filename2);
00605 void offset(float dx, float dy);
00606 void print();
00607 };