[180] | 1 | def latinterv (area): |
---|
| 2 | if area == "Europe": |
---|
| 3 | wlat = [20.,80.] |
---|
| 4 | wlon = [-50.,50.] |
---|
| 5 | elif area == "Central_America": |
---|
| 6 | wlat = [-10.,40.] |
---|
| 7 | wlon = [230.,300.] |
---|
| 8 | elif area == "Africa": |
---|
| 9 | wlat = [-20.,50.] |
---|
| 10 | wlon = [-50.,50.] |
---|
| 11 | elif area == "Whole": |
---|
| 12 | wlat = [-90.,90.] |
---|
| 13 | wlon = [-180.,180.] |
---|
| 14 | elif area == "Southern_Hemisphere": |
---|
| 15 | wlat = [-90.,60.] |
---|
| 16 | wlon = [-180.,180.] |
---|
| 17 | elif area == "Northern_Hemisphere": |
---|
| 18 | wlat = [-60.,90.] |
---|
| 19 | wlon = [-180.,180.] |
---|
| 20 | elif area == "Tharsis": |
---|
| 21 | wlat = [-30.,60.] |
---|
| 22 | wlon = [-170.,-10.] |
---|
| 23 | elif area == "Whole_No_High": |
---|
| 24 | wlat = [-60.,60.] |
---|
| 25 | wlon = [-180.,180.] |
---|
| 26 | elif area == "Chryse": |
---|
| 27 | wlat = [-60.,60.] |
---|
| 28 | wlon = [-60.,60.] |
---|
| 29 | elif area == "North_Pole": |
---|
| 30 | wlat = [60.,90.] |
---|
| 31 | wlon = [-180.,180.] |
---|
| 32 | elif area == "Close_North_Pole": |
---|
| 33 | wlat = [75.,90.] |
---|
| 34 | wlon = [-180.,180.] |
---|
| 35 | return wlon,wlat |
---|
| 36 | |
---|
| 37 | def ptitle (name): |
---|
| 38 | from matplotlib.pyplot import title |
---|
| 39 | title(name) |
---|
| 40 | print name |
---|
| 41 | |
---|
| 42 | def simplinterv (lon2d,lat2d): |
---|
| 43 | import numpy as np |
---|
| 44 | return [[np.min(lon2d),np.max(lon2d)],[np.min(lat2d),np.max(lat2d)]] |
---|
| 45 | |
---|
| 46 | def makeplotpngres (filename,res,pad_inches_value=0.25,folder='',disp=True): |
---|
| 47 | import matplotlib.pyplot as plt |
---|
| 48 | res = int(res) |
---|
[181] | 49 | if folder != '': name = folder+'/'+filename+str(res)+".png" |
---|
| 50 | else: name = filename+str(res)+".png" |
---|
[180] | 51 | plt.savefig(name,dpi=res,bbox_inches='tight',pad_inches=pad_inches_value) |
---|
[181] | 52 | if disp: display(name) |
---|
[180] | 53 | return |
---|
| 54 | |
---|
| 55 | def makeplotpng (filename,pad_inches_value=0.25,minres=100.,folder=''): |
---|
| 56 | makeplotpngres(filename,minres, pad_inches_value=pad_inches_value,folder=folder) |
---|
| 57 | makeplotpngres(filename,minres+200.,pad_inches_value=pad_inches_value,folder=folder,disp=False) |
---|
| 58 | return |
---|
| 59 | |
---|
| 60 | def getcoord2d (nc,nlat='XLAT',nlon='XLONG'): |
---|
| 61 | import numpy as np |
---|
| 62 | lat = nc.variables[nlat][0,:,:] |
---|
| 63 | lon = nc.variables[nlon][0,:,:] |
---|
| 64 | if np.array(lat).ndim != 2: [lon2d,lat2d] = np.meshgrid(lon,lat) |
---|
| 65 | else: [lon2d,lat2d] = [lon,lat] |
---|
| 66 | return lon2d,lat2d |
---|
| 67 | |
---|
| 68 | def smooth (field, coeff): |
---|
| 69 | ## actually blur_image could work with different coeff on x and y |
---|
| 70 | if coeff > 1: result = blur_image(field,int(coeff)) |
---|
| 71 | else: result = field |
---|
| 72 | return result |
---|
| 73 | |
---|
| 74 | def gauss_kern(size, sizey=None): |
---|
| 75 | import numpy as np |
---|
| 76 | ## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth |
---|
| 77 | # Returns a normalized 2D gauss kernel array for convolutions |
---|
| 78 | size = int(size) |
---|
| 79 | if not sizey: |
---|
| 80 | sizey = size |
---|
| 81 | else: |
---|
| 82 | sizey = int(sizey) |
---|
| 83 | x, y = np.mgrid[-size:size+1, -sizey:sizey+1] |
---|
| 84 | g = np.exp(-(x**2/float(size)+y**2/float(sizey))) |
---|
| 85 | return g / g.sum() |
---|
| 86 | |
---|
| 87 | def blur_image(im, n, ny=None) : |
---|
| 88 | from scipy.signal import convolve |
---|
| 89 | ## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth |
---|
| 90 | # blurs the image by convolving with a gaussian kernel of typical size n. |
---|
| 91 | # The optional keyword argument ny allows for a different size in the y direction. |
---|
| 92 | g = gauss_kern(n, sizey=ny) |
---|
| 93 | improc = convolve(im, g, mode='same') |
---|
| 94 | return improc |
---|
| 95 | |
---|
| 96 | def vectorfield (u, v, x, y, stride=3, scale=15., factor=250., color='black', csmooth=1): |
---|
| 97 | ## scale regle la reference du vecteur |
---|
| 98 | ## factor regle toutes les longueurs (dont la reference). l'AUGMENTER pour raccourcir les vecteurs. |
---|
| 99 | import matplotlib.pyplot as plt |
---|
| 100 | import numpy as np |
---|
| 101 | posx = np.max(x)*0.90 |
---|
| 102 | posy = np.mean(y) |
---|
| 103 | u = smooth(u,csmooth) |
---|
| 104 | v = smooth(v,csmooth) |
---|
| 105 | q = plt.quiver( x[::stride,::stride],\ |
---|
| 106 | y[::stride,::stride],\ |
---|
| 107 | u[::stride,::stride],\ |
---|
| 108 | v[::stride,::stride],\ |
---|
| 109 | angles='xy',color=color,\ |
---|
| 110 | scale=factor,width=0.003 ) |
---|
| 111 | if color=='white': kcolor='black' |
---|
| 112 | elif color=='yellow': kcolor=color |
---|
| 113 | else: kcolor=color |
---|
| 114 | p = plt.quiverkey(q,posx,posy,scale,\ |
---|
| 115 | str(int(scale)),coordinates='data',color=kcolor) |
---|
| 116 | return p |
---|
| 117 | |
---|
| 118 | def display (name): |
---|
| 119 | from os import system |
---|
[181] | 120 | system("display "+name+" > /dev/null 2> /dev/null &") |
---|
[180] | 121 | return name |
---|
| 122 | |
---|
| 123 | def findstep (wlon): |
---|
| 124 | steplon = int((wlon[1]-wlon[0])/3.) |
---|
| 125 | step = 60. |
---|
| 126 | if steplon < 60.: step = 30. |
---|
| 127 | if steplon < 30.: step = 15. |
---|
| 128 | if steplon < 15.: step = 10. |
---|
| 129 | if steplon < 10.: step = 5. |
---|
| 130 | if steplon < 5.: step = 1. |
---|
| 131 | return step |
---|
| 132 | |
---|
| 133 | def define_proj (char,wlon,wlat,back="."): |
---|
| 134 | from mpl_toolkits.basemap import Basemap |
---|
| 135 | import numpy as np |
---|
| 136 | import matplotlib as mpl |
---|
| 137 | meanlon = 0.5*(wlon[0]+wlon[1]) |
---|
| 138 | meanlat = 0.5*(wlat[0]+wlat[1]) |
---|
| 139 | h = 2000. |
---|
[181] | 140 | if char == "cyl": m = Basemap(projection='cyl',llcrnrlat=wlat[0],urcrnrlat=wlat[1],llcrnrlon=wlon[0],urcrnrlon=wlon[1]) |
---|
[180] | 141 | elif char == "moll": m = Basemap(projection='moll',lon_0=meanlon) |
---|
| 142 | elif char == "ortho": m = Basemap(projection='ortho',lon_0=meanlon,lat_0=meanlat) |
---|
| 143 | elif char == "lcc": m = Basemap(projection='lcc',lat_1=meanlat,lat_0=meanlat,lon_0=meanlon,\ |
---|
| 144 | llcrnrlat=wlat[0],urcrnrlat=wlat[1],llcrnrlon=wlon[0],urcrnrlon=wlon[1]) |
---|
| 145 | elif char == "npstere": m = Basemap(projection='npstere', boundinglat=wlat[0], lon_0=0.) |
---|
| 146 | elif char == "spstere": m = Basemap(projection='spstere', boundinglat=wlat[0], lon_0=0.) |
---|
| 147 | elif char == "nsper": m = Basemap(projection='nsper',lon_0=meanlon,lat_0=meanlat,satellite_height=h*1000.) |
---|
| 148 | fontsizemer = int(mpl.rcParams['font.size']*2./3.) |
---|
| 149 | if char in ["cyl","lcc"]: step = findstep(wlon) |
---|
| 150 | else: step = 10. |
---|
| 151 | m.drawmeridians(np.r_[-180.:180.:step*2.], labels=[0,0,0,1], color='grey', fontsize=fontsizemer) |
---|
| 152 | m.drawparallels(np.r_[-90.:90.:step], labels=[1,0,0,0], color='grey', fontsize=fontsizemer) |
---|
| 153 | if back == ".": m.warpimage(marsmap(),scale=0.75) |
---|
| 154 | elif back == None: pass |
---|
| 155 | else: m.warpimage(marsmap(back),scale=0.75) |
---|
| 156 | return m |
---|
| 157 | |
---|
| 158 | def marsmap (whichone="vishires"): |
---|
| 159 | whichlink = { \ |
---|
| 160 | "vis": "http://maps.jpl.nasa.gov/pix/mar0kuu2.jpg",\ |
---|
| 161 | "vishires": "http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/MarsMap_2500x1250.jpg",\ |
---|
| 162 | "mola": "http://www.lns.cornell.edu/~seb/celestia/mars-mola-2k.jpg",\ |
---|
| 163 | "molabw": "http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/MarsElevation_2500x1250.jpg",\ |
---|
| 164 | } |
---|
| 165 | if whichone not in whichlink: |
---|
| 166 | print "marsmap: choice not defined... you'll get the default one... " |
---|
| 167 | whichone = "vishires" |
---|
| 168 | return whichlink[whichone] |
---|
| 169 | |
---|
| 170 | def earthmap (whichone): |
---|
| 171 | if whichone == "contrast": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/EarthMapAtmos_2500x1250.jpg" |
---|
| 172 | elif whichone == "bw": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/EarthElevation_2500x1250.jpg" |
---|
| 173 | elif whichone == "nice": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/earthmap1k.jpg" |
---|
| 174 | return whichlink |
---|
| 175 | |
---|