C code defining NUTS format

 

//  NUTS data format
//  This is the code NUTS uses to write out data files
typedef struct
	{
	float r;
	float i;
	} Complex;
//  Global structure used by NUTS internally
//  May change at any time,
//  but, is named "spec" for this code
struct nmrdata
	{
	char comment[128];
	char user[40];
	char date[40];
   char exp[40];		//  Name of pulse experiment
	long headsize;		//  size of the NMR data file header
	long num_frames;	//  number of frames in the data  1D, 2D 3D etc.
   long LorF;			//  integer data = 1; floating point data = 0;
   long header_version;
   //  size of each header section for each frame, usually 32
	long frame_size;	
	long version;		      //  version number
	long decimation1;	      //  information about decimation
	long decimation2;
	long decimation3;
	long decimation4;
	long firmware_version1;	//  information about decimation
	long firmware_version2;
	long firmware_version3;
	long firmware_version4;
	long pts1d;
	long pts2d;
	long pts3d;
	long pts4d;
	short complex1;	/*  0 = real,  1 = complex;   2 = Bruker Interleaved */
	short complex2;	/*  0 = real,  1 = complex;   2 = Bruker Interleaved */
   short complex3;
   short complex4;
   short domain1;		/*  0 = time,  1 = frequency  */
	short domain2;		/*  0 = time,  1 = frequency  */
   short domain3;
   short domain4;
   short axis1;		/*  0 = none;  1 = points;  2 = Hz;  3 = PPM  */
	short axis2;		/*  0 = none;  1 = points;  2 = Hz;  3 = PPM  */
   short axis3;
   short axis4;
   float sf1;
	float sf2;
   float sf3;
   float sf4;
   float sw1;			/*  sweep width in first dimension  */
	float sw2;			/*  sweep width in second dimension  */
   float sw3;
   float sw4;
   float dw1;			/*  Dwell time in first dimension  */
	float dw2;			/*  Dwell time in first dimension  */
   float dw3;
   float dw4;
   float tlb1;
   float tlb2;
   float tlb3;
   float tlb4;
   float of1;			/*  Reference shift in first dimension  */
	float of2;			/*  Reference shift in second dimension  */
   float of3;
   float of4;
   long ofpt1;	   	/*  Reference point in first dimension  */
	long ofpt2;	   	/*  Reference point in second dimension  */
   long ofpt3;
   long ofpt4;
   long phpt1;	   	/*  Phase pivot in first dimension  */
	long phpt2;	   	/*  Phase pivot in second dimension  */
   long phpt3;
   long phpt4;
   float tpa1;			/*  Zero order phase in first dimension  */
   float tpa2;
   float tpa3;
   float tpa4;
   float tpb1;			/*  Fist order phase in first dimension  */
   float tpb2;
   float tpb3;
   float tpb4;
	long rg;				/*  Receiver Gain  */
	float de;			/*  Delay Entry for data after ADC trigger  */
	long na;				/*  Number of aquisitions completed  */
	long n0;				/*  Number of aquisitions requested */
	float pw;			/*  90 degree pulse  */
	float pd;			/*  recycle time  */
	float p1;
	float p2;
	float p3;
	float p4;
	float p5;
	float p6;
	float p7;
	float p8;
	float p9;
	float d1[64];     //  time array for T1, T2, kinetics etc;
	float d2;
	float d3;
	float d4;
	float d5;
	float d6;
	float d7;
	float d8;
	float d9;
	float temperature;	//  temperature of experiment
   long tailer;         //  -1  = Force tailer off
                        //   0  = No tailer
                        //   1  = Tailer Present
	};
//  Structure used by the structure below to define
//  each dimension of data.
struct DimParm
   {
   int pts  ;              //  Word   0
   int complex ;           //  Word   1 -- 0 = Real, 1 = Complex, 2 = TPPI
   int domain ;            //  Word   2 -- 0 = Time, 1 = Frequency
   int axis ;              //  Word   3 -- 0 = None, 1 = Points, 2 = Hz,  3 = PPM
   int decimation ;        //  Word   4 -- Bruker decimation number for digitally filtered data
   int firmware_version;   //  Word   5          
   int Dim1Dummy02;        //  Word   6          
   int Dim1Dummy03;        //  Word   7          
   int Dim1Dummy04;        //  Word   8          
   int Dim1Dummy05;        //  Word   9          
   int Dim1Dummy06;        //  Word  10          
   int Dim1Dummy07;        //  Word  11          
   int Dim1Dummy08;        //  Word  12          
   int Dim1Dummy09;        //  Word  13          
   int Dim1Dummy10;        //  Word  14          
   int Dim1Dummy11;        //  Word  15          
   float sw;               //  Word  16 - Sweep Width in 1st Dimension          
   float sf;               //  Word  17 - Spectrometer Frequency in 1st Dimension          
   float of;               //  Word  18 - Reference Offset in 1st Dimension          
   long RefPt;             //  Word  19 - Reference Point in 1st Dimension          
   long PivotPt;           //  Word  20 - Phase Pivot Point in 1st Dimension          
   float tpa;              //  Word  21 - Total Zero Order Phase Applied in 1st Dimension
   float tpb;              //  Word  22 - Total First Order Phase Applied in 1st Dimension
   float tlb;              //  Word  23 - Total Line Broadening Applied in 1st Dimension
   int DimDummy12;         //  Word  24          
   int DimDummy13;         //  Word  25          
   int DimDummy14;         //  Word  26          
   int DimDummy15;         //  Word  27          
   int DimDummy16;         //  Word  28          
   int DimDummy17;         //  Word  29          
   int DimDummy18;         //  Word  30          
   int DimDummy19;         //  Word  31          
   int DimDummy20;         //  Word  32
   int DimDummy21;         //  Word  33
   int DimDummy22;         //  Word  34
   int DimDummy23;         //  Word  35
   int DimDummy24;         //  Word  36
   int DimDummy25;         //  Word  37
   int DimDummy26;         //  Word  38
   int DimDummy27;         //  Word  39
   };
   
//  Current header format for NUTS 3/10/98
//  Another header is being developed that has more dimensions
//  for NMR data, but is not final yet.
struct NUTSHeader1
   {
   int ByteKey;            //  Word   0 -- 04030201 Hexadecimal
   int NumWordInHeader;    //  Word   1 -- Usually 256, data starts at (NumWordInHeader + 3) * sizeof(float)
   int NumDimInData;       //  Word   2 -- 1=1D, 2=2D
   int DataFormat;         //  Word   3 -- 0 = float, 1 = integer
   int HeaderVersion;      //  Word   4 -- 1 for NUTS 1st Header, 2 for NUTS 2nd Header, etc.
   int FrameSize;          //  Word   5 -- Usually 32
   int Version;            //  Word   6 -- Version Number of Program times 100
   int Dummy001;           //  Word   7
   int Tailer;             //  Word   8 -- 0 = No Tailer, 1 = Tailer
   int Dummy002[11];       //  Word   9-19
   float Time[64];         //  Word  20-83 -- 64 time values from an arrayed experiment
   int Dummy003[12];       //  Word  84-95
   struct DimParm D1;      //  Word   96-135 -- 1st Dimension Parameters
   struct DimParm D2;      //  Word  136-175 -- 2nd Dimension Parameters
   
   //  Other Parameters
   int Dummy004[28];       //  Word 176-203
   float Temp;             //  Word 204 -- Temperature for experiment                
   char PulProg[40];       //  Word 205-214          
   float PulLength;        //  Word 215 -- 90 degree pulse length in usec          
   float RecycleDelay;     //  Word 216 -- Recycle delay in seconds          
   int NumAcq;             //  Word 217 -- Number of acquisitions          
   char user[40];          //  Word 218-227 -- USER name string          
   char date[32];          //  Word 228-235 -- DATE string           
   char comment[84];       //  Word 236-256 -- COMMENT string 
   int Dummy005;           //  Word 257
   };
//  Routine used to write out the NMR file from NUTS
BOOL write_file( HWND hWnd, LPSTR lpstrFileName )
	{
   long i;
	long lzero = 0L;
	long lone = 1L;
	long keyword = 0x04030201;
   FILE *pf;
	DWORD error_code;
	
   pf = fopen( lpstrFileName, "w+b" );
   if( !pf )
		{
		error_code = GetLastError();
		sprintf( szBuffer,
				"Unable to open file\n%s\nfor writing!\n Error Code %ld",
				lpstrFileName, error_code );
		say_message( szBuffer );
		if( pf )
			fclose( pf );
		return FALSE;
		}
	//  try to write the file header
	if( !write_the_header( hWnd, pf, 1 ) )
		{
		say_message( "Could not write header in FileWrite." );
		if( pf )
			fclose( pf );
		return FALSE;
		}
   
   //  Make sure we are in the right place to start writing data
   fseek( pf, (spec.headsize+2L)*sizeof(float), SEEK_SET );
		
	for( i=0; i<spec.pts2d; i++ )
      {
      //  write the number of words in data slice
	   if( !write_a_long( pf, 2L*spec.pts1d ) ) 
		   {
		   say_message( "Could not write number of points in FileWrite." );
		   if( pf )
			   fclose( pf );
		   return FALSE;
		   }
	   //  write the data into memory
	   if( !write_CV_data( pf, show.cvmxNMRdata[i], (unsigned long)spec.pts1d ) )
		   {
		   sprintf( szBuffer, "Failed trying to save file\n%s\n", lpstrFileName );
		   strcat( szBuffer, "Disk Full ???" ); 
		   say_message( szBuffer );
		   if( pf )
			   fclose( pf );
		   return FALSE;
		   }
      }
   //  Write a tailer if necessary
   write_tailer_all( pf );
   //  close the file
	if( pf )
		fclose( pf );
	return TRUE;
	}
short write_the_header( HWND hWnd, FILE *pf, short doall )
	{
	//  go to the beginning of the file
   fseek( pf, 0, SEEK_SET );
	switch( spec.header_version )
      {
      case 2:
         if( write_NUTSHeader2( pf ) )
            return 1;
         else
            return 0;
      case 1:
      default:
         if( write_NUTSHeader1( pf ) )
            return 1;
         else
            return 0;
      }
	}
short write_NUTSHeader1( FILE *pf )
   {
   struct NUTSHeader1 hdr;
   int i;
   size_t result;
   hdr.ByteKey = 	0x04030201;
   hdr.NumWordInHeader = 256L;
   hdr.DataFormat = 0L;
   hdr.HeaderVersion = 1;
#ifdef _MAC
   hdr.NumDimInData = swap_bytes( spec.num_frames ); 
   hdr.FrameSize = swap_bytes( spec.frame_size );
   hdr.Version = swap_bytes( spec.version );
   hdr.Tailer = swap_bytes( spec.tailer );
   for( i=0; i<64; i++ )
      hdr.Time[i] = swap_bytes( spec.d1[i] );
   hdr.D1.pts  = swap_bytes( spec.pts1d );
   hdr.D1.complex = (int)swap_bytes( spec.complex1 );
   hdr.D1.domain = (int)swap_bytes( spec.domain1 );
   hdr.D1.axis = (int)swap_bytes( spec.axis1 );
   hdr.D1.decimation = swap_bytes( spec.decimation1 );
   hdr.D1.firmware_version = swap_bytes( spec.firmware_version1 );
   hdr.D1.sw = swap_bytes( spec.sw1 );
   hdr.D1.sf = swap_bytes( spec.sf1 );
   hdr.D1.of = swap_bytes( spec.of1 );
   hdr.D1.RefPt = swap_bytes( spec.ofpt1 );
   hdr.D1.PivotPt = spec.phpt1;
   hdr.D1.tpa = swap_bytes( spec.tpa1 );
   hdr.D1.tpb = swap_bytes( spec.tpb1 );
   hdr.D1.tlb = swap_bytes( spec.tlb1 );
   hdr.D2.pts  = swap_bytes( spec.pts2d );
   hdr.D2.complex = (int)swap_bytes( spec.complex2 );
   hdr.D2.domain = (int)swap_bytes( spec.domain2 );
   hdr.D2.axis = (int)swap_bytes( spec.axis2 );
   hdr.D2.decimation = swap_bytes( spec.decimation2 );
   hdr.D2.firmware_version = swap_bytes( spec.firmware_version2 );
   hdr.D2.sw = swap_bytes( spec.sw2 );
   hdr.D2.sf = swap_bytes( spec.sf2 );
   hdr.D2.of = swap_bytes( spec.of2 );
   hdr.D2.RefPt = swap_bytes( spec.ofpt2 );
   hdr.D2.PivotPt = swap_bytes( spec.phpt2 );
   hdr.D2.tpa = swap_bytes( spec.tpa2 );
   hdr.D2.tpb = swap_bytes( spec.tpb2 );
   hdr.D2.tlb = swap_bytes( spec.tlb2 );
   
   hdr.Temp = swap_bytes( spec.temperature );
   hdr.PulLength = swap_bytes( spec.pw );
   hdr.RecycleDelay = swap_bytes( spec.pd );
   hdr.NumAcq = swap_bytes( spec.na );
#else
   hdr.NumDimInData = spec.num_frames;
   hdr.FrameSize = spec.frame_size;
   hdr.Version = spec.version;
   hdr.Tailer = spec.tailer;
   for( i=0; i<64; i++ )
      hdr.Time[i] = spec.d1[i];
   hdr.D1.pts  = spec.pts1d;
   hdr.D1.complex = (int)spec.complex1;
   hdr.D1.domain = (int)spec.domain1;
   hdr.D1.axis = (int)spec.axis1;
   hdr.D1.decimation = spec.decimation1;
   hdr.D1.firmware_version = spec.firmware_version1;
   hdr.D1.sw = spec.sw1;
   hdr.D1.sf = spec.sf1;
   hdr.D1.of = spec.of1;
   hdr.D1.RefPt = spec.ofpt1;
   hdr.D1.PivotPt = spec.phpt1;
   hdr.D1.tpa = spec.tpa1;
   hdr.D1.tpb = spec.tpb1;
   hdr.D1.tlb = spec.tlb1;
   hdr.D2.pts  = spec.pts2d;
   hdr.D2.complex = (int)spec.complex2;
   hdr.D2.domain = (int)spec.domain2;
   hdr.D2.axis = spec.axis2;
   hdr.D2.decimation = spec.decimation2;
   hdr.D2.firmware_version = spec.firmware_version2;
   hdr.D2.sw = spec.sw2;
   hdr.D2.sf = spec.sf2;
   hdr.D2.of = spec.of2;
   hdr.D2.RefPt = spec.ofpt2;
   hdr.D2.PivotPt = spec.phpt2;
   hdr.D2.tpa = spec.tpa2;
   hdr.D2.tpb = spec.tpb2;
   hdr.D2.tlb = spec.tlb2;
   
   hdr.Temp = spec.temperature;
   hdr.PulLength = spec.pw;
   hdr.RecycleDelay = spec.pd;
   hdr.NumAcq = spec.na;
#endif
   strcpy( hdr.PulProg, spec.exp );
   strcpy( hdr.user, spec.user );
   strcpy( hdr.date, spec.date );
   strcpy( hdr.comment, spec.comment );
	i = sizeof(hdr);
   result = fwrite( &hdr, sizeof(hdr), 1, pf );
   if( result )
      return 1;
   else
      return 0;
   }
short write_a_long( FILE *pf, long x )
	{
	long y;
   size_t written;
	switch( nProcessor )
		{
		case INTELX86:
			y = x;
			break;
		case MACINTOSHPOWERPC:
		case MACINTOSH68K:
			y = swap_bytes_long( x );
			break;
		default:
			y = x;
			break;
		}
   written = fwrite( &y, sizeof(long), 1, pf );
   if( written < 1 )
      return 0;
   else
      return 1;
	}
short write_CV_data( FILE *pf, Complex *data, unsigned long length )
	{
   Complex *MacData;
	size_t written;
	long i;
   float temp;
	short return_flag;
	// Swap the bytes so all data is stored in Intel fashion
	if( nProcessor  ==  MACINTOSH68K   || nProcessor == MACINTOSHPOWERPC )
		{
      //  Mac data, try for a byte swapping memory buffer
      MacData = Cvalloc( (unsigned long)length );
      if( MacData )
         {
		   for( i=0; i<(long)length; i++ )
			   {
			   MacData[i].r = swap_bytes_float( data[i].r );
			   MacData[i].i = swap_bytes_float( data[i].i );
			   }
	      written = fwrite( MacData, sizeof(Complex), length, pf );
         if( written < length ) 
		      return_flag = 0;
	      else
		      return_flag = 1;								;
         Cvfree( &MacData );
      	return( return_flag);
         }
      else
         {
         //  Not enough memory for buffer, therefore do it the slow way 
		   for( i=0; i<(long)length; i++ )
			   {
			   temp = swap_bytes_float( data[i].r );
            fwrite( &temp, sizeof(float), 1, pf );
			   temp = swap_bytes_float( data[i].i );
            fwrite( &temp, sizeof(float), 1, pf );
			   }
         return 1;
         }
		}
   else
      {
      //  Not Mac data, so just write it out
	   written = fwrite( data, sizeof(Complex), length, pf );
      if( written < length ) 
		   return_flag = 0;
	   else
		   return_flag = 1;								;
	   return( return_flag);
      }
	}

 

Main Help page

Command Index

 

Last updated 3/12/98.