- 积分
- 20145
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2014-4-24
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 我是小猪 于 2016-7-15 17:23 编辑
- ; ==============================================================
- ; eof_0a.ncl
- ;此代码可以做出来!!!更新
- ; Concepts illustrated:
- ; - Reading a simple ascii file
- ; - Pretty printing
- ; - Rearranging data order via named dimension
- ; - Weighting the data
- ; - Calculating EOFs and Principal Components (ie: time series)
- ; - Reconstructing the original array from EOFs and PCsby unweighting
- ; - Calculating 'sum-of-square' to verify normalization
- ; - Calculating cross correlations to verify that each is zero.
- ; This verifies that they are orthogonal.
- ; =============================================================
- ; John C Davis
- ; Statistics and Data Analysis in Geology
- ; Wiley, 2nd Edition, 1986
- ; Source Data: page 524 , EOF results: page537
- ; =============================================================
- ; Cosine weighting is performed
- ; =============================================================
- ;;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" ; not needed after 6.0.0
- begin
- ; ================================> ; PARAMETERS
- ncol = 7 ; # columns (stations or grid points)
- nrow = 25 ; # time steps
- neval = 7 ; # EOFs to calculate (max=ncol)
- xmsg = -999.9 ; missing value (_FillValue)
-
- ntim = nrow ; # time steps
- nlat = ncol ; # latitudes
- ; ================================> ; READ THE ASCII FILE
- fname = "/disk1/dzz/data/test.txt" ; test data from Davis text book
- ; open "data " as 2-dim array
- data = asciiread (fname,(/ntim,nlat/), "float")
- data!0 = "time" ; name the dimensions for subsequent use
- data!1 = "lat"
- data@_FillValue = xmsg
- ; ================================> ; CREATE BOGUS LATITUDES & COSINE WEIGHTING
- ; lat = ispan(-60, 60, 20)*1.0
- ; lat!0 = "lat"
- ; lat@units = "degrees_north"
- ; data&lat = lat
- ; printVarSummary(lat)
-
- ; clat = sqrt(cos(lat*0.01745329)) ; [*]; cosine weighting
- ; ================================> ; PRETTY PRINT INPUT DATA
- opt = True
- opt@tspace = 5
- opt@title = "Davis Input Data"
- write_matrix (data, (nlat+"f9.3"), opt)
-
- ; ================================> ; REORDER TO WHAT EOFUNC EXPECTS
-
- x = data(lat | :,time | :) ; reorder ... eofunc want 'time' as rightmost dimension
- printVarSummary(x)
- print("")
- ; CLAT = conform(x, clat, 0) ; create explicit array (not needed; done for illustration)
- ; printVarSummary(CLAT)
- ; print("")
-
- ; ================================> ; REORDER TO WHAT EOFUNC EXPECTS
- ; xw = x*CLAT ; weight the observations; xw -> weighted
- ; copy_VarCoords(x, xw)
- ; printVarSummary(xw)
- ; print("")
-
- ; ================================> ; EOFs on weighted observationsS
- evecv = eofunc_Wrap (x,neval,False)
- evecv_ts = eofunc_ts_Wrap (x,evecv,False)
- print("")
- printVarSummary(evecv)
- print("")
- printVarSummary(evecv_ts)
- print("")
- ; ================================> ; PRETTY PRINT OUTPUT EOF RESULTS
- opt@title = "Eigenvector components from weighted values: evecv"
- write_matrix (evecv(lat|:,evn|:), (nlat+"f9.3"), opt) ; reorder to match book
- print("")
- opt@title = "Eigenvector time series from weighted values: evecv_ts"
- write_matrix (evecv_ts(time|:,evn|:), (nlat+"f9.3"), opt)
- print(" ")
- print("evecv_ts@ts_mean="+evecv_ts@ts_mean)
- print(" ")
- ; ================================> ; SUM OF THE SQUARES
- ; IF NORMALIZED, THEY SHOULD BE 1
- sumsqr = dim_sum(evecv^2)
- print("sum of squares: " + sumsqr)
- print(" ")
- ; ================================> ; RECONSTRUCT DATA:
- ; NCL WORKS ON ANOMALIES
- do n=0,neval-1
- evecv_ts(n,:) = evecv_ts(n,:) + evecv_ts@ts_mean(n) ; add weighted means
- end do
- xRecon = eof2data (evecv,evecv_ts) ; RECONSTRUCTED WEIGHTED DATA
- copy_VarCoords(x, xRecon)
- xRecon = xRecon/CLAT
- printVarSummary(xRecon)
- print(" ")
- opt@title = "Reconstructed data via NCL: default mode: constant offset"
- write_matrix (xRecon(time|:,lat|:), (nlat+"f9.3"), opt)
- print(" ")
- ; ================================> ; MAX ABSOLUTE DIFFERENCE
-
- mxDiff = max(abs(xRecon-x))
- print("mxDiff="+mxDiff)
- print(" ")
- ; ================================> ; ORTHOGONALITY: DOT PRODUCT
- ; 0=>orthogonal
- do ne=0,neval-1
- EVECV = conform(evecv, evecv(ne,:), 1 )
- print("=====> dotp for col "+ne+" <=====")
- do nn = 0, neval - 1
- dotp = dim_sum(evecv*EVECV)
- print("dotp patterns: " + dotp)
- print(" ")
- end do
- end do
- ; ================================> ; CORRELATION
-
- do ne = 0, neval - 1
- corr = escorc(evecv_ts(ne,:),evecv_ts)
- print("corr patterns: " + corr)
- print(" ")
- end do
- end
复制代码
我在NCL官网上找到站点资料的EOF脚本文件,数据也是用的一样的,但是我在xshell4运行之后总是莫名其妙的报错,脚本如下:
原数据如下:
3.760 3.660 0.540 5.275 9.768 13.741 4.782
8.590 4.990 1.340 10.022 7.500 10.162 2.130
6.220 6.140 4.520 9.842 2.175 2.732 1.089
7.570 7.280 7.070 12.662 1.791 2.101 0.822
9.030 7.080 2.590 11.762 4.539 6.217 1.276
5.510 3.980 1.300 6.924 5.326 7.304 2.403
3.270 0.620 0.440 3.357 7.629 8.838 8.389
8.740 7.000 3.310 11.675 3.529 4.757 1.119
9.640 9.490 1.030 13.567 13.133 18.519 2.354
9.730 1.330 1.000 9.871 9.871 11.064 3.704
8.590 2.980 1.170 9.170 7.851 9.909 2.616
7.120 5.490 3.680 9.716 2.642 3.430 1.189
4.690 3.010 2.170 5.983 2.760 3.554 2.013
5.510 1.340 1.270 5.808 4.566 5.382 3.427
1.660 1.610 1.570 2.799 1.783 2.087 3.716
5.900 5.760 1.550 8.388 5.395 7.497 1.973
9.840 9.270 1.510 13.604 9.017 12.668 1.745
8.390 4.920 2.540 10.053 3.956 5.237 1.432
4.940 4.380 1.030 6.678 6.494 9.059 2.807
7.230 2.300 1.770 7.790 4.393 5.374 2.274
9.460 7.310 1.040 11.999 11.579 16.182 2.415
9.550 5.350 4.250 11.742 2.766 3.509 1.054
4.940 4.520 4.500 8.067 1.793 2.103 1.292
8.210 3.080 2.420 9.097 3.753 4.657 1.719
9.410 6.440 5.110 12.495 2.446 3.103 0.914
我运行之后总是报错:
fatal:Variable (evecv) is undefined
fatal:["Execute.c":8565]:Execute: Error occurred at or near line 306
;我的脚本如下:
(不知道我的理解对不对,比如我有全国160站点降水资料的话,用该脚本文件运行之后得出的空间场和时间系数(层次*模态数),然后对时间系数再画xy-plot,对空间场先做NCL插值然后再做contour-plot,是这样的吗?)
- <div>; ==============================================================
- ; eof_0_640.ncl
- ;
- ; Concepts illustrated:
- ; - Reading a simple ascii file
- ; - Pretty printing
- ; - Rearranging data order via named dimension
- ; - Calculating EOFs and Principal Components (ie: time series)
- ; - Reconstructing the original array from EOFs and PCs
- ; - Calculating 'sum-of-square' to verify normalization
- ; - Calculating cross correlations to verify that each is zero.
- ; This verifies that they are orthogonal.
- ; =============================================================
- ; This script shows how to use the new eofunc_n function added
- ; in NCL V6.4.0 to avoid having to reorder the data before
- ; calculating the EOFs.
- ; =============================================================
- ; John C Davis
- ; Statistics and Data Analysis in Geology
- ; Wiley, 2nd Edition, 1986
- ; Source Data: page 524 , EOF results: page537
- ; =============================================================
- ; No weighting is performed
- ; =============================================================
- ; This file is loaded by default in NCL V6.2.0 and newer
- ; load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" ; not needed after 6.0.0
- ; ================================> ; PARAMETERS
- begin
- ncol = 7 ; # columns (stations or grid points)
- nrow = 25 ; # time steps
- neval = 7 ; # EOFs to calculate (max=ncol)
- xmsg = -999.9 ; missing value (_FillValue)
-
- ntim = nrow ; # time steps
- nsta = ncol ; # stations ( or grid points)
- ; ================================> ; READ THE ASCII FILE
- ; dir = "./"
- fname = "/disk1/dzz/data/test.txt" ; test data from Davis text book
- ; open "data " as 2-dim array
- data = asciiread (fname,(/ntim,nsta/), "float")
- data!0 = "time" ; name the dimensions for subsequent use
- data!1 = "sta"
- data@_FillValue = xmsg
- ; ================================> ; PRETTY PRINT INPUT DATA
- opt = True
- opt@tspace = 5
- opt@title = "Davis Input Data"
- write_matrix (data, (nsta+"f9.3"), opt)
-
- printVarSummary(data)
- print ("==========> EOFUNC_N <==========")
- evecv = eofunc_Wrap (data,neval,False,0)
- evecv_ts = eofunc_ts_Wrap (data,evecv,False,0)
- print("")
- printVarSummary(evecv)
- print("")
- printVarSummary(evecv_ts)
- print("")
- ; ================================> ; PRETTY PRINT OUTPUT EOF RESULTS
- opt@title = "Eigenvector components: evecv"
- write_matrix (evecv(sta|,evn|:), (ncol+"f9.3"), opt) ; reorder to match book
- print("")
- opt@title = "Eigenvector time series: evecv_ts"
- write_matrix (evecv_ts(time|,evn|:), (ncol+"f9.3"), opt)
- print(" ")
- print("evecv_ts@ts_mean="+evecv_ts@ts_mean)
- print(" ")
- ; ================================> ; SUM OF THE SQUARES
- ; IF NORMALIZED, THEY SHOULD BE 1
- sumsqr = dim_sum(evecv^2)
- print("sum of squares: " + sumsqr)
- print(" ")
- ; ================================> ; RECONSTRUCT DATA
- ; NCL WORKS ON ANOMALIES
- do n=0,neval-1
- evecv_ts(n,:) = evecv_ts(n,:) + evecv_ts@ts_mean(n) ; add time series mean
- end do
- xRecon = eof2data_n (evecv,evecv_ts,0) ; RECONSTRUCTED DATA
- copy_VarCoords(data, xRecon)
- printVarSummary(xRecon)
- print(" ")
- opt@title = "Reconstructed data via NCL: default mode: constant offset"
- write_matrix (xRecon, (nsta+"f9.3"), opt)
- print(" ")
- ; ================================> ; MAX ABSOLUTE DIFFERENCE
-
- mxDiff = max(abs(xRecon-data))
- print("mxDiff="+mxDiff)
- print(" ")
- ; ================================> ; ORTHOGONALITY: DOT PRODUCT
- ; 0=>orthogonal
- do ne=0,neval-1
- EVECV = conform(evecv, evecv(ne,:), 1 )
- print("=====> dotp for col "+ne+" <=====")
- do nn = 0, neval - 1
- dotp = dim_sum(evecv*EVECV)
- print("dotp patterns: " + dotp)
- print(" ")
- end do
- end do
- ; ================================> ; CORRELATION
-
- do ne = 0, neval - 1
- corr = escorc(evecv_ts(ne,:),evecv_ts)
- print("corr patterns: " + corr)
- print(" ")
- end do
- end</div>
复制代码
原脚本是eof_0.ncl那个例子
一开始脚本文件用的是eofunc_n_Wrap和eofunc_ts_n_Wrap函数,但是我运行之后报错:
fatal:["Execute.c":8565]:Execute: Error occurred at or near line 177
Undefined identifier: (eofunc_n_Wrap) is undefined, can't continue
所以去查这个函数官网上说其适用于NCL6.4及其之后版本,所以我改用eofunc_Wrap和eofunc_Wrap,运行之后继续报错:
fatal:["Execute.c":8565]:Execute: Error occurred at or near line 556
Variable (evecv) is undefined
继续修改函数,将 evecv = eofunc_Wrap (data,neval,False,0)改为: evecv = eofunc_Wrap (data,neval,False) (我没看懂官网上的“0”指的是什么意思),然后报错:
Undefined identifier: (eof2data_n) is undefined, can't continue
fatal:["Execute.c":8565]:Execute: Error occurred at or near line 711
,其实到此已经出现eof2data_n函数之前的结果:
Variable: evecv
Dimensions and sizes: [evn | 7] x [time | 25]
;
Variable: evecv_ts
Number of Dimensions: 2
Dimensions and sizes: [evn | 7] x [sta | 7]
但是奇怪的是,空间场和时间系数按理说应该分别是7*sta(7),和7*time(25),但是输出为什么会反着来呢,而且最终的结果实在老报错输出不来。
小弟才疏学浅,望各位不吝赐教,实在是纠结了好几天啊(如大家找不粗问题的话可以借鉴我的错误的流程)
|
|