#include #include #include "math.h" #include "structs.h" #include "nrutil.h" #define STRLEN 1000 void template (FILE *, struct inpars *, struct fitpars *fpar); void get_line (char buffer[], int n); int istrunc (char *objtype); void menu (struct inpars *input, struct fitpars *fpar, FILE *ftype, float xoffset, float yoffset, float chisq, int ndof); void init_first (struct fitpars *fpar, char *objtype); struct fitpars *newobject (char *objtype); int newfitpar (char *string, struct fitpars *fpar); void check_trunc (struct fitpars *fpar); void del_fitpars_struct (struct fitpars *fpar); void upcase (char *); int totnobj=0; void read_input (struct inpars *input, struct fitpars *fpar) { void checkpar (char *, struct inpars *, struct fitpars *fpar, int *objnum); FILE *pfile; char string[STRLEN]; int i, quit=0, objnum; extern struct runflags flags; strcpy (fpar->objtype, "none"); fpar->next = NULL; /*************************************************************\ * Read in a file that has all the parameters in the needed * * If there's no such file, then the user must enter them by * * hand via a menu. * \*************************************************************/ if (!strncmp(input->initparfile, "\0", 1)) { printf ("Enter template file name: "); get_line (input->initparfile, STRLEN); flags.interact = 1; }; if (strncmp(input->initparfile, "\0", 1)) { pfile = fopen (input->initparfile, "r"); if (pfile == (FILE *) 0) { printf ("\nError reading template parameter file....\n"); printf ("Input the parameters manually. \n"); strcpy (input->initparfile, "none"); flags.interact = 1; } else { template (pfile, input, fpar); fclose (pfile); } }; /* Loop until all the required parameters are read in */ objnum = totnobj; while ( !quit ){ check_trunc (fpar); menu (input, fpar, stdout, 0., 0., 0., 0); sprintf (string, ""); while (strncmp(string, "r", 1) !=0 && !quit && flags.interact == 1){ printf ("Currently modifying component number: %d \n", objnum); printf ("Enter item, initial value(s), fit switch(es) ==> "); get_line (string, STRLEN); checkpar (string, input, fpar, &objnum); if (strncmp(string, "q", 1) == 0 || flags.interact == 0) quit = 1; }; if (flags.interact == 0) quit= 1; }; return; } /* ------------------------------------------------------------------------ *\ \* ------------------------------------------------------------------------ */ void checkpar (char *string, struct inpars *input, struct fitpars *fpar, int *objnum) { char com[STRLEN], *ptr, objtype[10], *fmode; int intcom, delnum, box[5], i, match=0; struct fitpars *newobj, *ptrstart; extern struct sampars sample; FILE *pfile; void del_obj (struct fitpars *fpar, int delnum, struct sampars *sample); ptrstart = fpar; for (i=0; i < *objnum - 1; ++i) fpar = fpar->next; sscanf (string, " %s", com); /****************************\ * Convert to uppercase * \****************************/ upcase (com); /*****************************************************************\ * Check for fitting parameters first. The order of reading the * * parameters actually doesn't matter at all. It just makes it * * easier to do this first, because otherwise some of the * * parameters are degenerate in the first letter. If there's no * * match in newfitpar, return 0 * \*****************************************************************/ if (fpar != NULL && strncmp (fpar->objtype, "none", 4) != 0) match = newfitpar (string, fpar); if (strncmp (com, "A", 1)==0) sscanf (string, " %s %s", com, input->data); else if (strncmp (com, "B", 1)==0 && !match) sscanf (string, " %s %s", com, input->output); else if (strncmp (com, "C", 1)==0 && !match) sscanf (string, " %s %s", com, input->sigma); else if (strncmp (com, "D", 1)==0) sscanf (string, " %s %s %s", com, input->psf, input->kernel); else if (strncmp (com, "E", 1)==0) sscanf (string, " %s %d", com, &input->sampfac); else if (strncmp (com, "F", 1)==0 && !match) sscanf (string, " %s %s", com, input->badpix); else if (strncmp (com, "G", 1)==0) sscanf (string, " %s %s", com, &input->constraints); else if (strncmp (com, "H", 1)==0) { sscanf (string, " %s %d %d %d %d", com, &box[1], &box[2], &box[3], &box[4]); sprintf (input->imgsect, "[%d:%d,%d:%d]", box[1], box[2], box[3], box[4]); } else if (strncmp (com, "I", 1)==0) sscanf (string, " %s %d %d", com, &input->convbox[1], &input->convbox[2]); else if (strncmp (com, "J", 1)==0) sscanf (string, " %s %f", com, &input->magzpt); else if (strncmp (com, "K", 1)==0) sscanf (string, " %s %f %f", com, &input->d[1], &input->d[2]); else if (strncmp (com, "M", 1)==0) { sscanf (string, " %s %d", com, objnum); if (*objnum > totnobj) *objnum = totnobj; if (*objnum < 1 && totnobj >= 1) *objnum = 1; } else if (strncmp (com, "O", 1)==0) { sscanf (string, " %s %s", com, &input->device); if (strncmp (input->device, "both", 4) != 0 && strncmp (input->device, "reg", 3) != 0 && strncmp (input->device, "cur", 3) != 0) strcpy (input->device, "regular"); } else if (strncmp (com, "P", 1)==0) sscanf (string, " %s %d", com, &input->create); else if (strncmp (com, "X", 1)==0) { sscanf (string, " %s %d", com, &delnum); if (delnum <= totnobj) { fpar = ptrstart; del_obj (fpar, delnum, &sample); }; totnobj = sample.nobjs; if (*objnum > totnobj) *objnum = totnobj; } else if (strncmp (com, "Z", 1)==0) sscanf (string, " %s %d", com, &fpar->outtype); else if (strncmp (com, "0", 1)==0 || strncmp (com, "T0", 2)==0 || strncmp (com, "N", 1)==0) { /* Create a new link in the chain to hold parameters for a */ /* new component. */ sscanf (string, " %s %s", com, objtype); while ( strncmp (objtype, "sersic", 6) != 0 && strncmp (objtype, "expdisk", 7) != 0 && strncmp (objtype, "edgedisk", 8) != 0 && strncmp (objtype, "sinc", 4) != 0 && strncmp (objtype, "isophote", 8) != 0 && strncmp (objtype, "nuker", 5) != 0 && strncmp (objtype, "gaussian", 8) != 0 && strncmp (objtype, "psf", 3) != 0 && strncmp (objtype, "moffat", 6) != 0 && strncmp (objtype, "ferrer", 6) != 0 && strncmp (objtype, "devauc", 6) != 0 && strncmp (objtype, "king", 4) != 0 && !istrunc (objtype) && strncmp (objtype, "sky", 3) != 0) { printf ("\nEnter new component type: "); get_line (objtype, 10); }; /************************************************************\ * Create new object and initialize some parameters. Treat * * first object differently because the fpar container is * * already there, but not the arrays within it. * \************************************************************/ while (fpar->next != NULL) fpar = fpar->next; if (strncmp (fpar->objtype, "none", 4) == 0) init_first (fpar, objtype); /* First object */ else /* not first object */ fpar->next = newobject (objtype); totnobj++; *objnum = totnobj; sample.nobjs++; printf ("\n"); } else if (strncmp (com, "T", 1)==0 && !match) { printf ("Enter template file name: "); get_line (string, STRLEN); if (strncmp(string, "\0", 1)) { pfile = fopen (string, "r"); if (pfile == (FILE *) 0) { printf ("\nError reading template parameter file....\n"); printf ("Enter in the parameters manually. \n"); } else { fpar = ptrstart; template (pfile, input, fpar); fclose (pfile); *objnum = totnobj; }; }; } else if (strncmp (com, "#", 1)==0 || strncmp (com, "Q", 1) ==0 || strncmp(com,"=",1)==0 || strncmp(com,"R",1)==0) /* Do nothing */; else if (!match) { printf ("\n"); printf ("Your options are: \n"); printf (" [ ] Enter parameter number/letter followed by value(s)\n"); printf (" [M number] Modify component number parameters \n"); printf (" [N] Add new component \n"); printf (" [R] Redisplay the parameter menu \n"); printf (" [T] Read in initial parameter template file \n"); printf (" [X number] Delete component number \n"); printf (" [Q] Quit menu and go on to fitting \n"); printf ("\n"); }; return; } /* ------------------------------------------------------------------------ *\ \* ------------------------------------------------------------------------ */ void template (FILE *pfile, struct inpars *input, struct fitpars *fpar) { void checkpar (char *, struct inpars *, struct fitpars *fpar, int *objnum); char string[STRLEN]; while ( ! feof (pfile) ) { if ( fscanf (pfile, " %[^\n]", string) > 0 ) checkpar (string, input, fpar, &totnobj); }; } /* ------------------------------------------------------------------------ *\ \* ------------------------------------------------------------------------ */ void del_obj (struct fitpars *fpar, int delnum, struct sampars *sample) { int i=0, j; struct fitpars *fptr, *tfptr; fptr = fpar; for (i = 1; i < delnum - 1; i++) { fpar = fpar->next; fptr = fptr->next; }; fptr = fptr->next; if (delnum > 1) { if (fptr != NULL) { fpar->next = fptr->next; free ((float *)fptr->a); free ((int *)fptr->ia); free ((float *)fptr->sig); /* Delete higher order term structure */ if (fptr->high != NULL) del_fitpars_struct (fptr->high); free (fptr); } else fpar->next = NULL; } else if (delnum == 1) { free ((float *)fpar->a); free ((int *)fpar->ia); free ((float *)fpar->sig); if (fpar->high != NULL) del_fitpars_struct (fpar->high); if (sample->nobjs > 1) { sprintf(fpar->objtype, "%s ", fptr->objtype); fpar->a = fptr->a; fpar->ia = fptr->ia; fpar->sig = fptr->sig; fpar->high = fptr->high; fpar->next = fptr->next; free (fptr); } else if (sample->nobjs == 1) strcpy (fpar->objtype, "none"); }; --(sample->nobjs); }