My Project
ecat63p.c
Go to the documentation of this file.
1 /******************************************************************************
2 
3  Copyright (c) 2003-2008 Turku PET Centre
4 
5  Library: ecat63p.c
6  Description: Procedures for printing ECAT 6.3 header contents.
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  See the GNU Lesser General Public License for more details:
17  http://www.gnu.org/copyleft/lesser.html
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with this library/program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  Turku PET Centre, Turku, Finland, http://www.turkupetcentre.fi/
24 
25  Modification history:
26  2003-07-21 Vesa Oikonen
27  Contents separated from ecat63.c.
28  2004-02-08 VO
29  ecat63PrintMainheader() prints also sw_version.
30  2004-09-20 VO
31  Doxygen style comments.
32  2007-03-12 VO
33  Header print functions changed: file pointer is required as second
34  argument, set to stdout to make it work as before;
35  header contents are now printed one value per line.
36  2008-07-24 VO
37  Added ecat63PrintSubheader().
38 
39 
40 ******************************************************************************/
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <math.h>
44 #include <ctype.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <time.h>
48 /*****************************************************************************/
49 #include "swap.h"
50 #include "include/ecat63.h"
51 /*****************************************************************************/
52 
53 /*****************************************************************************/
54 
55 /*****************************************************************************/
63  char tmp[32];
64 
65  if(ECAT63_TEST) fprintf(stdout, "ecat63PrintMainheader()\n");
66  fprintf(fp, "original_file_name := %.20s\n", h->original_file_name);
67  fprintf(fp, "data_type := %d (%s)\n", h->data_type, ecat63Datatype(h->data_type));
68  fprintf(fp, "system_type := %d\n", h->system_type);
69  if(h->file_type==1) strcpy(tmp, "sinogram");
70  else if(h->file_type==2) strcpy(tmp, "image");
71  else if(h->file_type==3) strcpy(tmp, "attenuation");
72  else if(h->file_type==4) strcpy(tmp, "normalization");
73  else strcpy(tmp, "unknown");
74  fprintf(fp, "file_type := %d (%s)\n", h->file_type, tmp);
75  fprintf(fp, "sw_version := %d\n", h->sw_version);
76  fprintf(fp, "scan start := %02d.%02d.%04d %02d:%02d:%02d\n",
79  fprintf(fp, "isotope_code := %.8s\n", h->isotope_code);
80  fprintf(fp, "isotope_halflife := %E sec\n", h->isotope_halflife);
81  fprintf(fp, "radiopharmaceutical := %.32s\n", h->radiopharmaceutical);
82  fprintf(fp, "gantry_tilt := %g\n", h->gantry_tilt);
83  fprintf(fp, "gantry_rotation := %g\n", h->gantry_rotation);
84  fprintf(fp, "bed_elevation := %g\n", h->bed_elevation);
85  fprintf(fp, "axial_fov := %g\n", h->axial_fov);
86  fprintf(fp, "transaxial_fov := %g\n", h->transaxial_fov);
87  fprintf(fp, "calibration_factor := %g\n", h->calibration_factor);
88  fprintf(fp, "calibration_units := %d (%s)\n", h->calibration_units, ecat63Unit(h->calibration_units));
89  fprintf(fp, "study_name := %.12s\n", h->study_name);
90  fprintf(fp, "patient_id := %.32s\n", h->patient_id);
91  fprintf(fp, "patient_name := %.32s\n", h->patient_name);
92  fprintf(fp, "sex := %c\nage := %.10s\nheight := %.10s\nweigth := %.10s\ndexterity := %c\n",
95  fprintf(fp, "physician_name := %.32s\noperator_name := %.32s\n",
97  fprintf(fp, "study_description := %.32s\n", h->study_description);
98  fprintf(fp, "num_planes := %d\n", h->num_planes);
99  fprintf(fp, "num_frames := %d\n", h->num_frames);
100  fprintf(fp, "num_gates := %d\n", h->num_gates);
101  fprintf(fp, "num_bed_pos := %d\n", h->num_bed_pos);
102  fprintf(fp, "init_bed_position := %g\n", h->init_bed_position);
103  fprintf(fp, "plane_separation := %g cm\n", h->plane_separation);
104  fprintf(fp, "user_process_code := %.10s\n", h->user_process_code);
105 }
106 /*****************************************************************************/
107 
108 /*****************************************************************************/
116  int i;
117 
118  if(ECAT63_TEST) printf("ecat63PrintImageheader()\n");
119  fprintf(fp, "data_type := %d (%s)\n", h->data_type, ecat63Datatype(h->data_type));
120  fprintf(fp, "dimension_1 := %d\n", h->dimension_1);
121  fprintf(fp, "dimension_2 := %d\n", h->dimension_2);
122  fprintf(fp, "x_origin := %g\ny_origin := %g\nrecon_scale := %g\n",
123  h->x_origin, h->y_origin, h->recon_scale);
124  fprintf(fp, "quant_scale := %g\nimage_min := %d\nimage_max := %d\n",
125  h->quant_scale, h->image_min, h->image_max);
126  fprintf(fp, "slice_width := %g\npixel_size := %g\n", h->slice_width, h->pixel_size);
127  fprintf(fp, "frame_start_time := %d\nframe_duration := %d\n",
129  fprintf(fp, "reconstruction_start := %02d.%02d.%04d %02d:%02d:%02d\n",
132  fprintf(fp, "filter_code := %d\nimage_rotation := %g\nintrinsic_tilt := %g\n",
134  fprintf(fp, "filter_params :=");
135  for(i=0; i<6; i++) fprintf(fp, " %g", h->filter_params[i]); fprintf(fp, "\n");
136  fprintf(fp, "plane_eff_corr_fctr := %g\ndecay_corr_fctr := %g\nloss_corr_fctr := %g\n",
138  fprintf(fp, "quant_units := %d (%s)\n", h->quant_units, ecat63Unit(h->quant_units));
139  fprintf(fp, "ecat_calibration_fctr := %g\nwell_counter_cal_fctr := %g\n",
141  fprintf(fp, "annotation := %.40s\n", h->annotation);
142 }
143 /*****************************************************************************/
144 
145 /*****************************************************************************/
153  int i;
154 
155  if(ECAT63_TEST) printf("ecat63PrintScanheader()\n");
156  fprintf(fp, "data_type := %d (%s)\n", h->data_type, ecat63Datatype(h->data_type));
157  fprintf(fp, "dimension_1 := %d\n", h->dimension_1);
158  fprintf(fp, "dimension_2 := %d\n", h->dimension_2);
159  fprintf(fp, "sample_distance := %g cm\n", h->sample_distance);
160  fprintf(fp, "isotope_halflife := %g sec\n", h->isotope_halflife);
161  fprintf(fp, "gate_duration := %d\nr_wave_offset := %d\n",
163  fprintf(fp, "scale_factor := %g\n", h->scale_factor);
164  fprintf(fp, "scan_min := %d\nscan_max := %d\n", h->scan_min, h->scan_max);
165  fprintf(fp, "prompts := %d\ndelayed := %d\nmultiples := %d\nnet_trues := %d\n",
166  h->prompts, h->delayed, h->multiples, h->net_trues);
167  fprintf(fp, "cor_singles :=");
168  for(i=0; i<16; i++) fprintf(fp, " %8.0f", h->cor_singles[i]); printf("\n");
169  fprintf(fp, "uncor_singles :=");
170  for(i=0; i<16; i++) fprintf(fp, " %8.0f", h->uncor_singles[i]); printf("\n");
171  fprintf(fp, "tot_avg_cor := %g\ntot_avg_uncor := %g\n", h->tot_avg_cor, h->tot_avg_uncor);
172  fprintf(fp, "total_coin_rate := %d\n", h->total_coin_rate);
173  fprintf(fp, "frame_start_time := %d\nframe_duration := %d\n",
175  fprintf(fp, "loss_correction_fctr := %g\n", h->loss_correction_fctr);
176 }
177 /*****************************************************************************/
178 
179 /*****************************************************************************/
187  if(ECAT63_TEST) printf("ecat63PrintAttnheader()\n");
188  fprintf(fp, "data_type := %d (%s)\n", h->data_type, ecat63Datatype(h->data_type));
189  fprintf(fp, "dimension_1 := %d\n", h->dimension_1);
190  fprintf(fp, "dimension_2 := %d\n", h->dimension_2);
191  fprintf(fp, "sample_distance := %g cm\n", h->sample_distance);
192  fprintf(fp, "attenuation_type := %d\n", h->attenuation_type);
193  fprintf(fp, "scale_factor := %g\n", h->scale_factor);
194  fprintf(fp, "x_origin := %g\ny_origin := %g\nx_radius := %g\ny_radius := %g\n",
195  h->x_origin, h->y_origin, h->x_radius, h->y_radius);
196  fprintf(fp, "tilt_angle := %g\nattenuation_coeff := %g\n",
198 }
199 /*****************************************************************************/
200 
201 /*****************************************************************************/
209  if(ECAT63_TEST) printf("ecat63PrintNormheader()\n");
210  fprintf(fp, "data_type := %d (%s)\n", h->data_type, ecat63Datatype(h->data_type));
211  fprintf(fp, "dimension_1 := %d\n", h->dimension_1);
212  fprintf(fp, "dimension_2 := %d\n", h->dimension_2);
213  fprintf(fp, "scale_factor := %g\n", h->scale_factor);
214  fprintf(fp, "norm time := %02d.%02d.%04d %02d:%02d:%02d\n",
215  h->norm_day, h->norm_month, h->norm_year,
216  h->norm_hour, h->norm_minute, h->norm_second);
217 }
218 /*****************************************************************************/
219 
220 /*****************************************************************************/
227 char *ecat63Datatype(short int dtype) {
228  static char *ecat63_datatype[]={
229  /* 0 */ "Unknown",
230  /* 1 */ "BYTE_TYPE",
231  /* 2 */ "VAX_I2",
232  /* 3 */ "VAX_I4",
233  /* 4 */ "VAX_R4",
234  /* 5 */ "IEEE_R4",
235  /* 6 */ "SUN_I2",
236  /* 7 */ "SUN_I4",
237  /* 8 */ "Unknown",
238  /* 9 */ "Unknown"
239  };
240  if(dtype>=0 && dtype<10) return(ecat63_datatype[dtype]);
241  else return(ecat63_datatype[0]);
242 }
243 
250 char *ecat63Unit(short int dunit) {
251  static char *ecat63_unit[]={
252  /* 0 */ "Unknown",
253  /* 1 */ "Unknown",
254  /* 2 */ "ECAT counts",
255  /* 3 */ "uCi/ml",
256  /* 4 */ "LMRGlu",
257  /* 5 */ "LMRUGlu umol/min/100g",
258  /* 6 */ "LMRUGlu mg/min/100g",
259  /* 7 */ "nCi/mL",
260  /* 8 */ "Well counts",
261  /* 9 */ "Becquerels",
262  /* 10 */ "kBq/mL",
263  /* 11 */ "1/min",
264  /* 12 */ "mL/min/100g",
265  /* 13 */ "sec*kBq/mL",
266  /* 14 */ "sec*nCi/mL",
267  /* 15 */ "1/sec",
268  /* 16 */ "Unitless",
269  /* 17 */ "Unknown"
270  };
271  if(dunit>=0 && dunit<18) return(ecat63_unit[dunit]);
272  else return(ecat63_unit[0]);
273 }
274 /*****************************************************************************/
282 void float2parts(float *buf) {
283  unsigned int u, exp, mantissa;
284  char sign;
285 
286  memcpy(&u, buf, 4); if(u & 1L<<31) sign='-'; else sign='+';
287  exp=u<<1; exp=exp>>24; mantissa=u<<9; mantissa=mantissa>>9;
288  printf("%e = %c (%u/8388608 + 1)*2^(%u-127)\n", *buf, sign, mantissa, exp);
289 }
290 /*****************************************************************************/
291 
292 /*****************************************************************************/
300  FILE *fp,
302  int plane,
304  int frame,
306  FILE *ofp
307 ) {
308  int mi, ret, nr=0;
309  static MATRIXLIST mlist;
310  ECAT63_imageheader image_header;
311  ECAT63_scanheader scan_header;
312  ECAT63_attnheader attn_header;
313  ECAT63_normheader norm_header;
314  Matval matval;
315 
316 
317  /* Read matrix list and nr */
318  ecat63InitMatlist(&mlist);
319  ret=ecat63ReadMatlist(fp, &mlist);
320  if(ret) {
321  fprintf(stderr, "Error (%d): cannot read matrix list.\n", ret);
322  return 2;
323  }
324  if(mlist.matrixNr<=0) {
325  fprintf(stderr, "Error: matrix list is empty.\n");
326  return 2;
327  }
328  if(ECAT63_TEST>1) ecat63PrintMatlist(&mlist);
329 
330  /*
331  * Read and print subheaders one at a time
332  */
333  for(mi=nr=0; mi<mlist.matrixNr; mi++) {
334  /* Get plane and frame nr */
335  mat_numdoc(mlist.matdir[mi].matnum, &matval);
336  /* Check if this is supposed to be listed or not */
337  if(frame>=0 && frame!=matval.frame) continue;
338  if(plane>=0 && plane!=matval.plane) continue;
339  /* Read subheader */
340  if(mh.file_type==IMAGE_DATA)
341  ret=ecat63ReadImageheader(fp, mlist.matdir[mi].strtblk, &image_header);
342  else if(mh.file_type==RAW_DATA)
343  ret=ecat63ReadScanheader(fp, mlist.matdir[mi].strtblk, &scan_header);
344  else if(mh.file_type==ATTN_DATA)
345  ret=ecat63ReadAttnheader(fp, mlist.matdir[mi].strtblk, &attn_header);
346  else if(mh.file_type==NORM_DATA)
347  ret=ecat63ReadNormheader(fp, mlist.matdir[mi].strtblk, &norm_header);
348  if(ret) {
349  fprintf(stderr, "Error: cannot read matrix %u subheader.\n",
350  mlist.matdir[mi].matnum);
351  ecat63EmptyMatlist(&mlist); return 4;
352  }
353  /* Print subheader */
354  fprintf(fp, "Matrix: plane %d frame %d gate %d bed %d\n",
355  matval.plane, matval.frame, matval.gate, matval.bed);
356  if(mh.file_type==IMAGE_DATA)
357  ecat63PrintImageheader(&image_header, ofp);
358  else if(mh.file_type==RAW_DATA)
359  ecat63PrintScanheader(&scan_header, ofp);
360  else if(mh.file_type==ATTN_DATA)
361  ecat63PrintAttnheader(&attn_header, ofp);
362  else if(mh.file_type==NORM_DATA)
363  ecat63PrintNormheader(&norm_header, ofp);
364  nr++; // counter
365  } /* next matrix */
366  ecat63EmptyMatlist(&mlist);
367 
368  if(nr==0 && (plane>=0 || frame>=0)) {
369  fprintf(stderr, "Error: specified matrices not found.\n");
370  return(11);
371  }
372 
373  return(0);
374 }
375 /*****************************************************************************/
376 
377 /*****************************************************************************/
378 
#define ATTN_DATA
Definition: ecat63.h:40
int ECAT63_TEST
Definition: ecat63.h:52
#define RAW_DATA
Definition: ecat63.h:38
#define NORM_DATA
Definition: ecat63.h:41
#define IMAGE_DATA
Definition: ecat63.h:39
void ecat63InitMatlist(MATRIXLIST *mlist)
Definition: ecat63ml.c:69
void ecat63EmptyMatlist(MATRIXLIST *mlist)
Definition: ecat63ml.c:80
int ecat63ReadMatlist(FILE *fp, MATRIXLIST *ml)
Definition: ecat63ml.c:97
void ecat63PrintMatlist(MATRIXLIST *ml)
Definition: ecat63ml.c:160
void mat_numdoc(int matnum, Matval *matval)
Definition: ecat63ml.c:276
void ecat63PrintMainheader(ECAT63_mainheader *h, FILE *fp)
Definition: ecat63p.c:62
char * ecat63Unit(short int dunit)
Definition: ecat63p.c:250
void ecat63PrintImageheader(ECAT63_imageheader *h, FILE *fp)
Definition: ecat63p.c:115
void ecat63PrintScanheader(ECAT63_scanheader *h, FILE *fp)
Definition: ecat63p.c:152
void ecat63PrintNormheader(ECAT63_normheader *h, FILE *fp)
Definition: ecat63p.c:208
void float2parts(float *buf)
Definition: ecat63p.c:282
char * ecat63Datatype(short int dtype)
Definition: ecat63p.c:227
void ecat63PrintAttnheader(ECAT63_attnheader *h, FILE *fp)
Definition: ecat63p.c:186
int ecat6PrintSubheader(ECAT63_mainheader mh, FILE *fp, int plane, int frame, FILE *ofp)
Definition: ecat63p.c:296
int ecat63ReadNormheader(FILE *fp, int blk, ECAT63_normheader *h)
Definition: ecat63r.c:375
int ecat63ReadScanheader(FILE *fp, int blk, ECAT63_scanheader *h)
Definition: ecat63r.c:296
int ecat63ReadAttnheader(FILE *fp, int blk, ECAT63_attnheader *h)
Definition: ecat63r.c:238
int ecat63ReadImageheader(FILE *fp, int blk, ECAT63_imageheader *h)
Definition: ecat63r.c:152
MatDir * matdir
Definition: ecat63.h:65
int matrixNr
Definition: ecat63.h:63
int matnum
Definition: ecat63.h:56
int strtblk
Definition: ecat63.h:57
Definition: ecat63.h:68
int bed
Definition: ecat63.h:69
int frame
Definition: ecat63.h:69
int gate
Definition: ecat63.h:69
int plane
Definition: ecat63.h:69
float tilt_angle
Definition: ecat63.h:158
float attenuation_coeff
Definition: ecat63.h:159
float x_origin
Definition: ecat63.h:158
float x_radius
Definition: ecat63.h:158
float y_origin
Definition: ecat63.h:158
float scale_factor
Definition: ecat63.h:157
float sample_distance
Definition: ecat63.h:160
float y_radius
Definition: ecat63.h:158
short int data_type
Definition: ecat63.h:155
short int dimension_1
Definition: ecat63.h:156
short int dimension_2
Definition: ecat63.h:156
short int attenuation_type
Definition: ecat63.h:155
short int data_type
Definition: ecat63.h:107
short int recon_start_day
Definition: ecat63.h:120
int frame_start_time
Definition: ecat63.h:111
short int quant_units
Definition: ecat63.h:119
float decay_corr_fctr
Definition: ecat63.h:118
float well_counter_cal_fctr
Definition: ecat63.h:121
short int image_max
Definition: ecat63.h:109
float pixel_size
Definition: ecat63.h:110
float image_rotation
Definition: ecat63.h:117
char annotation[40]
Definition: ecat63.h:122
float plane_eff_corr_fctr
Definition: ecat63.h:117
short int dimension_1
Definition: ecat63.h:107
short int recon_start_month
Definition: ecat63.h:120
float recon_scale
Definition: ecat63.h:108
float ecat_calibration_fctr
Definition: ecat63.h:121
float loss_corr_fctr
Definition: ecat63.h:118
float slice_width
Definition: ecat63.h:110
float quant_scale
Definition: ecat63.h:108
short int dimension_2
Definition: ecat63.h:107
short int image_min
Definition: ecat63.h:109
short int filter_code
Definition: ecat63.h:115
short int recon_start_hour
Definition: ecat63.h:113
short int recon_start_sec
Definition: ecat63.h:113
float intrinsic_tilt
Definition: ecat63.h:118
short int recon_start_year
Definition: ecat63.h:120
short int recon_start_min
Definition: ecat63.h:113
float filter_params[6]
Definition: ecat63.h:121
short int num_frames
Definition: ecat63.h:97
short int scan_start_minute
Definition: ecat63.h:81
short int scan_start_hour
Definition: ecat63.h:81
short int scan_start_day
Definition: ecat63.h:80
float gantry_rotation
Definition: ecat63.h:85
char patient_age[10]
Definition: ecat63.h:92
float transaxial_fov
Definition: ecat63.h:87
char patient_weight[10]
Definition: ecat63.h:92
float gantry_tilt
Definition: ecat63.h:85
short int sw_version
Definition: ecat63.h:75
char patient_sex
Definition: ecat63.h:91
short int num_gates
Definition: ecat63.h:97
char radiopharmaceutical[32]
Definition: ecat63.h:84
char study_description[32]
Definition: ecat63.h:94
float bed_elevation
Definition: ecat63.h:85
short int data_type
Definition: ecat63.h:76
char patient_name[32]
Definition: ecat63.h:91
short int system_type
Definition: ecat63.h:77
char physician_name[32]
Definition: ecat63.h:93
float init_bed_position
Definition: ecat63.h:98
char original_file_name[20]
Definition: ecat63.h:74
float isotope_halflife
Definition: ecat63.h:83
char patient_dexterity
Definition: ecat63.h:93
float calibration_factor
Definition: ecat63.h:89
char operator_name[32]
Definition: ecat63.h:93
char patient_id[16]
Definition: ecat63.h:91
short int scan_start_year
Definition: ecat63.h:80
short int scan_start_month
Definition: ecat63.h:80
float axial_fov
Definition: ecat63.h:87
char patient_height[10]
Definition: ecat63.h:92
char isotope_code[8]
Definition: ecat63.h:82
short int calibration_units
Definition: ecat63.h:90
short int num_bed_pos
Definition: ecat63.h:97
char study_name[12]
Definition: ecat63.h:91
short int scan_start_second
Definition: ecat63.h:81
char user_process_code[10]
Definition: ecat63.h:101
short int file_type
Definition: ecat63.h:78
short int num_planes
Definition: ecat63.h:97
float plane_separation
Definition: ecat63.h:98
short int norm_month
Definition: ecat63.h:150
short int norm_second
Definition: ecat63.h:150
short int norm_year
Definition: ecat63.h:150
short int dimension_1
Definition: ecat63.h:148
short int norm_hour
Definition: ecat63.h:150
float scale_factor
Definition: ecat63.h:149
short int norm_minute
Definition: ecat63.h:150
short int dimension_2
Definition: ecat63.h:148
short int data_type
Definition: ecat63.h:147
short int norm_day
Definition: ecat63.h:150
float cor_singles[16]
Definition: ecat63.h:138
float uncor_singles[16]
Definition: ecat63.h:138
int frame_duration
Definition: ecat63.h:141
float loss_correction_fctr
Definition: ecat63.h:142
float isotope_halflife
Definition: ecat63.h:132
short int dimension_2
Definition: ecat63.h:129
short int scan_max
Definition: ecat63.h:136
int frame_start_time
Definition: ecat63.h:141
short int scan_min
Definition: ecat63.h:136
float sample_distance
Definition: ecat63.h:131
float scale_factor
Definition: ecat63.h:135
short int data_type
Definition: ecat63.h:128
int total_coin_rate
Definition: ecat63.h:140
short int dimension_1
Definition: ecat63.h:129
float tot_avg_uncor
Definition: ecat63.h:139
float tot_avg_cor
Definition: ecat63.h:139