/** A unified header reading routine for .bin and .fits files. It read the array information and string header if any. Return non zero value if reading failed*/ int read_header2(header_t *header, file_t *fp) { int ans; header->str=NULL; if(fp->isfits) { ans=read_fits_header(header, fp); } else { ans=read_bin_header(header, fp); } return ans; }
/*! <pre> ******************************************************************************* * Function : hyp_open_file * Arguments : * Returns : * Purpose : Attempts to open a hypothesis file that is associated with the opened * PFM specified by the PFM handle. NULL is returned if the file ".hyp" * could not be found. ******************************************************************************* </pre>*/ HypHead* hyp_open_file( char *list_file_path, NV_INT32 pfmhnd ) { HypHead *hyp; char filename[256]; /* Form the filename for the .hyp file */ if( hyp_form_hyp_filename( list_file_path, filename ) < 0 ) { sprintf( hyp_err_str, "Could not locate information from PFM list file '%s'.", list_file_path ); return NULL; } /* Create HypHead object */ hyp = (HypHead*)calloc(1, sizeof(HypHead)); memset( hyp, 0, sizeof(HypHead) ); /* Try to open hugeio file */ hyp->pfmhnd = pfmhnd; hyp->filehnd = hfopen( filename, "r+b" ); if( hyp->filehnd == -1 ) { hyp->filehnd = hfopen( filename, "rb" ); hyp->readonly = NVTrue; } if( hyp->filehnd == -1 ) { sprintf( hyp_err_str, "Could not load cube hypotheses from PFM file '%s'.", list_file_path ); if (hyp) { free( hyp ); hyp = NULL; } return NULL; } hyp->cube_param = (CubeParam *) calloc (1, sizeof(CubeParam)); /* Read bin header from pfm file */ read_bin_header( pfmhnd, &hyp->bin_header ); /* Create the Cube parameters */ /* hyp->cube_param = cube_init_param( HYP_DEFAULT_IHO_ORDER, hyp->bin_header.bin_size_xy, hyp->bin_header.bin_size_xy ); hyp->hypo_resolve_algo = hyp->cube_param->mthd; hyp->node_capture_percent = hyp->cube_param->capture_dist_scale * 100.0f; */ /* Read the header information from the file */ hyp_read_header( hyp ); /* Check that hyp version is proper */ if( hyp->version < 1.0 || hyp->version > HYP_VERSION ) { sprintf( hyp_err_str, "Hypothesis file had unsupported version '%.2f'. This file may have been created with a newer version of the software.", hyp->version ); if( hyp->cube_param ) free (hyp->cube_param); /* cube_release_param( hyp->cube_param ); */ if (hyp) { free( hyp ); hyp = NULL; } return NULL; } /* Check that hyp size is proper */ if( (hyp->ncols != hyp->bin_header.bin_width) || (hyp->nrows != hyp->bin_header.bin_height) ) { sprintf( hyp_err_str, "Hypothesis file did not match PFM file in size." ); if (hyp->cube_param ) { free (hyp->cube_param); hyp->cube_param = NULL; } /* cube_release_param( hyp->cube_param ); */ if (hyp) { free( hyp ); hyp = NULL; } return NULL; } /* Make sure file size matches expected */ /* hfseek( hyp->filehnd, 0, SEEK_END ); if( hftell( hyp->filehnd ) != hyp->file_size ) { sprintf( hyp_err_str, "Hypothesis file size is not correct. File may have been corrupted." ); free( hyp ); return NULL; }*/ /* Return created HypHead object */ return hyp; }
/*! <pre> ******************************************************************************* * Function : hyp_create_file * Arguments : * Returns : * Purpose : Creates a new hypothesis file with extension ".hyp". Associates the * structure with an open handle to a PFM file. The HYP data structure * built will be the same size as the existing PFM file. The attributes tell * the library where to find information from the PFM file. The horizontal * and vertical error attributes are required, but the others can be disabled * by passing -1. ******************************************************************************* </pre>*/ HypHead* hyp_create_file( char *list_file_path, NV_INT32 pfmhnd, NV_INT32 horizErrorAttr, NV_INT32 vertErrorAttr, NV_INT32 numHyposAttr, NV_INT32 hypoStrengthAttr, NV_INT32 uncertaintyAttr, NV_INT32 customHypoFlag ) { HypHead *hyp; char filename[256], *data; NV_INT32 hnd, i, j, percent, old_percent; /* Form the filename for the .hyp file */ if( hyp_form_hyp_filename( list_file_path, filename ) < 0 ) { fprintf(stderr, "Could not locate information from PFM list file '%s'.", list_file_path ); return NULL; } /* Try to open hugeio file */ hnd = hfopen( filename, "w+b" ); if( hnd == -1 ) { sprintf( hyp_err_str, "Error creating file in '%s'.", filename ); return NULL; } /* Create new object and initialize */ hyp = (HypHead *)calloc(1, sizeof(HypHead)); memset( hyp, 0, sizeof(HypHead) ); hyp->cube_param = (CubeParam *) calloc (1, sizeof(CubeParam)); read_bin_header( pfmhnd, &hyp->bin_header ); hyp->version = HYP_VERSION; hyp->pfmhnd = pfmhnd; hyp->ncols = hyp->bin_header.bin_width; hyp->nrows = hyp->bin_header.bin_height; hyp->filehnd = hnd; hyp->readonly = NVFalse; #ifdef HYP_LITENDIAN hyp->endian = HYP_FTYPE_LITENDIAN; #else hyp->endian = HYP_FTYPE_BIGENDIAN; #endif hyp->vert_error_attr = vertErrorAttr; hyp->horiz_error_attr = horizErrorAttr; hyp->num_hypos_attr = numHyposAttr; hyp->hypo_strength_attr = hypoStrengthAttr; hyp->uncertainty_attr = uncertaintyAttr; hyp->custom_hypo_flag = customHypoFlag; hyp->deleted_ptr = 0; hyp->file_size = HYP_ASCII_HEADER_SIZE + HYP_BINARY_HEADER_SIZE + ((NV_INT64)hyp->ncols * (NV_INT64)hyp->nrows * HYP_NODE_RECORD_SIZE); /* Create the Cube parameters */ /* hyp->cube_param = cube_init_param( HYP_DEFAULT_IHO_ORDER, hyp->bin_header.bin_size_xy, hyp->bin_header.bin_size_xy ); hyp->hypo_resolve_algo = hyp->cube_param->mthd; hyp->node_capture_percent = hyp->cube_param->capture_dist_scale * 100.0f; */ /* Write the header information to the file */ hyp_write_header( hyp ); /* Write empty information for the Nodes */ hfseek( hyp->filehnd, HYP_ASCII_HEADER_SIZE + HYP_BINARY_HEADER_SIZE, SEEK_SET ); data = (char*)malloc( HYP_NODE_RECORD_SIZE ); memset( data, 0, HYP_NODE_RECORD_SIZE ); old_percent = 0; for( i = 0; i < hyp->nrows; i++ ) { /* Write row data */ for( j = 0; j < hyp->ncols; j++ ) { hfwrite( (void*)data, HYP_NODE_RECORD_SIZE, 1, hyp->filehnd ); } /* Update status callback */ percent = (NV_INT32)( ((NV_FLOAT32)i / hyp->nrows) * 100.0 ); if (percent != old_percent) { /* Calls a status callback if register. */ if (hyp_progress_callback) (*hyp_progress_callback) (1, percent); old_percent = percent; } } free( data ); /* Return the created HypHead object */ return hyp; }