[ncl-talk] Return values in user defined functions

Dennis Shea shea at ucar.edu
Tue Mar 21 16:11:29 MDT 2017


Use NCL's list variable. The contents can be variables of different types,
shapes and sizes

undef("ansari")
function ansari(x,y,z,......)
local co, o3,no2, so2, pm25, pm10
begin
   co   = ...
   o3  =
        :
   pm10 =

   co at long_nme = ...
   o3 at long_name = ...

   co at units = ...
   o3 at units = ...

   copy_VarCoords(x, co)
          :

   return([/co, o3,no2, so2, pm25, pm10/])  ; return as list
end
;========
;  MAIN
;========

  var6 = ansari(...)    ; var6 will contain

; you can access the list variable elements
; list uses [...] to access elements

  print( var6[0] )        ; co

  print( var6[5] )       ; pm10

=========
For clarity, my preference is to explicitly extract each list element into
a separate variable

     var6 = ansari(...)
     co    = var6[0]
     o3    = var6[1]

+++++++++++++++++

If the co, o3, ...pm10 are all the same shape, size and type you could
place them into a single variable.
    nvar = 6
    VAR = new ( 9/nvar,.../), typeof(co), getVarFillValue(x))

     VAR(0,:,,,) = co_calculation
     VAR(1,...)  = o3

      return(VAR)

The issues is thateach element may have different units, coordinates, etc

Good Luck

https://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclVariables.shtml




On Tue, Mar 21, 2017 at 12:14 PM, Tabish Ansari <tabishumaransari at gmail.com>
wrote:

> Hi
>
> I'm trying to write a user defined function which calculates 6 arrays,
> say, co, o3,no2, so2, pm25, pm10. I want to call this function inside a
> loop which supplies different station-names each time - so that the
> function computes these variables for each station. But is it possible to
> return 6 variables through one function?
>
> How would the format be where this function is called, and also in the
> return statement?
> I would really appreciate an example for this.
>
> Thanks
>
> Tabish
>
> Tabish U Ansari
> PhD student, Lancaster Environment Center
> Lancaster Univeristy
> Bailrigg, Lancaster,
> LA1 4YW, United Kingdom
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170321/2dc1a215/attachment.html 


More information about the ncl-talk mailing list