GSE++ library: reading and writing GSE waveforms

◆ encode()

std::string GSE2::waveform::CM6::encode ( const intT invalue)

CM6 subformat encoding function.

The function encodes the integer value in CM6 subformat. It returns the character sequence representing the numerical value. The second differences must have already been supplied to the data stream. The value is already a second difference value.

Author
Thomas Forbriger, Stefan Stange and others (see comments in the source code)
Parameters
valueinteger value to be encoded in CM6 subformat
Returns
character string with encoded integer value
See also
GSE2::waveform::TDAT2writeCM6

Definition at line 77 of file gsexx_cm6.cc.

Referenced by GSE2::waveform::TDAT2writeCM6::convert().

78 {
79  // string variable to store return value
80  std::string retval;
81 
82 // The original version of the core of this function was coded by Stefan
83 // Stange. The code (compress_6b) can be found in gse_functions.c in his
84 // library. Here is the original comment by Stefan Stange:
85 /*********************************************************************
86  Function: compress_6b
87  This routine computes the 6Byte encoding of integer data according
88  GSE2.0 based on cmprs6.f in CODECO by Urs Kradolfer. Again, here we
89  can cope with consecutive chunks of a data series.
90  Input is the data series (integer) and the # of samples. The character
91  representation of the data is successively stored to the dynamic
92  character buffer written by Andreas Greve.
93  Attention: Clipping is at 2**27 - 1 although it looks like 2**28 -1
94  in the FORTRAN-Code.
95 
96  St. Stange, 28.4.1998
97 *********************************************************************/
98 
99  // copy of value to be manipulated
100  intT value=invalue;
101  // this defines the value to character mapping
102  char achar[] =
103  " +-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
104 
105  // some powers of 2
106  // 2**5 2**10 2**15 2**20 2**25 2**27
107  long expo_2[] = { 0, 32, 1024, 32768, 1048576, 33554432, 134217728 };
108 
109  // some powers of 2 minus 1
110  // -1 + 2**5 2**10 2**15 2**20 2**25
111  long expo_2m1_o[] = { 01, 037, 01777, 077777, 03777777, 0177777777 };
112 
113  // internal values
114  int nflag;
115  int mflag = 32;
116  long jc;
117  int case_expo;
118 
119  nflag = 1;
120  // convert sign
121  if (value < 0 )
122  { nflag += 16; value = -value; }
123 
124  // clip at 2**27 -1
125  // value = (value >= expo_2[6]) ? expo_2[6] - 1 : value;
126  // the original code clipped at 2**27 -1
127  // we consider a number that large to be illegal
128  if (value >= expo_2[6])
129  {
130  std::cerr << "ERROR (CM6::encode): "
131  << "sample value exceeds largest value which can be handled\n"
132  << "Error is triggered by absolute value being ";
133  std::cerr.setf(std::ios_base::hex,std::ios_base::basefield);
134  std::cerr << "0x0" << value << ">=" << "0x0" << expo_2[6] << std::endl;
135  std::cerr << "The input value passed to the encoder is "
136  << "0x0" << invalue << std::endl;
137  throw Terror("ERROR (CM6::encode): illegal value");
138  }
139 
140  // compute the exponent base 2
141  std::frexp (double(value), &case_expo);
142  // and reduce by integer division
143  case_expo = case_expo/5;
144 
145  // check value
146  if (case_expo > 5 || case_expo < 0)
147  {
148  std::cerr << "ERROR (CM6::encode): exponent is "
149  << case_expo << std::endl;
150  std::cerr << "ERROR (CM6::encode): sample value is is "
151  << invalue << std::endl;
152  throw Terror("ERROR (CM6::encode): illegal exponent");
153  }
154 
155  for ( ; case_expo > 0; case_expo--)
156  {
157  // create one character per turn
158  jc = value/expo_2[case_expo] + nflag + mflag;
159  /*if (jc > 64 || jc < 1) return jc;*/
160  retval+=achar[jc];
161  value = value & expo_2m1_o[case_expo];
162  nflag = 1;
163  }
164 
165  // one character to go
166  jc = value + nflag;
167  retval+=achar[jc];
168 
169  return(retval);
170 }
int intT
All GSE2 waveform data is based on 4 byte integers.
Definition: gsexx.h:89
Here is the caller graph for this function: