Changeset 1947 in lmdz_wrf for trunk/tools/generic_tools.py
- Timestamp:
- Jul 19, 2018, 4:58:53 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r1934 r1947 5763 5763 return angle 5764 5764 5765 def lonlat2D(lon, lat):5765 def lonlat2D(lon, lat, error=True): 5766 5766 """ Function to return lon, lat 2D matrices from any lon,lat matrix 5767 5767 lon= matrix with longitude values 5768 5768 lat= matrix with latitude values 5769 error= whether should quit if there is an error 5769 5770 """ 5770 5771 fname = 'lonlat2D' 5771 5772 5772 if len(lon.shape) != len(lat.shape): 5773 if len(lon.shape) == len(lat.shape): 5774 if len(lon.shape) == 3: 5775 lonvv = lon[0,:,:] 5776 latvv = lat[0,:,:] 5777 elif len(lon.shape) == 2: 5778 lonvv = lon[:] 5779 latvv = lat[:] 5780 elif len(lon.shape) == 1: 5781 lonlatv = np.meshgrid(lon[:],lat[:]) 5782 lonvv = lonlatv[0] 5783 latvv = lonlatv[1] 5784 else: 5773 5785 print errormsg 5774 5786 print ' ' + fname + ': longitude values with shape:', lon.shape, \ 5775 5787 'is different than latitude values with shape:', lat.shape, '(dif. size) !!' 5776 quit(-1) 5777 5778 if len(lon.shape) == 3: 5779 lonvv = lon[0,:,:] 5780 latvv = lat[0,:,:] 5781 elif len(lon.shape) == 2: 5782 lonvv = lon[:] 5783 latvv = lat[:] 5784 elif len(lon.shape) == 1: 5785 lonlatv = np.meshgrid(lon[:],lat[:]) 5786 lonvv = lonlatv[0] 5787 latvv = lonlatv[1] 5788 if error: quit(-1) 5789 5790 # Not so weird case !! 5791 print ' Trying to fix it!' 5792 if len(lon.shape) == 3: 5793 lonv0 = lon[0,:,:] 5794 else: 5795 lonv0 = lon.copy() 5796 if len(lat.shape) == 3: 5797 latv0 = lat[0,:,:] 5798 else: 5799 latv0 = lat.copy() 5800 if len(lonv0.shape) == 1 and len(latv0.shape) == 2: 5801 lonvv = np.zeros(latv0.shape, dtype=np.float) 5802 if lonv0.shape[0] == latv0.shape[0]: 5803 for k in range(latv0.shape[1]): 5804 lonvv[:,k] = lonv0[:] 5805 else: 5806 for k in range(latv0.shape[0]): 5807 lonvv[k,:] = lonv0[:] 5808 latvv = latv0.copy() 5809 5810 if len(lonv0.shape) == 2 and len(latv0.shape) == 1: 5811 lonvv = lonv0.copy() 5812 latvv = np.zeros(lonv0.shape, dtype=np.float) 5813 if lonv0.shape[0] == latv0.shape[0]: 5814 for k in range(lonv0.shape[1]): 5815 latvv[:,k] = latv0[:] 5816 else: 5817 for k in range(lonv0.shape[0]): 5818 latvv[k,:] = latv0[:] 5788 5819 5789 5820 return lonvv, latvv 5790 5821 5791 def interpolate_locs (locs,coords,kinterp):5822 def interpolate_locs_old(locs,coords,kinterp): 5792 5823 """ Function to provide interpolate locations on a given axis 5793 5824 interpolate_locs(locs,axis,kinterp) … … 5823 5854 5824 5855 for iloc in range(Nlocs): 5856 found = False 5857 a = None 5858 b = None 5859 c = None 5825 5860 for icor in range(Ncoords-1): 5826 5861 if locs[iloc] < minc and dcoords > 0.: 5827 5862 a = 0. 5828 b = 1. / (coords[1] - coords[0])5863 b = coords[1] - coords[0] 5829 5864 c = coords[0] 5830 5865 elif locs[iloc] > maxc and dcoords > 0.: 5831 5866 a = (Ncoords-1)*1. 5832 b = 1. / (coords[Ncoords-1] - coords[Ncoords-2])5867 b = coords[Ncoords-1] - coords[Ncoords-2] 5833 5868 c = coords[Ncoords-2] 5834 5869 elif locs[iloc] < minc and dcoords < 0.: 5835 5870 a = (Ncoords-1)*1. 5836 b = 1. / (coords[Ncoords-1] - coords[Ncoords-2])5871 b = coords[Ncoords-1] - coords[Ncoords-2] 5837 5872 c = coords[Ncoords-2] 5838 5873 elif locs[iloc] > maxc and dcoords < 0.: 5839 5874 a = 0. 5840 b = 1. /(coords[1] - coords[0])5875 b = (coords[1] - coords[0]) 5841 5876 c = coords[0] 5842 5877 elif locs[iloc] >= coords[icor] and locs[iloc] < coords[icor+1] and dcoords > 0.: 5843 5878 a = icor*1. 5844 b = 1. / (coords[icor+1] - coords[icor])5879 b = coords[icor+1] - coords[icor] 5845 5880 c = coords[icor] 5846 print coords[icor], locs[iloc], coords[icor+1], ':', icor, '->', a, b5847 5881 elif locs[iloc] <= coords[icor] and locs[iloc] > coords[icor+1] and dcoords < 0.: 5848 5882 a = icor*1. 5849 b = 1. / (coords[icor+1] - coords[icor])5883 b = coords[icor+1] - coords[icor] 5850 5884 c = coords[icor] 5885 if a is not None: found = True 5886 if not found: 5887 print errormsg 5888 print ' ' + fname + ": case not prepared !!" 5889 print ' locs[iloc]:', locs[iloc], 'minc:', minc, 'maxc:', maxc, 'dcoords:', dcoords 5890 quit(-1) 5851 5891 5852 5892 if kinterp == 'lin': 5853 intlocs[iloc] = a + (locs[iloc] - c) *b5893 intlocs[iloc] = a + (locs[iloc] - c)/b 5854 5894 else: 5855 5895 print errormsg 5856 5896 print ' ' + fname + ": interpolation kind '" + kinterp + "' not ready !!!!!" 5857 5897 quit(-1) 5898 5899 return intlocs 5900 5901 def interpolate_locs(locs,coords,kinterp): 5902 """ Function to provide interpolate locations on a given axis 5903 interpolate_locs(locs,axis,kinterp) 5904 locs= locations to interpolate 5905 coords= axis values with the reference of coordinates 5906 kinterp: kind of interpolation 5907 'lin': linear 5908 >>> coordinates = np.arange((10), dtype=np.float) 5909 >>> values = np.array([-1.2, 2.4, 5.6, 7.8, 12.0]) 5910 >>> interpolate_locs(values,coordinates,'lin') 5911 [ -1.2 2.4 5.6 7.8 12. ] 5912 >>> coordinates[0] = 0.5 5913 >>> coordinates[2] = 2.5 5914 >>> interpolate_locs(values,coordinates,'lin') 5915 [ -3.4 1.93333333 5.6 7.8 12. ] 5916 >>> coordinates = -10+np.arange((10), dtype=np.float) 5917 >>> values = -np.array([-1.2, 2.4, 5.6, 7.8, 12.0]) 5918 >>> interpolate_locs(values,coordinates,'lin') 5919 [ 11.2 7.6 4.4 2.2 -2. ] 5920 """ 5921 5922 fname = 'interpolate_locs' 5923 5924 if type(locs) == type('S') and locs == 'h': 5925 print fname + '_____________________________________________________________' 5926 print interpolate_locs.__doc__ 5927 quit() 5928 5929 Nlocs = locs.shape[0] 5930 Ncoords = coords.shape[0] 5931 5932 origsing = np.abs(coords[Ncoords-1]-coords[0])/(coords[Ncoords-1]-coords[0]) 5933 5934 intlocs = np.zeros((Nlocs), dtype=np.float) 5935 minc = np.min(coords) 5936 maxc = np.max(coords) 5937 5938 sortc = list(coords) 5939 sortc.sort() 5940 5941 for iloc in range(Nlocs): 5942 a = None 5943 refi = None 5944 if locs[iloc] < minc: 5945 a = 0 5946 refi = 0 5947 sing = -1. 5948 elif locs[iloc] > maxc: 5949 a = Ncoords-1 5950 refi = Ncoords-2 5951 sing = 1. 5952 else: 5953 for icor in range(Ncoords-1): 5954 if locs[iloc] >= sortc[icor] and locs[iloc] < sortc[icor+1]: 5955 a = icor 5956 refi = icor 5957 sing = 1. 5958 if a is None: 5959 print errormsg 5960 print ' ' + fname + ': locs[iloc]:', locs[iloc], 'minc:', minc, 'maxc:',\ 5961 maxc, 'not ready!!' 5962 quit(-1) 5963 b = sortc[refi+1]-sortc[refi] 5964 #print 'locs[iloc]:', locs[iloc], 'a:', a, 'refi:', refi, 'b:', b, 'np.abs(sortc[refi]-locs[iloc]):', np.abs(sortc[refi]-locs[iloc]) 5965 5966 if kinterp == 'lin': 5967 intlocs[iloc] = a + sing*np.abs(sortc[a]-locs[iloc])/b 5968 else: 5969 print errormsg 5970 print ' ' + fname + ": interpolation kind '" + kinterp + "' not ready !!!!!" 5971 quit(-1) 5972 5973 if origsing < 0.: intlocs = Ncoords - intlocs 5858 5974 5859 5975 return intlocs … … 12301 12417 12302 12418 # axis limits. They will be used to flip the axis in plot if necessary 12303 if len(dxv.shape) == 1: 12304 newxaxislim = np.array([dxv[0], dxv[len(dxv)-1]]) 12305 elif len(dxv.shape) == 2: 12306 newxaxislim = np.array([dxv[0,0], dxv[0,dxv.shape[1]-1]]) 12307 else: 12308 print errormsg 12309 print ' ' + fname + ": wrong rank of data for axis 'x':", dxv.shape, '!!' 12310 print ' must be of rank 2!!' 12311 quit(-1) 12312 12313 if len(dyv.shape) == 1: 12314 newyaxislim = np.array([dyv[0], dyv[len(dyv)-1]]) 12315 elif len(dyv.shape) == 2: 12316 newyaxislim = np.array([dyv[0,0], dyv[dyv.shape[0]-1],0]) 12317 else: 12318 print errormsg 12319 print ' ' + fname + ": wrong rank of data for axis 'y':", dyv.shape, '!!' 12320 print ' must be of rank 2!!' 12321 quit(-1) 12419 newxaxislim = [np.min(dxv), np.max(dxv)] 12420 newyaxislim = [np.min(dyv), np.max(dyv)] 12322 12421 12323 12422 for transform in transforms: … … 12376 12475 flip = transform.split('@')[1] 12377 12476 if flip == 'x': 12378 if len(newxaxislim.shape) == 1: 12379 newxaxislim = newxaxislim[::-1] 12380 else: 12381 for iy in len(newxaxislim.shape[0]): 12382 newxaxislim[iy,:] = newxaxislim[iy,::-1] 12477 oldv = list(newxaxislim) 12478 newxaxislim[0] = oldv[1] 12479 newxaxislim[1] = oldv[0] 12383 12480 elif flip == 'y': 12384 if len(newyaxislim.shape) == 1: 12385 newyaxislim = newyaxislim[::-1] 12386 else: 12387 for ix in len(newyaxislim.shape[1]): 12388 newyaxislim[:,ix] = newyaxislim[::-1,ix] 12481 oldv = list(newyaxislim) 12482 newyaxislim[0] = oldv[1] 12483 newyaxislim[1] = oldv[0] 12389 12484 elif flip == 'z': 12390 12485 newvals = newvals[...,::-1,:,:]
Note: See TracChangeset
for help on using the changeset viewer.