1

I have a file in IDL, I import it to Python using readsav from scipy, I change a parameter in the file and I want to export / save it back to the original format, IDL readable.

This is how I import it:

from scipy.io.idl import readsav
input = readsav('Original_file.inp')
mgalloy
  • 2,356
  • 1
  • 12
  • 10
T. Silva
  • 113
  • 5

1 Answers1

0

I haven't tested any of this, but here are a few options to try:

Python-to-IDL/IDL-to-Python Bridge

The Python to IDL bridge provides a way to run IDL routines within python. You could try the following

from idlpy import *
from scipy.io.idl import readsav
input = readsav('Original_file.inp')
**change parameter**
IDL.run("SAVE, /VARIABLES, FILENAME = 'New_file.sav'")

There is also an IDL to Python bridge, which might allow you to perform your desired Python operation within IDL, and skip all the loading and saving of files...

Read/Write JSON

It looks like readsav() just returns a dictionary of the contents of the IDL save file. I'm not sure of the contents of your file, so I don't know if this would work, but perhaps you could just write it as a JSON string,

import json
from scipy.io.idl import readsav
input = readsav('Original_file.inp')
**change parameter**
with open('New_file.txt', 'w') as outfile:  
    json.dump(modified_input, outfile)

and then read it back into IDL with JSON_PARSE() (documentation here).

Write your own hack

If all else fails, you could look at Craig Markwardt's Unofficial Format Specification of the IDL "SAVE" File, and write some custom code to write an IDL save file directly from Python. If nothing else, it would be an interesting exercise.

Steve G
  • 182
  • 9