PEARL Procedures rev-distro-3.1.0-0-gea838b3-dirty
Igor procedures for the analysis of PEARL data
Loading...
Searching...
No Matches
pearl-matrix-import.ipf
Go to the documentation of this file.
1#pragma TextEncoding = "UTF-8"
2#pragma rtGlobals=3
3#pragma version = 1.00
4#pragma IgorVersion = 6.36
5#pragma ModuleName = PearlMatrixImport
6
7// author: matthias.muntwiler@psi.ch
8// Copyright (c) 2016 Paul Scherrer Institut
9
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13// http://www.apache.org/licenses/LICENSE-2.0
14
32
37
38static strconstant package_name = "pearl_matrix_import"
39static strconstant package_path = "root:packages:pearl_matrix_import:"
40
41static strconstant ks_filematch_mtrx = "*_mtrx"
42
46static function init_package()
47
48 dfref savedf = getdatafolderdfr()
49
50 setdatafolder root:
51 newdatafolder /o/s packages
52 newdatafolder /o/s $package_name
53
54 variable /g loglevel = 3
55 string /g dataFilePath = ""
56 string /g resultFilePath = ""
57 variable /g runCycle = 0
58 variable /g scanCycle = 0
59 string /g channelName = ""
60 variable /g brickletID = 0
61 variable /g V_MatrixFileReaderOverwrite = 1
62 variable /g V_MatrixFileReaderFolder = 0
63 variable /g V_MatrixFileReaderDouble = 0
64
65 setdatafolder savedf
66 return 0
67end
68
73static function check_package_folder()
74 dfref df_pack = $(package_path)
75 if (DataFolderRefStatus(df_pack))
76 svar /sdfr=df_pack /z resultFilePath
77 if (!svar_exists(resultFilePath))
79 endif
80 else
82 endif
83end
84
86static function AfterFileOpenHook(refNum,file,pathName,type,creator,kind)
87 Variable refNum,kind
88 String file,pathName,type,creator
89 if( (kind >= 1) && (kind <= 2))
91 //load_prefs()
92 endif
93 return 0
94end
95
103static function BeforeFileOpenHook(refNum,fileName,path,type,creator,kind)
104 Variable refNum,kind
105 String fileName,path,type,creator
106
107 Variable handledOpen = 0
108 if (StringMatch(fileName, ks_filematch_mtrx))
109 setdatafolder root:
110 newdatafolder /o /s matrix
111 mtrx_load_preview("matrix", path, fileName)
112 handledOpen = 1
113 endif
114 return handledOpen
115End
116
121function /s matrix_format_elog_message(metadata)
122 wave /t metadata
123
124 string key
125 string value
126 variable nkeys = dimsize(metadata, 0)
127 variable ikey
128 string message_keys
129 message_keys = "resultFileName;sampleName;channelName;"
130 message_keys += "XYScanner.Points.value;XYScanner.Raster_Time.value;XYScanner.Raster_Time.unit;XYScanner.Width.value;XYScanner.Width.unit;XYScanner.Height.value;XYScanner.Height.unit;"
131 message_keys += "GapVoltageControl.Voltage.value;GapVoltageControl.Voltage.unit;"
132 message_keys += "Regulator.Loop_Gain_1_I.value;Regulator.Loop_Gain_1_I.unit;Regulator.Setpoint_1.value;Regulator.Setpoint_1.unit;"
133 message_keys += "Spectroscopy.Device_1_Start.value;Spectroscopy.Device_1_Start.unit;Spectroscopy.Spectroscopy_Mode.value;"
134 string message
135
136 message_keys = ""
137 for (ikey = 0; ikey < nkeys; ikey += 1)
138 key = metadata[ikey][0]
139 value = metadata[ikey][1]
140 if (WhichListItem(key, message_keys) >= 0)
141 message += key + " = " + value + "\r"
142 endif
143 endfor
144end
145
146function matrix_preview_2d(data, metadata)
147 wave data
148 wave /t metadata
149
150 Display
151 AppendImage data
152 ModifyImage data ctab= {*,*,Mud,0}
153 ModifyGraph margin(left)=30,margin(bottom)=30,margin(top)=5,margin(right)=5,height={Plan,1,left,bottom}
154 ModifyGraph mirror=2
155 ModifyGraph nticks=3
156 ModifyGraph axThick=0.5
157 ModifyGraph btLen=4
158
159end
160
171static function /wave preview_matrix_file(filename)
172 string filename
173
174 dfref saveDF = GetDataFolderDFR()
175 setdatafolder $package_path
176 svar s_preview_file
177 svar s_preview_source
178 mtrx_load_preview("preview_image", "pearl_explorer_filepath", filename)
179 s_preview_file = filename
180 s_preview_source = ""
181 wave /z preview_image
182
183 svar /z s_file_info
184 if (! svar_exists(s_file_info))
185 string /g s_file_info
186 endif
187 if (strlen(s_preview_file) > 0)
188 s_file_info = mtrx_load_info("pearl_explorer_filepath", filename)
189 else
190 s_file_info = ""
191 endif
192
193 setdatafolder saveDF
194 return preview_image
195end
196
198Structure errorCode
199 int32 SUCCESS
200 int32 UNKNOWN_ERROR
201 int32 ALREADY_FILE_OPEN
202 int32 EMPTY_RESULTFILE
203 int32 FILE_NOT_READABLE
204 int32 NO_NEW_BRICKLETS
205 int32 WRONG_PARAMETER
206 int32 INTERNAL_ERROR_CONVERTING_DATA
207 int32 NO_FILE_OPEN
208 int32 INVALID_RANGE
209 int32 WAVE_EXIST
210EndStructure
211
213static Function initStruct(errorCode)
214 Struct errorCode &errorCode
215
216 errorCode.SUCCESS =0
217 errorCode.UNKNOWN_ERROR=10001
218 errorCode.ALREADY_FILE_OPEN=10002
219 errorCode.EMPTY_RESULTFILE=10004
220 errorCode.FILE_NOT_READABLE=10008
221 errorCode.NO_NEW_BRICKLETS=10016
222 errorCode.WRONG_PARAMETER=10032
223 errorCode.INTERNAL_ERROR_CONVERTING_DATA=10064
224 errorCode.NO_FILE_OPEN=10128
225 errorCode.INVALID_RANGE=10256
226 errorCode.WAVE_EXIST=10512
227
228end
229
233
234 struct errorCode errorCode
236
237#if exists("MFR_OpenResultFile")
238 MFR_OpenResultFile
239 if(V_flag != errorCode.SUCCESS)
240 MFR_GetXOPErrorMessage
241 return -1
242 endif
243
244 MFR_GetBrickletData
245 if(V_flag != errorCode.SUCCESS)
246 MFR_GetXOPErrorMessage
247 return -1
248 endif
249
250 MFR_GetBrickletMetaData
251 if(V_flag != errorCode.SUCCESS)
252 MFR_GetXOPErrorMessage
253 return -1
254 endif
255
256 return 0
257#else
258 return -1
259#endif
260end
261
295function mtrx_parse_filename(fileName, resultFile, runCycle, scanCycle, channel)
296 string fileName
297 string &resultFile
298 variable &runCycle
299 variable &scanCycle
300 string &channel
301
302 variable fileType = 0
303 resultFile = ""
304 channel = ""
305 runCycle = 0
306 scanCycle = 0
307
308 string regexp = ""
309 string index1 = ""
310 string index2 = ""
311 string extension = ""
312
313 if (StringMatch(fileName, "*.mtrx"))
314 regexp = "(.+)_([[:digit:]]+)\.(.+)"
315 SplitString /E=regexp fileName, resultFile, index1, extension
316 fileType = 0
317 else
318 regexp = "(.+)--([[:digit:]]+)_([[:digit:]]+)\.((.+)_mtrx)"
319 SplitString /E=regexp fileName, resultFile, index1, index2, extension, channel
320 fileType = 1
321 runCycle = str2num(index1)
322 scanCycle = str2num(index2)
323 endif
324
325 return fileType
326end
327
333function /s mtrx_split_filename(fileName, prefix, datepart, timepart)
334 string fileName
335 string &prefix
336 string &datepart
337 string &timepart
338
339 string regexp
340 regexp = "([[:alpha:][:digit:]]+)[-_]([[:alpha:][:digit:]]+)[-_]([[:alpha:][:digit:]]+)[-_].+"
341 SplitString /E=regexp fileName, prefix, datepart, timepart
342 return datepart
343end
344
361function /df mtrx_create_folder(fileName, [df_base])
362 string fileName
363 dfref df_base
364
365 if (ParamIsDefault(df_base))
366 df_base = GetDataFolderDFR()
367 endif
368
369 string prefix
370 string datepart
371 string timepart
372 string folderName
373
374 mtrx_split_filename(fileName, prefix, datepart, timepart)
375 folderName = "mtrx_" + datepart + "_" + timepart
376 folderName = CleanupName(folderName, 0)
377
378 dfref df_save = GetDataFolderDFR()
379 setdatafolder root:
380 newdatafolder /o /s $foldername
381 dfref df = GetDataFolderDFR()
382
383 setdatafolder df_save
384 return df
385end
386
406function /df mtrx_get_cycle_folder([df_base, runCycle, scanCycle])
407 dfref df_base
408 variable runCycle
409 variable scanCycle
410
411 dfref df_save = GetDataFolderDFR()
412 dfref df_pack = $(package_path)
413 if (ParamIsDefault(df_base))
414 df_base = GetDataFolderDFR()
415 endif
416 if (ParamIsDefault(runCycle))
417 nvar /sdfr=df_pack defRunCycle = runCycle
418 runCycle = defRunCycle
419 endif
420 if (ParamIsDefault(scanCycle))
421 nvar /sdfr=df_pack defScanCycle = scanCycle
422 scanCycle = defScanCycle
423 endif
424
425 string dfname
426 if ((runCycle >= 1) && (scanCycle >= 1))
427 sprintf dfname, "r%us%u", runCycle, scanCycle
428 setdatafolder df_base
429 dfref df = $dfname
430 if (DataFolderRefStatus(df) == 0)
431 newdatafolder $dfname
432 dfref df = $dfname
433 endif
434 else
435 dfref df = df_base
436 endif
437
438 setdatafolder df_save
439 return df
440end
441
462function mtrx_file_brickletID(resultFile, runCycle, scanCycle, channel)
463 string resultFile
464 variable runCycle
465 variable scanCycle
466 string channel
467
468 dfref df_overview = NewFreeDataFolder()
469 variable link = 1
470 variable id = -1
471 variable idx = 0
472
473 string resultFileName
474#if exists("MFR_OpenResultFile")
475 struct errorCode errorCode
477 do
478 sprintf resultFileName, "%s_%04u.mtrx", resultFile, link
479 MFR_OpenResultFile /K resultFileName
480 if(V_flag != errorCode.SUCCESS)
481 return -1
482 endif
483 MFR_CreateOverviewTable /DEST=df_overview
484 // dimension labels are: brickletID, scanCycleCount, runCycleCount, sequenceID, dimension, channelName
485 wave /t /sdfr=df_overview overviewTable
486 make /n=(dimsize(overviewTable, 0)) /i /u /free runcycles, scancycles, ids, match
487 make /n=(dimsize(overviewTable, 0)) /t /free channels
488 ids = str2num(overviewtable[p][%brickletID])
489 if (runcycle > 0)
490 runcycles = str2num(overviewtable[p][%runCycleCount])
491 else
492 runcycles = runcycle
493 endif
494 if (scancycle > 0)
495 scancycles = str2num(overviewtable[p][%scanCycleCount])
496 else
497 scancycles = scancycle
498 endif
499 if (strlen(channel) > 0)
500 channels = overviewTable[p][%channelName]
501 else
502 channels = channel
503 endif
504 Extract /FREE ids, match_ids, (scancycles == scanCycle) && (runcycles == runCycle) && (cmpstr(channels, channel) == 0)
505 if (numpnts(match_ids) > 0)
506 id = match_ids[0]
507 else
508 link += 1
509 endif
510 while (id < 0)
511#endif
512 return id
513end
514
546function mtrx_open_file(pathName, fileNameOrPath)
547 string pathName
548 string fileNameOrPath
549
551 dfref df_save = GetDataFolderDFR()
552 dfref df_pack = $(package_path)
553 svar /sdfr=df_pack dataFilePath
554 svar /sdfr=df_pack resultFilePath
555 nvar /sdfr=df_pack runCycle
556 nvar /sdfr=df_pack scanCycle
557 svar /sdfr=df_pack channelName
558 nvar /sdfr=df_pack brickletID
559
560 string loc_resultFileName
561 string loc_resultFilePath
562 variable loc_runCycle
563 variable loc_scanCycle
564 string loc_channelName
565
566 // make sure we have a valid and complete file path
567 GetFileFolderInfo /P=$pathName /Q /Z=2 fileNameOrPath
568 string filePath
569 if ((v_flag == 0) && (v_isFile))
570 filePath = s_path
571 else
572 return -1
573 endif
574
575 // get base file name
576 string fileName
577 string fileDir
578 string baseFileName
579 variable fileType
580 fileName = ParseFilePath(0, filePath, ":", 1, 0)
581 fileDir = ParseFilePath(1, filePath, ":", 1, 0)
582 fileType = mtrx_parse_filename(fileName, baseFileName, loc_runCycle, loc_scanCycle, loc_channelName)
583
584 variable link = 1
585 variable id = -1
586 variable result = -1
587 variable using_cache = 0
588
589 struct errorCode errorCode
591 do
592 sprintf loc_resultFileName, "%s_%04u.mtrx", baseFileName, link
593 loc_resultFilePath = fileDir + loc_resultFileName
594#if exists("MFR_OpenResultFile")
595 if ((strlen(resultFilePath) == 0) || (cmpstr(loc_resultFilePath, resultFilePath) != 0))
596 MFR_OpenResultFile /K loc_resultFilePath
597 if(V_flag != errorCode.SUCCESS)
598 MFR_GetXOPErrorMessage
599 result = -1
600 break
601 endif
602 resultFilePath = loc_resultFilePath
603 if (fileType == 1)
604 dataFilePath = filePath
605 else
606 dataFilePath = ""
607 endif
608 runCycle = 0
609 scanCycle = 0
610 channelName = ""
611 brickletID = 0
612 MFR_CreateOverviewTable /DEST=df_pack
613 if(V_flag != errorCode.SUCCESS)
614 MFR_GetXOPErrorMessage
615 result = -1
616 break
617 endif
618 using_cache = 0
619 else
620 using_cache = 1
621 endif
622#else
623 print "matrixfilereader.xop not installed"
624 result = -2
625 break
626#endif
627 // dimension labels are: brickletID, scanCycleCount, runCycleCount, sequenceID, dimension, channelName
628 wave /t /sdfr=df_pack overviewTable
629 make /n=(dimsize(overviewTable, 0)) /i /u /o df_pack:runCycles, df_pack:scanCycles, df_pack:ids
630 make /n=(dimsize(overviewTable, 0)) /t /o df_pack:channels
631 wave /sdfr=df_pack ids, runCycles, scanCycles
632 wave /t /sdfr=df_pack channels
633 ids = str2num(overviewtable[p][%brickletID])
634 runCycles = str2num(overviewtable[p][%runCycleCount])
635 scanCycles = str2num(overviewtable[p][%scanCycleCount])
636 channels = overviewTable[p][%channelName]
637 result = fileType
638
639 // if a data file is opened, make sure we found the right result file
640 if ((loc_runCycle > 0) && (loc_scanCycle > 0))
641 Extract /FREE ids, match_ids, (runCycles == loc_runCycle) && (scanCycles == loc_scanCycle) && (cmpstr(channels, loc_channelName) == 0)
642 if (numpnts(match_ids) > 0)
643 id = match_ids[0]
644 runCycle = loc_runCycle
645 scanCycle = loc_scanCycle
646 channelName = loc_channelName
647 brickletID = id
648 break
649 elseif (using_cache)
650 resultFilePath = ""
651 else
652 link += 1
653 endif
654 else
655 break
656 endif
657 while (id < 0)
658
659 return result
660end
661
683function /s mtrx_load_preview(destName, pathName, fileName, [traces])
684 string destName
685 string pathName
686 string fileName
687 string traces
688
689 if (ParamIsDefault(traces))
690 traces = "*Up;*Down;*ReUp;*ReDown;"
691 endif
692
693 dfref df_save = GetDataFolderDFR()
694
695 string datanames = ""
696 string datapaths = ""
697 variable filestatus = mtrx_open_file(pathName, fileName)
698 if (filestatus != 1)
699 return ""
700 endif
701
702 dfref df_pack = $(package_path)
703 svar /sdfr=df_pack dataFilePath
704 svar /sdfr=df_pack resultFilePath
705 nvar /sdfr=df_pack runCycle
706 nvar /sdfr=df_pack scanCycle
707 svar /sdfr=df_pack channelName
708 nvar /sdfr=df_pack brickletID
709
710#if exists("MFR_OpenResultFile")
711 dfref df_data = df_save
712 variable /g df_data:V_MatrixFileReaderOverwrite = 1
713 variable /g df_data:V_MatrixFileReaderFolder = 0
714 variable /g df_data:V_MatrixFileReaderDouble = 0
715 struct errorCode errorCode
717 MFR_GetBrickletData /N=destName /R=(brickletID) /S=2 /DEST=df_data
718 if(V_flag == errorCode.SUCCESS)
719 datanames = S_waveNames
720 variable i
721 variable n = ItemsInList(datanames)
722 string s
723 s = StringFromList(0, datanames)
724 wave data = $s
726 if (WaveDims(data) == 2)
727 subtract_line_bg(data)
728 endif
729 datapaths = AddListItem(GetWavesDataFolder(data, 4), datapaths, ";", inf)
730 for (i = 1; i < n; i += 1)
731 s = StringFromList(i, datanames)
732 killwaves /z $s
733 endfor
734 else
735 MFR_GetXOPErrorMessage
736 endif
737 //MFR_GetBrickletMetaData /N=ANickName /R=(brickletID) // /DEST=dfref
738#else
739 print "matrixfilereader.xop not installed"
740#endif
741
742 setdatafolder df_save
743 return datapaths
744end
745
768function /s mtrx_load_file(pathName, fileName, [traces])
769 string pathName
770 string fileName
771 string traces
772
773 if (ParamIsDefault(traces))
774 traces = "*Up;*Down;*ReUp;*ReDown;"
775 endif
776
777 dfref df_save = GetDataFolderDFR()
778
779 string datanames = ""
780 string datapaths = ""
781 variable filestatus = mtrx_open_file(pathName, fileName)
782 if (filestatus != 1)
783 return ""
784 endif
785
786 dfref df_pack = $(package_path)
787 svar /sdfr=df_pack dataFilePath
788 svar /sdfr=df_pack resultFilePath
789 nvar /sdfr=df_pack runCycle
790 nvar /sdfr=df_pack scanCycle
791 svar /sdfr=df_pack channelName
792 nvar /sdfr=df_pack brickletID
793
794#if exists("MFR_OpenResultFile")
795 string resultFileName = ParseFilePath(0, resultFilePath, ":", 1, 0)
796 dfref df_result = mtrx_create_folder(resultFileName, df_base=df_save)
797 dfref df_data = mtrx_get_cycle_folder(df_base = df_result)
798 variable /g df_data:V_MatrixFileReaderOverwrite = 1
799 variable /g df_data:V_MatrixFileReaderFolder = 0
800 variable /g df_data:V_MatrixFileReaderDouble = 0
801
802 struct errorCode errorCode
804 string name
805 name = CleanupName(channelName, 0)
806 MFR_GetBrickletData /N=name /R=(brickletID) /DEST=df_data
807 if(V_flag == errorCode.SUCCESS)
808 datanames = S_waveNames
809 variable i
810 variable n = ItemsInList(datanames)
811 string s
812 for (i = 0; i < n; i += 1)
813 s = StringFromList(i, datanames)
814 wave /sdfr=df_data data = $s
816 datapaths = AddListItem(GetWavesDataFolder(data, 4), datapaths, ";", inf)
817 endfor
818 else
819 MFR_GetXOPErrorMessage
820 endif
821 //MFR_GetBrickletMetaData /N=ANickName /R=(brickletID) // /DEST=dfref
822#else
823 print "matrixfilereader.xop not installed"
824#endif
825
826 setdatafolder df_save
827 return datapaths
828end
829
830function mtrx_scale_dataset(data)
831 wave data
832
833 dfref df_pack = $(package_path)
834 nvar /sdfr=df_pack runCycle
835 nvar /sdfr=df_pack scanCycle
836 svar /sdfr=df_pack channelName
837 nvar /sdfr=df_pack brickletID
838
839 string scanDir = StringFromList(2, NameOfWave(data), "_")
840 if (WaveDims(data) == 2)
841 Note data, "AxisLabelX=X"
842 Note data, "AxisLabelY=Y"
843 endif
844 Note data, "AxisLabelD=" + channelName
845 string title
846 sprintf title, "%u-%u %s %s", runCycle, scanCycle, channelName, scanDir
847 Note data, "Dataset=" + title
848end
849
864function /s mtrx_load_info(APathName, AFileName)
865 string APathName
866 string AFileName
867
868 dfref saveDF = GetDataFolderDFR()
869 dfref fileDF = NewFreeDataFolder()
870 setdatafolder fileDF
871
872 variable fileID
873 string filepath
874 string scanpaths
875 variable nscans
876 variable iscan
877 string scanpath
878 string info = ""
879
880
881 setdatafolder saveDF
882 return info
883end
884
887function subtract_line_bg(img)
888 wave img
889
890 variable nx = dimsize(img, 0)
891 variable ny = dimsize(img, 1)
892 variable iy
893 make /n=(nx) /free line, fit
894 for (iy = 0; iy < ny; iy += 1)
895 line = img[p][iy]
896 if (numtype(sum(line)) == 0)
897 CurveFit /N /Q /NTHR=0 line line /D=fit
898 img[][iy] = line[p] - fit[p]
899 endif
900 endfor
901end
string mtrx_split_filename(string fileName, string *prefix, string *datepart, string *timepart)
split a matrix filename and return the first three parts
dfr mtrx_get_cycle_folder(dfref df_base=defaultValue, variable runCycle=defaultValue, variable scanCycle=defaultValue)
create a data folder for bricklet data.
string mtrx_load_info(string APathName, string AFileName)
load descriptive info from a Matrix data file.
static variable AfterFileOpenHook(variable refNum, string file, string pathName, string type, string creator, variable kind)
initialize the package and reload preferences after an experiment is loaded.
static variable init_package()
initialize the package data folder.
variable mtrx_parse_filename(string fileName, string *resultFile, variable *runCycle, variable *scanCycle, string *channel)
parse matrix file names
variable mtrx_open_file(string pathName, string fileNameOrPath)
open a matrix result or data file
variable mtrx_load_all()
load all data from a Matrix data file.
string mtrx_load_file(string pathName, string fileName, string traces=defaultValue)
load all data from a Matrix data file.
static const string package_path
string matrix_format_elog_message(wave metadata)
generate elog message from bricklet metadata
variable matrix_preview_2d(wave data, wave metadata)
dfr mtrx_create_folder(string fileName, dfref df_base=defaultValue)
create or look up a data folder based on a matrix file name.
static wave preview_matrix_file(string filename)
load the preview of a Matrix data file
variable subtract_line_bg(wave img)
remove linear background line-by-line
string mtrx_load_preview(string destName, string pathName, string fileName, string traces=defaultValue)
load a preview image from a Matrix data file.
static variable check_package_folder()
check that the package data folder exists
static const string package_name
static const string ks_filematch_mtrx
variable mtrx_file_brickletID(string resultFile, variable runCycle, variable scanCycle, string channel)
find out bricklet ID of a file
variable mtrx_scale_dataset(wave data)
static variable BeforeFileOpenHook(variable refNum, string fileName, string path, string type, string creator, variable kind)
open a matrix file that was dropped into Igor.
static variable initStruct(errorCode *errorCode)
from matrixfilereader help
from matrixfilereader help