2016-09-26 33 views
1

我導入的元數據具有預定義的嵌套結構(示例如下所示),這是在導入到DM後的單個字符串。 整個元數據和每個分支級別都包含在大括號{}中,所有的鍵和鍵值都包含在引號「」中並用冒號分隔:如何將嵌套字符串(元數據)包裝到TagGroup中

我的問題是,如何轉換數據幷包裝它們轉換成TagGroup對象,以便索引,搜索和數據訪問操作可以更容易完成?

謝謝!

下面是一個例子:

{ 
    "Acquisition": { 
     "AcquisitionStartDatetime": { 
      "DateTime": "1473763749" 
     }, 
     "AcquisitionDatetime": { 
      "DateTime": "0" 
     }, 
     "BeamType": "", 
     "SourceType": "Monochromator" 
    }, 
    "BinaryResult": { 
     "AcquisitionUnit": "", 
     "CompositionType": "", 
     "DetectorIndex": "3", 
     "Detector": "HAADF", 
     "PixelSize": { 
      "width": "5.408370946750477e-010", 
      "height": "5.408370946750477e-010" 
     }, 
     "PixelUnitX": "m", 
     "PixelUnitY": "m", 
     "Offset": { 
      "x": "-2.769085924736244e-007", 
      "y": "-2.769085924736244e-007" 
     }, 
     "Encoding": "" 
    }, 
    "Sample": "", 
    "GasInjectionSystems": "" 
} 
+0

其實這個元數據是從FEI 1.2 Velox的數據(轉換.emd),一種基於HDF5的文件格式。我正試圖實施一個工作腳本來做文件導入。任何幫助,將不勝感激! – w4m

+0

看來你需要一個適度複雜的遞歸字符串解析器,這是不容易提供的作爲這個論壇上的問題的簡短答案。我爲XML導入到DM中做了類似的解析器,並且有興趣幫助您處理應用程序。如果您對協作努力感興趣,請通過我的個人資料中提供的網站/聯繫信息與我聯繫。 –

+0

有一個Velox導入腳本肯定是很多人可能感興趣的東西!一旦你有一些工作,如果你可以將它提交給[DM腳本數據庫](http://www.felmi-zfe.at/dm-script/),那將是非常好的。 – BmyGuest

回答

1

編輯:現在固定碼

我敢肯定,一些更加清理版本是可能的,但它的任務是:

Class CMetaStringToTagGroup 
{ 
    // Find next string bracketed by " in input string, starting search 
    // at given index. Returns string and end-position of search 
    string FindNextKeyName(object self, string input, number & searchPos) 
    { 
     number totalLength = len(input) 
     number start = 0, end = 0 
     while(searchPos < totalLength) 
     { 
      searchpos++ 
      if ("\"" == input.mid(searchpos-1,1)) 
      { 
       if (!start) 
        start = searchpos-1 
       else 
        { 
        end = searchpos-1 
        return input.mid(start+1,end-start-1) 
        } 
      } 
     } 
     return "" 
    } 

    // Returns the next of either "{" , "}" or """ after a collon ":" 
    string GetNextIndicator(object self, string input, number & searchPos) 
    { 
     number totalLength = len(input) 
     while(searchPos < totalLength) 
     { 
      searchpos++   
      if ("{" == input.mid(searchpos-1,1)) 
       return "{" 
      if ("}" == input.mid(searchpos-1,1)) 
       return "}" 
      if ("\"" == input.mid(searchpos-1,1)) 
       return "\"" 
     } 
     return "" 
    } 

    // In a tag-path string, find location of last colon 
    number findLastColon(object self, string input) 
    { 
     number totalLength = len(input) 
     number lastPos = -1 
     number searchPos = 0 
     while(searchPos < totalLength) 
     { 
      searchpos++ 
      if (":" == input.mid(searchpos-1,1)) 
       lastPos = searchpos-1 
     } 
     return lastPos 
    } 

    // Parse textstring and create taggroup from it 
    TagGroup CreateTagFromText(object self, string input) 
    { 
     TagGroup rootTG = NewTagGroup() 
     string currentPath = "" 

     number totalLength = len(input) 
     number searchPos = 0 
     number searchPos2 
     string keyName, indicator 
     while(searchPos < totalLength) 
     { 
      // search for new key or closing bracket, whatever first 
      searchPos2 = searchPos 
      indicator = self.GetNextIndicator(input, searchPos2) 
      keyName = self.FindNextKeyName(input, searchPos) 
      if (("}" == indicator) && (searchpos2<searchPos)) 
      { 
       // decrease hierachy 
       number cutPos = self.findLastColon(currentPath) 
       currentPath = left(currentPath, cutPos) 
       result("\n DEC ") 
       searchPos = searchPos2 
      } 
      else 
      { 
       // Either add value or new sub-tagGroup 
       if ("" == keyname) break; // No more keys found 
       indicator = self.GetNextIndicator(input, searchPos) 
       if ("" == indicator) break; // No more indicator found -- should not happen! 

       if ("{" == indicator) 
       { 
        // increase hierachy 
        currentPath += ":" + keyname 
        rootTg.TagGroupSetTagAsTagGroup(currentPath, NewTagGroup()) 
        result("\n INC ("+keyname+")") 
       } 
       else if ("\"" == indicator) 
       { 
        // Add value 
        searchPos-- 
        string valStr = self.FindNextKeyName(input, searchPos) 
        rootTg.TagGroupSetTagAsString(currentPath + ":" + keyname, valStr) 
        result("\n VAL("+keyname+") ") 
       } 
      } 
     } 
     return rootTg 
    } 
} 

{ 
    // Reading input text 
    number fileID = OpenFileForReading("C:\\test.txt") 
    object fStream = NewStreamFromFileReference(fileID,1) 
    string inputStr = fStream.StreamReadAsText(0, fStream.StreamGetSize()) 

    // Parsing text 
    number searchPos = 0 
    TagGroup con = alloc(CMetaStringToTagGroup).CreateTagFromText(inputStr) 
    con.TagGroupopenBrowserwindow("",0) 
} 

enter image description here

+0

非常感謝!結果似乎不完全正確......「採集」,「二元結果」,「樣品」和「氣體注射系統」應該處於同一水平。根據我對元數據結構的觀察,文件夾具有[「FOLDER_NAME」:{]模式,條目具有[「ENTRY_NAME」:「ENTRY_VALUE」]模式,最好跳過第一個{和最後一個}字符。我會嘗試先關注你的代碼... – w4m

+0

@ w4m現在修復它。 – BmyGuest

+0

順便說一句,有沒有任何現有的功能,寫這個漂亮的TagGroup的圖像標籤?根據DM幫助和您的手冊中的文檔,我只找到了「Set ... Note(image,string)」函數組。 – w4m

2

正如邁克指出了意見,這是不是困難的任務乏味。最好在一個單獨的類,它原來的格式

"NAME: {TagGroups標籤名稱加上增加的層次結構級別創建一個小解析器腳本。

"NAME": "VALUE"標籤的標籤名稱和值VALUE的

}成 '減少層次結構級別' 步驟。

請注意,您可以在創建標記組時始終使用字符串,即使您想在稍後的時間點將其作爲編號讀出。

遞歸瀏覽並記住您所在的「標籤組級別」,以便在該級別添加每個新標籤。跳過無效的文本部分。

DigitalMicrograph的F1幫助documenation對串的部分,其中列出你最有可能需要的命令:

String StringAppend(String s1, String s2) 
Number StringCompare(String s1, String s2) 
Boolean StringIsValid(String str) 
String StringToLower(String str) 
String StringToUpper(String str) 
Number len(String str) 
String left(String str, Number count) 
String mid(String str, Number offset, Number count) 
String right(String str, Number count) 
Number find(String s1, String s2) 
Number val(String str) 

此外,我發現它有時是有用的,以用戶的叔操作字符串像在

number isOK = 1 
string str = isOK == 1 ? "true" : "false" 

此外,解析時,注意製表符和行返回字符。 (使用\ t和\ n搜索它們,當在一個字符串中指定int時,可能需要使用「\ n」和「\ t」,因爲\將被解釋爲控制字符)。

相關問題