加载中…
个人资料
Volvop
Volvop
  • 博客等级:
  • 博客积分:0
  • 博客访问:204
  • 关注人气:26
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2013-08-23 19:23:56)
标签:

read_ascii

ascii_template

列数据

idl

it

分类: 技术
首先厘清一个概念,什么是Ascii文件:
ASCII file
A file that contains data made up of ASCII characters. It is essentially raw text just like the words you are reading now. Each byte in the file contains one character that conforms to the standard ASCII code (see ASCII chart). Program source code, batch files, macros and scripts are straight text and stored as ASCII files. HTML and XML files are also ASCII files. Text editors such as Notepad create ASCII files as their native file format.
延展阅读请看:【Ascii VS Binary】http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/asciiBin.html
当然,ASCII文件可以简单理解为TXT文本文件,常见ASCII文本后缀名有'.txt','.dat'等。
——.———————.————————.—————.—————.———
IDL默认以行读取数据,在读取列数据时略麻烦,因此从IDL 5.0版本开始引入了Read_Ascii()和Ascii_template()处理文本列数据。

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

  dlg=dialog_message('Not a valid file!',/information);

  return;

  endif

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',$

       color='red');绘图

接下来,我们以交互GUI方式处理同样的列数据。代码如下:

fname=dialog_pickfile(path='D:\IGSNRR\',filter='*.asc',/read)

if fname eq '' then begin

  dlg=dialog_message('Not a valid file!',/information);

  return;

  endif

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() 和 Ascii_template()" TITLE="IDL列数据操作——Read Ascii() 和 Ascii_template()" />

首先要选择数据的起始行,注意图中的选择位置。

http://s5/mw690/6bb9fb09gx6C5k019sgb4&690Ascii() 和 Ascii_template()" TITLE="IDL列数据操作——Read Ascii() 和 Ascii_template()" />

第二步,选择列数据的分割方式,支持空格,tab,逗号等等。

http://s10/mw690/6bb9fb09gx6C5k04Fo519&690Ascii() 和 Ascii_template()" TITLE="IDL列数据操作——Read Ascii() 和 Ascii_template()" />

经过前两步,可见该文本数据一共有三列,在结构体中分别对应Field1,field1,field3,注意此处与前文field1的区别。

Finish之后就使用read_ascii()进行读取,再转入数据的进一步处理,此处我们使用plot生成了反射率图像。

http://s16/mw690/6bb9fb09gx6C5k09qRpcf&690Ascii() 和 Ascii_template()" TITLE="IDL列数据操作——Read Ascii() 和 Ascii_template()" />



0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有