[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 | |
---|
[191] | 37 | #def landers (map) |
---|
| 38 | # map.plot(blue_calf_lon,blue_calf_lat, 'gs') |
---|
| 39 | # return |
---|
| 40 | |
---|
[195] | 41 | def getlschar ( namefile ): |
---|
| 42 | #### strangely enough this does not work for api or ncrcat results! |
---|
| 43 | from netCDF4 import Dataset |
---|
| 44 | from timestuff import sol2ls |
---|
| 45 | nc = Dataset(namefile) |
---|
[199] | 46 | if 'Times' in nc.variables and 'vert' not in nc.variables: |
---|
[195] | 47 | zetime = nc.variables['Times'][0] |
---|
| 48 | zetimestart = getattr(nc, 'START_DATE') |
---|
| 49 | zeday = int(zetime[8]+zetime[9]) - int(zetimestart[8]+zetimestart[9]) |
---|
| 50 | if zeday < 0: lschar="" ## might have crossed a month... fix soon |
---|
| 51 | else: lschar="_Ls"+str( int( sol2ls ( getattr( nc, 'JULDAY' ) + zeday ) ) ) |
---|
[197] | 52 | ### |
---|
| 53 | zetime2 = nc.variables['Times'][1] |
---|
| 54 | one = int(zetime[11]+zetime[12]) + int(zetime[14]+zetime[15])/37. |
---|
| 55 | next = int(zetime2[11]+zetime2[12]) + int(zetime2[14]+zetime2[15])/37. |
---|
| 56 | zehour = one |
---|
| 57 | zehourin = abs ( next - one ) |
---|
[195] | 58 | else: |
---|
| 59 | lschar="" |
---|
[197] | 60 | zehour = 0 |
---|
| 61 | zehourin = 1 |
---|
| 62 | return lschar, zehour, zehourin |
---|
[195] | 63 | |
---|
[186] | 64 | def api_onelevel ( path_to_input = None, \ |
---|
| 65 | input_name = 'wrfout_d0?_????-??-??_??:00:00', \ |
---|
| 66 | path_to_output = None, \ |
---|
| 67 | output_name = 'output.nc', \ |
---|
| 68 | process = 'list', \ |
---|
| 69 | fields = 'tk,W,uvmet,HGT', \ |
---|
| 70 | debug = False, \ |
---|
| 71 | bit64 = False, \ |
---|
| 72 | oldvar = True, \ |
---|
| 73 | interp_method = 4, \ |
---|
| 74 | extrapolate = 0, \ |
---|
| 75 | unstagger_grid = False, \ |
---|
| 76 | onelevel = 0.020 ): |
---|
| 77 | import api |
---|
| 78 | import numpy as np |
---|
| 79 | if not path_to_input: path_to_input = './' |
---|
| 80 | if not path_to_output: path_to_output = path_to_input |
---|
| 81 | api.api_main ( path_to_input, input_name, path_to_output, output_name, \ |
---|
| 82 | process, fields, debug, bit64, oldvar, np.arange (299), \ |
---|
| 83 | interp_method, extrapolate, unstagger_grid, onelevel ) |
---|
| 84 | return |
---|
| 85 | |
---|
[184] | 86 | def getproj (nc): |
---|
| 87 | map_proj = getattr(nc, 'MAP_PROJ') |
---|
| 88 | cen_lat = getattr(nc, 'CEN_LAT') |
---|
| 89 | if map_proj == 2: |
---|
| 90 | if cen_lat > 10.: |
---|
| 91 | proj="npstere" |
---|
| 92 | print "NP stereographic polar domain" |
---|
| 93 | else: |
---|
| 94 | proj="spstere" |
---|
| 95 | print "SP stereographic polar domain" |
---|
| 96 | elif map_proj == 1: |
---|
| 97 | print "lambert projection domain" |
---|
| 98 | proj="lcc" |
---|
| 99 | elif map_proj == 3: |
---|
| 100 | print "mercator projection" |
---|
| 101 | proj="merc" |
---|
[186] | 102 | else: |
---|
| 103 | proj="merc" |
---|
[184] | 104 | return proj |
---|
| 105 | |
---|
[180] | 106 | def ptitle (name): |
---|
| 107 | from matplotlib.pyplot import title |
---|
| 108 | title(name) |
---|
| 109 | print name |
---|
| 110 | |
---|
| 111 | def simplinterv (lon2d,lat2d): |
---|
| 112 | import numpy as np |
---|
| 113 | return [[np.min(lon2d),np.max(lon2d)],[np.min(lat2d),np.max(lat2d)]] |
---|
| 114 | |
---|
[184] | 115 | def wrfinterv (lon2d,lat2d): |
---|
| 116 | nx = len(lon2d[0,:])-1 |
---|
| 117 | ny = len(lon2d[:,0])-1 |
---|
| 118 | return [[lon2d[0,0],lon2d[nx,ny]],[lat2d[0,0],lat2d[nx,ny]]] |
---|
| 119 | |
---|
[180] | 120 | def makeplotpngres (filename,res,pad_inches_value=0.25,folder='',disp=True): |
---|
| 121 | import matplotlib.pyplot as plt |
---|
| 122 | res = int(res) |
---|
[186] | 123 | name = filename+"_"+str(res)+".png" |
---|
| 124 | if folder != '': name = folder+'/'+name |
---|
[180] | 125 | plt.savefig(name,dpi=res,bbox_inches='tight',pad_inches=pad_inches_value) |
---|
[181] | 126 | if disp: display(name) |
---|
[180] | 127 | return |
---|
| 128 | |
---|
| 129 | def makeplotpng (filename,pad_inches_value=0.25,minres=100.,folder=''): |
---|
| 130 | makeplotpngres(filename,minres, pad_inches_value=pad_inches_value,folder=folder) |
---|
| 131 | makeplotpngres(filename,minres+200.,pad_inches_value=pad_inches_value,folder=folder,disp=False) |
---|
| 132 | return |
---|
| 133 | |
---|
[184] | 134 | def dumpbdy (field): |
---|
| 135 | nx = len(field[0,:])-1 |
---|
| 136 | ny = len(field[:,0])-1 |
---|
| 137 | return field[5:ny-5,5:nx-5] |
---|
[180] | 138 | |
---|
[184] | 139 | def getcoord2d (nc,nlat='XLAT',nlon='XLONG',is1d=False): |
---|
| 140 | import numpy as np |
---|
| 141 | if is1d: |
---|
| 142 | lat = nc.variables[nlat][:] |
---|
| 143 | lon = nc.variables[nlon][:] |
---|
| 144 | [lon2d,lat2d] = np.meshgrid(lon,lat) |
---|
| 145 | else: |
---|
| 146 | lat = nc.variables[nlat][0,:,:] |
---|
| 147 | lon = nc.variables[nlon][0,:,:] |
---|
| 148 | [lon2d,lat2d] = [lon,lat] |
---|
| 149 | return lon2d,lat2d |
---|
| 150 | |
---|
[180] | 151 | def smooth (field, coeff): |
---|
| 152 | ## actually blur_image could work with different coeff on x and y |
---|
| 153 | if coeff > 1: result = blur_image(field,int(coeff)) |
---|
| 154 | else: result = field |
---|
| 155 | return result |
---|
| 156 | |
---|
| 157 | def gauss_kern(size, sizey=None): |
---|
| 158 | import numpy as np |
---|
| 159 | ## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth |
---|
| 160 | # Returns a normalized 2D gauss kernel array for convolutions |
---|
| 161 | size = int(size) |
---|
| 162 | if not sizey: |
---|
| 163 | sizey = size |
---|
| 164 | else: |
---|
| 165 | sizey = int(sizey) |
---|
| 166 | x, y = np.mgrid[-size:size+1, -sizey:sizey+1] |
---|
| 167 | g = np.exp(-(x**2/float(size)+y**2/float(sizey))) |
---|
| 168 | return g / g.sum() |
---|
| 169 | |
---|
| 170 | def blur_image(im, n, ny=None) : |
---|
| 171 | from scipy.signal import convolve |
---|
| 172 | ## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth |
---|
| 173 | # blurs the image by convolving with a gaussian kernel of typical size n. |
---|
| 174 | # The optional keyword argument ny allows for a different size in the y direction. |
---|
| 175 | g = gauss_kern(n, sizey=ny) |
---|
| 176 | improc = convolve(im, g, mode='same') |
---|
| 177 | return improc |
---|
| 178 | |
---|
[184] | 179 | def vectorfield (u, v, x, y, stride=3, scale=15., factor=250., color='black', csmooth=1, key=True): |
---|
| 180 | ## scale regle la reference du vecteur |
---|
| 181 | ## factor regle toutes les longueurs (dont la reference). l'AUGMENTER pour raccourcir les vecteurs. |
---|
| 182 | import matplotlib.pyplot as plt |
---|
| 183 | import numpy as np |
---|
[187] | 184 | #posx = np.max(x) + np.std(x) / 3. ## pb pour les domaines globaux ... |
---|
| 185 | #posy = np.mean(y) |
---|
[184] | 186 | #posx = np.min(x) |
---|
| 187 | #posy = np.max(x) |
---|
[187] | 188 | #posx = np.max(x) - np.std(x) / 10. |
---|
| 189 | #posy = np.max(y) + np.std(y) / 10. |
---|
| 190 | posx = np.min(x) - np.std(x) / 10. |
---|
| 191 | posy = np.min(y) - np.std(y) / 10. |
---|
[184] | 192 | u = smooth(u,csmooth) |
---|
| 193 | v = smooth(v,csmooth) |
---|
[188] | 194 | widthvec = 0.003 #0.005 #0.003 |
---|
[184] | 195 | q = plt.quiver( x[::stride,::stride],\ |
---|
| 196 | y[::stride,::stride],\ |
---|
| 197 | u[::stride,::stride],\ |
---|
| 198 | v[::stride,::stride],\ |
---|
| 199 | angles='xy',color=color,\ |
---|
| 200 | scale=factor,width=widthvec ) |
---|
| 201 | if color=='white': kcolor='black' |
---|
| 202 | elif color=='yellow': kcolor=color |
---|
| 203 | else: kcolor=color |
---|
| 204 | if key: p = plt.quiverkey(q,posx,posy,scale,\ |
---|
[194] | 205 | str(int(scale)),coordinates='data',color=kcolor,labelpos='S',labelsep = 0.03) |
---|
[184] | 206 | return |
---|
[180] | 207 | |
---|
| 208 | def display (name): |
---|
[184] | 209 | from os import system |
---|
| 210 | system("display "+name+" > /dev/null 2> /dev/null &") |
---|
| 211 | return name |
---|
[180] | 212 | |
---|
| 213 | def findstep (wlon): |
---|
[184] | 214 | steplon = int((wlon[1]-wlon[0])/4.) #3 |
---|
| 215 | step = 120. |
---|
| 216 | while step > steplon and step > 15. : step = step / 2. |
---|
| 217 | if step <= 15.: |
---|
| 218 | while step > steplon and step > 5. : step = step - 5. |
---|
| 219 | if step <= 5.: |
---|
| 220 | while step > steplon and step > 1. : step = step - 1. |
---|
| 221 | if step <= 1.: |
---|
| 222 | step = 1. |
---|
[180] | 223 | return step |
---|
| 224 | |
---|
| 225 | def define_proj (char,wlon,wlat,back="."): |
---|
| 226 | from mpl_toolkits.basemap import Basemap |
---|
| 227 | import numpy as np |
---|
| 228 | import matplotlib as mpl |
---|
| 229 | meanlon = 0.5*(wlon[0]+wlon[1]) |
---|
| 230 | meanlat = 0.5*(wlat[0]+wlat[1]) |
---|
[184] | 231 | if wlat[0] >= 80.: blat = 40. |
---|
| 232 | elif wlat[0] <= -80.: blat = -40. |
---|
| 233 | else: blat = wlat[0] |
---|
[180] | 234 | h = 2000. |
---|
[184] | 235 | radius = 3397200 |
---|
| 236 | if char == "cyl": m = Basemap(rsphere=radius,projection='cyl',\ |
---|
[180] | 237 | llcrnrlat=wlat[0],urcrnrlat=wlat[1],llcrnrlon=wlon[0],urcrnrlon=wlon[1]) |
---|
[184] | 238 | elif char == "moll": m = Basemap(rsphere=radius,projection='moll',lon_0=meanlon) |
---|
| 239 | elif char == "ortho": m = Basemap(rsphere=radius,projection='ortho',lon_0=meanlon,lat_0=meanlat) |
---|
| 240 | elif char == "lcc": m = Basemap(rsphere=radius,projection='lcc',lat_1=meanlat,lat_0=meanlat,lon_0=meanlon,\ |
---|
| 241 | llcrnrlat=wlat[0],urcrnrlat=wlat[1],llcrnrlon=wlon[0],urcrnrlon=wlon[1]) |
---|
| 242 | elif char == "npstere": m = Basemap(rsphere=radius,projection='npstere', boundinglat=blat, lon_0=0.) |
---|
| 243 | elif char == "spstere": m = Basemap(rsphere=radius,projection='spstere', boundinglat=blat, lon_0=0.) |
---|
| 244 | elif char == "nsper": m = Basemap(rsphere=radius,projection='nsper',lon_0=meanlon,lat_0=meanlat,satellite_height=h*1000.) |
---|
| 245 | elif char == "merc": m = Basemap(rsphere=radius,projection='merc',lat_ts=0.,\ |
---|
| 246 | llcrnrlat=wlat[0],urcrnrlat=wlat[1],llcrnrlon=wlon[0],urcrnrlon=wlon[1]) |
---|
| 247 | fontsizemer = int(mpl.rcParams['font.size']*3./4.) |
---|
| 248 | if char in ["cyl","lcc","merc"]: step = findstep(wlon) |
---|
| 249 | else: step = 10. |
---|
[180] | 250 | m.drawmeridians(np.r_[-180.:180.:step*2.], labels=[0,0,0,1], color='grey', fontsize=fontsizemer) |
---|
| 251 | m.drawparallels(np.r_[-90.:90.:step], labels=[1,0,0,0], color='grey', fontsize=fontsizemer) |
---|
| 252 | if back == ".": m.warpimage(marsmap(),scale=0.75) |
---|
| 253 | elif back == None: pass |
---|
| 254 | else: m.warpimage(marsmap(back),scale=0.75) |
---|
| 255 | return m |
---|
| 256 | |
---|
| 257 | def marsmap (whichone="vishires"): |
---|
| 258 | whichlink = { \ |
---|
| 259 | "vis": "http://maps.jpl.nasa.gov/pix/mar0kuu2.jpg",\ |
---|
| 260 | "vishires": "http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/MarsMap_2500x1250.jpg",\ |
---|
| 261 | "mola": "http://www.lns.cornell.edu/~seb/celestia/mars-mola-2k.jpg",\ |
---|
| 262 | "molabw": "http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/MarsElevation_2500x1250.jpg",\ |
---|
| 263 | } |
---|
| 264 | if whichone not in whichlink: |
---|
| 265 | print "marsmap: choice not defined... you'll get the default one... " |
---|
| 266 | whichone = "vishires" |
---|
| 267 | return whichlink[whichone] |
---|
| 268 | |
---|
| 269 | def earthmap (whichone): |
---|
| 270 | if whichone == "contrast": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/EarthMapAtmos_2500x1250.jpg" |
---|
| 271 | elif whichone == "bw": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/EarthElevation_2500x1250.jpg" |
---|
| 272 | elif whichone == "nice": whichlink="http://users.info.unicaen.fr/~karczma/TEACH/InfoGeo/Images/Planets/earthmap1k.jpg" |
---|
| 273 | return whichlink |
---|
| 274 | |
---|