mar345_header.c

Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * mar345_header.c
00004  * 
00005  * Author:      Claudio Klein, X-Ray Research GmbH.
00006  * Version:     1.5
00007  * Date:        06/09/1999
00008  *
00009  * History:
00010  * Version    Date      Changes
00011  * ______________________________________________________________________
00012  *
00013  * 1.5        06/09/99  Element gap extended (8 values: gaps 1-8)
00014  * 1.4        14/05/98  Element gap introduced 
00015  *
00016  *********************************************************************/
00017 
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include <ctype.h>
00021 #include <math.h>
00022 #ifndef __sgi
00023 #include <stdlib.h>
00024 #endif
00025 #include <time.h>
00026 
00027 /*
00028  * mar software include files
00029  */
00030 #include "mar345_header.h"
00031 
00032 /*
00033  * Definitions
00034  */
00035 #define STRING          0
00036 #define INTEGER         1
00037 #define FLOAT           2
00038 
00039 /*
00040  * External variables
00041  */
00042 extern int      InputType();
00043 extern void     WrongType();    
00044 extern void     RemoveBlanks(); 
00045 
00046 /*
00047  * Local functions
00048  */
00049 MAR345_HEADER   Getmar345Header(FILE *);
00050 MAR345_HEADER   Setmar345Header(void  );
00051 int             Putmar345Header(int   , MAR345_HEADER);
00052 
00053 /******************************************************************
00054  * Function: Getmar345Header
00055  ******************************************************************/
00056 MAR345_HEADER
00057 Getmar345Header(FILE *fp)
00058 {
00059 MAR345_HEADER   h;
00060 int             i,j, ntok=0,fpos=0;
00061 int             ngaps = 0;
00062 char            str[64], buf[128], *key, *token[20];
00063 int             head[32];
00064 
00065 
00066         /*
00067          * Set defaults
00068          */
00069 
00070         h = Setmar345Header();
00071 
00072         if( fp == NULL ) return( h ); 
00073 
00074         fseek( fp, 0, SEEK_SET );
00075 
00076         if ( fread( head, sizeof(int), 32, fp ) < 32) {
00077                 return( h );
00078         }
00079 
00080         /* First 32 longs contain: */
00081         h.byteorder     = (int  )head[ 0];
00082         h.size          = (short)head[ 1];
00083         h.high          = (int  )head[ 2];
00084         h.format        = (char )head[ 3];
00085         h.mode          = (char )head[ 4];
00086         h.pixels        = (int  )head[ 5];
00087         h.pixel_length  = (float)head[ 6];
00088         h.pixel_height  = (float)head[ 7];
00089         h.wave          = (float)head[ 9]/1000000.;
00090         h.dist          = (float)head[ 8]/1000.;
00091         h.phibeg        = (float)head[10]/1000.;
00092         h.phiend        = (float)head[11]/1000.;
00093         h.omebeg        = (float)head[12]/1000.;
00094         h.omeend        = (float)head[13]/1000.;
00095         h.chi           = (float)head[14]/1000.;
00096         h.theta         = (float)head[15]/1000.;
00097 
00098         /* First ASCII line (bytes 128 to 192 contains: mar research */
00099         /* Ignore it...                                              */
00100 
00101         fpos = fseek( fp, 192, SEEK_SET );
00102 
00103         /*
00104          * Read input lines
00105          */
00106 
00107         while( fgets(buf,64,fp)!= NULL){
00108 
00109                 /* Always add 64 bytes to current filemarker after 1 read */
00110                 fpos += 64;
00111                 fseek( fp, fpos, SEEK_SET );
00112 
00113                 /* Keyword: END OF HEADER*/
00114                 if(strstr(buf,"END OF HEADER") ) 
00115                         break;
00116                 else if ( strstr( buf, "SKIP" ) ) continue;
00117 
00118                 if ( strlen(buf) < 2 ) continue;
00119 
00120                 /* Scip comment lines */
00121                 if( buf[0] == '#' || buf[0]=='!' ) continue;
00122 
00123                 /* Tokenize input string */
00124                 /* ntok  = number of items on input line - 1 (key) */
00125                 ntok = -1;      
00126 
00127                 for(i=0;i<64;i++) {
00128                         /* Convert TAB to SPACE */
00129                         if( buf[i] == '\t') buf[i] = ' ';
00130                         if( buf[i] == '\f') buf[i] = ' '; 
00131                         if( buf[i] == '\n') buf[i] = '\0'; 
00132                         if( buf[i] == '\r') buf[i] = '\0'; 
00133                         if( buf[i] == '\0') break;
00134                 }
00135 
00136                 for(i=0;i<strlen(buf);i++) {
00137                         if( buf[i] == ' ' ) continue;
00138                         ntok++; 
00139                         for (j=i;j<strlen(buf);j++) 
00140                                 if( buf[j] == ' ') break;
00141                         i=j;
00142                 }
00143                 if (strlen(buf) < 3 ) continue; 
00144 
00145                 key = strtok( buf, " ");
00146 
00147                 /* Convert keyword to uppercase */
00148                 for ( i=0; i<strlen(key); i++ )
00149                         if ( isalnum( key[i] ) ) key[i] = toupper( key[i] );
00150 
00151                 for(i=0;i<ntok;i++) {
00152                         token[i] = strtok( NULL, " ");
00153                         strcpy( str, token[i] );
00154 
00155                         for ( j=0; j<strlen( str ); j++ )
00156                                 if ( isalnum( str[j] ) && !strstr(key,"PROG") ) str[j] = toupper( str[j] );
00157                         strcpy( token[i] , str );
00158                         RemoveBlanks( token[i] );
00159                 }
00160 
00161                 /* Keyword: PROGRAM */
00162                 if(!strncmp(key,"PROG",4) && ntok >= 2 ) {
00163                         strcpy( h.program, token[0] );
00164                         strcpy( h.version, token[2] );
00165                 }
00166 
00167                 /* Keyword: OFFSET */
00168                 else if(!strncmp(key,"OFFS",4) && ntok >= 1 ) {
00169                         for ( i=0; i<ntok; i++ ) {
00170                             if ( strstr( token[i], "ROF" ) ) {
00171                                 i++;
00172                                 if ( InputType( token[i] ) >= INTEGER ) {
00173                                         h.roff = atof( token[i] );
00174                                 }
00175                                 else
00176                                         WrongType( FLOAT, key, token[i] );
00177                             }
00178                             else if ( strstr( token[i], "TOF" ) ) {
00179                                 i++;
00180                                 if ( InputType( token[i] ) >= INTEGER ) {
00181                                         h.toff = atof( token[i] );
00182                                 }
00183                                 else
00184                                         WrongType( FLOAT, key, token[i] );
00185                             }
00186                             /* Compatibility with previous versions for GAP entries: */
00187                             else if ( strstr( token[i], "GAP" ) ) {
00188                                 i++;
00189                                 if ( InputType( token[i] ) == INTEGER ) {
00190                                         h.gap[1]  = atoi( token[i] );
00191                                 }
00192                                 else
00193                                         WrongType( INTEGER, key, token[i] );
00194                             }
00195                         }
00196                 }
00197                 /* Keyword: GAP */
00198                 else if(!strncmp(key,"GAPS",4) && ntok >= 1 ) {
00199                         for ( i=0; i<ntok; i++ ) {
00200                                 if ( InputType( token[i] ) == INTEGER ) {
00201                                         if ( ngaps < N_GAPS ) 
00202                                                 h.gap[ngaps++]  = atoi( token[i] );
00203                                 }
00204                                 else
00205                                         WrongType( INTEGER, key, token[i] );
00206                         }
00207                 }
00208 
00209                 /* Keyword: ADC */
00210                 else if(!strncmp(key,"ADC",3) && ntok >= 1 ) {
00211                         for ( i=0; i<ntok; i++ ) {
00212                             if ( strstr( token[i], "A" ) && strlen( token[i] ) == 1 ) {
00213                                 i++;
00214                                 if ( InputType( token[i] ) == INTEGER ) {
00215                                         h.adc_A = atoi( token[i] );
00216                                 }
00217                                 else
00218                                         WrongType( INTEGER, key, token[i] );
00219                             }
00220                             else if ( strstr( token[i], "B" ) && strlen( token[i] ) == 1 ) {
00221                                 i++;
00222                                 if ( InputType( token[i] ) == INTEGER ) {
00223                                         h.adc_B = atoi( token[i] );
00224                                 }
00225                                 else
00226                                         WrongType( INTEGER, key, token[i] );
00227                             }
00228                             else if ( strstr( token[i], "ADD_A" ) ) {
00229                                 i++;
00230                                 if ( InputType( token[i] ) == INTEGER ) {
00231                                         h.add_A = atoi( token[i] );
00232                                 }
00233                                 else
00234                                         WrongType( INTEGER, key, token[i] );
00235                             }
00236                             else if ( strstr( token[i], "ADD_B" ) ) {
00237                                 i++;
00238                                 if ( InputType( token[i] ) == INTEGER ) {
00239                                         h.add_B = atoi( token[i] );
00240                                 }
00241                                 else
00242                                         WrongType( INTEGER, key, token[i] );
00243                             }
00244                         }
00245                 }
00246 
00247                 /* Keyword: MULTIPLIER */
00248                 else if(!strncmp(key,"MULT",4) && ntok >= 0 )
00249                         if ( InputType( token[0] ) >= INTEGER ) 
00250                                 h.multiplier = atof( token[0] );
00251                         else
00252                                 WrongType( FLOAT, key, token[0] );
00253 
00254                 /* Keyword: GAIN */
00255                 else if(!strncmp(key,"GAIN",4) && ntok >= 0 )
00256                         if ( InputType( token[0] ) >= INTEGER ) 
00257                                 h.gain = atof( token[0] );
00258                         else
00259                                 WrongType( FLOAT, key, token[0] );
00260 
00261                 /* Keyword: COUNTS */
00262                 else if(!strncmp(key,"COUN",4) && ntok >= 1 )
00263                         for(i=0;i<ntok;i++) {
00264                             if ( strstr( token[i], "STA" ) ) {
00265                                 i++;
00266                                 if ( InputType( token[i] ) >= INTEGER ) {
00267                                         h.dosebeg = atof( token[i] );
00268                                 }
00269                                 else
00270                                         WrongType( FLOAT, key, token[i] );
00271                             }
00272                             else if ( strstr( token[i], "END" ) ) {
00273                                 i++;
00274                                 if ( InputType( token[i] ) >= INTEGER ) {
00275                                         h.doseend = atof( token[i] );
00276                                 }
00277                                 else
00278                                         WrongType( FLOAT, key, token[i] );
00279                             }
00280                             else if ( strstr( token[i], "MIN" ) ) {
00281                                 i++;
00282                                 if ( InputType( token[i] ) >= INTEGER ) {
00283                                         h.dosemin = atof( token[i] );
00284                                 }
00285                                 else
00286                                         WrongType( FLOAT, key, token[i] );
00287                             }
00288                             else if ( strstr( token[i], "MAX" ) ) {
00289                                 i++;
00290                                 if ( InputType( token[i] ) >= INTEGER ) {
00291                                         h.dosemax = atof( token[i] );
00292                                 }
00293                                 else
00294                                         WrongType( FLOAT, key, token[i] );
00295                             }
00296                             else if ( strstr( token[i], "AVE" ) ) {
00297                                 i++;
00298                                 if ( InputType( token[i] ) >= INTEGER ) {
00299                                         h.doseavg = atof( token[i] );
00300                                 }
00301                                 else
00302                                         WrongType( FLOAT, key, token[i] );
00303                             }
00304                             else if ( strstr( token[i], "SIG" ) ) {
00305                                 i++;
00306                                 if ( InputType( token[i] ) >= INTEGER ) {
00307                                         h.dosesig = atof( token[i] );
00308                                 }
00309                                 else
00310                                         WrongType( FLOAT, key, token[i] );
00311                             }
00312                             else if ( strstr( token[i], "NME" ) ) {
00313                                 i++;
00314                                 if ( InputType( token[i] ) == INTEGER ) {
00315                                         h.dosen = atoi( token[i] );
00316                                 }
00317                                 else
00318                                         WrongType( INTEGER, key, token[i] );
00319                             }
00320                         }
00321 
00322                 /* Keyword: MODE */
00323                 else if( !strncmp(key,"MODE",4) && ntok >= 0 ) 
00324                         if ( strstr( token[0], "TIME" ) )
00325                                 h.mode  = 1;
00326                         else if ( strstr( token[0], "DOSE" ) )
00327                                 h.mode  = 0;
00328                         else
00329                                 WrongType( STRING, key, token[0] );
00330 
00331                 /* Keyword: DISTANCE */
00332                 else if(!strncmp(key,"DIST",4) && ntok >= 0 )
00333                         if ( InputType( token[0] ) >= INTEGER ) 
00334                                 h.dist = atof( token[0] );
00335                         else
00336                                 WrongType( FLOAT, key, token[0] );
00337 
00338                 /* Keyword: PIXELSIZE */
00339                 else if(!strncmp(key,"PIXE",4) && ntok >= 0 ) {
00340                         for(i=0;i<ntok;i++) {
00341                             if ( strstr( token[i], "LEN" ) ) {
00342                                 i++;
00343                                 if ( InputType( token[i] ) >= INTEGER ) {
00344                                         h.pixel_length = atof( token[i] );
00345                                 }
00346                                 else
00347                                         WrongType( FLOAT, key, token[i] );
00348                             }
00349                             else if ( strstr( token[i], "HEI" ) ) {
00350                                 i++;
00351                                 if ( InputType( token[i] ) >= INTEGER ) {
00352                                         h.pixel_height= atof( token[i] );
00353                                 }
00354                                 else
00355                                         WrongType( FLOAT, key, token[i] );
00356                             }
00357                         }
00358                 }
00359 
00360 
00361                 /* Keyword: SCANNER */
00362                 else if(!strncmp(key,"SCAN",4) && ntok >= 0 )
00363                         if ( InputType( token[0] ) == INTEGER ) 
00364                                 h.scanner = atoi( token[0] );
00365                         else
00366                                 WrongType( INTEGER, key, token[0] );
00367 
00368                 /* Keyword: HIGH */
00369                 else if(!strncmp(key,"HIGH",4) && ntok >= 0 )
00370                         if ( InputType( token[0] ) == INTEGER ) 
00371                                 h.high    = atoi( token[0] );
00372                         else
00373                                 WrongType( INTEGER, key, token[0] );
00374 
00375                 /* Keyword: DATE */
00376                 else if(!strncmp(key,"DATE",4) && ntok >= 0 ) {
00377                         for ( i=0; i<strlen( buf ); i++ ) 
00378                                 if ( buf[i] == ' ' ) break;
00379                         for ( j=i; j<strlen( buf ); j++ ) 
00380                                 if ( buf[j] != ' ' ) break;
00381                         strcpy( h.date, buf+j );
00382                 }
00383 
00384                 /* Keyword: REMARK */
00385                 else if(!strncmp(key,"REMA",4) && ntok >= 0 ) {
00386                         for ( i=0; i<strlen( buf ); i++ ) 
00387                                 if ( buf[i] == ' ' ) break;
00388                         for ( j=i; j<strlen( buf ); j++ ) 
00389                                 if ( buf[j] != ' ' ) break;
00390                         strcpy( h.remark, buf+j );
00391                 }
00392 
00393                 /* Keyword: FORMAT */
00394                 else if(!strncmp(key,"FORM",4) && ntok >= 1 )  {
00395                         if ( InputType( token[0] ) == INTEGER ) 
00396                                 h.size = atoi( token[0] );
00397                         else
00398                                 WrongType( INTEGER, key, token[0] );
00399                         for ( i=1; i<ntok; i++ ) {
00400                                 if ( strstr( token[i], "PCK" ) ) 
00401                                         h.format = 1;
00402                                 else if ( strstr( token[i], "IMA" ) ) 
00403                                         h.format = 0;
00404                                 else if ( strstr( token[i], "SPI" ) )
00405                                         h.format = 2;
00406                                 else {
00407                                         if ( InputType( token[i] ) == INTEGER ) 
00408                                                 h.pixels = atoi( token[i] );
00409                                         else
00410                                                 WrongType( INTEGER, key, token[i] );
00411                                 }
00412                         }
00413                 }
00414 
00415                 /* Keyword: LAMBDA or WAVELENGTH */
00416                 else if( (!strncmp(key,"LAMB",4) || !strncmp(key,"WAVE",4) ) && ntok >= 0 ) 
00417                         if ( InputType( token[0] ) >= INTEGER ) 
00418                                 h.wave = atof( token[0] );
00419                         else
00420                                 WrongType( FLOAT, key, token[0] );
00421                 
00422 
00423                 /* Keyword: MONOCHROMATOR */
00424                 else if( !strncmp(key,"MONO",4) && ntok >=0 ) { 
00425                     for ( i=0; i<ntok; i++ ) {
00426                          if ( strstr( token[i], "POLA" ) ) {
00427                                 i++;
00428                                 if ( InputType( token[i] ) >= INTEGER ) {
00429                                         h.polar = atof( token[i] );
00430                                 }
00431                                 else
00432                                         WrongType( FLOAT, key, token[i] );
00433                         }
00434                         else {
00435                                 strcat( h.filter, token[i] );
00436                         }
00437                     }
00438                 }
00439 
00440 
00441                 /* Keyword: PHI */
00442                 else if(!strncmp(key,"PHI",3) && ntok >= 1 )
00443                         for(i=0;i<ntok;i++) {
00444                             if ( strstr( token[i], "STA" ) ) {
00445                                 i++;
00446                                 if ( InputType( token[i] ) >= INTEGER ) {
00447                                         h.phibeg = atof( token[i] );
00448                                 }
00449                                 else
00450                                         WrongType( FLOAT, key, token[i] );
00451                             }
00452                             else if ( strstr( token[i], "END" ) ) {
00453                                 i++;
00454                                 if ( InputType( token[i] ) >= INTEGER ) {
00455                                         h.phiend = atof( token[i] );
00456                                 }
00457                                 else
00458                                         WrongType( FLOAT, key, token[i] );
00459                             }
00460                             else if ( strstr( token[i], "OSC" ) ) {
00461                                 i++;
00462                                 if ( InputType( token[i] ) == INTEGER ) {
00463                                         h.phiosc = atoi( token[i] );
00464                                 }
00465                                 else
00466                                         WrongType( INTEGER, key, token[i] );
00467                             }
00468                         }
00469 
00470                 /* Keyword: OMEGA */
00471                 else if(!strncmp(key,"OMEG",4) && ntok >= 1 )
00472                         for(i=0;i<ntok;i++) {
00473                             if ( strstr( token[i], "STA" ) ) {
00474                                 i++;
00475                                 if ( InputType( token[i] ) >= INTEGER ) {
00476                                         h.omebeg = atof( token[i] );
00477                                 }
00478                                 else
00479                                         WrongType( FLOAT, key, token[i] );
00480                             }
00481                             else if ( strstr( token[i], "END" ) ) {
00482                                 i++;
00483                                 if ( InputType( token[i] ) >= INTEGER ) {
00484                                         h.omeend = atof( token[i] );
00485                                 }
00486                                 else
00487                                         WrongType( FLOAT, key, token[i] );
00488                             }
00489                             else if ( strstr( token[i], "OSC" ) ) {
00490                                 i++;
00491                                 if ( InputType( token[i] ) == INTEGER ) {
00492                                         h.omeosc = atoi( token[i] );
00493                                 }
00494                                 else
00495                                         WrongType( INTEGER, key, token[i] );
00496                             }
00497                         }
00498 
00499                 /* Keyword: TWOTHETA */
00500                 else if( !strncmp(key,"TWOT",4) && ntok >= 0 ) 
00501                         if ( InputType( token[0] ) >= INTEGER ) 
00502                                 h.theta = atof( token[0] );
00503                         else
00504                                 WrongType( FLOAT, key, token[0] );
00505 
00506                 /* Keyword: CHI */
00507                 else if( !strncmp(key,"CHI",3) && ntok >= 0 ) 
00508                         if ( InputType( token[0] ) >= INTEGER ) 
00509                                 h.chi   = atof( token[0] );
00510                         else
00511                                 WrongType( FLOAT, key, token[0] );
00512 
00513                 /* Keyword: RESOLUTION */
00514                 else if( !strncmp(key,"RESO",4) && ntok >= 0 ) 
00515                         if ( InputType( token[0] ) >= INTEGER ) 
00516                                 h.resol = atof( token[0] );
00517                         else
00518                                 WrongType( FLOAT, key, token[0] );
00519 
00520                 /* Keyword: TIME */
00521                 else if( !strncmp(key,"TIME",4) && ntok >= 0 ) 
00522                         if ( InputType( token[0] ) >= INTEGER ) 
00523                                 h.time  = atof( token[0] );
00524                         else
00525                                 WrongType( FLOAT, key, token[0] );
00526 
00527                 /* Keyword: CENTER */
00528                 else if( !strncmp(key,"CENT",4) && ntok >= 1 ) {
00529                         for(i=0;i<ntok;i++) {
00530                             if ( strstr( token[i], "X" ) ) {
00531                                 i++;
00532                                 if ( InputType( token[i] ) >= INTEGER ) {
00533                                         h.xcen = atof( token[i] );
00534                                 }
00535                                 else
00536                                         WrongType( FLOAT, key, token[i] );
00537                             }
00538                             else if ( strstr( token[i], "Y" ) ) {
00539                                 i++;
00540                                 if ( InputType( token[i] ) >= INTEGER ) {
00541                                         h.ycen = atof( token[i] );
00542                                 }
00543                                 else
00544                                         WrongType( FLOAT, key, token[i] );
00545                             }
00546                         }
00547                 }
00548                 
00549                 /* Keyword: COLLIMATOR, SLITS */
00550                 else if( ( !strncmp(key,"COLL",4) || !strncmp(key,"SLIT",4) )&& ntok >= 1 ) {
00551                         for(i=0;i<ntok;i++) {
00552                             if ( strstr( token[i], "WID" ) ) {
00553                                 i++;
00554                                 if ( InputType( token[i] ) >= INTEGER ) {
00555                                         h.slitx= atof( token[i] );
00556                                 }
00557                                 else
00558                                         WrongType( FLOAT, key, token[i] );
00559                             }
00560                             else if ( strstr( token[i], "HEI" ) ) {
00561                                 i++;
00562                                 if ( InputType( token[i] ) >= INTEGER ) {
00563                                         h.slity = atof( token[i] );
00564                                 }
00565                                 else
00566                                         WrongType( FLOAT, key, token[i] );
00567                             }
00568                         }
00569                 }
00570                 
00571                 /* Keyword: GENERATOR */
00572                 else if( !strncmp(key,"GENE",4) && ntok >= 0 ) {
00573                     for(i=0;i<ntok;i++) {
00574                         if ( strstr( token[i], "MA" ) ) {
00575                                 i++;
00576                                 if ( InputType( token[i] ) >= INTEGER ) {
00577                                         h.mA = atof( token[i] );
00578                                 }
00579                                 else
00580                                         WrongType( FLOAT, key, token[i] );
00581                         }
00582                         else if ( strstr( token[i], "KV" ) ) {
00583                                 i++;
00584                                 if ( InputType( token[i] ) >= INTEGER ) {
00585                                         h.kV = atof( token[i] );
00586                                 }
00587                                 else
00588                                         WrongType( FLOAT, key, token[i] );
00589                         }
00590                         else
00591                                 strcat( h.source, token[i] );
00592                     }
00593                 }
00594 
00595                 /* Keyword: INTENSITY */
00596                 else if( !strncmp(key,"INTE",4) && ntok >= 0 ) {
00597                     for(i=0;i<ntok;i++) {
00598                         if ( strstr( token[i], "MIN" ) ) {
00599                                 i++;
00600                                 if ( InputType( token[i] ) == INTEGER ) {
00601                                         h.valmin = atoi( token[i] );
00602                                 }
00603                                 else
00604                                         WrongType( INTEGER, key, token[i] );
00605                         }
00606                         else if ( strstr( token[i], "MAX" ) ) {
00607                                 i++;
00608                                 if ( InputType( token[i] ) == INTEGER ) {
00609                                         h.valmax = atoi( token[i] );
00610                                 }
00611                                 else
00612                                         WrongType( INTEGER, key, token[i] );
00613                         }
00614                         else if ( strstr( token[i], "AVE" ) ) {
00615                                 i++;
00616                                 if ( InputType( token[i] ) >= INTEGER ) {
00617                                         h.valavg = atof( token[i] );
00618                                 }
00619                                 else
00620                                         WrongType( FLOAT, key, token[i] );
00621                         }
00622                         else if ( strstr( token[i], "SIG" ) ) {
00623                                 i++;
00624                                 if ( InputType( token[i] ) >= INTEGER ) {
00625                                         h.valsig = atof( token[i] );
00626                                 }
00627                                 else
00628                                         WrongType( FLOAT, key, token[i] );
00629                         }
00630                     }
00631                 }
00632                 
00633                 /* Keyword: HISTOGRAM */
00634                 else if( !strncmp(key,"HIST",4) && ntok >= 0 ) {
00635                     for(i=0;i<ntok;i++) {
00636                         if ( strstr( token[i], "STA" ) ) {
00637                                 i++;
00638                                 if ( InputType( token[i] ) == INTEGER ) {
00639                                         h.histbeg = atoi( token[i] );
00640                                 }
00641                                 else
00642                                         WrongType( INTEGER, key, token[i] );
00643                         }
00644                         else if ( strstr( token[i], "END" ) ) {
00645                                 i++;
00646                                 if ( InputType( token[i] ) == INTEGER ) {
00647                                         h.histend = atoi( token[i] );
00648                                 }
00649                                 else
00650                                         WrongType( INTEGER, key, token[i] );
00651                         }
00652                         else if ( strstr( token[i], "MAX" ) ) {
00653                                 i++;
00654                                 if ( InputType( token[i] ) == INTEGER ) {
00655                                         h.histmax = atoi( token[i] );
00656                                 }
00657                                 else
00658                                         WrongType( INTEGER, key, token[i] );
00659                         }
00660                     }
00661                 }
00662                 
00663         } /* End of while loop */
00664 
00665         /*
00666          * End of input lines (while loop)
00667          */
00668 
00669         return( h );
00670 
00671 }
00672 
00673 /******************************************************************
00674  * Function: Setmar345Header
00675  ******************************************************************/
00676 MAR345_HEADER
00677 Setmar345Header()
00678 {
00679 MAR345_HEADER   h;
00680 int             i;
00681 
00682         h.byteorder             = 1234;
00683         h.wave                  = 1.541789;
00684         h.polar                 = 0.0;
00685         h.pixel_length          = 150.0;
00686         h.pixel_height          = 150.0;
00687         h.scanner               = 1;
00688         h.format                = 1;
00689         h.high                  = 0;
00690         h.size                  = 0;
00691         h.dist                  = 70.0;
00692         h.multiplier            = 1.0;
00693         h.mode                  = 1;
00694         h.time                  = 0.0;
00695         h.dosebeg               = 0.0;
00696         h.doseend               = 0.0;
00697         h.dosemin               = 0.0;
00698         h.dosemax               = 0.0;
00699         h.doseavg               = 0.0;
00700         h.dosesig               = 0.0;
00701         h.dosen                 = 0;
00702         h.phibeg                = 0.0;
00703         h.phiend                = 0.0;
00704         h.omebeg                = 0.0;
00705         h.omeend                = 0.0;
00706         h.phiosc                = 0;
00707         h.omeosc                = 0;
00708         h.theta                 = 0.0;
00709         h.chi                   = 0.0;
00710         h.gain                  = 1.0;
00711         h.xcen                  = 600.;
00712         h.ycen                  = 600.;
00713         h.kV                    = 40.0;
00714         h.mA                    = 50.0;
00715         h.valmin                = 0;
00716         h.valmax                = 0;
00717         h.valavg                = 0.0;
00718         h.valsig                = 0.0;
00719         h.histbeg               = 0;
00720         h.histend               = 0;
00721         h.histmax               = 0;
00722         h.roff                  = 0.0;
00723         h.toff                  = 0.0;
00724         h.slitx                 = 0.3;
00725         h.slity                 = 0.3;
00726         h.pixels                = 0;
00727         h.adc_A                 = -1;
00728         h.adc_B                 = -1;
00729         h.add_A                 = 0;
00730         h.add_B                 = 0;
00731 
00732         h.date[0]               = '\0';
00733         h.remark[0]             = '\0';
00734         strcpy( h.source, "" );
00735         strcpy( h.filter, "" );
00736 
00737         for ( i=0; i<N_GAPS; i++ )
00738                 h.gap[i]        = 0;
00739 
00740         return( h );
00741 }
00742 
00743 /******************************************************************
00744  * Function: Putmar345Header
00745  ******************************************************************/
00746 int
00747 Putmar345Header(int fd, MAR345_HEADER h)
00748 {
00749 int             i,k=0;
00750 int             io;
00751 int             head[32];
00752 int             byte_order=1234;
00753 time_t          now;
00754 char            c=' ', str[128];
00755 char            *mode[2]  ={"DOSE","TIME"};
00756 char            *format[3]={"IMAGE","PCK","SPIRAL"};
00757 char            *source[3]={"SEALED TUBE","ROTATING ANODE","SYNCHROTRON"};
00758 char            *filter[3]={"GRAPHITE",   "MIRRORS"       ,"FILTER"     };
00759 
00760         if ( fd < 0 )return(0);
00761 
00762         h.byteorder = 1234;
00763 
00764         time( &now);    
00765         sprintf( str, "%s", (char *)ctime( &now ));
00766         memcpy( h.date, str, 24 );
00767 
00768         lseek( fd, 0, SEEK_SET );
00769 
00770         /* Make some necessary checks */
00771         if ( h.mode   > 1 ) h.mode = 1;
00772         if ( h.format > 2 ) h.format = 1;
00773 
00774         /* First 32 longs contain: */
00775         memset( head, 0, 32*sizeof(int) );
00776 
00777         head[ 0]        = (int)( h.byteorder );
00778         head[ 1]        = (int)( h.size );
00779         head[ 2]        = (int)( h.high );
00780         head[ 3]        = (int)( h.format );
00781         head[ 4]        = (int)( h.mode );
00782         head[ 5]        = (int)( h.pixels );
00783         head[ 6]        = (int)( h.pixel_length );
00784         head[ 7]        = (int)( h.pixel_height );
00785         head[ 8]        = (int)( h.wave   * 1000000. );
00786         head[ 9]        = (int)( h.dist   * 1000. );
00787         head[10]        = (int)( h.phibeg * 1000. );
00788         head[11]        = (int)( h.phiend * 1000. );
00789         head[12]        = (int)( h.omebeg * 1000. );
00790         head[13]        = (int)( h.omeend * 1000. );
00791         head[14]        = (int)( h.chi    * 1000. );
00792         head[15]        = (int)( h.theta  * 1000. );
00793 
00794 
00795         /* First 4 bytes: integer with 1234 */
00796         io = write( fd, head, 32*sizeof(int) );
00797 
00798         sprintf( str, "mar research"); 
00799         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00800         io += write( fd, str, 64); k++;
00801 
00802         sprintf( str, "PROGRAM        %s Version %s",h.program,h.version); 
00803         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00804         io += write( fd, str, 64); k++;
00805 
00806         sprintf( str, "DATE           %s",h.date); 
00807         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00808         io += write( fd, str, 64 ); k++;
00809 
00810         sprintf( str, "SCANNER        %d\n",h.scanner); 
00811         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00812         io += write( fd, str, 64 ); k++;
00813 
00814         sprintf( str, "FORMAT         %d  %s %d\n",h.size,format[(int)h.format],h.pixels); 
00815         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00816         io += write( fd, str, 64 ); k++;
00817 
00818         sprintf( str, "HIGH           %d\n",h.high); 
00819         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00820         io += write( fd, str, 64 ); k++;
00821 
00822         sprintf( str, "PIXEL          LENGTH %1.0f  HEIGHT %1.0f\n",h.pixel_length,h.pixel_height); 
00823         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00824         io += write( fd, str, 64 ); k++;
00825 
00826         sprintf( str, "OFFSET         ROFF %1.0f  TOFF %1.0f\n",h.roff,h.toff); 
00827         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00828         io += write( fd, str, 64 ); k++;
00829 
00830         sprintf( str, "GAPS           %d %d %d %d %d %d %d %d\n",
00831                 h.gap[0],h.gap[1],h.gap[2],h.gap[3],h.gap[4],h.gap[5],h.gap[6],h.gap[7]);
00832         
00833         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00834         io += write( fd, str, 64 ); k++;
00835 
00836         if ( h.adc_A >= 0 ) {
00837                 sprintf( str, "ADC            A %d  B %d  ADD_A %d  ADD_B %d\n",h.adc_A,h.adc_B,h.add_A,h.add_B); 
00838                 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00839                 io += write( fd, str, 64 ); k++;
00840         }
00841 
00842         sprintf( str, "MULTIPLIER     %1.3f\n",h.multiplier); 
00843         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00844         io += write( fd, str, 64 ); k++;
00845 
00846         sprintf( str, "GAIN           %1.3f\n",h.gain); 
00847         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00848         io += write( fd, str, 64 ); k++;
00849 
00850         sprintf( str, "WAVELENGTH     %1.5f\n",h.wave); 
00851         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00852         io += write( fd, str, 64 ); k++;
00853 
00854         sprintf( str, "DISTANCE       %1.3f\n",h.dist); 
00855         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00856         io += write( fd, str, 64 ); k++;
00857 
00858         sprintf( str, "RESOLUTION     %1.3f\n",h.resol); 
00859         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00860         io += write( fd, str, 64 ); k++;
00861 
00862         sprintf( str, "PHI            START %1.3f  END %1.3f  OSC %d\n",h.phibeg,h.phiend,h.phiosc); 
00863         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00864         io += write( fd, str, 64 ); k++;
00865 
00866         sprintf( str, "OMEGA          START %1.3f  END %1.3f  OSC %d\n",h.omebeg,h.omeend,h.omeosc); 
00867         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00868         io += write( fd, str, 64 ); k++;
00869 
00870         sprintf( str, "CHI            %1.3f\n",h.chi); 
00871         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00872         io += write( fd, str, 64 ); k++;
00873 
00874         sprintf( str, "TWOTHETA       %1.3f\n",h.theta); 
00875         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00876         io += write( fd, str, 64 ); k++;
00877 
00878         sprintf( str, "CENTER         X %1.3f  Y %1.3f\n",h.xcen,h.ycen); 
00879         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00880         io += write( fd, str, 64 ); k++;
00881 
00882         sprintf( str, "MODE           %s\n",mode[(int)h.mode]); 
00883         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00884         io += write( fd, str, 64 ); k++;
00885 
00886         sprintf( str, "TIME           %1.2f\n",h.time); 
00887         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00888         io += write( fd, str, 64 ); k++;
00889 
00890         sprintf( str, "COUNTS         START %1.2f END %1.2f  NMEAS %d\n",h.dosebeg,h.doseend,h.dosen); 
00891         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00892         io += write( fd, str, 64 ); k++;
00893 
00894         sprintf( str, "COUNTS         MIN %1.2f  MAX %1.2f\n",h.dosemin,h.dosemax); 
00895         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00896         io += write( fd, str, 64 ); k++;
00897 
00898         sprintf( str, "COUNTS         AVE %1.2f  SIG %1.2f\n",h.doseavg,h.dosesig); 
00899         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00900         io += write( fd, str, 64 ); k++;
00901 
00902         sprintf( str, "INTENSITY      MIN %d  MAX %d  AVE %1.1f  SIG %1.2f\n",h.valmin,h.valmax,h.valavg,h.valsig); 
00903         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00904         io += write( fd, str, 64 ); k++;
00905 
00906         sprintf( str, "HISTOGRAM      START %d  END %d  MAX %d\n",h.histbeg,h.histend,h.histmax); 
00907         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00908         io += write( fd, str, 64 ); k++;
00909 
00910         sprintf( str, "GENERATOR      %s  kV %1.1f  mA %1.1f\n",h.source,h.kV, h.mA); 
00911         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00912         io += write( fd, str, 64 ); k++;
00913 
00914         sprintf( str, "MONOCHROMATOR  %s  POLAR %1.3f\n",h.filter,h.polar); 
00915         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00916         io += write( fd, str, 64 ); k++;
00917 
00918         sprintf( str, "COLLIMATOR     WIDTH %1.2f  HEIGHT %1.2f\n",h.slitx,h.slity); 
00919         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00920         io += write( fd, str, 64 ); k++;
00921 
00922         sprintf( str, "REMARK         %s\n",h.remark); 
00923         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00924         io += write( fd, str, 64 ); k++;
00925 
00926         sprintf( str, "END OF HEADER\n"); 
00927         for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00928         io += write( fd, str, 64 ); k++;
00929 
00930         /*
00931          * Fill rest of header (4096 bytes) with blanks:
00932          */
00933         k = 4096;
00934         while ( lseek( fd, 0, SEEK_CUR ) < k ) {
00935                 i = write( fd, &c, 1 ); 
00936                 if ( i < 1 ) break;
00937                 io += i;
00938         }
00939 
00940         if ( io < k ) {
00941                 fprintf( stderr, "ERROR: only %d out of %d bytes written into image header!!!\n",io,k);
00942                 return 0;
00943         }
00944 
00945         return 1;
00946 
00947 }