Example #1
0
void write_rawiv_float(float *result,FILE* fp)
{
	int i, j, k;
	float c_float;
	size_t fwrite_return;
	
	
	//#ifdef _LITTLE_ENDIAN
	if(!big_endian())
	  {
	    swap_buffer((char *)minext, 3, sizeof(float));
	    swap_buffer((char *)maxext, 3, sizeof(float));
	    swap_buffer((char *)&nverts, 1, sizeof(int));
	    swap_buffer((char *)&ncells, 1, sizeof(int));
	    swap_buffer((char *)dim, 3, sizeof(unsigned int));
	    swap_buffer((char *)orig, 3, sizeof(float));
	    swap_buffer((char *)span, 3, sizeof(float));
	  }
	//#endif 
	
	fwrite_return = fwrite(minext, sizeof(float), 3, fp);
	fwrite_return = fwrite(maxext, sizeof(float), 3, fp);
	fwrite_return = fwrite(&nverts, sizeof(int), 1, fp);
	fwrite_return = fwrite(&ncells, sizeof(int), 1, fp);
	fwrite_return = fwrite(dim, sizeof(unsigned int), 3, fp);
	fwrite_return = fwrite(orig, sizeof(float), 3, fp);
	fwrite_return = fwrite(span, sizeof(float), 3, fp);
	//#ifdef _LITTLE_ENDIAN
	if(!big_endian())
	  {
	    swap_buffer((char *)minext, 3, sizeof(float));
	    swap_buffer((char *)maxext, 3, sizeof(float));
	    swap_buffer((char *)&nverts, 1, sizeof(int));
	    swap_buffer((char *)&ncells, 1, sizeof(int));
	    swap_buffer((char *)dim, 3, sizeof(unsigned int));
	    swap_buffer((char *)orig, 3, sizeof(float));
	    swap_buffer((char *)span, 3, sizeof(float));
	  }
	//#endif 
	
	for (k=0; k<ZDIM; k++) 
		for (j=0; j<YDIM; j++)
			for (i=0; i<XDIM; i++) {
				
				c_float = result[IndexVect(i,j,k)];
				
				//#ifdef _LITTLE_ENDIAN
				if(!big_endian())
				  swap_buffer((char *)&c_float, 1, sizeof(float));
				//#endif 
				fwrite_return = fwrite(&c_float, sizeof(float), 1, fp);
				//#ifdef _LITTLE_ENDIAN
				if(!big_endian())
				  swap_buffer((char *)&c_float, 1, sizeof(float));
				//#endif 
			}
				
				fclose(fp);
}
Example #2
0
static inline std::string encode_seq_key(uint64_t seq){
	seq = big_endian(seq);
	std::string ret;
	ret.push_back(DataType::SYNCLOG);
	ret.append((char *)&seq, sizeof(seq));
	return ret;
}
Example #3
0
/****************************************************
 * write_binary_contraction_data
 ****************************************************/
int write_binary_contraction_data_3d(double * const s, LimeWriter * limewriter, const int prec, const int N, DML_Checksum * ans) {
#ifdef MPI
  fprintf(stderr, "[write_binary_contraction_data_3d] No mpi version.\n");
  return(1);
#else
  int x, y, z, i=0, mu, status=0;
  double *tmp;
  float  *tmp2;
  int proc_coords[4], tloc,xloc,yloc,zloc, proc_id;
  n_uint64_t bytes;
  DML_SiteRank rank;
  int words_bigendian = big_endian();
  unsigned int VOL3 = LX*LY*LZ;
  DML_checksum_init(ans);

  tmp = (double*)malloc(2*N*sizeof(double));
  tmp2 = (float*)malloc(2*N*sizeof(float));

  if(prec == 32) bytes = (n_uint64_t)2*N*sizeof(float);
  else bytes = (n_uint64_t)2*N*sizeof(double);

  if(g_cart_id==0) {
      for(x = 0; x < LX; x++) {
      for(y = 0; y < LY; y++) {
      for(z = 0; z < LZ; z++) {
        /* Rank should be computed by proc 0 only */
        rank = (DML_SiteRank) (( x * LY + y)*LZ + z);
        for(mu=0; mu<N; mu++) {
          i = _GWI(mu, g_ipt[0][x][y][z], VOL3);
          if(!words_bigendian) {
            if(prec == 32) {
              byte_swap_assign_double2single( (tmp2+2*mu), (s + i), 2);
            } else {
              byte_swap_assign( (tmp+2*mu), (s + i), 2);
            }
          } else {
            if(prec == 32) {
              double2single((float*)(tmp2+2*mu), (s + i), 2);
            } else {
              tmp[2*mu  ] = s[i  ];
              tmp[2*mu+1] = s[i+1];
            }
          }
        }
        if(prec == 32) {
          DML_checksum_accum(ans,rank,(char *) tmp2,2*N*sizeof(float));
          status = limeWriteRecordData((void*)tmp2, &bytes, limewriter);
        }
        else {
          status = limeWriteRecordData((void*)tmp, &bytes, limewriter);
          DML_checksum_accum(ans,rank,(char *) tmp, 2*N*sizeof(double));
        }
      }}}
  }
  free(tmp2);
  free(tmp);
  return(0);
#endif
}
Example #4
0
static inline uint64_t decode_seq_key(const leveldb::Slice &key){
	uint64_t seq = 0;
	if(key.size() == (sizeof(uint64_t) + 1) && key.data()[0] == DataType::SYNCLOG){
		seq = *((uint64_t *)(key.data() + 1));
		seq = big_endian(seq);
	}
	return seq;
}
Example #5
0
 template<class T> bool 
 unpack_int (T *v)
 {
   u_int8_t buf[sizeof (T)];
   bool ok = get_bytes (buf, sizeof (T));
   if (ok) {
     big_endian (*v, buf, sizeof (T));
   } 
   return ok;
 }
Example #6
0
static unsigned read_uint (FILE *stream)
{
	unsigned char b[MNIST_SIZE_INT];
	size_t read = fread(b, MNIST_SIZE_INT, 1, stream);

	if (!read) {
		return 0;
	}

	return big_endian()
		? (b[3] << 24) + (b[2] << 16) + (b[1] << 8) + b[0]
		: (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + b[3];
}
Example #7
0
FILE *open_wave_file (NV_CHAR *path)
{
  FILE *fp;

  NV_INT32 big_endian ();


  swap = (NV_BOOL) big_endian ();
  first = NVTrue;

  if ((fp = fopen64 (path, "rb")) == NULL)
    {
      perror (path);
    }
  else
    {
      wave_read_header (fp, &l_head);
    }


  return (fp);
}
Example #8
0
// Check checksums
void file_check(const size_t glob_check,
                const std::vector<boost::uint64_t>& checksums,
                const std::vector<cmplx>& correlators){
  size_t tmp = 0;
  size_t bytecount = correlators.size()*sizeof(double);
  
  tmp = checksum<std::vector<cmplx> > (correlators, bytecount);
  if (big_endian()) swap_endian <size_t> (tmp);
  std::cout << bytecount << std::endl;
  if (tmp == glob_check) {
    std::cout << " File Checksum matches data. "<< std::endl;
  }
  else{
    std::cout << " Checksum broken, please check correlation functions ! "
       << std::endl;
    std::cout << "Read in: " << glob_check << " calculated: " << tmp << std::endl; 
  }

}
Example #9
0
static int X11_xshm_xv_blit(struct DriverInstance* sh,
                            const uint8_t* fb,
                            int width, int height,
                            struct blit_params* params,
                            int needs_adjust,
                            char* error_text, int text_len)
{
  // wait until last blit has completed
  if (sh->event_pending)
    {
      int max_wait = 15;
      XEvent event;
      sh->event_pending = 0;
      
      while (max_wait--)
        {
          XNextEvent(sh->display, &event);

          if (event.type == sh->completion_type)
            break;          
        }
    }


  if (sh->xv_image == 0 ||
      width  != sh->image_width ||
      height != sh->image_height)
    {
      if (sh->xv_image)
        XFree(sh->xv_image);

      sh->xv_image = XvShmCreateImage(sh->display,
                                      sh->xv_port,
                                      sh->xv_format_id,
                                      sh->shminfo.shmaddr,
                                      width, height,
                                      &sh->shminfo);
      sh->image_width  = width;
      sh->image_height = height;
    }

  //TODO: this is a hack for big-endian machines
  if (big_endian())
    convert_endianness((uint8_t*) fb, sh->width, sh->height);

  if (sh->vis.depth == 24)
    {
#if defined(OPT_INCLUDE_MMX)
      if (sh->mmx_supported)
        cvt_rgb32_to_i420_mmx((uint8_t*) sh->xv_image->data,
                              (uint32_t*) fb, width, height,
                              sh->xv_image->pitches, sh->xv_image->offsets);
      else
#endif
        cvt_rgb32_to_i420((uint8_t*) sh->xv_image->data,
                          (uint32_t*) fb, width, height,
                          sh->xv_image->pitches, sh->xv_image->offsets);
    }
  else if (sh->vis.depth == 16)
    cvt_rgb16_to_i420((uint8_t*) sh->xv_image->data,
                      (uint16_t*) fb, width, height,
                      sh->xv_image->pitches, sh->xv_image->offsets);

  // blit shared mem image
  XvShmPutImage(sh->display, sh->xv_port, sh->win, sh->gc, sh->xv_image,
                0, 0, width, height, 0, 0, sh->width, sh->height, True);
  sh->event_pending = 1;

  XFlush(sh->display);
  //XSync(sh->display, False);
  return 1;
}
Example #10
0
// Read in the gaugefield, deprecated tmLqcd routine
void GaugeField::read_lime_gauge_field_doubleprec_timeslices(double *gaugefield,
                                                             const char *filename,
                                                             const ssize_t slice_i,
                                                             const ssize_t slice_f) {
  try {
    // const int slice_i = 0;
    // const int slice_f = Lt+1;

    FILE *ifs;
    ssize_t t, x, y, z;
    int status;
    n_uint64_t bytes;
    char *header_type;
    LimeReader *limereader;
    double tmp[72], tmp2[72];
    int words_bigendian;

    printf("reading gauge fields from files:\n");
    //    if(verbose){
    //      printf("reading gauge fields from files:\n");
    //    }
    //    else{
    //      printf("\treading gauge fields:\n");
    //    }

    words_bigendian = big_endian();
    ifs = fopen(filename, "r");
    if (ifs == (FILE *)NULL) {
      fprintf(stderr, "Could not open file %s\n Aborting...\n", filename);
      exit(500);
    }
    limereader = limeCreateReader(ifs);
    if (limereader == (LimeReader *)NULL) {
      throw std::runtime_error("Unable to open LimeReader!");
    }
    while ((status = limeReaderNextRecord(limereader)) != LIME_EOF) {
      if (status != LIME_SUCCESS) {
        fprintf(
            stderr, "limeReaderNextRecord returned error with status = %d!\n", status);
        status = LIME_EOF;
        break;
      }
      header_type = limeReaderType(limereader);
      if (strcmp("ildg-binary-data", header_type) == 0)
        break;
    }
    if (status == LIME_EOF) {
      fprintf(stderr, "no ildg-binary-data record found in file %s\n", filename);
      limeDestroyReader(limereader);
      fclose(ifs);
      exit(-2);
    }
    bytes = limeReaderBytes(limereader);
    if (bytes != (n_uint64_t)Lx * Ly * Lz * Lt * 72 * (n_uint64_t)sizeof(double)) {
      if (bytes != (n_uint64_t)Lx * Ly * Lz * Lt * 72 * (n_uint64_t)sizeof(double) / 2) {
        fprintf(stderr,
                "Probably wrong lattice size or precision (bytes=%lu) in file %s "
                "expected %lu\n",
                (n_uint64_t)bytes,
                filename,
                (n_uint64_t)Lx * Ly * Lz * Lt * 72 * (n_uint64_t)sizeof(double));
        fprintf(stderr, "Aborting...!\n");
        fflush(stdout);
        exit(501);
      } else {
        fclose(ifs);
        throw std::runtime_error("Single precision read!");
      }
    }

    bytes = (n_uint64_t)72 * sizeof(double);

    for (t = 0; t < Lt; t++) {
      for (z = 0; z < Lz; z++) {
        for (y = 0; y < Ly; y++) {
          for (x = 0; x < Lx; x++) {
            // check for endianess and reading in data
            // the pointer limereader is internally increased by bytes
            // in the limeReaderReadData function
            if (!words_bigendian) {
              status = limeReaderReadData(tmp, &bytes, limereader);
              byte_swap_assign(tmp2, tmp, 72);
            } else
              status = limeReaderReadData(tmp2, &bytes, limereader);
            // check if reading was successfull
            if (status < 0 && status != LIME_EOR) {
              fprintf(stderr,
                      "LIME read error occured with status = %d while reading file %s!\n "
                      "Aborting...\n",
                      status,
                      filename);
              exit(500);
            }

            // we just want to read in data in the specific range of timeslices
            // must be here because the limereader pointer must be increased
            // correctly
            // could be done with much more performance but might be tricky to
            // do correctly
            if (t < slice_i || t > slice_f)
              continue;

            // copy of link variables from tmp2 into config
            // ILDG has mu-order: x,y,z,t so it is changed here to: t,x,y,z !
            const ssize_t p =
                (ssize_t)(((t - slice_i) * Lx * Lz * Lz + x * Ly * Lz + y * Lz + z) *
                          72);  // position in config
            ssize_t k = 0;
            for (ssize_t mu = 1; mu <= 4; mu++) {  // mu=4 is for the shift of U_t
              ssize_t index;
              if (mu != 4)
                index = p + mu * 18;  // for U_x, U_y and U_z
              else
                index = p;  // U_t is copied into the beginning of
              // the (config+p) array

              for (ssize_t i = 0; i < 3; i++) {
                for (ssize_t j = 0; j < 3; j++) {
                  gaugefield[index + 6 * i + 2 * j] = tmp2[2 * k];
                  gaugefield[index + 6 * i + 2 * j + 1] = tmp2[2 * k + 1];
                  k++;
                }
              }

            }  // loop over mu ends here

          }  // loop over position space ends here
        }
      }
    }
    if (status < 0 && status != LIME_EOR) {
      fprintf(stderr,
              "LIME read error occured with status = %d while reading file %s!\n "
              "Aborting...\n",
              status,
              filename);
      exit(500);
    }
    limeDestroyReader(limereader);
    fclose(ifs);
    return;

  } catch (std::exception &e) {
    std::cout << e.what()
              << "in: ReadWrite::read_lime_gauge_field_doubleprec_timeslices\n";
    throw e;
  }
}
Example #11
0
int main(int argc, char **argv)
{
  unsigned long long i,j,k;
  unsigned int red_var,green_var,blue_var,alpha_var,timestep;
  color_t colors[257]; /* one extra for error handling */
  size_t num_colors = 0;

  atexit(cleanup);

  if(argc != 2 && argc != 8)
    {
      printf("Every color in the input RawVRGBA file (RawV with 4 variables: red, green, blue, and alpha)\n"
	     "will be mapped to some voxel value range within [0-255] in the output rawiv file (unsigned char volume).\n"
	     "The number of values in each color's range is dependent on the number of different voxel colors in the\n"
	     "RawVRGBA volume. The input voxel's alpha value will determine which value within each color's selected\n"
	     "range the output voxel will take.  If the input voxel's alpha value is 0, then the value in the output volume\n"
	     "will be 0. Due to the fact that the output volume voxels are of unsigned char\n"
	     "type, the maximum number of different colors that can be represented in the output volume are 256.  If\n"
	     "there are more than 256 colors, those colors will cause their corresponding output voxel to take the value\n"
	     "0.\n\n");
      printf("Usage: %s <input rawv with >=4 variables> <red var> <green var> <blue var> <alpha var> <timestep> <output rawiv>\n\n",argv[0]);
      printf("Example: %s heart.rawv 0 1 2 3 0 heart.rawiv\n"
	     "Most RawVRGBA files have the RGBA variables in order, thus the above example should work in most cases.\n"
	     "To produce a rendering similar to the RGBA rendering of the input RawV file, make sure to map each voxel\n"
	     "value range to the color used in the RawV file.",argv[0]);
      return 0;
    }

  vol = new MappedRawVFile(argv[1],true,true);
  if(!vol->isValid())
    {
      printf("Error loading %s!\n",argv[1]);
      return 1;
    }

  printf("File: %s\n",argv[1]);
  printf("Num Vars: %d\n",vol->numVariables());
  printf("Vars: ");
  for(i=0; i<vol->numVariables(); i++) printf("%s ",vol->get(i,0)->name());
  printf("\n");
  printf("Num Timesteps: %d\n",vol->numTimesteps());
  printf("Dimensions: %d x %d x %d\n",vol->XDim(),vol->YDim(),vol->ZDim());
  printf("Span: %lf x %lf x %lf\n",vol->XSpan(),vol->YSpan(),vol->ZSpan());
  printf("TSpan: %lf\n",vol->TSpan());

  if(argc == 2) { return 0; } /* only need to print out volume info */

  red_var = atoi(argv[2]);
  green_var = atoi(argv[3]);
  blue_var = atoi(argv[4]);
  alpha_var = atoi(argv[5]);
  timestep = atoi(argv[6]);

  outvol = fopen(argv[7],"wb+");
  if(outvol == NULL)
    {
      char err_str[512];
      sprintf(err_str,"Error opening %s",argv[7]);
      perror(err_str);
      return 1;
    }

  unsigned long long len = vol->XDim()*vol->YDim()*vol->ZDim()*sizeof(unsigned char)+68;

  /* create the header for the new file */
  RawIVHeader header;
  header.min[0] = 0.0; header.min[1] = 0.0; header.min[2] = 0.0;
  header.max[0] = (vol->XDim()-1)*vol->XSpan();
  header.max[1] = (vol->YDim()-1)*vol->YSpan();
  header.max[2] = (vol->ZDim()-1)*vol->ZSpan();
  header.numVerts = vol->XDim()*vol->YDim()*vol->ZDim();
  header.numCells = (vol->XDim()-1)*(vol->YDim()-1)*(vol->ZDim()-1);
  header.dim[0] = vol->XDim(); header.dim[1] = vol->YDim(); header.dim[2] = vol->ZDim();
  header.origin[0] = 0.0; header.origin[1] = 0.0; header.origin[2] = 0.0;
  header.span[0] = vol->XSpan(); header.span[1] = vol->YSpan(); header.span[2] = vol->ZSpan();
	
  if(!big_endian())
    {
      for(i=0; i<3; i++) SWAP_32(&(header.min[i]));
      for(i=0; i<3; i++) SWAP_32(&(header.max[i]));
      SWAP_32(&(header.numVerts));
      SWAP_32(&(header.numCells));
      for(i=0; i<3; i++) SWAP_32(&(header.dim[i]));
      for(i=0; i<3; i++) SWAP_32(&(header.origin[i]));
      for(i=0; i<3; i++) SWAP_32(&(header.span[i]));
    }

  fwrite(&header,sizeof(RawIVHeader),1,outvol);
	
  /* get a slice at a time because it's quicker */
  unsigned char *slice[5]; /* [0-3] == rgba slices, [4] == output slice */
  for(i=0; i<5; i++) slice[i] = (unsigned char *)malloc(vol->XDim()*vol->YDim()*sizeof(unsigned char));
	
  for(k=0; k<vol->ZDim(); k++)
    {
      /* get the colors for this slice (ignoring alpha for now) */
      vol->get(red_var,timestep)->getMapped(0,0,k,vol->XDim(),vol->YDim(),1,slice[0]);
      vol->get(green_var,timestep)->getMapped(0,0,k,vol->XDim(),vol->YDim(),1,slice[1]);
      vol->get(blue_var,timestep)->getMapped(0,0,k,vol->XDim(),vol->YDim(),1,slice[2]);
	  
      /* check each colored voxel and add all new colors to the list to determine output voxel ranges */
      color_t color;
      for(i=0; i<vol->XDim(); i++)
	for(j=0; j<vol->YDim(); j++)
	  {
	    if(num_colors > 256)
	      {
		printf("Warning: more than 256 colors! Any color not in the list will result in a zero output voxel.\n");
		num_colors = 256;
		goto docalc;
	      }
		
	    color.r = slice[0][i+j*vol->XDim()];
	    color.g = slice[1][i+j*vol->XDim()];
	    color.b = slice[2][i+j*vol->XDim()];
		  
	    if(lsearch(&color,colors,&num_colors,sizeof(color_t),color_cmp) == NULL)
	      {
		printf("Error in lsearch()!\n");
		return 1;
	      }
	  }
		
      fprintf(stderr,"Determining color list... %5.2f %%   \r",(((float)k)/((float)((int)(vol->ZDim()-1))))*100.0);
    }
  printf("\n");
	
 docalc:;

  printf("Number of colors: %d\n",num_colors);
  printf("Colors: ");
  for(i=0; i<num_colors; i++)
    printf("(%d,%d,%d) ",colors[i].r,colors[i].g,colors[i].b);
  printf("\n");
  unsigned int range_size = 256/num_colors; /* range size == the whole space divided by the number of found colors */
  printf("Range size: %d\n",range_size);
	
  /* now write the output volume */
  for(k=0; k<vol->ZDim(); k++)
    {
      /* get the colors for this slice */
      vol->get(red_var,timestep)->getMapped(0,0,k,vol->XDim(),vol->YDim(),1,slice[0]);
      vol->get(green_var,timestep)->getMapped(0,0,k,vol->XDim(),vol->YDim(),1,slice[1]);
      vol->get(blue_var,timestep)->getMapped(0,0,k,vol->XDim(),vol->YDim(),1,slice[2]);
      vol->get(alpha_var,timestep)->getMapped(0,0,k,vol->XDim(),vol->YDim(),1,slice[3]);
	  
      /* lookup each color to determine it's output voxel value */
      /* check each colored voxel and add all new colors to the list to determine output voxel ranges */
      color_t color,*cur;
      unsigned int index, min, max;
      for(i=0; i<vol->XDim(); i++)
	for(j=0; j<vol->YDim(); j++)
	  {
	    color.r = slice[0][i+j*vol->XDim()];
	    color.g = slice[1][i+j*vol->XDim()];
	    color.b = slice[2][i+j*vol->XDim()];
		  
	    cur = (color_t *)lfind(&color,colors,&num_colors,sizeof(color_t),color_cmp);
	    if(cur == NULL)
	      {
		slice[4][i+j*vol->XDim()] = 0;
		continue;
	      }
	    index = ((unsigned int)(cur - colors)); /* determine the color's index */
	    min = index*range_size; /* find the start of this color's range */
	    max = min+range_size-1; /* find the end of this color's range */ /* Note: due to the discreet nature of unsigned char,
										we may not use the entire available 256 voxel values.
									     */
	    /* now use the color's alpha value to determine where on the range the output voxel is */
	    slice[4][i+j*vol->XDim()] = slice[3][i+j*vol->XDim()] == 0 ? 0 : (unsigned char)(min + float(range_size-1)*(float(slice[3][i+j*vol->XDim()])/255.0));
	  }
	
      fwrite(slice[4],sizeof(unsigned char),vol->XDim()*vol->YDim(),outvol);
	
      fprintf(stderr,"Writing output volume... %5.2f %%   \r",(((float)k)/((float)((int)(vol->ZDim()-1))))*100.0);
    }
  printf("\n");
	
  for(i=0; i<5; i++) free(slice[i]);
	
  return 0;
}
Example #12
0
FILE *open_gps_file (NV_CHAR *path)
{
  FILE                   *fp;
  NV_INT32               i;
  GPS_OUTPUT_T           gps;
  time_t                 tv_sec;
  NV_INT32               tv_nsec;
  struct tm              tm;
  static NV_INT32        tz_set = 0;


  NV_INT32 big_endian ();


  sscanf (&path[strlen (path) - 15], "%02d%02d%02d", &year, &month, &day);


  /*  tm struct wants years since 1900!!!  */

  tm.tm_year = year + 100;
  tm.tm_mon = month - 1;
  tm.tm_mday = day;
  tm.tm_hour = 0.0;
  tm.tm_min = 0.0;
  tm.tm_sec = 0.0;
  tm.tm_isdst = -1;

  if (!tz_set)
    {
#ifdef NVWIN3X
  #ifdef __MINGW64__
      _putenv("TZ=GMT");
      _tzset();
  #else
      _putenv("TZ=GMT");
      _tzset();
  #endif
#else
      putenv("TZ=GMT");
      tzset();
#endif
      tz_set = 1;
    }


  /*  Get seconds from the epoch (01-01-1970) for the date in the filename. 
      This will also give us the day of the week for the GPS seconds of
      week calculation.  */

  tv_sec = mktime (&tm);
  tv_nsec = 0.0;


  /*  Subtract the number of days since Saturday midnight (Sunday morning) in seconds.  */

  tv_sec = tv_sec - (tm.tm_wday * 86400);
  start_week = tv_sec;


  /*  We have to assume that the file is little endian since there is no
      header and no field that we can use to deduce what it is.  */

  swap = (NV_BOOL) big_endian ();


  if ((fp = fopen (path, "rb")) == NULL)
    {
      return ((FILE *) NULL);
    }
  else
    {
      fread (&gps, sizeof (GPS_OUTPUT_T), 1, fp);
      if (swap) swap_gps (&gps);
      start_timestamp = (NV_INT64) (((NV_FLOAT64) start_week + gps.gps_time) * 1000000.0);
      start_record = 0;


      i = fseeko64 (fp, -sizeof (GPS_OUTPUT_T), SEEK_END);

      fread (&gps, sizeof (GPS_OUTPUT_T), 1, fp);
      if (swap) swap_gps (&gps);
      end_timestamp = (NV_INT64) (((NV_FLOAT64) start_week + gps.gps_time) * 1000000.0);

      end_record = ftell (fp) / sizeof (GPS_OUTPUT_T);

      fseek (fp, 0, SEEK_SET);
    }

  return (fp);
}
Example #13
0
File: io.c Project: robfre21/tmLQCD
int read_lime_gauge_field_doubleprec(double * config, char * filename,
                                     const int T, const int LX, const int LY, const int LZ) {
    FILE * ifs;
    int t, x, y, z, status, p=0;
    n_uint64_t bytes;
    char * header_type;
    LimeReader * limereader;
    double tmp[72];
    int words_bigendian;

    words_bigendian = big_endian();
    ifs = fopen(filename, "r");
    if(ifs == (FILE *)NULL) {
        fprintf(stderr, "Could not open file %s\n Aborting...\n", filename);
        exit(500);
    }
    limereader = limeCreateReader( ifs );
    if( limereader == (LimeReader *)NULL ) {
        fprintf(stderr, "Unable to open LimeReader\n");
        exit(500);
    }
    while( (status = limeReaderNextRecord(limereader)) != LIME_EOF ) {
        if(status != LIME_SUCCESS ) {
            fprintf(stderr, "limeReaderNextRecord returned error with status = %d!\n", status);
            status = LIME_EOF;
            break;
        }
        header_type = limeReaderType(limereader);
        if(!strcmp("ildg-binary-data",header_type)) break;
    }
    if(status == LIME_EOF) {
        fprintf(stderr, "no ildg-binary-data record found in file %s\n",filename);
        limeDestroyReader(limereader);
        fclose(ifs);
        exit(-2);
    }
    bytes = limeReaderBytes(limereader);
    if((int)bytes != LX*LY*LZ*T*72*sizeof(double)) {
        fprintf(stderr, "Probably wrong lattice size or precision (bytes=%d) in file %s\n", (int)bytes, filename);
        fprintf(stderr, "Aborting...!\n");
        fflush( stdout );
        exit(501);
    }

    bytes = (n_uint64_t)72*sizeof(double);

    for(t = 0; t < T; t++) {
        for(z = 0; z < LZ; z++) {
            for(y = 0; y < LY; y++) {
                for(x = 0; x < LX; x++) {
                    p = (((t*LZ+z)*LY+y)*LX+x)*72;
                    if(!words_bigendian) {
                        status = limeReaderReadData(tmp, &bytes, limereader);
                        byte_swap_assign(&config[p], tmp, 72);
                    }
                    else {
                        status = limeReaderReadData(&config[p], &bytes, limereader);
                    }
                    if(status < 0 && status != LIME_EOR) {
                        fprintf(stderr, "LIME read error occured with status = %d while reading file %s!\n Aborting...\n",
                                status, filename);
                        exit(500);
                    }
                }
            }
        }
    }
    limeDestroyReader(limereader);
    fclose(ifs);
    return(0);
}
Example #14
0
File: repl.cpp Project: lamphp/ssdb
static inline std::string encode_key(int pos){
	pos = big_endian((uint32_t)pos);
	std::string ret = "sync|";
	ret.append((char *)&pos, sizeof(int));
	return ret;
}
Example #15
0
int read_binary_contraction_data_3d(double * const s, LimeReader * limereader, const int prec, const int N, DML_Checksum *ans) {
#ifdef MPI
  fprintf(stderr, "[read_binary_contraction_data_3d] No mpi version.\n");
  return(1);
#else
  int status=0, mu;
  n_uint64_t bytes, ix;
  double *tmp;
  DML_SiteRank rank;
  float *tmp2;
  int x, y, z;
  int words_bigendian = big_endian();
  unsigned int VOL3 = LX * LY * LZ;

  DML_checksum_init(ans);
  rank = (DML_SiteRank) 0;
 
  if( (tmp = (double*)malloc(2*N*sizeof(double))) == (double*)NULL ) {
    exit(500);
  }
  if( (tmp2 = (float*)malloc(2*N*sizeof(float))) == (float*)NULL ) {
    exit(501);
  }
 
 
  if(prec == 32) bytes = 2*N*sizeof(float);
  else bytes = 2*N*sizeof(double);
  for(x = 0; x < LX; x++){
  for(y = 0; y < LY; y++){
  for(z = 0; z < LZ; z++){
    ix = g_ipt[0][x][y][z];
    rank = (DML_SiteRank) (( LXstart + x)*(LY*g_nproc_y) + LYstart + y)*LZ + z;
    if(prec == 32) {
      status = limeReaderReadData(tmp2, &bytes, limereader);
      DML_checksum_accum(ans,rank,(char *) tmp2, bytes);	    
    }
    else {
      status = limeReaderReadData(tmp, &bytes, limereader);
      DML_checksum_accum(ans,rank,(char *) tmp, bytes);
    }
 
    for(mu=0; mu<N; mu++) {
      if(!words_bigendian) {
        if(prec == 32) {
          byte_swap_assign_single2double(s + _GWI(mu,ix,VOL3), (float*)(tmp2+2*mu), 2);
        } else {
          byte_swap_assign(s + _GWI(mu,ix,VOL3), (float*)(tmp+2*mu), 2);
        }
      } else {  // words_bigendian true
        if(prec == 32) {
          single2double(s + _GWI(mu,ix,VOL3), (float*)(tmp2+2*mu), 2);
        }
        else {
          s[_GWI(mu, ix,VOL3)  ] = tmp[2*mu  ];
          s[_GWI(mu, ix,VOL3)+1] = tmp[2*mu+1];
        }
      }
    }

    if(status < 0 && status != LIME_EOR) {
      return(-1);
    }
  }}}
  if(g_cart_id == 0) printf("\n# [read_binary_contraction_data] The final checksum is %#lx %#lx\n", (*ans).suma, (*ans).sumb);

  free(tmp2); free(tmp);
  return(0);
#endif
}
Example #16
0
bool MappedRawVFile::readHeader()
{
  Variable::VariableType rawv_type_conv[] = { Variable::UCHAR, Variable::UCHAR, Variable::USHORT, Variable::UINT, Variable::FLOAT, Variable::DOUBLE };
  int rawv_type_sizes[] = { 0, 1, 2, 4, 4, 8 };
  char *rawv_type_strings[] = { NULL, "unsigned char", "unsigned short", "unsigned int/long", "float", "double" };
  unsigned int i,j,a,b,c;
  VariableRecord *var_records;
  RawVHeader header;
  lfmap_ptr_t head_ptr, var_rec_ptr;

  head_ptr = lfmap_ptr(m_LFMappedVolumeFile,0,sizeof(RawVHeader));
  if(head_ptr == NULL)
    {
      fprintf(stderr,"MappedRawVFile::readHeader(): Truncated header, invalid RawV file.\n");
      return false;
    }

  memcpy(&header,head_ptr,sizeof(RawVHeader));
  
  if(!big_endian())
    {
      SWAP_32(&(header.magic));
      for(i=0; i<3; i++) SWAP_32(&(header.dim[i]));
      SWAP_32(&(header.numTimesteps));
      SWAP_32(&(header.numVariables));
      for(i=0; i<4; i++) SWAP_32(&(header.min[i]));
      for(i=0; i<4; i++) SWAP_32(&(header.max[i]));
    }
  
  /* initial error check */
  if(header.magic != 0xBAADBEEF)
    {
      fprintf(stderr,"MappedRawVFile::readHeader(): Error: Magic number not present in file.\n");
      return false;
    }
  
  /* variable initialization */
  m_XDim = header.dim[0];
  m_YDim = header.dim[1];
  m_ZDim = header.dim[2];
  m_XSpan = (header.max[0] - header.min[0])/(header.dim[0] - 1);
  m_YSpan = (header.max[1] - header.min[1])/(header.dim[1] - 1);
  m_ZSpan = (header.max[2] - header.min[2])/(header.dim[2] - 1);
  m_TSpan = (header.max[3] - header.min[3])/header.numTimesteps;
#ifdef DEBUG
  printf("MappedRawVFile::readHeader(): XDim: %lld, YDim: %lld, ZDim: %lld\n",m_XDim,m_YDim,m_ZDim);
  printf("MappedRawVFile::readHeader(): XSpan: %f, YSpan: %f, ZSpan: %f, TSpan: %f\n",m_XSpan,m_YSpan,m_ZSpan,m_TSpan);
#endif
  m_NumVariables = header.numVariables;
  m_NumTimesteps = header.numTimesteps;
#ifdef DEBUG
  printf("MappedRawVFile::readHeader(): Num Variables: %d, Num Timesteps: %d\n",m_NumVariables,m_NumTimesteps);
#endif
  
  /* error checking */
  lfmap_uint64_t dataBytes=0;
  if(sizeof(RawVHeader)+sizeof(VariableRecord)*m_NumVariables>=m_Filesize)
  {
    fprintf(stderr,"MappedRawVFile::readHeader(): Error: Incorrect filesize.\n");
    return false;
  }
  if(m_NumVariables == 0)
  {
    fprintf(stderr,"MappedRawVFile::readHeader(): Error: Number of variables == 0.\n");
    return false;
  }

  /* make a copy of the variable records in the case that the pointer changes due to
     an lfmap remapping */
  var_records = (VariableRecord*)malloc(m_NumVariables*sizeof(VariableRecord));
  var_rec_ptr = lfmap_ptr(m_LFMappedVolumeFile,sizeof(RawVHeader),sizeof(VariableRecord)*m_NumVariables);
  if(var_rec_ptr == NULL)
    {
      fprintf(stderr,"MappedRawVFile::readHeader(): Error: Variable records truncated, not a RawV file.\n");
      free(var_records);
      return false;
    }
  memcpy(var_records,var_rec_ptr,m_NumVariables*sizeof(VariableRecord));

  for(i=0; i<m_NumVariables; i++)
  {
#ifdef DEBUG
    printf("MappedRawVFile::readHeader(): Checking variable record for variable '%d'.\n",i);
#endif
    /* check for null byte in variable name */
    for(j=0; j<64; j++)
      if(var_records[i].varName[j] == '\0') break;
    if(j==64)
      {
	fprintf(stderr,"MappedRawVFile::readHeader(): Error: Non null terminated variable name for variable '%d'\n",i);
	free(var_records);
	return false;
      }
	 
    if(var_records[i].varType > 5)
      {
	fprintf(stderr,"MappedRawVFile::readHeader(): Illegal variable type '%d'.\n",var_records[i].varType);
	free(var_records);
	return false;
      }
    dataBytes += m_XDim*m_YDim*m_ZDim*rawv_type_sizes[var_records[i].varType]*m_NumTimesteps;
#ifdef DEBUG
    printf("MappedRawVFile::readHeader(): Variable record for variable '%d' ('%s' of type '%s') correct.\n",
	   i,var_records[i].varName,rawv_type_strings[var_records[i].varType]);
#endif
  }
  if(sizeof(RawVHeader)+sizeof(VariableRecord)*m_NumVariables+dataBytes != m_Filesize)
    {
      fprintf(stderr,"MappedRawVFile::readHeader(): File size does not match header info.\n");
      free(var_records);
      return false;
    }
  
  m_Variables = new Variable*[m_NumVariables*m_NumTimesteps];
  for(i=0; i<m_NumVariables; i++)
    for(j=0; j<m_NumTimesteps; j++)
      {
	int index = i+m_NumVariables*j;
	lfmap_uint64_t single_length = m_XDim*m_YDim*m_ZDim*rawv_type_sizes[var_records[i].varType];
	lfmap_uint64_t var_start=0;
	for(unsigned int d=0; d<i; d++) /* count the number of bytes to the start of this variable */
	  var_start += m_XDim*m_YDim*m_ZDim*rawv_type_sizes[var_records[d].varType]*m_NumTimesteps;
#ifdef DEBUG
	printf("MappedRawVFile::readHeader(): Reading variable '%s' of type '%s' (timestep %d)\n",var_records[i].varName,rawv_type_strings[var_records[i].varType],j);
#endif
	m_Variables[index] = new Variable(this,(sizeof(RawVHeader)+sizeof(VariableRecord)*m_NumVariables)+var_start+single_length*j,var_records[i].varName,rawv_type_conv[var_records[i].varType],!big_endian());
	/* get the min/max value */
	if(rawv_type_conv[var_records[i].varType] == Variable::UCHAR || !m_CalcMinMax)
	  {
	    m_Variables[index]->m_Min = 0;
	    m_Variables[index]->m_Max = 255;
	  }
	else
	  {
	    lfmap_ptr_t slice = (lfmap_ptr_t)malloc(m_XDim*m_YDim*rawv_type_sizes[var_records[i].varType]);
#ifdef USING_QT
	    QProgressDialog progress(QString("Calculating variable min/max for variable %1, timestep %2").arg(i).arg(j),
				     "Abort",m_ZDim,NULL,"progress",true);
	    progress.show();
#endif

	    m_Variables[index]->m_Min = m_Variables[index]->m_Max = m_Variables[index]->get(0,0,0);
	    for(c=0; c<m_ZDim; c++)
	      {
		double val;

		m_Variables[index]->get(0,0,c,m_XDim,m_YDim,1,slice); /* get a slice at a time because it's much faster */

#define GETMIN(vartype)                                                                                      \
		{                                                                                            \
		  for(a=0; a<m_XDim; a++)                                                                    \
		    for(b=0; b<m_YDim; b++)                                                                  \
		      {                                                                                      \
			val = double(*((vartype *)(slice+(a+m_XDim*b)*sizeof(vartype))));                    \
			if(m_Variables[index]->m_Min > val)                                                  \
			  m_Variables[index]->m_Min = val;                                                   \
			else if(m_Variables[index]->m_Max < val)                                             \
			  m_Variables[index]->m_Max = val;                                                   \
		      }                                                                                      \
		}

		switch(rawv_type_conv[var_records[i].varType])
		  {
		  case Variable::UCHAR:  GETMIN(unsigned char);  break;
		  case Variable::USHORT: GETMIN(unsigned short); break;
		  case Variable::UINT:   GETMIN(unsigned int);   break;
		  case Variable::FLOAT:  GETMIN(float);          break;
		  case Variable::DOUBLE: GETMIN(double);         break;
		  }

#undef GETMIN

#ifdef DEBUG
		fprintf(stderr,"%5.2f %%\r",(((float)c)/((float)((int)(m_ZDim-1))))*100.0);
#endif

#ifdef USING_QT
		progress.setProgress(c);
		qApp->processEvents();
		if(progress.wasCanceled())
		  return false;
#endif
	      }
#ifdef DEBUG
	    printf("\n");
#endif

#ifdef USING_QT
	    progress.setProgress(m_ZDim);
#endif

	    free(slice);
	  }
#ifdef DEBUG
	printf("MappedRawVFile::readHeader(): Variable %d, Timestep %d: min: %f, max: %f\n",i,j,m_Variables[index]->m_Min,m_Variables[index]->m_Max);
#endif
      }

  free(var_records);
  return true;
}
Example #17
0
	template<typename T> inline void from_little_endian (T &t) {
		if (big_endian()) revert_endian(t);
	}
Example #18
0
int read_spinorfield_cm_single(spinor * const s, spinor * const r, char * filename, 
			       const int ts, const int vol) {
  /*
   * ts is the number of the timeslice to be used
   *    if ts < 0 read a volume source
   *
   * if ts >= 0 and vol > 0 the file is a volume file
   * but only one timeslice should be read
   */

  FILE * ifs;
  int t, x, y , z, i = 0;
  spinor * p = NULL;
  float tmp[24];

  ifs = fopen(filename, "r");
  if(ifs == (FILE *)NULL) {
    return(-1);
  }

  for(x = 0; x < LX; x++) {
    for(y = 0; y < LY; y++) {
      for(z = 0; z < LZ; z++) {
#if (defined TM_USE_MPI)
	fseek(ifs,
	      (g_proc_coords[0]*T+
	       (((g_proc_coords[1]*LX+x)*g_nproc_y*LY+g_proc_coords[2]*LY+y)*g_nproc_z*LZ
		+ g_proc_coords[3]*LZ+z)*T*g_nproc_t)*sizeof(spinor)/2,
	      SEEK_SET);
#endif
	for(t = 0; t < T; t++) {

	  i = g_lexic2eosub[ g_ipt[t][x][y][z] ];
	  if((t+x+y+z+
	      g_proc_coords[0]*T+g_proc_coords[1]*LX+
	      g_proc_coords[2]*LY+g_proc_coords[3]*LZ)%2==0) {
	    p = s;
	  }
	  else {
	    p = r;
	  }
	  
	  if(ts == t || ts < 0 || ts >= T){
	    /* Read the data */
	    fread(tmp, sizeof(spinor)/2, 1, ifs);

            /* Test if we read the data with the correct endian order */
            if(isnan(tmp[0]) || isnan(tmp[1]) || isnan(tmp[2]) || isnan(tmp[3]) || isnan(tmp[4]) || isnan(tmp[5]) ||
            isnan(tmp[6]) || isnan(tmp[7]) || isnan(tmp[8]) || isnan(tmp[9]) || isnan(tmp[10]) || isnan(tmp[11]) ||
            isnan(tmp[12]) || isnan(tmp[13]) || isnan(tmp[14]) || isnan(tmp[15]) || isnan(tmp[16]) || isnan(tmp[17]) ||
            isnan(tmp[18]) || isnan(tmp[19]) || isnan(tmp[20]) || isnan(tmp[21]) || isnan(tmp[22]) || isnan(tmp[23]))
            {
              if(g_proc_id == 0)
              {
                if(big_endian())
                  printf("\nBig endian order gives some NaN. Trying little endian order instead...\n\n");
                else
                  printf("\nLittle endian order gives some NaN. Trying big endian order instead...\n\n");
              }

              fclose(ifs);
              return read_spinorfield_cm_swap_single(s,r,filename,ts,vol);
            }
	    single2double_cm(p+i, tmp);
	  }
	  else {
	    if(vol > 0) {
	      fread(tmp, sizeof(spinor)/2, 1, ifs);
	    }
	    /* Padding with zeros */
	    zero_spinor(p+i);
	  }
	}
      }
    }
  }
  fclose(ifs);
  return(0);
}
Example #19
0
FILE *open_image_file (NV_CHAR *path)
{
  FILE                  *fp;
  NV_INT32              i;
  OLD_IMAGE_INDEX_T     old_record;


  NV_INT32 big_endian ();


  swap = (NV_BOOL) big_endian ();


  l_head.text.data_size = 0;


  /*  Brute force!  Load the index into memory, it ain't big anyway.  */

  if ((fp = fopen64 (path, "rb")) == NULL)
    {
      perror (path);
    }
  else
    {
      old = image_read_header (fp, &l_head);

      if (records) free (records);

      records = (IMAGE_INDEX_T *) calloc (l_head.text.number_images, sizeof (IMAGE_INDEX_T));

      if (records == NULL)
        {
          perror ("Allocating image index");
          exit (-1);
        }

      if (old)
        {
          for (i = 0 ; i < l_head.text.number_images ; i++)
            {
              fread (&old_record, sizeof (OLD_IMAGE_INDEX_T), 1, fp);
                
              if (swap)
                {
                  swap_NV_INT64 (&old_record.timestamp);
                  swap_NV_INT64 (&old_record.byte_offset);

                  swap_NV_INT32 (&old_record.image_size);
                  swap_NV_INT32 (&old_record.image_number);
                }
              records[i].timestamp = old_record.timestamp;
              records[i].byte_offset = old_record.byte_offset;
              records[i].image_size = old_record.image_size;
              records[i].image_number = old_record.image_number;

              l_head.text.data_size += records[i].image_size;
            }
        }
      else
        {
          for (i = 0 ; i < l_head.text.number_images ; i++)
            {
              fread (&records[i], sizeof (IMAGE_INDEX_T), 1, fp);

              if (swap)
                {
                  swap_NV_INT64 (&records[i].timestamp);
                  swap_NV_INT64 (&records[i].byte_offset);

                  swap_NV_INT32 (&records[i].image_size);
                  swap_NV_INT32 (&records[i].image_number);
                }

              l_head.text.data_size += records[i].image_size;
            }
        }
    }

  return (fp);
}
Example #20
0
// xshm and no xv
static int X11_xshm_blit(struct DriverInstance* sh,
                         const uint8_t* fb,
                         int width, int height,
                         struct blit_params* params,
                         int needs_adjust,
                         char* error_text, int text_len)
{
  // wait until last blit has completed
  if (sh->event_pending)
    {
      int max_wait = 15;
      XEvent event;
      sh->event_pending = 0;
      
      while (max_wait--)
        {
          XNextEvent(sh->display, &event);

          if (event.type == sh->completion_type)
            break;
        }
    }

  if (sh->ximage == 0)
    {
      snprintf(error_text, text_len, "Internal error: sh->ximage == 0");
      return 0;
    }
 
  // adjust and scale input framebuffer frb into shared mem
  if (needs_adjust)
    {
      ls_set_adjustment(sh->pal, params->brightness, params->contrast,
			params->gamma, params->invert);
                     
      ls_scale32m_adjust((uint32_t*)sh->ximage->data, sh->width, sh->height,
			 (const uint32_t*)fb, width, height, params->mirrorx,
			 params->mirrory, sh->pal);
    }
  else
    {
      ls_scale32m((uint32_t*)sh->ximage->data, sh->width, sh->height,
                  (const uint32_t*)fb, width, height, params->mirrorx,
                  params->mirrory);
    }

  //TODO: this is a hack for big-endian machines
  if (big_endian())
    convert_endianness(sh->ximage->data, sh->width, sh->height);

  if (sh->vis.depth == 16)
      convert_to_16_inplace(sh->ximage->data, sh->width, sh->height);

  // blit shared mem image
  XShmPutImage(sh->display, sh->win, sh->gc, sh->ximage,
               0, 0, 0, 0, sh->width, sh->height, True);
  sh->event_pending = 1;

  XFlush(sh->display);
  //XSync(sh->display, False);
  return 1;
}
Example #21
0
NV_INT32 main (NV_INT32 argc, NV_CHAR *argv[])
{
  NV_INT32       i, j, k, m, shift_lat, shift_lon, n, hit_land, hit_water,
                 pos, swap = 1, prev_value, first, ndx, total_bits, block;
  uLong          total_bytes;
  uLongf         out_bytes;
  FILE           *fp, *block_fp, *ofp;
  NV_INT16       row[1200][1201], delta[1200 * 1200], bias, start_val = 0, last_val = 0, diff, min_diff, max_diff, num_bits, null_val;
  NV_CHAR        string[512], ofile[512], dir[512], lathem, lonhem, header_block[HEADER_SIZE], block_file[512];
  NV_CHAR        dir_name[6][40] = {"Africa", "Australia", "Eurasia", "Islands", "North_America", "South_America"};
  NV_INT64       lpos;
  NV_U_BYTE      water[4], head[8], *in_buf, *out_buf, map[MAP_BYTES], byte, block_map[64800];
  time_t         t;
  struct tm      *cur_tm;


  printf ("\n\n%s\n\n", VERSION);


  if (argc < 2)
    {
      fprintf (stderr, "Usage: %s SRTM3_HGT_DIRECTORY_NAME\n\n", argv[0]);
      exit (-1);
    }


  /*  Open and read the block map file.  */

  if (getenv ("SRTM_DATA") == NULL)
    {
      fprintf (stderr, "\n\nYou must set the SRTM_DATA environment variable to point to the location of srtm3 directory\n\n");
      exit (-1);
    }

  strcpy (dir, getenv ("SRTM_DATA"));


  /*  Check the hgt directory name.  */

  block = 0;
  for (i = 0 ; i < 6 ; i++)
    {
      if (!strcmp (dir_name[i], argv[1]))
        {
          block = i + 1;
          break;
        }
    }

  if (!block)
    {
      fprintf (stderr, "SRTM3_HGT_DIRECTORY_NAME %s is incorrect.  Only the following are valid:\n\n", argv[1]);
      for (i = 0 ; i < 6 ; i++) fprintf (stderr, "\t%s\n", dir_name[i]);
      fprintf (stderr, "\n\n");
      exit (-1);
    }


  /*  Open and read the block map file.  */

  sprintf (block_file, "%s%1csrtm3%1csrtm3_block_map.dat", dir, SEPARATOR, SEPARATOR);

  if ((block_fp = fopen (block_file, "rb+")) == NULL)
    {
      if ((block_fp = fopen (block_file, "wb+")) == NULL)
        {
          perror (block_file);
          exit (-1);
        }


      /*  This must be the first time so initialize it.  */

      byte = 0;
      for (i = 0 ; i < 180 ; i++)
        {
          for (j = 0 ; j < 360 ; j++)
            {
              fwrite (&byte, 1, 1, block_fp);
            }
        }
    }

  fseek (block_fp, 0, SEEK_SET);
  fread (block_map, sizeof (block_map), 1, block_fp);


  /*  Are we on a big endian machine?  */

  if (big_endian ()) swap = 0;;


  /*  Set the water flag for the map area.  */

  srtm_bit_pack (water, 0, 32, 0);


  /*  Open the output file.  */

  sprintf (ofile, "%s%1csrtm3%1c%s.cte", dir, SEPARATOR, SEPARATOR, argv[1]);

  if ((ofp = fopen64 (ofile, "wb")) == NULL)
    {
      perror (ofile);
      exit (-1);
    }


  /*  Write the (minimalist) ASCII header.  */


  memset (header_block, 0, HEADER_SIZE);

  t = time (&t);
  cur_tm = gmtime (&t);

  sprintf (header_block, "[HEADER SIZE] = %d\n", HEADER_SIZE);
  sprintf (&header_block[strlen (header_block)], "[VERSION] = %s\n", VERSION);
  sprintf (&header_block[strlen (header_block)], "[ZLIB VERSION] = %s\n", zlibVersion ());
  sprintf (&header_block[strlen (header_block)], "[CREATION DATE] = %s", asctime (cur_tm));
  sprintf (&header_block[strlen (header_block)], "[END OF HEADER]\n");


  fwrite (header_block, HEADER_SIZE, 1, ofp);


  /*  Set the default for all map addresses to 2 (undefined).  Also, write the blank map area out to the
      file to save the space.  */

  for (i = 0 ; i < 180 ; i++)
    {
      for (j = 0 ; j < 360 ; j++)
        {
          srtm_double_bit_pack (map, (i * 360 + j) * 36, 36, 2);
        }
    }
  fseeko64 (ofp, (NV_INT64) HEADER_SIZE, SEEK_SET);
  fwrite (map, MAP_BYTES, 1, ofp);


  /*  Loop through -90 to 90 lat.  */

  for (i = -90 ; i < 90 ; i++)
    {
      lathem = 'N';
      if (i < 0) lathem = 'S';


      /*  Shift into the 0 to 180 world.  */

      shift_lat = i + 90;


      /*  Loop through the entire range of longitudes.  */

      for (j = -180 ; j < 180 ; j++)
        {
          lonhem = 'E';
          if (j < 0) lonhem = 'W';


          /*  Shift into the 0 to 360 world.  */

          shift_lon = j + 180;


          /*  Look for the file.  */

          sprintf (string, "%s%1c%1c%02d%1c%03d.hgt", argv[1], SEPARATOR, lathem, abs (i), lonhem, abs (j));


          /*  If we can open the file, read and process it.  */

          if ((fp = fopen (string, "rb")) != NULL)
            {
              prev_value = -1;
              pos = 0;


              /*  Note that we're only going to 1200 not 1201 because we don't need the redundant data.  */

              for (k = 0 ; k < 1200 ; k++)
                {
                  /*  Read one row (all 1201).  */

                  fread (row[k], 1201 * sizeof (NV_INT16), 1, fp);

                  for (m = 0 ; m < 1200 ; m++)
                    {
                      /*  If we're on a little endian system we need to swap the bytes.  */

                      if (swap)
                        {
#ifdef __GNUC__
                          swab (&row[k][m], &row[k][m], 2);
#else
                          _swab ((NV_CHAR *) &row[k][m], (NV_CHAR *) &row[k][m], 2);
#endif
                        }
                    }
                }


              first = 1;
              ndx = 0;
              min_diff = 32767;
              max_diff = -32768;
              hit_land = 0;
              hit_water = 0;


              /*  Loop through the cell.  */

              for (k = 0 ; k < 1200 ; k++)
                {

                  /*  Snake dance test.  */

                  if (!(k % 2))
                    {
                      /*  West to east.  */

                      for (m = 0 ; m < 1200 ; m++)
                        {
                          if (row[k][m] != -32768)
                            {
                              /*  Set the "hit" flags.  */

                              if (row[k][m])
                                {
                                  hit_land = 1;
                                }
                              else
                                {
                                  hit_water = 1;
                                }


                              /*  First time through set the last_val to start_val.  */

                              if (first)
                                {
                                  start_val = row[k][m];
                                  last_val = start_val;
                                  first = 0;
                                }
                              diff = row[k][m] - last_val;
                              if (diff > max_diff) max_diff = diff;
                              if (diff < min_diff) min_diff = diff;
                              last_val = row[k][m];
                              delta[ndx++] = diff;
                            }
                          else
                            {
                              /*  From what I've seen, -32768 means land but no defined elevation.  */

                              hit_land = 1;
                              delta[ndx++] = -32768;
                            }
                        }
                    }
                  else
                    {

                      /*  East to west.  */

                      for (m = 1199 ; m >= 0 ; m--)
                        {
                          if (row[k][m] != -32768)
                            {
                              /*  Set the "hit" flags.  */

                              if (row[k][m])
                                {
                                  hit_land = 1;
                                }
                              else
                                {
                                  hit_water = 1;
                                }


                              /*  First time through set the last_val to start_val.  */

                              if (first)
                                {
                                  start_val = row[k][m];
                                  last_val = start_val;
                                  first = 0;
                                }
                              diff = row[k][m] - last_val;
                              if (diff > max_diff) max_diff = diff;
                              if (diff < min_diff) min_diff = diff;
                              last_val = row[k][m];
                              delta[ndx++] = diff;
                            }
                          else
                            {
                              /*  From what I've seen, -32768 means land but no defined elevation.  */

                              hit_land = 1;
                              delta[ndx++] = -32768;
                            }
                        }
                    }
                }


              /*  All water cell.  */

              if (!hit_land && hit_water)
                {
                  srtm_double_bit_pack (map, (shift_lat * 360 + shift_lon) * 36, 36, 0);
                }


              /*  Land and water or all land cell.  */

              else
                {
                  /*  The bias is the negative of the minimum difference.  We bias so we don't have to play with
                      sign extension in the bit unpacking.  */

                  bias = -min_diff;


                  /*  Add two to the max difference to allow room for the null value.  */

                  max_diff += (bias + 2);


                  /*  Compute the number of bits needed to store a delta.  */

                  num_bits = NINT (log10 ((NV_FLOAT64) max_diff) / LOG2 + 0.5L);
                  null_val = NINT (pow (2.0L, (NV_FLOAT64) num_bits)) - 1;


                  /*  Compute the total bytes needed to store the block.  */

                  total_bits = 16 + 16 + 4 + 1200 * 1200 * num_bits;
                  total_bytes = total_bits / 8;
                  if (total_bits % 8) total_bytes++;


                  /*  Allocate the uncompressed memory block.  */

                  in_buf = (NV_U_BYTE *) calloc (total_bytes, 1);
                  if (in_buf == NULL)
                    {
                      perror ("Allocating in_buf");
                      exit (-1);
                    }


                  /*  Allocate the compressed memory block.  */

                  out_bytes = total_bytes + NINT ((NV_FLOAT32) total_bytes * 0.10) + 12;
                  out_buf = (NV_U_BYTE *) calloc (out_bytes, 1);
                  if (out_buf == NULL)
                    {
                      perror ("Allocating out_buf");
                      exit (-1);
                    }


                  /*  Pack the internal header.  */

                  pos = 0;
                  srtm_bit_pack (in_buf, pos, 16, start_val); pos += 16;
                  srtm_bit_pack (in_buf, pos, 16, bias); pos += 16;
                  srtm_bit_pack (in_buf, pos, 4, num_bits); pos += 4;


                  /*  Pack the deltas.  */

                  for (k = 0 ; k < 1200 * 1200 ; k++)
                    {
                      if (delta[k] == -32768)
                        {
                          delta[k] = null_val;
                        }
                      else
                        {
                          delta[k] += bias;
                        }
                      srtm_bit_pack (in_buf, pos, num_bits, delta[k]); pos += num_bits;
                    }


                  /*  Compress at maximum level.  */

                  n = compress2 (out_buf, &out_bytes, in_buf, total_bytes, 9);
                  if (n)
                    {
                      fprintf (stderr, "Error %d compressing record\n", n);
                      exit (-1);
                    }
                  free (in_buf);


                  /*  Pack the header.  */

                  pos = 0;
                  srtm_bit_pack (head, pos, 3, 1); pos += 3;
                  srtm_bit_pack (head, pos, 30, out_bytes); pos += 30;
                  srtm_bit_pack (head, pos, 31, total_bytes);


                  /*  Get the address where we're going to write the compressed block.  */

                  fseek (ofp, 0LL, SEEK_END);
                  lpos = ftell (ofp);


                  /*  Write the header to the file.  */

                  fwrite (head, 8, 1, ofp);


                  /*  Write the buffer to the file.  */

                  fwrite (out_buf, out_bytes, 1, ofp);
                  free (out_buf);


                  /*  Save the address of the block to the map.  */

                  srtm_double_bit_pack (map, (shift_lat * 360 + shift_lon) * 36, 36, lpos);
                }

              fclose (fp);


              /*  Set the block map value to the block number (1-6 for Africa, Australia, Eurasia, Islands, 
                  North_America, or South_America respectively).  */

              block_map[shift_lat * 360 + shift_lon] = block;


              fprintf (stderr, "block: %s   lon: %03d   lat: %03d       \r", argv[1], j, i);
              fflush (stderr);
            }
        }
    }


  /*  Write the map.  */

  fseek (ofp, (NV_INT64) HEADER_SIZE, SEEK_SET);
  fwrite (map, MAP_BYTES, 1, ofp);


  fclose (ofp);


  /*  Write the block map.  */

  fseek (block_fp, 0, SEEK_SET);
  fwrite (block_map, sizeof (block_map), 1, block_fp);

  fclose (block_fp);


  fprintf (stderr, "%s processing complete             \n\n", argv[1]);
  fflush (stderr);


  return (0);
}
Example #22
0
NV_INT32 image_read_header (FILE *fp, IMAGE_HEADER_T *head)
{
  NV_INT32    ret;
  NV_INT64    long_pos;
  NV_CHAR     varin[1024], info[1024];

  NV_CHAR *ngets (NV_CHAR *s, NV_INT32 size, FILE *stream);
  NV_INT32 big_endian ();


  swap = NVFalse;


  /*  Check for the new format file.  If the first four characters are 
      "File" it's the new format, otherwise read the binary header.  */

  fseeko64 (fp, 0LL, SEEK_SET);
  ngets (varin, sizeof (varin), fp);

  if (!strncmp (varin, "File", 4))
    {
      fseeko64 (fp, 0LL, SEEK_SET);


      /*  Read each entry.    */

      head->text.header_size = 0;
      while (ngets (varin, sizeof (varin), fp) != NULL)
        {
          if (!strcmp (varin, "EOF")) break;


          /*  Put everything to the right of the colon into 'info'.   */
            
          if (strchr (varin, ':') != NULL) strcpy (info, (NV_CHAR *) (strchr (varin, ':') + 1));


          /*  Check input for matching strings and load values if found.  */
            
          if (strstr (varin, "FileType:") != NULL) lidar_get_string (varin, head->text.file_type);

          if (strstr (varin, "EndianType:") != NULL)
            {
              if (strstr (info, "Little"))
                {
                  head->text.endian = NVFalse;
                  if (big_endian ()) swap = NVTrue;
                }
              else
                {
                  head->text.endian = NVTrue;
                  if (!big_endian ()) swap = NVTrue;
                }
            }


          if (strstr (varin, "SoftwareVersionNumber:") != NULL) sscanf (info, "%f", &head->text.software_version);

          if (strstr (varin, "FileVersionNumber:") != NULL) sscanf (info, "%f", &head->text.file_version);


          if (strstr (varin, "Downloaded By:") != NULL) lidar_get_string (varin, head->text.UserName);


          if (strstr (varin, "HeaderSize:") != NULL) sscanf (info, "%d", &head->text.header_size);

          if (strstr (varin, "TextBlockSize:") != NULL) sscanf (info, "%d", &head->text.text_block_size);

          if (strstr (varin, "BinaryBlockSize:") != NULL) sscanf (info, "%d", &head->text.bin_block_size);


          if (strstr (varin, "Project:") != NULL) lidar_get_string (varin, head->text.project);

          if (strstr (varin, "Mission:") != NULL) lidar_get_string (varin, head->text.mission);

          if (strstr (varin, "Dataset:") != NULL) lidar_get_string (varin, head->text.dataset);

          if (strstr (varin, "FlightlineNumber:") != NULL) lidar_get_string (varin, head->text.flightline_number);

          if (strstr (varin, "CodedFLNumber:") != NULL) sscanf (info, "%hd", &head->text.coded_fl_number);

          if (strstr (varin, "FlightDate:") != NULL) lidar_get_string (varin, head->text.flight_date);

          if (strstr (varin, "StartTime:") != NULL) lidar_get_string (varin, head->text.start_time);

          if (strstr (varin, "EndTime:") != NULL) lidar_get_string (varin, head->text.end_time);

          if (strstr (varin, "StartTimestamp:") != NULL) sscanf (info, NV_INT64_SPECIFIER, &head->text.start_timestamp);

          if (strstr (varin, "EndTimestamp:") != NULL) sscanf (info, NV_INT64_SPECIFIER, &head->text.end_timestamp);

          if (strstr (varin, "NumberImages:") != NULL) sscanf (info, "%d", &head->text.number_images);

          if (strstr (varin, "IndexRecordSize:") != NULL) sscanf (info, "%d", &head->text.record_size);

          if (strstr (varin, "IndexBlockSize:") != NULL) sscanf (info, "%d", &head->text.block_size);

          if (strstr (varin, "FileCreateDate:") != NULL) lidar_get_string (varin, head->text.dataset_create_date);

          if (strstr (varin, "FileCreateTime:") != NULL) lidar_get_string (varin, head->text.dataset_create_time);

          long_pos = ftello64 (fp);
          if (head->text.header_size && long_pos >= head->text.header_size) break;
        }


      /*  Make sure we're past the header.  */

      fseeko64 (fp, (NV_INT64) head->text.header_size, SEEK_SET);

      ret = 0;
    }
  else
    {
      /*  Early versions were screwed up.  */

      fseeko64 (fp, (NV_INT64) IMAGE_HEAD_SIZE, SEEK_SET);

      fread (&head->info, sizeof (IMAGE_INFO_T), 1, fp);


      /*  Swap the INFO block if needed.  */

      if (swap) swap_image_header (&head->info);


      head->text.start_timestamp = head->info.start_timestamp;
      head->text.end_timestamp = head->info.end_timestamp;
      head->text.number_images = head->info.number_images;

      ret = 1;
    }


  head->text.data_size = l_head.text.data_size;


  return (ret);
}
Example #23
0
static int X11_xv_blit(struct DriverInstance* sh,
                       const uint8_t* fb,
                       int width, int height,
                       struct blit_params* params,
                       int needs_adjust,
                       char* error_text, int text_len)
{
  if (sh->xv_image == 0 ||
      width  != sh->image_width ||
      height != sh->image_height)
    {
      if (sh->xv_image)
        XFree(sh->xv_image);

      sh->xv_image = XvCreateImage(sh->display,
                                   sh->xv_port,
                                   sh->xv_format_id,
                                   0,
                                   width, height);
      sh->image_width = width;
      sh->image_height = height;
    }

  if (sh->data == 0 || sh->data_size < sh->xv_image->data_size)
    {
      if (sh->data)
        free(sh->data);

      sh->data = malloc(sh->xv_image->data_size);
      sh->data_size = sh->xv_image->data_size;

      if (sh->data == 0)
	{
	  snprintf(error_text, text_len,
		   "Could not allocate data for XVImage");
	  return 0;
	}
    }

  assert(sh->data);

  //TODO: this is a hack for big-endian machines
  if (big_endian())
   convert_endianness(sh->data, sh->width, sh->height);

  sh->xv_image->data = sh->data;

  if (sh->vis.depth == 24)
    cvt_rgb32_to_i420((uint8_t*) sh->data,
                      (uint32_t*) fb, width, height,
                      sh->xv_image->pitches, sh->xv_image->offsets);
  else if (sh->vis.depth == 16)
    cvt_rgb16_to_i420((uint8_t*) sh->data,
                      (uint16_t*) fb, width, height,
                      sh->xv_image->pitches, sh->xv_image->offsets);

  // blit image
  XvPutImage(sh->display, sh->xv_port, sh->win, sh->gc, sh->xv_image,
             0, 0, width, height, 0, 0, sh->width, sh->height);

  XFlush(sh->display);
  //XSync(sh->display, False);
  return 1;
}
Example #24
0
static int X11_ximage_blit(struct DriverInstance* sh,
                           const uint8_t* fb,
                           int width, int height,
                           struct blit_params* params,
                           int needs_adjust,
                           char* error_text, int text_len)
{
  uint8_t* framebuffer = 0;

  if (width != sh->width || height != sh->height ||
      params->mirrorx || params->mirrory || needs_adjust)
    {
      int size = sh->width*sh->height*4;

      if (sh->data && sh->data_size < size)
	{
	  free(sh->data);
	  sh->data = 0;
	}

      if (sh->data == 0)
        {
          sh->data = malloc(size);
          sh->data_size = size;
        }

      if (sh->data == 0)
	{
	  snprintf(error_text, text_len,
		   "Could not allocate data for XImage");
	  return 0;
	}

      if (needs_adjust)
        {            
          ls_scale32m_adjust((uint32_t*) sh->data, sh->width, sh->height,
                             (const uint32_t*) fb, width, height,
			     params->mirrorx, params->mirrory, sh->pal);
        }
      else
        {
          ls_scale32m((uint32_t*) sh->data, sh->width, sh->height,
                      (const uint32_t*) fb, width, height,
		      params->mirrorx, params->mirrory);
        }

      framebuffer = sh->data;
    }
  else
    {
      if (sh->data != 0)
        {
          free(sh->data);
          sh->data = 0;
          sh->data_size = 0;
        }

      assert(width == sh->width);
      assert(height == sh->height);

      framebuffer = (uint8_t*) fb;
    }

  assert(framebuffer != 0);

  // now create an XImage using the framebuffers pixel data

  //TODO: this is a hack for big-endian machines
  if (big_endian())
   convert_endianness(framebuffer, sh->width, sh->height);

  if (sh->vis.depth == 16)
    convert_to_16_inplace(framebuffer, sh->width, sh->height);

  if (sh->ximage == 0 ||
      sh->image_width  != sh->width ||
      sh->image_height != sh->height)
    {
      if (sh->ximage)
	{
	  sh->ximage->data = 0;
	  XDestroyImage(sh->ximage);
	}

      sh->ximage = XCreateImage(sh->display,
                                sh->vis.visual,
                                sh->vis.depth,
                                ZPixmap,
                                0,
                                0,
                                sh->width,
                                sh->height,
                                32,
                                0);

      sh->image_width  = sh->width;
      sh->image_height = sh->height;
    }

  if (sh->ximage == 0)
    {
      snprintf(error_text, text_len, "Could not create XImage!");
      return 0;
    }

  sh->ximage->data = framebuffer;

  // ... blit it to screen
  XPutImage(sh->display, sh->win, sh->gc, sh->ximage, 0, 0, 0, 0,
            sh->width, sh->height);

  // and finally make sure the xserver performs the blitting
  XFlush(sh->display);
  //XSync(sh->display, 0);
  return 1;
}
Example #25
0
void write_vtk(FILE * ht, double complex wi[], const double t) {
	// Write the data in the file handler *ht
	int i,j,k;
	float q0;

	DEBUG_START_FUNC;

#ifdef MPI_SUPPORT
	double * chunk = NULL;
	if(rank==0) {
		chunk = (double *) malloc( NX * sizeof(double));
		if (chunk == NULL) ERROR_HANDLER( ERROR_CRITICAL, "No memory for chunk allocation");
	}
#endif

	for( i = 0 ; i < NTOTAL_COMPLEX ; i++) {
		w1[i] = wi[i];
	}
	
	gfft_c2r(w1);

	for( i = 0 ; i < 2 * NTOTAL_COMPLEX ; i++) {
		wr1[i] = wr1[i] / ((double) NTOTAL );
	}
	
#ifdef WITH_SHEAR
	remap_output(wr1,t);
#endif

#ifdef BOUNDARY_C
	for( k = 0 ; k < NZ / 2 ; k++) {
#else
	for( k = 0 ; k < NZ; k++) {
#endif
		for( j = 0; j < NY; j++) {
#ifdef MPI_SUPPORT
			// We have to transpose manually to be Fortran compliant
			for(i = 0; i < NX/NPROC; i++) {
				wr2[i] = wr1[k + j * (NZ + 2) + i * NY * (NZ + 2) ];   // Transfer the chunk of data to wr2
			}
			MPI_Gather(wr2, NX/NPROC, MPI_DOUBLE,
					   chunk, NX/NPROC, MPI_DOUBLE, 0, MPI_COMM_WORLD); // Put the full chunk in chunk in the root process
#endif			
			for( i = 0; i < NX; i++) {
#ifdef MPI_SUPPORT
				if(rank==0) {
					q0 = big_endian( (float) chunk[ i ] );
					fwrite(&q0, sizeof(float), 1, ht);
				}
#else
#ifdef WITH_2D
				q0 = big_endian( (float) wr1[j + i * (NY + 2)] );
#else
				q0 = big_endian( (float) wr1[k + j * (NZ + 2) + i * NY * (NZ + 2) ] );
#endif
				fwrite(&q0, sizeof(float), 1, ht);
				if(ferror(ht)) ERROR_HANDLER( ERROR_CRITICAL, "Error writing VTK file");
#endif
			}
#ifdef MPI_SUPPORT
			MPI_Barrier(MPI_COMM_WORLD);
#endif
		}
	}

#ifdef MPI_SUPPORT	
	if(rank==0) free(chunk);
#endif

	DEBUG_END_FUNC;
	
	return;
}

// Geo's personnal VTK writer, using structured data points	
/***********************************************************/
/** 
	Output a legacy VTK file readable by Paraview. This routine
	will output all the variables in files data/v****.vtk.
	
	@param n Number of the file in which the output will done.
	@param t Current time of the simulation.
*/
/***********************************************************/

void output_vtk(struct Field fldi, const int n, double t) {
	FILE *ht = NULL;
	char  filename[50];
	int num_remain_field;
	int array_size, i;
	
	DEBUG_START_FUNC;

	sprintf(filename,"data/v%04i.vtk",n);
#ifdef BOUNDARY_C
	array_size=NX*NY*NZ/2;	// Remove half of the vertical direction for symmetry reason when using walls in z
#else
	array_size=NX*NY*NZ;
#endif

	if(rank==0) {
		ht=fopen(filename,"w");
	
		fprintf(ht, "# vtk DataFile Version 2.0\n");
		fprintf(ht, "t= %015.15e Snoopy Code v5.0\n",t);
		fprintf(ht, "BINARY\n");
		fprintf(ht, "DATASET STRUCTURED_POINTS\n");
#ifdef BOUNDARY_C
		fprintf(ht, "DIMENSIONS %d %d %d\n", NX, NY, NZ / 2);
#else
		fprintf(ht, "DIMENSIONS %d %d %d\n", NX, NY, NZ);
#endif
		fprintf(ht, "ORIGIN %g %g %g\n", -param.lx/2.0, -param.ly/2.0, -param.lz/2.0);
		fprintf(ht, "SPACING %g %g %g\n", param.lx/NX, param.ly/NY, param.lz/NZ);
	
		// Write the primary scalar (f***ing VTK legacy format...)
		fprintf(ht, "POINT_DATA %d\n",array_size);
		fprintf(ht, "SCALARS %s float\n",fldi.fname[0]);
		fprintf(ht, "LOOKUP_TABLE default\n");
	}
	write_vtk(ht,fldi.farray[0],t);
	
	num_remain_field = fldi.nfield - 1;		// we have already written the first one
		
	if(param.output_vorticity)
		num_remain_field +=3;
		
#ifndef MPI_SUPPORT
#ifdef WITH_PARTICLES
	num_remain_field++;
#endif
#endif
		
	if(rank==0) fprintf(ht, "FIELD FieldData %d\n",num_remain_field);
	
	// Write all the remaining fields
	
	for(i = 1 ; i < fldi.nfield ; i++) {
		if(rank==0) fprintf(ht, "%s 1 %d float\n",fldi.fname[i],array_size);
		write_vtk(ht,fldi.farray[i],t);
	}
	
	if(param.output_vorticity) {
		// Compute the vorticity field
		for( i = 0 ; i < NTOTAL_COMPLEX ; i++) {
			w4[i] = I * (ky[i] * fldi.vz[i] - kz[i] * fldi.vy[i]);
			w5[i] = I * (kz[i] * fldi.vx[i] - kxt[i] * fldi.vz[i]);
			w6[i] = I * (kxt[i] * fldi.vy[i] - ky[i] * fldi.vx[i]);
		}
		if(rank==0) fprintf(ht, "wx 1 %d float\n",array_size);
		write_vtk(ht,w4,t);
		if(rank==0) fprintf(ht, "wy 1 %d float\n",array_size);
		write_vtk(ht,w5,t);
		if(rank==0) fprintf(ht, "wz 1 %d float\n",array_size);
		write_vtk(ht,w6,t);
	}
		
#ifndef MPI_SUPPORT
#ifdef WITH_PARTICLES
	if(rank==0) fprintf(ht, "particules 1 %d float\n",array_size);
	write_vtk_particles(fldi, ht, t);
#endif
#endif
		
	if(rank==0) {
		if(ferror(ht)) ERROR_HANDLER( ERROR_CRITICAL, "Error writing VTK file");
		fclose(ht);
	}
	
	DEBUG_END_FUNC;
	
	return;
	
}
Example #26
0
void read_data(int *xd, int *yd, int *zd, float **data, 
			   /*float *span_t, float *orig_t,*/ const char *input_name)
{
	float c_float;
	unsigned char c_unchar;
	unsigned short c_unshort;
	int i,j,k;
	float *dataset;
	
	struct stat filestat;
	size_t size[3];
	int datatype = 0;
	int found;
	FILE *fp;
	
	size_t fread_return=0;
	
	
	
	if ((fp=fopen(input_name, "rb"))==NULL){
		printf("read error...\n");
		exit(0);
	}
	stat(input_name, &filestat);
	
	/* reading RAWIV header */
	fread_return = fread(minext, sizeof(float), 3, fp);
	fread_return = fread(maxext, sizeof(float), 3, fp);
	fread_return = fread(&nverts, sizeof(int), 1, fp);
	fread_return = fread(&ncells, sizeof(int), 1, fp);
	//#ifdef _LITTLE_ENDIAN
	if(!big_endian())
	  {
	    swap_buffer((char *)minext, 3, sizeof(float));
	    swap_buffer((char *)maxext, 3, sizeof(float));
	    swap_buffer((char *)&nverts, 1, sizeof(int));
	    swap_buffer((char *)&ncells, 1, sizeof(int));
	  }
	//#endif  
	
	size[0] = 12 * sizeof(float) + 2 * sizeof(int) + 3 * sizeof(unsigned int) +
		nverts * sizeof(unsigned char);
	size[1] = 12 * sizeof(float) + 2 * sizeof(int) + 3 * sizeof(unsigned int) +
		nverts * sizeof(unsigned short);
	size[2] = 12 * sizeof(float) + 2 * sizeof(int) + 3 * sizeof(unsigned int) +
		nverts * sizeof(float);
	
	found = 0;
	for (i = 0; i < 3; i++)
		if (size[i] == (unsigned int)filestat.st_size)
		{
			if (found == 0)
			{
				datatype = i;
				found = 1;
			}
		}
			if (found == 0)
			{
				printf("Corrupted file or unsupported dataset type\n");
				exit(5);
			}
			
			
			fread_return = fread(dim, sizeof(unsigned int), 3, fp);
	fread_return = fread(orig, sizeof(float), 3, fp);
	fread_return = fread(span, sizeof(float), 3, fp);
	//#ifdef _LITTLE_ENDIAN
	if(!big_endian())
	  {
	    swap_buffer((char *)dim, 3, sizeof(unsigned int));
	    swap_buffer((char *)orig, 3, sizeof(float));
	    swap_buffer((char *)span, 3, sizeof(float));
	  }
	//#endif 
	/*
	 span_t[0] = span[0];
	 span_t[1] = span[1];
	 span_t[2] = span[2];
	 orig_t[0] = orig[0];
	 orig_t[1] = orig[1];
	 orig_t[2] = orig[2];
	 */
	XDIM = dim[0];
	YDIM = dim[1];
	ZDIM = dim[2];
	dataset = (float *)malloc(sizeof(float)*XDIM*YDIM*ZDIM);
	
	maxraw = -99999999.f;
	minraw = 99999999.f;
	
	if (datatype == 0) {
		printf("data type: unsigned char \n");
		for (i=0; i<ZDIM; i++)
			for (j=0; j<YDIM; j++)
				for (k=0; k<XDIM; k++) {
					fread_return = fread(&c_unchar, sizeof(unsigned char), 1, fp);
					dataset[IndexVect(k,j,i)]=(float)c_unchar;
					
					if (c_unchar > maxraw)
						maxraw = c_unchar;
					if (c_unchar < minraw)
						minraw = c_unchar;
				}
	}
		else if (datatype == 1) {
			printf("data type: unsigned short \n");
			for (i=0; i<ZDIM; i++)
				for (j=0; j<YDIM; j++)
					for (k=0; k<XDIM; k++) {
						fread_return = fread(&c_unshort, sizeof(unsigned short), 1, fp);
						//#ifdef _LITTLE_ENDIAN
						if(!big_endian())
						  swap_buffer((char *)&c_unshort, 1, sizeof(unsigned short));
						//#endif 
						dataset[IndexVect(k,j,i)]=(float)c_unshort;
						
						if (c_unshort > maxraw)
							maxraw = c_unshort;
						if (c_unshort < minraw)
							minraw = c_unshort;
					}
		}
			else if (datatype == 2) {
				printf("data type: float \n");
				for (i=0; i<ZDIM; i++) 
					for (j=0; j<YDIM; j++)
						for (k=0; k<XDIM; k++) {
							fread_return = fread(&c_float, sizeof(float), 1, fp);
							//#ifdef _LITTLE_ENDIAN
							if(!big_endian())
							  swap_buffer((char *)&c_float, 1, sizeof(float));
							//#endif 
							dataset[IndexVect(k,j,i)]=c_float;
							
							if (c_float > maxraw)
								maxraw = c_float;
							if (c_float < minraw)
								minraw = c_float;
						}
			}
				
				else {
					printf("error\n");
					fclose(fp);
					exit(1);
				}
				
				fclose(fp);
				
				for (i=0; i<ZDIM; i++) 
					for (j=0; j<YDIM; j++)
						for (k=0; k<XDIM; k++)
							dataset[IndexVect(k,j,i)] = 255*(dataset[IndexVect(k,j,i)] - 
															 minraw)/(maxraw-minraw); 
				
				printf("minimum = %f,   maximum = %f \n",minraw,maxraw);
				
				
				*xd = XDIM;
				*yd = YDIM;
				*zd = ZDIM;
				*data = dataset;
				
				loadedVolumeInfo.read(input_name);
}
Example #27
0
void wapp2fb(FILE *input, FILE *output) /* includefile */
{
  FILE *bptr, *fpou, *alfa[2];
  int pixel[2];
  double pra, pdec;
  double bw, bandwidth, scale, power, hweight, tsamp_us, crate, lmst;
  double *lag, *sum, *acf, *window, jan1, days, epoch, ras,des,rahr,dede; 
  float zerolag,*block,smin,smax;
  int doit,i,j,k,two_nlags,nlags,stat,rec_size,idump,swap_bytes,ifnum,opened;
  int filesize,headersize,beam,utsecs,iymdf[4],rah,ram,ded,dem;
  unsigned char *cblock, zuc;
  unsigned short *sblock, zus; 
  unsigned int zul;
  char message[80], outfile[80];
  void *dump;
  static float realtime=0.0;

#ifdef FFTW
  fftw_plan fftplan;
#endif
  /* establish whether we need to swap bytes (WAPP is little endian) */
  swap_bytes=big_endian();

  /* initialise correlator parameters used below */
  nlags=nchans;  
  two_nlags=2*nlags;  
  bandwidth=foff*nlags;
  tsamp_us=tsamp*1.0e6;
  if (bandwidth<0.0) bandwidth *= -1.0;

#ifdef FFTW
  acf = fftw_malloc(sizeof(double) * two_nlags);
  lag = fftw_malloc(sizeof(double) * two_nlags);
  /* set up fftw table and acf array when computing power spectra */
  fftplan=fftw_plan_r2r_1d(two_nlags,acf,lag,FFTW_R2HC,FFTW_PATIENT);
#endif
#ifndef FFTW
  /* set up acf array when computing power spectra */
  acf = (double *) malloc(two_nlags * sizeof(double));
  lag = (double *) malloc(two_nlags * sizeof(double));
#endif

  if (compute_spectra) {
    /* ranges for scaling spectra */
    smin=0.0;smax=3.0;
  } else {
    /* ranges for scaling correlation functions */
    smin=-0.5;smax=1.0;
  }

  /* set up the weights for windowing of ACF to monimize FFT leakage */
  if (hanning) {
    /* Hanning window */
    hweight=0.50;
  } else if (hamming) {
    /* Hamming window */
    hweight=0.54;
  } else {
    /* no window (default) */
    hweight=1.00;
  }

  /* define the smoothing window to be applied base on the above weight */
  window = (double *) malloc(nlags * sizeof(double));
  for (j=0; j<nlags; j++) window[j]=(hweight+(1.0-hweight)*cos(PI*j/nlags));

  /* work out number of IFs to loop over */
  if (sumifs && (nifs>1)) {
    smin*=2.0;
    smax*=2.0;
    ifnum=2;
  } else {
    sumifs=0;
    ifnum=nifs;
  }

  /* calculate required record size for reading - i.e. number of bytes/dump */
  rec_size = nifs*nlags*(nbits/8);
  dump = malloc(rec_size); /* pointer to the correlator dump */

  /* correlator data rate */
  crate = 1.0/(tsamp_us-WAPP_DEAD_TIME); 
  /* scale factor to normalize correlation functions */
  if (bandwidth < 50.0) 
    bw=50.0; /* correct scaling for narrow-band use */
  else
    bw=bandwidth;

  scale = crate/bw;
  if (wapp_level==9) scale/=16.0; /* 9-level sampling */
  if (wapp_sum) scale/=2.0;  /* summed IFs (search mode) */
  scale*=pow(2.0,(double)wapp_lagtrunc); /* needed for truncation modes */

  /* now define a number of working arrays to store lags and spectra */
  block = (float *) malloc(nlags * sizeof(float));
  cblock = (unsigned char *) malloc(nlags * sizeof(unsigned char));
  sblock = (unsigned short *) malloc(nlags * sizeof(unsigned short));

  /* if the file is ALFA data --- do the demultiplexing to two files */
  if (wapp_isalfa) {

    angle_split(src_raj,&rah,&ram,&ras);
    rahr=(double)rah+(double)ram/60.0+(double)ras/3600.0;
    angle_split(src_dej,&ded,&dem,&des);
    if (ded>0)
      dede=(double)ded+(double)dem/60.0+(double)des/3600.0;
    else
      dede=(double)ded-(double)dem/60.0-(double)des/3600.0;
    /* calculate local sidereal time in hours */
    lmst=slaGmst(tstart)*12.0/4.0/atan(1.0)-4.4502051459439667;
    if (lmst<0.0) lmst+=24.0;
    slaDjcal(5,tstart,iymdf,&stat);
    slaCaldj(iymdf[0],1,1,&jan1,&stat);
    days=tstart-jan1+1.0;
    epoch=(double)iymdf[0]+days/365.25;
    utsecs=86400*(tstart-floor(tstart));

    pixel[0]=(wapp_number-1)*2;
    pixel[1]=pixel[0]+1;
    puts("opening output files for demultiplexed ALFA data...");
    for (i=0; i<2; i++) {
      if (alfa_raj[pixel[i]] == 0.0) {
      alfa_position(rahr,dede,lmst,epoch,alfa_ang,0.0,0.0,pixel[i],&pra,&pdec);
      src_raj=h2hms(pra);
      src_dej=deg2dms(pdec);
      } else {
      src_raj=h2hms(alfa_raj[pixel[i]]);
      src_dej=deg2dms(alfa_dej[pixel[i]]);
      }
      sprintf(outfile,"%s_%.0f_%05d_%04d_%s_%d.fil",
	    project,floor(tstart),utsecs,
	    scan_number,source_name,pixel[i]);
      alfa[i]=open_file(outfile,"wb");
      puts(outfile);
      filterbank_header(alfa[i]);
    }
    beam=0;
  }

  if (headerfile) {
    /* write output ASCII header file */
    fpou=open_file("head","w");  
    fprintf(fpou,"Original WAPP file: %s\n",inpfile);
    fprintf(fpou,"Sample time (us): %f\n",tsamp_us);
    fprintf(fpou,"Observation time (s): %f\n",wapp_obstime);
    fprintf(fpou,"Time stamp (MJD): %18.12f\n",tstart);
    fprintf(fpou,"Number of samples/record: %d\n",512);
    fprintf(fpou,"Center freq (MHz): %f\n",fch1+(float)nlags*foff/2.0);
    fprintf(fpou,"Channel band (kHz): %f\n",bandwidth*1000.0/nlags);
    fprintf(fpou,"Number of channels/record: %d\n",nlags);
    fprintf(fpou,"Nifs: %d\n",ifnum);
    fprintf(fpou,"RA (J2000): %f\n",src_raj);
    fprintf(fpou,"DEC (J2000):  %f\n",src_dej);
    fprintf(fpou,"Gal l: %.4f\n",srcl);
    fprintf(fpou,"Gal b: %.4f\n",srcb); 
    fprintf(fpou,"Name: %s\n",source_name);
    fprintf(fpou,"Lagformat: %d\n",wapp_lagformat);
    fprintf(fpou,"Sum: %d\n",wapp_sum);
    fprintf(fpou,"Level: %d\n",wapp_level);
    fprintf(fpou,"AZ at start: %f\n",az_start);
    fprintf(fpou,"ZA at start: %f\n",za_start);
    fprintf(fpou,"AST at start: %f\n",ast0);
    fprintf(fpou,"LST at start: %f\n",lst0);
    fprintf(fpou,"Project ID: %s\n",project);
    fprintf(fpou,"Observers: %s\n",culprits);
    filesize=sizeof_file(inpfile);
    fprintf(fpou,"File size (bytes): %d\n",filesize);
    headersize=wapp_header_size+wapp_incfile_length;
    fprintf(fpou,"Data size (bytes): %d\n",filesize-headersize);
    fprintf(fpou,"Number of samples: %d\n",nsamples(inpfile,headersize,nbits,nifs,nchans));
    fclose(fpou);
  }

  /* initialise various counters and flags */
  opened=idump=i=j=0; 

  /* main loop reading data from infile until no more left to read */
  while( (stat=read(wapp_file,dump,rec_size)) == rec_size) {

    /* calculate elapsed time and determine whether we process this record */
    realtime += (float) tsamp;
    if ( (doit=process(realtime,start_time,final_time)) == -1) break;

    if (doit) {

      /* set ALFA beam output if necessary */
      if (wapp_isalfa) {
	output=alfa[beam];       /* set output file for this loop */
	beam=!(beam);            /* flip file for next iteration */
      }

      /* clear zerolag and blocksum arrays */
      zerolag=0.0;
      for (j=0; j<nlags; j++) block[j]=0.0;

      /* loop over the IFs */
      for (i=0; i<ifnum; i++) {
	if (ifstream[i]=='Y') {
	  if (zerolagdump) {
	    /* only interested in the zero lag term for each IF */
	    switch (nbits) {
	    case 8:
	      zuc = *(((unsigned char *)dump)+i*nlags);
	      zerolag+=zuc;
	      break;
	    case 16:
	      zus = *(((unsigned short *)dump)+i*nlags);
	      if (swap_bytes) swap_short(&zus); 
	      zerolag+=zus;
	      break;
	    case 32:
	      zul = *(((unsigned int *)dump)+i*nlags);
	      if (swap_bytes) swap_int(&zul); 
	      zerolag+=zul;
	      break;
	    }
	    /* write out the data checking IF number for summed mode */
	    if ( (sumifs && (i==1)) || (!sumifs) ) {
	      if (obits==32) {
		if (swapout) swap_float(&zerolag);
		fwrite(&zerolag,sizeof(float),1,output);
	      } else {
		sprintf(message,"cannot write %d bits in zerolag mode",obits);
		error_message(message);
	      }
	    }
	  } else {
	    /* fill lag array with scaled CFs */
	    for (j=0; j<nlags; j++) {
	      switch (nbits) {
	      case 8:
		zuc = *(((unsigned char *)dump)+j+i*nlags);
		lag[j] = scale * (double) zuc - 1.0;
		break;
	      case 16:
		zus = *(((unsigned short *)dump)+j+i*nlags);
		if (swap_bytes) swap_short(&zus);
		lag[j] = scale * (double) zus - 1.0;
		break;
	      case 32:
		zul = *(((unsigned int  *)dump)+j+i*nlags);
		if (swap_bytes) swap_int(&zul);
		lag[j] = scale * (double) zul - 1.0;
		break;
	      }
	    }
	    /* calculate power and correct for finite level quantization */
	    power = inv_cerf(lag[0]);
	    power = 0.1872721836/power/power;
	    if (i<2) {
	      if (do_vanvleck) { 
		if (wapp_level==3) {
		  /* apply standard 3-level van vleck correction */
		  vanvleck3lev(lag,nlags);
		} else if (wapp_level==9) {
		  /* apply 9-level van vleck correction */
		  vanvleck9lev(lag,nlags);
		}
	      }
	    }

	    if (compute_spectra) {
	      /* form windowed even ACF in array */
	      for(j=1; j<nlags; j++) {
		acf[j]=window[j]*lag[j]*power;
		acf[two_nlags-j]=acf[j];
	      }   
	      acf[nlags]=0.0;
	      acf[0]=lag[0]*power; 
	      /* FFT the ACF (which is real and even) -> real and even FFT */
#ifdef FFTW
	      fftw_execute(fftplan);
#endif
#ifndef FFTW
	      rfft(two_nlags,acf,lag);
#endif
	      /* if the band needs to be flipped --- do it here */
	      if (wapp_flip) {
		/* use acf as temporary array */
		for (j=0;j<nlags;j++) acf[j]=lag[j]; 
		k=nlags-1;
		for (j=0;j<nlags;j++) {
		  lag[k]=acf[j];
		  k--;
		}
	      }
  	      /* add lags to block array */
	      for (j=0; j<nlags; j++) block[j]+=lag[j];
	    } else {			
	      /* just copy correlation functions into block */
	      for (j=0; j<nlags; j++) block[j]=lag[j];	
	    }
	    /* write out data block checking IF number for summed mode */
	    if ( (sumifs && (i==1)) || (!sumifs) ) {
	      if (obits==32) {
		if (swapout) for (j=0; j<nlags; j++) swap_float(&block[j]);
		fwrite(block,sizeof(float),nlags,output);
	      } else if (obits==16) {
		float2short(block,nlags,smin,smax,sblock);
		if (swapout) for (j=0; j<nlags; j++) swap_short(&sblock[j]);
		fwrite(sblock,sizeof(unsigned short),nlags,output);
	      } else if (obits==8) {
		float2char(block,nlags,smin,smax,cblock);
		fwrite(cblock,sizeof(unsigned char),nlags,output);
	      } else if (obits==4) {
		float2four(block,nlags,smin,smax,cblock);
		fwrite(cblock,sizeof(unsigned char),nlags/2,output);
	      } else {
		sprintf(message,"cannot write %d bits in wapp2fb",obits);
		error_message(message);
	      }
	    }
	  } /* end of zerolagdump if */

	  if (!sumifs) { /* reset block and zerolag if not summing */
	    zerolag=0.0;
	    for (j=0; j<nlags; j++) block[j]=0.0;
	  }
	}          /* end of IFstream if */
      }            /* end of loop over IFs */
    }              /* end of processing if */

    /* increment dump counter and update logfile every 512 dumps */
    idump++;
    if (idump%512 == 0) {
      if (!opened) {
	/* open up logfile */
	open_log("filterbank.monitor");
	opened=1;
      }
      sprintf(message,"time:%.1fs",realtime);
      update_log(message);
    }
  }                /* end of main read loop*/

    
  /* job done - free up remaining arrays */
  free(dump);free(block);free(sblock);free(cblock);free(window);
#ifdef FFTW
  fftw_destroy_plan(fftplan);
  fftw_free(acf); fftw_free(lag);
#endif
#ifndef FFTW
  free(acf); free(lag);
#endif
}
Example #28
0
FILE *open_pos_file (NV_CHAR *path)
{
  FILE                   *fp;
  NV_INT32               i;
  POS_OUTPUT_T           pos;
  time_t                 tv_sec;
  NV_INT32               tv_nsec;
  struct tm              tm;
  static NV_INT32        tz_set = 0;


  NV_INT32 big_endian ();


  /*  Check the file name for following the naming convention as best we can.  */

  if (path[strlen (path) - 16] != '_' || path[strlen (path) - 9] != '_' || path[strlen (path) - 4] != '.')
    {
      fprintf (stderr, "\n\n\nFilename %s does not conform to standard\n", path);
      fprintf (stderr, "Please correct to match _YYMMDD_NNNN.pos or _YYMMDD_NNNN.out standard.\n\n\n");
      fflush (stderr);
      return (NULL);
    }


  sscanf (&path[strlen (path) - 15], "%02d%02d%02d", &year, &month, &day);


  /*  tm struct wants years since 1900!!!  */

  tm.tm_year = year + 100;
  tm.tm_mon = month - 1;
  tm.tm_mday = day;
  tm.tm_hour = 0.0;
  tm.tm_min = 0.0;
  tm.tm_sec = 0.0;
  tm.tm_isdst = -1;

  if (!tz_set)
    {
#ifdef NVWIN3X
  #ifdef __MINGW64__
      putenv("TZ=GMT");
      tzset();
  #else
      _putenv("TZ=GMT");
      _tzset();
  #endif
#else
      putenv("TZ=GMT");
      tzset();
#endif
      tz_set = 1;
    }


  /*  Get seconds from the epoch (01-01-1970) for the date in the filename. 
      This will also give us the day of the week for the GPS seconds of
      week calculation.  */

  tv_sec = mktime (&tm);
  tv_nsec = 0.0;


  /*  Subtract the number of days since Saturday midnight (Sunday morning) in seconds.  */

  tv_sec = tv_sec - (tm.tm_wday * 86400);
  start_week = tv_sec;


  /*  We have to assume that the file is little endian since there is no
      header and no field that we can use to deduce what it is.  */

  swap = (NV_BOOL) big_endian ();


  if ((fp = fopen (path, "rb")) == NULL)
    {
      return ((FILE *) NULL);
    }
  else
    {
      fread (&pos, sizeof (POS_OUTPUT_T), 1, fp);
      if (swap) swap_pos (&pos);
      start_timestamp = (NV_INT64) (((NV_FLOAT64) start_week + pos.gps_time) * 1000000.0);
      start_record = 0;
      start_gps_time = pos.gps_time;


      i = fseek (fp, -sizeof (POS_OUTPUT_T), SEEK_END);

      fread (&pos, sizeof (POS_OUTPUT_T), 1, fp);
      if (swap) swap_pos (&pos);
      end_timestamp = (NV_INT64) (((NV_FLOAT64) start_week + pos.gps_time) * 1000000.0);


      /*  Check for crossing midnight at end of GPS week (stupid f***ing Applanix bozos).  */

      if (end_timestamp < start_timestamp)
        {
          midnight = NVTrue;
          end_timestamp += ((NV_INT64) WEEK_OFFSET * 1000000);
        }


      end_record = ftell (fp) / sizeof (POS_OUTPUT_T);

      fseek (fp, 0, SEEK_SET);
    }

  return (fp);
}