LZN's Blog CodePlayer

【更新中】ncl读取ASCII文件

2015-01-05
LZN

帮CDH做sst forcing的试验,需要用ncl读入个规整的index (35x12),回归出sst anomalies pattern,测试了下asciiread函数,非常好用,有点类似MATLAB的load函数,能够直接识别分隔符。

拷个example过来

Example 1

Assume you have an ASCII data file called “data.asc”:

 1.  2.  3.
 4.  5.  6.
 7.  8.  9.
10. 11. 12.
13. 14. 15.

You can read it various ways as shown with the following NCL script:

begin  
  z1 = asciiread("data.asc",(/5,3/),"float")
  z2 = asciiread("data.asc",(/4,2/),"float")
end

The results of z1 and z2 would be:

z1(0,0) = 1.
z1(0,1) = 2.
z1(0,2) = 3.
z1(1,0) = 4.
z1(1,1) = 5.
z1(1,2) = 6.
z1(2,0) = 7.
.
.
.
z1(4,0) = 13.
z1(4,1) = 14.


z2(0,0) = 1.
z2(0,1) = 2.
z2(1,0) = 3.
z2(1,1) = 4.
z2(2,0) = 5.
z2(2,1) = 6.
z2(3,0) = 7.
z2(3,1) = 8.

Example of using -1:

begin
	z3 = asciiread("data.asc",-1,"float")
end
z3(0) = 1.
z3(1) = 2.
z3(2) = 3.
.
.
.
z3(14)= 15.

The above z3 could be reshaped via use of onedtond:

     Z3 = onedtond(z3, (/5,3/) )

Comments

Content