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