[cdftools] 41/228: JMM modify the cdfmoy series in order to add an additional attribute (iweight) indicating the number of frames used when computing the mean field. This extra attribute is used by the new tool cdfmoy_weight in order to compute mean values with files of different weight. This new tool make the use of cdfmoy_annual obsolete (this latter was only valid for 5-days output and simulation without leap years.

Alastair McKinstry mckinstry at moszumanska.debian.org
Fri Jun 12 08:21:26 UTC 2015


This is an automated email from the git hooks/post-receive script.

mckinstry pushed a commit to branch master
in repository cdftools.

commit 99ec8fe542ac9e5506870ab7db02c209353d5916
Author: molines <molines at 1055176f-818a-41d9-83e1-73fbe5b947c5>
Date:   Mon May 17 12:47:12 2010 +0000

    JMM modify the cdfmoy series in order to add an additional attribute (iweight) indicating the number of frames used when computing the mean field.
        This extra attribute is used by the new tool cdfmoy_weight in order to compute mean values with files of different weight. This new tool make
        the use of cdfmoy_annual obsolete (this latter was only valid for 5-days output and simulation without leap years.
    
    
    git-svn-id: http://servforge.legi.grenoble-inp.fr/svn/CDFTOOLS/trunk@317 1055176f-818a-41d9-83e1-73fbe5b947c5
---
 JOBS/mkordre        |   2 +-
 Makefile            |   9 ++-
 cdfio.f90           |  29 +++++++++-
 cdfmoy.f90          |   4 +-
 cdfmoy_weighted.f90 | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 cdfnan.f90          |   3 +-
 cdfvT.f90           |   8 +--
 7 files changed, 197 insertions(+), 12 deletions(-)

diff --git a/JOBS/mkordre b/JOBS/mkordre
index 2ef612f..3184b1c 100755
--- a/JOBS/mkordre
+++ b/JOBS/mkordre
@@ -17,7 +17,7 @@ fi
 ls *anne*  2> /dev/null && { chkdir ANNEX ; mv *anne* ANNEX  ; }
 
 # Scan the different types of files and send then to the YEAR directory
-for type in gridT gridU gridV gridW icemod ptrcT; do
+for type in gridT gridU gridV gridW icemod ptrcT trends dynT flxT; do
   ls *${type}.nc  2> /dev/null &&  { \
   for f in *_${type}.nc ; do 
     tag=$( echo $f | sed -e 's/_/ /g' | awk '{print $2}' )
diff --git a/Makefile b/Makefile
index 528bfde..4490b58 100644
--- a/Makefile
+++ b/Makefile
@@ -13,7 +13,8 @@ include make.macro
 
 CDFTOOLS=CDFTOOLS-2.1
 
-EXEC = cdfmoy cdfmoyt  cdfmoy_sp cdfstd cdfmoy_sal2_temp2  cdfmoy_annual cdfmoy_chsp cdfmoy_freq cdfvT cdfvsig cdfspeed cdfsum\
+EXEC = cdfmoy cdfmoyt  cdfmoy_sp cdfstd cdfmoy_sal2_temp2  cdfmoy_annual cdfmoy_weighted cdfmoy_chsp cdfmoy_freq cdfvT \
+       cdfvsig cdfspeed cdfsum\
        cdfmoyuv cdfmoyuvwt \
        cdfeke cdfrmsssh cdfstdevw cdfstdevts cdflinreg cdfimprovechk\
        cdfbn2 cdfbn2-full  cdfsig0 cdfsigi cdfsiginsitu cdfbottomsig0 cdfbottomsigi cdfspice\
@@ -71,6 +72,9 @@ cdfstd: cdfio.o  cdfstd.f90
 cdfmoy_annual: cdfio.o   cdfmoy_annual.f90
 	$(F90) cdfmoy_annual.f90 -o cdfmoy_annual cdfio.o  $(FFLAGS)
 
+cdfmoy_weighted: cdfio.o   cdfmoy_weighted.f90
+	$(F90) cdfmoy_weighted.f90 -o cdfmoy_weighted cdfio.o  $(FFLAGS)
+
 cdfeke: cdfio.o  cdfeke.f90
 	$(F90) cdfeke.f90 -o cdfeke cdfio.o $(FFLAGS)
 
@@ -450,6 +454,9 @@ cdfvar: cdfio.o cdfvar.f90
 cdfcsp: cdfio.o cdfcsp.f90
 	$(F90)   cdfcsp.f90  -o cdfcsp cdfio.o $(FFLAGS)
 
+cdfnan: cdfio.o cdfnan.f90
+	$(F90)   cdfnan.f90  -o cdfnan cdfio.o $(FFLAGS)
+
 cdfnorth_unfold: cdfio.o cdfnorth_unfold.f90
 	$(F90)   cdfnorth_unfold.f90  -o cdfnorth_unfold cdfio.o $(FFLAGS)
 
diff --git a/cdfio.f90 b/cdfio.f90
index 18bd31e..3b9786c 100644
--- a/cdfio.f90
+++ b/cdfio.f90
@@ -34,6 +34,7 @@
      REAL(kind=4)    :: scale_factor=1.
      REAL(kind=4)    :: add_offset=0.
      REAL(kind=4)    :: savelog10=0.
+     INTEGER         :: iwght=1
      CHARACTER(LEN=256):: long_name
      CHARACTER(LEN=256):: short_name
      CHARACTER(LEN=256):: online_operation
@@ -360,6 +361,8 @@ CONTAINS
     IF (putatt /= 0 ) THEN ;PRINT *, NF90_STRERROR(putatt)  ; STOP 'putatt longname'; ENDIF
     putatt=NF90_PUT_ATT(kout,kid,'short_name',tyvar%short_name) 
     IF (putatt /= 0 ) THEN ;PRINT *, NF90_STRERROR(putatt)  ; STOP 'putatt short name'; ENDIF
+    putatt=NF90_PUT_ATT(kout,kid,'iweight',tyvar%iwght) 
+    IF (putatt /= 0 ) THEN ;PRINT *, NF90_STRERROR(putatt)  ; STOP 'putatt iweight'; ENDIF
     putatt=NF90_PUT_ATT(kout,kid,'online_operation',tyvar%online_operation) 
     IF (putatt /= 0 ) THEN ;PRINT *, NF90_STRERROR(putatt)  ; STOP 'putatt online oper'; ENDIF
     putatt=NF90_PUT_ATT(kout,kid,'axis',tyvar%axis) 
@@ -663,6 +666,7 @@ CONTAINS
     INTEGER :: istatus
     CHARACTER(LEN=256) :: cldum=''
     REAL(KIND=4) :: zatt
+    INTEGER      :: iatt
 
     istatus=NF90_OPEN(cdfile,NF90_NOWRITE,ncid)
     DO jv = 1, knvars
@@ -698,6 +702,13 @@ CONTAINS
           ptypvar(jv)%valid_max=0.
        ENDIF
 
+       IF ( NF90_INQUIRE_ATTRIBUTE(ncid,jv,'iweight') == NF90_NOERR ) THEN
+          istatus=NF90_GET_ATT(ncid,jv,'iweight',iatt)
+          ptypvar(jv)%iwght=iatt
+       ELSE
+          ptypvar(jv)%iwght=1
+       ENDIF
+
        IF ( NF90_INQUIRE_ATTRIBUTE(ncid,jv,'long_name',len=ILEN) == NF90_NOERR ) THEN
           istatus=NF90_GET_ATT(ncid,jv,'long_name',cldum(1:ILEN))
           ptypvar(jv)%long_name=TRIM(cldum)
@@ -1504,7 +1515,7 @@ CONTAINS
 
   END FUNCTION putheadervar
 
-  FUNCTION putvarr8(kout, kid,ptab, klev, kpi, kpj,ktime)
+  FUNCTION putvarr8(kout, kid,ptab, klev, kpi, kpj,ktime, kwght)
     !!-----------------------------------------------------------
     !!                       ***  FUNCTION  putvar  ***
     !!
@@ -1523,6 +1534,7 @@ CONTAINS
     INTEGER, INTENT(in) :: klev             ! level at which ptab will be written
     INTEGER, INTENT(in) :: kpi,kpj          ! dimension of ptab
     INTEGER, OPTIONAL, INTENT(in) :: ktime  ! dimension of ptab
+    INTEGER, OPTIONAL, INTENT(in) :: kwght  ! weight of this variable
     REAL(KIND=8), DIMENSION(kpi,kpj),INTENT(in) :: ptab ! 2D array to write in file
     INTEGER :: putvarr8                       ! return status
 
@@ -1546,11 +1558,14 @@ CONTAINS
     ENDIF
     icount(:) = 1 ; icount(1) = kpi ; icount(2) = kpj
     istatus=NF90_PUT_VAR(kout,kid, ptab, start=istart,count=icount)
+    IF (PRESENT(kwght) ) THEN
+      istatus=NF90_PUT_ATT(kout,kid,'iweight',kwght)
+    ENDIF
     putvarr8=istatus
 
   END FUNCTION putvarr8
 
-  FUNCTION putvarr4(kout, kid,ptab, klev, kpi, kpj,ktime)
+  FUNCTION putvarr4(kout, kid,ptab, klev, kpi, kpj, ktime, kwght)
     !!-----------------------------------------------------------
     !!                       ***  FUNCTION  putvar  ***
     !!
@@ -1569,6 +1584,7 @@ CONTAINS
     INTEGER, INTENT(in) :: klev             ! level at which ptab will be written
     INTEGER, INTENT(in) :: kpi,kpj          ! dimension of ptab
     INTEGER, OPTIONAL, INTENT(in) :: ktime  ! dimension of ptab
+    INTEGER, OPTIONAL, INTENT(in) :: kwght  ! weight of this variable
     REAL(KIND=4), DIMENSION(kpi,kpj),INTENT(in) :: ptab ! 2D array to write in file
     INTEGER :: putvarr4                       ! return status
 
@@ -1592,11 +1608,14 @@ CONTAINS
     ENDIF
     icount(:) = 1 ; icount(1) = kpi ; icount(2) = kpj
     istatus=NF90_PUT_VAR(kout,kid, ptab, start=istart,count=icount)
+    IF (PRESENT(kwght) ) THEN
+      istatus=NF90_PUT_ATT(kout,kid,'iweight',kwght)
+    ENDIF
     putvarr4=istatus
 
   END FUNCTION putvarr4
 
-  FUNCTION reputvarr4 (cdfile,cdvar,klev,kpi,kpj,kimin,kjmin, ktime,ptab)
+  FUNCTION reputvarr4 (cdfile,cdvar,klev,kpi,kpj,kimin,kjmin, ktime,ptab,kwght)
     !!-----------------------------------------------------------
     !!                       ***  FUNCTION  putvar  ***
     !!
@@ -1617,6 +1636,7 @@ CONTAINS
     INTEGER, OPTIONAL, INTENT(in) :: klev           ! Optional variable. If missing 1 is assumed
     INTEGER, OPTIONAL, INTENT(in) :: kimin,kjmin    ! Optional variable. If missing 1 is assumed
     INTEGER, OPTIONAL, INTENT(in) :: ktime          ! Optional variable. If missing 1 is assumed
+    INTEGER, OPTIONAL, INTENT(in) :: kwght          ! weight of this variable
     REAL(KIND=4), DIMENSION(kpi,kpj) ::  ptab     ! 2D REAL 4 holding variable field at klev
     INTEGER :: reputvarr4
 
@@ -1641,6 +1661,9 @@ CONTAINS
     IF ( nldim(3) == id_dimunlim)  THEN ; ilev=itime ; itime=1 ; ENDIF
     istatus=NF90_PUT_VAR(ncid,id_var,ptab,start=(/imin,jmin,ilev,itime/), count=(/kpi,kpj,1,1/) )
     !PRINT *,TRIM(NF90_STRERROR(istatus)),' in reputvar'
+    IF (PRESENT(kwght)) THEN
+      istatus=NF90_PUT_ATT(ncid,id_var,'iweight',kwght)
+    ENDIF
     reputvarr4=istatus
     istatus=NF90_CLOSE(ncid)
 
diff --git a/cdfmoy.f90 b/cdfmoy.f90
index 0ec9e90..1db61d2 100644
--- a/cdfmoy.f90
+++ b/cdfmoy.f90
@@ -175,8 +175,8 @@ PROGRAM cdfmoy
            rmean(:,:) = tab(:,:)/ntframe
            IF (cvarname2(jvar) /= 'none' ) rmean2(:,:) = tab2(:,:)/ntframe
            ! store variable on outputfile
-           ierr = putvar(ncout, id_varout(jvar) ,rmean, jk, npiglo, npjglo)
-           IF (cvarname2(jvar) /= 'none' ) ierr = putvar(ncout2,id_varout2(jvar),rmean2, jk,npiglo, npjglo)
+           ierr = putvar(ncout, id_varout(jvar) ,rmean, jk, npiglo, npjglo, kwght=ntframe)
+           IF (cvarname2(jvar) /= 'none' ) ierr = putvar(ncout2,id_varout2(jvar),rmean2, jk,npiglo, npjglo, kwght=ntframe)
            IF (lcaltmean )  THEN
               timean(1)= total_time/ntframe
               ierr=putvar1d(ncout,timean,1,'T')
diff --git a/cdfmoy_weighted.f90 b/cdfmoy_weighted.f90
new file mode 100644
index 0000000..e8962a3
--- /dev/null
+++ b/cdfmoy_weighted.f90
@@ -0,0 +1,154 @@
+PROGRAM cdfmoy_weighted
+  !!-----------------------------------------------------------------------
+  !!                 ***  PROGRAM cdfmoy_weighted  ***
+  !!
+  !!  **  Purpose: Compute weighted mean values from monthly mean
+  !!  
+  !!  **  Method: monthly mean were computed (cdfmoy) with all dumps that fall within a montn
+  !!              thus, all month have different weigth : Feb = 5. March, Dec. = 7 other = 6
+  !!
+  !! history :
+  !!     Original code :   J.M. Molines (Nov 2004 ) for ORCA025
+  !!                       J.M. Molines (Apr 2005 ) put all NCF stuff in module
+  !!                              now valid for grid T U V W icemod
+  !!-----------------------------------------------------------------------
+  !!  $Rev$
+  !!  $Date$
+  !!  $Id$
+  !!--------------------------------------------------------------
+  !!
+  USE cdfio 
+
+  IMPLICIT NONE
+  INTEGER   :: jk,jt,jvar, jv                               !: dummy loop index
+  INTEGER   :: ierr,idum                                    !: working integer
+  INTEGER   :: narg, iargc                                  !: 
+  INTEGER   :: npiglo,npjglo, npk                           !: size of the domain
+  INTEGER   ::  nvars                                       !: Number of variables in a file
+  INTEGER , DIMENSION(:), ALLOCATABLE :: id_var , &         !: arrays of var id's
+       &                             ipk    , &         !: arrays of vertical level for each var
+       &                             id_varout 
+  INTEGER, DIMENSION(:), ALLOCATABLE :: iweight
+  REAL(KIND=8) , DIMENSION (:,:), ALLOCATABLE :: tab  !: Arrays for cumulated values
+  REAL(KIND=8)                                :: total_time, sumw
+  REAL(KIND=4) , DIMENSION (:,:), ALLOCATABLE :: v2d ,&       !: Array to read a layer of data
+       &                                   rmean
+  REAL(KIND=4),DIMENSION(1)                   :: timean, tim
+
+  CHARACTER(LEN=256) :: cfile ,cfileout           !: file name
+  CHARACTER(LEN=256) ::  cdep
+  CHARACTER(LEN=256) ,DIMENSION(:), ALLOCATABLE:: cvarname   !: array of var name
+  CHARACTER(LEN=256) ,DIMENSION(:), ALLOCATABLE:: cdummy     !: array of var name
+  
+  TYPE (variable), DIMENSION(:), ALLOCATABLE :: typvar, typvardum
+
+  INTEGER    :: ncout
+  INTEGER    :: istatus
+
+  !!
+
+  !!  Read command line
+  narg= iargc()
+  IF ( narg ==  0 ) THEN
+     PRINT *,' Usage : cdfmoy_weighted ''list of files'' '
+     STOP
+  ENDIF
+  ALLOCATE (iweight(narg) )  ! as maby weights as files
+  !!
+  !! Initialisation from 1st file (all file are assume to have the same geometry)
+  CALL getarg (1, cfile)
+
+  npiglo= getdim (cfile,'x')
+  npjglo= getdim (cfile,'y')
+  npk   = getdim (cfile,'depth',cdtrue=cdep, kstatus=istatus)
+
+  IF (istatus /= 0 ) THEN
+     npk   = getdim (cfile,'z',cdtrue=cdep,kstatus=istatus)
+     IF (istatus /= 0 ) THEN
+        npk   = getdim (cfile,'sigma',cdtrue=cdep,kstatus=istatus)
+        IF (istatus /= 0 ) THEN 
+! STOP 'depth dimension name not suported'
+         PRINT *,' assume file with no depth'
+         npk=0
+        ENDIF
+     ENDIF
+  ENDIF
+  
+
+  PRINT *, 'npiglo=', npiglo
+  PRINT *, 'npjglo=', npjglo
+  PRINT *, 'npk   =', npk
+
+  ALLOCATE( tab(npiglo,npjglo), v2d(npiglo,npjglo) )
+  ALLOCATE( rmean(npiglo,npjglo) )
+
+  nvars = getnvar(cfile)
+  PRINT *,' nvars =', nvars
+
+  ALLOCATE (cvarname(nvars),cdummy(nvars) )
+  ALLOCATE (typvar(nvars), typvardum(nvars)  )
+  ALLOCATE (id_var(nvars),ipk(nvars),id_varout(nvars) )
+
+  ! get list of variable names and collect attributes in typvar (optional)
+  cvarname(:)=getvarname(cfile,nvars,typvar)
+
+
+  id_var(:)  = (/(jv, jv=1,nvars)/)
+  ! ipk gives the number of level or 0 if not a T[Z]YX  variable
+  ipk(:)     = getipk (cfile,nvars,cdep=cdep)
+  WHERE( ipk == 0 ) cvarname='none'
+  typvar(:)%name=cvarname
+
+  ! create output fileset
+  cfileout='cdfmoy_weighted.nc'
+  ! create output file taking the sizes in cfile
+
+  ncout =create(cfileout, cfile,npiglo,npjglo,npk,cdep=cdep)
+
+  ierr= createvar(ncout , typvar,  nvars, ipk, id_varout )
+
+  ierr= putheadervar(ncout , cfile, npiglo, npjglo, npk,cdep=cdep)
+
+  DO jvar = 1,nvars
+     ! fill iweight for each variables: need to scan all the input files
+     DO jt=1,narg  ! this is far from optimal : think about a special function
+                   ! for retrieving an attribute of a variable
+       CALL getarg(jt,cfile)
+       cdummy(:)=getvarname(cfile,nvars,typvardum)
+       iweight(jt)=typvardum(jvar)%iwght
+     ENDDO
+     PRINT *, iweight
+     IF (cvarname(jvar) == 'nav_lon' .OR. &
+          cvarname(jvar) == 'nav_lat' ) THEN
+        ! skip these variable
+     ELSE
+        PRINT *,' Working with ', TRIM(cvarname(jvar)), ipk(jvar)
+        DO jk = 1, ipk(jvar)
+           PRINT *,'level ',jk
+           tab(:,:) = 0.d0 ; total_time = 0. ; sumw=0.
+           DO jt = 1, narg
+              sumw = sumw + iweight(jt)
+              IF (jk == 1 .AND. jvar == nvars )  THEN
+                 tim=getvar1d(cfile,'time_counter',1)
+                 total_time = total_time + tim(1)
+              END IF
+              CALL getarg (jt, cfile)
+              v2d(:,:)= getvar(cfile, cvarname(jvar), jk ,npiglo, npjglo )
+              tab(:,:) = tab(:,:) + iweight(jt)* v2d(:,:)
+           END DO
+           ! finish with level jk ; compute mean (assume spval is 0 )
+           rmean(:,:) = tab(:,:)/sumw
+           ! store variable on outputfile
+           ierr = putvar(ncout, id_varout(jvar) ,rmean, jk, npiglo, npjglo,kwght=INT(sumw) )
+           IF (jk == 1 .AND. jvar == nvars )  THEN
+              timean(1)= total_time/narg
+              ierr=putvar1d(ncout,timean,1,'T')
+           END IF
+        END DO  ! loop to next level
+     END IF
+  END DO ! loop to next var in file
+
+  istatus = closeout(ncout)
+
+
+END PROGRAM cdfmoy_weighted
diff --git a/cdfnan.f90 b/cdfnan.f90
index 636cb64..a65e4d4 100644
--- a/cdfnan.f90
+++ b/cdfnan.f90
@@ -51,6 +51,7 @@ PROGRAM cdfnan
     CALL getarg ( narg - 1, cfile)
     IF (TRIM(cfile) == '-value' ) THEN
       CALL getarg(narg,cfile) ; READ(cfile,*) replace ; l_replace=.true.
+      narg=narg -2 
     ENDIF
   ENDIF
   CALL getarg (1, cfile)
@@ -86,7 +87,7 @@ PROGRAM cdfnan
 
   DO jf = 1, narg
      CALL getarg (jf, cfile)
-     PRINT *, 'Change spval on file ', cfile
+     PRINT *, 'Change NaN on file ', cfile
      ncid = ncopen(cfile)
      nt    = getdim (cfile,'time_counter')
      DO jvar = 1,nvars
diff --git a/cdfvT.f90 b/cdfvT.f90
index ff8cfa5..11ef831 100644
--- a/cdfvT.f90
+++ b/cdfvT.f90
@@ -212,16 +212,16 @@ PROGRAM cdfvT
      END DO  ! jt
      ! finish with level jk ; compute mean (assume spval is 0 )
      rmean(:,:) = zcumulvt(:,:)/ntframe
-     ierr = putvar(ncout, id_varout(1) ,rmean, jk,npiglo, npjglo )
+     ierr = putvar(ncout, id_varout(1) ,rmean, jk,npiglo, npjglo, kwght=ntframe )
 
      rmean(:,:) = zcumulvs(:,:)/ntframe
-     ierr = putvar(ncout, id_varout(2) ,rmean, jk,npiglo, npjglo )
+     ierr = putvar(ncout, id_varout(2) ,rmean, jk,npiglo, npjglo, kwght=ntframe )
 
      rmean(:,:) = zcumulut(:,:)/ntframe
-     ierr = putvar(ncout, id_varout(3) ,rmean, jk,npiglo, npjglo )
+     ierr = putvar(ncout, id_varout(3) ,rmean, jk,npiglo, npjglo, kwght=ntframe )
 
      rmean(:,:) = zcumulus(:,:)/ntframe
-     ierr = putvar(ncout, id_varout(4) ,rmean, jk,npiglo, npjglo )
+     ierr = putvar(ncout, id_varout(4) ,rmean, jk,npiglo, npjglo, kwght=ntframe )
 
      IF (lcaltmean )  THEN
         timean(1)= total_time/ntframe

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/cdftools.git



More information about the debian-science-commits mailing list