IDL列数据操作——Read Ascii() 和 Ascii_template()

标签:
read_asciiascii_template列数据idlit |
分类: 技术 |
READ_ASCII
The READ_ASCII function reads data from an ASCII file into an IDL structure variable. READ_ASCII may be used with templates created by the ASCII_TEMPLATE function.
This routine handles ASCII files consisting of an optional header of a fixed number of lines, followed by columnar data. One or more rows of data constitute a record. Each data element within a record is considered to be in a different column, or field. The data in one field must be of, or promotable to, a single type (e.g., FLOAT). Adjacent fields may be collected into multi-column fields, called groups. Files may also contain comments, which exist between a user-specified comment string and the corresponding end-of-line.
READ_ASCII is designed to be used with templates created by the ASCII template function.
ASCII_TEMPLATE
The
ASCII_TEMPLATE function
presents a graphical user interface (GUI) which generates a
template defining an ASCII file format. Templates are IDL structure
variables that may be used when reading ASCII files with the
READ_ASCII routine.
由以上说明可以知道,Read_ascii()函数读入文本文件,存储到一个结构体变量中进行进一步处理,而Ascii_template()函数为列数据处理提供了更加交互的界面方式,从而可以自主选择待处理文本。
因此,列文本数据的处理可以分为两种:直接使用Read_Ascii()或者联合Ascii_template()进行交互处理。交互处理的优势在于操作简便,清晰易读,对数据范围可以灵活选择,适用于单文件处理。而非交互式则适用于批量数据处理,效率更高,但要求也较高,需要设计者对数据本身有更全面的了解。(两个函数的详细说明请参考IDL官方帮助)
首先我们来看仅使用Read_Ascii()读取规定范围的列数据。举例说明:有一个后缀名为.asc的光谱文件,我们从第17列开始读取。
fname=dialog_pickfile(path='D:\IGSNRR\',filter='*.asc',/read)
if fname eq '' then begin
asc=read_ascii(fname,count=count,data_start=16);count为已读取的行数,data_start为要跳过的行数
wal=asc.field1[0,*];注意到asc是一个结构体,所有列数据均存储在名为asc.field1的一个二维数组中
ref=asc.field1[1,*];
print,'count',count;
;help,asc;
p=plot(wal,ref,title='AvaField Spectrum',xtitle='Wavelength',ytitle='Reflectance',$
接下来,我们以交互GUI方式处理同样的列数据。代码如下:
fname=dialog_pickfile(path='D:\IGSNRR\',filter='*.asc',/read)
if fname eq '' then begin
ftemplate=ascii_template(fname);
asc=read_ascii(fname,template=ftemplate,count=count);
print,'Read Rows:',count;
wal=asc.field1;
ref=asc.field2;
以上这段示例代码与之前代码不同之处在于增添了template关键字,这段代码的执行顺序是这样的:首先使用ascii_template()对列数据进行初始化并生成基于当前文件的模版(以交互的方式),read_ascii()函数读取该模版。ascii_template(fname)会弹出如下界面:
http://s6/mw690/6bb9fb09gx6C5jZWxBX95&690Ascii()
首先要选择数据的起始行,注意图中的选择位置。
http://s5/mw690/6bb9fb09gx6C5k019sgb4&690Ascii()
第二步,选择列数据的分割方式,支持空格,tab,逗号等等。
http://s10/mw690/6bb9fb09gx6C5k04Fo519&690Ascii()
经过前两步,可见该文本数据一共有三列,在结构体中分别对应Field1,field1,field3,注意此处与前文field1的区别。
Finish之后就使用read_ascii()进行读取,再转入数据的进一步处理,此处我们使用plot生成了反射率图像。
http://s16/mw690/6bb9fb09gx6C5k09qRpcf&690Ascii()