Changeset 2267 in lmdz_wrf
- Timestamp:
- Dec 26, 2018, 7:38:41 PM (6 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/module_scientific.f90
r2266 r2267 4932 4932 END SUBROUTINE spacepercen_within_reg 4933 4933 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 4934 5008 END MODULE module_scientific -
trunk/tools/nc_var_tools.py
r2266 r2267 27200 27200 [dimxn]: name of the dimension along the x-axis 27201 27201 [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 27202 27206 [slicedim]: series of values to define the slicing of the variable 27203 27207 * [integer]: which value of the dimension … … 27240 27244 27241 27245 iif = 0 27246 Nexp = 8 27242 27247 for fileinf in files: 27243 27248 expectargs = '[netcdfname]:[dimvals]' … … 27254 27259 quit(-1) 27255 27260 27256 if len(dimvalsref) != 6:27261 if len(dimvalsref) != Nexp: 27257 27262 print errormsg 27258 27263 print ' ' + fname + ": wrong number of dimension-values for " + \ 27259 27264 "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]'" 27263 27269 quit(-1) 27264 27270 … … 27267 27273 refdimxn = dimvalsref[0] 27268 27274 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] 27273 27281 27274 27282 if not gen.searchInlist(oncref.dimensions, refdimxn): … … 27290 27298 quit(-1) 27291 27299 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 27292 27309 if not gen.searchInlist(oncref.dimensions, refdimyn): 27293 27310 print errormsg … … 27308 27325 quit(-1) 27309 27326 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) 27310 27335 27311 27336 slicedict = {refdimxn: slicedimx, refdimyn: slicedimy} 27312 27337 ovarx = oncref.variables[refvarxn] 27338 ovarxbnds = oncref.variables[refvarxbndsn] 27313 27339 ovary = oncref.variables[refvaryn] 27340 ovarybnds = oncref.variables[refvarybndsn] 27314 27341 27315 27342 refvarx, dims = SliceVarDict(ovarx,slicedict) 27343 refvarbndsx, dims = SliceVarDict(ovarxbnds,slicedict) 27316 27344 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] 27317 27351 27318 27352 else: … … 27326 27360 quit(-1) 27327 27361 27328 if len(dimvalsget) != 6:27362 if len(dimvalsget) != Nexp: 27329 27363 print errormsg 27330 27364 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]'" 27334 27369 quit(-1) 27335 27370 … … 27338 27373 getdimxn = dimvalsget[0] 27339 27374 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] 27344 27381 27345 27382 if not gen.searchInlist(oncget.dimensions, getdimxn): … … 27361 27398 quit(-1) 27362 27399 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 27363 27409 if not gen.searchInlist(oncget.dimensions, getdimyn): 27364 27410 print errormsg … … 27379 27425 quit(-1) 27380 27426 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) 27381 27435 27382 27436 slicedict = {getdimxn: slicedimx, getdimyn: slicedimy} 27383 27437 ovarx = oncget.variables[getvarxn] 27438 ovarxbnds = oncget.variables[getvarxbndsn] 27384 27439 ovary = oncget.variables[getvaryn] 27385 27440 27386 27441 getvarx, dims = SliceVarDict(ovarx,slicedict) 27442 getvarxbnds, dims = SliceVarDict(ovarxbnds,slicedict) 27387 27443 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] 27388 27450 27389 27451 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') 27390 27520 27391 27521 # Getting values 27392 27522 27523 add_global_PyNCplot(onewnc, 'nc_var_tools', fname, '1.0') 27524 27393 27525 return 27394 27526 27395 fvals= 'reference_data.nc:lon;lon; -1;lat;lat;-1,get_data.nc:lon;lon;-1;lat;lat;-1'27527 fvals= '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' 27396 27528 area_weighted('yes:mean,min,max',fvals,'ct_values') 27397 27529
Note: See TracChangeset
for help on using the changeset viewer.