Ejemplo n.º 1
0
struct disp_struct *
disp_table_new_from_nk_file (const char * filename)
{
  struct disp_struct *disp;
  struct disp_table *table;
  FILE * f;
  int j, npt, nread;
  float wlmax, wlmin;

  disp = disp_new (DISP_TABLE);
  table = & disp->disp.table;

  f = fopen (filename, "r");

  if (f == NULL)
    {
      notify_error_msg (LOADING_FILE_ERROR, "Cannot open %s", filename);
      return NULL;
    }

  nread = fscanf(f, "%*i %f %f %i\n", & wlmin, & wlmax, &npt);
  if (nread < 3)
    {
      notify_error_msg (LOADING_FILE_ERROR, "File %s not in NK format",
			filename);
      return NULL;
    }

  disp_table_init (table, npt+1);

  for (j = 0; j <= npt; j++)
    {
      float nr, ni;

      nread = fscanf(f, "%f %f\n", & nr, & ni);
      if (nread < 2)
	{
	  notify_error_msg (LOADING_FILE_ERROR, "invalid format for nk table");
	  goto disp_nk_free;
	}

      set_index_value (table, j, nr, ni);
    }

  set_range (table, wlmin * 1.0E3, wlmax * 1.0E3);

  fclose (f);
  return disp;

 disp_nk_free:
  disp_table_free (disp);
  fclose (f);
  return NULL;
}
Ejemplo n.º 2
0
void *
retrieve_parsed_object (struct symtab *symtab, enum toplevel_type tp,
			str_ptr name)
{
  obj_t *obj;

  obj = symbol_table_find (symtab, name);

  if (obj == NULL)
    {
      notify_error_msg (SCRIPT_ERROR, "default %s not defined",
			tl_type_name[tp]);
      return NULL;
    }

  if (obj->type != tp)
    {
      notify_error_msg (SCRIPT_ERROR, "default %s is invalid",
			tl_type_name[tp]);
      return NULL;
    }

  return obj->cont.generic;
}
Ejemplo n.º 3
0
int
set_config_value_raw (struct symtab *symtab, lex_string_t *id, val_t *v)
{
  struct fit_config *cfg = symtab->config_table;

  if (LEX_STRING_EQUAL(id, "range"))
    {
      if (v->type == VAL_TP_STRING)
	{
	  const char *s = CSTR(v->cont.string);
	  char *tail;
	  double inf, sup;
	  inf = strtod (s, &tail);
	  if (tail == s || *tail != '-')
	    goto range_exit_err;
	  s = tail + 1;
	  sup = strtod (s, &tail);
	  if (tail == s || *tail != 0)
	    goto range_exit_err;
	  cfg->spectr_range.active = 1;
	  cfg->spectr_range.min = inf;
	  cfg->spectr_range.max = sup;
	  return 0;
	}
    range_exit_err:
      notify_error_msg (SCRIPT_ERROR, "Invalid range specification.");
      return 1;
    }
  else if (LEX_STRING_EQUAL(id, "chisq_thresold"))
    {
      if (v->type == VAL_TP_DOUBLE)
	{
	  cfg->chisq_thresold = v->cont.real;
	  cfg->thresold_given = 1;
	  return 0;
	}

    }
  else if (LEX_STRING_EQUAL(id, "max_iterations"))
    {
      if (v->type == VAL_TP_DOUBLE)
	{
	  cfg->nb_max_iters = (int) v->cont.real;
	  return 0;
	}
    }
  else if (LEX_STRING_EQUAL(id, "subsampling"))
    {
      if (v->type == VAL_TP_STRING)
	{
	  if (strcmp (CSTR(v->cont.string), "on") == 0)
	    {
	      cfg->subsampling = 1;
	      return 0;
	    }
	  else if (strcmp (CSTR(v->cont.string), "off") == 0)
	    {
	      cfg->subsampling = 0;
	      return 0;
	    }
	  notify_error_msg (SCRIPT_ERROR, "subsampling should be either "
			    "\"on\" or \"off\" (with quotes).");
	  return 1;
	}
    }
  else if (LEX_STRING_EQUAL(id, "directory"))
    {
      if (v->type == VAL_TP_STRING)
	{
	  int status = chdir (CSTR(v->cont.string));
	  if (status != 0)
	    {
	      notify_error_msg (SCRIPT_ERROR, "%s.", strerror (errno));
	      return 1;
	    }
	  return 0;
	}
    }

  notify_error_msg (SCRIPT_ERROR, "Invalid value.");
  return 1;
}
Ejemplo n.º 4
0
struct spectrum *
load_ellips_spectrum(const char *filename, struct extra_param *einf) {
    struct spectrum *spectr = NULL;
    enum spectra_kind spkind;
    FILE *f;
    int iseof, npt, nr;
    str_t ln;

sr_start:
    f = fopen(filename, "r");

    if(f == NULL) {
        notify_error_msg(LOADING_FILE_ERROR, "error loading spectra %s",
                         filename);
        return NULL;
    }

    str_init(ln, 64);
    str_getline(ln, f);
    if(strstr(CSTR(ln), "SE ALPHA BETA")) {
        spkind = AB_ELLISS_SPECTR;
    } else if(strstr(CSTR(ln), "SE PSI DELTA")) {
        spkind = PSIDEL_ELLISS_SPECTR;
    } else {
        goto no_good;
    }

    einf->aoi = 71.7;
    einf->analyzer = 25.0;
    einf->numap = 0.0;

    for(;;) {
        iseof = str_getline(ln, f);

        if(iseof != 0) {
            break;
        }

        nr = sscanf(CSTR(ln), "AOI %lf", & einf->aoi);
        if(nr == 1) {
            continue;
        }

        nr = sscanf(CSTR(ln), "NA %lf", & einf->numap);
        if(nr == 1) {
            continue;
        }

        nr = sscanf(CSTR(ln), "A %lf", & einf->analyzer);
        if(nr == 1) {
            continue;
        }

        break;
    }

    einf->aoi = DEGREE(einf->aoi);
    einf->analyzer = DEGREE(einf->analyzer);

    for(npt = 0; ; npt++, iseof = str_getline(ln, f)) {
        double a[3];
        double *dt[3] = {a, a+1, a+2};
        int k;

        if(spectr) {
            dt[0] = & spectr->lambda[npt];
            dt[1] = & spectr->data.ab[npt].alpha;
            dt[2] = & spectr->data.ab[npt].beta;
        }

        while(iseof == 0) {
            k = sscanf(CSTR(ln), "%*s %lf %lf %lf\n", dt[0], dt[1], dt[2]);
            if(k == 3) {
                break;
            }
            iseof = str_getline(ln, f);
        }

        if(iseof != 0) {
            if(spectr != NULL) {
                break;
            }

            if(npt < 2) {
                goto no_good;
            }

            spectr = emalloc(sizeof(struct spectrum));
            spectr->kind = spkind;
            spectr->npt = npt;
            spectr->lambda  = emalloc(npt * sizeof(double));
            spectr->data.ab = emalloc(npt * sizeof(struct elliss_ab));

            fclose(f);
            str_free(ln);

            goto sr_start;
        }
    }

    spectr->lmin = spectr->lambda[0];
    spectr->lmax = spectr->lambda[spectr->npt - 1];

    str_free(ln);
    fclose(f);

    return spectr;
no_good:
    notify_error_msg(LOADING_FILE_ERROR, "format of spectra %s is incorrect",
                     filename);
    str_free(ln);
    fclose(f);
    return NULL;
}
Ejemplo n.º 5
0
disp_t *
disp_sample_table_new_from_mat_file (const char * filename)
{
  FILE * f;
  str_t row;
  disp_t *disp = NULL;
  enum disp_type dtype;
  int convert_ev = 0;
  int provide_diel_k = 0;

  f = fopen (filename, "r");

  if (f == NULL)
    {
      notify_error_msg (LOADING_FILE_ERROR, "Cannot open %s", filename);
      return NULL;
    }

  str_init (row, 64);

  str_getline (row, f);
  str_getline (row, f);

  if (strncasecmp (CSTR(row), "CAUCHY", 6) == 0)
    {
      dtype = DISP_CAUCHY;
    }
  else if (strncasecmp (CSTR(row), "ev", 2) == 0)
    {
      convert_ev = 1;
    }
  else if (strncasecmp (CSTR(row), "nm", 2) != 0)
    {
      notify_error_msg (LOADING_FILE_ERROR, "Invalide MAT format: %s",
			filename);
      goto close_exit;
    }

  str_getline (row, f);
  if (strncasecmp (CSTR(row), "nk", 2) == 0)
    {
      dtype = DISP_SAMPLE_TABLE;
    }
  else if (strncasecmp (CSTR(row), "e1e2", 4) == 0)
    {
      dtype = DISP_SAMPLE_TABLE;
      provide_diel_k = 1;
    }
  else
    {
      notify_error_msg (LOADING_FILE_ERROR, "Invalide MAT format: %s",
			filename);
      goto close_exit;
    }
    
  switch (dtype)
    {
    case DISP_SAMPLE_TABLE:
      {
	struct disp_sample_table *dt;
	struct data_table *table;
	long start_pos = ftell (f);
	int j, lines;

	disp = disp_new (DISP_SAMPLE_TABLE);
	dt = & disp->disp.sample_table;
	disp_sample_table_clear (dt);

	start_pos = ftell (f);

	for (lines = 0; ; )
	  {
	    float xd[3];
	    int read_status = fscanf (f, "%f %f %f\n", xd, xd+1, xd+2);
	    if (read_status == 3)
	      lines ++;
	    if (read_status == EOF)
	      break;
	  }

	if (lines < 2)
	  {
	    disp_free (disp);
	    disp = NULL;
	    break;
	  }

	fseek (f, start_pos, SEEK_SET);
	
	disp_sample_table_init (dt, lines);
	
	table = dt->table_ref;

	for (j = 0; j < lines; j++)
	  {
	    float *dptr = table->heap + 3 * j;
	    int read_status;
	    do
	      read_status = fscanf (f, "%f %f %f\n", dptr, dptr+1, dptr+2);
	    while (read_status < 3 && read_status != EOF);

	    if (convert_ev)
	      dptr[0] = 1239.8 / dptr[0];

	    if (provide_diel_k)
	      {
		double e1 = dptr[1], e2 = dptr[2];
		double ne = sqrt(e1*e1 + e2*e2);
		dptr[1] = sqrt((ne + e1) / 2.0);
		dptr[2] = sqrt((ne - e1) / 2.0);
	      }

	    if (read_status == EOF)
	      break;
	  }

	break;
      }	  
    case DISP_CAUCHY:
      notify_error_msg (LOADING_FILE_ERROR, "cauchy MAT files");
      break;
#if 0
      cn = disp->disp.cauchy.n;
      ck = disp->disp.cauchy.k;
      fscanf (f, "%lf %lf %lf %*f %*f %*f\n", cn, cn+1, cn+2);
      cn[1] *= 1e3;
      cn[2] *= 1e6;
      ck[0] = ck[1] = ck[2] = 0.0;
      break;
#endif
    default:
      notify_error_msg (LOADING_FILE_ERROR, "corrupted material card");
      break;
    }

 close_exit:
  fclose (f);
  str_free (row);
  return disp;
}