/tomo/pyhst

To get this branch, use:
bzr branch http://darksoft.org/webbzr/tomo/pyhst
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
"""
    EdfFile.py
    Generic class for Edf files manipulation.    

    Interface:
    ===========================
    class EdfFile:          
        __init__(self,FileName)	
        GetNumImages(self)
        def GetData(self,Index, DataType="",Pos=None,Size=None):
        GetPixel(self,Index,Position)
        GetHeader(self,Index)
        GetStaticHeader(self,Index)
        WriteImage (self,Header,Data,Append=1,DataType="",WriteAsUnsigened=0,ByteOrder="")


    Edf format assumptions:
    ===========================
    The following details were assumed for this implementation:
    - Each Edf file contains a certain number of data blocks.
    - Each data block represents data stored in an one, two or three-dimensional array.
    - Each data block contains a header section, written in ASCII, and a data section of
      binary information.
    - The size of the header section in bytes is a multiple of 1024. The header is
      padded with spaces (0x20). If the header is not padded to a multiple of 1024,
      the file is recognized, but the output is always made in this format.
    - The header section starts by '{' and finishes by '}'. It is composed by several
      pairs 'keyword = value;'. The keywords are case insensitive, but the values are case
      sensitive. Each pair is put in a new line (they are separeted by 0x0A). In the
      end of each line, a semicolon (;) separes the pair of a comment, not interpreted.
      Exemple:
        {
        ; Exemple Header
        HeaderID = EH:000001:000000:000000    ; automatically generated
        ByteOrder = LowByteFirst              ; 
        DataType = FloatValue                 ; 4 bytes per pixel
        Size = 4000000                        ; size of data section
        Dim_1= 1000                           ; x coordinates
        Dim_2 = 1000                          ; y coordinates
        
        (padded with spaces to complete 1024 bytes)
        }
    - There are some fields in the header that are required for this implementation. If any of
      these is missing, or inconsistent, it will be generated an error:
        Size: Represents size of data block
        Dim_1: size of x coordinates (Dim_2 for 2-dimentional images, and also Dim_3 for 3d)
        DataType
        ByteOrder
    - For the written images, these fields are automatically genereted:
        Size,Dim_1 (Dim_2 and Dim_3, if necessary), Byte Order, DataType, HeaderID and Image
      These fields are called here "static header", and can be retrieved by the method
      GetStaticHeader. Other header components are taken by GetHeader. Both methods returns
      a dictionary in which the key is the keyword of the pair. When writting an image through
      WriteImage method, the Header parameter should not contain the static header information,
      which is automatically generated.
    - The indexing of images through these functions is based just on the 0-based position in
      the file, the header items HeaderID and Image are not considered for referencing the
      images.
    - The data section contais a number of bytes equal to the value of Size keyword. Data
      section is going to be translated into an 1D, 2D or 3D Numaric Array, and accessed
      through GetData method call.      


    IMPORTANT - READ THIS
    ===========================
    If you are going to use EdfFile, you have to care about the type of your data.
    The EdfFile class stores data in a Numeric Python array, very efficient
    way for doing matrix operations.

    However, for an unknow reason (to us), Numeric Python doesn't handle the following
    types:
    - unsigned short
    - unsigned integer
    - unsigned long
    Which are supported by Edf file specification.
    So if you use these formats, pay attention to the type parameters when reading
    from or writing to a file (when using other Edf types, the convertions are direct,
    and you don't need to mention the type, unless you really want to change it).

    By default, if no type is mentioned, the EdfFile class stores, when reading a file:
    - UnsignedShort data into an short array
    - UnsignedInteger data into an integer array
    - UnsignedLong data into a long array

    This keeps the size of storage in memory, but can imply in loss of information.

    Taking "unsigned short" as an exemple:
    1) Supposing you get data with: "array=obj.GetData(0)", this array is going to be
       created as signed short. If you write it then as: 'obj2.WriteImage({},array)',
       the edf image created is going to be of signed short type, different from the
       original. To save in the same way as the former image, you must be explicit
       about the data type: 'obj2.WriteImage({},array,DataType="UnsignedShort")'
    2) If you intend to make operations, or even just read properly the values of an
       image, you should read this image as 'array=obj.GetData(0),DataType="Long")'.
       This will require two times the storage space but will assure correct values.
       If you intend to save it again, you should include the correct data type you
       are saving to.
    3) When you are saving an unsigned short array into a long, float or double
       format, you should be explicit to the fact you want this array to be
       considered a signed or unsigned (through the parameter WriteAsUnsigened).
       Suppose an hexa value of FFFF in this array. This means -1 if the array
       comes from signed data, or 65535 if it cames from unsigned data. If you save
       the array as this: 'obj2.WriteImage({},array,DataType="FloatValue")' it is
       going to be considered unsigned, and a value of FFFF is going to be
       translated into a float -1. If you want to consider the array as unsigned
       you should do:
       'obj2.WriteImage({},array,DataType="FloatValue", WriteAsUnsigened=1 )'
       In this way, a FFFF value is going to be translated into a float 65535.


"""

__author__ =  'Alexandre Gobbo (gobbo@esrf.fr)'
__version__=  '$Revision: 1.4 $'

################################################################################  
import sys, string
import Numeric
import os.path , tempfile, shutil
try:
    from FastEdf import extended_fread
    CAN_USE_FASTEDF = 1
    print " imported the fast binary for edffile readings "
except:
    print " WARNING, je ne peu pas utiliser le module binaire FastEdfF "
    CAN_USE_FASTEDF = 0

################################################################################
# constants
HEADER_BLOCK_SIZE = 1024
STATIC_HEADER_ELEMENTS=("HeaderID","Image","ByteOrder","DataType","Dim_1","Dim_2","Dim_3","Size")
STATIC_HEADER_ELEMENTS_CAPS=("HEADERID","IMAGE","BYTEORDER","DATATYPE","DIM_1","DIM_2","DIM_3","SIZE")

LOWER_CASE=0
UPPER_CASE=1

KEYS=1
VALUES=2

###############################################################################
class Image:
    """
    """
    def __init__(self):
        """ Constructor
        """
        self.Header={}
        self.StaticHeader={}
        self.HeaderPosition=0
        self.DataPosition=0
        self.Size=0
        self.NumDim=1
        self.Dim1=0
        self.Dim2=0
        self.Dim3=0
        self.DataType=""
        #for i in STATIC_HEADER_ELEMENTS: self.StaticHeader[i]=""
   
################################################################################

class  EdfFile:    
    """
    """
    ############################################################################
    #Interface
    def __init__(self,FileName,fastedf=None):
        """ Constructor
            FileName:   Name of the file (either existing or to be created)  
        """
        self.Images=[]
        self.NumImages=0
        self.FileName=FileName
        self.File = 0
        if fastedf is None:fastedf=0
        self.fastedf=fastedf
        
        if sys.byteorder=="big": self.SysByteOrder="HighByteFirst"
        else: self.SysByteOrder="LowByteFirst"
        
        try:
            if os.path.isfile(self.FileName)==0:                
                self.File = open(self.FileName, "wb")
                self.File.close()    

            if (os.access(self.FileName,os.W_OK)):
                self.File=open(self.FileName, "r+b")
            else : 
                self.File=open(self.FileName, "rb")

            self.File.seek(0, 0)
        except:
            try:
                self.File.close()
            except:
                pass
            raise Exception, "EdfFile: Error opening file"

        self.File.seek(0, 0)
        
        Index=0
        line = self.File.readline()        
        while line != "":            
            if string.count(line, "{\n") >= 1 or string.count(line, "{\r\n")>=1:
                Index=self.NumImages
                self.NumImages = self.NumImages + 1                
                self.Images.append(Image())
                self.Images[Index].HeaderPosition=self.File.tell()    
                
            if string.count(line, "=") >= 1:
                listItems = string.split(line, "=", 1)
                typeItem = string.strip(listItems[0])
                listItems = string.split(listItems[1], ";", 1)
                valueItem = string.strip(listItems[0])

                #if typeItem in self.Images[Index].StaticHeader.keys():          
                if (string.upper(typeItem)) in STATIC_HEADER_ELEMENTS_CAPS:          
                    self.Images[Index].StaticHeader[typeItem]=valueItem                    
                else:
                    self.Images[Index].Header[typeItem]=valueItem
            if string.count(line, "}\n") >= 1:
                #for i in STATIC_HEADER_ELEMENTS_CAPS:
                #    if self.Images[Index].StaticHeader[i]=="":
                #        raise Exception, "Bad File Format"
                self.Images[Index].DataPosition=self.File.tell()
                #self.File.seek(string.atoi(self.Images[Index].StaticHeader["Size"]), 1)
                StaticPar = SetDictCase(self.Images[Index].StaticHeader,UPPER_CASE,KEYS)
                if "SIZE" in StaticPar.keys():
                    self.Images[Index].Size = string.atoi(StaticPar["SIZE"])
                else:
                    raise Exception, "EdfFile: Image doesn't have size information"                
                if "DIM_1" in StaticPar.keys():
                    self.Images[Index].Dim1 = string.atoi(StaticPar["DIM_1"])
                else:
                    raise Exception, "EdfFile: Image doesn't have dimension information"
                if "DIM_2" in StaticPar.keys():
                    self.Images[Index].NumDim=2
                    self.Images[Index].Dim2 = string.atoi(StaticPar["DIM_2"])
                if "DIM_3" in StaticPar.keys():
                    self.Images[Index].NumDim=3
                    self.Images[Index].Dim3 = string.atoi(StaticPar["DIM_3"])
                if "DATATYPE" in StaticPar.keys():
                    self.Images[Index].DataType=StaticPar["DATATYPE"]
                else:
                    raise Exception, "EdfFile: Image doesn't have datatype information"
                if "BYTEORDER" in StaticPar.keys():
                    self.Images[Index].ByteOrder=StaticPar["BYTEORDER"]
                else:
                    raise Exception, "EdfFile: Image doesn't have byteorder information"
                    

                                
                self.File.seek(self.Images[Index].Size, 1)
                
            line = self.File.readline()
    
        
    def GetNumImages(self):
        """ Returns number of images of the object (and associated file)
        """
        return self.NumImages

        
    
    def GetData(self,Index, DataType="",Pos=None,Size=None):
        """ Returns numeric array with image data
            Index:          The zero-based index of the image in the file
            DataType:       The edf type of the array to be returnd
                            If ommited, it is used the default one for the type
                            indicated in the image header
                            Attention to the absence of UnsignedShort,
                            UnsignedInteger and UnsignedLong types in
                            Numeric Python
                            Default relation between Edf types and NumPy's typecodes:
                                SignedByte          1
                                UnsignedByte        b       
                                SignedShort         s
                                UnsignedShort       w
                                SignedInteger       i
                                UnsignedInteger     u
                                SignedLong          l
                                UnsignedLong        u
                                FloatValue          f
                                DoubleValue         d
            Pos:            Tuple (x) or (x,y) or (x,y,z) that indicates the begining
                            of data to be read. If ommited, set to the origin (0),
                            (0,0) or (0,0,0)
            Size:           Tuple, size of the data to be returned as x) or (x,y) or
                            (x,y,z) if ommited, is the distance from Pos to the end.

            If Pos and Size not mentioned, returns the whole data.                         
        """
        fastedf = self.fastedf
        if Index < 0 or Index >= self.NumImages: raise Exception, "EdfFile: Index out of limit"
        if fastedf is None:fastedf = 0
        if Pos is None and Size is None:
            self.File.seek(self.Images[Index].DataPosition,0)
            Data = Numeric.fromstring(self.File.read(self.Images[Index].Size), self.__GetDefaultNumericType__(self.Images[Index].DataType))
            if self.Images[Index].NumDim==3:
                Data = Numeric.reshape(Data, (self.Images[Index].Dim3,self.Images[Index].Dim2, self.Images[Index].Dim1))            
            elif self.Images[Index].NumDim==2:
                Data = Numeric.reshape(Data, (self.Images[Index].Dim2, self.Images[Index].Dim1)) 
        elif fastedf and CAN_USE_FASTEDF:
            type= self.__GetDefaultNumericType__(self.Images[Index].DataType)
            size_pixel=self.__GetSizeNumericType__(type)
            Data=Numeric.array([],type)
            if self.Images[Index].NumDim==1:
                if Pos==None: Pos=(0,)
                if Size==None: Size=(0,)
                sizex=self.Images[Index].Dim1
                Size=list(Size)                
                if Size[0]==0:Size[0]=sizex-Pos[0]
                self.File.seek((Pos[0]*size_pixel)+self.Images[Index].DataPosition,0)
                Data = Numeric.fromstring(self.File.read(Size[0]*size_pixel), type)
            elif self.Images[Index].NumDim==2:                
                if Pos==None: Pos=(0,0)
                if Size==None: Size=(0,0)
                Size=list(Size)
                sizex,sizey=self.Images[Index].Dim1,self.Images[Index].Dim2
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                Data=Numeric.zeros([Size[1],Size[0]],type)
                self.File.seek((((Pos[1]*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                extended_fread(Data, Size[0]*size_pixel , Numeric.array([Size[1]]), Numeric.array([sizex*size_pixel]) ,self.File)

            elif self.Images[Index].NumDim==3:
                if Pos==None: Pos=(0,0,0)
                if Size==None: Size=(0,0,0)
                Size=list(Size)
                sizex,sizey,sizez=self.Images[Index].Dim1,self.Images[Index].Dim2,self.Images[Index].Dim3
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                if Size[2]==0:Size[2]=sizez-Pos[2]
                Data=Numeric.zeros([Size[2],Size[1],Size[0]],type)
                self.File.seek(((((Pos[2]*sizey+Pos[1])*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                extended_fread(Data, Size[0]*size_pixel , Numeric.array([Size[2],Size[1]]),
                        Numeric.array([ sizey*sizex*size_pixel , sizex*size_pixel]) ,self.File)

        else:
            if fastedf:print "I could not use fast routines"
            type= self.__GetDefaultNumericType__(self.Images[Index].DataType)
            size_pixel=self.__GetSizeNumericType__(type)
            Data=Numeric.array([],type)
            if self.Images[Index].NumDim==1:
                if Pos==None: Pos=(0,)
                if Size==None: Size=(0,)
                sizex=self.Images[Index].Dim1
                Size=list(Size)                
                if Size[0]==0:Size[0]=sizex-Pos[0]
                self.File.seek((Pos[0]*size_pixel)+self.Images[Index].DataPosition,0)
                Data = Numeric.fromstring(self.File.read(Size[0]*size_pixel), type)
            elif self.Images[Index].NumDim==2:                
                if Pos==None: Pos=(0,0)
                if Size==None: Size=(0,0)
                Size=list(Size)
                sizex,sizey=self.Images[Index].Dim1,self.Images[Index].Dim2
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                for y in range(Pos[1],Pos[1]+Size[1]):
                    self.File.seek((((y*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                    line = Numeric.fromstring(self.File.read(Size[0]*size_pixel), type)
                    Data=Numeric.concatenate((Data,line))
                Data = Numeric.reshape(Data, (Size[1],Size[0]))                            
            elif self.Images[Index].NumDim==3:
                if Pos==None: Pos=(0,0,0)
                if Size==None: Size=(0,0,0)
                Size=list(Size)
                sizex,sizey,sizez=self.Images[Index].Dim1,self.Images[Index].Dim2,self.Images[Index].Dim3
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                if Size[2]==0:Size[2]=sizez-Pos[2]
                for z in range(Pos[2],Pos[2]+Size[2]):
                    for y in range(Pos[1],Pos[1]+Size[1]):
                        self.File.seek(((((z*sizey+y)*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                        line = Numeric.fromstring(self.File.read(Size[0]*size_pixel), type)
                        Data=Numeric.concatenate((Data,line))                
                Data = Numeric.reshape(Data, (Size[2],Size[1],Size[0]))

        if string.upper(self.SysByteOrder)!=string.upper(self.Images[Index].ByteOrder):
            Data=Data.byteswapped()
        if DataType != "":
            Data=self.__SetDataType__ (Data,DataType)
        return Data



    def GetPixel(self,Index, Position):
        """ Returns double value of the pixel, regardless the format of the array
            Index:      The zero-based index of the image in the file
            Position:   Tuple with the coordinete (x), (x,y) or (x,y,z)
        """
        if Index < 0 or Index >= self.NumImages: raise Exception, "EdfFile: Index out of limit"
        if len(Position)!= self.Images[Index].NumDim: raise Exception, "EdfFile: coordinate with wrong dimension "
        
        size_pixel=self.__GetSizeNumericType__(self.__GetDefaultNumericType__(self.Images[Index].DataType))
        offset=Position[0]*size_pixel
        if self.Images[Index].NumDim>1:
            size_row=size_pixel * self.Images[Index].Dim1
            offset=offset+ (Position[1]* size_row)
            if self.Images[Index].NumDim==3:
                size_img=size_row * self.Images[Index].Dim2
                offset=offset+ (Position[2]* size_img)
        self.File.seek(self.Images[Index].DataPosition + offset,0)
        Data = Numeric.fromstring(self.File.read(size_pixel), self.__GetDefaultNumericType__(self.Images[Index].DataType))
        if string.upper(self.SysByteOrder)!=string.upper(self.Images[Index].ByteOrder):
            Data=Data.byteswapped() 
        Data=self.__SetDataType__ (Data,"DoubleValue")
        return Data[0]
         
        
    def GetHeader(self,Index):
        """ Returns dictionary with image header fields.
            Does not include the basic fields (static) defined by data shape, 
            type and file position. These are get with GetStaticHeader
            method.
            Index:          The zero-based index of the image in the file
        """
        if Index < 0 or Index >= self.NumImages: raise Exception, "Index out of limit"
        #return self.Images[Index].Header
        ret={}
        for i in self.Images[Index].Header.keys():
            ret[i]=self.Images[Index].Header[i]        
        return ret

        
    def GetStaticHeader(self,Index):
        """ Returns dictionary with static parameters
            Data format and file position dependent information
            (dim1,dim2,size,datatype,byteorder,headerId,Image)
            Index:          The zero-based index of the image in the file
        """ 
        if Index < 0 or Index >= self.NumImages: raise Exception, "Index out of limit"
        #return self.Images[Index].StaticHeader
        ret={}
        for i in self.Images[Index].StaticHeader.keys():
            ret[i]=self.Images[Index].StaticHeader[i]        
        return ret


    def WriteImage (self,Header,Data,Append=1,DataType="",ByteOrder=""):
        """ Writes image to the file. 
            Header:         Dictionary containing the non-static header
                            information (static information is generated
                            according to position of image and data format
            Append:         If equals to 0, overwrites the file. Otherwise, appends
                            to the end of the file
            DataType:       The data type to be saved to the file:
                                SignedByte          
                                UnsignedByte               
                                SignedShort         
                                UnsignedShort       
                                SignedInteger       
                                UnsignedInteger     
                                SignedLong          
                                UnsignedLong        
                                FloatValue          
                                DoubleValue         
                            Default: according to Data array typecode:
                                    1:  SignedByte
                                    b:  UnsignedByte
                                    s:  SignedShort       
				    w:  UnsignedShort
                                    i:  SignedInteger
                                    l:  SignedLong          
				    u:  UnsignedLong
                                    f:  FloatValue       
                                    d:  DoubleValue
            ByteOrder:      Byte order of the data in file:
                                HighByteFirst
                                LowByteFirst
                            Default: system's byte order
        """
        if Append==0:
            self.File.truncate(0)
            self.Images=[]
            self.NumImages=0
        Index=self.NumImages
        self.NumImages = self.NumImages + 1                
        self.Images.append(Image())

        #self.Images[Index].StaticHeader["Dim_1"] = "%d" % Data.shape[1]
        #self.Images[Index].StaticHeader["Dim_2"] = "%d" % Data.shape[0]
        if len(Data.shape)==1:
            self.Images[Index].Dim1=Data.shape[0]
            self.Images[Index].StaticHeader["Dim_1"] = "%d" % self.Images[Index].Dim1
            self.Images[Index].Size=(Data.shape[0]*self.__GetSizeNumericType__(Data.typecode()))
        elif len(Data.shape)==2:
            self.Images[Index].Dim1=Data.shape[1]
            self.Images[Index].Dim2=Data.shape[0]
            self.Images[Index].StaticHeader["Dim_1"] = "%d" % self.Images[Index].Dim1
            self.Images[Index].StaticHeader["Dim_2"] = "%d" % self.Images[Index].Dim2
            self.Images[Index].Size=(Data.shape[0]*Data.shape[1]*self.__GetSizeNumericType__(Data.typecode()))
            self.Images[Index].NumDim=2
        elif len(Data.shape)==3:
            self.Images[Index].Dim1=Data.shape[2]
            self.Images[Index].Dim2=Data.shape[1]
            self.Images[Index].Dim3=Data.shape[0]
            self.Images[Index].StaticHeader["Dim_1"] = "%d" % self.Images[Index].Dim1
            self.Images[Index].StaticHeader["Dim_2"] = "%d" % self.Images[Index].Dim2
            self.Images[Index].StaticHeader["Dim_3"] = "%d" % self.Images[Index].Dim3
            self.Images[Index].Size=(Data.shape[0]*Data.shape[1]*Data.shape[2]*self.__GetSizeNumericType__(Data.typecode()))
            self.Images[Index].NumDim=3
        elif len(Data.shape)>3:
            raise Exception, "EdfFile: Data dimension not suported"
        

        if DataType=="":
            self.Images[Index].DataType=self.__GetDefaultEdfType__(Data.typecode())
        else:
            self.Images[Index].DataType=DataType
            Data=self.__SetDataType__ (Data,DataType)
                            
        if ByteOrder=="":
            self.Images[Index].ByteOrder=self.SysByteOrder
        else:
            self.Images[Index].ByteOrder=ByteOrder
                
        self.Images[Index].StaticHeader["Size"]  = "%d" % self.Images[Index].Size
        self.Images[Index].StaticHeader["Image"] = Index+1
        self.Images[Index].StaticHeader["HeaderID"] = "EH:%06d:000000:000000" % self.Images[Index].StaticHeader["Image"]
        self.Images[Index].StaticHeader["ByteOrder"]=self.Images[Index].ByteOrder
        self.Images[Index].StaticHeader["DataType"]=self.Images[Index].DataType

        
        self.Images[Index].Header={}            
        self.File.seek(0,2)
        StrHeader = "{\n"
        for i in STATIC_HEADER_ELEMENTS:
            if i in self.Images[Index].StaticHeader.keys():
                StrHeader = StrHeader + ("%s = %s ;\n" % (i , self.Images[Index].StaticHeader[i]))
        for i in Header.keys():
            StrHeader = StrHeader + ("%s = %s ;\n" % (i,Header[i]))
            self.Images[Index].Header[i]=Header[i]
        newsize=(((len(StrHeader)+1)/HEADER_BLOCK_SIZE)+1)*HEADER_BLOCK_SIZE -2                   
        StrHeader = string.ljust(StrHeader,newsize)
        StrHeader = StrHeader+"}\n"

        self.Images[Index].HeaderPosition=self.File.tell()    
        self.File.write(StrHeader)
        self.Images[Index].DataPosition=self.File.tell()

        #if self.Images[Index].StaticHeader["ByteOrder"] != self.SysByteOrder:
        if string.upper(self.Images[Index].ByteOrder) != string.upper(self.SysByteOrder):
            self.File.write((Data.byteswapped()).tostring())  
        else:
            self.File.write(Data.tostring())
        
        

    ############################################################################
    #Internal Methods
        
    def __GetDefaultNumericType__(self, EdfType):
        """ Internal method: returns NumPy type according to Edf type
        """
        return GetDefaultNumericType(EdfType)

    def __GetDefaultEdfType__(self, NumericType):
        """ Internal method: returns Edf type according Numpy type
        """
        if  NumericType  == "1":            return "SignedByte"
        elif NumericType == "b":            return "UnsignedByte"
        elif NumericType == "s":            return "SignedShort"          
        elif NumericType == "w":            return "UnsignedShort"          
        elif NumericType == "i":            return "SignedInteger"  
        elif NumericType == "l":            return "SignedLong"           
	elif NumericType == "u":	    return "UnsignedLong"
        elif NumericType == "f":            return "FloatValue"         
        elif NumericType == "d":            return "DoubleValue"
        else: raise Exception, "__GetDefaultEdfType__: unknown NumericType"


    def __GetSizeNumericType__(self, NumericType):
        """ Internal method: returns size of NumPy's Array Types
        """
        if  NumericType  == "1":            return 1
        elif NumericType == "b":            return 1
        elif NumericType == "s":            return 2         
	elif NumericType == "w":	    return 2
        elif NumericType == "i":            return 4  
        elif NumericType == "l":            return 4           
        elif NumericType == "u":	    return 4
        elif NumericType == "f":            return 4         
        elif NumericType == "d":            return 8
        else: raise Exception, "__GetSizeNumericType__: unknown NumericType"


    def __SetDataType__ (self,Array,DataType):
        """ Internal method: array type convertion
        """
        FromEdfType= Array.typecode()
        ToEdfType= self.__GetDefaultNumericType__(DataType)
        if ToEdfType != FromEdfType:
            aux=Array.astype(self.__GetDefaultNumericType__(DataType))    
            return aux
        return Array

    def __del__(self):
        try:
            self.File.close()
        except:
            pass
        

def GetDefaultNumericType(EdfType):
    """ Returns NumPy type according Edf type
    """
    EdfType=string.upper(EdfType)
    if   EdfType == "SIGNEDBYTE":       return "1"
    elif EdfType == "UNSIGNEDBYTE":     return "b"       
    elif EdfType == "SIGNEDSHORT":      return "s"
    elif EdfType == "UNSIGNEDSHORT":    return "w"
    elif EdfType == "SIGNEDINTEGER":    return "i"
    elif EdfType == "UNSIGNEDINTEGER":  return "u"
    elif EdfType == "SIGNEDLONG":       return "l"
    elif EdfType == "UNSIGNEDLONG":     return "u"
    elif EdfType == "FLOATVALUE":       return "f"
    elif EdfType == "FLOAT":            return "f"
    elif EdfType == "DOUBLEVALUE":      return "d"
    else: raise Exception, "__GetDefaultNumericType__: unknown EdfType"


def SetDictCase(Dict, Case, Flag):
    """ Returns dictionary with keys and/or values converted into upper or lowercase
        Dict:   input dictionary
        Case:   LOWER_CASE, UPPER_CASE
        Flag:   KEYS, VALUES or KEYS | VALUES        
    """
    newdict={}
    for i in Dict.keys():
        newkey=i
        newvalue=Dict[i]
        if Flag & KEYS:
            if Case == LOWER_CASE:  newkey = string.lower(newkey)
            else:                   newkey = string.upper(newkey)
        if Flag & VALUES:
            if Case == LOWER_CASE:  newvalue = string.lower(newvalue)
            else:                   newvalue = string.upper(newvalue)
        newdict[newkey]=newvalue
    return newdict    


def GetRegion(Arr,Pos,Size):
    """Returns array with refion of Arr.
       Arr must be 1d, 2d or 3d
       Pos and Size are tuples in the format (x) or (x,y) or (x,y,z)
       Both parameters must have the same size as the dimention of Arr
    """
    Dim=len(Arr.shape)
    if len(Pos) != Dim:  return None
    if len(Size) != Dim: return None
    
    if (Dim==1):
        SizeX=Size[0]
        if SizeX==0: SizeX=Arr.shape[0]-Pos[0]
        ArrRet=Numeric.take(Arr, range(Pos[0],Pos[0]+SizeX))
    elif (Dim==2):
        SizeX=Size[0]
        SizeY=Size[1]
        if SizeX==0: SizeX=Arr.shape[1]-Pos[0]
        if SizeY==0: SizeY=Arr.shape[0]-Pos[1]
        ArrRet=Numeric.take(Arr, range(Pos[1],Pos[1]+SizeY))
        ArrRet=Numeric.take(ArrRet, range(Pos[0],Pos[0]+SizeX),1)
    elif (Dim==3):
        SizeX=Size[0]
        SizeY=Size[1]
        SizeZ=Size[2]
        if SizeX==0: SizeX=Arr.shape[2]-Pos[0]
        if SizeY==0: SizeX=Arr.shape[1]-Pos[1]
        if SizeZ==0: SizeZ=Arr.shape[0]-Pos[2]
        ArrRet=Numeric.take(Arr, range(Pos[2],Pos[2]+SizeZ))
        ArrRet=Numeric.take(ArrRet, range(Pos[1],Pos[1]+SizeY),1)
        ArrRet=Numeric.take(ArrRet, range(Pos[0],Pos[0]+SizeX),2)
    else:
        ArrRet=None
    return ArrRet

#EXEMPLE CODE:        
if __name__ == "__main__":    
    #Creates object based on file exe.edf
    exe=EdfFile("images/test_image.edf")
    x=EdfFile("images/test_getdata.edf")
    #Gets unsigned short data, storing in an signed long
    arr=exe.GetData(0,Pos=(100,200),Size=(200,400))
    x.WriteImage({},arr,0)

    arr=exe.GetData(0,Pos=(100,200))
    x.WriteImage({},arr)

    arr=exe.GetData(0,Size=(200,400))
    x.WriteImage({},arr)

    arr=exe.GetData(0)
    x.WriteImage({},arr)
    
    sys.exit()
        
    #Creates object based on file exe.edf
    exe=EdfFile("images/.edf")

    #Creates long array , filled with 0xFFFFFFFF(-1)
    la=Numeric.zeros((100,100))
    la=la-1
    
    #Creates a short array, filled with 0xFFFF
    sa=Numeric.zeros((100,100))
    sa=sa+0xFFFF
    sa=sa.astype("s")

    #Writes long array, initializing file (append=0)
    exe.WriteImage({},la,0,"")
    
    #Appends short array with new header items
    exe.WriteImage({'Name': 'Alexandre', 'Date': '16/07/2001'},sa)    

    #Appends short array, in Edf type unsigned
    exe.WriteImage({},sa,DataType="UnsignedShort")    

    #Appends short array, in Edf type unsigned
    exe.WriteImage({},sa,DataType="UnsignedLong")    

    #Appends long array as a double, considering unsigned
    exe.WriteImage({},la,DataType="DoubleValue",WriteAsUnsigened=1)

    #Gets unsigned short data, storing in an signed long
    ushort=exe.GetData(2,"SignedLong")

    #Makes an operation
    ushort=ushort-0x10

    #Saves Result as signed long
    exe.WriteImage({},ushort)

    #Saves in the original format (unsigned short)
    OldHeader=exe.GetStaticHeader(2)
    exe.WriteImage({},ushort,1,OldHeader["DataType"] )