示例#1
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;
}
示例#2
0
int cbf_undo_links (cbf_node **node)
{
  cbf_node *snode;
  
  cbf_node *pnode;
  
  snode = *node;
  
  pnode = NULL;
  
  while (*node) {
  
    if ((*node)->type == CBF_LINK) {
    
      pnode = *node;
      
      if ((*node)->children) {
      
        cbf_failnez(cbf_set_children(*node,0))
                    	
      }
      
      *node = (*node)->link;
          	
    } else break;
示例#3
0
 int cbf_open_temporary (cbf_context *context, cbf_file **temporary)
 {
     FILE *stream;
     
     int errorcode;
     
     
     /* Check the arguments */
     
     if (!context || !temporary)
         
         return CBF_ARGUMENT;
     
     
     /* Does a temporary file already exist? */
     
     if (context->temporary)
     {
         cbf_failnez (cbf_add_fileconnection (&context->temporary, NULL))
         
         *temporary = context->temporary;
         
         return 0;
     }
     
     
     /* Create the temporary file */
     
     stream = tmpfile ();
     
     if (!stream)
         
         return CBF_FILEOPEN;
     
     errorcode = cbf_make_file (&context->temporary, stream);
     
     
     context->temporary->temporary = 1;
     
     
     if (errorcode)
     {
         if (fclose (stream))
             
             errorcode |= CBF_FILECLOSE;
         
         return errorcode;
     }
     
     
     /* Open a connection */
     
     return cbf_open_temporary (context, temporary);
 }
示例#4
0
 int cbf_close_temporary (cbf_context *context, cbf_file **temporary)
 {
     /* Check the arguments */
     
     if (!context || !temporary)
         
         return CBF_ARGUMENT;
     
     if (!*temporary)
         
         return CBF_ARGUMENT;
     
     
     /* Check that the temporary file matches */
     
     if (context->temporary != *temporary)
         
         return CBF_NOTFOUND;
     
     
     /* Delete the connection */
     
     cbf_failnez (cbf_delete_fileconnection (&context->temporary))
     
     *temporary = NULL;
     
     
     /* Is there only one connection left? */
     
     if (context->temporary)
         
         if (cbf_file_connections (context->temporary) == 1)
             
             cbf_failnez (cbf_free_file (&context->temporary))
             
             
         /* Success */
             
             return 0;
 }
示例#5
0
int cbf_free_text(const char **old_block, size_t *old_nelem)
{

  void * vold_block;

  vold_block = (void *)*old_block;

  cbf_failnez(cbf_free(&vold_block, old_nelem))

  *old_block = NULL;

  return 0;

}
示例#6
0
 int cbf_make_context (cbf_context **context)
 {
     /* Allocate the memory */
     
     cbf_failnez (cbf_alloc ((void **) context, NULL, sizeof (cbf_context), 1))
     
     
     /* Initialise */
     
     (*context)->temporary = NULL;
     
     (*context)->connections = 1;
     
     
     /* Success */
     
     return 0;
 }
示例#7
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");
}
示例#8
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;
}
示例#9
0
int cbf_decompress_parameters (int          *eltype,
                               size_t       *elsize,
                               int          *elsigned,
                               int          *elunsigned,
                               size_t       *nelem,
                               int          *minelem,
                               int          *maxelem,
                               unsigned int  compression,
                               cbf_file     *file)
{
  unsigned int nelem_file;

  int errorcode, minelement_file, maxelement_file,
                   elsigned_file, elunsigned_file;


    /* Discard any bits in the buffers */

  file->bits [0] = 0;
  file->bits [1] = 0;

   /* Check compression type */

  if (compression != CBF_CANONICAL   &&
      (compression&CBF_COMPRESSION_MASK) != CBF_PACKED      &&
      (compression&CBF_COMPRESSION_MASK) != CBF_PACKED_V2   &&
      compression != CBF_BYTE_OFFSET &&
      compression != CBF_PREDICTOR   &&
      compression != CBF_NONE)

    return CBF_FORMAT;

  if (compression == CBF_NONE || compression == CBF_BYTE_OFFSET )
  {
    nelem_file = 0;

    minelement_file = maxelement_file = 0;
  }
  else
  {
      /* Read the number of elements (64 bits) */

    cbf_failnez (cbf_get_integer (file, (int *) &nelem_file, 0, 64))


      /* Read the minimum element (64 bits) */

    errorcode = cbf_get_integer (file, &minelement_file, 1, 64);

    if (errorcode && errorcode != CBF_OVERFLOW)

      return errorcode;


      /* Read the maximum element (64 bits) */

    errorcode = cbf_get_integer (file, &maxelement_file, 1, 64);

    if (errorcode && errorcode != CBF_OVERFLOW)

      return errorcode;
  }


    /* Update the element sign, type, minimum, maximum and number */

  elsigned_file = !(((unsigned) minelement_file) <=
                    ((unsigned) maxelement_file) &&
                    ((signed)   minelement_file) >
                    ((signed)   maxelement_file));

  elunsigned_file = !(((signed)   minelement_file) <=
                      ((signed)   maxelement_file) &&
                      ((unsigned) minelement_file) >
                      ((unsigned) maxelement_file));

  if (elsigned)

    *elsigned = elsigned_file;

  if (elunsigned)

    *elunsigned = elunsigned_file;

  if (eltype)

    *eltype = CBF_INTEGER;

  if (elsize) {

      /* Calculate the minimum number of bytes needed to hold the elements */

    if (minelement_file == 0 && maxelement_file == 0) {

      *elsize = 0;

    } else {

      if ((!elsigned_file ||
          ((signed) minelement_file == (signed short) minelement_file &&
           (signed) maxelement_file == (signed short) maxelement_file)) ||
          (!elunsigned_file ||
          ((unsigned) minelement_file == (unsigned short) minelement_file &&
           (unsigned) maxelement_file == (unsigned short) maxelement_file))) {

        if ((!elsigned_file ||
            ((signed) minelement_file == (signed char) minelement_file &&
             (signed) maxelement_file == (signed char) maxelement_file)) ||
             (!elunsigned_file ||
            ((unsigned) minelement_file == (unsigned char) minelement_file &&
             (unsigned) maxelement_file == (unsigned char) maxelement_file))) {

          *elsize = sizeof (char);

        } else {

          *elsize = sizeof (short);

        }

      } else {

        *elsize = sizeof (int);

      }
    }
  }

  if (minelem)

    *minelem = minelement_file;

  if (maxelem)

    *maxelem = maxelement_file;

  if (nelem)

    *nelem = nelem_file;


    /* Success */

  return 0;
}
示例#10
0
int cbf_compress (void         *source,
                  size_t        elsize,
                  int           elsign,
                  size_t        nelem,
                  unsigned int  compression,
                  cbf_file     *file,
                  size_t       *compressedsize,
                  int          *bits,
                  char         *digest,
                  int           realarray,
                  const char   *byteorder,
                  size_t        dimfast,
                  size_t        dimmid,
                  size_t        dimslow,
                  size_t        padding)
{
  int errorcode;

  size_t size;


    /* Discard any bits in the buffers */

  cbf_failnez (cbf_reset_bits (file))

  if (compressedsize)

    *compressedsize = 0;


    /* Start a digest? */

  if (digest)

    cbf_failnez (cbf_start_digest (file))


  errorcode = 0;

  size = 0;

  switch (compression&CBF_COMPRESSION_MASK)
  {
    case CBF_CANONICAL:

      errorcode = cbf_compress_canonical (source, elsize, elsign, nelem,
                                          compression, file,
                                          &size, bits, realarray,
                                          byteorder, dimfast, dimmid, dimslow, padding);
      break;

    case CBF_PACKED:
    case CBF_PACKED_V2:
    case 0:

      errorcode = cbf_compress_packed (source, elsize, elsign, nelem,
                                       compression, file,
                                       &size, bits, realarray,
                                       byteorder, dimfast, dimmid, dimslow, padding);
      break;

    case CBF_BYTE_OFFSET:

      errorcode = cbf_compress_byte_offset (source, elsize, elsign, nelem,
                                            compression, file,
                                            &size, bits, realarray,
                                            byteorder, dimfast, dimmid, dimslow, padding);
      break;

    case CBF_PREDICTOR:

      errorcode = cbf_compress_predictor (source, elsize, elsign, nelem,
                                          compression, file,
                                          &size, bits, realarray,
                                          byteorder, dimfast, dimmid, dimslow, padding);
      break;

    case CBF_NONE:

      errorcode = cbf_compress_none (source, elsize, elsign, nelem,
                                     compression, file,
                                     &size, bits, realarray,
                                     byteorder, dimfast, dimmid, dimslow, padding);
      break;

  default:

      errorcode = CBF_ARGUMENT;
  }


    /* Add the compressed size */

  if (compressedsize)

    *compressedsize += size;


    /* Flush the buffers */

  errorcode |= cbf_flush_bits (file);


    /* Get the digest? */

  if (digest)

     errorcode |= cbf_end_digest (file, digest);


    /* Done */

  return errorcode;
}
示例#11
0
int main (int argc, char *argv [])
{
	int error = CBF_SUCCESS;
    FILE *file = NULL;
    clock_t a,b;
    cbf_handle cif;
    cbf_getopt_handle opts;
    int c = 0;
    int errflg = 0;
	size_t cifid = 0;
	size_t f = 0;
	int h5_write_flags = 0;
	cbf_h5handle h5out = NULL;
	const char ** const cifin = memset(malloc(argc*sizeof(char*)),0,argc*sizeof(char*));
	const char *hdf5out = NULL;
	const char *group = NULL;
    char *ciftmp=NULL;
#ifndef NOMKSTEMP
    int ciftmpfd;
#endif
    int ciftmpused = 0;
    int nbytes;
    char buf[C2CBUFSIZ];

    int digest = 0;

    const char * optarg = NULL;

	cbf_onfailnez(cbf_make_getopt_handle(&opts),free(cifin));

	cbf_onfailnez(cbf_getopt_parse(opts, argc, argv, "c(compression):g(group):o(output):Z(register):" ),free(cifin));

	if (!cbf_rewind_getopt_option(opts)) {
        for(; !cbf_get_getopt_data(opts,&c,NULL,NULL,&optarg); cbf_next_getopt_option(opts)) {
            switch (c) {
				case 'c': { /* compression */
					if (!cbf_cistrcmp("zlib",optarg?optarg:"")) {
						h5_write_flags |= CBF_H5COMPRESSION_ZLIB;
						h5_write_flags &= ~CBF_H5COMPRESSION_CBF;
					} else if (!cbf_cistrcmp("cbf",optarg?optarg:"")) {
						h5_write_flags &= ~CBF_H5COMPRESSION_ZLIB;
						h5_write_flags |= CBF_H5COMPRESSION_CBF;
					}else if (!cbf_cistrcmp("none",optarg?optarg:"")) {
						/* remove any previously set (system default?) compressions */
						h5_write_flags &= ~CBF_H5COMPRESSION_ZLIB;
						h5_write_flags &= ~CBF_H5COMPRESSION_CBF;
					}
					else ++errflg;
					break;
				}
				case 'g': { /* group within output file where data should be stored */
					if (group) errflg++;
					else group = optarg;
					break;
				}
				case 'o': { /* output file */
					if (hdf5out) errflg++;
                    else hdf5out = optarg;
                    break;
				}
				case 'Z': { /* automatic or manual filter registration? */
					if (cbf_cistrcmp(optarg?optarg:"","manual") == 0) {
						h5_write_flags |= CBF_H5_REGISTER_COMPRESSIONS;
					} else if (cbf_cistrcmp(optarg?optarg:"","plugin") == 0) {
						h5_write_flags &= ~CBF_H5_REGISTER_COMPRESSIONS;
					} else {
						errflg++;
					}
					break;
				}
				case 0: { /* input file */
					if (NULL != optarg) cifin[cifid++] = optarg;
					break;
				}
				default: {
                    errflg++;
                    break;
				}
            }
        }
	}
	if (!hdf5out) {
		fprintf(stderr,"No output file given.\n");
		++errflg;
	}
	if (!cifid) {
		fprintf(stderr,"No input files given.\n");
		++errflg;
	}
	if (errflg) {
		fprintf(stderr, "Usage:\n\t%s -o|--output output_nexus input_minicbf_files...\n"
				"Options:\n"
						"\t-c|--compression cbf|none|zlib (default: none)\n"
						"\t-g|--group output_group (default: 'entry')\n"
						"\t-Z|--register manual|plugin (default: plugin)\n"
						"These options are NOT case-sensitive.\n",
				argv[0]);
        exit(2);
    }

	// prepare the output file
	if(cbf_create_h5handle2(&h5out,hdf5out)) printf ("Couldn't open the HDF5 file '%s'.\n", hdf5out);
	cbf_h5handle_require_entry(h5out,0,group);
	h5out->flags = h5_write_flags;

#ifdef CBF_USE_ULP
	// set up some parameters for comparing floating point numbers
	h5out->cmp_double_as_float = 0;
	h5out->float_ulp = 4;
#ifndef NO_UINT64_TYPE
	h5out->double_ulp = 4;
#endif
#endif

	for (f = 0; CBF_SUCCESS == error && f != cifid; ++f) {

		/* Read the minicbf */

		if (!(cifin[f]) || strcmp(cifin[f]?cifin[f]:"","-") == 0) {
			ciftmp = (char *)malloc(strlen("/tmp/cif2cbfXXXXXX")+1);
	#ifdef NOTMPDIR
			strcpy(ciftmp, "cif2cbfXXXXXX");
	#else
			strcpy(ciftmp, "/tmp/cif2cbfXXXXXX");
	#endif
	#ifdef NOMKSTEMP
			if ((ciftmp = mktemp(ciftmp)) == NULL ) {
				fprintf(stderr,"%s: Can't create temporary file name %s.\n%s\n", argv[0], ciftmp,strerror(errno));
				exit(1);
			}
			if ( (file = fopen(ciftmp,"wb+")) == NULL) {
				fprintf(stderr,"Can't open temporary file %s.\n%s\n", ciftmp,strerror(errno));
				exit(1);
			}
	#else
			if ((ciftmpfd = mkstemp(ciftmp)) == -1 ) {
				fprintf(stderr,"%s: Can't create temporary file %s.\n%s\n", argv[0], ciftmp,strerror(errno));
				exit(1);
			}
			if ( (file = fdopen(ciftmpfd, "w+")) == NULL) {
				fprintf(stderr,"Can't open temporary file %s.\n%s\n", ciftmp,strerror(errno));
				exit(1);
			}
	#endif
			while ((nbytes = fread(buf, 1, C2CBUFSIZ, stdin))) {
				if(nbytes != fwrite(buf, 1, nbytes, file)) {
					fprintf(stderr,"Failed to write %s.\n", ciftmp);
					exit(1);
				}
			}
			fclose(file);
			cifin[f] = ciftmp;
			ciftmpused = 1;
		}


		// start timing
		a = clock ();
		{ // do the work
			// prepare the file
			FILE * const in = fopen (cifin[f], "rb");
			if (NULL == in) {
				fprintf (stderr,"Couldn't open the input CIF file '%s': %s\n", cifin[f], strerror(errno));
				exit (1);
			}
			// ensure temporary file is removed when the program closes
			if (ciftmpused) {
				if (unlink(ciftmp)) {
					fprintf(stderr,"Can't unlink temporary file '%s': %s\n", ciftmp,strerror(errno));
					exit(1);
				}
			}
			// make the handle
			if ( cbf_make_handle (&cif) ) {
				fprintf(stderr,"Failed to create handle for input_cif\n");
				exit(1);
			}
			// read the file
			cbf_onfailnez(cbf_read_file(cif, in, MSG_DIGEST|(digest&MSG_DIGESTWARN)),free(cifin));
		}
		// stop timing
		b = clock ();
		printf("Time to read '%s': %.3fs\n", cifin[f], ((float)(b - a))/CLOCKS_PER_SEC);

		// start timing
		a = clock ();
		{ // do the work
			// convert to nexus format
			error |= cbf_write_cbf_h5file(cif, h5out);
		}

		cbf_free_handle(cif);

		// stop timing
		b = clock ();
		printf("Time to convert the data: %.3fs\n", ((float)(b - a))/CLOCKS_PER_SEC);

	}

	{ // write the file
		// start timing
		a = clock ();
		// clean up cbf handles
		cbf_free_h5handle(h5out);
		// stop timing
		b = clock ();
		printf("Time to write '%s': %.3fs\n", hdf5out, ((float)(b - a))/CLOCKS_PER_SEC);
	}


	// cleanup
	free(cifin);
	cbf_failnez(cbf_free_getopt_handle(opts));
	return CBF_SUCCESS==error ? 0 : 1;
}
示例#12
0
int cbf_make_new_node (cbf_node **node, CBF_NODETYPE type,
                       cbf_context *context, const char *name)
{
  int errorcode;

  if (!node)

    return CBF_ARGUMENT;


    /* Create the new node */

  cbf_failnez (cbf_alloc ((void **) node, NULL, sizeof (cbf_node), 1))


    /* Initialise the node */

  (*node)->type = type;

  (*node)->name = NULL;

  (*node)->link = NULL;

  (*node)->parent = NULL;

  (*node)->children = 0;

  (*node)->child_size = 0;

  (*node)->child = NULL;


    /* Add the context? */

  if (type == CBF_LINK)

    (*node)->context = NULL;

  else
  {
      /* Does the context exist? */

    if (context)

      (*node)->context = context;

    else

      (*node)->context = NULL;


      /* Add a context connection */

    cbf_onfailnez (cbf_add_contextconnection (&(*node)->context),
                   cbf_free ((void **) node, NULL))


      /* Name the node */

    errorcode = cbf_name_new_node (*node, name);

    if (errorcode)
    {
      errorcode |= cbf_free_context (&(*node)->context);

      return errorcode | cbf_free_node (*node);
    }
  }


    /* Success */

  return 0;
}
示例#13
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;
}