Re: Variable number of arguments in NCL functions?

From: Dennis Shea (shea AT XXXXXX)
Date: Mon Feb 17 2003 - 13:38:18 MST

  • Next message: Maurice McHugh: "problems reading gsn_*.ncl with AIX"

    >Is it possible to write an NCL function that can take a variable number of
    >arguments?
    >
    >I'm trying to write a function that can take different numbers of arguments
    >depending on circumstances. I could always pass dummy arguments, I suppose,
    >but would prefer not to.
    >

    Yes.

    For example, that is how all the gsn* graphical interfaces
    accept varying number of plot options [resources].

    The process in NCL is to pass arguments as attributes of a variable.
    Most frequently, users create a variable of type logical [however,
    it can be any type]. One can test for specific optional arguments
    by using the "isatt" function.

        optArg = True ; option arguments will be used
        optArg@scale_factor = 0.01
        optArg@add_offset = 123.
        optArg@weights = (/ 0.25, 0.50, 0.25 /)
                                            ; let x be a multi-dimensional array
        optArg@xArray = x ; NCL allows multidimensional atts

    --------
        function/procedure foo (arg1, arg2, ..., optArg:logical)
        begin
           if (optArg) then
               ...
           end if
           
           if (optArg .and isatt(optArg,"scale_factor")) then
               dumy = arg1*optArg@scale_factor ; mult by scale factor
             if (isatt(optArg,"add_offset")) then
               dumy = dumy + optArg@add_offset ; add offset
             end if
           end if
           
           :
           
           if (optArg .and isatt(optArg,"weights")) then
               zRunAve = wgt_runave (z, optArg@weights , 0)
           end if
           
           if (optArg .and isatt(optArg,"xArray")) then
               xNew = optArg@xArray ; transfer to local variable
           end if
          :
         
        end
    ------------

    Also, remember that the arguments arg1, arg2, ... are data objects
    that may or may not have attributes associated with them.
    These may also be tested/used.

    Regards,
    Dennis Shea

    _______________________________________________
    ncl-talk mailing list
    ncl-talk AT ucar.edu
    http://mailman.ucar.edu/mailman/listinfo/ncl-talk



    This archive was generated by hypermail 2b29 : Mon Feb 17 2003 - 17:23:53 MST