[135] | 1 | subroutine mkstats(ierr) |
---|
| 2 | |
---|
| 3 | ! |
---|
| 4 | ! This program writes a stats.nc file from sums and sums of squares |
---|
| 5 | ! to means and standard deviations and also writes netcdf style |
---|
| 6 | ! file so that the data can be viewed easily. The data file is |
---|
| 7 | ! overwritten in place. |
---|
| 8 | ! SRL 21 May 1996 |
---|
| 9 | ! Yann W. july 2003 |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | implicit none |
---|
| 13 | |
---|
| 14 | #include "dimensions.h" |
---|
| 15 | #include "statto.h" |
---|
| 16 | #include "netcdf.inc" |
---|
| 17 | |
---|
| 18 | integer,parameter :: iip1=iim+1 |
---|
| 19 | integer,parameter :: jjp1=jjm+1 |
---|
| 20 | integer :: ierr,nid,nbvar,i,ndims,lt,nvarid |
---|
| 21 | integer, dimension(4) :: id,varid,start,size |
---|
| 22 | integer, dimension(5) :: dimids |
---|
| 23 | character (len=50) :: name,nameout,units,title |
---|
| 24 | real, dimension(iip1,jjp1,llm) :: sum3d,square3d,mean3d,sd3d |
---|
| 25 | real, dimension(iip1,jjp1) :: sum2d,square2d,mean2d,sd2d |
---|
| 26 | real, dimension(istime) :: time |
---|
| 27 | real, dimension(jjp1) :: lat |
---|
| 28 | real, dimension(iip1) :: lon |
---|
| 29 | real, dimension(llm) :: alt |
---|
| 30 | logical :: lcopy=.true. |
---|
| 31 | !integer :: latid,lonid,altid,timeid |
---|
| 32 | integer :: meanid,sdid |
---|
| 33 | !integer, dimension(4) :: dimout |
---|
| 34 | |
---|
| 35 | ! Incrementation of count for the last step, which is not done in wstats |
---|
| 36 | count(istime)=count(istime)+1 |
---|
| 37 | |
---|
| 38 | ierr = NF_OPEN("stats.nc",NF_WRITE,nid) |
---|
| 39 | |
---|
| 40 | ! We catch the id of dimensions of the stats file |
---|
| 41 | |
---|
| 42 | ierr= NF_INQ_DIMID(nid,"latitude",id(1)) |
---|
| 43 | ierr= NF_INQ_DIMID(nid,"longitude",id(2)) |
---|
| 44 | ierr= NF_INQ_DIMID(nid,"altitude",id(3)) |
---|
| 45 | ierr= NF_INQ_DIMID(nid,"Time",id(4)) |
---|
| 46 | |
---|
| 47 | ierr= NF_INQ_VARID(nid,"latitude",varid(1)) |
---|
| 48 | ierr= NF_INQ_VARID(nid,"longitude",varid(2)) |
---|
| 49 | ierr= NF_INQ_VARID(nid,"altitude",varid(3)) |
---|
| 50 | ierr= NF_INQ_VARID(nid,"Time",varid(4)) |
---|
| 51 | |
---|
| 52 | ! Time initialisation |
---|
| 53 | |
---|
| 54 | do i=1,istime |
---|
| 55 | time(i)=i*24./istime |
---|
| 56 | #ifdef NC_DOUBLE |
---|
| 57 | ierr= NF_PUT_VARA_DOUBLE(nid,varid(4),i,1,time(i)) |
---|
| 58 | #else |
---|
| 59 | ierr= NF_PUT_VARA_REAL(nid,varid(4),i,1,time(i)) |
---|
| 60 | #endif |
---|
| 61 | enddo |
---|
| 62 | |
---|
| 63 | ! We catche the values of the variables |
---|
| 64 | |
---|
| 65 | #ifdef NC_DOUBLE |
---|
| 66 | ierr = NF_GET_VAR_DOUBLE(nid,varid(1),lat) |
---|
| 67 | ierr = NF_GET_VAR_DOUBLE(nid,varid(2),lon) |
---|
| 68 | ierr = NF_GET_VAR_DOUBLE(nid,varid(3),alt) |
---|
| 69 | #else |
---|
| 70 | ierr = NF_GET_VAR_REAL(nid,varid(1),lat) |
---|
| 71 | ierr = NF_GET_VAR_REAL(nid,varid(2),lon) |
---|
| 72 | ierr = NF_GET_VAR_REAL(nid,varid(3),alt) |
---|
| 73 | #endif |
---|
| 74 | |
---|
| 75 | ! We catch the number of variables in the stats file |
---|
| 76 | ierr = NF_INQ_NVARS(nid,nbvar) |
---|
| 77 | |
---|
| 78 | ! to catche the "real" number of variables (without the "additionnal variables") |
---|
| 79 | nbvar=(nbvar-4)/2 |
---|
| 80 | |
---|
| 81 | do i=1,nbvar |
---|
| 82 | varid=(i-1)*2+5 |
---|
| 83 | |
---|
| 84 | ! What's the variable's name? |
---|
| 85 | ierr=NF_INQ_VARNAME(nid,varid,name) |
---|
| 86 | write(*,*) "OK variable ",name |
---|
| 87 | ! Its units? |
---|
| 88 | units=" " |
---|
| 89 | ierr=NF_GET_ATT_TEXT(nid,varid,"units",units) |
---|
| 90 | ! Its title? |
---|
| 91 | title=" " |
---|
| 92 | ierr=NF_GET_ATT_TEXT(nid,varid,"title",title) |
---|
| 93 | ! Its number of dimensions? |
---|
| 94 | ierr=NF_INQ_VARNDIMS(nid,varid,ndims) |
---|
| 95 | ! Its values? |
---|
| 96 | |
---|
| 97 | if(ndims==4) then ! lat, lon, alt & time |
---|
| 98 | |
---|
| 99 | ! dimout(1)=lonid |
---|
| 100 | ! dimout(2)=latid |
---|
| 101 | ! dimout(3)=altid |
---|
| 102 | ! dimout(4)=timeid |
---|
| 103 | |
---|
| 104 | size=(/iip1,jjp1,llm,1/) |
---|
| 105 | do lt=1,istime |
---|
| 106 | start=(/1,1,1,lt/) |
---|
| 107 | ! Extraction of the "source" variables |
---|
| 108 | #ifdef NC_DOUBLE |
---|
| 109 | ierr = NF_GET_VARA_DOUBLE(nid,varid,start,size,sum3d) |
---|
| 110 | ierr = NF_GET_VARA_DOUBLE(nid,varid+1,start,size,square3d) |
---|
| 111 | #else |
---|
| 112 | ierr = NF_GET_VARA_REAL(nid,varid,start,size,sum3d) |
---|
| 113 | ierr = NF_GET_VARA_REAL(nid,varid+1,start,size,square3d) |
---|
| 114 | #endif |
---|
| 115 | ! Calculation of these variables |
---|
| 116 | mean3d=sum3d/count(lt) |
---|
| 117 | sd3d=sqrt(max(0.,square3d/count(lt)-mean3d**2)) |
---|
| 118 | ! Writing of the variables |
---|
| 119 | #ifdef NC_DOUBLE |
---|
| 120 | ierr = NF_PUT_VARA_DOUBLE(nid,varid,start,size,mean3d) |
---|
| 121 | ierr = NF_PUT_VARA_DOUBLE(nid,varid+1,start,size,sd3d) |
---|
| 122 | #else |
---|
| 123 | ierr = NF_PUT_VARA_REAL(nid,varid,start,size,mean3d) |
---|
| 124 | ierr = NF_PUT_VARA_REAL(nid,varid+1,start,size,sd3d) |
---|
| 125 | #endif |
---|
| 126 | enddo |
---|
| 127 | |
---|
| 128 | else if (ndims.eq.3) then |
---|
| 129 | |
---|
| 130 | ! dimout(1)=lonid |
---|
| 131 | ! dimout(2)=latid |
---|
| 132 | ! dimout(3)=timeid |
---|
| 133 | |
---|
| 134 | size=(/iip1,jjp1,1,0/) |
---|
| 135 | do lt=1,istime |
---|
| 136 | start=(/1,1,lt,0/) |
---|
| 137 | ! Extraction of the "source" variables |
---|
| 138 | #ifdef NC_DOUBLE |
---|
| 139 | ierr = NF_GET_VARA_DOUBLE(nid,varid,start,size,sum2d) |
---|
| 140 | ierr = NF_GET_VARA_DOUBLE(nid,varid+1,start,size,square2d) |
---|
| 141 | #else |
---|
| 142 | ierr = NF_GET_VARA_REAL(nid,varid,start,size,sum2d) |
---|
| 143 | ierr = NF_GET_VARA_REAL(nid,varid+1,start,size,square2d) |
---|
| 144 | #endif |
---|
| 145 | ! Calculation of these variables |
---|
| 146 | mean2d=sum2d/count(lt) |
---|
| 147 | sd2d=sqrt(max(0.,square2d/count(lt)-mean2d**2)) |
---|
| 148 | ! Writing of the variables |
---|
| 149 | #ifdef NC_DOUBLE |
---|
| 150 | ierr = NF_PUT_VARA_DOUBLE(nid,varid,start,size,mean2d) |
---|
| 151 | ierr = NF_PUT_VARA_DOUBLE(nid,varid+1,start,size,sd2d) |
---|
| 152 | #else |
---|
| 153 | ierr = NF_PUT_VARA_REAL(nid,varid,start,size,mean2d) |
---|
| 154 | ierr = NF_PUT_VARA_REAL(nid,varid+1,start,size,sd2d) |
---|
| 155 | #endif |
---|
| 156 | enddo |
---|
| 157 | |
---|
| 158 | endif |
---|
| 159 | enddo |
---|
| 160 | |
---|
| 161 | ierr= NF_CLOSE(nid) |
---|
| 162 | |
---|
| 163 | end |
---|