示例#1
0
void swap_endian_grids(const char *filename_in,const char *filename_out,int mode){

  SID_log("Swapping endian of grids...",SID_LOG_OPEN);

  // Sanity check
  if(check_mode_for_flag(mode,SWAP_SSIMPL_ENDIAN_FROM_NATIVE) && check_mode_for_flag(mode,SWAP_SSIMPL_ENDIAN_FROM_NATIVE))
     SID_trap_error("Invalid mode flag (%d) in swap_endian_grids().",ERROR_LOGIC,mode);

  // Open input and output files
  FILE *fp_in =NULL;
  FILE *fp_out=NULL;
  if((fp_in=fopen(filename_in,"r"))==NULL)
     SID_log("not present.",SID_LOG_CLOSE,filename_in);
  else{
     if((fp_out=fopen(filename_out,"w"))==NULL)
        SID_trap_error("Could not open {%s} for writing.",ERROR_IO_OPEN,filename_out);

     // Read the needed header information and rewind
     int    n[3];
     double L[3];
     int    n_grids;
     int    scheme;
     fread_verify(&(n[0]), sizeof(int),   1,fp_in);
     fread_verify(&(n[1]), sizeof(int),   1,fp_in);
     fread_verify(&(n[2]), sizeof(int),   1,fp_in);
     fread_verify(&(L[0]), sizeof(double),1,fp_in);
     fread_verify(&(L[1]), sizeof(double),1,fp_in);
     fread_verify(&(L[2]), sizeof(double),1,fp_in);
     fread_verify(&n_grids,sizeof(int),   1,fp_in);
     fread_verify(&scheme, sizeof(int),   1,fp_in);
     if(check_mode_for_flag(mode,SWAP_SSIMPL_ENDIAN_TO_NATIVE)){
        swap_endian((char *)(n),       3,sizeof(int));
        swap_endian((char *)(&n_grids),1,sizeof(int));
     }
     int grid_size=n[0]*n[1]*n[2];
     rewind(fp_in);

     // Create a read buffer
     char *buffer=(char *)SID_malloc(sizeof(char)*grid_size*sizeof(fftw_real));

     // Process the file
     rewrite_swap_endian(fp_in,fp_out,3,sizeof(int),   buffer);
     rewrite_swap_endian(fp_in,fp_out,3,sizeof(double),buffer);
     rewrite_swap_endian(fp_in,fp_out,2,sizeof(int),   buffer);
     for(int i_grid=0;i_grid<n_grids;i_grid++){
        rewrite_swap_endian(fp_in,fp_out,GRID_IDENTIFIER_SIZE,sizeof(char),     buffer);
        rewrite_swap_endian(fp_in,fp_out,grid_size,           sizeof(fftw_real),buffer);
     }

     // Free the read buffer
     SID_free(SID_FARG buffer);

     // Close files
     fclose(fp_in);
     fclose(fp_out);
  
     SID_log("Done.",SID_LOG_CLOSE);
  }
}
示例#2
0
void swap_endian_halos_subgroups_local(const char *filename_in_root,
                                       const char *filename_out_root,
                                       const char *filename_halo_type,
                                       int         snap_number,
                                       int         mode) {
    SID_log("Swapping endian of subgroup file...", SID_LOG_OPEN);

    // Sanity check
    if(SID_CHECK_BITFIELD_SWITCH(mode, SWAP_SSIMPL_ENDIAN_FROM_NATIVE) &&
            SID_CHECK_BITFIELD_SWITCH(mode, SWAP_SSIMPL_ENDIAN_FROM_NATIVE))
        SID_exit_error("Invalid mode flag (%d) in swap_endian_halos_subgroups_local().", SID_ERROR_LOGIC, mode);

    // Set filenames
    char filename_in[SID_MAX_FILENAME_LENGTH];
    char filename_out[SID_MAX_FILENAME_LENGTH];
    sprintf(filename_in, "%s/%s_%03d.catalog_subgroups", filename_in_root, filename_halo_type, snap_number);
    sprintf(filename_out, "%s/%s_%03d.catalog_subgroups", filename_out_root, filename_halo_type, snap_number);

    // Open input and output files
    FILE *fp_in  = NULL;
    FILE *fp_out = NULL;
    if((fp_in = fopen(filename_in, "r")) == NULL)
        SID_exit_error("Could not open {%s} for reading.", SID_ERROR_IO_OPEN, filename_in);
    if((fp_out = fopen(filename_out, "w")) == NULL)
        SID_exit_error("Could not open {%s} for writing.", SID_ERROR_IO_OPEN, filename_out);

    // Read the needed header information and rewind
    int n_subgroups;
    int offset_size_bytes;
    SID_fread_verify(&n_subgroups, sizeof(int), 1, fp_in);
    SID_fread_verify(&offset_size_bytes, sizeof(int), 1, fp_in);
    if(SID_CHECK_BITFIELD_SWITCH(mode, SWAP_SSIMPL_ENDIAN_TO_NATIVE)) {
        swap_endian((char *)(&n_subgroups), 1, sizeof(int));
        swap_endian((char *)(&offset_size_bytes), 1, sizeof(int));
    }
    rewind(fp_in);

    // Create a read buffer
    char *buffer = (char *)SID_malloc(sizeof(char) * offset_size_bytes);

    // Process the file
    rewrite_swap_endian(fp_in, fp_out, 2, sizeof(int), buffer);
    for(int i_subgroup = 0; i_subgroup < n_subgroups; i_subgroup++)
        rewrite_swap_endian(fp_in, fp_out, 1, sizeof(int), buffer);
    for(int i_subgroup = 0; i_subgroup < n_subgroups; i_subgroup++)
        rewrite_swap_endian(fp_in, fp_out, 1, offset_size_bytes, buffer);
    for(int i_subgroup = 0; i_subgroup < n_subgroups; i_subgroup++)
        rewrite_swap_endian(fp_in, fp_out, 1, sizeof(int), buffer);

    // Free the read buffer
    SID_free(SID_FARG buffer);

    // Close files
    fclose(fp_in);
    fclose(fp_out);

    SID_log("Done.", SID_LOG_CLOSE);
}