Skip to main content

IDL programming: control variables in IDL across routines

IDL, unlike C++, has its own approach and principle when handling variables across routines. These features are often very different with the variable scope within C++.
I will try to answer several key questions and prove them using simple demos.
Question 1: Will the value of a variable be changed after it’s passed into a function and changed inside?
Demo:
;;.....................................................................................
PRO variable_change
a=1.0
b=2.0
PRINT,'Before the operation, a is ',a
c=plus(a,b)
PRINT,'After the operation, a is ',a
END
FUNCTION plus,a,b
a=a+b
RETURN,a
END
And the IDL Console output:
Before the operation, a is 1.00000
After the operation, a is 3.00000
;;.....................................................................................
Conclusion:
If the value of a variable has been changed inside a routine, it will remains changed outside. Question 2: What if this variable is not even passed into any routine, but the routine has a variable which has the same name?
Demo:
;;.....................................................................................
FUNCTION plus, m, n
a= m+n
print, a
RETURN, a
END
PRO variable_test
a = 1
PRINT,a
b =2
c= 3
d= plus(b,c)
PRINT,d
PRINT,a
END
a is 1 a is 5 d is 5 a is 1
;;.....................................................................................
Conclusion: If it's not passed, it won't change even other routines have the same variable name.
Tips:
Whenever you have many variables which may be passed to other routines, try not to use the same variable name as much as possible. A simple solution is to get a copy of the variable before changing it.
Question 3: Then how to define a global variable?Answer: IDL provides common and Defsysv to handle global variables.
Demo:
;;.....................................................................................
PRO demo
COMMON exponent,m
m=2.0
result=QROMO('integration', 1.0, 2.0)
PRINT,'The integration result is', result
END
FUNCTION integration,x
;To integrate the expression: y=x^m+1
;However variable m is uncertain.
COMMON exponent,m
result=x^m+1
RETURN, result
END
And the IDL Console output:
The integration result is 3.33333
;;.....................................................................................
Discussion:
By reviewing the IDL Helper, as we could see, the COMMON defines a shared block to store the variables. And there are more than one way to refer the shared block. In the above demo, you can also simply use: COMMON exponent in the integration function without specify the m.
Besides, if a shared block is defined, it can’t be changed in number and type in the other functions. Also, the sequence of all variables could be exactly the same as you refer.
Tips:
Though the COMMON is a traditional way to share variables, it could also cause conflicts if not configured well.
In the Defsysv case:
The IDL provides system variable way to store global variables as well. The defsysv procedure  could create a new system variable with initial value if configured.
Demo:
;;.....................................................................................
PRO demo
DEFSYSV, '!m', 2.0
result=QROMO('integration', 1.0, 2.0)
PRINT,'The integration result is', result
END
FUNCTION integration,x
;To integrate the expression: y=x^m+1
;However variable m is uncertain.
result=x^!m+1
RETURN, result
END
The console output as:
The integration result is 3.33333
;;.....................................................................................
So it is the same with the common case. And the only thing I did was ‘DEFSYSV, '!m', 2.0  ’. It seems that this approach is somewhat easier than ‘common’ way.
However, once defined , the system variable can not be destroyed until the whole procedure ends.
What if the variable we want to configure is a vector?
The demo will indicate that the ‘defsysv’ won’t allow it. But ‘common’ could!
Of course, other approaches like uvalue and sav file could also provide similar function.
Especially the uvalue method is frequently used along with defsysv.
Hope it can help!
I will present a specific example to solve a complex nonlinear equation using the above method in other posts.

Comments

Popular posts from this blog

Spatial datasets operations: mask raster using region of interest

Climate change related studies usually involve spatial datasets extraction from a larger domain.
In this article, I will briefly discuss some potential issues and solutions.

In the most common scenario, we need to extract a raster file using a polygon based shapefile. And I will focus as an example.

In a typical desktop application such as ArcMap or ENVI, this is usually done with a tool called clip or extract using mask or ROI.

Before any analysis can be done, it is the best practice to project all datasets into the same projection.

If you are lucky enough, you may find that the polygon you will use actually matches up with the raster grid perfectly. But it rarely happens unless you created the shapefile using "fishnet" or other approaches.

What if luck is not with you? The algorithm within these tool usually will make the best estimate of the value based on the location. The nearest re-sample, but not limited to, will be used to calculate the value. But what about the outp…

Numerical simulation: ode/pde solver and spin-up

For Earth Science model development, I inevitably have to deal with ODE and PDE equations. I also have come across some discussion related to this topic, i.e.,

https://www.researchgate.net/post/What_does_one_mean_by_Model_Spin_Up_Time

In an attempt to answer this question, as well as redefine the problem I am dealing with, I decided to organize some materials to illustrate our current state on this topic.

Models are essentially equations. In Earth Science, these equations are usually ODE or PDE. So I want to discuss this from a mathematical perspective.

Ideally, we want to solve these ODE/PDE with initial condition (IC) and boundary condition (BC) using various numerical methods.
https://en.wikipedia.org/wiki/Initial_value_problem
https://en.wikipedia.org/wiki/Boundary_value_problem

Because of the nature of geology, everything is similar to its neighbors. So we can construct a system of equations which may have multiple equation for each single grid cell. Now we have an array of equation…

Lessons I have learnt during E3SM development

I have been involved with the E3SM development since I joined PNNL as a postdoc. Over the course of time, I have learnt a lot from the E3SM model. I also found many issues within the model, which reflects lots of similar struggles in the lifespan of software engineering.

Here I list a few major ones that we all dislike but they are around in almost every project we have worked on.

Excessive usage of existing framework even it is not meant to Working in a large project means that you should NOT re-invent the wheels if they are already there. But more often, developers tend to use existing data types and functions even when they were not designed to do so. The reason is simple: it is easier to use existing ones than to create new ones. For example, in E3SM, there was not a data type to transfer data between river and land. Instead, developers use the data type designed for atmosphere and land to do the job. While it is ok to do so, it added unnecessary confusion for future development a…