Esempio n. 1
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;
}
Esempio n. 2
0
int main (int argc, char *argv [])
{
	int error = CBF_SUCCESS;
    cbf_getopt_handle opts = NULL;
	int h5_write_flags = 0;
	int list = 0;
    int noCBFnames = 0;
	unsigned int frame = 0;
	const char * cifout = NULL;
	const char * hdf5in = NULL;
	const char * group = NULL;

	/* Attempt to read the arguments */
	if (CBF_SUCCESS != (error |= cbf_make_getopt_handle(&opts))) {
		fprintf(stderr,"Could not create a 'cbf_getopt' handle.\n");
	} else if (CBF_SUCCESS != (error |= cbf_getopt_parse(opts, argc, argv, "c(compression):g(group):o(output):Z(register):f(frame):\1(list)\2(no-list)\3(CBFnames)\4(noCBFnames)" ))) {
		fprintf(stderr,"Could not parse arguments.\n");
	} else {
    	int errflg = 0;
		/* successful so far, try to make sense of the options */
		if (!cbf_rewind_getopt_option(opts)) {
			int c = 0;
			const char * optarg = NULL;
			for(; !cbf_get_getopt_data(opts,&c,NULL,NULL,&optarg); cbf_next_getopt_option(opts)) {
				switch (c) {
					case 'f': { /* the index of the frame to be converted */
						if (!optarg) {
							++errflg;
						} else {
							char * end = NULL;
							const unsigned int _frame = strtoul(optarg,&end,0);
							if (end==optarg) {
								fprintf(stderr,"error: Expected a number for the frame index, got '%s'.\n",optarg);
								++errflg;
							} else {
								frame = _frame;
							}
						}
                        break;
					}
					case 'g': { /* group within input file where data should be found */
						if (group) ++errflg;
						else group = optarg;
						break;
					}
					case 'o': { /* output file */
						if (cifout) ++errflg;
						else cifout = 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 (hdf5in) ++errflg;
						else hdf5in = optarg;
						break;
					}
					case '\1': {
						if (list) ++errflg;
						list = 1;
						break;
					}
					case '\2': {
						if (list) ++errflg;
						list = -1;
						break;
					}
					case '\3': {
						if (noCBFnames) ++errflg;
						noCBFnames = -1;
						break;
					}
					case '\4': {
						if (noCBFnames) ++errflg;
						noCBFnames = 1;
						break;
					}
					default: {
						++errflg;
						break;
					}
				}
			}
		}

		/* check for missing data */
		if (!cifout) {
			fprintf(stderr,"No output files given.\n");
			++errflg;
		}
		if (!hdf5in) {
			fprintf(stderr,"No input file given.\n");
			++errflg;
		}
		if (errflg) {
			fprintf(stderr, "Usage:\n\t%s [options] -o|--output output_cbf input_nexus\n"
					"Options:\n"
							"\t-g|--group output_group (default: 'entry')\n"
							"\t-Z|--register manual|plugin (default: plugin)\n"
							"\t-f|--frame <unsigned int> (default: 0)\n",
					argv[0]);
			error |= CBF_ARGUMENT;
		}
	}

	/* If I could read the arguments then attempt to use them */
	if (CBF_SUCCESS == error) {
		cbf_h5handle h5in = NULL;
		cbf_handle cif = NULL;

		/* prepare the input file */
		{
			hid_t hdf_file = CBF_H5FAIL, fapl = CBF_H5FAIL;
			if ((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) {
				fprintf(stderr,"Couldn't create file access property list.\n");
				error |= CBF_H5ERROR;
			} else if ((H5Pset_fclose_degree(fapl,H5F_CLOSE_STRONG)) < 0) {
				fprintf(stderr,"Couldn't set a file access property.\n");
				error |= CBF_H5ERROR;
			} else if ((hdf_file = H5Fopen(hdf5in,H5F_ACC_RDONLY,fapl)) < 0) {
				fprintf(stderr,"Couldn't open a HDF5 file.\n");
				error |= CBF_H5ERROR;
			} else if(CBF_SUCCESS != (error |= cbf_create_h5handle3(&h5in,hdf_file))) {
				fprintf(stderr,"Couldn't open the HDF5 file '%s'.\n", hdf5in);
			} else if (CBF_SUCCESS != (error |= cbf_h5handle_require_entry(h5in,0,group))) {
				fprintf(stderr,"Couldn't create an NXentry group in the HDF5 file '%s'.\n", hdf5in);
			} else {
				h5in->flags = h5_write_flags;
				h5in->slice = frame;
#ifdef CBF_USE_ULP
				// set up some parameters for comparing floating point numbers
				h5in->cmp_double_as_float = 0;
				h5in->float_ulp = 4;
#ifndef NO_UINT64_TYPE
				h5in->double_ulp = 4;
#endif
#endif
			}
			H5Pclose(fapl);
		}

		/* make the handle for the output file */
		if (CBF_SUCCESS != (error |= cbf_make_handle(&cif))) {
			fprintf(stderr,"Failed to create handle for input_cif\n");
			error |= CBF_ALLOC;
		}

		/* Time the conversion */
		if (CBF_SUCCESS == error) {
			clock_t a = clock(), b;
			h5in->slice = 0;
            if (list > 0) h5in->logfile = stdout;
            if (noCBFnames >= 0) h5in->flags |= CBF_H5_CBFNONAMES;
			error |= cbf_write_nx2cbf(h5in, cif);
			b = clock();
			printf("Time to convert '%s': %.3fs\n", cifout, ((float)(b - a))/CLOCKS_PER_SEC);
		} else {
			fprintf(stderr,"An error occured, will not try to convert the file '%s'\n",hdf5in);
		}

		/* Write the file */
		if (CBF_SUCCESS == error) {
			/* start timing */
			clock_t a = clock(), b;
			FILE * const out = fopen(cifout,"wb");
			if (NULL == out) {
				fprintf(stderr,"Couldn't open the output CIF file '%s': %s\n", cifout, strerror(errno));
				error |= CBF_FILEOPEN;
			} else if (CBF_SUCCESS != (error |= cbf_write_file(cif,out,0,CBF,0,0))) {
				fprintf(stderr,"Couldn't write the output CIF file '%s': %s\n", cifout, cbf_strerror(error));
				error |= CBF_FILEWRITE;
			}
			if (NULL!=out) fclose(out);
			/* stop timing */
			b = clock();
			printf("Time to write '%s': %.3fs\n", cifout, ((float)(b - a))/CLOCKS_PER_SEC);
		} else {
			fprintf(stderr,"An error occured, will not try to write the file '%s'\n",cifout);
		}

		/* Clean up */
		if (cif) cbf_free_handle(cif);
		if (h5in) cbf_free_h5handle(h5in);

	}

	/*******************************************************************************************************************/

	/* Cleanup */
	error |= cbf_free_getopt_handle(opts);
	return CBF_SUCCESS==error ? 0 : 1;
}