test_EOF.R 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. source('../CFU_Rfunc.txt')
  2. library(s2dverification)
  3. #
  4. # A little testing script by Virginie
  5. #
  6. nt=6
  7. ny=5
  8. nx=4
  9. neofs=min(nt,nx*ny)
  10. a=array(rnorm(nt*ny*nx),dim=c(nt,ny,nx))
  11. a=a-InsertDim(Mean1Dim(a,1),1,nt)
  12. lon=c(20,25,30,35)
  13. lat=c(20,25,30,35,40)
  14. a[c(18,22)]=NA
  15. # Weight computation
  16. #wgt=array(cos(lat*pi/180),dim=c(ny,nx))
  17. #wgt <- wgt/sum(wgt,na.rm=T)
  18. #wgt <- sqrt(wgt)
  19. #wgt <- array(1,dim=c(ny,nx))
  20. #Within CFU_EOF
  21. b=CFU_EOF(lon,lat,a,neofs=neofs)
  22. #Without CFU_EOF
  23. #abis=array(a*InsertDim(wgt,1,nt),dim=c(nt,ny*nx))
  24. #b=La.svd(abis)
  25. #EOF=array(b$v,dim=c(neofs,ny,nx))
  26. #PC=b$u
  27. test=array(0,dim=dim(a))
  28. for (i in 1:nt) {
  29. for (j in 1:neofs) {
  30. # Check within CFU_EOF
  31. #test[i,,]=test[i,,]+b$PCs[i,j]*b$EOFs[j,,]*b$D[j]/wgt
  32. #test[i,,]=test[i,,]+b$PCs[i,j]*b$EOFs[j,,]/wgt # new PC normalization
  33. test[i,,]=test[i,,]+b$PCs[i,j]*b$EOFs[j,,] # new EOF and PC normalization
  34. # Check without CFU_EOF
  35. #test[i,,]=test[i,,]+PC[i,j]*EOF[j,,]*b$d[j]/wgt
  36. }
  37. }
  38. print("Restitution of the original field after decomposing by CFU_EOF")
  39. print("The difference below should be of the order of the machine precision:")
  40. print(max(abs(test-a),na.rm=T))
  41. test2=array(0,dim=c(nt,neofs))
  42. for (j in 1:neofs) {
  43. proj=CFU_MODE(InsertDim(InsertDim(InsertDim(a,1,1),2,1),4,1),b,mode=j)
  44. for (i in 1:nt) {
  45. # Check within CFU_MODE
  46. test2[i,j]=test2[i,j]+proj$PC.ver[1,1,i,1]
  47. # Check without CFU_MODE
  48. #test2[i,j]=test2[i,j]+sum(a[i,,]*EOF[j,,]/b$d[j]/wgt)
  49. }
  50. }
  51. print("Restitution of the PC from CFU_EOF when projecting with CFU_MODE")
  52. print("The difference below should be of the order of the machine precision:")
  53. #diag=test2-PC
  54. diag=test2-b$PCs
  55. #diag[which(1e-14>abs(InsertDim(b$d,1,nt)))]=0 # If the eigenvalue is of the order
  56. # of the machine precision ~ 1e-15 then dividing by it gives a huge PC by projection
  57. # hence I do not consider the reconstruction in such case.
  58. diag[which(1e-5>abs(InsertDim(b$Var,1,nt)))]=0 # If the eigenvalue is of the order
  59. # of the machine precision ~ 1e-15 then dividing by it gives a huge PC by projection
  60. # hence I do not consider the reconstruction in such case.
  61. print(max(abs(diag)))