user2137944
user2137944

Reputation: 73

Multiple procedures in IDL program

I've written a procedure in IDL which performs some calculations on data and outputs an array of values. The calculations take about 2 minutes to run.
I need to then perform analysis on these results, and ideally I would like not to have to perform the initial calculations each time I want to perform some different analysis.

Is the best way to achieve this to save the output from the calculation to a data file and then read this in from a different program? Or is there a less cumbersome way to go about this?

Thanks in advance for any help

Upvotes: 1

Views: 701

Answers (3)

EL_DON
EL_DON

Reputation: 1506

Saving is a good way to do it, but if you run in the same session and your second program won't mess up the data from the first one, you can just call one and then pass the result to the second one.

pro do_calculations,result1,result2,result3
    result1=1
    result2=1.
    result3=result1/result2
    return
end

pro use_calculations,result1,result2,result3,result4
    result4=result1-result2+result3
    return
end

Then

IDL> do_calculations,result1,result2,result3
IDL> use_calculations,result1,result2,result3,result4

If you edit use_calculations, you can go again by:

IDL> use_calculations,result1,result2,result3,result4

Because the earlier results will stay in memory unless use_calculations does something bad to them.

You could also set up the second procedure to check to see if it has valid results from the first one and call it as needed.

Upvotes: 0

Pi Marillion
Pi Marillion

Reputation: 4674

Yes, saving to a file is the easiest way to save the results from your first program for later use in the second (assuming you quit IDL between the two). There are may ways to save the data, depending on it's type and your preferences.

Easiest Way:

An IDL .sav file created by the SAVE command can store any kind of data, IDL variables, and even the whole state of your IDL session. Unfortunately, it only works for IDL (no other languages), and it can need to be re-generated if you upgrade IDL version. You read these files with RESTORE, which even remembers the names of the variables.

my_variable = 'Some data here.'

SAVE, my_variable, FILENAME='myfile.sav'  ; save variable(s)

... IDL opened and closed here ...

RESTORE, 'myfile.sav'                     ; read variable(s) from file

print, my_variable
Some data here.

Most Portable Way:

For simple tabular data, CSV has the advantage of being highly portable and human readable. However, it's also slow, since numbers are stored in ASCII. Use WRITE_CSV to write, and READ_CSV to read.

Most Portable Binary Formats:

For complex data that needs to be read by multiple languages, consider the HDF5 or NetCDF libraries. Both of these are binary formats that can store most types of IDL-supported data. Note that NetCDF is actually an easier-to-use subset of HDF5.

Simplest Binary Format:

Another option for tabular data is a simple binary dump. Use WRITEU to write to a normal file opened for writing. Use READU to read from a normal file open for reading.

Upvotes: 3

Chris Torrence
Chris Torrence

Reputation: 452

Assuming that your data calculations will only change very rarely, then, yes, your best solution is to just save the calculations to an output file, and then read them back into your analysis program. You don't say what kind of data this is, so it's hard to give a more specific answer. Assuming that you have a two-dimensional array of data, you could just write the results as a "flat" binary file:

pro perform_calculations
  ...
  ; assume mydata is a float array of dimensions [m,n]
  openw, 1, 'results.dat'
  writeu, 1, mydata
  close, 1
end

Then, in either the same file or preferably a different .pro file:

pro perform_analysis
  mydata = fltarr(m, n)
  openr, 1, 'results.dat'
  readu, 1, mydata
  close, 1
  ...
end

Hope this helps.

Upvotes: 0

Related Questions