Changeset 2267 in lmdz_wrf


Ignore:
Timestamp:
Dec 26, 2018, 7:38:41 PM (6 years ago)
Author:
lfita
Message:

Getting there...

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/module_scientific.f90

    r2266 r2267  
    49324932  END SUBROUTINE spacepercen_within_reg
    49334933
     4934  SUBROUTINE spacepercen(dxA, dyA, xCAvals, yCAvals, NAvertexmax, xBAvals, yBAvals, dxB, dyB, xCBvals,&
     4935    yCBvals, NBvertexmax, xBBvals, yBBvals, dxyB, strict, Ngridsin, gridsin, percentages)
     4936  ! Subroutine to compute the space-percentages of a series of grid cells (B) into another series of
     4937  !   grid-cells (A)
     4938  ! NOTE: Assuming coordinates on the plane with rectilinar, distance preserved and perpendicular x
     4939  !   and y axes.
     4940
     4941    IMPLICIT NONE
     4942
     4943    INTEGER, INTENT(in)                                  :: dxA, dyA, NAvertexAma, dxB, dyB,          &
     4944      NBvertexmax, dxyB
     4945    REAL(r_k), DIMENSION(dxA,dyA), INTENT(in)            :: xCAvals, yCAvals
     4946    REAL(r_k), DIMENSION(dxB,dyB), INTENT(in)            :: xCBvals, yCBvals
     4947    REAL(r_k), DIMENSION(dxA,dyA,NAvertexmax), INTENT(in):: xBAvals, yBAvals
     4948    REAL(r_k), DIMENSION(dxB,dyB,NBvertexmax), INTENT(in):: xBBvals, yBBvals
     4949    LOGICAL, INTENT(in)                                  :: strict
     4950    INTEGER, DIMENSION(dxA,dyA), INTENT(out)             :: Ngridsin
     4951    INTEGER, DIMENSION(dxA,dyA,dxyB,2), INTENT(out)      :: gridsin
     4952    REAL(r_k), DIMENSION(dxA,dyA,Ngridsin), INTENT(out)  :: percentages
     4953
     4954! Local
     4955   INTEGER                                               :: iv, ix, iy
     4956   INTEGER                                               :: Nvertex, NvertexAgrid, Ncoin, Nsort
     4957   CHARACTER(len=20)                                     :: DS
     4958   REAL(r_k)                                             :: areapoly, areagpoly, totarea, totpercent
     4959   REAL(r_k), ALLOCATABLE, DIMENSION(:,:)                :: vertexgrid, poinsin
     4960
     4961!!!!!!! Variables
     4962! dxA, dyA: shape of the matrix with the grid points A
     4963! xCAvals, yCAvals: coordinates of the center of the grid cells A
     4964! NAvertexmax: Maximum number of vertices of the grid cells A
     4965! xBAvals, yBAvals: coordinates of th vertices of the grid cells A (-99999 for no vertex)
     4966! dxB, dyB: shape of the matrix with the grid points B
     4967! xCBvals, yCBvals: coordinates of the center of the grid cells B
     4968! NBvertexmax: Maximum number of vertices of the grid cells B
     4969! xBBvals, yBBvals: coordinates of th vertices of the grid cells B (-99999 for no vertex)
     4970! strict: give an error if the area of the polygon is not fully covered
     4971! Ngridsin: number of grids from grid B with some extension within the grid cell A
     4972! gridsin: indices of B grids within the grids of A
     4973! percentages: percentages of area of cells B of each of the grids within the grid cell A
     4974
     4975    fname = 'spacepercen'
     4976
     4977    DO ix = 1, dimxA
     4978      DO iy = 1, dimyA
     4979
     4980      ! Getting grid vertices
     4981      Nvertex = 0
     4982      DO iv=1, NAvertexmax
     4983        IF (xBAvals(ix,iy,iv) /= fillvalI) THEN
     4984          Nvertex = Nvertex + 1
     4985        END IF
     4986      END DO
     4987      IF (ALLOCATED(vertexgrid)) DEALLOCATE(vertexgrid)
     4988      ALLOCATE(vertexgrid(Nvertex,2))
     4989      vertexgrid(:,1) = xBAvals(ix,iy,1:Nvertex)
     4990      vertexgrid(:,2) = yBAvals(ix,iy,1:Nvertex)
     4991 
     4992      CALL grid_within_polygon(Nvertex, vertexgrid, dxB, dyB, dxB*dyB, xBCvals, yBCvals, NBvertexmax, &
     4993        xBBvals, yBBvals, Ngridsin(ix,iy), gridsin(ix,iy,:,:))
     4994      IF (ALLOCATE(poinsin)) DEALLOCATE(poinsin)
     4995      ALLOCATE(poinsin(Ngridsin(ix,iy),2))
     4996
     4997      DO iv=1, Ngridsin(ix,iy)
     4998        poinsin(i,1) = gridsin(ix,iy,iv,1)
     4999        poinsin(i,2) = gridsin(ix,iy,iv,2)
     5000      END DO
     5001
     5002      CALL spacepercen_within_reg(Nvertex, vertexgrid, dxB, dyB, NBvertexmax, xBBvals, yBBvals,       &
     5003        Ngridsin(ix,iy), poinsin, strict, percentages(ix,iy,:))
     5004
     5005      END DO
     5006    END DO
     5007
    49345008END MODULE module_scientific
  • trunk/tools/nc_var_tools.py

    r2266 r2267  
    2720027200            [dimxn]: name of the dimension along the x-axis
    2720127201            [varxn]: name of the variable with the values of dimxn
     27202            [varxbndsn]: name of the variable with the bounds of the values of varxn
     27203            [dimyn]: name of the dimension along the y-axis
     27204            [varyn]: name of the variable with the values of dimyn
     27205            [varybndsn]: name of the variable with the bounds of the values of varyn
    2720227206            [slicedim]: series of values to define the slicing of the variable
    2720327207              * [integer]: which value of the dimension
     
    2724027244
    2724127245    iif = 0
     27246    Nexp = 8
    2724227247    for fileinf in files:
    2724327248        expectargs = '[netcdfname]:[dimvals]'
     
    2725427259                quit(-1)
    2725527260
    27256             if len(dimvalsref) != 6:
     27261            if len(dimvalsref) != Nexp:
    2725727262                print errormsg
    2725827263                print '  ' + fname + ": wrong number of dimension-values for " +     \
    2725927264                  "reference !!"
    27260                 print '    6 values are expected and you provided', len(dimvalsref)
    27261                 print "    values must be '[dimxn];[varxn];[slicedim];[dimyn];" +    \
    27262                   "[varyn];[slicedim]'"
     27265                print '    ', Nexp,' values are expected and you provided',          \
     27266                  len(dimvalsref)
     27267                print "    values must be '[dimxn];[varxn];[varxbndsn];[slicedim];"+ \
     27268                  "[dimyn];[varyn];[varybndsn];[slicedim]'"
    2726327269                quit(-1)
    2726427270
     
    2726727273            refdimxn = dimvalsref[0]
    2726827274            refvarxn = dimvalsref[1]
    27269             slicedimx = dimvalsref[2]
    27270             refdimyn = dimvalsref[3]
    27271             refvaryn = dimvalsref[4]
    27272             slicedimy = dimvalsref[5]
     27275            refvarxbndsn = dimvalsref[2]
     27276            slicedimx = dimvalsref[3]
     27277            refdimyn = dimvalsref[4]
     27278            refvaryn = dimvalsref[5]
     27279            refvarybndsn = dimvalsref[6]
     27280            slicedimy = dimvalsref[7]
    2727327281
    2727427282            if not gen.searchInlist(oncref.dimensions, refdimxn):
     
    2729027298                quit(-1)
    2729127299
     27300            if not oncref.variables.has_key(refvarxbndsn):
     27301                print errormsg
     27302                print '  ' + fname + ": reference file '" + netcdfrefn + "' does " + \
     27303                  "not have variable dimension for x '" + refvarxbndsn + "' !!"
     27304                ncvar = list(oncref.varibales.keys)
     27305                ncvar.sort()
     27306                print '    available ones:', ncvar
     27307                quit(-1)
     27308
    2729227309            if not gen.searchInlist(oncref.dimensions, refdimyn):
    2729327310                print errormsg
     
    2730827325                quit(-1)
    2730927326
     27327            if not oncref.variables.has_key(refvarybndsn):
     27328                print errormsg
     27329                print '  ' + fname + ": reference file '" + netcdfrefn + "' does " + \
     27330                  "not have variable dimension for x '" + refvarybndsn + "' !!"
     27331                ncvar = list(oncref.varibales.keys)
     27332                ncvar.sort()
     27333                print '    available ones:', ncvar
     27334                quit(-1)
    2731027335
    2731127336            slicedict = {refdimxn: slicedimx, refdimyn: slicedimy}
    2731227337            ovarx = oncref.variables[refvarxn]
     27338            ovarxbnds = oncref.variables[refvarxbndsn]
    2731327339            ovary = oncref.variables[refvaryn]
     27340            ovarybnds = oncref.variables[refvarybndsn]
    2731427341
    2731527342            refvarx, dims = SliceVarDict(ovarx,slicedict)
     27343            refvarbndsx, dims = SliceVarDict(ovarxbnds,slicedict)
    2731627344            refvary, dims = SliceVarDict(ovary,slicedict)
     27345            refvarbndsy, dims = SliceVarDict(ovarybnds,slicedict)
     27346
     27347            reflon, reflat =  gen.lonlat2D(refvarx, refvary)
     27348            refdx = reflon.shape[1]
     27349            refdy = reflon.shape[0]
     27350            refmaxvert = refvarxbnds.shape[2]
    2731727351
    2731827352        else:
     
    2732627360                quit(-1)
    2732727361
    27328             if len(dimvalsget) != 6:
     27362            if len(dimvalsget) != Nexp:
    2732927363                print errormsg
    2733027364                print '  ' + fname + ": wrong number of dimension-values for get !!"
    27331                 print '    6 values are expected and you provided', len(dimvalsget)
    27332                 print "    values must be '[dimxn];[varxn];[slicedim];[dimyn];" +    \
    27333                   "[varyn];[slicedim]'"
     27365                print '    ', Nexp,' values are expected and you provided',          \
     27366                  len(dimvalsget)
     27367                print "    values must be '[dimxn];[varxn];[varxbndsn];[slicedim];"+ \
     27368                  "[dimyn];[varyn];[varybndsn];[slicedim]'"
    2733427369                quit(-1)
    2733527370
     
    2733827373            getdimxn = dimvalsget[0]
    2733927374            getvarxn = dimvalsget[1]
    27340             slicedimx = dimvalsget[2]
    27341             getdimyn = dimvalsget[3]
    27342             getvaryn = dimvalsget[4]
    27343             slicedimy = dimvalsget[5]
     27375            getvarxbndsn = dimvalsget[2]
     27376            slicedimx = dimvalsget[3]
     27377            getdimyn = dimvalsget[4]
     27378            getvaryn = dimvalsget[5]
     27379            getvarybndsn = dimvalsget[6]
     27380            slicedimy = dimvalsget[7]
    2734427381
    2734527382            if not gen.searchInlist(oncget.dimensions, getdimxn):
     
    2736127398                quit(-1)
    2736227399
     27400            if not oncget.variables.has_key(getvarxbndsn):
     27401                print errormsg
     27402                print '  ' + fname + ": get file '" + netcdfgetn + "' does " +       \
     27403                  "not have variable dimension for x '" + getvarxbndsn + "' !!"
     27404                ncvar = list(oncget.varibales.keys)
     27405                ncvar.sort()
     27406                print '    available ones:', ncvar
     27407                quit(-1)
     27408
    2736327409            if not gen.searchInlist(oncget.dimensions, getdimyn):
    2736427410                print errormsg
     
    2737927425                quit(-1)
    2738027426
     27427            if not oncget.variables.has_key(getvarybndsn):
     27428                print errormsg
     27429                print '  ' + fname + ": get file '" + netcdfgetn + "' does " +       \
     27430                  "not have variable dimension for x '" + getvarybndsn + "' !!"
     27431                ncvar = list(oncget.varibales.keys)
     27432                ncvar.sort()
     27433                print '    available ones:', ncvar
     27434                quit(-1)
    2738127435
    2738227436            slicedict = {getdimxn: slicedimx, getdimyn: slicedimy}
    2738327437            ovarx = oncget.variables[getvarxn]
     27438            ovarxbnds = oncget.variables[getvarxbndsn]
    2738427439            ovary = oncget.variables[getvaryn]
    2738527440
    2738627441            getvarx, dims = SliceVarDict(ovarx,slicedict)
     27442            getvarxbnds, dims = SliceVarDict(ovarxbnds,slicedict)
    2738727443            getvary, dims = SliceVarDict(ovary,slicedict)
     27444            getvarybnds, dims = SliceVarDict(ovarybnds,slicedict)
     27445
     27446            getlon, getlat =  gen.lonlat2D(getvarx, getvary)
     27447            getdx = getlon.shape[1]
     27448            getdy = getlon.shape[0]
     27449            getmaxvert = getvarxbnds.shape[2]
    2738827450
    2738927451        iif = iif + 1
     27452    # Getting the space weights
     27453    reflont = reflon.transpose()
     27454    reflatt = reflat.transpose()
     27455    refvarxbndst = refvarxbnds.transpose()
     27456    refvarybndst = refvarybnds.transpose()
     27457    getlont = getlon.transpose()
     27458    getlatt = getlat.transpose()
     27459    getvarxbndst = getvarxbnds.transpose()
     27460    getvarybndst = getvarybnds.transpose()
     27461
     27462    Ngridsint, gridsint, percenst = fsci.module_scientific.spacepercen(dxa=refdx,    \
     27463      dya=refdy, xcavals=reflont, ycavals=reflatt, navertmax=refmaxvert,             \
     27464      xbAvals=refvarxbndst, ybAvals=refvarybndst,                                    \
     27465      dxb=getdx, dyb=getdy, xcbvals=getlont, ycbvals=getlatt, nbvertmax=getmaxvert,  \
     27466      xbbvals=getvarxbndst, ybbvals=getvarybndst, strict=strict)
     27467
     27468    Ngridsin = Ngridint.transpose()
     27469    gridsin = gridint.transpose()
     27470    percens = percenst.transpose()
     27471
     27472    # Opening new file
     27473    ofilen = fname + '.nc'
     27474
     27475    onewnc = NetCDFFile(ofilen, 'w')
     27476    # dimensions
     27477    newdim = onewnc.createDimension('lon',refdx)
     27478    newdim = onewnc.createDimension('lat',refdy)
     27479    newdim = onewnc.createDimension('bnds',refmaxvert)
     27480    newdim = onewnc.createDimension('gridin',np.max(gridsin))
     27481    newdim = onewnc.createDimension('coords',2)
     27482
     27483    # variable-dimensions
     27484    newvar = onewnc.createVariable('lon','f8',('lat','lon'))
     27485    newvar[:] = reflon
     27486    basicvardef(newvar,'longitude','Longitude','degrees_east')
     27487    newavr.setncattr('bounds','lon_bnds')
     27488
     27489    newvar = onewnc.createVariable('lon_bnds','f8',('bnds', 'lat','lon'))
     27490    newvar[:] = refvarxbnds
     27491    basicvardef(newvar,'longitude_bnds','Bounds of Longitude','degrees_east')
     27492
     27493    newvar = onewnc.createVariable('lat','f8',('lat','lon'))
     27494    newvar[:] = reflat
     27495    basicvardef(newvar,'latitude','Latitude','degrees_north')
     27496    newavr.setncattr('bounds','lat_bnds')
     27497
     27498    newvar = onewnc.createVariable('lat_bnds','f8',('bnds', 'lat','lon'))
     27499    newvar[:] = refvarybnds
     27500    basicvardef(newvar,'latitude_bnds','Bounds of Latitude','degrees_north')
     27501
     27502    # variables space-weight
     27503    newvar = onewnc.createVariable('Ngrid','i',('gridin', 'lat','lon'))
     27504    newvar[:] = Ngridsin
     27505    basicvardef(newvar, 'Ngrid', "number of grids cells grom 'get' laying within" +  \
     27506      " 'ref'",'-')
     27507    newavr.setncattr('coordinates','lon lat')
     27508
     27509    newvar = onewnc.createVariable('gridin','i',('coord', 'gridin', 'lat', 'lon'))
     27510    newvar[:] = Ngridsin
     27511    basicvardef(newvar,'gridin',"coordinates of the grids cells grom 'get' laying "+ \
     27512      "within 'ref'",'-')
     27513    newavr.setncattr('coordinates','lon lat')
     27514
     27515    newvar = onewnc.createVariable('gridpercen','i',('coord', 'gridin', 'lat', 'lon'))
     27516    newvar[:] = percens
     27517    basicvardef(newvar,'gridpercen',"percentages of the grids cells grom 'get' " +   \
     27518      "laying within 'ref'", '1')
     27519    newavr.setncattr('coordinates','lon lat')
    2739027520
    2739127521    # Getting values
    2739227522
     27523    add_global_PyNCplot(onewnc, 'nc_var_tools', fname, '1.0')
     27524
    2739327525    return
    2739427526
    27395 fvals= 'reference_data.nc:lon;lon;-1;lat;lat;-1,get_data.nc:lon;lon;-1;lat;lat;-1'
     27527fvals= 'reference_data.nc:lon;lon;lon_bnds;-1;lat;lat;lat_bnds;-1,get_data.nc:lon;lon;lon_bnds;-1;lat;lat;lat_bnds;-1'
    2739627528area_weighted('yes:mean,min,max',fvals,'ct_values')
    2739727529
Note: See TracChangeset for help on using the changeset viewer.