Example #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;
}
Example #2
0
disp_t *
disp_sample_table_new_from_mat_file(const char * filename, str_ptr *error_msg)
{
    FILE * f;
    str_t row;
    disp_t *disp = NULL;
    enum disp_type dtype;
    unsigned int wl_unit_conv = 0;
    int provide_diel_k = 0;

    f = fopen(filename, "r");

    if(f == NULL) {
        *error_msg = new_error_message(LOADING_FILE_ERROR, "File \"%s\" does not exists or cannot be opened", filename);
        return NULL;
    }

    str_t name;
    str_init(name, 64);
    str_init(row, 64);
    str_getline(name, f);
    str_getline(row, f);

    if(strncasecmp(CSTR(row), "CAUCHY", 6) == 0) {
        dtype = DISP_CAUCHY;
    } else if(strncasecmp(CSTR(row), "ev", 2) == 0) {
        wl_unit_conv |= WL_UNIT_CONVERT_EV;
    } else if(strncasecmp(CSTR(row), "ANGSTROMS", 9) == 0) {
        wl_unit_conv |= WL_UNIT_CONVERT_ANGSTROMS;
    } else if(strncasecmp(CSTR(row), "nm", 2) != 0) {
        *error_msg = new_error_message(LOADING_FILE_ERROR, "Invalide MAT file: \"%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 {
        *error_msg = new_error_message(LOADING_FILE_ERROR, "Invalide MAT file: \"%s\"", filename);
        goto close_exit;
    }

    switch(dtype) {
    case DISP_SAMPLE_TABLE: {
        struct disp_sample_table *dt;
        long start_pos = ftell(f);
        int j, lines;

        disp = disp_new(DISP_SAMPLE_TABLE);
        str_copy(disp->name, name);
        dt = & disp->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);

        init(dt, lines);

        double *wptr = wavelength_array(dt);
        double *nptr = n_array(dt);
        double *kptr = k_array(dt);
        for(j = 0; j < lines; j++, wptr++, nptr++, kptr++) {
            int read_status;
            do {
                read_status = fscanf(f, "%lf %lf %lf\n", wptr, nptr, kptr);
            } while(read_status < 3 && read_status != EOF);

            if(wl_unit_conv & WL_UNIT_CONVERT_EV) {
                *wptr = 1239.8 / *wptr;
            } else if(wl_unit_conv & WL_UNIT_CONVERT_ANGSTROMS) {
                *wptr /= 10.0;
            }

            if(provide_diel_k) {
                double e1 = *nptr, e2 = *kptr;
                double ne = sqrt(e1*e1 + e2*e2);
                *nptr = sqrt((ne + e1) / 2.0);
                *kptr = sqrt((ne - e1) / 2.0);
            }

            if(read_status == EOF) {
                break;
            }
        }
        prepare_interp(dt);
        break;
    }
    case DISP_CAUCHY:
        *error_msg = new_error_message(LOADING_FILE_ERROR, "cauchy MAT format is unsupported");
        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:
        *error_msg = new_error_message(LOADING_FILE_ERROR, "corrupted material card");
        break;
    }

close_exit:
    fclose(f);
    str_free(name);
    str_free(row);
    return disp;
}
Example #3
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;
}