source: trunk/UTIL/NCL/planeto.ncl @ 497

Last change on this file since 497 was 497, checked in by slebonnois, 13 years ago

Ajout de scripts NCL dans UTIL. Pas encore tres developpe mais dites moi ce que vous en pensez. + correction bug pour marees grav Titan dans leapfrog

File size: 11.2 KB
Line 
1load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
2load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
3load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
4load "~/Graphiques/Ncl/visu-utils.ncl"
5
6begin
7
8;---------------------
9;---- load the file
10;---------------------
11
12infile=addfile(inFile,"r")
13
14;---------------------
15;---- get variable list and dimension names
16;---------------------
17
18liste=getfilevarnames(infile)
19nvars=dimsizes(liste)
20
21;---- finding the dimensions
22tname="dummy"
23pname="dummy"
24lname="dummy"
25Lname="dummy"
26do i=0,nvars-1
27  if ((liste(i).eq."time").or.(liste(i).eq."time_counter").or.(liste(i).eq."Time")) then
28    tname=liste(i)
29  end if
30  if ((liste(i).eq."lev").or.(liste(i).eq."presnivs")) then
31    pname=liste(i)
32  end if
33  if ((liste(i).eq."lat").or.(liste(i).eq."latitude")) then
34    lname=liste(i)
35  end if
36  if ((liste(i).eq."lon").or.(liste(i).eq."longitude")) then
37    Lname=liste(i)
38  end if
39end do
40if ((tname.eq."dummy").or.(pname.eq."dummy").or.(tname.eq."dummy").or.(tname.eq."dummy")) then
41  print("One dimension was not found... See -h for help.")
42  exit
43else
44  names=(/tname,pname,lname,Lname/)
45end if
46
47;---- temporal axis : for Titan, Ls ; for Venus, Vdays.
48Vday=1.0087e7      ; Venus day
49Tday=1.37889e6     ; Titan day
50axels=False
51timeax=infile->$tname$
52timeaxis=tofloat(timeax-timeax(0))/Vday
53copy_VarMeta(timeax,timeaxis)
54timeaxis@units="Local days"
55if (isfilevar(infile,"ls")) then
56   delete(timeaxis)
57   timeaxis=infile->ls
58   axels=True
59end if
60if (isfilevar(infile,"Ls")) then
61   delete(timeaxis)
62   timeaxis=infile->Ls
63   axels=True
64end if
65ndls=dimsizes(dimsizes(timeaxis))
66if (ndls.ne.1) then
67   delete(timeax)
68   timeax=timeaxis(:,0,0)
69   delete(timeaxis)
70   timeaxis=timeax
71end if
72
73;---------------------
74;---- print only list of variables
75;---------------------
76if (var.eq."liste") then
77  print(liste)
78  exit
79end if
80
81;==============================
82; Prepare variable(s) for the plot
83;==============================
84
85;---------------------
86;---- load variable(s)
87;---------------------
88  variable=infile->$var$
89  nbdim=dimsizes(filevardimsizes(infile,var))
90
91  overplot=False
92  diffdims=False
93  if (var2.ne."dummy") then
94     variable2=infile->$var2$
95     nbdim2=dimsizes(filevardimsizes(infile,var2))
96     if (nbdim2.ne.nbdim) then
97       diffdims=True
98     end if
99     overplot=True
100  end if
101 
102;---------------------
103;---- trim variable to desired region
104;---------------------
105     dimname=new(nbdim,string)
106     mindimval=new(nbdim,float)
107     maxdimval=new(nbdim,float)
108     axetim=(/0./)          ; dummy
109     dimavg=new(4,logical)
110     do i=0,3
111       dimavg(i)=False
112     end do
113
114; locate dimensions of the variable
115     flag=positionDims(variable,nbdim,names)
116     if (diffdims) then
117       flag2=positionDims(variable2,nbdim2,names)
118       dimname2=new(nbdim2,string)
119       mindimval2=new(nbdim2,float)
120       maxdimval2=new(nbdim2,float)
121       dimavg2=new(4,logical)
122     end if
123
124; store dim names, associated limits and flags
125     index=0
126     if (flag(0).ne.10) then
127        dimname(index)=tname
128        if (ntcutmin.ne.-999) then
129          mindimval(index)=tofloat(variable&$tname$(ntcutmin))
130          maxdimval(index)=tofloat(variable&$tname$(ntcutmax))
131; A REVOIR !!!
132     ; !! stupid bug, due to precision when time axis in double in file...
133     ;     if ((ntcutmin.eq.0).and.(ntcutmax.eq.0)) then
134     ;       mindimval(index)=mindimval(index)*1.000001
135     ;       maxdimval(index)=maxdimval(index)*1.000001
136     ;     end if
137     ; !! stupid bug, due to precision when time axis in double in file...
138        else
139          mindimval(index)=-999
140          maxdimval(index)=-999
141        end if
142        if (tavg.eq.-999) then
143          dimavg(index)=True
144        else
145     ; trim time axis
146          delete(axetim)
147          axetim=timeaxis({$dimname(0)$|mindimval(0):maxdimval(0)})
148        end if
149        index=index+1
150     end if
151     if (flag(1).ne.10) then
152        dimname(index)=pname
153        mindimval(index)=pcutmin
154        maxdimval(index)=pcutmax
155        if (pavg.eq.-999) then
156          dimavg(index)=True
157        end if
158        index=index+1
159     end if
160     if (flag(2).ne.10) then
161        dimname(index)=lname
162        mindimval(index)=lcutmin
163        maxdimval(index)=lcutmax
164        if (lavg.eq.-999) then
165          dimavg(index)=True
166        end if
167        index=index+1
168     end if
169     if (flag(3).ne.10) then
170        dimname(index)=Lname
171        mindimval(index)=Lcutmin
172        maxdimval(index)=Lcutmax
173        if (Lavg.eq.-999) then
174          dimavg(index)=True
175        end if
176     end if
177
178; if global average, prepare limits
179     do i=0,nbdim-1
180        if (mindimval(i).eq.-999) then
181          mindimval(i)=tofloat(variable&$dimname(i)$(0))
182          maxdimval(i)=tofloat(variable&$dimname(i)$(dimsizes(variable&$dimname(i)$)-1))
183        end if
184     end do
185
186; if orthographic projection, default limits for lat and lon
187     if (typeplot.eq."ortho") then
188      do i=0,nbdim-1
189        if ((dimname(i).eq.lname).or.(dimname(i).eq.Lname)) then
190          mindimval(i)=tofloat(variable&$dimname(i)$(0))
191          maxdimval(i)=tofloat(variable&$dimname(i)$(dimsizes(variable&$dimname(i)$)-1))
192        end if
193      end do
194      print("Latitude and Longitude are defaulted to full map.")
195     end if
196     
197; if stereographic projection, default limits for lon
198     if (typeplot.eq."stereo") then
199      do i=0,nbdim-1
200        if (dimname(i).eq.Lname) then
201          mindimval(i)=tofloat(variable&$dimname(i)$(0))
202          maxdimval(i)=tofloat(variable&$dimname(i)$(dimsizes(variable&$dimname(i)$)-1))
203        end if
204      end do
205      print("Longitude is defaulted to full map.")
206     end if
207     
208; case of second variable
209     if (.not.diffdims) then
210        dimname2  =dimname
211        mindimval2=mindimval
212        maxdimval2=maxdimval
213        dimavg2   =dimavg
214     else
215      index=0
216      if (flag2(0).ne.10) then
217        dimname2(index)=tname
218        index=index+1
219      end if
220      if (flag2(1).ne.10) then
221        dimname2(index)=pname
222        index=index+1
223      end if
224      if (flag2(2).ne.10) then
225        dimname2(index)=lname
226        index=index+1
227      end if
228      if (flag2(3).ne.10) then
229        dimname2(index)=Lname
230      end if
231      do i=0,nbdim2-1
232        do j=0,nbdim-1
233          if (dimname2(i).eq.dimname(j)) then
234            mindimval2(i)=mindimval(j)
235            maxdimval2(i)=maxdimval(j)
236            dimavg2(i)   =dimavg(j)
237          end if
238        end do
239      end do
240     end if
241     
242; trim variable(s)
243     vartrim=trimVar(variable,nbdim,dimname,mindimval,maxdimval)
244
245     if (overplot) then
246       vartrim2=trimVar(variable2,nbdim2,dimname2,mindimval2,maxdimval2)
247     end if
248
249;---------------------
250;---- make averages 
251; (if limits are identical, average need to be made to trim the dimension)
252;---------------------
253     varavg=avgVar(vartrim,nbdim,dimavg)
254
255     if (overplot) then
256       varavg2=avgVar(vartrim2,nbdim2,dimavg2)
257     end if
258
259; after trim, redo dim sizes
260     nbdimavg=dimsizes(dimsizes(varavg))
261     if (overplot) then
262       nbdimavg2=dimsizes(dimsizes(varavg2))
263       if (nbdimavg2.ne.nbdimavg) then
264         print("This case is not yet possible: the second variable must be plotted similarly to the first one...")
265         overplot=False
266       end if
267     end if
268
269;==============================
270; Single point
271;==============================
272
273if ((nbdimavg.eq.1).and.(dimsizes(varavg).eq.1)) then
274  print(mindimval)
275  print(varavg)
276  if (overplot) then
277    print(varavg2)
278  end if
279  exit
280end if
281
282;==============================
283;       TRACES
284;==============================
285
286; after trim, redo flag for dims
287     flag=positionDims(varavg,nbdimavg,names)
288
289; 2d maps (e.g. for ortho proj):
290; revert longitudes if decreasing
291  if ( (nbdimavg.eq.2).and.(varavg!0.eq.lname).and.(varavg!1.eq.Lname).and.(varavg&$Lname$(0).gt.varavg&$Lname$(1)) ) then
292     newvaravg=revertLon_2dmap(varavg)
293     delete(varavg)
294     varavg=newvaravg
295     if (overplot) then
296        newvaravg=revertLon_2dmap(varavg2)
297        delete(varavg2)
298        varavg2=newvaravg
299     end if
300  end if
301 
302; Pressure axis used ?
303  axep=False
304  if (flag(1).ne.10) then
305    axep=True
306  end if
307
308; Time axis used ?
309  axet=False
310  if (flag(0).ne.10) then
311    axet=True
312  end if
313
314; limits for contours (2d) or variable axis (1d) ?
315  limits=new(3,float)
316  limits(0)=valmin
317  limits(1)=valmax
318  limits(2)=step
319  if (overplot) then
320    limits2=new(3,float)
321    limits2(0)=valmin2
322    limits2(1)=valmax2
323    limits2(2)=step2
324  end if
325
326; output workstation
327  wks   = gsn_open_wks(media,outputname)
328  resbase          = True
329  resbase@gsnDraw  = False
330  resbase@gsnFrame = False
331
332;---------------------
333if (typeplot.eq."coupe") then
334;---------------------
335
336; Too many dims
337  if (nbdimavg.gt.2) then
338    print("Too many dimensions for a simple 1D/2D plot...")
339    exit
340  end if
341
342; 2D MAPS
343  if (nbdimavg.eq.2) then
344    res2d = resbase
345  ;Time axis used with lat or pressure: it must be on X
346    if ((axet).and.(flag(3).eq.10)) then
347      revvar=inversDim_2d(varavg)
348      delete(varavg)
349      varavg=revvar
350    end if
351  ;defaults and options for plot
352    res2d=deflimits_2d(varavg,res2d)
353    res2d=contours_2d(res2d,limits)
354  ;pressure axis
355    if (axep) then
356      res2d=axePress_2d(res2d)
357    end if
358  ;time axis
359    if ((axet).and.(flag(3).eq.10)) then
360      if (axels) then
361        res2d=axeTimeXls_2d(res2d,axetim)
362      else
363        res2d=axeTimeX_2d(res2d,axetim)
364      end if
365    end if
366    if ((axet).and.(flag(3).ne.10)) then
367      if (axels) then
368        res2d=axeTimeYls_2d(res2d,axetim)
369      else
370        res2d=axeTimeY_2d(res2d,axetim)
371      end if
372    end if
373  ;creating map
374    plotvar=Map_2d(varavg,wks,res2d)
375  ;overlay
376    if (overplot) then
377      res2d_op=res2d
378      res2d_op=contours_2d(res2d_op,limits2)
379      res2d_op=optionsOverPlot_2d(res2d_op)
380      plotvar2=OPlot_2d(varavg2,wks,res2d_op)
381      overlay(plotvar,plotvar2)
382    end if
383  end if
384
385; 1D PLOT
386  if (nbdimavg.eq.1) then
387    res1d = resbase
388    axename=getvardims(varavg)
389    axe1d=varavg&$axename(0)$
390  ;time axis
391    if (axet) then
392      delete(axe1d)
393      axe1d=axetim
394      if (axels) then
395        res1d=axeTimels_1d(res1d,axe1d)
396      else
397        res1d=axeTime_1d(res1d,axe1d)
398      end if
399    end if
400  ;creating plot
401    plotvar=Plot_1d(varavg,wks,res1d,axe1d,axep,limits)
402  end if
403
404end if
405
406;---------------------
407if (typeplot.eq."ortho") then
408;---------------------
409
410; Need 2D
411  if ((nbdimavg.ne.2).and.((flag(1).eq.10).or.(flag(2).eq.10))) then
412    print("You need 2D lat/lon for orthographic projection...")
413    exit
414  end if
415
416; options
417  center  = (/Lcenter,lcenter/)
418  grd     = grid
419  spacing = (/gLspc,glspc/)
420  gridcol = gcol
421 
422  res2d = resbase
423  res2d = contours_2d(res2d,limits)
424  res2d = optionsOrtho(res2d,center,grd,spacing,gridcol)
425
426;creating plot
427  plotvar=Ortho_2d(varavg,wks,res2d)
428
429end if
430
431;---------------------
432if (typeplot.eq."stereo") then
433;---------------------
434
435; Need 2D
436  if ((nbdimavg.ne.2).and.((flag(1).eq.10).or.(flag(2).eq.10))) then
437    print("You need 2D lat/lon for stereographic projection...")
438    exit
439  end if
440
441; options
442  if (lcenter.eq.-90) then
443    hemisph="SH"
444  else
445    hemisph="NH"
446  end if
447  grd     = grid
448  spacing = (/gLspc,glspc/)
449  gridcol = gcol
450 
451  res2d = resbase
452  res2d = contours_2d(res2d,limits)
453  res2d = optionsStereo(res2d,hemisph,grd,spacing,gridcol)
454 
455;creating plot
456  plotvar=Stereo_2d(varavg,wks,res2d)
457
458end if
459
460;---------------------
461  draw(plotvar)                   
462  frame(wks)
463
464end
465
Note: See TracBrowser for help on using the repository browser.