Example #1
0
int	main(int argc, char *argv[])
{
	FILE		*fp;
	char		in_filename[1024], out_filename[1024];
	char		field[128];
	char		header_bytes[6];
	int		data_size, header_size_char;
	int		cbf_status;
	int		i, j, k;
	int		size1, size2;
	int		output_packing;
	int		status_pclose;
	char		*header;
	unsigned short	*data;
	 char *    lbo; /* local byte order */   
	static char	*endings[] = {
					".img",
					".img.gz",
					".img.bz2",
					NULL
				     };
	static char	*flags[] = {
					"--gz",
					"--bz2",
					NULL
				   };

	int		cbf2adscimg_sub(char *filename, char **header, unsigned short **data);

	if(argc < 2)
	{
		usage();
		exit(0);
	}

	output_packing = -1;

	while(argc > 1 && argv[1][0] == '-' && argv[1][1] == '-')
	{
		for(j = 0; flags[j] != NULL; j++)
			if(NULL != strstr(argv[1], flags[j]))
				break;
		if(NULL == flags[j])
		{
			fprintf(stderr,"cbf2adscimg: %s is an unknown flag\n\n", argv[1]);
			usage();
			exit(0);
		}
		output_packing = j;
		argc--;
		argv++;
	}
	output_packing++;

	while(argc > 1)
	{
		strcpy(in_filename, argv[1]);
		i = strlen(in_filename);
		k = strlen(".cbf");
		if(0 == endswith(in_filename, ".cbf"))
		{
			fprintf(stderr,"cbf2adscimg: Input file name %s does not end in .cbf\n", in_filename);
			exit(0);
		}
		strcpy(out_filename, in_filename);
		out_filename[i - k] = '\0';
		strcat(out_filename, endings[output_packing]);

		cbf_status = cbf2adscimg_sub(in_filename, &header, &data);

		if(0 != cbf_status)
		{
			fprintf(stderr, "cbf2adscimg: Error converting cbf file %s to .img format\n", in_filename);
			exit(0);
		}
		if(NULL == header)
		{
			fprintf(stderr, "cbf2adscimg: Error: cbf2adscimg_sub returned NULL for header on file %s\n", in_filename);
			exit(0);
		}

		if(0 == output_packing)
		{
			if(NULL == (fp = fopen(out_filename, "wb")))
			{
				fprintf(stderr, "cbf2adscimg: Cannot create %s as output .img file\n", out_filename);
				exit(0);
			}
		}
		else
		{
			if(0 == output_packing)
				sprintf(popen_command, "gzip > %s", out_filename);
			else
				sprintf(popen_command, "bzip2 > %s", out_filename);
			if(NULL == (fp = popen(popen_command, "w")))
			{
				fprintf(stderr, "cbf2adscimg: Cannot exec %s command to compress output image file.\n", popen_command);
				exit(0);
			}
		}

		/*
		 *	Output the header block(s).
		 */

		for(i = 0; i < 5; i++)
			header_bytes[0 + i] = header[15 + i];

		header_bytes[5] = '\0';
		header_size_char = atoi(header_bytes);
		

		if(header_size_char != fwrite(header, sizeof (char), header_size_char, fp))
		{
			fprintf(stderr, "cbf2adscimg: Cannot write header, size %d bytes, of file %s.\n", header_size_char, in_filename);
			if(0 == output_packing)
				fclose(fp);
			else
			{
				status_pclose = pclose(fp);
				if(0 != status_pclose)
				{
					fprintf(stderr, "Status returned from compress command via popen NON-ZERO: %d\n", status_pclose);
					perror("popen command (maybe this will be useful)");
					fprintf(stderr, "Filename being compressed: %s with command: %s\n", out_filename, popen_command);
					fprintf(stderr, "Program exiting.\n");
					exit(0);
				}
			}
			exit(0);
		}

		field[0] = '\0';
		gethd("SIZE1", field, header);
		if('\0' == field[0])
		{
			fprintf(stderr,"cbf2adscimg: keyword SIZE1 not found in header.  Cannot convert file %s\n", in_filename);
			exit(0);
		}
		size1 = atoi(field);
		
		field[0] = '\0';
		gethd("SIZE2", field, header);
		if('\0' == field[0])
		{
			fprintf(stderr,"cbf2adscimg: keyword SIZE2 not found in header.  Cannot convert file %s\n", in_filename);
			exit(0);
		}
		size2 = atoi(field);
		
		data_size = size1 * size2 * sizeof(unsigned short);

		gethd("BYTE_ORDER", field, header);

		cbf_get_local_integer_byte_order(&lbo);

		if (cbf_cistrcmp(field,lbo)) {
                  unsigned char *p;
		  unsigned char temp;
		  size_t ii;
		  p = (unsigned char *)data;
		  for (ii=0; ii<data_size; ii+=2) {
		    temp = p[ii];
		    p[ii] = p[ii+1];
		    p[ii+1] = temp;
                  }
		}

		if(data_size != fwrite(data, sizeof (char), data_size, fp))
		{
			fprintf(stderr, "cbf2adscimg: Cannot write data (size %d bytes) from input file %s.\n", 
					data_size, in_filename);
			if(0 == output_packing)
				fclose(fp);
			else
			{
				status_pclose = pclose(fp);
				if(0 != status_pclose)
				{
					fprintf(stderr, "Status returned from compress command via popen NON-ZERO: %d\n", status_pclose);
					perror("popen command (maybe this will be useful)");
					fprintf(stderr, "Filename being compressed: %s with command: %s\n", out_filename, popen_command);
					fprintf(stderr, "Program exiting.\n");
					exit(0);
				}
			}
			exit(0);
		}
		if(0 == output_packing)
			fclose(fp);
		else
		{
			status_pclose = pclose(fp);
			if(0 != status_pclose)
			{
				fprintf(stderr, "Status returned from compress command via popen NON-ZERO: %d\n", status_pclose);
				perror("popen command (maybe this will be useful)");
				fprintf(stderr, "Filename being compressed: %s with command: %s\n", out_filename, popen_command);
				fprintf(stderr, "Program exiting.\n");
				exit(0);
			}
		}


		free(header);
		free(data);

		argv++;
		argc--;
	}
	exit(0);
}
Example #2
0
int	main(int argc, char *argv[])
{
	FILE		*fp;
	char		in_filename[1024], out_filename[1024];
	char		field[128];
	char		*hptr;
	unsigned short	*uptr;
	char		header_bytes[6];
    int     numargs = argc;
    int     iarg;
    int     argtype_array[argc];
    int     *argtype=argtype_array;
    int     filec;
	int		file_size, header_size_char, actread;
	int		cbf_status;
	int		i, j, k=0;
	int		size1, size2;
	int		file_type;
	int		pack_flags;
	int     pad_flag;
	int		status_pclose;
	int		beam_center_convention;
	int     add_minicbf_header=0;
    int     bin=0;
    int     transpose=0;
    int     interleave=0;
    int     *new_int_data, *old_int_data;
    size_t  new_int_data_size, old_int_data_size;
	double  thickness=0.;
    const char *    argval_array[argc];
    const char * *  argval=argval_array;
	const char *    roi=NULL;
	const char *    thickstr=NULL;
	const char *    polarstr=NULL;
    const char *    scalestr=NULL;
    const char *    offsetstr=NULL;
    const char *    cliplowstr=NULL;
    const char *    cliphighstr=NULL;
    const char *    rad_smoothstr=NULL;
    const char *    ifile=NULL;
    const char *    ofile=NULL;
	char *     strstrhit;
	static char	*endings[] = {
					".img",
					".img.gz",
					".img.bz2",
					".img.Z",
					NULL
				     };
	static char	*flags[32] = {
					"--cbf_byte_offset",            /* 0 */
					"--cbf_packed_v2",              /* 1 */
					"--cbf_packed",                 /* 2 */
					"--no_compression",             /* 3 */
					"--beam_center_from_header",    /* 4 */
					"--beam_center_mosflm",         /* 5 */
					"--beam_center_ulhc",           /* 6 */
					"--beam_center_llhc",           /* 7 */
                    "--pad_1K",                     /* 8 */
                    "--pad_2K",                     /* 9 */
                    "--pad_4K",                     /*10 */
                    "--no_pad",                     /*11 */
                    "--cbf_nibble_offset",          /*12 */
                    "--region-of-interest",         /*13 */
                    "--region_of_interest",         /*14 */
                    "--sensor-thickness",           /*15 */
                    "--sensor_thickness",           /*16 */
                    "--add-minicbf-header",         /*17 */
                    "--add_minicbf_header",         /*18 */
                    "--polarization-source-ratio",  /*19 */
                    "--polarization_source_ratio",  /*20 */
                    "--bin",                        /*21 */
                    "--scale",                      /*22 */
                    "--offset",                     /*23 */
                    "--cliplow",                    /*24 */
                    "--cliphigh",                   /*25 */
                    "--radial-smooth",              /*26 */
                    "--input",                      /*27 */
                    "--output",                     /*28 */
                    "--transpose",                  /*29 */
                    "--interleave",                 /*30 */
					NULL
				   };


	if(argc < 2)
	{
		usage();
		exit(0);
	}
    
    argval[0] = argv[0];
    argtype[0] = -1;
    for (iarg = 1; iarg < numargs; iarg ++) {
        char modc;
        if ( argv[iarg][0] == '-' && argv[iarg][1] == '-' ) {
            for(j = 0; flags[j] != NULL; j++) {
                strstrhit = cbf_cistrnstr(argv[iarg], flags[j], strlen(flags[j]));
                if(NULL != strstrhit && strstrhit==argv[iarg] )
                    break;
            }
            if(NULL == flags[j])
            {
                fprintf(stderr,"adscimg2cbf: %s is an unknown flag\n\n", argv[iarg]);
                usage();
                exit(0);
            }
            argtype[iarg] = j;
            argval[iarg] = argv[iarg]+strlen(flags[j]);
            modc = '\0';
            if (j > 12) modc = '=';
            if(j < 3 || j==12) modc = ',';
            if (argval[iarg][0] != '\0'
                && argval[iarg][0] != modc ) {
                fprintf(stderr,"adscimg2cbf: %s is an unknown flag\n\n", argv[iarg]);
                usage();
                exit(0);
            }
            if (modc != '\0' && argval[iarg][0] == modc) argval[iarg]++;
        } else {
            argval[iarg]=argv[iarg];
            argtype[iarg] = -1;
        }
    }

	pack_flags = CBF_BYTE_OFFSET;
	beam_center_convention = BEAM_CENTER_FROM_HEADER;
	pad_flag = PAD_4K;

	while(argc > 1)
    {
        if ( (j =  argtype[1]) >= 0 ) {
            ifile = NULL;
            switch(j)
            {
                case 0:
                    pack_flags = CBF_BYTE_OFFSET;
                    break;
                case 1:
                    pack_flags = CBF_PACKED_V2;
                    break;
                case 2:
                    pack_flags = CBF_PACKED;
                    break;
                case 3:
                    pack_flags = CBF_NONE;
                    break;
                case 4:
                    beam_center_convention = BEAM_CENTER_FROM_HEADER;
                    break;
                case 5:
                    beam_center_convention = BEAM_CENTER_MOSFLM;
                    break;
                case 6:
                    beam_center_convention = BEAM_CENTER_ULHC;
                    break;
                case 7:
                    beam_center_convention = BEAM_CENTER_LLHC;
                    break;
                case 8:
                    pad_flag = PAD_1K;
                    break;
                case 9:
                    pad_flag = PAD_2K;
                    break;
                case 10:
                    pad_flag = PAD_4K;
                    break;
                case 11:
                    pad_flag = 0;
                    break;
                case 12:
                    pack_flags = CBF_NIBBLE_OFFSET;
                    break;
                case 13:
                case 14:
                    roi = argval[1];
                    break;
                case 15:
                case 16:
                    thickstr = argval[1];
                    break;
                case 17:
                case 18:
                    add_minicbf_header=1;
                    break;
                case 19:
                case 20:
                    polarstr = argval[1];
                    break;
                case 21:
                    bin = 1;
                    break;
                case 22:
                    scalestr = argval[1];
                    break;
                case 23:
                    offsetstr = argval[1];
                    break;
                case 24:
                    cliplowstr = argval[1];
                    break;
                case 25:
                    cliphighstr = argval[1];
                    break;
                case 26:
                    rad_smoothstr = argval[1];
                    break;
                case 27:
                    ifile = argval[1];
                    break;
                case 28:
                    ofile = argval[1];
                    break;
                case 29:
                    if (argval[1][0] == '\0') {
                        transpose = 1;
                    } else {
                        transpose = atoi(argval[1]);
                    }
                    break;
                case 30:
                    if (argval[1][0] == '\0') {
                        interleave = 1;
                    } else {
                        interleave = atoi(argval[1]);
                    }
                    break;

            }
            if(j < 3 || j==12)
            {
                if (cbf_cistrcmp(argval[1],"flat")==0) {
                    pack_flags |= CBF_FLAT_IMAGE;
                } else {
                    if(cbf_cistrcmp(argval[1],"uncorrelated")==0) {
                        pack_flags |= CBF_UNCORRELATED_SECTIONS;
                    } else {
                        fprintf(stderr,"adscimg2cbf: %s is an unknown flag modifier\n\n", argval[1]);
                        usage();
                        exit(0);
                    }
                }
            }
            if (!ifile) {
                argc--;
                argv++;
                argval++;
                argtype++;
            continue;
            }
            
        }
        
        filec = 0;
        for (j = 1; j < argc; j++) {
            if (argtype[j] < 0) filec++;
            if (!ofile || (argtype[j] >= 0 && argtype[j] != 13 && argtype[j] != 14)) break;
        }
        
        old_int_data = NULL;
        old_int_data_size = 0;
        
        while (filec > 0 && argc > 1) {
            
            if (argtype[1]==13 || argtype[1]==14) {
                roi = argval[1];
                argv++;
                argc--;
                argtype++;
                argval++;
                continue;
            }
            
            file_type = 0;
            strcpy(in_filename, argval[1]);
            i = strlen(in_filename);
            for(j = 0; endings[j] != NULL; j++)
            {
                k = strlen(endings[j]);
                if(endswith(in_filename, endings[j]))
                {
                    file_type = j;
                    break;
                }
            }
            if(NULL == endings[j])
            {
                fprintf(stderr,"adscimg2cbf: Input file name %s does not end in .img, .img.gz, or .img.bz2, or .img.Z\n", in_filename);
                exit(0);
            }
            if (!ofile) {
                strcpy(out_filename, in_filename);
                out_filename[i - k] = '\0';
                strcat(out_filename, ".cbf");
            } else {
                strcpy(out_filename,ofile);
            }
            
            if(0 == file_type)
            {
                if(NULL == (fp = fopen(in_filename, "rb")))
                {
                    fprintf(stderr, "adscimg2cbf: Cannot open %s as input .img file\n", in_filename);
                    exit(0);
                }
            }
            else
            {
                if(2 == file_type)
                    sprintf(popen_command, "bzcat %s", in_filename);
                else
                    sprintf(popen_command, "zcat %s", in_filename);
                if(NULL == (fp = popen(popen_command, "rb")))
                {
                    fprintf(stderr, "adscimg2cbf: Cannot exec %s command to uncompress input file\n", popen_command);
                    exit(0);
                }
            }
            
            /*
             *	Get the first header block.  Can't use seeks on input file.
             */
            
            if(NULL == (hptr = malloc(512 * sizeof (char))))
            {
                fprintf(stderr,"adscimg2cbf: cannot allocate memory for first 512 bytes of header of input file %s\n", in_filename);
                exit(0);
            }
            if(512 != (actread=fread(hptr, sizeof (char), 512, fp)))
            {
                fprintf(stderr, "adscimg2cbf: Cannot read first header block of file %s, actual read %d.\n",
                        in_filename,actread);
                if(0 == file_type)
                    fclose(fp);
                else
                {
                    status_pclose = pclose(fp);
                    if(0 != status_pclose)
                    {
                        fprintf(stderr, "Status returned from uncompress command via popen NON-ZERO: %d\n", status_pclose);
                        perror("popen command (maybe this will be useful)");
                        fprintf(stderr, "Program exiting.  This may be evidence of a corrupt compressed file!\n");
                        fprintf(stderr, "Filename being uncompressed: %s with command: %s\n", in_filename, popen_command);
                        exit(0);
                    }
                }
                exit(0);
            }
            for(i = 0; i < 5; i++)
                header_bytes[0 + i] = hptr[15 + i];
            
            header_bytes[5] = '\0';
            header_size_char = atoi(header_bytes);
            
            if(NULL == (hptr = realloc(hptr, header_size_char)))
            {
                fprintf(stderr,"adscimg2cbf: cannot reallocate memory for %d bytes of header of input file %s\n",
                        header_size_char, in_filename);
                exit(0);
            }
            
            if(header_size_char > 512)
            {
                if((header_size_char - 512) != (actread=fread(hptr + 512, sizeof (char),
                                                              (header_size_char - 512), fp)))
                {
                    fprintf(stderr, "adscimg2cbf: Cannot read next %d bytes of header of file %s,"
                            " actual read %d.\n",
                            header_size_char - 512, in_filename, actread);
                    if(0 == file_type)
                        fclose(fp);
                    else
                    {
                        status_pclose = pclose(fp);
                        if(0 != status_pclose)
                        {
                            fprintf(stderr, "Status returned from uncompress command via popen NON-ZERO: %d\n", status_pclose);
                            perror("popen command (maybe this will be useful)");
                            fprintf(stderr, "Program exiting.  This may be evidence of a corrupt compressed file!\n");
                            fprintf(stderr, "Filename being uncompressed: %s with command: %s\n", in_filename, popen_command);
                            exit(0);
                        }
                    }
                    exit(0);
                }
            }
            field[0] = '\0';
            gethd("SIZE1", field, hptr);
            if('\0' == field[0])
            {
                fprintf(stderr,"adscimg2cbf: keyword SIZE1 not found in header.  Cannot convert file %s\n", in_filename);
                exit(0);
            }
            size1 = atoi(field);
            
            field[0] = '\0';
            gethd("SIZE2", field, hptr);
            if('\0' == field[0])
            {
                fprintf(stderr,"adscimg2cbf: keyword SIZE2 not found in header.  Cannot convert file %s\n", in_filename);
                exit(0);
            }
            size2 = atoi(field);
            
            field[0] = '\0';
            gethd("TYPE", field, hptr);
            if (cbf_cistrcmp(field,"unsigned_short") != 0) {
                fprintf(stderr,"adscimg2cbf: TYPE '%s' not processed at this time.  Cannot convert file %s\n", field, in_filename);
                exit(0);
            }
            
            
            file_size = header_size_char + size1 * size2 * sizeof(unsigned short);
            
            if(NULL == (hptr = realloc(hptr, file_size)))
            {
                fprintf(stderr,"adscimg2cbf: cannot reallocate memory (size %d) for input file %s\n",
                        file_size, in_filename);
                exit(0);
            }
            if((file_size - header_size_char) != (actread=fread(hptr + header_size_char, sizeof (char),
                                                                (file_size - header_size_char), fp)))
            {
                fprintf(stderr, "adscimg2cbf: Cannot read data (size %d bytes) from input file %s."
                        " actual read %d\n",
                        file_size - header_size_char, in_filename, actread);
                if(0 == file_type)
                    fclose(fp);
                else
                {
                    status_pclose = pclose(fp);
                    if(0 != status_pclose)
                    {
                        fprintf(stderr, "Status returned from uncompress command via popen NON-ZERO: %d\n", status_pclose);
                        perror("popen command (maybe this will be useful)");
                        fprintf(stderr, "Program exiting.  This may be evidence of a corrupt compressed file!\n");
                        fprintf(stderr, "Filename being uncompressed: %s with command: %s\n", in_filename, popen_command);
                        exit(0);
                    }
                }
                exit(0);
            }
            if(0 == file_type)
                fclose(fp);
            else
            {
                status_pclose = pclose(fp);
                if(0 != status_pclose)
                {
                    fprintf(stderr, "Status returned from uncompress command via popen NON-ZERO: %d\n", status_pclose);
                    perror("popen command (maybe this will be useful)");
                    fprintf(stderr, "Program exiting.  This may be evidence of a corrupt compressed file!\n");
                    fprintf(stderr, "Filename being uncompressed: %s with command: %s\n", in_filename, popen_command);
                    exit(0);
                }
            }
            
            uptr = ((unsigned short *) (hptr + header_size_char));
            
            cbf_status = adscimg2cbf_sub2(hptr,
                                          uptr,
                                          old_int_data,
                                          old_int_data_size,
                                          &new_int_data,
                                          &new_int_data_size,
                                          (ofile && filec > 1)?NULL:out_filename,
                                          pack_flags,
                                          beam_center_convention,
                                          pad_flag,
                                          roi,
                                          thickstr,
                                          add_minicbf_header,
                                          polarstr,
                                          bin,
                                          scalestr,
                                          offsetstr,
                                          cliplowstr,
                                          cliphighstr,
                                          rad_smoothstr,
                                          transpose,
                                          interleave);
            free(hptr);
            if (old_int_data && old_int_data_size) free(old_int_data);
            old_int_data = new_int_data;
            old_int_data_size = new_int_data_size;
            
            argv++;
            argc--;
            argtype++;
            argval++;
            filec--;
        }
    }
	exit(0);
}
Example #3
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;
}
Example #4
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;
}