Changeset 2304 in lmdz_wrf for trunk


Ignore:
Timestamp:
Jan 30, 2019, 7:41:32 PM (6 years ago)
Author:
lfita
Message:

Almost working !?

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/module_scientific.f90

    r2302 r2304  
    4444! multi_index_mat3DRK: Subroutine to provide the indices of the different locations of a value inside a 3D RK matrix
    4545! multi_index_mat4DRK: Subroutine to provide the indices of the different locations of a value inside a 4D RK matrix
     46! multi_spaceweightstats_in3DRK3_slc3v3: Subroutine to compute an spatial statistics value from a 3D RK matrix using 3rd dimension as
     47!   running one into a matrix of 3-variables slices of rank 3 using spatial weights
     48! multi_spaceweightstats_in3DRK3_slc3v4: Subroutine to compute an spatial statistics value from a 3D RK matrix using 3rd dimension as
     49!   running one into a matrix of 3-variables slices of rank 4 using spatial weights
    4650! NcountR: Subroutine to count real values
    4751! paths_border: Subroutine to search the paths of a border field.
     
    56345638  END SUBROUTINE spaceweightstats
    56355639
    5636   SUBROUTINE multi_spacewightstats_in3DRK3_slc3v3(varin, Ngridsin, gridsin, percentages, varout,      &
     5640  SUBROUTINE multi_spaceweightstats_in3DRK3_slc3v3(varin, Ngridsin, gridsin, percentages, varout,     &
    56375641    di1, di2, di3, ds1, ds2, ds3, maxNgridsin)
    56385642  ! Subroutine to compute an spatial statistics value from a 3D RK matrix using 3rd dimension as
     
    56535657! Local
    56545658    INTEGER                                              :: i1, i2, i3, s1, s2, s3, iv
     5659    INTEGER                                              :: ii3, ss1, ss2, ss3
    56555660    INTEGER                                              :: Ncounts, Nin
    56565661    CHARACTER(len=3)                                     :: val1S, val2S
    56575662    CHARACTER(len=30)                                    :: val3S
    56585663    REAL(r_k)                                            :: minv, maxv, meanv, mean2v, stdv, medv
    5659     INTEGER, DIMENSION(:), ALLOCATABLE                   :: pin
     5664    REAL(r_k), DIMENSION(:), ALLOCATABLE                 :: pin
    56605665    INTEGER, DIMENSION(:,:), ALLOCATABLE                 :: gin
    56615666    REAL(r_k), DIMENSION(:), ALLOCATABLE                 :: svin
     
    56815686! varout: output statistical variable
    56825687
    5683     fname = 'multi_spacewightstats_in3DRK3_slc3v3'
     5688    fname = 'multi_spaceweightstats_in3DRK3_slc3v3'
     5689
     5690    varout = fillval64
     5691
     5692    ss1 = 8 + 1
     5693    ss2 = 5 + 1
     5694    ss3 = 3 + 1
     5695    ii3 = 1 + 1
    56845696
    56855697    ! Let's be efficient?
     
    57005712          pin = percentages(s1,s2,s3,1:Nin)
    57015713
    5702           minv = fillVal64
    5703           maxv = -fillVal64
    5704           meanv = zeroRK
    5705           mean2v = zeroRK
    5706           stdv = zeroRK
    5707 
    57085714          ! Getting the values
    57095715          DO iv=1, Nin
     
    57135719          END DO
    57145720
     5721          IF (s1 == ss1 .AND. s2 == ss2 .AND. s3 == ss3) THEN
     5722            PRINT *, ' Lluis FOR Nin:', Nin
     5723            PRINT *, ' Lluis FOR values:', vin(:,ii3)
     5724            DO iv=1, Nin
     5725              PRINT *,' c:', iv, ':', gin(iv,:), ' p:', pin(iv)
     5726            END DO
     5727          END IF
     5728
    57155729          ! Computing along d3
    57165730          DO i3=1, di3
     5731            minv = fillVal64
     5732            maxv = -fillVal64
     5733            meanv = zeroRK
     5734            mean2v = zeroRK
     5735            stdv = zeroRK
    57175736            minv = MINVAL(vin(:,i3))
    57185737            maxv = MAXVAL(vin(:,i3))
    5719             meanv = SUM(vin(:,i3)*pin)/Nin
    5720             mean2v = SUM(vin(:,i3)**2*pin)/Nin
    5721             stdv = SQRT(mean2v - meanv**2)
     5738            meanv = SUM(vin(:,i3)*pin)
     5739            mean2v = SUM(vin(:,i3)**2*pin)
     5740            DO iv=1,Nin
     5741              stdv = stdv + ( (meanv - vin(iv,i3))*pin(iv) )**2
     5742            END DO
     5743            stdv = SQRT(stdv)
    57225744            svin = vin(:,i3)
    57235745            CALL SortR_K(svin, Nin)
     
    57425764    RETURN
    57435765
    5744   END SUBROUTINE multi_spacewightstats_in3DRK3_slc3v3
    5745 
    5746   SUBROUTINE multi_spacewightstats_in3DRK3_slc3v4(varin, Ngridsin, gridsin, percentages, varout,      &
     5766  END SUBROUTINE multi_spaceweightstats_in3DRK3_slc3v3
     5767
     5768  SUBROUTINE multi_spaceweightstats_in3DRK3_slc3v4(varin, Ngridsin, gridsin, percentages, varout,     &
    57475769    di1, di2, di3, ds1, ds2, ds3, ds4, maxNgridsin)
    57485770  ! Subroutine to compute an spatial statistics value from a 3D RK matrix using 3rd dimension as
     
    57685790    CHARACTER(len=30)                                    :: val3S
    57695791    REAL(r_k)                                            :: minv, maxv, meanv, mean2v, stdv, medv
    5770     INTEGER, DIMENSION(:), ALLOCATABLE                   :: pin
     5792    REAL(r_k), DIMENSION(:), ALLOCATABLE                 :: pin
    57715793    INTEGER, DIMENSION(:,:), ALLOCATABLE                 :: gin
    57725794    REAL(r_k), DIMENSION(:), ALLOCATABLE                 :: svin
     
    57925814! varout: output statistical variable
    57935815
    5794     fname = 'multi_spacewightstats_in3DRK3_slc3v4'
     5816    fname = 'multi_spaceweightstats_in3DRK3_slc3v4'
     5817
     5818    varout = fillval64
    57955819
    57965820    ! Let's be efficient?
     
    58275851            ! Computing along d3
    58285852            DO i3=1, di3
     5853              minv = fillVal64
     5854              maxv = -fillVal64
     5855              meanv = zeroRK
     5856              mean2v = zeroRK
     5857              stdv = zeroRK
     5858
    58295859              minv = MINVAL(vin(:,i3))
    58305860              maxv = MAXVAL(vin(:,i3))
    5831               meanv = SUM(vin(:,i3)*pin)/Nin
    5832               mean2v = SUM(vin(:,i3)**2*pin)/Nin
    5833               stdv = SQRT(mean2v - meanv**2)
     5861              meanv = SUM(vin(:,i3)*pin)
     5862              mean2v = SUM(vin(:,i3)**2*pin)
     5863              DO iv=1,Nin
     5864                stdv = stdv + ( (meanv - vin(iv,i3))*pin(iv) )**2
     5865              END DO
     5866              stdv = SQRT(stdv)
    58345867              svin = vin(:,i3)
    58355868              CALL SortR_K(svin, Nin)
     
    58555888    RETURN
    58565889
    5857   END SUBROUTINE multi_spacewightstats_in3DRK3_slc3v4
     5890  END SUBROUTINE multi_spaceweightstats_in3DRK3_slc3v4
    58585891
    58595892  SUBROUTINE multi_index_mat2DI(d1, d2, d12, mat, value, Nindices, indices)
  • trunk/tools/nc_var_tools.py

    r2303 r2304  
    2874228742                for iB in range(dxB):
    2874328743                    Nin = sN[jA,iA,jB,iB]
    28744                     sgpa[0:Nin,jA,iA,jB,iB] = anewvar[0:Nin,jA,iA,jB,iB]/            \
    28745                       aanewvar[jA,iA,jB,iB]
    28746                     panewvar[0:Nin,jA,iA,jB,iB] = sgpa[0:Nin,jA,iA,jB,iB]
     28744                    if Nin > 0 and aanewvar[jA,iA,jB,iB] != 0.:
     28745                        sgpa[0:Nin,jA,iA,jB,iB] = anewvar[0:Nin,jA,iA,jB,iB]/        \
     28746                          aanewvar[jA,iA,jB,iB]
     28747                        panewvar[0:Nin,jA,iA,jB,iB] = sgpa[0:Nin,jA,iA,jB,iB]
     28748                        # Let's check
     28749                        psum = np.sum(sgpa[0:Nin,jA,iA,jB,iB])
     28750                        if psum != 1.:
     28751                            print warnmsg
     28752                            print '    ' + fname + 'at slide:', jA,iA,jB,iB,         \
     28753                              'percentages do NOT add one !!'
     28754                            print '    Ngrids:', Nin, 'slice area:',                 \
     28755                              aanewvar[jA,iA,jB,iB], 'total sum:', psum
     28756                            #for iv in range(Nin):
     28757                            #    print iv, sgpa[iv,jA,iA,jB,iB]
    2874728758    onewnc.sync()
    2874828759                     
     28760    # Masking...
     28761    sgpa = ma.masked_equal(sgpa,gen.fillValueF)
     28762
    2874928763    # removing monotones. Fortran only accepts rank <= 7 arrays !!
    2875028764    sN, Ndims = gen.remove_monotones(sN, oNslice.dimensions)
     
    2875928773
    2876028774    sNt = sN.transpose()
    28761     sint = sin.transpose()
     28775    #sint = sin.transpose()
     28776    sint0 = sin.transpose()
     28777    ## We are in Fortran thus (dx,dy) and 1st:1 !!!!
     28778    sint = np.ones((sint0.shape), dtype=np.float)*gen.fillValueF
     28779    sint[...,0] = sint0[...,1]
     28780    sint[...,1] = sint0[...,0]
    2876228781    spt = sgpa.transpose()
     28782
     28783    # Checking
     28784    i = 3
     28785    j = 5
     28786    k = 8
     28787    chijk = [i,j,k]
     28788    it = 1
     28789    slicesvals = []
     28790    print 'Lluis Ndims:', Ndims
     28791    idd = 0
     28792    for dnn in Ndims:
     28793        scvv = slicesinf[dnn.split('_')[1]]
     28794        sccvvv = scvv[3]
     28795        print 'sccvvv:', sccvvv
     28796        print 'Lluis dnn:', dnn, sccvvv[chijk[idd]]
     28797        slicesvals.append(sccvvv[chijk[idd]])
     28798        idd = idd + 1
    2876328799
    2876428800    for varn in varns:
     
    2877628812        for dnn in vdimns:
    2877728813            vdimn = dimvars[dnn]
    28778             if vdimn == 'WRFtime': 
     28814            if vdimn == 'WRFtime':
    2877928815                vdimn = 'time'
    28780                 odimvar = onc.variables['Times']
    28781                 timewrfv = odimvar[:]
    28782                 tvals, urefvals = compute_WRFtime(timewrfv, refdate='19491201000000',\
    28783                   tunitsval='minutes')
    28784                 tvals = np.array(tvals)
    28785                 dimtwrf = len(tvals)
     28816                if not onewnc.variables.has_key('time'):
     28817                    odimvar = onc.variables['Times']
     28818                    timewrfv = odimvar[:]
     28819                    tvals, urefvals = compute_WRFtime(timewrfv,                      \
     28820                      refdate='19491201000000', tunitsval='minutes')
     28821                    tvals = np.array(tvals)
     28822                    dimtwrf = len(tvals)
     28823                    if not gen.searchInlist(onewnc.dimensions,vdimn):
     28824                        newdim = onewnc.createDimension(vdimn, None)
     28825                    if not gen.searchInlist(onewnc.variables,vdimn):
     28826                        newvar = onewnc.createVariable(vdimn, 'f', ('time'))
     28827                        newvar[:] = tvals
     28828                        basicvardef(newvar, 'time', 'Time', urefvals)
     28829                        newvar.setncattr('calendar','gregorian')
    2878628830                if gen.searchInlist(slicestatsdim, dnn):
    2878728831                    vardims.append(vdimn)
    2878828832                    varshape.append(dimtwrf)
    28789                 if not gen.searchInlist(onewnc.dimensions,vdimn):
    28790                     newdim = onewnc.createDimension(vdimn, None)
    28791                 if not gen.searchInlist(onewnc.variables,vdimn):
    28792                     newvar = onewnc.createVariable(vdimn, 'f', ('time'))
    28793                     newvar[:] = tvals
    28794                     basicvardef(newvar, 'time', 'Time', urefvals)
    28795                     newvar.setncattr('calendar','gregorian')
    2879628833
    2879728834            else:
     
    2880928846        newvardims = vardims + dimsslice
    2881028847        newvarshape = varshape + shapeslice
    28811         print 'Lluis newvardims:', newvardims, 'newvarshape:', newvarshape
     28848        print 'Lluis newvardims:', newvardims, 'newvarshape:', newvarshape, 'varv:', varv.shape
     28849        print 'Lluis shapes sint:', sint.shape
    2881228850
    2881328851#        newvarslice = []
     
    2883228870        #print allslices
    2883328871
     28872        vout = None
     28873        voutt = None
     28874        varvt = None
    2883428875        varvt = varv.transpose()
    28835 
     28876        print 'vs:', varvt[7, 69, :]
    2883628877        if len(vshape) == 3 and len(sN.shape) == 4:
    28837             voutt = fsci.module_scientific.multi_spacewightstats_in3drk3_slc3v4(     \
     28878            voutt = fsci.module_scientific.multi_spaceweightstats_in3drk3_slc3v4(    \
    2883828879              varin=varvt, ngridsin=sNt, gridsin=sint, percentages=spt,              \
    2883928880              di1=vshape[2], di2=vshape[1], di3=vshape[0], ds1=ssh[3],               \
     
    2884128882            vout = voutt.transpose()
    2884228883        elif len(vshape) == 3 and len(sN.shape) == 3:
    28843             voutt = fsci.module_scientific.multi_spacewightstats_in3drk3_slc3v3(     \
     28884            voutt = fsci.module_scientific.multi_spaceweightstats_in3drk3_slc3v3(    \
    2884428885              varin=varvt, ngridsin=sNt, gridsin=sint, percentages=spt,              \
    2884528886              di1=vshape[2], di2=vshape[1], di3=vshape[0], ds1=ssh[2],               \
     
    2885528896            quit(-1)
    2885628897
     28898        print "Lluis Let's check !!", vout.shape
     28899        print 'i j k it:', i, j, k ,it, 'N:', sN[i,j,k], '<>', slicesvals
     28900        for iv in range(sN[i,j,k]):
     28901            print iv, 'c:', sin[:,iv,i,j,k], 'p:', sgpa[iv,i,j,k], 'v:',             \
     28902              varv[it,sin[0,iv,i,j,k],sin[1,iv,i,j,k]]
     28903        print 'min max mean mean2 std median count _______'
     28904        print vout[:,it,i,j,k]
     28905
    2885728906        ist = 0
    2885828907        for statn in statns:
Note: See TracChangeset for help on using the changeset viewer.