Changeset 1728 in lmdz_wrf
- Timestamp:
- Dec 15, 2017, 4:18:27 PM (7 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/module_scientific.f90
r1661 r1728 36 36 ! read_overlap_polys_ascii: Subroutine to read from an ASCII file the associated polygons at a given time-step 37 37 ! read_overlap_tracks_ascii: Subroutine to write to an ASCII the polygons associated to a trajectory at a given time step 38 ! reconstruct_matrix: Subroutine to reconstruct a 2D matrix from a pair of syncronized vectors with the positions on x 39 ! and y coordinates 38 40 ! SortR_K*: Subroutine receives an array x() r_K and sorts it into ascending order. 39 41 ! StatsR_K: Subroutine to provide the minmum, maximum, mean, the quadratic mean, and the standard deviation of a … … 51 53 52 54 CONTAINS 55 56 SUBROUTINE reconstruct_matrix(vectorXpos, vectorYpos, dvec, Xmin, Xmax, Ymin, Ymax, dmatx, dmaty, & 57 matProj, maxdiff, matind, matX, matY) 58 ! Subroutine to reconstruct a 2D matrix from a pair of syncronized vectors with the positions on x 59 ! and y coordinates 60 61 IMPLICIT NONE 62 63 INTEGER, INTENT(in) :: dvec, dmatx, dmaty 64 REAL(r_k), DIMENSION(dvec), INTENT(in) :: vectorXpos, vectorYpos 65 REAL(r_k), INTENT(in) :: Xmin, Xmax, Ymin, Ymax, maxdiff 66 CHARACTER(len=50), INTENT(in) :: matPRoj 67 INTEGER, DIMENSION(dmatx, dmaty), INTENT(out) :: matind 68 REAL(r_k), DIMENSION(dmatx, dmaty), INTENT(out) :: matX, matY 69 70 ! Local 71 INTEGER :: i,j, idiff, jdiff 72 REAL(r_k) :: ddx, ddy, Xpos, Ypos, mindiff 73 REAL(r_k), DIMENSION(dvec) :: diff 74 INTEGER, DIMENSION(1) :: mindiffloc 75 CHARACTER(LEN=50) :: RSa, RSb 76 77 !!!!!!! Variables 78 ! vectorXpos, vectorYpos: syncronized vectors with the x,y coordinates from which the matrix will be reconstructed 79 ! dvec: quantitiy of coordinates to use 80 ! Xmin,Xmax,Ymin,Ymax: Location range of the matrix to be reconstructed 81 ! maxdiff: Authorized maximum distance between matrix location and vector location 82 ! dmatx, dmaty: Size in grid points of the matrix to be reconstructed 83 ! matProj: Assumed projection of the values of the matrix 84 ! 'latlon': regular lat/lon projection 85 ! matind: matrix with the correspondant indiced from the vector of positions 86 ! matX, matY: matrix with the coordinates of the elements of the matrix 87 88 fname = 'reconstruct_matrix' 89 90 matind = zeroRK 91 92 Projection: SELECT CASE(TRIM(matProj)) 93 94 CASE('latlon') 95 ! Resolution along matrix axes 96 ddx = (Xmax - Xmin)/(dmatx-1) 97 ddy = (Ymax - Ymin)/(dmaty-1) 98 99 ! Filling matrix positions 100 DO i=1,dmatx 101 Xpos = Xmin + ddx*(i-1) 102 DO j=1,dmaty 103 Ypos = Ymin + ddy*(j-1) 104 matX(i,j) = Xpos 105 matY(i,j) = Ypos 106 107 diff = SQRT((Xpos - vectorXpos)**2 + (Ypos - vectorYpos)**2) 108 mindiffloc = MINLOC(diff) 109 mindiff = MINVAL(diff) 110 IF (mindiff > maxdiff) THEN 111 WRITE(RSa, '(f20.10)')mindiff 112 WRITE(RSb, '(f20.10)')maxdiff 113 msg = 'Difference: ' // TRIM(RSa) // ' overpasses the maximum authorized distance: ' // & 114 TRIM(RSb) 115 CALL ErrMsg(msg, fname, -1) 116 END IF 117 118 matind(i,j) = mindiffloc(1) 119 120 END DO 121 END DO 122 123 CASE DEFAULT 124 msg = "Projection of matrix '" // TRIM(matProj) // "' not ready " // CHAR(10) // & 125 " Available ones: 'latlon'" 126 CALL ErrMsg(msg, fname, -1) 127 END SELECT Projection 128 129 END SUBROUTINE reconstruct_matrix 53 130 54 131 SUBROUTINE read_finaltrack_ascii(funit, dt, itrack, ftrack) -
trunk/tools/nc_var_tools.py
r1705 r1728 111 111 # pinterp: Function to vertically interpolate using subroutines from the p_interp.F90 NCAR program 112 112 # put_variable_slice: Function to add a variable from a netcdf object to a new one following a slice 113 # reconstruct_matrix_from_vector: Function to reconstruct a 2D matrix from a pair of syncronized vectors with the positions on x and y coordinates 113 114 # remapnn: Function to remap to the nearest neightbor a variable using projection from another file 114 115 # remapnn_old: Function to remap to the nearest neightbor a variable using projection from another fil … … 20693 20694 return WRFtime, tunits 20694 20695 20696 def reconstruct_matrix_from_vector(values, ncfile, variable): 20697 """ Function to reconstruct a 2D matrix from a pair of syncronized vectors with 20698 the positions on x and y coordinates 20699 values=[varXpos]:[varYpos]:[Xmin]:[Xmax]:[Ymin]:[Ymax]:[matProj]:[maxdiff] 20700 [varXpos]: variable with the vector of X positions 20701 [varYpos]: variable with the vector of Y positions 20702 [Xmin]: minimum X-value of the range of the matrix to be reconstructed ('all' for all range) 20703 [Xmax]: maximum X-value of the range of the matrix to be reconstructed ('all' for all range) 20704 [Ymin]: minimum Y-value of the range of the matrix to be reconstructed ('all' for all range) 20705 [Ymax]: maximum Y-value of the range of the matrix to be reconstructed ('all' for all range) 20706 [matProj]: assumed projection of the final matrix 20707 'latlon': regular lon/lat projection 20708 [maxdiff]: maximum allowed distance between matrix grid point and vector location 20709 ncfile= netCDF file to use 20710 variable= ',' list of variables to re-matrix ('all' for all variables) 20711 """ 20712 import module_ForSci as Sci 20713 fname = 'reconstruct_matrix_from_vector' 20714 20715 availProj = ['latlon'] 20716 20717 if values == 'h': 20718 print fname + '_____________________________________________________________' 20719 print reconstruct_matrix_from_vector.__doc__ 20720 quit() 20721 20722 expectargs = '[varXpos]:[varYpos]:[Xmin]:[Xmax]:[Ymin]:[Ymax]:[matProj]:' + \ 20723 '[maxdiff]' 20724 gen.check_arguments(fname, values, expectargs, ':') 20725 20726 varXpos = values.split(':')[0] 20727 varYpos = values.split(':')[1] 20728 if values.split(':')[2] != 'all': 20729 Xmin = np.float(values.split(':')[2]) 20730 Xmax = np.float(values.split(':')[3]) 20731 Ymin = np.float(values.split(':')[4]) 20732 Ymax = np.float(values.split(':')[5]) 20733 else: 20734 Xmin = values.split(':')[2] 20735 matProj = values.split(':')[6] 20736 maxdiff = np.float(values.split(':')[7]) 20737 20738 if not gen.searchInlist(availProj, matProj): 20739 print errormsg 20740 print ' ' + fname + ": projection '" + matProj + "' not available !!" 20741 print ' available ones:', availProj 20742 uit(-1) 20743 20744 onc = NetCDFFile(ncfile, 'r') 20745 ncvars = onc.variables.keys() 20746 ncdims = onc.dimensions 20747 20748 # checking 20749 if not gen.serarchInlist(ncvars, varXpos): 20750 print errormsg 20751 print ' ' + fname + ": file '" + ncfile + "' does not have variable '" + \ 20752 varXpos + "' !!" 20753 print ' available ones:', ncvars 20754 quit(-1) 20755 if not gen.serarchInlist(ncvars, varYpos): 20756 print errormsg 20757 print ' ' + fname + ": file '" + ncfile + "' does not have variable '" + \ 20758 varYpos + "' !!" 20759 print ' available ones:', ncvars 20760 quit(-1) 20761 20762 if variable == 'all': 20763 varns = ncvars 20764 else: 20765 varns = variable.split(',') 20766 for vn in varns: 20767 if not gen.serarchInlist(ncvars, vn): 20768 print errormsg 20769 print ' ' + fname + ": file '" + ncfile + "' does not have " + \ 20770 " variable '" + vn + "' !!" 20771 print ' available ones:', ncvars 20772 quit(-1) 20773 20774 # Getting values 20775 oxpos = onc.variables[varXpos] 20776 oypos = onc.variables[varYpos] 20777 20778 xposv = oxpos[:] 20779 yposv = oypos[:] 20780 20781 gen.same_shape(xposv, yposv) 20782 vecdim = oxpos.dimensions[0] 20783 20784 if gen.searchInlist(oxpos.ncattrs(), 'units'): 20785 xunits = oxpos.getattribute('units') 20786 else: 20787 xunits = '-' 20788 if gen.searchInlist(oypos.ncattrs(), 'units'): 20789 yunits = oypos.getattribute('units') 20790 else: 20791 yunits = '-' 20792 20793 if Xmin == 'all': 20794 Xmin = np.min(xposv) 20795 Xmax = np.max(xposv) 20796 Ymin = np.min(yposv) 20797 Ymax = np.max(yposv) 20798 20799 # Matrix values 20800 if matProj == 'latlon': 20801 ddx = xposv[1]- xposv[0] 20802 ddy = yposv[1]- yposv[0] 20803 dimx = (Xmax - Xmin+ddx)/ddx 20804 dimy = (Ymax - Ymin+ddy)/ddy 20805 20806 matindt, matXt, matYt = Sci.reconstruct_matrix(vectorxpos=xposv, vectorypos=yposv, \ 20807 dvec=len(xposv), xmin=Xmin, xmax=Xmax, ymin=Ymin, ymax=Ymax, dmatx=dimx, \ 20808 dmaty=dimy, matProj, maxdiff) 20809 20810 matind = matindt.transpose() 20811 20812 # Creation of file 20813 onewnc = NetCDFFile('reconstruct_matrix.nc', 'w') 20814 20815 # Dimensions 20816 newdim = onewnc.createDimension('x', dimx) 20817 newdim = onewnc.createDimension('y', dimy) 20818 20819 # Variable-dimension 20820 newvar = onewnc.createVariable(varXpos, 'f8', ('y', 'x')) 20821 newvar[:] = matXt.transpose() 20822 basicvardef(newvar, varXpos, varXpos, xunits) 20823 20824 newvar = onewnc.createVariable(varYpos, 'f8', ('y', 'x')) 20825 newvar[:] = matYt.transpose() 20826 basicvardef(newvar, varYpos, varYpos, yunits) 20827 onewnc.sync() 20828 20829 # Getting variables 20830 for vn in varns: 20831 if not onewnc.variables.has_key(vn): 20832 ovar = onc.variables[vn] 20833 indn = ovar.dimensions 20834 vardims = [] 20835 shapevar = [] 20836 for dn in indn: 20837 if not gen.searchInlist(onewnc.dimensions, dn) and dn != Xdimn and \ 20838 dn != Ydimn: 20839 if onc.dimensions[dn].isunlimited(): 20840 newdim = nc.createDimension(dn, None) 20841 else: 20842 newdim = nc.createDimension(dn, len(onc.dimensions[dn])) 20843 20844 if dn == vecdim: 20845 vardims.append('y') 20846 vardims.append('x') 20847 shapevar.append(dimy) 20848 shapevar.append(dimx) 20849 else: 20850 vardims.append(dn) 20851 shapevar.append(len(onc.dimensions[dn])) 20852 20853 if ovar.dtype = type(int(2)): 20854 newvar= onewnc.createVariable(vn, nctype(ovar.dtype), tuple(vardims),\ 20855 fill_value=gen.fillValueI) 20856 varvals = np.ones(tuple(shapevar), dtype=ovar.dtype)*gen.fillValueI 20857 else: 20858 newvar= onewnc.createVariable(vn, nctype(ovar.dtype), tuple(vardims),\ 20859 fill_value=gen.fillValueF) 20860 varvals = np.ones(tuple(shapevar), dtype=ovar.dtype)*gen.fillValueF 20861 20862 slices = gen.provide_slices(vardims, shapevar, ['x','y']) 20863 # Filling variable. It would be faster if we can avoid this loop... I'm feeling lazy! 20864 iix = vardims.index('x') 20865 iiy = vardims.index('y') 20866 for slc in slices: 20867 ival = slc[iix] 20868 jval = slc[iiy] 20869 oldvarslice = [] 20870 for dn in indn: 20871 iid = vardims.index(dn) 20872 if dn == vecdimn: oldvarslice.append(matind(jval,ival)) 20873 else: oldvarslice.append(slc[iid]) 20874 20875 newvar[tuple(slc)] = onc[tuple(oldvarslice)] 20876 20877 # Attributes 20878 for atn in ovar.ncattrs(): 20879 if atn != 'fill_Value': 20880 atv = ovar.getattribute(atn) 20881 set_attribute(newvar, atn, atv) 20882 onewnc.sync() 20883 20884 # Global attributes 20885 for atn in onc.ncattrs(): 20886 atv = ovar.getattribute(atn) 20887 set_attribute(onewnc, atn, atv) 20888 onewnc.sync() 20889 add_global_PyNCplot(onewnc, pyscript, fname, '0.1') 20890 20891 return 20892 20695 20893 #quit()
Note: See TracChangeset
for help on using the changeset viewer.