void uireinitdialog( a_dialog *info, VFIELD *fields ) { unsigned i; uiposnhotspots( info->vs, fields ); info->dirty = FALSE; info->first = NULL; info->other = NULL; info->fields = fields; info->first = &fields[0]; /* set first to be first field in tab sequence */ while( notintab( info->first ) ) { if( info->first->typ == FLD_VOID ) { info->first = NULL; break; } ++(info->first); } info->curr = info->first; if( info->first != NULL ) { /* set curr to first field in tab sequence not an unset radio button */ while( radiooff( info->curr ) ) { info->curr = nextfield( info->curr ); } } for( i = 0 ; fields[i].typ != FLD_VOID ; ++i ) { print_field( info->vs, &fields[i], &fields[i] == info->curr ); } enter_field( info, info->curr ); }
int main() { int field[M][N] = { DEAD }; int count = 0; int steady = 0; int i; load(field); while (count < MAXSTEPS && !steady) { display(field); printf("%d \n",count); steady = nextfield(field); if (steady) printf("\n\tReached a steady state.\n"); count ++; //system("pause");// use this line if you are on windows system("read -p \"Press [Enter] key to continue...\""); //mac } if (count >= MAXSTEPS ) printf ("Max number of steps reached"); return count; }
static void *forwardtab( a_dialog *info ) { VFIELD *fld; if( info->curr == NULL ){ fld = info->first; } else { fld = nextfield( info->curr ); if( fld == NULL ) { fld = info->first; } } while( radiooff( fld ) ) { fld = nextfield( fld ); } return( fld ); }
static void *backwardtab( a_dialog *ui_dlg_info ) { VFIELD *fld, *hold; hold = NULL; for( fld = ui_dlg_info->first; fld != ui_dlg_info->curr; fld = nextfield( fld ) ) { if( !radiooff( fld ) ) { hold = fld; } } if( hold == NULL ) { /* wrap */ for( ; fld != NULL; fld = nextfield( fld ) ) { if( !radiooff( fld ) ) { hold = fld; } } } return( hold ); }
static void *backwardtab( a_dialog *info ) { VFIELD *fld, *hold; hold = NULL; fld = info->first; while( fld != info->curr ) { if( !radiooff( fld ) ) { hold = fld; } fld = nextfield( fld ); } if( hold == NULL ) { /* wrap */ while( fld != NULL ) { if( !radiooff( fld ) ) { hold = fld; } fld = nextfield( fld ); } } return( hold ); }
char * logdir(char *name) { char *p; int i, j; int pwf; /* attempt to open the password file */ if ((pwf = myopen("/etc/passwd", 0, 0)) == -1) return(0); /* find the matching password entry */ do { /* get the next line in the password file */ i = read(pwf, line, OURBUFSIZ); for (j = 0; j < i; j++) if (line[j] == '\n') break; /* return a null pointer if the whole file has been read */ if (j >= i) return(0); line[++j] = 0; /* terminate the line */ (void) lseek(pwf, (long) (j - i), 1); /* point at the next line */ p = nextfield(line); /* get the logname */ } while (*name != *line || /* fast pretest */ strcmp(name, line) != 0); (void) close(pwf); /* skip the intervening fields */ p = nextfield(p); p = nextfield(p); p = nextfield(p); p = nextfield(p); /* return the login directory */ (void) nextfield(p); return(p); }
static EVENT uitabkey( EVENT ev, a_dialog *info ) { VFIELD *curr, *fld; ORD row, col; SAREA area; EVENT newev; if( info->first == NULL ) return( FALSE ); curr = info->curr; newev = ev; switch( ev ){ case EV_MOUSE_DCLICK: case EV_MOUSE_PRESS: { a_combo_box *combo; a_list *list; VSCREEN *mousevs; mousevs = uivmousepos( info->vs, &row, &col ); fld = info->first; while( fld != NULL ) { list = NULL; area = fld->area; if( fld->typ == FLD_PULLDOWN ) { list = fld->ptr; area.height = 1; area.width += 1; /* pulldown button */ } else if( fld->typ == FLD_LISTBOX ) { list = fld->ptr; } else if( fld->typ == FLD_EDIT_MLE ) { list = fld->ptr; } else if( fld->typ == FLD_COMBOBOX ) { area.height = 1; area.width += 2; /* pulldown button */ combo = fld->ptr; list = &combo->list; } if( mousevs != info->vs ) { if( list != NULL && list->box != NULL ) { if( list->box->vs == mousevs ) { break; } } } else if( row >= area.row && row < area.row + area.height && col >= area.col && col < area.col + area.width ) { break; } fld = nextfield( fld ); } /* check boxes don't get mouse events unless mouse over them */ if( fld == NULL && curr != NULL && curr->typ == FLD_CHECK ) { newev = EV_NO_EVENT; } else { curr = fld; } break; } case EV_TAB_FORWARD : curr = forwardtab( info ); break; case EV_TAB_BACKWARD : curr = backwardtab( info ); break; case EV_CURSOR_RIGHT : case EV_CURSOR_DOWN : { a_radio *r1, *r2; if( curr!= NULL && curr->typ == FLD_RADIO ) { fld = nextfield( curr ); if( fld != NULL ) { r1 = curr->ptr; r2 = fld->ptr; if( r1->group == r2->group ) { curr = fld; do_radio( info, fld ); newev = EV_CHECK_BOX_CLICK; } } } break; } case EV_CURSOR_LEFT : case EV_CURSOR_UP : { a_radio *r1, *r2; if( curr!= NULL && curr->typ == FLD_RADIO ) { if( curr != info->first ) { fld = curr - 1; r1 = curr->ptr; r2 = fld->ptr; if( r1->group == r2->group ) { curr = fld; do_radio( info, fld ); newev = EV_CHECK_BOX_CLICK; } } } break; } } if( info->curr != curr ) { uidialogsetcurr( info, curr ); } return( newev ); }
/* \fcnfh get number of isotopes from file and set index @returns number of lines read */ int getmnfromfile(FILE *fp, struct atm_data *at, struct transit *tr, int nmb) { char line[maxline],*lp; int ison=0,i; struct isotopes *iso=tr->ds.iso; enum isodo *isodo=iso->isodo; //Set variable to handle proportional to isotopes int ipi=0,ipa=at->ipa=4; isolineinatm=(_Bool *)calloc(iso->n_i,sizeof(_Bool)); isoprop=(struct atm_isoprop *)calloc(ipa,sizeof(struct atm_isoprop)); at->begline=0; enum isodo atisodo; at->isodo = (enum isodo *)calloc(nmb,sizeof(enum isodo)); at->isoeq = (int *) calloc(nmb,sizeof(int)); at->m = (PREC_ZREC *) calloc(nmb,sizeof(PREC_ZREC)); at->n = (char **) calloc(nmb,sizeof(char *)); at->n[0] = (char *) calloc(nmb*maxeisoname,sizeof(char)); at->isoeq[0]=-1; for(i=1;i<nmb;i++){ at->n[i]=at->n[0]+i*maxeisoname; at->isoeq[i]=-1; } //while t,p data doesn't start, check for the various modifiers while(1){ switch(fgetupto_err(line,maxline,fp,&atmerr,atmfilename, at->begline++)){ case '\n': //Ignore comments and case '#': // blank lines continue; case 0: //Error if EOF transiterror(TERR_SERIOUS|TERR_ALLOWCONT, "readatminfo:: EOF unexpectedly found at line %i\n" "of file %s while no t,p data points have been read\n" ,at->begline,atmfilename); exit(EXIT_FAILURE); continue; case 'q': //Whether is mass or number abundance lp=line+1; while(*lp++==' '); lp--; switch(*lp|0x20){ case 'n': at->mass=0; break; case 'm': at->mass=1; break; default: transiterror(TERR_SERIOUS, "'q' option in the atmosphere file can only be followed\n" "by 'm' (for abundances by mass) or 'n' (for abundances by\n" "number). '%s' is invalid.\n" ,line); break; } continue; case 'z': //Zero radius value zerorad=atof(line+1); continue; case 'f': //An isotope is to be taken as //proportional to other. lp=line+1; while(*lp==' '||*lp=='\t') lp++; if(ipi==ipa) isoprop=(struct atm_isoprop *)realloc(isoprop,(ipa<<=1)* sizeof(struct atm_isoprop)); isoprop[ipi].m=getds(lp,0,isoprop[ipi].n,maxeisoname-1); //skip over recently read field, and go to next field. lp=nextfield(lp); //skip an optional equal '=' sign if(*lp=='=' && lp[1]==' ') lp=nextfield(lp); //get factor, which has to be between 0 and 1 isoprop[ipi].f=strtod(lp,NULL); if(isoprop[ipi].f<0 ) transiterror(TERR_CRITICAL, "Abundance ratio has to be positive in atmosphere\n" "file '%s' in line: %s" ,atmfilename,line); lp=nextfield(lp); //get name of reference and increase index i=0; while(*lp) isoprop[ipi].t[i++]=*lp++; isoprop[ipi].t[i]='\0'; //now check if that isotope is one of the given in the lineinfo //file isoprop[ipi].eq=-1; isisoline(isoprop[ipi].n,isoprop[ipi].m,&isoprop[ipi].eq,factor, iso->isof,isodo,iso->n_i); //advance index and go for the next line ipi++; continue; case 'u': //Change factorization of radius, temp, //or press switch(line[1]){ case 'r': at->rads.fct=atof(line+2); break; case 'p': at->atm.pfct=atof(line+2); break; case 't': at->atm.tfct=atof(line+2); break; default: transiterror(TERR_SERIOUS, "Invalid unit factor indication in atmosphere file\n"); exit(EXIT_FAILURE); } continue; case 'n': //Name or identifier for file data storename(at,line+1); continue; case 'i': //Isotope information lp=line+1; while(*lp==' '||*lp=='\t') lp++; //'i' has to come before 'f' if(ipi) transiterror(TERR_CRITICAL, "In line '%s'.\n" " 'f' lines have to come after all the 'i' lines in\n" " atmosphere file '%s'" ,line,atmfilename); //for each field while(*lp){ atisodo=atmfile; //Allocate if necessary if(ison==nmb){ nmb<<=1; at->isodo = (enum isodo *)realloc(at->isodo,nmb*sizeof(enum isodo)); at->isoeq = (int *) realloc(at->isoeq,nmb*sizeof(int)); at->m = (PREC_ZREC *) realloc(at->m, nmb*sizeof(PREC_ZREC)); at->n = (char **) realloc(at->n, nmb*sizeof(char *)); at->n[0] = (char *) realloc(at->n[0], nmb*maxeisoname*sizeof(char)); for(i=1;i<nmb;i++) at->n[i]=at->n[0]+i*maxeisoname; for(i=nmb/2;i<nmb;i++) at->isoeq[i]=-1; } //get mass and name, checking that is correct. First see if this //isotope wants to be ignored. if(*lp=='!'){ lp++; atisodo=ignore; } at->m[ison]=getds(lp,0,at->n[ison],maxeisoname-1); if(at->m[ison]<0||at->n[ison]=='\0'){ transiterror(TERR_SERIOUS, "Invalid field in file %s, line %i while reading isotope" " info at:\n%s\n" ,atmfilename,at->begline,lp); } //now check if that isotope is one of the given in the lineinfo //file isisoline(at->n[ison],at->m[ison],at->isoeq+ison,atisodo, iso->isof,isodo,iso->n_i); at->isodo[ison++]=atisodo; //skip over recently read field, and go to next field. while(*lp!=' '&&*lp!='\0') lp++; while(*lp==' '||*lp=='\t') lp++; } continue; default: //T,P seems to be starting break; } break; } transitprint(3,verblevel, "Read all keywords in atmosphere file without problems\n"); //Check if there was at least an isotope identification and allocate new //arrays if(!ison) transiterror(TERR_SERIOUS, "No isotopes were found in atmosphere file, make sure to\n" "specify them in a line starting with the letter 'i'.\n" "First non-comment line read:\n%s\n" ,line); at->begpos=ftell(fp)-strlen(line)-1; //shorten extra length of arrays fonly=(struct fonly *)calloc(nfonly,sizeof(struct fonly)); at->ipa=ipa=ipi; isoprop=(struct atm_isoprop *)realloc(isoprop,ipa* sizeof(struct atm_isoprop)); //Makes at arrays bigger, so that they can hold the factorized values. nmb = at->n_aiso = ison + ipa; at->isodo = (enum isodo *)realloc(at->isodo,nmb*sizeof(enum isodo) ); at->isoeq = (int *) realloc(at->isoeq,nmb*sizeof(int) ); at->m = (PREC_ZREC *) realloc(at->m, nmb*sizeof(PREC_ZREC) ); at->n = (char **) realloc(at->n, nmb*sizeof(char *) ); at->n[0] = (char *) realloc(at->n[0], nmb*maxeisoname*sizeof(char)); for(i=1;i<nmb;i++) at->n[i] = at->n[0] + i * maxeisoname; //initialize values for the factorized elements for(i=ison;i<nmb;i++){ strncpy(at->n[i],isoprop[i-ison].n,maxeisoname-1); at->n[i][maxeisoname-1] = '\0'; at->isoeq[i] = i-ison; at->m[i] = isoprop[i-ison].m; at->isodo[i] = factor; } //Resolve what to do with those isotopes that appear in the transition //database, but not in the atmosphere file. at->n_niso = checknonmatch(tr,at,isodo); //Set full isotope info in the transit structure nmb = iso->n_i + at->n_niso; iso->isodo = (enum isodo *)realloc(iso->isodo, nmb*sizeof(enum isodo)); iso->isof = (prop_isof *)realloc(iso->isof, nmb*sizeof(prop_isof)); iso->isof[iso->n_i].n = (char *)realloc(iso->isof[iso->n_i].n, (nmb-iso->n_i)*maxeisoname* sizeof(char)); for(i=1;i<nmb-iso->n_i;i++) iso->isof[iso->n_i+i].n = iso->isof[iso->n_i].n + i * maxeisoname; //Look for isotopes who have not been associated and see whether they //are supposed to be ignored. nmb = iso->n_i; ipi = 0; int lineignore=0; for(i=0 ; i<ison+ipa ; i++) //If the isotope is not associated to the linedb isotopes, then //associate it. Note that isotopes in linedb that are ignored will //be associated (see comments for Line \label{isodbassoc}). Hence //they won't be detected in this IF. Factor isotopes will also be //associated, and will be handled below if(at->isoeq[i] == -1){ //If they are not going to be ignored then associate them with the //following index available of post linedb isotopes. if(at->isodo[i] != ignore){ at->isoeq[i] = nmb; iso->isodo[nmb] = at->isodo[i]; iso->isof[nmb].m = at->m[i]; strcpy(iso->isof[nmb++].n, at->n[i]); } //otherwise, they might only be used as a reference to factor. else{ at->isoeq[i] = ipi; strcpy(fonly[ipi++].n, at->n[i]); } } //Just count the number of ignored isotopes that belonged to the line //isotopes. else if(at->isodo[i] == ignore) lineignore++; //If there is factor isotopes else if(at->isodo[i] == factor && isoprop[at->isoeq[i]].eq == -1){ if(at->isodo[i]==ignore) transiterror(TERR_CRITICAL, "Trying to ignore an factor isotope, that is not\n" "posible.\n"); isoprop[at->isoeq[i]].eq = nmb; iso->isodo[nmb] = at->isodo[i]; iso->isof[nmb].m = at->m[i]; strcpy(iso->isof[nmb++].n, at->n[i]); } //Reduce the array to get rid of nonline-ignored isotopes. (isov has //not even been allocated yet) iso->n_e = nmb; iso->isodo = (enum isodo *)realloc(iso->isodo, nmb*sizeof(enum isodo)); iso->isof = (prop_isof *)realloc(iso->isof, nmb*sizeof(prop_isof)); iso->isof[iso->n_i].n = (char *)realloc(iso->isof[iso->n_i].n, nmb*maxeisoname*sizeof(char)); for(i=1;i<nmb-iso->n_i;i++) iso->isof[iso->n_i+i].n = iso->isof[iso->n_i].n + i * maxeisoname; //Check that everything makes sense double cumulother=0; for(i=0 ; i<at->n_aiso ; i++) if(at->isodo[i]==factor){ int feq=at->isoeq[i]; if(strcasecmp(isoprop[feq].n,"other")==0) cumulother+=isoprop[feq].f; } //It doesn't make sense for cumulother to be anything different from //unity (except round-off error): you want to associate the //remainder of the atmosphere to some isotopic properties. if( cumulother!=0 && (int)(cumulother*ROUNDOFF+0.5)!=(int)(ROUNDOFF+0.5) ) transiterror(TERR_SERIOUS, "If you are specifying isotopes proportional to 'other'\n" "you have to complete unity (%g). It doesn't make sense\n" "otherwise\n" ,cumulother); transitASSERT(nmb+nfonly!=ison+ipa, "Oooops, number of ignored-nonline elements (%i), plus the\n" "number of ignored-line elements(%i), plus the number of\n" "nonignored (%i), doesn't match the number of elements\n" "found in fields 'i'(%i) and 'f'(%i) of the atmosphere\n" "file '%s'\n" ,nfonly,lineignore,nmb-lineignore,ison,ipa,atmfilename); transitASSERT(nmb!=iso->n_e, "Uyuyuyuyu! Problem in file %s, line %i,\n" "assertion failed: %i != %i!!\n" ,__FILE__,__LINE__,nmb,iso->n_e); //free unused array, store factor info in at structure and return line //where T,P start free(isolineinatm); at->isoprop=isoprop; return at->begline; }
void getmass(struct atm_data *at, struct molecules *mol){ int nmol = at->n_aiso; /* FINDME: De-hardcode filename, put it in tr.ds.at: */ char *filename = "../inputs/molecules.dat"; FILE *elist; /* Atomic masses, names, alias names, alias molecules, and sizes: */ double *amass, /* Atomic masses form list */ *radius; /* Molecular radii from list */ char **aname, /* Atomic symbol names */ **rname, /* Molecules names for listed radii */ **alias, /* Alias of names given in atmfile */ **amol, /* Corresponding molecule for alias */ **elements; /* Elements in a molecule */ int natoms = 92, /* Number of listed atoms */ nalias = 2, /* Number of listed alias names */ nradii = 14, /* Number of listed radii */ namelen = 3, /* Atomic symbol name length */ maxlinelen = 501, molnamelen, /* Length of a molecule name */ elen, /* Element name length */ iatom, /* Atom's index from list */ ielement, /* Element counter in a molecule */ i, j; /* Auxiliary for-loop index */ int *nelements; /* Number of elements in a molecule */ char line[maxlinelen], *lp, molecule[MAXNAMELEN]; /* Current molecule's name */ /* Alias names, and corresponding molecules: */ amass = (double *)calloc(natoms, sizeof(double)); aname = (char **)calloc(natoms, sizeof(char *)); aname[0] = (char *)calloc(natoms*namelen, sizeof(char)); for (i=1; i<natoms; i++) aname[i] = aname[0] + i*namelen; /* Open Molecules file: */ if((elist=verbfileopen(filename, "Molecular info ")) == NULL) exit(EXIT_FAILURE); do{ /* Read lines, skipping comments and blank lines: */ lp = fgets(line, maxlinelen, elist); }while (lp[0] == '\0' || lp[0] == '\n' || lp[0] == '#'); /* Fill atoms and mass array with info from element list: */ for (i=0; i<natoms; i++){ lp += 19; /* The element's symbol starts at the 19th character */ getname(lp, aname[i]); lp = nextfield(lp); amass[i] = strtod(lp, NULL); lp = fgets(line, maxlinelen, elist); } /* Allocate alias names and corresponding molecules: */ alias = (char **)calloc(nalias, sizeof(char *)); amol = (char **)calloc(nalias, sizeof(char *)); alias[0] = (char *)calloc(nalias*MAXNAMELEN, sizeof(char)); amol[0] = (char *)calloc(nalias*MAXNAMELEN, sizeof(char)); for (i=1; i<nalias; i++){ alias[i] = alias[0] + i*MAXNAMELEN; amol[i] = amol[0] + i*MAXNAMELEN; } /* Continue reading the file to get the alias names: */ do{ /* Skip blank and comment lines: */ lp = fgets(line, maxlinelen, elist); }while (lp[0] == '\0' || lp[0] == '\n' || lp[0] == '#'); /* Get aliases from file: */ for (i=0; i<nalias; i++){ /* Get alias and molecule name: */ getname(lp, alias[i]); lp = nextfield(lp); getname(lp, amol[i]); lp = fgets(line, maxlinelen, elist); } /* Allocate names and radii: */ radius = (double *)calloc(nradii, sizeof(double)); rname = (char **)calloc(nradii, sizeof(char *)); rname[0] = (char *)calloc(nradii*MAXNAMELEN, sizeof(char)); for (i=1; i<nradii; i++) rname[i] = rname[0] + i*MAXNAMELEN; /* Go to next block */ do{ lp = fgets(line, maxlinelen, elist); }while (lp[0] == '\0' || lp[0] == '\n' || lp[0] == '#'); /* Get radii from file: */ for (i=0; i<nradii; i++){ /* Get molecules' name and radius: */ getname(lp, rname[i]); lp = nextfield(lp); radius[i] = strtod(lp, NULL)/2.0; lp = fgets(line, maxlinelen, elist); } /* Allocate max number of molecules and max len of molecule name: */ nelements = (int *)calloc(MAXNAMELEN, sizeof(int)); elements = (char **)calloc(MAXNAMELEN, sizeof(char *)); elements[0] = (char *)calloc((MAXNAMELEN+1)*MAXNAMELEN, sizeof(char)); for (j=0; j<MAXNAMELEN; j++) elements[j] = elements[0] + j*(MAXNAMELEN+1); /* For each molecule: */ for (i=0; i<nmol; i++){ /* Check if molecule name is an alias: */ if ((j=findstring(mol->name[i], alias, nalias)) >= 0) strcpy(molecule, amol[j]); else strcpy(molecule, mol->name[i]); /* Allocate elements in a molecule: */ molnamelen = (int)strlen(molecule); /* Break down molecule into its elements: */ elen = 0; /* Element name length */ ielement = 0; /* Elements in a molecule counter */ for (j=0; j<molnamelen; j++){ if (isalpha(molecule[j])){ if (isupper(molecule[j])){ /* Uppercase letter: */ if (elen > 0){ /* End last element, advance j, store new letter */ if (elen <= 2){ /* If name is longer, it's an alias name */ elen = 0; ielement++; /* Count 1 more element */ nelements[ielement] = 1; } elements[ielement][elen++] = molecule[j]; } else{ /* New Atom (elen==0) */ elements[ielement][elen++] = molecule[j]; nelements[ielement] = 1; } } else{ /* Lowercase: */ elements[ielement][elen++] = molecule[j]; } } else{ /* A numeric value: */ nelements[ielement] = (int)strtol(&molecule[j], NULL, 10); elements[ielement][elen] = '\0'; j += (int)log10((double)nelements[ielement++]); elen = 0; } } if (elen != 0) ielement++; /* Calculate molecule's mass: */ for (j=0; j<ielement; j++){ /* Find index of atom in list: */ iatom = findstring(elements[j], aname, natoms); transitprint(30, verblevel, "Found %d %2s[%2d] atom(s) with mass " "%9.6f u.\n", nelements[j], aname[iatom], iatom, amass[iatom]); /* Get mass and multiply by the number of atoms in molecule: */ mol->mass[i] += amass[iatom] * nelements[j]; } /* Set the radius: */ j = findstring(molecule, rname, nradii); mol->radius[i] = radius[j] * ANGSTROM; transitprint(30, verblevel, "Molecule '%s' has radius %4.2f A and mass " "%4.2f u.\n", mol->name[i], mol->radius[i]/ANGSTROM, mol->mass[i]); } }
/* \fcnfh Get keyword variables from atmosphere file (mass/number abundance bool; zero-radius offset; radius, temperature, and pressure units factor; atmfile name/info; list isotopes; list of proportional-abundance isotopes). Store molecules and proportional isotopes in atm_data struct. Determine which linedb isotope corresponds to such atm_data isotope. Solve non-matched linedb isotope cases. Put all non-ignore isotopes in transit.ds.iso structure. Return: Number of lines read */ int getmnfromfile(FILE *fp, /* Pointer to atmospheric file */ struct atm_data *at, /* atmosphere structure */ struct transit *tr, /* transit structure */ PREC_ZREC *f_remainder){ /* Remainder molecules' factor */ struct molecules *mol=tr->ds.mol; char line[maxline], *lp; int nimol=0, /* Number of molecules with abundance profile */ nmol=0, /* Total number of molecules */ i; /* Auxiliary for-loop index */ double cumulother = 0; /* Cumulative remainder-molecules' factor */ int ipi = 0; /* Number of remainder molecules */ /* Is the isotope defined in the atm file?: */ //isoprop = (struct atm_isoprop *)calloc(ipa, sizeof(struct atm_isoprop)); at->begline = 0; /* Line where the info begins */ /* Read and store the keyword atmospheric variables: */ while(1){ switch(fgetupto_err(line, maxline, fp, &atmerr, atmfilename, at->begline++)){ /* Ignore comments and blank lines: */ case '\n': case '#': continue; case 0: /* Throw error if EOF */ transiterror(TERR_SERIOUS|TERR_ALLOWCONT, "readatm :: EOF unexpectedly found at line %i " "of file %s while no t,p data points have been read.\n", at->begline, atmfilename); exit(EXIT_FAILURE); continue; /* Determine whether abundance is by mass or number: */ case 'q': lp = line + 1; while(*lp++ == ' '); /* Skip blank spaces */ lp--; switch(*lp|0x20){ case 'n': at->mass = 0; /* Number abundance (mixing ratio) */ break; case 'm': at->mass = 1; /* Mass abundance (mass mixing ratio) */ break; default: transiterror(TERR_SERIOUS, "'q' option in the atmosphere file can only be followed " "by 'm' (for abundances by mass) or 'n' (for abundances " "by number). '%s' is invalid.\n", line); break; } continue; /* Zero radius value: */ case 'z': zerorad = atof(line+1); continue; /* Radius, temperature, or pressure units factor: */ case 'u': switch(line[1]){ case 'r': at->rads.fct = atof(line+2); break; case 'p': at->atm.pfct = atof(line+2); break; case 't': at->atm.tfct = atof(line+2); break; default: transiterror(TERR_SERIOUS, "Invalid unit factor indication in " "atmosphere file.\n"); exit(EXIT_FAILURE); } continue; case 'n': /* Name or identifier for file data */ storename(at, line+1); continue; case 'i': /* Molecule names with an abundance profile: */ /* Count the number of wrds (molecules) in line: */ nimol = countfields(line+1, ' '); transitprint(15, verblevel, "The number of molecules is %d.\n", nimol); /* Allocate Molecules names: */ mol->name = (char **)calloc(nimol, sizeof(char *)); mol->name[0] = (char *)calloc(nimol*maxeisoname, sizeof(char)); for(i=1; i<nimol; i++) mol->name[i] = mol->name[0] + i*maxeisoname; transitprint(1, verblevel, "Molecules with abundance profile:\n "); lp = line; lp = nextfield(lp); /* Skip keyword */ /* Read and store names: */ for (i=0; i<nimol; i++){ getname(lp, mol->name[i]); lp = nextfield(lp); transitprint(1, verblevel, "%s, ", mol->name[i]); } transitprint(1, verblevel, "\b\b.\n"); continue; /* Molecules with abundance proportional to the remainder: */ case 'f': lp = line; lp = nextfield(lp); /* Skip keyword */ /* Current total number of molecules: */ nmol = ++ipi + nimol; /* Re-allocate to add the new molecule: */ mol->name = (char **)realloc(mol->name, nmol*sizeof(char *)); mol->name[0] = (char *)realloc(mol->name[0], nmol*maxeisoname*sizeof(char)); for (i=1; i<nmol; i++) mol->name[i] = mol->name[0] + i*maxeisoname; /* Re-allocate remainder factors: */ f_remainder = (PREC_ZREC *)realloc(f_remainder, ipi*sizeof(PREC_ZREC)); /* Read and store the molecule's name: */ getname(lp, mol->name[nmol-1]); lp = nextfield(lp); /* Move pointer to next field */ if(*lp == '=') /* Skip an optional equal '=' sign */ lp++; /* Read and store factor: */ f_remainder[ipi-1] = strtod(lp, NULL); transitprint(30, verblevel, "%s remainder factor: %.3f\n", mol->name[nmol-1], f_remainder[ipi-1]); if(f_remainder[ipi-1] < 0) transiterror(TERR_CRITICAL, "Abundance ratio has to be positive in atmosphere " "file '%s' in line: '%s'.\n", atmfilename, line); continue; /* End of keyword variables: */ default: break; } break; } transitprint(1, verblevel, "Molecules with abundance proportional to " "remainder:\n "); for(i=nimol; i<nmol; i++) transitprint(1, verblevel, "%s, ", mol->name[i]); transitprint(1, verblevel, "\b\b.\n"); transitprint(3, verblevel, "Read all keywords in atmosphere file without " "problems.\n"); /* Set total number of molecules in atmosphere: */ mol->nmol = at->n_aiso = nmol; /* Check that there was at least one isotope defined and re-allocate array sizes to their final size: */ if(!nimol) transiterror(TERR_SERIOUS, "No isotopes were found in atmosphere file, " "make sure to specify them in a line starting " "with the letter 'i'. First non-comment line " "read:\n%s\n", line); /* Set position of beginning of data: */ at->begpos = ftell(fp) - strlen(line) - 1; /* Calculate cumulative fraction of remainder molecules: */ for(i=0; i < nmol-nimol; i++) cumulother += f_remainder[i]; transitprint(30, verblevel, "Cumulative remainder fraction: %.4f.\n", cumulother); /* Check that cumulother sums to 1.0 (within allowed errors): */ if(nmol>nimol && abs(1.0 - cumulother) > ROUNDTHRESH) transiterror(TERR_SERIOUS, "Sum of remainder-molecules fractional " "abundance (%g) must add to 1.0 +/- %g.\n", cumulother, ROUNDTHRESH); /* Resolve what to do with those isotopes that appear in the line transition database, but not in the atmosphere file. Get the number of non-ignored isotopes in atm_data without linelist: */ //at->n_niso = checknonmatch(tr, at, isodo); /* FINDME: This will be a task in readline (if actually needed). */ return at->begline; }
/* \fcnfh Read CIA info from tabulated files. Return: 0 on success */ int readcia(struct transit *tr){ FILE *fp; /* Pointer to CIA file */ char *file, /* CIA file name */ *colname; /* CIA isotope names */ PREC_CIA **a, /* CIA cross sections sample */ *wn; /* CIA sampled wavenumber array */ static struct cia st_cia; /* CIA structure */ tr->ds.cia = &st_cia; int npairs = tr->ds.cia->nfiles = tr->ds.th->ncia; /* Number of CIA files */ int p; /* Auxiliary wavenumber index */ long nt, wa; /* Number of temperature, wn samples in CIA file */ char rc; char *lp, *lpa; /* Pointers in file */ int maxline=300, n; /* Max length of line. Counter */ long lines; /* Lines read counter */ long i; /* Auxiliary for indices */ char line[maxline+1]; /* Array to hold line being read */ struct molecules *mol=tr->ds.mol; /* Make sure that radius and wavenumber samples exist: */ transitcheckcalled(tr->pi, "interpolatecia", 2, "makewnsample", TRPI_MAKEWN, "makeradsample", TRPI_MAKERAD); /* Allocate (output) transit extinction array (in cm-1): */ st_cia.e = (PREC_CIA **)calloc(tr->wns.n, sizeof(PREC_CIA *)); st_cia.e[0] = (PREC_CIA *)calloc(tr->wns.n*tr->rads.n, sizeof(PREC_CIA)); for(p=1; p < tr->wns.n; p++) st_cia.e[p] = st_cia.e[0] + p*tr->rads.n; memset(st_cia.e[0], 0, tr->wns.n*tr->rads.n*sizeof(double)); /* If there are no files, allocate tr.ds.cia.e (extinction) and return: */ if(!npairs){ return 0; } transitprint(1, verblevel, "Computing CIA opacities for %i database%s:\n", npairs, npairs>1 ? "s":""); /* Allocate string for molecule names: */ colname = (char *)calloc(maxline, sizeof(char)); /* Allocate molecules' ID: */ st_cia.mol1 = (int *)calloc(npairs, sizeof(int)); st_cia.mol2 = (int *)calloc(npairs, sizeof(int)); /* Number of temperature and wavenumber samples per file: */ st_cia.ntemp = (int *)calloc(npairs, sizeof(int)); st_cia.nwave = (int *)calloc(npairs, sizeof(int)); /* CIA, temperature and wavenumber samples: */ st_cia.cia = (PREC_CIA ***)calloc(npairs, sizeof(PREC_CIA **)); st_cia.temp = (PREC_CIA **)calloc(npairs, sizeof(PREC_CIA *)); st_cia.wn = (PREC_CIA **)calloc(npairs, sizeof(PREC_CIA *)); for(p=0; p < npairs; p++){ /* Copy file names from hint: */ file = xstrdup(tr->ds.th->ciafile[p]); /* Attempt to open the files: */ if((fp=fopen(file, "r")) == NULL) transiterror(TERR_SERIOUS, "Cannot read CIA file '%s'.\n", file); transitprint(10, verblevel, " CIA file (%d/%d): '%s'\n", p+1, npairs, file); lines = 0; /* lines read counter */ lpa = 0; /* Read the file headers: */ while(1){ /* Skip comments, blanks and read next line: */ while((rc=fgetupto_err(lp=line, maxline, fp, &ciaerr, file, lines++)) =='#' || rc=='\n'); /* If it is end of file, stop loop: */ if(!rc) transiterror(TERR_SERIOUS, "File '%s' finished before opacity info.\n", file); switch(rc){ case 'i': /* Read the name of the isotopes: */ while(isblank(*++lp)); /* Check that there are exactly two isotopes: */ if(countfields(lp, ' ') != 2) transiterror(TERR_SERIOUS, "Wrong line %i in CIA file '%s', if it begins with a " "'i', it should have the species separated by blank " "spaces. Rest of line:\n'%s'\n", lines, file, lp); st_cia.mol1[p] = st_cia.mol2[p] = -1; /* Allocate and copy the name of the first moleculee: */ getname(lp, colname); /* Find the ID of the first molecule: */ for(i=0; i<mol->nmol; i++) if(strcmp(mol->name[i], colname)==0) st_cia.mol1[p] = i; /* If the molecule is not in the atmosphere file: */ if(st_cia.mol1[p] == -1) transiterror(TERR_SERIOUS, "CIA molecule '%s' from file '%s' does " "not match any in the atmsopheric file.\n", colname, file); /* Allocate and store the name of the second isotope: */ lp = nextfield(lp); getname(lp, colname); for(i=0; i < mol->nmol; i++) if(strcmp(mol->name[i], colname)==0) st_cia.mol2[p] = i; if(st_cia.mol2[p] == -1) transiterror(TERR_SERIOUS, "CIA molecule '%s' from file '%s' does " "not match any in the atmsopheric file.\n", colname, file); transitprint(10, verblevel, " CIA molecules: [%s, %s]\n", mol->name[st_cia.mol1[p]], mol->name[st_cia.mol2[p]]); continue; case 't': /* Read the sampling temperatures array: */ while(isblank(*++lp)); nt = st_cia.ntemp[p] = countfields(lp, ' '); /* Number of temps. */ transitprint(10, verblevel, " Number of temperature samples: %ld\n", nt); if(!nt) transiterror(TERR_SERIOUS, "Wrong line %i in CIA file '%s', if it " "begins with a 't' then it should have the " "blank-separated fields with the temperatures. Rest " "of line: %s.\n", lines, file, lp); /* Allocate and store the temperatures array: */ st_cia.temp[p] = (PREC_CIA *)calloc(nt, sizeof(PREC_CIA)); n = 0; /* Count temperatures per line */ lpa = lp; /* Pointer in line */ transitprint(20, verblevel, " Temperatures (K) = ["); while(n < nt){ while(isblank(*lpa++)); st_cia.temp[p][n] = strtod(--lpa, &lp); /* Get value */ transitprint(20, verblevel, "%d, ", (int)st_cia.temp[p][n]); if(lp==lpa) transiterror(TERR_CRITICAL, "Less fields (%i) than expected (%i) " "were read for temperature in the CIA file '%s'.\n", n, nt, file); if((lp[0]|0x20) == 'k') lp++; /* Remove trailing K if exists */ lpa = lp; n++; } transitprint(20, verblevel, "\b\b]\n"); continue; default: break; } break; } /* Set an initial value for allocated wavenumber fields: */ wa = 32; /* Allocate wavenumber array: */ wn = (PREC_CIA *)calloc(wa, sizeof(PREC_CIA)); /* Allocate input extinction array (in cm-1 amagat-2): */ a = (PREC_CIA **)calloc(wa, sizeof(PREC_CIA *)); a[0] = (PREC_CIA *)calloc(wa*nt, sizeof(PREC_CIA)); for(i=1; i<wa; i++) a[i] = a[0] + i*nt; n=0; /* Read information for each wavenumber sample: */ while(1){ /* Skip comments and blanks; read next line: */ if (n) while((rc=fgetupto_err(lp=line, maxline, fp, &ciaerr, file, lines++)) =='#'||rc=='\n'); /* Stop, if it is end of file: */ if(!rc) break; /* Re-allocate (double the size) if necessary: */ if(n==wa){ wn = (PREC_CIA *)realloc(wn, (wa<<=1) * sizeof(PREC_CIA)); a = (PREC_CIA **)realloc(a, wa * sizeof(PREC_CIA *)); a[0] = (PREC_CIA *)realloc(a[0], wa * nt * sizeof(PREC_CIA)); for(i=1; i<wa; i++) a[i] = a[0] + i*nt; } /* Store new line: wavenumber first, then loop over cross sections: */ while(isblank(*lp++)); wn[n] = strtod(lp-1, &lpa); /* Store wavenumber */ if(lp==lpa+1) transiterror(TERR_CRITICAL, "Invalid fields for the %ith wavenumber " "in the CIA file '%s'.\n", n+1, file); i = 0; while(i<nt){ a[n][i] = strtod(lpa, &lp); /* Store cross section */ if(lp==lpa) transiterror(TERR_CRITICAL, "Less fields (%i) than expected (%i) " "were read for the %ith wavenumber in the CIA " "file '%s'.\n", i, nt, n+1, file); lpa = lp; i++; } n++; } /* Re-allocate arrays to their final sizes: */ if(n<wa){ st_cia.wn[p] = (PREC_CIA *)realloc(wn, n* sizeof(PREC_CIA)); a = (PREC_CIA **)realloc(a, n* sizeof(PREC_CIA *)); a[0] = (PREC_CIA *)realloc(a[0], n*nt*sizeof(PREC_CIA)); for(i=1; i<n; i++) a[i] = a[0] + i*nt; } transitprint(10, verblevel, " Number of wavenumber samples: %d\n", n); transitprint(20, verblevel, " Wavenumber array (cm-1) = [%.1f, %.1f, " "%.1f, ..., %.1f, %.1f, %.1f]\n", st_cia.wn[p][0], st_cia.wn[p][1], st_cia.wn[p][2], st_cia.wn[p][n-3], st_cia.wn[p][n-2], st_cia.wn[p][n-1]); st_cia.cia[p] = a; st_cia.nwave[p] = n; fclose(fp); } /* FINDME: The program breaks when I free colname, it makes no sense */ free(colname); transitprint(1, verblevel, "Done.\n"); tr->pi |= TRPI_CIA; return 0; }