Save data in arbitrary directory

Do you really need to do so?

First, consider whether you really need to save data in a different directory. Remember that you can run many simulation with one single script just using a different simulation name, like:

s1 = Simulation('one')
s2 = Simulation('two')

When you save the data for simulation one you get files like one_dat.h5 and one_dat.ndt, while when dealing with simulation two you get two_dat.h5 and two_dat.nd5. There is no interference between the two simulation, which may be the main reason why you may be reading this part of the Nsim documentation.

Here is how you do it

When you run a simulation script which saves data from a simulation, you get that the files are saved in the current working directory.
In order to change this and save data into a directory called ./mydir/ you should start your script in the following way:

import nmag
import nsim.features
fts = nsim.features.Features()
fts.set('etc', 'savedir', './mydir/')

Alternatively, you can change the current working directory at the beginning of the file with ordinary Python code:

import os
initial_dir = os.path.abspath(os.path.curdir)

If the directory you want to write to does not exist then (in both the two example) you may have to create it first, with something like:

the_dir = './mydir'
import os
if not os.path.exists(the_dir):

Also available in: HTML TXT