imgunit.c
Go to the documentation of this file.
00001 /******************************************************************************
00002 
00003   Copyright (c) 2006-2011 Turku PET Centre
00004 
00005   Library:      imgunit
00006   Description:  Functions for setting image calibration unit.
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016   See the GNU Lesser General Public License for more details:
00017   http://www.gnu.org/copyleft/lesser.html
00018 
00019   You should have received a copy of the GNU Lesser General Public License
00020   along with this library/program; if not, write to the Free Software
00021   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
00022 
00023   Turku PET Centre, Turku, Finland, http://www.turkupetcentre.fi
00024 
00025   Modification history:
00026   2006-10-30 Vesa Oikonen
00027     First created.
00028   2007-02-02 VO
00029     imgUnitId() identifies more unit strings.
00030     Added more IMG units.
00031     Functions imgUnitFromEcat7() and imgUnit() rewritten.
00032   2007-17-07 Harri Merisaari
00033     Modified for optional ANSi compatibility
00034   2008-07-24 VO
00035     Added function imgSetUnit().
00036   2010-05-11 VO
00037     Function imgUnitFromEcat7() tries to identify unit with imgUnitId()
00038     always when ECAT 7 calibration_units is 2; previosly in these cases
00039     unit was assumed to be unknown.
00040   2011-02-04 VO
00041     imgUnitToEcat7() writes label "unitless" in the header, previosly header
00042     was marked to contain no data_units label.
00043 
00044 
00045 ******************************************************************************/
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <math.h>
00049 #include <time.h>
00050 #include <string.h>
00051 #include <strings.h>
00052 #include <ctype.h>
00053 #include <float.h>
00054 /*****************************************************************************/
00055 #include "img.h"
00056 #include "substitutions.h"
00057 /*****************************************************************************/
00058 #include "include/imgunit.h"
00059 /*****************************************************************************/
00061 static char *img_unit_string[] = {
00062   /*  0 */  "unknown",
00063   /*  1 */  "cnts/sec",
00064   /*  2 */  "counts",
00065   /*  3 */  "kBq/mL",
00066   /*  4 */  "sec*kBq/mL",
00067   /*  5 */  "1/sec",
00068   /*  6 */  "1/min",
00069   /*  7 */  "mL/mL",
00070   /*  8 */  "mL/dL",
00071   /*  9 */  "mL/(mL*min)",
00072   /* 10 */  "mL/(dL*min)",
00073   /* 11 */  "unitless",
00074   /* 12 */  "nCi/mL",
00075   /* 13 */  "MBq/mL",
00076   /* 14 */  "Bq/cc",
00077   /* 15 */  "uCi/cc",
00078   /* 16 */  "umol/(100g*min)",
00079   /* 17 */  "mg/(100g*min)",
00080             0
00081 };
00082 /*****************************************************************************/
00083 
00084 /*****************************************************************************/
00091 int imgUnitId(char *unit) {
00092   if(unit==NULL) return(-1);
00093   if(strlen(unit)==0)                             return IMGUNIT_UNKNOWN;
00094   else if(strcasecmp(unit, "unknown")==0)         return IMGUNIT_UNKNOWN;
00095   else if(strcasecmp(unit, "cnts/sec")==0)        return IMGUNIT_CPS;
00096   else if(strcasecmp(unit, "counts/sec")==0)      return IMGUNIT_CPS;
00097   else if(strcasecmp(unit, "ECAT counts/sec")==0) return IMGUNIT_CPS;
00098   else if(strcasecmp(unit, "cps")==0)             return IMGUNIT_CPS;
00099   else if(strcasecmp(unit, "counts")==0)          return IMGUNIT_COUNTS;
00100   else if(strcasecmp(unit, "cnts")==0)            return IMGUNIT_COUNTS;
00101   else if(strcasecmp(unit, "kBq/cc")==0)          return IMGUNIT_KBQ_PER_ML;
00102   else if(strcasecmp(unit, "kBq/mL")==0)          return IMGUNIT_KBQ_PER_ML;
00103   else if(strcasecmp(unit, "sec*kBq/cc")==0)      return IMGUNIT_SEC_KBQ_PER_ML;
00104   else if(strcasecmp(unit, "sec*kBq/mL")==0)      return IMGUNIT_SEC_KBQ_PER_ML;
00105   else if(strcasecmp(unit, "integral")==0)        return IMGUNIT_SEC_KBQ_PER_ML;
00106   else if(strcasecmp(unit, "1/sec")==0)           return IMGUNIT_PER_SEC;
00107   else if(strcasecmp(unit, "1/s")==0)             return IMGUNIT_PER_SEC;
00108   else if(strcasecmp(unit, "s-1")==0)             return IMGUNIT_PER_SEC;
00109   else if(strcasecmp(unit, "1/min")==0)           return IMGUNIT_PER_MIN;
00110   else if(strcasecmp(unit, "min-1")==0)           return IMGUNIT_PER_MIN;
00111   else if(strcasecmp(unit, "mL/mL")==0)           return IMGUNIT_ML_PER_ML;
00112   else if(strcasecmp(unit, "mL/cc")==0)           return IMGUNIT_ML_PER_ML;
00113   else if(strcasecmp(unit, "mL/dL")==0)           return IMGUNIT_ML_PER_DL;
00114   else if(strcasecmp(unit, "mL/100mL")==0)        return IMGUNIT_ML_PER_DL;
00115   else if(strcasecmp(unit, "mL/(mL*min)")==0)     return IMGUNIT_ML_PER_ML_PER_MIN;
00116   else if(strcasecmp(unit, "mL/(min*mL)")==0)     return IMGUNIT_ML_PER_ML_PER_MIN;
00117   else if(strcasecmp(unit, "mL/(cc*min)")==0)     return IMGUNIT_ML_PER_ML_PER_MIN;
00118   else if(strcasecmp(unit, "mL/(min*cc)")==0)     return IMGUNIT_ML_PER_ML_PER_MIN;
00119   else if(strcasecmp(unit, "mL/mL/min")==0)       return IMGUNIT_ML_PER_ML_PER_MIN;
00120   else if(strcasecmp(unit, "mL/min/mL")==0)       return IMGUNIT_ML_PER_ML_PER_MIN;
00121   else if(strcasecmp(unit, "mL/cc/min")==0)       return IMGUNIT_ML_PER_ML_PER_MIN;
00122   else if(strcasecmp(unit, "mL/min/cc")==0)       return IMGUNIT_ML_PER_ML_PER_MIN;
00123   else if(strcasecmp(unit, "mL/(dL*min)")==0)     return IMGUNIT_ML_PER_DL_PER_MIN;
00124   else if(strcasecmp(unit, "mL/(min*dL)")==0)     return IMGUNIT_ML_PER_DL_PER_MIN;
00125   else if(strcasecmp(unit, "mL/(100mL*min)")==0)  return IMGUNIT_ML_PER_DL_PER_MIN;
00126   else if(strcasecmp(unit, "mL/(min*100mL)")==0)  return IMGUNIT_ML_PER_DL_PER_MIN;
00127   else if(strcasecmp(unit, "mL/dL/min")==0)       return IMGUNIT_ML_PER_DL_PER_MIN;
00128   else if(strcasecmp(unit, "mL/min/dL")==0)       return IMGUNIT_ML_PER_DL_PER_MIN;
00129   else if(strcasecmp(unit, "mL/100mL/min")==0)    return IMGUNIT_ML_PER_DL_PER_MIN;
00130   else if(strcasecmp(unit, "mL/min/100mL")==0)    return IMGUNIT_ML_PER_DL_PER_MIN;
00131   else if(strcasecmp(unit, "unitless")==0)        return IMGUNIT_UNITLESS;
00132   else if(strcasecmp(unit, "nCi/cc")==0)          return IMGUNIT_NCI_PER_ML;
00133   else if(strcasecmp(unit, "nCi/mL")==0)          return IMGUNIT_NCI_PER_ML;
00134   else if(strcasecmp(unit, "MBq/cc")==0)          return IMGUNIT_MBQ_PER_ML;
00135   else if(strcasecmp(unit, "MBq/mL")==0)          return IMGUNIT_MBQ_PER_ML;
00136   else if(strcasecmp(unit, "Bq/cc")==0)           return IMGUNIT_BQ_PER_ML;
00137   else if(strcasecmp(unit, "Bq/mL")==0)           return IMGUNIT_BQ_PER_ML;
00138   else if(strcasecmp(unit, "uCi/cc")==0)          return IMGUNIT_UCI_PER_ML;
00139   else if(strcasecmp(unit, "uCi/mL")==0)          return IMGUNIT_UCI_PER_ML;
00140   else if(strcasecmp(unit, "umol/(100g*min)")==0) return IMGUNIT_UMOL_PER_MIN_PER_100G;
00141   else if(strcasecmp(unit, "umol/(min*100g)")==0) return IMGUNIT_UMOL_PER_MIN_PER_100G;
00142   else if(strcasecmp(unit, "umol/100g/min")==0)   return IMGUNIT_UMOL_PER_MIN_PER_100G;
00143   else if(strcasecmp(unit, "umol/min/100g")==0)   return IMGUNIT_UMOL_PER_MIN_PER_100G;
00144   else if(strcasecmp(unit, "mg/(100g*min)")==0)   return IMGUNIT_MG_PER_MIN_PER_100G;
00145   else if(strcasecmp(unit, "mg/(min*100g)")==0)   return IMGUNIT_MG_PER_MIN_PER_100G;
00146   else if(strcasecmp(unit, "mg/100g/min")==0)     return IMGUNIT_MG_PER_MIN_PER_100G;
00147   else if(strcasecmp(unit, "mg/min/100g")==0)     return IMGUNIT_MG_PER_MIN_PER_100G;
00148 
00149   return(-3);
00150 }
00151 /*****************************************************************************/
00152 
00153 /*****************************************************************************/
00160 void imgUnitFromEcat(IMG *img, int ecat_unit) {
00161   switch(ecat_unit) {
00162     case  0: /* Unknown               */
00163       img->unit=IMGUNIT_UNKNOWN; break;
00164     case  1: /* MBq/mL                */
00165       img->unit=IMGUNIT_MBQ_PER_ML; break;
00166     case  2: /* ECAT counts           */
00167       img->unit=IMGUNIT_COUNTS; break;
00168     case  3: /* uCi/ml                */
00169       img->unit=IMGUNIT_UCI_PER_ML; break;
00170     case  4: /* LMRGlu                */
00171       img->unit=IMGUNIT_UNKNOWN; break;
00172     case  5: /* LMRUGlu umol/min/100g */
00173       img->unit=IMGUNIT_UMOL_PER_MIN_PER_100G; break;
00174     case  6: /* LMRUGlu mg/min/100g   */
00175       img->unit=IMGUNIT_MG_PER_MIN_PER_100G; break;
00176     case  7: /* nCi/mL                */
00177       img->unit=IMGUNIT_NCI_PER_ML; break;
00178     case  8: /* Well counts           */
00179       img->unit=IMGUNIT_CPS; break;
00180     case  9: /* Becquerels            */
00181       img->unit=IMGUNIT_BQ_PER_ML; break;
00182     case 10: /* kBq/mL                */
00183       img->unit=IMGUNIT_KBQ_PER_ML; break;
00184     case 11: /* 1/min                 */
00185       img->unit=IMGUNIT_PER_MIN; break;
00186     case 12: /* mL/min/100g           */
00187       img->unit=IMGUNIT_ML_PER_DL_PER_MIN; break;
00188     case 13: /* sec*kBq/mL            */
00189       img->unit=IMGUNIT_SEC_KBQ_PER_ML; break;
00190     case 14: /* sec*nCi/mL            */
00191       img->unit=IMGUNIT_UNKNOWN; break;
00192     case 15: /* 1/sec                 */
00193       img->unit=IMGUNIT_PER_SEC; break;
00194     case 16: /* Unitless              */
00195       img->unit=IMGUNIT_UNITLESS; break;
00196     case 17: /* Unknown               */
00197     default:
00198       img->unit=IMGUNIT_UNKNOWN; break;
00199   }
00200 }
00201 /*****************************************************************************/
00202 
00203 /*****************************************************************************/
00210 void imgUnitFromEcat7(IMG *img, ECAT7_mainheader *h) {
00211   if(h->calibration_units==0) { /* Not calibrated */
00212     img->unit=1;
00213   } else if(h->calibration_units==1) {
00214     if(h->calibration_units_label==0)
00215       img->unit=IMGUNIT_BQ_PER_ML;
00216     else
00217       img->unit=imgUnitId(h->data_units);
00218   } else if(h->calibration_units==2) {
00219     img->unit=imgUnitId(h->data_units);
00220   } else {
00221     img->unit=IMGUNIT_UNKNOWN;
00222   }
00223 }
00224 /*****************************************************************************/
00225 
00226 /*****************************************************************************/
00233 int imgUnitToEcat6(IMG *img) {
00234   int ecat_unit;
00235   switch(img->unit) {
00236     case IMGUNIT_CPS: ecat_unit=9; break;
00237     case IMGUNIT_COUNTS: ecat_unit=2; break;
00238     case IMGUNIT_KBQ_PER_ML: ecat_unit=10; break;
00239     case IMGUNIT_SEC_KBQ_PER_ML: ecat_unit=13; break;
00240     case IMGUNIT_PER_SEC: ecat_unit=15; break;
00241     case IMGUNIT_PER_MIN: ecat_unit=11; break;
00242     case IMGUNIT_ML_PER_ML: ecat_unit=16; break;
00243     case IMGUNIT_ML_PER_DL: ecat_unit=16; break;
00244     case IMGUNIT_ML_PER_ML_PER_MIN: ecat_unit=11; break;
00245     case IMGUNIT_ML_PER_DL_PER_MIN: ecat_unit=12; break;
00246     case IMGUNIT_UNITLESS: ecat_unit=16; break;
00247     case IMGUNIT_NCI_PER_ML: ecat_unit=7; break;
00248     case IMGUNIT_MBQ_PER_ML: ecat_unit=1; break;
00249     case IMGUNIT_BQ_PER_ML: ecat_unit=9; break;
00250     case IMGUNIT_UCI_PER_ML: ecat_unit=3; break;
00251     case IMGUNIT_UMOL_PER_MIN_PER_100G: ecat_unit=5; break;
00252     case IMGUNIT_MG_PER_MIN_PER_100G: ecat_unit=6; break;
00253     default: ecat_unit=0; break;
00254   }
00255   return(ecat_unit);
00256 }
00257 /*****************************************************************************/
00258 
00259 /*****************************************************************************/
00266 void imgUnitToEcat7(IMG *img, ECAT7_mainheader *h) {
00267   switch(img->unit) {
00268     case IMGUNIT_CPS:
00269     case IMGUNIT_COUNTS:
00270       h->calibration_units=0;
00271       h->calibration_units_label=1;
00272       strcpy(h->data_units, "ECAT counts/sec");
00273       break;
00274     case IMGUNIT_KBQ_PER_ML:
00275       h->calibration_units=1;
00276       h->calibration_units_label=1;
00277       strcpy(h->data_units, "kBq/cc");
00278       break;
00279     case IMGUNIT_SEC_KBQ_PER_ML:
00280       h->calibration_units=2;
00281       h->calibration_units_label=1;
00282       strcpy(h->data_units, "sec*kBq/mL");
00283       break;
00284     case IMGUNIT_PER_SEC:
00285       h->calibration_units=2;
00286       h->calibration_units_label=1;
00287       strcpy(h->data_units, "1/sec");
00288       break;
00289     case IMGUNIT_PER_MIN:
00290       h->calibration_units=2;
00291       h->calibration_units_label=1;
00292       strcpy(h->data_units, "1/min");
00293       break;
00294     case IMGUNIT_ML_PER_ML:
00295       h->calibration_units=2;
00296       h->calibration_units_label=1;
00297       strcpy(h->data_units, "mL/mL");
00298       break;
00299     case IMGUNIT_ML_PER_DL:
00300       h->calibration_units=2;
00301       h->calibration_units_label=1;
00302       strcpy(h->data_units, "mL/dL");
00303       break;
00304     case IMGUNIT_ML_PER_ML_PER_MIN:
00305       h->calibration_units=2;
00306       h->calibration_units_label=1;
00307       strcpy(h->data_units, "mL/(mL*min)");
00308       break;
00309     case IMGUNIT_ML_PER_DL_PER_MIN:
00310       h->calibration_units=2;
00311       h->calibration_units_label=1;
00312       strcpy(h->data_units, "mL/(dL*min)");
00313       break;
00314     case IMGUNIT_UNITLESS:
00315       h->calibration_units=1;
00316       h->calibration_units_label=1;
00317       strcpy(h->data_units, "unitless");
00318       break;
00319     case IMGUNIT_NCI_PER_ML:
00320       h->calibration_units=1;
00321       h->calibration_units_label=1;
00322       strcpy(h->data_units, "nCi/cc");
00323       break;
00324     case IMGUNIT_MBQ_PER_ML:
00325       h->calibration_units=1;
00326       h->calibration_units_label=1;
00327       strcpy(h->data_units, "MBq/cc");
00328       break;
00329     case IMGUNIT_BQ_PER_ML:
00330       h->calibration_units=1;
00331       h->calibration_units_label=1;
00332       strcpy(h->data_units, "Bq/cc");
00333       break;
00334     case IMGUNIT_UCI_PER_ML:
00335       h->calibration_units=1;
00336       h->calibration_units_label=1;
00337       strcpy(h->data_units, "uCi/cc");
00338       break;
00339     case IMGUNIT_UMOL_PER_MIN_PER_100G:
00340       h->calibration_units=1;
00341       h->calibration_units_label=1;
00342       strcpy(h->data_units, "umol/min/100g");
00343       break;
00344     case IMGUNIT_MG_PER_MIN_PER_100G:
00345       h->calibration_units=1;
00346       h->calibration_units_label=1;
00347       strcpy(h->data_units, "mg/min/100g");
00348       break;
00349     default:
00350       h->calibration_units=2;
00351       h->calibration_units_label=0;
00352       strcpy(h->data_units, "");
00353       break;
00354   }
00355 }
00356 /*****************************************************************************/
00357 
00358 /*****************************************************************************/
00365 char *imgUnit(int dunit) {
00366   int n=0;
00367   while(img_unit_string[n]!=0) n++;
00368   if(dunit<0 || dunit>n-1) return(img_unit_string[IMGUNIT_UNKNOWN]);
00369   else return(img_unit_string[dunit]);
00370 }
00371 /*****************************************************************************/
00372 
00373 /*****************************************************************************/
00377 int imgSetUnit(IMG *img, char *unit)
00378 {
00379   int new_unit;
00380 
00381   if(img==NULL || unit==NULL) return(1);
00382   new_unit=imgUnitId(unit); if(new_unit<0) return(1-new_unit);
00383   img->unit=new_unit;
00384   return(0);
00385 }
00386 /*****************************************************************************/
00387 
00388 /*****************************************************************************/
00389