tools_extractdataΒΆ

This section contains the tools_extractdata script.

Download file: tools_extractdata.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
###########################################################################
# (C) 2016 Elettra - Sincrotrone Trieste S.C.p.A.. All rights reserved.   #
#                                                                         #
#                                                                         #
# This file is part of STP-Core, the Python core of SYRMEP Tomo Project,  #
# a software tool for the reconstruction of experimental CT datasets.     #
#                                                                         #
# STP-Core is free software: you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the   #
# Free Software Foundation, either version 3 of the License, or (at your  #
# option) any later version.                                              #
#                                                                         #
# STP-Core is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or   #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License    #
# for more details.                                                       #
#                                                                         #
# You should have received a copy of the GNU General Public License       #
# along with STP-Core. If not, see <http://www.gnu.org/licenses/>.        #
#                                                                         #
###########################################################################

#
# Author: Francesco Brun
# Last modified: July, 8th 2016
#


import os
import os.path
import numpy
import time

from sys import argv, exit
from h5py import File as getHDF5
from numpy import float32

# pystp-specific:
import stp_core.io.tdf as tdf

def main(argv):    
    """Extract a 2D image (projection or sinogram) from the input TDF file (DataExchange HDF5) and
    creates a 32-bit RAW file to disk.

    Parameters
    ----------
    argv[0] : string
        The absolute path of the input TDF.

    argv[1] : int
        The relative position of the image within the dataset.

    argv[2] : string
        One of the following options: 'tomo', 'sino', 'flat', 'dark'.

    argv[3] : string
        The absolute path of the output 32-bit RAW image file. Filename will be modified by adding 
        image width, image height, minimum and maximum value of the input TDF dataset.

    Example
    -------
    tools_extractdata "S:\\dataset.tdf" 128 tomo "R:\\proj" 

    """
    try:
        #
        # Get input parameters:
        #
        infile   = argv[0]
        index    = int(argv[1]) 
        imtype   = argv[2]
        outfile  = argv[3]      
    
        #
        # Body
        #   
    
        # Check if file exists:
        if not os.path.exists(infile):      
            #log = open(logfilename,"a")
            #log.write(os.linesep + "\tError: input TDF file not found. Process will end.")             
            #log.close()            
            exit()  

        # Open the HDF5 file:

        f = getHDF5( infile, 'r' )
        if (imtype == 'sino'):
            if "/tomo" in f:
                dset = f['tomo']    
            else: 
                dset = f['exchange/data']
            im = tdf.read_sino( dset, index )   
        elif (imtype == 'dark'):
            if "/dark" in f:
                dset = f['dark']    
            else: 
                dset = f['exchange/data_dark']  
            im = tdf.read_tomo( dset, index )
        elif (imtype == 'flat'):
            if "/flat" in f:
                dset = f['flat']    
            else: 
                dset = f['exchange/data_white'] 
            im = tdf.read_tomo( dset, index )
        else:
            if "/tomo" in f:
                dset = f['tomo']    
            else: 
                dset = f['exchange/data']   
            im = tdf.read_tomo( dset, index )
                

        min = float(numpy.nanmin(im[:]))
        max = float(numpy.nanmax(im[:]))
            
        # Get global attributes (if any):
        try:
            if ('version' in f.attrs):
                if (f.attrs['version'] == '1.0'):   
                    min = float(dset_tomo.attrs['min'])
                    max = float(dset_tomo.attrs['max'])         
        except: 
            pass
        
        f.close()
        
        # Cast type:
        im = im.astype(float32)
        
        # Modify file name:
        outfile = outfile + '_' + str(im.shape[1]) + 'x' + str(im.shape[0]) + '_' + str(min) + '$' + str(max)   
        
        # Write RAW data to disk:
        im.tofile(outfile)          
    
    except:             
        
        exit()

if __name__ == "__main__":
    main(argv[1:])