Re: Problem Returning graphic object from user defined function

From: Adam Phillips <asphilli_at_nyahnyahspammersnyahnyah>
Date: Fri Sep 13 2013 - 15:55:02 MDT

Hi Mohamed,
(Everyone: this initially went offline.)

Thank you for sending on a simple example (offline). I think the issue
is that you cannot pass the plot back outside your plotting function
when you create the workstation within that function. If you want to
bring the plot back outside the function the workstation must be created
outside and passed into the function. Here are modified versions of
call_plot.ncl and make_plot.ncl that show this process.
Adam

call_plot.ncl
;=======================================================================================
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "make_plot.ncl"
;=======================================================================================
begin
x = fspan(0,1,10)
y = x^2
wks = gsn_open_wks("png", "tmp_figure" )
figure = make_plot(wks,x,y)
draw(figure)
frame(wks)

draw(figure)
frame(wks)
end
;============
make_plot.ncl
;=======================================================================================
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/diagnostics_cam.ncl"
;=======================================================================================
undef("make_plot")
function make_plot(wks:graphic, data_x:numeric,data_y:numeric)
begin

res = True
res@gsnDraw = False
res@gsnFrame = False
plot=gsn_csm_xy (wks,data_x,data_y,res)

print(plot)
return(plot)
end

On 9/13/13 1:50 AM, Mohamed Abdel Kader wrote:
> Dear All,
> I would like to make a figure of different scatter plots super-imposed
> a global map. I have a user defined function that generates the
> scatter plots and I return the scatter plot graphic object. The
> returned graphic object has missing value.
> I have to do in that way since the scatter plot is highly customized
> and it's being called many times from different scripts. So, I would
> like to keep one scatter plot script. Could you please help me on
> solving this problem.
> I attached parts from the script.
> P.S.: The scatter function works fine as standalone
>
> Thanks a lot and let me know if you need more information.
>
> *_========================================================================================================================_*
> *_Scatter plot function:_*
> function
> Plot_Scatter_function(x_title:string,y_title:string,data_x:numeric,data_y:numeric,f_name:string,data_set_string:string,
> ps_op:logical, plot:graphic)
> local plot1, plot2
> begin
> .
> .
> .
> . "many commands"
> .
> .
> wks = gsn_open_wks("eps", f_name )
>
> plot1 = gsn_csm_xy (wks,ndtooned(data_x),ndtooned(data_y),res)
> plot2 = gsn_csm_xy (wks,(/res@trXMinF
> <mailto:res@trXMinF>,res@trXMaxF <mailto:res@trXMaxF>/),(/res@trYMinF
> <mailto:res@trYMinF>,res@trYMaxF <mailto:res@trYMaxF>/),res2)
>
> overlay(plot1,plot2)
> draw(plot1)
> frame(wks)
> print(plot1)
> plot=plot1
> *return (plot)*
> end
>
> the print out from scatter function
> Variable: plot
> Type: obj
> Total Size: 4 bytes
> 1 values
> Number of Dimensions: 1
> Dimensions and sizes: [1]
> Coordinates:
> Number Of Attributes: 2
> dataspec : /EMAC_map_scatter_____tmp_data xyDataSpecClass 89
> data : /EMAC_map_scatter_____tmp_data coordArraysClass 67
> (0) /EMAC_map_scatter_____tmp_xy xyPlotClass 88
> *_========================================================================================================================_*
>
> _*Parent script:*_
> .
> .
> .
> .
> dum_poly = new(NREG,graphic)
>
> do n=0,NREG-1
> dum_poly(n)=
> *Plot_Scatter_function*(x_title,y_title,data_xx,data_yy,f_name+"_____tmp",data_set_string,
> ps_op,dum_poly(n))
> ann = gsn_add_annotation (map,dum_poly(n),amres)
> end do
> .
> .
> .
> .
> .
> .
> end
>
> _the message error_
>
> warning:NhlAddAnnotation: bad HLU id passed in, ignoring it
> fatal:_NclAddHLURef: internal error: ncl object 647412112 does not
> exist for vname anno_id
> fatal:_NclAddHLURef: internal error: ncl object 647412112 does not
> exist for vname ann
>
> *_The print out of dum_poly(n): from the Parent script._*
> Variable: dum_poly
> Type: obj
> Total Size: 4 bytes
> 1 values
> Number of Dimensions: 1
> Dimensions and sizes: [1]
> Coordinates:
> Number Of Attributes: 3
> data : -1
> dataspec : -1
> _FillValue : -1
> (0) -1
> *_========================================================================================================================_*
> *_
> _*
> *_
> _*
>
>
> Best Regards
> Mohamed M. Abdel Kader
> Atmosphere and Climate Modelling Group
> Research Assistant/PhD Candidate
> Environmental & Atmospheric Sciences
> The Cyprus Institute, Cyprus
> 20 Constantinou Kavafi Street
> P.O. Box: 27456
> Nicosia 2121, Cyprus
> Tel. +357 222 08679
> Fax. +357 222 08625
> Mobile: +357 992 62108
> e-mails: m.abdelkader@cyi.ac.cy <mailto:m.abdelkader@cyi.ac.cy>
> mmkader@gmail.com <mailto:mmkader@gmail.com>
> Skype: mmkader
>
>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Sep 13 15:55:12 2013

This archive was generated by hypermail 2.1.8 : Mon Sep 16 2013 - 13:43:52 MDT