Exemple #1
0
/* pipe_array(): read a pipe-format data file into a datum array.
 * @D: pointer to the destination datum structure.
 */
int pipe_array (datum *D) {
  /* declare a few required variables:
   * @endian: byte ordering of the input file.
   * @i: dimension loop counter.
   * @n_words: number of data words in the file.
   * @n_expected: expected file size.
   * @n_actual: actual file size.
   * @offset: header byte offset.
   * @hdr: pipe header structure.
   * @fh: input file handle.
   */
  enum byteorder endian = BYTES_ENDIAN_AUTO;
  unsigned int d, n_words, n_expected, n_actual, offset;
  struct pipe_header hdr;
  FILE *fh;

  /* check that the input filename is valid. */
  if (D->fname == NULL)
    throw("invalid input filename");

  /* read the header information from the data file. */
  if (!pipe_read_header(D->fname, &endian, &hdr))
    throw("failed to read header of '%s'", D->fname);

  /* compute the byte offset from which to begin reading point data. */
  offset = sizeof(struct pipe_header);

  /* compute the actual number of bytes in the file. */
  n_actual = bytes_size(D->fname) - offset;
  n_words = n_actual / sizeof(float);

  /* calculate the expected number of bytes in the file. */
  for (d = 0, n_expected = sizeof(float); d < D->nd; d++)
    n_expected *= (D->dims[d].cx ? 2 : 1) * D->dims[d].sz;

  /* check that the byte counts match. */
  if (n_expected != n_actual)
    throw("data size mismatch (expected %u, read %u)", n_expected, n_actual);

  /* open the input file for reading. */
  fh = fopen(D->fname, "rb");

  /* check that the file was opened. */
  if (!fh)
    throw("failed to open '%s'", D->fname);

  /* read data from the file into the output array. */
  if (!hx_array_fread_raw(fh, &D->array, endian, sizeof(float), 1,
                          offset, 0, 1, n_words, 0))
    throw("failed to read raw data from '%s'", D->fname);

  /* close the input file. */
  fclose(fh);

  /* interlace the complex direct-dimension traces, if necessary. */
  if (D->dims[0].cx && !pipe_interlace(&D->array, D->dims[0].sz))
    throw("failed to interlace complex traces");

  /* return success. */
  return 1;
}
Exemple #2
0
/* rnmrtk_array(): read an rnmrtk data file into a datum array.
 * @D: pointer to the source datum structure.
 */
int rnmrtk_array (datum *D) {
  /* declare a structure to hold parameter information. */
  struct rnmrtk_parms par;

  /* declare variables required for array raw input:
   * @wordsz: number of bytes per data word.
   * @isflt: whether the words are floats.
   * @ntrue: number of bytes in the input file.
   * @ncalc: estimated value of @ntrue.
   * @offhead: file header byte offset.
   * @offblk: block header byte offset.
   * @offend: block footer byte offset.
   * @nblks: number of data blocks.
   * @nwords: number of words per block.
   * @nalign: block alignment byte count.
   * @fh: input file handle.
   */
  unsigned int wordsz;
  unsigned int isflt;
  unsigned int ntrue;
  unsigned int ncalc;
  unsigned int offhead;
  unsigned int offblk;
  unsigned int offend;
  unsigned int nblks;
  unsigned int nwords;
  unsigned int nalign;
  FILE *fh;

  /* check that the input filename is valid. */
  if (D->fname == NULL)
    throw("invalid input filename");

  /* read the parameter file. */
  if (!rnmrtk_read_parms(D->fname, &par))
    throw("failed to read rnmrtk parfile for '%s'", D->fname);

  /* check the word type. */
  if (par.isflt) {
    /* set the float flag and the word size. */
    wordsz = sizeof(float);
    isflt = 1;
  }
  else {
    /* set the int flag and the word size. */
    wordsz = sizeof(int32_t);
    isflt = 0;
  }

  /* get the header and block byte offsets. */
  offhead = par.nheader;
  offblk = par.nbegin;
  offend = par.nend;

  /* get the total file size. */
  ntrue = bytes_size(D->fname);

  /* check that the file size is valid. */
  if (ntrue <= offhead)
    throw("invalid data file size of %u bytes", ntrue);

  /* get (or compute) the number of words per block. */
  if (par.reclen) {
    /* get the number from the parm structure. */
    nwords = par.reclen;

    /* determine how many blocks are in the file. */
    nblks = (ntrue - offhead) / (offblk + nwords * wordsz + offend);
  }
  else {
    /* assume a single data block. */
    nblks = 1;

    /* determine the size of the data block. */
    nwords = (ntrue - offhead - offblk - offend) / wordsz;
  }

  /* check that the computed blocking information matches the file size. */
  ncalc = offhead + nblks * (offblk + nwords * wordsz + offend);
  if (ncalc != ntrue)
    throw("expected file size %uB does not match actual %uB", ncalc, ntrue);

  /* check if block alignment is required. */
  if (offend) {
    /* build the block alignment size. */
    nalign = offblk + nwords * wordsz + offend;
  }
  else
    nalign = 0;

  /* open the input file for reading. */
  fh = fopen(D->fname, "rb");

  /* check that the file was opened. */
  if (!fh)
    throw("failed to open '%s'", D->fname);

  /* read data from the file into the output array. */
  if (!hx_array_fread_raw(fh, &D->array, par.endian, wordsz, isflt, offhead,
                          offblk, nblks, nwords, nalign))
    throw("failed to read raw data from '%s'", D->fname);

  /* close the input file. */
  fclose(fh);

  /* return success. */
  return 1;
}
	auto send(const T&... t){
		std::size_t sizes[] = {bytes_size(t)...};
		void *bufs[] = {to_bytes_helper(t,alloca(bytes_size(t)))...};
		return raw_send(sizeof...(T),sizes,bufs);
	}