在IDL的使用中,经常看到lun
、/get_lun
、free_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