fgettra.c
Upload User: qiu126
Upload Date: 2015-02-06
Package Size: 3950k
Code Size: 3k
Category:

Other systems

Development Platform:

Visual C++

  1. /* FGETTRA: $Revision: 1.6 $; $Date: 90/12/23 17:03:09 $ */
  2. /*----------------------------------------------------------------------
  3.  * Copyright (c) Colorado School of Mines, 1989.
  4.  * All rights reserved.
  5.  *
  6.  * This code is part of SU.  SU stands for Seismic Unix, a processing line
  7.  * developed at the Colorado School of Mines, partially based on Stanford
  8.  * Exploration Project (SEP) software.  Inquiries should be addressed to:
  9.  *
  10.  *  Jack K. Cohen, Center for Wave Phenomena, Colorado School of Mines,
  11.  *  Golden, CO 80401  (jkc@dix.mines.colorado.edu)
  12.  *----------------------------------------------------------------------
  13.  */
  14. #include "su.h"
  15. #include "segy.h"
  16. #include "header.h"
  17. /* fgettra - get disk trace by trace number
  18.  *
  19.  * Returns:
  20.  * integer number of traces in the disk file
  21.  *
  22.  * Synopsis:
  23.  * #include "segy.h"
  24.  * fgettra(FILE *fp, segy *traceptr, int itr)
  25.  *
  26.  * gettra(tp, itr) is a macro for fgettra(stdin, tp, itr)
  27.  *
  28.  * Credits:
  29.  * SEP: Einar
  30.  * CWP: Jack
  31.  *
  32.  */
  33. int fgettra(FILE *fp, segy *tp, int itr)
  34. {
  35. static int nsfirst; /* samples on first trace */
  36. static int nsegy; /* size of trace in bytes */
  37. static int ntr; /* number of traces in file */
  38. static bool first=true; /* flag for first entry */
  39. int nread; /* bytes read */
  40. static char card[EBCBYTES];
  41. static char bhdr[BNYBYTES];
  42. static int hdbytes;
  43. if (first) { /* first entry; get number of traces */
  44. unsigned short bytesper;/* bytes per float (packed?) */
  45. long length; /* length of file in bytes */
  46. filetype ftype = filestat(fileno(fp));
  47. first = false;
  48. switch(ftype) {
  49. case TTY:
  50. warn("stdin not redirected");
  51. selfdoc();
  52. break;
  53. case DISK: /* correct */
  54. break;
  55. default:
  56. err("%s: input must be disk file", __FILE__);
  57. break;
  58. }
  59. efread(card, 1, 10, fp);
  60.                 if (strncmp(card, "C 1 CLIENT",10)==0 ) {
  61. efseek(fp, EBCBYTES+BNYBYTES, 0);
  62. hdbytes = EBCBYTES + BNYBYTES;
  63. }
  64. else {
  65. efseek(fp, 0, 0);
  66. hdbytes = 0;
  67. }
  68.    
  69. if (HDRBYTES != (nread = efread(tp, 1, HDRBYTES, fp))) {
  70. err("%s: read only %d bytes of header",
  71. __FILE__, nread);
  72. }
  73. nsfirst = tp->ns;
  74. if (nsfirst > SU_NFLTS) {
  75. err("%s: trace too long: nsfirst=%d>SU_NFLTS=%d",
  76. __FILE__, nsfirst, SU_NFLTS);
  77. }
  78. if      (tp->trid == CHARPACK)  bytesper = sizeof(char);
  79. else if (tp->trid == SHORTPACK) bytesper = sizeof(short);
  80. else                            bytesper = sizeof(float);
  81. nsegy = HDRBYTES + nsfirst * bytesper;
  82. efseek(fp, 0, SEEK_END);
  83. length = eftell(fp);
  84. ntr = (length-hdbytes) / nsegy;
  85. } /* end first entry */
  86. /* Check on requested trace number */
  87. if (itr >= ntr)  err("%s, trying to read off end of file", __FILE__);
  88. /* Position file pointer at start of requested trace */
  89. efseek(fp, itr*nsegy+hdbytes, SEEK_SET);
  90. nread = efread(tp, 1, nsegy, fp);
  91. if (nread != nsegy)
  92. err("%s: read only %d of %d bytes in trace",
  93. __FILE__, nread, nsegy);
  94. if (tp->ns != nsfirst)
  95.     warn("%s: header ns field = %d differs from first trace = %d",
  96. __FILE__, tp->ns, nsfirst);
  97. return ntr;
  98. }