桃之夭夭分享 http://blog.sciencenet.cn/u/yelloworld

博文

IDL_Code_根据两个TXT文档的关键字列进行合并

已有 2924 次阅读 2012-3-22 08:56 |个人分类:IDL|系统分类:科研笔记| ASCII_Link_2Txtfile

;*******************************************************************************
;*Usage: According to cognominal column between two txtfiles, the subrutine    *
;*will extract the common part and obtain the common  items for each txt file. *
;*finally, combine the two common files together and output to a new txt file. *                                       *
;*******************************************************************************
;*Parameters:                                                                  *
;* in_txt1: infile of ASCII format                                              *
;* link_col1: specified column for searching key
;* item_num1:specified the number of items in txtfile                           *
;* in_txt2: infile of ASCII format                                              *
;* link_col2: sepcified column for searching key
;* item_num2:specified the number of items in txtfile                             *
;* tile_label: if the in_txt file contain the tile.1 for yes, 0 for no         *
;* split_character: specify the character for splitting each of columns $      *
;*                  such as ' ', ',' , '9B'                                    *
;* out_txt: outfile of ASCII format, the same data structure as in_txt file, $ *
;*          but, add the extracted fields from the ENVI image                  *                                                                     *
;*******************************************************************************                                                                  *
pro ASCII_Link_2Txtfile,in_txt1,link_col1,item_num1,in_txt2,link_col2,item_num2,tile_label,split_character, $
                              out_txt
   ;*Query the in_txt1 file
    openr,lun_1,in_txt1,/get_lun
    ;*Query the in_txt1 file
    openr,lun_2,in_txt2,/get_lun
    ;Create outfile
    openw,lun_out,out_txt,/get_lun
    ;processing:
    ;Get items of in_txt file into memory
    temp1=' ';define two temporary string variable to contain each of items of the txt file
    temp2=' '
    ;**************************************
    ;read the title line to jump,if it exists
    if tile_label eq 1 then begin
       readf,lun_1,temp1
       readf,lun_2,temp2
       printf,lun_out,temp1+temp2
    endif
    ; to allocate the array to read the key-column and the txt-items in for each file
    txt1=strarr(item_num1)
    txt2=strarr(item_num2)
    link_key1=strarr(item_num1)
    link_key2=strarr(item_num2)
    for i=1,item_num1 do begin
       readf,lun_1,temp1;read the items of in_txt into memory
       records=strsplit(temp1,split_character,/extract)
       link_key1[i-1]=records[link_col1-1]
       txt1[i-1]=temp1
    endfor
    for i=1,item_num2 do begin
       readf,lun_2,temp2;read the items of in_txt into memory
       records=strsplit(temp2,split_character,/extract)
       link_key2[i-1]=records[link_col2-1]
       txt2[i-1]=temp2
   endfor
   for i=1,item_num1 do begin
      for j=1,item_num2 do begin
      ;index=where(link_key2=link_key1[i-1],count)
      if link_key1[i-1] eq link_key2[j-1] then begin
         
         printf,lun_out,txt1[i-1]+"#"+txt2[j-1]
         print,txt2[j-1]
         print,i,j
         break
      endif
      endfor
      count=0
   endfor
   free_lun,lun_1
   free_lun,lun_2
   free_lun,lun_out
   close,lun_1
   close,lun_2
   close,lun_out
END


https://blog.sciencenet.cn/blog-219445-550418.html

上一篇:自动设置更改EXCEL的页眉文字
下一篇:IDL_Code图像减法
收藏 IP: 202.99.63.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-20 04:35

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部