getting (too?) fancy with attributes

From: Jonathan Vigh <vigh_at_nyahnyahspammersnyahnyah>
Date: Fri, 28 Nov 2008 22:13:33 +0000

Greetings NCL'ers,
     I'm writing a large code with many subroutines, and each subroutine
has many variables to be passed. These are all single valued numbers or
strings, so I'm not to concerned with the ability to attach attributes
to each variable. So I decided to attach the values to one logical
container variable and just pass that instead, kind of like an
object-oriented data structure:

; in main
CONTAINER = True
CONTAINER_at_varA = new(1,"integer")
CONTAINER_at_varB = new(1,"string")
... etc.

Then when I want to pass everything to a procedure which will populate
the values or do some calculations, I just pass container:
read_data(CONTAINER)

I was trying to think if there were any drawbacks over the traditional
approach of explicitly passing the variables. It seems that there is.
I'm declaring the attached attributes as a certain type using the 'new'
function in the main program to ensure that I'll know about any type
mismatching in the procedure. But if in the procedure I assign the
variable to its fill value (in the case that I want to make sure that
the procedure starts off with a clean slate). If the attribute is
reassigned to the fill value in the procedure, the ismissing function
returns an unexpected result of False, even though the value is fill value.

I've attached a stand-alone script which you can easily run to replicate
this behavior.

So I was wondering if there is an easy way around this issue, and
whether my approach is a) valid and b) good programming practice.
 Are there any other drawbacks to this approach (besides not being able
to attach attributes to the attribute)? I guess this is kind of
equivalent to declaring all the variables globally - maybe that is bad
practice? Is there a better way to pass large numbers of variables
between functions? Does NCL have a way to build a data structure and
pass that all at once?

Thanks in advance,
     Jonathan

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Test the feasibility of passing arguments to functions as
; attributes rather than variables.
;*****************************************************************
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/shea_util.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
;********************************************************

; global parameters
integer_FillValue = -999

procedure do_stuff(CONTAINER)
local stuff

begin

  CONTAINER_at_variableB = integer_FillValue

  print("In procedure . . .")
  print("variableA = "+CONTAINER_at_variableA+" ismissing? "+ismissing(CONTAINER_at_variableA))
  print("variableB = "+CONTAINER_at_variableB+" ismissing? "+ismissing(CONTAINER_at_variableB))
  print(" ")

  CONTAINER_at_variableB = -3.4
  
  
  
  
  CONTAINER_at_variableA = -3.4

end

; main program
begin
  
  CONTAINER = True
  CONTAINER_at_variableA = new(1,"integer",integer_FillValue)
  CONTAINER_at_variableB = new(1,"integer",integer_FillValue)

  print("In main program . . .")
  print("variableA = "+CONTAINER_at_variableA+" ismissing? "+ismissing(CONTAINER_at_variableA))
  print("variableB = "+CONTAINER_at_variableB+" ismissing? "+ismissing(CONTAINER_at_variableB))
  print(" ")
 
  do_stuff(CONTAINER)
 
end

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Nov 28 2008 - 15:13:33 MST

This archive was generated by hypermail 2.2.0 : Sun Nov 30 2008 - 08:25:42 MST