mar300_header.c

Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * mar300_header.c
00004  * 
00005  * Author:  Claudio Klein, X-Ray Research GmbH.
00006  * Version: 1.0
00007  * Date:    05/07/1996
00008  *
00009  *********************************************************************/
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include <ctype.h>
00013 #include <math.h>
00014 #include <time.h>
00015 #ifndef __sgi
00016 #include <stdlib.h>
00017 #endif
00018 
00019 /*
00020  * mar software include files
00021  */
00022 #include "mar300_header.h"
00023 
00024 /*
00025  * Local functions
00026  */
00027 MAR300_HEADER   Getmar300Header(FILE *);
00028 MAR300_HEADER   Setmar300Header(void);
00029 int             Putmar300Header(int, int, MAR300_HEADER );
00030 
00031 /******************************************************************
00032  * Function: Getmar300Header
00033  ******************************************************************/
00034 MAR300_HEADER
00035 Getmar300Header(FILE *fp)
00036 {
00037 MAR300_HEADER   h;
00038 int             i,j, io,ntok=0;
00039 char            str[64], buf[64], *key, *token[20], q[64];
00040 float           ftmp,fh[15];
00041 
00042 
00043         if ( fp == NULL ) return( h );
00044 
00045         /*
00046          * Read 10*int and 15*float and 24*char
00047          */
00048 
00049         fseek( fp, 0, SEEK_SET );
00050         io  = fread(  &h, sizeof( MAR300_HEADER ), 1, fp );
00051 
00052         if ( io==1 ) io = sizeof( MAR300_HEADER ); 
00053 
00054         io += fread( str, sizeof( char ), 16, fp ); 
00055         if ( !strstr( str, "MAR" ) ) {
00056                 if ( io < 140 ) {
00057                         fprintf( stderr, "ERROR: only %d out of 140 bytes read from image header!!!\n",io );
00058                 }
00059                 io = 588;
00060                 goto SWAP;
00061         }
00062         else {
00063 
00064                 /* New style header: take all information from strings */
00065                 fseek( fp, sizeof(MAR300_HEADER)+16, SEEK_SET );
00066                 j = ftell( fp );
00067 
00068                 for ( i = 0; i< 28; i++ ) {
00069                         io += fread( str, sizeof( char ), 16, fp ); 
00070                         j += 16;
00071                         fseek( fp, j, SEEK_SET );
00072                         str[16]='\0';
00073                         if      ( i ==  3 ) sscanf( str, "%d", &h.pixels_x);
00074                         else if ( i ==  4 ) sscanf( str, "%d", &h.pixels_y);
00075                         else if ( i ==  5 ) sscanf( str, "%d", &h.lrecl);
00076                         else if ( i ==  6 ) sscanf( str, "%d", &h.max_rec);
00077                         else if ( i ==  7 ) sscanf( str, "%d", &h.high_pixels);
00078                         else if ( i ==  8 ) sscanf( str, "%d", &h.high_records);
00079                         else if ( i ==  9 ) sscanf( str, "%d", &h.counts_start);
00080                         else if ( i == 10 ) sscanf( str, "%d", &h.counts_end);
00081                         else if ( i == 11 ) sscanf( str, "%d", &h.exptime_sec);
00082                         else if ( i == 12 ) sscanf( str, "%d", &h.exptime_units);
00083                         else if ( i == 13 ) sscanf( str, "%f", &h.prog_time);
00084                         else if ( i == 14 ) sscanf( str, "%f", &h.r_max);
00085                         else if ( i == 15 ) sscanf( str, "%f", &h.r_min);
00086                         else if ( i == 16 ) sscanf( str, "%f", &h.p_r);
00087                         else if ( i == 17 ) sscanf( str, "%f", &h.p_l);
00088                         else if ( i == 18 ) sscanf( str, "%f", &h.p_x);
00089                         else if ( i == 19 ) sscanf( str, "%f", &h.p_y);
00090                         else if ( i == 20 ) sscanf( str, "%f", &h.centre_x);
00091                         else if ( i == 21 ) sscanf( str, "%f", &h.centre_y);
00092                         else if ( i == 22 ) sscanf( str, "%f", &h.lambda);
00093                         else if ( i == 23 ) sscanf( str, "%f", &h.distance);
00094                         else if ( i == 24 ) sscanf( str, "%f", &h.phi_start);
00095                         else if ( i == 25 ) sscanf( str, "%f", &h.phi_end);
00096                         else if ( i == 26 ) sscanf( str, "%f", &h.omega);
00097                         else if ( i == 27 ) sscanf( str, "%f", &h.multiplier);
00098                 }
00099         }
00100 
00101 SWAP:
00102         if ( h.pixels_x < 100 || h.pixels_x > 5000  ) {
00103                 swaplong( &h, 10*sizeof(int) );
00104                 if ( h.pixels_x < 100 || h.pixels_x > 5000  ) {
00105                         fprintf( stderr, "ERROR: something wrong with image header!!!\n");
00106                         h.pixels_x = h.pixels_y = 0;
00107                         return( h );
00108                 }
00109                 fseek( fp, 40, SEEK_SET );
00110                 fread( fh, sizeof(float), 15, fp );
00111 
00112                 swaplong( fh, 15*sizeof(float) );
00113 /*
00114                 for ( i=0;i<32; i++ ) {
00115                         q[0] = '\0';
00116                         if ( fh[10] & (int)( pow(2,i) )  )
00117                                 strcat( q, "0" );
00118                         else
00119                                 strcat( q, "1" );
00120                          printf("%s (0x%x)\n",q,fh[10]);
00121                 }
00122 */
00123                 sprintf( str, "%1.3f",fh[10] );
00124                 ftmp  = (float)atof( str );
00125 #if ( LINUX || linux || VMS )
00126                 ftmp = fh[10];
00127 #endif
00128 #if ( __sgi || LINUX || linux )
00129                 if ( ftmp <= 1.0 || ftmp  >  2000. ) {
00130                         swaplong( fh, 15*sizeof(float) );
00131                         swapvms(  fh, 15 );
00132                 }
00133 #endif
00134 #ifdef VMS
00135                 if ( fh[ 9] < 0.001 || fh[9] > 100. ) {
00136                         for ( i=0; i<15; i++ ) fh[i] = 0.0;
00137                         fh[3] = fh[4] = fh[5] = fh[6] = 0.15;
00138                 }
00139 #endif
00140 
00141                 h.prog_time     = fh[0];
00142                 h.r_max         = fh[1];
00143                 h.r_min         = fh[2];
00144                 h.p_r           = fh[3];
00145                 h.p_l           = fh[4];
00146                 h.p_x           = fh[5];
00147                 h.p_y           = fh[6];
00148                 h.centre_x      = fh[7];
00149                 h.centre_y      = fh[8];
00150                 h.lambda        = fh[9];
00151                 h.distance      = fh[10];
00152                 h.phi_start     = fh[11];
00153                 h.phi_end       = fh[12];
00154                 h.omega         = fh[13];
00155                 h.multiplier    = fh[14];
00156         }
00157                         
00158         if ( io <  588 )
00159                 fprintf( stderr, "ERROR: only %d out of 588 bytes read from image header!!!\n",io );
00160 
00161         fseek( fp, 2*h.pixels_x, SEEK_SET );
00162 
00163         return( h );
00164 }
00165 
00166 /******************************************************************
00167  * Function: Setmar300Header
00168  ******************************************************************/
00169 MAR300_HEADER
00170 Setmar300Header()
00171 {
00172 MAR300_HEADER   h;
00173 time_t          now;
00174 
00175         h.pixels_x              = 1200;
00176         h.pixels_x              = 1200;
00177         h.lrecl                 = 1200;
00178         h.max_rec               = 1200;
00179         h.high_pixels           = 0;
00180         h.high_records          = 0;
00181         h.counts_start          = 0;
00182         h.counts_end            = 0;
00183         h.exptime_sec           = 0;
00184         h.exptime_units         = 0;
00185 
00186         h.prog_time             = 0.0;
00187         h.r_max                 = 90.0;
00188         h.r_min                 = 0.0;
00189         h.p_x                   = 0.15;
00190         h.p_y                   = 0.15;
00191         h.p_l                   = 0.15;
00192         h.p_r                   = 0.15;
00193         h.centre_x              = 90.;
00194         h.centre_y              = 90.;
00195         h.lambda                = 1.541789;
00196         h.distance              = 70.0;
00197         h.phi_start             = 0.0;
00198         h.phi_end               = 0.0;
00199         h.omega                 = 0.0;
00200         h.multiplier            = 4.0;
00201 
00202         time( &now );
00203         sprintf( h.date, "%s", (char *)ctime( &now ) );
00204 
00205         return( h );
00206 }
00207 
00208 /******************************************************************
00209  * Function: Putmar300Header
00210  ******************************************************************/
00211 int
00212 Putmar300Header(int fd, int serial, MAR300_HEADER h)
00213 {
00214 int             i;
00215 int             io=0;
00216 char            c=' ',str[32];
00217 time_t          now;
00218 
00219 
00220         if ( fd < 0 )return(0);
00221 
00222         /*
00223          * Rewind file
00224          */
00225 
00226          i = lseek( fd, 0, SEEK_SET );
00227 
00228         if ( strlen( h.date ) < 5 ) {
00229                 time( &now );
00230                 
00231                 sprintf( str,"%s", (char *)ctime( &now ) );
00232                 for ( i=24; i<32; i++ ) 
00233                         str[i]='\0';
00234                 strncpy( h.date, str, 24 );
00235         }
00236 
00237         /*
00238          * Write image header (10xint+15*float+date)
00239          */
00240         io  = write( fd, &h, sizeof( h ) ); 
00241 
00242         /*
00243          * New header additions, all items are 16 byte strings.
00244          * 1.) MARCONTROL + Version no.
00245          * 2.) Scanner serial no.
00246          * 3.) Computer architecture
00247          * 4.) Sum of xray units
00248          * 5-...) Repeat normal header (i.e. 10 ints and 15 floats).
00249          */
00250 
00251         str[15] = '\0';
00252         sprintf( str, "MARCONTROL 1.0  ");
00253         io  += write( fd, str, 16 ); 
00254         sprintf( str, "%16d", serial);
00255         io  += write( fd, str, 16 ); 
00256 #if ( LINUX || linux )
00257         sprintf( str, "LINUX          " );
00258 #elif ( __sgi )
00259         sprintf( str, "SGI            " );
00260 #elif ( __osf__ )
00261         sprintf( str, "DEC UNIX       " );
00262 #elif ( __hpux  )
00263         sprintf( str, "HPUX           " );
00264 #elif ( __ALPHA )
00265         sprintf( str, "ALPHA          " );
00266 #else
00267         sprintf( str, "UNKNOWN        " );
00268 #endif
00269         io  += write( fd, str, 16 ); 
00270         sprintf( str, "%16.3f", (float)h.exptime_units );
00271         io  += write( fd, str, 16 ); 
00272 
00273         str[16] = '\0';
00274         /* 10 x integer as string */
00275         sprintf( str, "%16d",   h.pixels_x                     );
00276         io  += write( fd, str, 16 ); 
00277         sprintf( str, "%16d",   h.pixels_y                     );
00278         io  += write( fd, str, 16 ); 
00279         sprintf( str, "%16d",   h.lrecl                      );
00280         io  += write( fd, str, 16 ); 
00281         sprintf( str, "%16d",   h.max_rec                    );
00282         io  += write( fd, str, 16 ); 
00283         sprintf( str, "%16d",   h.high_pixels                );
00284         io  += write( fd, str, 16 ); 
00285         sprintf( str, "%16d",   h.high_records               );
00286         io  += write( fd, str, 16 ); 
00287         sprintf( str, "%16d",   h.counts_start               );
00288         io  += write( fd, str, 16 ); 
00289         sprintf( str, "%16d",   h.counts_end                 );
00290         io  += write( fd, str, 16 ); 
00291         sprintf( str, "%16d",   h.exptime_sec                );
00292         io  += write( fd, str, 16 ); 
00293         sprintf( str, "%16d",   h.exptime_units              );
00294         io  += write( fd, str, 16 ); 
00295 
00296         /* 15 x integer as string */
00297         sprintf( str, "%16.3f", h.prog_time                  );
00298         io  += write( fd, str, 16 ); 
00299         sprintf( str, "%16.3f", h.r_max                      );
00300         io  += write( fd, str, 16 ); 
00301         sprintf( str, "%16.3f", h.r_min                      );
00302         io  += write( fd, str, 16 ); 
00303         sprintf( str, "%16.3f", h.p_r                        );
00304         io  += write( fd, str, 16 ); 
00305         sprintf( str, "%16.3f", h.p_l                        );
00306         io  += write( fd, str, 16 ); 
00307         sprintf( str, "%16.3f", h.p_x                        );
00308         io  += write( fd, str, 16 ); 
00309         sprintf( str, "%16.3f", h.p_y                        );
00310         io  += write( fd, str, 16 ); 
00311         sprintf( str, "%16.3f", h.centre_x                   );
00312         io  += write( fd, str, 16 ); 
00313         sprintf( str, "%16.3f", h.centre_y                   );
00314         io  += write( fd, str, 16 ); 
00315         sprintf( str, "%16.3f", h.lambda                     );
00316         io  += write( fd, str, 16 ); 
00317         sprintf( str, "%16.3f", h.distance                   );
00318         io  += write( fd, str, 16 ); 
00319         sprintf( str, "%16.3f", h.phi_start                  );
00320         io  += write( fd, str, 16 ); 
00321         sprintf( str, "%16.3f", h.phi_end                    );
00322         io  += write( fd, str, 16 ); 
00323         sprintf( str, "%16.3f", h.omega                      );
00324         io  += write( fd, str, 16 ); 
00325         sprintf( str, "%16.3f", h.multiplier                 );
00326         io  += write( fd, str, 16 ); 
00327 
00328         /*
00329          * Fill remaining bytes of record with blanks
00330          */
00331 FILL:
00332         while ( lseek( fd, 0, SEEK_CUR ) < (2*h.pixels_x) ) 
00333                 if ( write( fd, &c, 1) < 1 )
00334                         break;
00335                 else
00336                         io++;
00337 
00338         if ( io < 2*h.pixels_x ) 
00339                 return 0;
00340 
00341         return 1;
00342 }