Exemplo n.º 1
0
ide::transform_flags
ide::CBFAdaptor::transform_flags() const{
    std::string precedence,direction;
    const char *temp_value;
    ide::transform_flags T;
    T.transpose = file_is_transposed();
    cbf_failnez ( cbf_find_category(cbf_h,"array_structure_list") );

      cbf_failnez ( cbf_rewind_column(cbf_h) );
      cbf_failnez ( cbf_find_column(cbf_h,"precedence") );
      cbf_failnez ( cbf_rewind_row(cbf_h) );
      precedence = "1";
      cbf_failnez ( cbf_find_row(cbf_h,precedence.c_str()) );
      cbf_failnez ( cbf_rewind_column(cbf_h) );
      cbf_failnez ( cbf_find_column(cbf_h,"direction") );
      cbf_failnez ( cbf_get_value(cbf_h,&temp_value) );
      direction = std::string(temp_value);
      SCITBX_ASSERT( direction == "increasing" || direction == "decreasing" );
      T.reverse_fast = (direction=="decreasing");

      cbf_failnez ( cbf_rewind_column(cbf_h) );
      cbf_failnez ( cbf_find_column(cbf_h,"precedence") );
      cbf_failnez ( cbf_rewind_row(cbf_h) );
      precedence = "2";
      cbf_failnez ( cbf_find_row(cbf_h,precedence.c_str()) );
      cbf_failnez ( cbf_rewind_column(cbf_h) );
      cbf_failnez ( cbf_find_column(cbf_h,"direction") );
      cbf_failnez ( cbf_get_value(cbf_h,&temp_value) );
      direction = std::string(temp_value);
      SCITBX_ASSERT( direction == "increasing" || direction == "decreasing" );
      T.reverse_slow = (direction=="decreasing");

    return T;
}
Exemplo n.º 2
0
std::string
ide::CBFAdaptor::raster_description(){
    std::string precedence,direction;
    std::string result="";
    const char *temp_value;
    cbf_failnez ( cbf_find_category(cbf_h,"array_structure_list") );
    typedef std::vector<std::string> el_lst;
    el_lst elements;
    elements.push_back("ELEMENT_X");elements.push_back("ELEMENT_Y");

    for (el_lst::const_iterator e = elements.begin(); e!=elements.end(); ++e) {
      cbf_failnez ( cbf_find_column(cbf_h,"axis_set_id") );
      cbf_failnez ( cbf_rewind_row(cbf_h) );
      cbf_failnez ( cbf_find_row(cbf_h,e->c_str()) );
      cbf_failnez ( cbf_rewind_column(cbf_h) );
      cbf_failnez ( cbf_find_column(cbf_h,"direction") );
      cbf_failnez ( cbf_get_value(cbf_h,&temp_value) );
      direction = std::string(temp_value);
      cbf_failnez ( cbf_rewind_column(cbf_h) );
      cbf_failnez ( cbf_find_column(cbf_h,"precedence") );
      cbf_failnez ( cbf_get_value(cbf_h,&temp_value) );
      precedence = std::string(temp_value);
      result = result+std::string(*e)+" "+direction+" precedence="+precedence+'\n';
    }

    return result;
}
Exemplo n.º 3
0
bool
ide::CBFAdaptor::file_is_transposed() const{
    std::string elem,precedence;
    const char *temp_value;
    cbf_failnez ( cbf_find_category(cbf_h,"array_structure_list") );
    elem = "ELEMENT_X";

    cbf_failnez ( cbf_find_column(cbf_h,"axis_set_id") );
    cbf_failnez ( cbf_rewind_row(cbf_h) );
    cbf_failnez ( cbf_find_row(cbf_h,elem.c_str()) );
    cbf_failnez ( cbf_rewind_column(cbf_h) );
    cbf_failnez ( cbf_find_column(cbf_h,"precedence") );
    cbf_failnez ( cbf_get_value(cbf_h,&temp_value) );
    precedence = std::string(temp_value);

    if (precedence=="1") return false; //ELEMENT_X precedence=1 means it is fastest
    if (precedence=="2") return true;  //ELEMENT_X precedence=2 means it is slow
    throw iotbx::detectors::Error ("Unable to determine precedence of ELEMENT_X");
}
Exemplo n.º 4
0
int main (int argc, char *argv [])
{
  FILE *in, *out;

  clock_t a,b;

  img_handle img, cbf_img;

  cbf_handle cbf;

  int id, index;

  unsigned int column, row;

  size_t nelem_read;

  double pixel_size, gain, wavelength, distance;

  int overload, dimension [2], precedence [2];

  const char *detector;

  char *detector_char;

  char detector_id [64];

  const char *direction [2], *array_id;


     /* Usage */ 

  if (argc < 3)
  {
    fprintf (stderr, "\n Usage: %s imagefile cbffile\n", argv [0]);

    exit (2);
  }


    /* Read the image */

  img = img_make_handle ();

  a = clock ();

  cbf_failnez (img_read (img, argv [1]))

  b = clock ();

  fprintf (stderr, " Time to read the image: %.3fs\n", ((b - a) * 1.0) / CLOCKS_PER_SEC);


    /* Get some detector parameters */

    /* Detector identifier */

  detector = img_get_field (img, "DETECTOR");

  if (!detector)

    detector = "unknown";

  strncpy (detector_id, detector, 63);

  detector_id [63] = 0;

  detector_char = detector_id;

  while (*detector_char)

    if (isspace (*detector_char))

      memmove (detector_char, detector_char + 1, strlen (detector_char));

    else
    {
      *detector_char = tolower (*detector_char);

      detector_char++;
    }


    /* Pixel size */
    
  pixel_size = img_get_number (img, "PIXEL SIZE") * 0.001;


    /* Wavelength */

  wavelength = img_get_number (img, "WAVELENGTH");
  

    /* Distance */

  distance = img_get_number (img, "DISTANCE") * 0.001;
  

    /* Image size and orientation & gain and overload */

  if (strcmp (detector_id, "mar180") == 0 ||
      strcmp (detector_id, "mar300") == 0)
  {
    gain = 1.08;

    overload = 120000;

    dimension [0] = img_rows (img);
    dimension [1] = img_columns (img);

    precedence [0] = 1;
    precedence [1] = 2;

    direction [0] = "decreasing";
    direction [1] = "increasing";
  }
  else

    if (strcmp (detector_id, "mar345") == 0)
    {
      gain = 1.55;

      overload = 240000;

      dimension [0] = img_columns (img);
      dimension [1] = img_rows (img);

      precedence [0] = 2;
      precedence [1] = 1;

      direction [0] = "increasing";
      direction [1] = "increasing";
    }
    else

      if (strncmp (detector_id, "adscquantum", 11) == 0)
      {
        gain = 0.20;

        overload = 65000;

        dimension [0] = img_columns (img);
        dimension [1] = img_rows (img);

        precedence [0] = 2;
        precedence [1] = 1;

        direction [0] = "increasing";
        direction [1] = "increasing";
      }
      else
      {
        gain = 0.0;

        overload = 0;

        dimension [0] = img_rows (img);
        dimension [1] = img_columns (img);

        precedence [0] = 1;
        precedence [1] = 2;

        direction [0] = NULL;
        direction [1] = NULL;
      }


    /* Make a cbf version of the image */

  a = clock ();
                                                

    /* Create the cbf */

  cbf_failnez (cbf_make_handle (&cbf))


    /* Make a new data block */

  cbf_failnez (cbf_new_datablock (cbf, "image_1"))


    /* Make the _diffrn category */

  cbf_failnez (cbf_new_category (cbf, "diffrn"))
  cbf_failnez (cbf_new_column   (cbf, "id"))
  cbf_failnez (cbf_set_value    (cbf, "DS1"))


    /* Make the _diffrn_source category */

  cbf_failnez (cbf_new_category (cbf, "diffrn_source"))
  cbf_failnez (cbf_new_column   (cbf, "diffrn_id"))
  cbf_failnez (cbf_set_value    (cbf, "DS1"))
  cbf_failnez (cbf_new_column   (cbf, "source"))
  cbf_failnez (cbf_set_value    (cbf, "synchrotron"))
  cbf_failnez (cbf_new_column   (cbf, "type"))
  cbf_failnez (cbf_set_value    (cbf, "ssrl crystallography"))


    /* Make the _diffrn_radiation category */  

  cbf_failnez (cbf_new_category (cbf, "diffrn_radiation"))
  cbf_failnez (cbf_new_column   (cbf, "diffrn_id"))
  cbf_failnez (cbf_set_value    (cbf, "DS1"))
  cbf_failnez (cbf_new_column   (cbf, "wavelength_id"))
  cbf_failnez (cbf_set_value    (cbf, "L1"))


    /* Make the _diffrn_radiation_wavelength category */

  cbf_failnez (cbf_new_category    (cbf, "diffrn_radiation_wavelength"))
  cbf_failnez (cbf_new_column      (cbf, "id"))
  cbf_failnez (cbf_set_value       (cbf, "L1"))
  cbf_failnez (cbf_new_column      (cbf, "wavelength"))

  if (wavelength)
  
    cbf_failnez (cbf_set_doublevalue (cbf, "%.4f", wavelength))

  cbf_failnez (cbf_new_column      (cbf, "wt"))
  cbf_failnez (cbf_set_value       (cbf, "1.0"))


    /* Make the _diffrn_measurement category */  

  cbf_failnez (cbf_new_category (cbf, "diffrn_measurement"))
  cbf_failnez (cbf_new_column   (cbf, "diffrn_id"))
  cbf_failnez (cbf_set_value    (cbf, "DS1"))
  cbf_failnez (cbf_new_column   (cbf, "method"))
  cbf_failnez (cbf_set_value    (cbf, "oscillation"))
  cbf_failnez (cbf_new_column   (cbf, "sample_detector_distance"))

  if (distance)

    cbf_failnez (cbf_set_doublevalue (cbf, "%.4f", distance))


    /* Make the _diffrn_detector category */  

  cbf_failnez (cbf_new_category (cbf, "diffrn_detector"))
  cbf_failnez (cbf_new_column   (cbf, "id"))
  cbf_failnez (cbf_set_value    (cbf, detector_id))
  cbf_failnez (cbf_new_column   (cbf, "diffrn_id"))
  cbf_failnez (cbf_set_value    (cbf, "DS1"))
  cbf_failnez (cbf_new_column   (cbf, "type"))
  cbf_failnez (cbf_set_value    (cbf, detector))


    /* Make the _diffrn_detector_element category */  

  cbf_failnez (cbf_new_category     (cbf, "diffrn_detector_element"))
  cbf_failnez (cbf_new_column       (cbf, "id"))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_new_column       (cbf, "detector_id"))
  cbf_failnez (cbf_set_value        (cbf, detector_id))


    /* Make the _diffrn_frame_data category */  

  cbf_failnez (cbf_new_category     (cbf, "diffrn_frame_data"))
  cbf_failnez (cbf_new_column       (cbf, "id"))
  cbf_failnez (cbf_set_value        (cbf, "frame_1"))
  cbf_failnez (cbf_new_column       (cbf, "detector_element_id"))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_new_column       (cbf, "detector_id"))
  cbf_failnez (cbf_set_value        (cbf, detector_id))
  cbf_failnez (cbf_new_column       (cbf, "array_id"))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_column       (cbf, "binary_id"))
  cbf_failnez (cbf_set_integervalue (cbf, 1))


    /* Make the _array_structure_list category */  

  cbf_failnez (cbf_new_category     (cbf, "array_structure_list"))
  cbf_failnez (cbf_new_column       (cbf, "array_id"))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_row          (cbf))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_column       (cbf, "index"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_next_row         (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, 2))
  cbf_failnez (cbf_new_column       (cbf, "dimension"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, dimension [0]))
  cbf_failnez (cbf_next_row         (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, dimension [1]))
  cbf_failnez (cbf_new_column       (cbf, "precedence"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, precedence [0]))
  cbf_failnez (cbf_next_row         (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, precedence [1]))
  cbf_failnez (cbf_new_column       (cbf, "direction"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_set_value        (cbf, direction [0]))
  cbf_failnez (cbf_next_row         (cbf))
  cbf_failnez (cbf_set_value        (cbf, direction [1]))


    /* Make the _array_element_size category */

  cbf_failnez (cbf_new_category     (cbf, "array_element_size"))
  cbf_failnez (cbf_new_column       (cbf, "array_id"))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_row          (cbf))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_column       (cbf, "index"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_next_row         (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, 2))
  cbf_failnez (cbf_new_column       (cbf, "size"))

  if (pixel_size > 0)
  {
    cbf_failnez (cbf_rewind_row       (cbf))
    cbf_failnez (cbf_set_doublevalue  (cbf, "%.1fe-6", pixel_size * 1e6))
    cbf_failnez (cbf_next_row         (cbf))
    cbf_failnez (cbf_set_doublevalue  (cbf, "%.1fe-6", pixel_size * 1e6))
  }


    /* Make the _array_intensities category */

  cbf_failnez (cbf_new_category     (cbf, "array_intensities"))
  cbf_failnez (cbf_new_column       (cbf, "array_id"))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_column       (cbf, "binary_id"))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_new_column       (cbf, "linearity"))
  cbf_failnez (cbf_set_value        (cbf, "linear"))
  cbf_failnez (cbf_new_column       (cbf, "gain"))

  if (gain)
  
    cbf_failnez (cbf_set_doublevalue  (cbf, "%.3g", gain))
    
  cbf_failnez (cbf_new_column       (cbf, "overload"))

  if (overload)
    
    cbf_failnez (cbf_set_integervalue (cbf, overload))
    
  cbf_failnez (cbf_new_column       (cbf, "undefined"))
  cbf_failnez (cbf_set_integervalue (cbf, 0))


    /* Make the _array_data category */

  cbf_failnez (cbf_new_category     (cbf, "array_data"))
  cbf_failnez (cbf_new_column       (cbf, "array_id"))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_column       (cbf, "binary_id"))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_new_column       (cbf, "data"))


    /* Save the binary data */

  cbf_failnez (cbf_set_integerarray_wdims_fs (cbf, CBF_PACKED|CBF_FLAT_IMAGE, 1,
                                 img_pixelptr (img, 0, 0), sizeof (int), 1,
                                 img_rows (img) * img_columns (img),
                                 "little_endian",img_rows (img),img_columns (img),0,0 ))
  

    /* Write the new file */

  out = fopen (argv [2], "w+b");

  if (!out)
  {
    fprintf (stderr, " Couldn't open the CBF file %s\n", argv [2]);

    exit (1);
  }

  cbf_failnez (cbf_write_file (cbf, out, 1, CBF, MSG_DIGEST | MIME_HEADERS  , 0))
  

    /* Free the cbf */

  cbf_failnez (cbf_free_handle (cbf))

  b = clock ();

  fprintf (stderr, " Time to write the CBF image: %.3fs\n", 
                                      ((b - a) * 1.0) / CLOCKS_PER_SEC); 


    /* Read the CBF file and compare the image to the original */
    
  a = clock ();


    /* Create the cbf */

  cbf_failnez (cbf_make_handle (&cbf))


    /* Read the file */

  in = fopen (argv [2], "rb");

  if (!in)
  {
    fprintf (stderr, " Couldn't reopen the CBF file %s\n", argv [2]);

    exit (1);
  }

  cbf_failnez (cbf_read_file (cbf, in, MSG_DIGEST))


    /* Get the image identifier */

  cbf_failnez (cbf_rewind_datablock (cbf))
  cbf_failnez (cbf_find_category    (cbf, "diffrn_frame_data"))
  cbf_failnez (cbf_find_column      (cbf, "array_id"))
  cbf_failnez (cbf_get_value        (cbf, &array_id))
  

    /* Get the image dimensions (second dimension = fast, first = slow) */

  cbf_failnez (cbf_find_category    (cbf, "array_structure_list"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_find_column      (cbf, "array_id"))

  dimension [0] = dimension [1] = 0;

  while (cbf_find_nextrow (cbf, array_id) == 0)
  {
    cbf_failnez (cbf_find_column      (cbf, "precedence"))
    cbf_failnez (cbf_get_integervalue (cbf, &index))

    if (index >= 1 && index <= 2)
    {
      cbf_failnez (cbf_find_column (cbf, "dimension"))

      cbf_failnez (cbf_get_integervalue (cbf, &dimension [2 - index]))
    }
    else

      exit (1);

    cbf_failnez (cbf_find_column (cbf, "array_id"))
  }

  if (dimension [0] == 0 || dimension [1] == 0)

    exit (1);


    /* Create the new image */

  cbf_img = img_make_handle ();

  img_set_dimensions (cbf_img, dimension [0], dimension [1]);


    /* Find the binary data */
  
  cbf_failnez (cbf_find_category (cbf, "array_data"))
  cbf_failnez (cbf_find_column   (cbf, "array_id"))
  cbf_failnez (cbf_find_row      (cbf, array_id))
  cbf_failnez (cbf_find_column   (cbf, "data"))


    /* Read the binary data */
  
  cbf_failnez (cbf_get_integerarray (cbf,
                                 &id, img_pixelptr (cbf_img, 0, 0), sizeof (int), 1,
                                 img_rows (cbf_img) * img_columns (cbf_img), &nelem_read))


    /* Free the cbf */

  cbf_failnez (cbf_free_handle (cbf))

  b = clock ();

  fprintf (stderr, " Time to read the CBF image: %.3fs\n", 
                                     ((b - a) * 1.0) / CLOCKS_PER_SEC);


    /* Compare the images */

  if (img_rows (img) != img_rows (cbf_img) || img_columns (img) != img_columns (cbf_img))
  {
    fprintf (stderr, " The dimensions of the CBF image don't match the original\n");

    exit (1);
  }

  for (column = 0; column < (unsigned int) img_columns (cbf_img); column++)

    for (row = 0; row < (unsigned int) img_rows (cbf_img); row++)

      if (img_pixel (cbf_img, column, row) != img_pixel (img, column, row))
      {
        fprintf (stderr, " The CBF image differs from the original at (%d, %d)\n", column, row);

        exit (1);
      }

  fprintf (stderr, " The CBF image matches the original\n");


    /* Free the images */

  img_free_handle (img);

  img_free_handle (cbf_img);


    /* Success */

  return 0;
}