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