Returning multiple variables from a function

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Wed Jun 19 2013 - 07:04:25 MDT

This is in response to an offline question. However, the response
is generally applicable.
===========================================

A variable can return additional information such asnanother variable
like you have below. However, that additional variable can *not* have
*any* meta data associated with it.
-------
There *is* a way to return multiple variables with meta data
from a function. It is similar to (say) matlab ... not quite as nice.

   [a,b,c] = foo(...) ; matlab; a, b, c can be used directly

---
In NCL, return a variable of type 'list' which contains a,b,c.
See:
http://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclVariables.shtml
Search for:   List variables
This description is rather cryptic. Here is a simple example.
   undef("foo")
   function foo(...)
   begin
     a = ...   ; may have meta data
     b = ...   ;  "   "     "    "
     c = ...   ;  "   "     "    "
     return( [/ a,b,c /] )  ; create a variable of
                            ; type list via [/ ... /]
   end
   result = foo(...)        ; result is a variable of type list
You can access individual elements of 'result'
   print(result[2])  ; print values of the returned 'c' variable
However, this is cumbersome and not particularly clear.
I suggest explicitly extracting the variables upon return and then
deleting the list variable. This is what I mean by 'not as nice
as Matlab'
   aa = result[0]   ; includes meta data
   bb = result[1]
   cc = result[2]
   delete(result)   ; no longer needed
---
In your example
   return( [/ tp, pdf /] )
On 6/19/13 2:30 AM, Marston Johnston wrote:
> I think I now understand my mistake with both the area-weighted average and
> pdf. My I ask you another question relating to this topic:
>
> If I want to return more than one variable from a function, can this be
> done by attaching them as attributes to a main variable? By this I mean how
> do I have a variable with an attribute which itself has attributes?
> It would something like this, but I know this doesn't work:
>
> function Illustrative_example(...)
> begin
> tp = True
> tp@data = new(...)
> pdf = True
> pdf@_FillValue = -9999.0
> pdf@pdf = pdfx(...)
> tp@pdf = pdf
>
> return(tp)
> end
>
> Sincerely,
> /Marston
>
>
> On Mon, Jun 17, 2013 at 4:55 PM, Dennis Shea <shea@ucar.edu> wrote:
>
>> This is confusing.
>>
>> It indicates below that 'tp' has attributes lat2d, lon2d and lat
>>
>> tp@lat
>> tp@lat2d
>> tp@lon2d
>>
>> On the surface of it I'd say there is something incorrect with
>> what you are doing. However, you have not sent enough information.
>>
>> The lat2d/lon2d imply a curvilinear grid with
>> 2-dimensional lat/lon arrays.
>>
>> Then you use
>>
>>
>>   latwgt = latRegWgt(tp@lat, "float", 0)
>>
>> which has a one dimensional array of latitudes.
>>
>> Something is inconsistent and, likely, wrong here.
>>
>>
>> ==============================**===================
>> The following might be more appropriate.
>> ==============================**====================
>>
>> undef("Atlantic")
>> functionn Atlantic(tp:numeric,nbin:**integer)
>>
>> begin
>>
>>    print("Setuping Atlantic region arrays....")
>>
>>    latMin  = -7
>>    latMax  = 25
>>    lonMin  = 310
>>    lonMax  = 350
>>
>>    tp = mask (tp, conform(tp, (tp@lat2d.ge.latMin .and. \
>>                                tp@lat2d.le.latMax .and. \
>>                                tp@lon2d.ge.lonMin .and. \
>>                                tp@lon2d.le.lonMax), (/1,2/)), True)
>>    printVarSummary(tp)
>>
>>
>>    rad    = 0.01745329
>>    latwgt = cos(lat2d*rad)     ; (:,:)
>>
>> ; http://www.ncl.ucar.edu/**Document/Functions/Built-in/**
>> wgt_areaave2.shtml<http://www.ncl.ucar.edu/Document/Functions/Built-in/wgt_areaave2.shtml>
>>    areaAve = wgt_areaave2(tp, latwgt, 1)
>>    print("Area Average "+areaAve)
>>
>>    pdf = pdfx(atlantic,nbin,False)
>> ;;pdf = where(pdf.ne.0,pdf,pdf@_**FillValue)   ; ???
>>
>>    return(pdf)
>> end
>>
[SNIP]
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Wed Jun 19 07:04:35 2013

This archive was generated by hypermail 2.1.8 : Mon Jun 24 2013 - 11:46:47 MDT