source: palm/trunk/SCRIPTS/NCL/spectra.ncl @ 532

Last change on this file since 532 was 532, checked in by heinze, 14 years ago

NCL scripts allow the output of png files

File size: 21.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"
4
5;***************************************************
6; load .ncl.config or .ncl.config.default
7;***************************************************
8
9function which_script()
10local script
11begin
12   script="spectra"
13   return(script)
14end
15   
16if (isfilepresent("$PALM_BIN/../../.ncl.config")) then
17   loadscript("$PALM_BIN/../../.ncl.config")
18else
19  if (isfilepresent("$PALM_BIN/NCL/.ncl.config.default")) then
20     loadscript( "$PALM_BIN/NCL/.ncl.config.default")
21  else
22      palm_bin_path = getenv("PALM_BIN")
23      print(" ")
24      print("Neither the personal configuration file '.ncl.config' exists in")
25      print("~/palm/current_version")
26      print("nor the default configuration file '.ncl.config.default' exists in")
27      print(palm_bin_path + "/NCL")
28      print(" ")
29      exit
30   end if
31end if   
32
33begin
34
35   ;***************************************************
36   ; Retrieving the NCL version used
37   ;***************************************************
38   
39   ncl_version_ch = systemfunc("ncl -V")
40   ncl_version    = stringtofloat(ncl_version_ch)
41
42   ;***************************************************
43   ; Retrieving the double quote character
44   ;***************************************************
45   
46   dq=str_get_dq()
47
48   ;***************************************************
49   ; set up default parameter values and strings
50   ;***************************************************
51 
52   if (file_1 .EQ. "File in") then
53      print(" ")
54      print("Declare input file 'file_1=' in '.ncl.config' or prompt")
55      print(" ")
56      exit
57   else
58      file_in = file_1   
59   end if
60
61   if (format_out .NE. "x11" .AND. format_out .NE. "pdf" .AND. format_out .NE. "eps" .AND. format_out .NE. "ps" .AND. format_out .NE. "epsi" .AND. format_out .NE. "ncgm" .AND. format_out .NE. "png")then
62      print(" ")
63      print("'format_out = "+format_out+"' is invalid and set to'x11'")
64      print(" ")
65      format_out="x11"
66   end if
67
68   if (ncl_version .LE. 5.1 .AND. format_out .EQ. "png") then
69      print(" ")
70      print("Output of png files not available")
71      print("png output is avaiable with NCL version 5.2.0 and higher ")
72      print("NCL version used: " + ncl_version_ch)
73      print(" ")
74      exit
75   end if
76   
77   if (log_x .NE. 0 .AND. log_x .NE. 1)then
78      print(" ")
79      print("'log_x'= "+log_x+" is invalid and set to 1")
80      print(" ")
81      log_x = 1
82   end if 
83   
84   if (log_y .NE. 0 .AND. log_y .NE. 1)then
85      print(" ")
86      print("'log_y'= "+log_y+" is invalid and set to 1")
87      print(" ")
88      log_y = 1
89   end if   
90 
91   if (norm_y .EQ. 0.) then
92      print(" ")
93      print("Normalising with 0 is not allowed, 'norm_y' is set to 1.0")
94      print(" ")
95      norm_y = 1.0
96   end if
97   if (norm_x .EQ. 0.) then
98      print(" ")
99      print("Normalising with 0 is not allowed, 'norm_x' is set to 1.0")
100      print(" ")
101      norm_x= 1.0
102   end if
103   
104   if (sort .NE. "height" .AND. sort .NE. "time") then 
105      print(" ")
106      print("'sort'= "+sort+" is invalid and set to 'height'")
107      print(" ")
108      sort = "height" 
109   end if
110   
111   if (black .NE. 0 .AND. black .NE. 1)then
112      print(" ")
113      print("'black'= "+black+" is invalid and set to 0")
114      print(" ")
115      black = 0
116   end if
117 
118   if (dash .NE. 0 .AND. dash .NE. 1)then
119      print(" ")
120      print("'dash'= "+dash+" is invalid and set to 0")
121      print(" ")
122      dash = 0
123   end if
124
125   ;***************************************************
126   ; open input file
127   ;***************************************************
128   
129   file_in_1 = False
130   if (isStrSubset(file_in, ".nc"))then
131      start_f = -2
132      end_f = -2
133      file_in_1 = True     
134   end if
135
136   if (start_f .EQ. -1)then
137      print(" ")
138      print("'start_f' must be one of the cyclic numbers (at least 0) of your input file(s)")
139      print(" ") 
140      exit
141   end if
142   if (end_f .EQ. -1)then
143      print(" ")
144      print("'end_f' must be one of the cyclic numbers (at least 0) of your input file(s)")
145      print(" ") 
146      exit
147   end if
148
149   files=new(end_f-start_f+1,string)
150   
151   if (file_in_1)then
152      if (isfilepresent(file_in))then
153         files(0)=file_in
154      else
155         print(" ")
156         print("1st input file: '"+file_in+"' does not exist")
157         print(" ")
158         exit
159      end if
160   else   
161      if (start_f .EQ. 0)then
162         if (isfilepresent(file_in+".nc"))then
163            files(0)=file_in+".nc"
164            do i=1,end_f
165               if (isfilepresent(file_in+"."+i+".nc"))then   
166                  files(i)=file_in+"."+i+".nc"
167               else
168                  print(" ")
169                  print("Input file: '"+file_in+"."+i+".nc' does not exist")
170                  print(" ")
171                  exit 
172               end if       
173            end do         
174         else
175            print(" ")
176            print("Input file: '"+file_in+".nc' does not exist")
177            print(" ")
178            exit
179         end if
180      else
181         do i=start_f,end_f
182            if (isfilepresent(file_in+"."+i+".nc"))then   
183               files(i-start_f)=file_in+"."+i+".nc"
184            else
185               print(" ")
186               print("Input file: '"+file_in+"."+i+".nc' does not exist")
187               print(" ")
188               exit 
189            end if
190         end do
191      end if
192   end if
193
194   f=addfiles(files,"r")
195   f_att=addfile(files(0),"r")
196   ListSetType(f,"cat")
197   
198   vNam = getfilevarnames(f_att)
199   print(" ")
200   print("Variables in input file:")
201   print("- "+ vNam)
202   print(" ")
203   dim = dimsizes(vNam)
204   vDim = getfiledimsizes(f_att)
205 
206   t_all = f[:]->time
207   nt    = dimsizes(t_all)
208   delta_t=t_all(nt-1)/nt
209   
210   k_x=f_att->k_x
211   dimx=dimsizes(k_x)
212   k_y=f_att->k_y
213   dimy=dimsizes(k_y)
214   
215   
216   dim_level=dimsizes(height_level)
217
218   do i=0,dim-1
219      if (vNam(i) .EQ. "zu_sp")then
220         zu=f_att->zu_sp         
221         if (height_level(0) .EQ. -1)then
222            dimz=dimsizes(zu)
223         else
224            if (dim_level .GT. dimsizes(zu))then
225               print(" ")
226               print("'height_level' has more elements than available height levels in input file (= "+dimz+")")
227               print(" ")
228               exit
229            else
230               zuh=new(dim_level,double)
231               do le=0,dim_level-1
232                  zuh(le)=zu(height_level(le))
233               end do
234               dimz=dim_level
235            end if   
236         end if 
237      else
238         if (vNam(i) .EQ. "zw_sp")then
239            zw=f_att->zw_sp
240            if (height_level(0) .EQ. -1)then             
241               dimz=dimsizes(zw)
242            else
243               if (dim_level .GT. dimsizes(zw))then
244                  print(" ")
245                  print("'height_level' has more elements than available height levels in input file (= "+dimz+")")
246                  print(" ")
247                  exit
248               else
249                  zwh=new(dim_level,double)
250                  do le=0,dim_level-1
251                     zwh(le)=zw(height_level(le))
252                  end do
253                  dimz=dim_level
254               end if   
255            end if
256         end if
257      end if
258   end do
259
260   ;****************************************************       
261   ; start of time step and different types of mistakes that could be done
262   ;****************************************************
263   
264   if (start_time_step .EQ. -1.d) then         
265      start_time_step=t_all(0)/3600
266   else
267      if (start_time_step .GT. t_all(nt-1)/3600)then
268         print(" ")
269         print("'start_time_step' = "+ start_time_step +"h is greater than last time step = " + t_all(nt-1)+"s = "+t_all(nt-1)/3600+"h")
270         print(" ")
271         print("Select another 'start_time_step'")
272         print(" ")
273         exit
274      end if
275      if (start_time_step .LT. t_all(0)/3600)then
276         print(" ")
277         print("'start_time_step' = "+ start_time_step +"h is lower than first time step = " + t_all(0)+"s = "+t_all(0)/3600+"h")       
278         exit
279      end if
280   end if
281
282   do i=0,nt-1     
283      if (start_time_step .GE. (t_all(i)-delta_t/2)/3600 .AND. start_time_step .LT. (t_all(i)+delta_t/2)/3600)then
284         st=i
285         break
286      end if
287   end do
288   
289   if (.not. isvar("st"))then
290      print(" ")
291      print("'start_time_step' = "+ start_time_step +"h is invalid")
292      print(" ")
293      print("Select another 'start_time_step'")
294      print(" ")
295      exit
296   end if
297   
298   ;****************************************************
299   ; end of time step and different types of mistakes that could be done
300   ;****************************************************
301
302   if (end_time_step .EQ. -1.d) then           
303      end_time_step = t_all(nt-1)/3600
304   else
305      if (end_time_step .GT. t_all(nt-1)/3600)then
306         print(" ")
307         print("'end_time_step' = "+ end_time_step +"h is greater than last time step = " + t_all(nt-1)+"s = "+t_all(nt-1)/3600+"h")
308         print(" ")
309         print("Select another 'end_time_step'") 
310         print(" ")
311         exit
312      end if
313      if (end_time_step .LT. start_time_step/3600)then
314         print(" ")
315         print("'end_time_step' = "+ end_time_step +"h is lower than 'start_time_step' = "+start_time_step+"h")
316         print(" ")
317         print("Select another 'start_time_step' or 'end_time_step'")
318         print(" ")
319         exit
320      end if
321   end if
322
323   do i=0,nt-1     
324      if (end_time_step .GE. (t_all(i)-delta_t/2)/3600 .AND. end_time_step .LT. (t_all(i)+delta_t/2)/3600)then
325         et=i
326         break
327      end if
328   end do
329   
330   if (.not. isvar("et"))then
331      print(" ")
332      print("'end_time_step' = "+ end_time_step +"h is invalid")
333      print(" ")
334      print("Select another 'end_time_step'")
335      print(" ")
336      exit
337   end if
338
339   delete(start_time_step)
340   start_time_step=round(st,3)
341   delete(end_time_step)
342   end_time_step=round(et,3)
343
344   print(" ")
345   print("Output of time steps from "+t_all(start_time_step)/3600+" h = "+t_all(start_time_step)+" s => index = "+start_time_step)
346   print("                     till "+t_all(end_time_step)/3600+" h = "+t_all(end_time_step)+" s => index = "+end_time_step)
347   print(" ")
348
349   dimt = end_time_step-start_time_step+1
350 
351   ;***************************************************
352   ; set up recourses
353   ;***************************************************
354     
355   res = True
356   res@gsnDraw                 = False
357   res@gsnFrame                = False
358   res@txFont                  = "helvetica"
359   res@tiMainFont              = "helvetica"
360   res@tiXAxisFont             = "helvetica"
361   res@tiYAxisFont             = "helvetica"
362   res@tmXBLabelFont           = "helvetica"
363   res@tmYLLabelFont           = "helvetica"
364   res@lgLabelFont             = "helvetica"
365   res@tmLabelAutoStride       = True
366   
367   res@lgLabelFontHeightF     = font_size_legend
368   res@lgTitleFontHeightF     = font_size
369   res@txFontHeightF      = font_size
370   res@tiXAxisFontHeightF = font_size
371   res@tiYAxisFontHeightF = font_size
372   res@tmXBLabelFontHeightF = font_size
373   res@tmYLLabelFontHeightF = font_size
374   
375   res@tmXBMinorPerMajor = 4
376   res@tmYLMinorPerMajor = 4
377   
378   if (log_x .EQ. 1) then
379      res@trXLog = True
380   else
381      res@trXLog = False
382   end if
383   if (log_y .EQ. 1)then
384      res@trYLog = True
385   else 
386      res@trYLog = False
387   end if
388
389   legend_label=new(dimt,double)
390   legend_label_zu=new(dimz,double)
391   legend_label_zw=new(dimz,double)
392   legend_label_z=new(dimz,double)
393   do p=start_time_step,end_time_step
394      if (t_all(p)/3600 .LT. 1) then
395         legend_label(p-start_time_step)=decimalPlaces(t_all(p)/3600,2,True)
396      else
397         legend_label(p-start_time_step)=decimalPlaces(t_all(p)/3600,0,True)
398      end if
399   end do 
400   if (sort .EQ. "time")
401      plot  = new(dim*dimz,graphic)
402      np=dimt
403      res@lgTitleString = "Time [h]"
404   else
405      plot  = new(dim*dimt,graphic)
406      np=dimz
407     
408      do p=0,dimz-1
409         if (height_level(0) .EQ. -1)then
410            legend_label_zu(p)=round(zu(p),3)
411            legend_label_zw(p)=round(zw(p),3)
412         else
413            legend_label_zu(p)=round(zuh(p),3)
414            legend_label_zw(p)=round(zwh(p),3)
415         end if
416      end do
417   end if
418
419   if (black .eq. 0 ) then
420      if (np .EQ. 1)then
421         color = 237
422      else   
423         step=round(235/(np-1),3)
424         color = ispan(2,237,step)
425      end if
426   else
427      color = 2
428   end if
429   if ( dash .eq. 0 ) then
430      res@xyMonoDashPattern = True
431   end if
432
433   if ( format_out .EQ. "pdf" .OR. format_out .EQ. "ps" ) then
434      format_out@wkPaperSize = "A4"
435   end if
436   if ( format_out .EQ. "png" ) then
437      format_out@wkWidth  = 1000
438      format_out@wkHeight = 1000
439   end if
440
441   wks=gsn_open_wks(format_out,file_out)
442   gsn_define_colormap(wks,"rainbow+white")
443
444   n=0
445   do varn =dim-1,0,1
446     
447      check = True
448
449      if ( isStrSubset( vNam(varn), "time") .OR. isStrSubset( vNam(varn), "zu_sp") .OR. isStrSubset( vNam(varn), "zw_sp") .OR. isStrSubset( vNam(varn), "k_x") .OR. isStrSubset( vNam(varn), "k_y")) then
450            check = False
451      end if
452
453      if (var .NE. "all") then
454         check = isStrSubset( var,","+vNam(varn)+"," )
455      end if
456
457      if(check) then
458
459         temp = f[:]->$vNam(varn)$
460         data = temp(start_time_step:end_time_step,0:dimz-1,:)
461
462         temp_att = f_att->$vNam(varn)$
463         a=getvardims(temp_att)
464         b=dimsizes(a)
465
466         if (height_level(0) .NE. -1)then
467            do te=0,dimz-1
468            print(height_level(te))
469               data(:,te,:) = temp(start_time_step:end_time_step,height_level(te),:)           
470            end do
471         end if
472
473         data=data/(norm_y*norm_x)  ;SMOOTHING: #temp=smth9(temp/norm, 0.50, -0.25, False)#
474           
475         do i=0,b-1           
476            if (isStrSubset( a(i),"zu_sp" ))then
477               legend_label_z=legend_label_zu
478               if (height_level(0) .NE. -1)then
479                  z=zuh
480               else
481                  z=zu
482               end if
483            else
484               if (isStrSubset( a(i),"zw_sp" ))then
485                  legend_label_z=legend_label_zw
486                  if (height_level(0) .NE. -1)then
487                     z=zwh
488                  else
489                     z=zw
490                  end if   
491               end if
492            end if
493         end do 
494         
495         min_x=new(dimz,double)
496         max_x=new(dimz,double)
497         min_y=new(dimz,float)
498         max_y=new(dimz,float)
499         plot_h  = new(dimz,graphic)
500         
501         if (isStrSubset(vNam(varn),"x"))then
502            x_axis = new((/dimz,dimx/),double)
503            do q=0,dimz-1
504               x_axis(q,:) = f_att->k_x
505               x_axis = x_axis/norm_x
506            end do
507            if (norm_x .NE. 1.)then
508               res@tiXAxisString = "k~B~x~N~ ["+unit_x+"]"
509            else
510               if (norm_height .EQ. 1)then
511                  res@tiXAxisString = "k~B~x~N~ x z [1]"
512               else
513                  res@tiXAxisString = "k~B~x~N~ [1/m]"
514               end if
515            end if
516            dim_r=dimx
517         else
518            x_axis=new((/dimz,dimy/),double)
519            do q=0,dimz-1
520               x_axis(q,:) = f_att->k_y
521               x_axis = x_axis/norm_x
522            end do
523            if (norm_x .NE. 1.)then
524               res@tiXAxisString = "k~B~x~N~ ["+unit_x+"]"
525            else
526               if (norm_height .EQ. 1)then
527                  res@tiXAxisString = "k~B~x~N~ x z [1]"
528               else
529                  res@tiXAxisString = "k~B~x~N~ [1/m]"
530               end if
531            end if
532            dim_r=dimy
533         end if
534       
535         if (sort .EQ. "time")
536            res@xyLineColors = color
537            res@pmLegendDisplayMode     = "Always"
538            res@pmLegendSide            = "Top"
539            res@pmLegendParallelPosF    = 1.2
540            res@pmLegendOrthogonalPosF  = -1.0
541            res@pmLegendWidthF          = 0.12
542            res@pmLegendHeightF         = 0.04*(end_time_step-start_time_step+1)
543            do p=dimz-1,0,1
544               if (log_y .EQ. 1)then 
545                  do q=0,dimt-1
546                     do r=0,dim_r-1
547                        if (data(q,p,r) .EQ. 0)then
548                           st=p+start_time_step
549                           print(" ")
550                           print("'"+vNam(varn)+"("+st+","+q+","+r+")' is equal 0; Logarithmic scale for y-axis and height "+z(p)+" cannot be used")
551                           print(" ")
552                           res@trYLog = False
553                        end if
554                     end do
555                  end do
556               end if
557               res@gsnLeftString      = vNam(varn)
558               res@gsnRightString     = "Height = "+z(p)+"m"               
559               res@tiYAxisString      = "["+unit_y+"]"           
560               res@xyExplicitLegendLabels  = legend_label             
561               if (norm_height .EQ. 1)then
562                  data(:,p,:)=data(:,p,:)*doubletofloat(z(p))
563                  x_axis(p,:) = x_axis(p,:)*z(p)
564               end if
565               res@trXMinF = min(x_axis(p,:))
566               res@trXMaxF = max(x_axis(p,:))
567               plot(n)  = gsn_csm_xy(wks,x_axis(p,:),data(:,p,:),res)
568               n=n+1
569            end do
570         else
571            if (sort .EQ. "height")
572               do p=0,dimt-1           
573                  do q=0,dimz-1
574                     do r=0,dim_r-1
575                        if (data(p,q,r) .EQ. 0)then
576                           st=p+start_time_step
577                           print(" ")
578                           print("'"+vNam(varn)+"("+st+","+q+","+r+")' is equal 0; Logarithmic scale for y-axis and time "+legend_label(p)+" cannot be used")
579                           print(" ")
580                           res@trYLog = False
581                        end if
582                     end do
583                     if (norm_height .EQ. 1 .AND. p .EQ. 0)then
584                        data(p,q,:) = data(p,q,:)*doubletofloat(legend_label_z(q))
585                        x_axis(q,:) = x_axis(q,:)*doubletofloat(legend_label_z(q))
586                     end if
587                     max_y(q)=max(data(p,q,:))
588                     min_y(q)=min(data(p,q,:))
589                     min_x(q)=min(x_axis(q,:))
590                     max_x(q)=max(x_axis(q,:))                                   
591                  end do
592                  do q=0,dimz-1
593                     res@xyLineColor = color(q)
594                     if (dash .EQ. 1)then
595                        res@xyDashPattern = q
596                     end if
597                     if (q .EQ. 0)then
598                        res@tiYAxisString      = "["+unit_y+"]"                 
599                        res@gsnLeftString      = vNam(varn)
600                        res@gsnRightString     = "Time = "+legend_label(p)+"h"
601                        res@trXMinF = min(min_x)
602                        res@trXMaxF = max(max_x)
603                        res@trYMinF = min(min_y)
604                        res@trYMaxF = max(max_y)
605                       
606                        plot_h(q)  = gsn_csm_xy(wks,x_axis(q,:),data(p,q,:),res)
607                       
608                        lgres = True
609                        if (dash .EQ. 0)then
610                           lgres@lgMonoDashIndex = True
611                        else
612                           lgres@lgDashIndexes   = ispan(0,dimz-1,1)
613                        end if
614                        if (black .EQ. 1)then
615                           lgres@lgMonoLineColors = True
616                        else
617                           lgres@lgLineColors = color
618                        end if
619                        lgres@lgTitleString = "Height [m]" 
620                        lgres@lgLabelFont        = "helvetica"
621                        lgres@lgLabelFontHeightF     = font_size_legend
622                        lgres@lgTitleFontHeightF     = font_size     
623                        lgres@vpWidthF           = 0.12           
624                        lgres@vpHeightF          = font_size_legend/5*dimz           
625 
626                        lbid = gsn_create_legend(wks,dimz,legend_label_z,lgres)       
627
628                        amres = True
629                        amres@amParallelPosF   = 0.75               
630                        amres@amOrthogonalPosF = 0.15           
631                        annoid1 = gsn_add_annotation(plot_h(q),lbid,amres)
632                     else
633                        plot_h(q)  = gsn_csm_xy(wks,x_axis(q,:),data(p,q,:),res)
634                        overlay(plot_h(0),plot_h(q))
635                     end if
636                  end do             
637                  plot(n)=plot_h(0)
638                  n=n+1
639               end do
640            end if
641         end if
642         delete(data)
643         delete(temp)
644         delete(x_axis)
645         delete(min_x)
646         delete(max_x)
647         delete(min_y)
648         delete(max_y)
649         delete(plot_h)
650      end if
651   end do
652
653   if (n .EQ. 0) then
654      print(" ")
655      print("The variables 'var="+var+"' do not exist on your input file;")
656      print("be sure to have one comma berfore and after each variable")
657      print(" ")
658      exit
659   end if
660 
661   ; ***************************************************
662   ; merge plots onto one page
663   ; ***************************************************
664
665   resP                        = True
666   resP@txFont                 = "helvetica"
667   resP@txString               = f_att@title
668   resP@txFuncCode             = "~"
669   resP@txFontHeightF          = 0.015
670
671   if ((format_out .EQ. "eps" .OR. format_out .EQ. "epsi") .AND. n .gt. no_rows*no_columns) then
672      gsn_panel(wks,plot,(/n,1/),resP)
673      print(" ")
674      print("Outputs to .eps or .epsi have only one frame")
675      print(" ")
676   else   
677      do i = 0,n-1, no_rows*no_columns
678         if( (i+no_rows*no_columns) .gt. (n-1)) then
679            gsn_panel(wks,plot(i:n-1),(/no_rows,no_columns/),resP)
680         else
681           gsn_panel(wks,plot(i:i+no_rows*no_columns-1),(/no_rows,no_columns/),resP)
682         end if
683      end do
684   end if
685
686   if (format_out .EQ. "png" ) then
687     png_output = new((/no_frames/), string)
688     j = 0
689     do i=0, no_frames-1
690       j = i + 1
691       if (j .LE. 9) then
692         png_output(i) = file_out+".00000"+j+".png"
693       end if
694       if (j .GT. 9 .AND. j .LE. 99) then
695         png_output(i) = file_out+".0000"+j+".png"
696       end if
697       if (j .GT. 99 .AND. j .LE. 999) then
698         png_output(i) = file_out+".000"+j+".png"
699       end if
700       if (j .GT. 999) then
701         png_output(i) = file_out+".00"+j+".png"
702       end if
703
704       ;using imagemagick's convert for reducing the white
705       ;space around the plot
706       cmd = "convert -geometry 1000x1000 -density 300 -trim " +  \
707              png_output(i) + " " + png_output(i)
708       system(cmd)
709     end do
710
711     print(" ")
712     print("Output to: "+ png_output)
713     print(" ")
714   else
715     print(" ")
716     print("Output to: " + file_out +"."+ format_out)
717     print(" ")
718   end if
719   
720end
Note: See TracBrowser for help on using the repository browser.