-
Notifications
You must be signed in to change notification settings - Fork 3
/
ImportRPL.s
439 lines (358 loc) · 13.6 KB
/
ImportRPL.s
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
//
// Copyright 2011 Luiz Fernando Zagonel
// ImportRPL 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 2 of the License, or
// (at your option) any later version.
// ImportRPL 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 Hyperspy; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
// USA
// This script is designed to import Ripple files into Digital Micrograph.
// It is used to easy data transit between DigitalMicrograph and Hyperspy
// and Esprit 1.9.
// The script will ask for 2 files:
// 1- the riple file with the data format and calibrations
// 2- the data itself in raw format.
// IF a file with the same name and path as the riple file exits
// with raw or bin extension it is opened directly without prompting
// Last modified 20/10/2011, Mike Sarahan
// Spelling fixes, added conversion to SI if eV is in units
// Made 2D images nicer to look at (no longer requires slice tool)
// Last modified 16/06/2012, Luiz Fernando Zagonel
// Datatype issue with spectrum picker solved.
// 14/05/2015, Eric Prestat
// Convert to EELS of EDS using the "signal" tag
// 25/05/2015, Eric Prestat
// Workaround to open RPL files created by Esprit 1.9
image SpectrumConvertToEELS(image img)
{
img.setstringnote("Meta Data:Format","Spectrum image")
img.setstringnote("Meta Data:Signal","EELS")
return img
}
image SpectrumConvertToEDS(image img)
{
img.setstringnote("Meta Data:Format","Spectrum image")
img.setstringnote("Meta Data:Signal","X-ray")
return img
}
image OpenRPL(string filenameRPL)
{
Object file_stream
Image img, img2, img3
String filenameRAW
Number size_x, size_y, size_z, pixel_size
realnumber scale_x, scale_y,scale_z, origin_x, origin_y, origin_z
string units_x, units_y, units_z
Number file_RPL, file_RAW
Number index
string textline,value,data_type_value,recorded_by_value
string width_name_value, height_name_value, depth_name_value, signal
string imagename
size_x = size_y = size_z = 1
origin_x = 0
scale_x = 1
units_x = ""
origin_y = 0
scale_y = 1
units_x = ""
origin_z = 0
scale_z = 1
units_z = ""
Number openfromEsprit = 0
//opens the riple file
file_RPL = OpenFileForReading(filenameRPL)
//Read ripple file line by line
While(ReadFileLine(file_RPL, textline))
{
//Result(textline + "\n") //display each line read
//Look for Keywords to read format and calibration
if(textline.find("width")!=-1)
if(textline.mid(5,1)!="-")
{
value = textline.right(textline.len() - 5 )
size_x = value.val()
}
if(textline.find("width-scale")!=-1)
{
value = textline.right(textline.len() - 11)
scale_x = value.val()
}
if(textline.find("width-origin")!=-1)
{
value = textline.right(textline.len() - 12)
origin_x = value.val()
}
if(textline.find("width-name")!=-1)
{
width_name_value = textline.right(textline.len() - 11)
width_name_value = width_name_value.left(width_name_value.len()-2)
}
if(textline.find("width-units")!=-1)
{
units_x = textline.right(textline.len() - 12)
units_x = units_x.left(units_x.len()-2)
}
if(textline.find("height")!=-1)
if(textline.mid(6,1)!="-")
{
value = textline.right(textline.len() - 6)
size_y = value.val()
}
if(textline.find("height-scale")!=-1)
{
value = textline.right(textline.len() - 12)
scale_y = value.val()
}
if(textline.find("height-origin")!=-1)
{
value = textline.right(textline.len() - 13)
origin_y = value.val()
}
if(textline.find("height-name")!=-1)
{
height_name_value = textline.right(textline.len() - 12)
height_name_value = height_name_value.left(height_name_value.len()-2)
}
if(textline.find("height-units")!=-1)
{
units_y = textline.right(textline.len() - 13)
units_y = units_y.left(units_y.len()-2)
}
if(textline.find("depth")!=-1)
if(textline.mid(5,1)!="-")
{
value = textline.right(textline.len() - 5)
size_z = value.val()
}
if(textline.find("depth-origin")!=-1)
{
value = textline.right(textline.len() - 12)
origin_z = value.val()
}
if(textline.find("depth-scale")!=-1)
{
value = textline.right(textline.len() - 11)
scale_z = value.val()
}
if(textline.find("depth-units")!=-1)
{
depth_name_value = textline.right(textline.len() - 11)
depth_name_value = depth_name_value.left(depth_name_value.len()-2)
}
if(textline.find("depth-units")!=-1)
{
units_z = textline.right(textline.len() - 12)
units_z = units_z.left(units_z.len()-2)
}
if(textline.find("data-length")!=-1)
{
value = textline.right(textline.len() - 11)
pixel_size = value.val()
}
if(textline.find("data-type")!=-1)
{
data_type_value = textline.right(textline.len() - 10)
data_type_value = data_type_value.left(data_type_value.len()-2)
}
// Rpl files generated by Esprit 1.9 software have a typo:
// data-Length instead of data-length. We can take advantage of it to
// recognized a RPL created by Esprit 1.9.
if(textline.find("data-Length")!=-1)
{
openfromEsprit = 1
value = textline.right(textline.len() - 11)
pixel_size = value.val()
}
if(textline.find("record-by")!=-1)
{
recorded_by_value = textline.right(textline.len() - 10)
recorded_by_value = recorded_by_value.left(recorded_by_value.len()-2)
}
// hyperspy specific information.
// Signal type, i. e. EDS_SEM, EDS_TEM, EELS or image
if(textline.find("signal")!=-1)
{
signal = textline.right(textline.len() - 7)
signal = signal.left(signal.len()-2)
}
}
// Wrong formatting of the RPL file by Esprit: easy workaround is to
// assumed default parameter...
if(openfromEsprit==1)
{
result("Reading RPL file created by Esprit\n")
data_type_value="unsigned"
// You can change the parameter below to hardcode the energy scale
// of the spectrum imported from Esprit
//origin_z = -48.0
//scale_z = 0.01
//units_z = "keV"
signal="EDS"
recorded_by_value="vector"
}
//Riple file reading is finished
CloseFile(file_RPL)
if(data_type_value=="signed")
if(pixel_size>4)
{
//Give some info on the information obtained from the riple file.
Result( "Data dimensions:" + size_x + " x " + size_y + " x " +size_z + " x " + pixel_size + " \n" )
Result( "Scales:" + scale_x + " x " + scale_y + " x " + scale_z + " \n" )
Result( "Origins:" + origin_x + " x " + origin_y + " x " + origin_z +" \n" )
Result( "Units:" + units_x + " x " + units_y + " x " + units_z +" \n" )
Result("Data type is "+ data_type_value + ".\n")
Result( "This script can not open data with data type integer and pixel size higher than 4. \n")
Exit(0)
}
// Check for a file with same name of riple but with raw extension
file_RAW = -1
filenameRAW = filenameRPL.left(filenameRPL.len() - 3 )
filenameRAW = filenameRAW+"raw"
if(!DoesFileExist(filenameRAW)) // if no such file is found
{
// Check for a file with same name of riple but with bin extension
filenameRAW = filenameRPL.left(filenameRPL.len() - 3 )
filenameRAW = filenameRAW+"bin"
if(!DoesFileExist(filenameRAW))
// if no such file is found, prompts the user for a raw or bin file
{
OkDialog("Raw or Bin file not found in path. Please select file manually." )
If (!OpenDialog(filenameRAW)) Exit(0)
}
}
//opens the raw (or bin or file given by user) file
file_RAW = OpenFileForReading(filenameRAW)
file_stream = NewStreamFromFileReference(file_RAW, 1)
imagename = PathExtractFileName(filenameRAW,0)
//Create an image depending on the data-type
if(data_type_value=="signed")
img := IntegerImage(imagename, pixel_size, 1, size_x, size_y, size_z)
if(data_type_value=="float")
img := RealImage(imagename, pixel_size, size_x, size_y, size_z)
if(data_type_value=="unsigned")
img := IntegerImage(imagename, pixel_size, 0, size_x, size_y, size_z)
//if data-type was not given or recognized
if(!ImageIsValid(img))
{
result("Unrecognized data-type value. Data-type formats are:signed, unsigned and float. Using float by default."+"\n")
img := RealImage(imagename, pixel_size, size_x, size_y, size_z)
}
//if the data is recorded by vector, create another image to change the data order
if(recorded_by_value=="vector")
{
if(data_type_value=="signed")
img2 := IntegerImage(imagename, pixel_size, 1, size_z, size_x, size_y)
if(data_type_value=="float")
img2 := RealImage(imagename, pixel_size, size_z, size_x, size_y)
if(data_type_value=="unsigned")
img2 := IntegerImage(imagename, pixel_size, 0, size_z, size_x, size_y)
if(!ImageIsValid(img2))
{
img2 := RealImage(imagename, pixel_size, size_z, size_x, size_y)
}
}
if(recorded_by_value=="vector")
{
if(size_x>1) // if it is an spectrum-image
{
ImageReadImageDataFromStream(img2, file_stream, 0)
img = img2[iplane,icol,irow]
Closeimage(img2)
}
else //if the data is just spectra
{
ImageReadImageDataFromStream(img2, file_stream, 0)
Closeimage(img)
img := img2
}
}
else // recorded-by should be "image" or "dont-care"
ImageReadImageDataFromStream(img, file_stream, 0)
//data reading if finished
CloseFile(file_RAW)
if(ImageGetDataElementBitSize(img)!=32)
{
if(ImageGetDataElementBitSize(img)<32)
{
img3 := RealImage(imagename, 4, size_x, size_y, size_z)
img3 = img
Closeimage(img)
img := img3
result("Warning: Data-type value changed to float (32 bit)."+"\n")
}
else
if((recorded_by_value=="vector")&&(size_x>1))
{
if(TwoButtonDialog("Data format is float 64. In order to use the Spectrum Picker, it should be clipped to float 32. Do you what to clip this data to float 32?", "Yes", "No"))
{
img3 := RealImage(imagename, 4, size_x, size_y, size_z)
img3 = img
Closeimage(img)
img := img3
result("Warning: Data-type value changed to float (32 bit)."+"\n")
}
}
}
if((data_type_value=="unsigned")|(data_type_value=="signed"))
{
if(TwoButtonDialog("Data format is integer 32. In order to use the Spectrum Picker, it should be clipped to float 32. Do you what to clip this data to float 32?", "Yes", "No"))
{
img3 := RealImage(imagename, 4, size_x, size_y, size_z)
img3 = img
Closeimage(img)
img := img3
result("Warning: Data-type value changed to signed float (32 bit)."+"\n")
}
}
//Add scale calibration and format to the image
if(size_x>1)
{
img.ImageSetDimensionOrigin(0, origin_x )
img.ImageSetDimensionScale( 0, scale_x )
img.ImageSetDimensionUnitString(0, units_x )
}
if(size_y>1)
{
img.ImageSetDimensionOrigin(1, origin_y )
img.ImageSetDimensionScale( 1, scale_y )
img.ImageSetDimensionUnitString(1, units_y )
}
if(size_z>1)
{
img.ImageSetDimensionOrigin(2, origin_z )
img.ImageSetDimensionScale( 2, scale_z )
img.ImageSetDimensionUnitString(2, units_z )
}
//Add corresponding tag for EELS or EDS
if(signal.find("EDS")!=-1)
{
img = SpectrumConvertToEDS(img)
result("Dataset converted to EDS\n")
}
if(signal=="EELS")
{
img = SpectrumConvertToEELS(img)
result("Dataset converted to EELS\n")
}
//if(size_x==1) //if the data is a collection of spectra, display in raster mode
//setDisplayType(img,4)
//UpdateImage (Img)
return img;
}
void importRPL()
{
string filenameRPL
image img
//Prompt the user to locate the Ripple file
If (!OpenDialog(filenameRPL)) Exit(0)
img := OpenRPL(filenameRPL);
img.ShowImage()
}
// Main program starts here
importRPL()