ReadTimeSeries_source.c 5.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#define CONCAT2x(a,b) a##b
#define CONCAT2(a,b) CONCAT2x(a,b)
#define CONCAT3x(a,b,c) a##b##c
#define CONCAT3(a,b,c) CONCAT3x(a,b,c)
#define STRING(a) #a

#define STYPE CONCAT2(TYPE,TimeSeries)
#define VTYPE CONCAT2(TYPE,Sequence)
#define FUNC CONCAT3(LAL,TYPECODE,ReadTimeSeries)

11 12 13 14 15
#ifndef BASETYPE
#define BASETYPE TYPE
#endif


16 17 18 19 20
/* Maybe for consistent allocation of memory we should include a
companion function called element counter that will notify the calling
routine of how large we should allocate the Series to be before
calling the read frequency series module */

Karl Wette's avatar
Karl Wette committed
21

22 23 24 25
void 
FUNC ( LALStatus* status,
                  STYPE *series,
                  const CHAR *filename )
Karl Wette's avatar
Karl Wette committed
26
 
27 28 29 30
{
  REAL8Vector	*t=NULL;
  REAL8         *tPtr;
  REAL8         *tStopPtr;
31
  union { TYPE value; BASETYPE array[sizeof(TYPE)/sizeof(BASETYPE)]; } data;
32 33 34 35 36 37 38
  TYPE	        *outputPtr;
  FILE		*fp;
  CHAR		 line[MaxLineLength];  /*holds data from each line*/
  LALUnit        tempUnit;
  CHARVector    *string=NULL;

  /* need to declare error section here */
39
  INITSTATUS(status);
40 41 42 43 44 45 46
  ATTATCHSTATUSPTR(status);  
 
  ASSERT( filename != NULL,status,READFTSERIESH_EFILENOTFOUND,
	  READFTSERIESH_MSGEFILENOTFOUND );

  /* if (filename == NULL) return; */
  
47
  fp = LALFopen( filename, "r" );
48 49 50 51 52 53 54 55 56 57 58 59 60
  if (fp == NULL) 
  {
    ABORT( status, READFTSERIESH_EFILENOTFOUND,
	   READFTSERIESH_MSGEFILENOTFOUND );
  }

  /* limited to line of data not exceeding MaxLineLength chars*/
  
  if (fgets(line,sizeof(line),fp) == NULL) 
  {
    ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
  }

61 62 63 64 65
  if (line[0] != '#' || line[1] != ' ')
  {
    ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
  }

66
  if (snprintf( series->name, sizeof( series->name ), "%s", line + 2) < 0)
67 68 69 70 71 72 73 74 75 76 77 78 79
  {
    ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
  }
  /* Change trailing linefeed to '\0' */
  if ( changeCharToNull(series->name, '\n', series->name + LALNameLength) )
  {	
    ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
  }

  if (fgets(line,sizeof(line),fp) == NULL) 
  {
    ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
  }
80 81 82 83 84
  if (line[0] != '#' || line[1] != ' ')
  {
    ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
  }
  if (line[2] == '\n') 
85 86 87
  {
    series->f0 = 0.0;
  }
88
  else if ( sscanf( line, "# Heterodyned at %lf Hz\n", &(series->f0) ) != 1 )
89 90 91 92 93 94 95 96 97
  {
    ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
  }

  if (fgets(line,sizeof(line),fp) == NULL) 
  {
    ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
  }

98 99 100 101 102
  if (line[0] != '#' || line[1] != ' ')
  {
    ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
  }
  if (line[2] == '\n') 
103 104 105 106
  {
    series->epoch.gpsSeconds = 0;
    series->epoch.gpsNanoSeconds = 0;
  }
107
  else if ( sscanf( line, "# Epoch is %d seconds, %d nanoseconds\n",
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	            &(series->epoch.gpsSeconds), 
                    &(series->epoch.gpsNanoSeconds) )
	    != 2 )
  {
    ABORT( status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE );
  }
  
  TRY( LALCHARCreateVector(status->statusPtr, &string, MaxLineLength), status );
  
  if (fgets(line,sizeof(line),fp) == NULL) 
  {
    TRY( LALCHARDestroyVector( status->statusPtr, &string ), status );
    ABORT( status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE );
  }

123
  if (!strcmp(line,"# Units are ()\n"))
124 125 126 127
  {
    series->sampleUnits = lalDimensionlessUnit; 
  }
  else {
128
    if ( sscanf( line, "# Units are (%[^)]", string->data ) != 1 )
129 130 131 132
    {
      TRY( LALCHARDestroyVector( status->statusPtr, &string ), status );
      ABORT( status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE );
    }
133 134
    if ( XLALParseUnitString(&tempUnit, string->data) == NULL )
    {
135
      TRY( LALCHARDestroyVector( status->statusPtr, &string ), status );
136 137
      ABORT( status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE );
    }
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    series->sampleUnits = tempUnit;
  }

  TRY( LALCHARDestroyVector( status->statusPtr, &string ), status );
  
  TRY( LALDCreateVector(status->statusPtr, &t, series->data->length),
       status );


  tPtr = &(t->data[0]);
  tStopPtr = tPtr + t->length;
  outputPtr = &(series->data->data[0]);

  if(fgets(line,sizeof(line),fp) == NULL) 
  {
    TRY( LALDDestroyVector( status->statusPtr, &t ), status );
    ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
  }

  while(fgets(line,sizeof(line),fp)!=NULL)
  {
    /*change arg so we dereference pointer */
    if ( sscanf(line, FMT, tPtr, ARG) != 1 + NARGS ) 
    {
      TRY( LALDDestroyVector( status->statusPtr, &t ), status );
      ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
    }
165
    *(outputPtr) = data.value;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    tPtr++;
    outputPtr++;	

    if (tPtr > tStopPtr) 
    {
      TRY( LALDDestroyVector( status->statusPtr, &t ), status );
      ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
    }
  }
  if (tPtr != tStopPtr) 
  {
    TRY( LALDDestroyVector( status->statusPtr, &t ), status );
    ABORT(status, READFTSERIESH_EPARSE, READFTSERIESH_MSGEPARSE);
  }

  (series->deltaT) = ( t->data[1] - t->data[0] );

  TRY( LALDDestroyVector( status->statusPtr, &t ), status );

  DETATCHSTATUSPTR(status);
  RETURN(status);  
}
188 189

#undef BASETYPE