18、lun

在IDL的使用中,经常看到lun/get_lunfree_lun等,到底这个lun是什么东西?有什么用呢?参量IDL帮助文件和网上的资料,整理如下:

1. lun是啥?

The GET_LUN procedure allocates a file unit from a pool of free units. Instead of writing routines to assume the use of certain file units, IDL functions and procedures should use GET_LUN to reserve unit numbers in order to avoid conflicts with other routines. Use FREE_LUN to free the file units when finished.
【GET_LUN过程从可用单元池中分配文件单元】
IDL函数和过程应该使用GET_LUN来保留单元号,以避免与其他例程冲突,而不是编写例程来假定使用某些文件单元。 完成后使用FREE_LUN释放文件单元。

2. lun有啥用?

Instead of explicitly specifying a file unit number(文件单元号) that may already be used, use
 GET_LUN to obtain a free one and store the result in the variable U by entering:
GET_LUN, U

Now U can be used in opening a file:

OPENR, U, 'file.dat'

Once the data from “file.dat” has been read, the file can be closed and the file unit can be freed with the command:

FREE_LUN, U

Note also that OPENR has a GET_LUN keyword that allows you to simultaneously obtain a free file unit and open a file. The following command performs the same tasks as the first two commands above:

OPENR, U, 'file.dat', /GET_LUN

小结:
GET_LUN的功能是为文件分配一个文件单元号,以便后期可以打开、读取和关闭文件

3. 如何用?

以读取.txt文件为例

pro ReadTXT

  COMPILE_OPT idl2
  envi, /restore_base_save_files
  envi_batch_init, log_file='batch.txt'


  txtname="C:\Users\HP\Desktop\Temporary.txt"
  if file_test(txtname)then begin;The FILE_TEST function checks files for existence
  ;and other attributes without having to first open the file/用来检查文件是否存在及在不打开文件的情况下检查其属性
    nLines=file_lines(txtname);file_lines()用于返回数据的行
    tmp='';两个单引号
    
;  打开文件
    openr,lun,txtname,/get_lun
    while(~EOF(lun))do begin;
      readf,lun,tmp;读取第一行                  
;      print,tmp
;      help,tmp
      Col=strsplit(tmp,/extract);拆分第一行获取列数
      ColNum=N_elements(Col)
      Arr_1=fltarr(ColNum,nLines-1);定义一个n列n-1行,元素为0的数组
      readf,lun,Arr_1
      print,lun
      Arr_1=[[Col],[Arr_1]];重新组合数组(n,n-1)+第一行=(n,n)      
    endwhile
    print,Arr_1[0:1,*]
    help,Arr_1[0:1,*]
  end


end
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值