multipleregress.R 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env Rscript
  2. #
  3. # This script extract from any 1d (time) to 3d (time-lat-lon) field the
  4. # combined effect of greenhouse gases, ENSO, and any mode of variability
  5. # through a multilinear regression over the corresponding index
  6. # (greenhouse gas concentration, ENSO index ..)
  7. #
  8. # Usage : ./regressedts.R <ncfile> <ncvar> <indexfile1> <indexvar1>
  9. # <indexfile2> <indexvar2> (as many indices as wanted) <outfile>
  10. #
  11. # History : Virginie Guemas - Initial version - 2011
  12. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. library(ncdf)
  14. args=commandArgs(TRUE)
  15. filein=args[1] # Input netcdf file
  16. varin=args[2] # Input netcdf variable name
  17. jind=3
  18. indexes=c() # List of netcdf files containing the indices to regress on
  19. varindex=c() # List of index variable names
  20. while ( jind <= (length(args)-1) ) {
  21. indexes=c(indexes,args[jind])
  22. varindex=c(varindex,args[jind+1])
  23. jind=jind+2
  24. }
  25. fileout=args[jind] # Output file
  26. fnc=open.ncdf(filein)
  27. absent=T
  28. for ( jvar in 1:fnc$nvars ) {
  29. if ( fnc$var[[jvar]]$name == varin ) {
  30. data=fnc$var[[jvar]]
  31. unit=data$units
  32. n0dims=data$ndims
  33. wdimsout=data$dim
  34. var=get.var.ncdf(fnc,varin)
  35. ndims=length(dim(var))
  36. miss=att.get.ncdf(fnc,varin,'_FillValue')
  37. if ( miss$hasatt == TRUE ) { var[var==miss$value]=NA }
  38. miss=att.get.ncdf(fnc,varin,'missing_value')
  39. if ( miss$hasatt == TRUE ) { var[var==miss$value]=NA }
  40. absent=F
  41. }
  42. }
  43. if (absent) { stop("Variable not in input file !") }
  44. close.ncdf(fnc)
  45. system(paste('cdo showmon ',filein,'> mons 2>/dev/null'))
  46. mons=read.table('mons')
  47. system(paste('cdo showyear ',filein,'> years 2>/dev/null'))
  48. years=read.table('years')
  49. system('rm -f mons years')
  50. if (is.null(ndims)) { ndims=1 }
  51. dimsvar=switch(ndims,length(var),dim(var),c(dim(var)[1]*dim(var)[2],dim(var)[3]))
  52. if ( ndims > 1 ) { tmpvar=t(array(var,dim=dimsvar)) } else { tmpvar=var }
  53. var_ts=ts(tmpvar,start=c(years[[1]],mons[[1]]),end=c(years[[length(years)]],mons[[length(mons)]]),frequency=12)
  54. lstind=list()
  55. for (ind in 1:length(indexes) ) {
  56. fnc=open.ncdf(indexes[ind])
  57. vind=get.var.ncdf(fnc,varindex[ind])
  58. close.ncdf(fnc)
  59. system(paste('cdo showmon ',indexes[ind],'> mons 2>/dev/null'))
  60. mons=read.table('mons')
  61. system(paste('cdo showyear ',indexes[ind],'> years 2>/dev/null'))
  62. years=read.table('years')
  63. system('rm -f mons years')
  64. index_ts=ts(vind,start=c(years[[1]],mons[[1]]),end=c(years[[length(years)]],mons[[length(mons)]]),frequency=12)
  65. lstind[[ind]]=index_ts
  66. }
  67. def=TRUE
  68. if ( ndims > 1 ) {
  69. for (jpt in 1:dimsvar[1]) {
  70. varjpt=var_ts[,jpt]
  71. if (length(sort(varjpt)) > 1 ) {
  72. toto = varjpt
  73. f='dataf[,1] ~ '
  74. for (ind in 1:length(indexes)) {
  75. toto = ts.intersect(toto,lstind[[ind]], dframe=FALSE)
  76. f=paste(f,'dataf[,',as.character(ind+1),'] +',sep="")
  77. }
  78. dataf=toto
  79. if (def==TRUE) {
  80. tmp=array(,dim=c(dimsvar[1],dim(toto)[1]))
  81. def=FALSE
  82. }
  83. f=substr(f,start=1,stop=(nchar(f)-1))
  84. dataf=toto
  85. lm.out=lm(as.formula(f),data=toto,na.action=NULL)
  86. tmp[jpt,]=lm.out$fitted.values
  87. }
  88. }
  89. }else{
  90. toto = var_ts
  91. f='dataf[,1] ~ '
  92. for (ind in 1:length(indexes)) {
  93. toto = ts.intersect(toto,lstind[[ind]], dframe=FALSE)
  94. f=paste(f,'dataf[,',as.character(ind),'] +',sep="")
  95. }
  96. f=substr(f,start=1,stop=(nchar(f)-1))
  97. dataf=toto
  98. lm.out=lm(as.formula(f),data=dataf,na.action=NULL)
  99. tmp=lm.out$fitted.values
  100. }
  101. tmp[is.na(tmp)]=1e20
  102. wtime=dim.def.ncdf("time",paste("months since ",as.character(start(toto)[1]),"-",as.character(start(toto)[2]),"-15 12:00:00",sep=""),seq(0,dim(toto)[1]-1))
  103. wdimsout[[n0dims]]=wtime
  104. dimsout=switch(ndims,length(tmp),dim(tmp),c(dim(var)[1],dim(var)[2],dim(tmp)[2]))
  105. wvarout=var.def.ncdf(varin,unit,wdimsout,1e20)
  106. f=create.ncdf(fileout,wvarout)
  107. put.var.ncdf(f,wvarout,array(tmp,dim=dimsout))
  108. close.ncdf(f)
  109. rm(list=ls())
  110. quit()