Ejemplo n.º 1
0
/* \fcnfh
   Wrapper for fgetupto() with error parameters previously stored
*/
inline char
fgetupto(char *line,
	 int max,
	 FILE *fp)
{
  long zero=0;
  if (!fgut_currline) fgut_currline = &zero;
  return fgetupto_err(line, max, fp, fgut_errfcn, fgut_file,
		      *fgut_currline);
}
Ejemplo n.º 2
0
/* \fcnfh
   Read abundances and pressure for each isotope and radius

   @returns number of radius point
*/
int
readatmfile(FILE *fp,		/* File */
	    struct transit *tr, /* transit info */
	    struct atm_data *at, /* atmosphere info */
	    prop_samp *rads,	/* radius sampling */
	    int nrad)		/* number of allocated radii, note that
				   is not returned updated */
{
  //find abundance related quantities for each radius
  int lines=at->begline;
  PREC_NREC r=0;
  PREC_RES tmp;
  char rc;
  float allowq=1-tr->allowrq;
  double sumq;
  char line[maxline],*lp,*lp2;
  prop_isov *isov=at->isov;
  int *isoeq=at->isoeq;
  struct isotopes *iso=tr->ds.iso;
  enum isodo *isodo=at->isodo;
  int i,neiso=iso->n_e;

  fseek(fp,at->begpos,SEEK_SET);
  while(1){
    //reallocate if necessary
    if(r==nrad){
      nrad<<=1;
      rads->v=(PREC_ATM *)realloc(rads->v,nrad*sizeof(PREC_ATM));
      at->atm.t= (PREC_ATM *)realloc(at->atm.t,nrad*sizeof(PREC_ATM));
      at->atm.p= (PREC_ATM *)realloc(at->atm.p,nrad*sizeof(PREC_ATM));
      at->mm=(double *)realloc(at->mm,nrad*sizeof(double));
      for(i=0;i<neiso;i++){
	isov[i].d=(PREC_ATM *)realloc(isov[i].d,
				      nrad*sizeof(PREC_ATM));
	isov[i].q=(PREC_ATM *)realloc(isov[i].q,
				      nrad*sizeof(PREC_ATM));
	isov[i].n=nrad;
      }
    }

    //Skip comments and read next line
    while((rc=fgetupto_err(lp=line,maxline,fp,&atmerr,atmfilename,lines++))
	  =='#'||rc=='\n');
    //if it is end of file, stop loop
    if(!rc)
      break;

    tmp=rads->v[r]=strtod(lp,&lp2)+zerorad;
    checkposvalue(tmp,1,lines);
    if(lp==lp2) 
      invalidfield(line, lines, 1, "radius");
    tmp=at->atm.p[r]=strtod(lp2,&lp);
    checkposvalue(tmp,2,lines);
    if(lp==lp2)
      invalidfield(line, lines, 2, "pressure");
    tmp=at->atm.t[r]=strtod(lp,&lp2);
    checkposvalue(tmp,3,lines);
    if(lp==lp2)
      invalidfield(line, lines, 3, "temperature");

    //variables to be used by factor (except ieq which is general)
    int ieq, feq;
    double ref;
    _Bool otherfct[neiso];
    memset(otherfct,0,sizeof(otherfct));

    //now read abundances for every isotope, but don't process
    //factorized elements. Because they might be proportional to a fixed
    //element which is set below.
    for(i=0;i<at->n_aiso;i++){
      ieq=isoeq[i];
      switch(isodo[i]){
      case fixed:
	if(!r){
	  isov[ieq].q[0]=askforposd(" %s abundance for isotope %s: "
				    ,at->mass?"Mass":"Number"
				    ,iso->isof[ieq].n);
	  if(isov[ieq].q[0]>=1){
	    fprintf(stderr," Abundance for any single isotope has to be"
		    " less than one\n Try Again!\n");
	    i--;
	  }
	}
	else
	  isov[ieq].q[r]=isov[ieq].q[0];
	break;
      case factor:
	//don't process yet those that will use whatever abundance is left
	//to complete unity
	feq=ieq;
	ieq=isoprop[feq].eq;
	if(strcasecmp(isoprop[feq].t,"other")==0){
	  otherfct[ieq]=1;
	  continue;
	}
	//find the reference value
	ref=findfactq(isoprop[feq].t,iso->isof,isov,neiso,r);
	isov[ieq].q[r]=isoprop[feq].f*ref;
	break;
      default:
	transiterror(TERR_CRITICAL,
		     "Trying to read isotope in readatmfile() which is\n"
		     "not 'fixed', 'atmfile', 'ignored', nor 'factor'.\n"
		     );
	exit(EXIT_FAILURE);
	break;
      case atmfile:
      case ignore:
	transitASSERT(ieq<0 || 
		      (isodo[i]==ignore&&ieq>=nfonly) || 
		      (isodo[i]!=ignore&&ieq>=iso->n_e),
		      "Assertion failed in file %s, line %i: %i!=[0,%i].\n"
		      " Fonly: %i\n"
		      ,__FILE__, __LINE__, isoeq[i], 
		      isodo[i]==ignore?nfonly:iso->n_e-1, isodo[i]==ignore);
	//Read the abundance of the new element. There are two ways:      
	//If processing one of the factor only elements
	if(isodo[i]==ignore)
	  tmp=fonly[ieq].q=strtod(lp2,&lp);
	//otherwise if this element is going to be considered
	else
	  tmp=isov[ieq].q[r]=strtod(lp2,&lp);
	checkposvalue(tmp, i+4, lines);

	if(lp==lp2)
	  invalidfield(line, lines, 4+i, "isotope abundance");
	lp2=lp;
	break;
      }
    }


    //process factorized elements that will take care of the rest of the
    //atmosphere
    ref=1-addq(isov,iso->isodo,otherfct,neiso,r);
    for(i=0;i<at->n_aiso;i++)
      if(isodo[i]==factor){
	feq=isoeq[i];
	ieq=isoprop[feq].eq;
	if(otherfct[ieq])
	  isov[ieq].q[r]=isoprop[feq].f*ref;
      }

    //calculate mean molecular mass and check whether abundances add up
    //correctly, up to round off error of course
    sumq=checkaddmm(at->mm+r,r,isov,iso->isof,neiso,at->mass,iso->isodo);
    if((int)(sumq*ROUNDOFF+0.5)<(int)(allowq*ROUNDOFF+0.5))
      transiterror(TERR_WARNING,
		   "In radius %g(%i: %g in file), abundances\n"
		   "don't add up to 1: %.9g\n"
		   ,at->rads.v[r],r,at->rads.v[r]-zerorad,sumq);


    //Calculate densities
    for(i=0;i<neiso;i++)
      isov[i].d[r]=stateeqnford(at->mass,
				isov[i].q[r],
				at->mm[r],
				iso->isof[i].m,
				at->atm.p[r]*at->atm.pfct,
				at->atm.t[r]*at->atm.tfct);
    r++;
  }

  //reduce array to the right number of radii
  rads->n=nrad=r;
  rads->v=(PREC_ATM *)realloc(rads->v,nrad*sizeof(PREC_ATM));
  at->atm.t= (PREC_ATM *)realloc(at->atm.t,nrad*sizeof(PREC_ATM));
  at->atm.p= (PREC_ATM *)realloc(at->atm.p,nrad*sizeof(PREC_ATM));
  at->mm=(double *)realloc(at->mm,nrad*sizeof(double));
  for(i=0;i<neiso;i++){
    isov[i].d=(PREC_ATM *)realloc(isov[i].d,
				  nrad*sizeof(PREC_ATM));
    isov[i].q=(PREC_ATM *)realloc(isov[i].q,
				  nrad*sizeof(PREC_ATM));
    isov[i].n=nrad;
  }

  //free arrays that were used only to get the factorizing elements
  free(fonly);


  return nrad;
}
Ejemplo n.º 3
0
/* \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;
}
Ejemplo n.º 4
0
/* \fcnfh
    Read radius, pressure, temperature, and abundances and store it into
    at_data of transit.  Calculate mean molecular mass and densities.

    Detailed:
    Read and store radius, pressure, and temperature from file.
    Read abundances for each (non other-factor) isotope.
    Sum fractional abundances. Calculate ramaining (other-factor) abundances.
    Calculate mean molecular mass per radius.
    Calculate densities per isotope at each radius.

    Returns: number of sample radius                                         */
int
readatmfile(FILE *fp,                /* Atmospheric file               */
            struct transit *tr,      /* transit struct                 */
            struct atm_data *at,     /* Atmosphere struct              */
            prop_samp *rads,         /* Radius sampling                */
            int nrad,                /* Size of allocated radius array */
            PREC_ZREC *f_remainder){ /* Remainder molecules' factor    */

  transitprint(1, verblevel, "Start reading abundances.\n");
  /* Find abundance related quantities for each radius */
  int lines = at->begline;
  PREC_NREC r = 0; /* Radius index (number of radii being read) */
  char rc;         /* File reading output */
  float allowq = 1 - tr->allowrq;
  int nabundances;  /* Number of abundances in list */
  double sumq;      /* Sum of abundances per line   */
  char line[maxline], *lp, *lp2;
  prop_mol *molec = at->molec;
  struct molecules *mol = tr->ds.mol;
  int i, j;            /* Auxiliary for-loop indices */
  /* Variables to be used by factor (except ieq which is general): */

  /* Count the number of abundances in each line:                      */
  fseek(fp, at->begpos, SEEK_SET); /* Go to position where data begins */
  /* Skip comments:                    */
  while((rc=fgetupto_err(lp=line, maxline, fp, &atmerr, atmfilename, lines++))
        =='#' || rc=='\n');
  /* Count values per line:            */
  nabundances = countfields(lp, ' ') - 3; /* Subtract rad, p, and T columns */
 
  fseek(fp, at->begpos, SEEK_SET); /* Go to position where data begins */
  while(1){
    /* Reallocate if necessary: */
    if(r==nrad){
      nrad <<= 1;
      rads->v     = (PREC_ATM *)realloc(rads->v,   nrad*sizeof(PREC_ATM));
      at->atm.t   = (PREC_ATM *)realloc(at->atm.t, nrad*sizeof(PREC_ATM));
      at->atm.p   = (PREC_ATM *)realloc(at->atm.p, nrad*sizeof(PREC_ATM));
      at->mm      = (double   *)realloc(at->mm,    nrad*sizeof(double));
      for(i=0; i<at->n_aiso; i++){
        molec[i].d = (PREC_ATM *)realloc(molec[i].d, nrad*sizeof(PREC_ATM));
        molec[i].q = (PREC_ATM *)realloc(molec[i].q, nrad*sizeof(PREC_ATM));
        molec[i].n = nrad;
      }
    }

    /* Skip comments and read next line: */
    while((rc=fgetupto_err(lp=line, maxline, fp, &atmerr, atmfilename, lines++))
          =='#' || rc=='\n');
    /* If it is end of file, stop loop: */
    if(!rc)
      break;

    /* Read and store radius, pressure, and temperature from file: */
    rads->v[r] = strtod(lp, &lp2) + zerorad; /* Radius       */
    checkposvalue(rads->v[r], 1, lines);       /* Check value is positive */
    if(lp==lp2) 
      invalidfield(line, lines, 1, "radius");
    at->atm.p[r] = strtod(lp2, &lp);         /* Pressure     */
    checkposvalue(at->atm.p[r], 2, lines); 
    if(lp==lp2)
      invalidfield(line, lines, 2, "pressure");
    at->atm.t[r] = strtod(lp, &lp2);         /* Temperature  */
    checkposvalue(at->atm.t[r], 3, lines);
    if(lp==lp2)
      invalidfield(line, lines, 3, "temperature");

    /* Read abundances for each isotope.  Keep reading-in values
       while there are numbers in line:                            */
    for(i=0, sumq=0; i<nabundances; i++){
      lp = lp2;
      /* Read the abundance of the isotope:                        */
      molec[i].q[r] = strtod(lp, &lp2);
      if (r==0)
        transitprint(30, verblevel, "density[%d, %li]: %.9f.\n",
                                    i, r, molec[i].q[r]);
      sumq += molec[i].q[r]; /* Add the abundances */
      checkposvalue(molec[i].q[r], i+4, lines); /* Check that tmp is positive */
      if(lp==lp2)
        invalidfield(line, lines, 4+i, "isotope abundance");
    }

    /* Remainder of the sum of abundances:     */
    /* Set abundance of remainder molecules:   */
    for(j=0; i < at->n_aiso; i++, j++)
      molec[i].q[r] = f_remainder[j]*(1-sumq);

    transitASSERT(i!=at->n_aiso, "The line %s of file %s contains %d abundance "
                                 "values, when there were %d expected.\n",
                                 __LINE__, __FILE__, i, at->n_aiso);
    
    /* Calculate mean molecular mass and check whether abundances add up
       to one (within roundoff error): */
    sumq = checkaddmm(at->mm+r, r, molec, mol, at->n_aiso, at->mass);
    if((int)(sumq*ROUNDOFF+0.5)<(int)(allowq*ROUNDOFF+0.5))
      transiterror(TERR_WARNING,
                   "In radius %g (%i: %g in file), abundances "
                   "don't add up to 1: %.9g\n",
                   at->rads.v[r], r, at->rads.v[r]-zerorad, sumq);

    /* Calculate densities using ideal gas law: */
    if (r>=0){
      transitprint(30, verblevel, "Abund: %.9f, mmm: %.3f, mass: %.3f, "
                                "p: %.3f, T: %.3f.\n", molec[2].q[r], at->mm[r],
                                   mol->mass[2], at->atm.p[r]*at->atm.pfct,
                                   at->atm.t[r]*at->atm.tfct);
    }
    for(i=0; i<at->n_aiso; i++)
      molec[i].d[r] = stateeqnford(at->mass, molec[i].q[r], at->mm[r],
                                   mol->mass[i], at->atm.p[r]*at->atm.pfct,
                                   at->atm.t[r]*at->atm.tfct);
    transitprint(30, verblevel, "dens[%2li]: %.14f,   ", r, molec[2].d[r]);
    r++;
  }

  /* Re-allocate arrays to final size (nrad):  */
  rads->n = nrad = r;
  rads->v   = (PREC_ATM *)realloc(rads->v,   nrad*sizeof(PREC_ATM));
  at->atm.t = (PREC_ATM *)realloc(at->atm.t, nrad*sizeof(PREC_ATM));
  at->atm.p = (PREC_ATM *)realloc(at->atm.p, nrad*sizeof(PREC_ATM));
  at->mm    = (double   *)realloc(at->mm,    nrad*sizeof(double));
  for(i=0; i<at->n_aiso; i++){
    molec[i].d = (PREC_ATM *)realloc(molec[i].d, nrad*sizeof(PREC_ATM));
    molec[i].q = (PREC_ATM *)realloc(molec[i].q, nrad*sizeof(PREC_ATM));
    molec[i].n = nrad;
  }

  /* Free arrays that were used only to get the factorizing elements: */
  free(fonly);
  nfonly = 0;

  return nrad;
}
Ejemplo n.º 5
0
/* \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;
}
Ejemplo n.º 6
0
/* \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;
}