Re: Can variable type(dimension)/function definition check be skipped in NCL?

From: 李嘉鹏 <lijpbasin_at_nyahnyahspammersnyahnyah>
Date: Thu Apr 26 2012 - 20:07:47 MDT

Thank you!
I checked the List related NCL function pages, and found a small typo in the "NewList" page:
http://www.ncl.ucar.edu/Document/Functions/Built-in/NewList.shtml
"lifo" (List In, Firs Out) should be "lifo" (Last In, First Out)

I have a small question for ListPush, must argument 1 (v) be a defined variable, or can also be some temporary value (like the result of a function or an expression). What would happen if I push a variable named "i" into a List, while List already contains a member with the same name? I'm thinking of the task in which I need to get a list of indices, and have to search it continuously through a loop (impossible to get them via a array function), and add it to the list each time a proper index is found.

Regards
Jiapeng

At 2012-04-26 22:51:13,"Wei Huang" <huangwei@ucar.edu> wrote:
Jiapeng,

For the list type, we recently revisited it and added/modified some of its functionalities.
For next release, we will have better document for list, I have a copy below for it.
(Some of the list functions were already in 6.0.0, but un-documented).

But I am not clear if this is what the meaning of your append a record to a list.
(As you may mean appending an element to an array variable, which is harder than
add an element to a list, as the array variables have fixed dimensions.)

Your example has shown that you can prototype a function/procedure first,
and implement it later. Which seems the function/procedure check is not a bad thing.
You may use the list variable to hide/avoid some of the variable's dimension/type check
by using list.

Hope this help.

Wei

huangwei@ucar.edu
VETS/CISL
National Center for Atmospheric Research
P.O. Box 3000 (1850 Table Mesa Dr.)
Boulder, CO 80307-3000 USA
(303) 497-8924

_____

List variables
List variables like stacks or queues which different kind of NCL elements can be stored into. And the elements can be in diferent types, and different sizes.

Create list variables
There are two ways to create a list variables. The first one is use the '[/.../]' (square braket) to create a list variable, as shown in the example:
 i = (/ (/1,2,3/), (/4,5,6/), (/7,8,9/) /)
 x = 5.0
 d = (/100000.d, 283457.23d/)
 s = "abcde"
 c = stringtochar("abcde")
 vl = [/i, x, d, c, s/]

The second one is treat list variable as a stack, and we push elements into the list, as shown below, where 'NewList' is a function to create a list.

 x = (/1,2,3,4/)
 x@attr = "integer array"
 y = (/6.,7.,8.,9./)
 y@attr = "float array"
 s = (/"one","two","three"/)
 s@attr = "string array"

 my_list = NewList("lifo")

 ListPush(my_list,x)
 ListPush(my_list,y)
 ListPush(my_list,s)

Note: When using ListPush to add elements to the list, the newly added elements is always at the the head of the list. Or new elements is always added at the front of the list.

Check list variables
There are is a function named ListCount to count the total elements in a list, and a funcion named ListIndex to check if an element is in the list, as shown below.

 cnt = ListCount(my_list)
;print(cnt)

 idx = ListIndex(my_list, x)
;print(idx)

Access list elements
There are two ways to access elements in a list as well. The first way is to access the element at certain position with square bracket, as below. This did not change the element (number) in the list.

One can use ListIndex to get the index of of a variable, and then use this index to access this element.

 e = my_list[1]

 print(e)

 idx = ListIndex(my_list, x)
 print(idx)

 nx = my_list[idx]

 print("ori x = " + x)
 print("new x = " + nx)

The second one is to access the element with a function named ListPop (for those familiar with stacks and queues, we also has an alias named ListDequeue). For instance:

 a = ListPop(my_list)

Note: When using ListPop to access element in the list, the element itself is removed from the list after ListPop. Also, there is a choice to Pop/Dequeue from head or tail of the list depends on the type of the list. There are two types of list: 1. FIFO (First-In, First-Out, which is functioning like a queue in computer science); 2. LIFO (Last-In, First-Out, which is just like a stack).

The list type can be checked using ListGetType, and changed with ListSetType.

 lt = ListGetType(my_list)
 ListSetType(my_list, "lifo")
 ListSetType(my_list, "fifo")

On Apr 24, 2012, at 8:37 PM, 李嘉鹏 wrote:

Dear NCL users:
NCL has the most restrict variable/function declaration check mechanism among all the languages I use. I understand that these checks are absolutely necessary to ensure the correctness and efficiency of the written programs.
However sometimes I found it rather cumbersome, for example there is no easy way to append a record to a existing list, even though function "array_append_record" is provided, if the original variable name needs to be kept.
If procedure B calls procedure A, the A must be defined before procedure B, although B can still call another version of A that is defined after, since any function/procedure can be redefined without difficulty, with the use of undef procedure, as the code below showed.
; Define procedure A
undef("A")
procedure A()
begin
  print("This is A before B")
end

; Define procedure B
undef("B")
procedure B()
begin
  A()
  print("This is B")
end

; Define procedure A
undef("A")
procedure A()
begin
  print("This is A after B")
end

begin
  B()
end

I guess function/procedure check can be carried out as late as the main code is being checked, and the variable dimension/type can be turned on/off by offering a flexible syntax check option. Is there any current solution I haven't seen or would it be implemented in future releases?

Thanks in advance!
Regards
Jiapeng

网易Lofter,专注兴趣,分享创作!_______________________________________________
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 Thu Apr 26 20:08:05 2012

This archive was generated by hypermail 2.1.8 : Mon Apr 30 2012 - 09:21:12 MDT