User Tools

Site Tools


howtos:toolcoding:good_practices

This is an old revision of the document!


Good TOOL Coding Practices

Create time sequences dynamically

There are many ways to create a time sequence dimension in an object:

  1. myObj[t] = create (; dim=SEQ;time:1990:2000:1;year)
  2. myObj[t] = create (; dimFrom1=myTimeObj[t]), where dimension t in myTimeObj is the time sequence 1990 to 2000
  3. myObj[t] = create (; dim=SEQ;time:$startYear:$endYear:1;year), where $startYear=1990 and $endYear=2000

Method 1 causes problems if you later update the time sequence in the model. If the time sequence changes, all code with hard-coded references the years in that sequence need to be updated in the procedure code, model views, and any TOOL scripts. For larger models, this can be cumbersome and error-prone.

Method 2 causes problems if you later change the dimensions in myTimeObj. For example, if you add a new dimension so to myTimeObj so that it is now myTimeObj[x,t], myObj in the above code would now be getting its dimension from x, which is now the first dimension, instead of t. This error would most likely be detected during dimensional analysis, but if it somehow were to pass dimensional analysis then the error would be much harder to detect. In either case, the code would have to be updated in the model procedures, model views, and TOOL scripts.

Method 3 is the recommended method for creating time sequences in an object. Use getobjinfo to derive the values for $startYear and $endYear as shown below

integer $startYear, $endYear
getobjinfo (myTimeObj[t], $startYear; info=sequenceStart, dim=time)
getobjinfo (myTimeObj[t], $endYear; info=sequenceEnd, dim=time)
myObj[t] = create (; dim=SEQ;time:$startYear:$endYear:1;year)

With this method changes to the time sequence will be automatically inherited because the sequence start and end years are derived dynamically. Also, this method will still work if new dimensions are added to myTimeObj because the getobjinfo statement explicitly looks for the dimension time regardless of whether it is the first dimension.

howtos/toolcoding/good_practices.1288970217.txt.gz · Last modified: 2010/11/05 15:16 by deryn.crockett