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

Last change on this file since 3457 was 888, checked in by slebonnois, 12 years ago

SL: small modifications to the tools, to Venus default .def files and to outputs (including forgotten modifications linked to the 1D); + bug corrections in phytitan

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