1
$\begingroup$

I am having a problem with construction of a loop that would load data from a bunch of files one by one, then produce a plot for each of the files, then save each of the plots as a separate image file, and then make a movie from the resulting bunch of images.

INTRODUCTION. Suppose there is one file with numerical data in a local directory. The file contains numerical data, for example a two-dimensional function "n1". One can import the data from the file using this command:

n1 = Import["1ExampleGraphix.h5", {"Datasets", "/n1"}];

Then one can plot the data using ListDensityPlot and save the resulting plot as a jpg file "1image.jpg":

Export["1image.jpg", ListDensityPlot[n1, ColorFunction -> "Rainbow"]]

PART I: LOADING, PLOTTING AND SAVING FILES ONE BY ONE. Now suppose there is a bunch of 100 files in a local directory:

1ExampleGraphix.h5

...

100ExampleGraphix.h5

and one ultimately wants to make a movie from this sequence.

The strategy is to load the files one by one, make a plot from each file, and save the corresponding image to a new file. One needs to generate a new filename according to the loop number. This loop is supposed to generate a sequence of files with images:

1image.jpg

...

100image.jpg

My problem is that I cannot load a file with the filename generated by ToString. Does anyone know how to do this correctly? I am trying the following loop:

For[i = 1, i < 2, 
 i++, {
  (*The next line creates a string with the filename to be 
  loaded*)

  loadfilename = ToString[i] "ExampleGraphix.h5";

  (*The next line loads the data for making a plot*)

  n1 = Import[loadfilename, {"Datasets", "/n1"}];

  (*The next line creates a string with the filename to be saved*)

  savefilename = ToString[i] "image.jpg";

  (*The next line saves the plotted data as a jpg image*)

  Export[savefilename, 
   ListDensityPlot[argPsi1, ColorFunction -> "Rainbow"]]}] 

The error message is:

Import::chtype: "\!\(\*
StyleBox[\"\\\"First argument \\\"\", \"MT\"]\)\!\(\* StyleBox[ RowBox[{\"\\\"1\\\"\", \" \", \"\\\"ExampleGraphix.h5\\\"\"}], \"MT\"]\)\!\(\* StyleBox[\"\"\", \"MT\"]\) is not a valid file, directory, or URL specification.

PART II: CREATING A MOVIE FROM THE SEQUENCE OF IMAGES. The second question is: How to make sure that the files are arranged in frames in the correct order, when the movie is created?

I am trying the following commands:

files = FileNames["*.jpg"];
images = Map[Import, files];
Export["video.avi", images]

The first line picks all .jpg files from the directory, but it is unclear how are the frames sorted in the resulting video file?

Any help would be greatly appreciated.

With best wishes, Dmitry

$\endgroup$
  • $\begingroup$ Try Do[With[{s = IntegerString[k]}, Export[s <> "image.jpg", ListDensityPlot[Import[s <> "ExampleGraphix.h5", {"Datasets", "/n1"}], ColorFunction -> "Rainbow"]]], {k, 1, 100}] $\endgroup$ – J. M. is away Sep 28 '18 at 17:26
  • $\begingroup$ Thank you very much for the answer. Your code has solved my problem! $\endgroup$ – Dmitry Sep 28 '18 at 19:35
1
$\begingroup$

You may use StringTemplate for the file names. You can also take advantage of parallel processing with ParallelMap instead of the procedural For.

LaunchKernels[];
ParallelEvaluate[SetDirectory[$UserDocumentsDirectory]];
ParallelMap[
 Export[
   StringTemplate["``image.jpg"]@#,
   ListDensityPlot[
    Import[
     StringTemplate["``ExampleGraphix.h5"]@#, {"Datasets", "/n1"}],
    ColorFunction -> "Rainbow"
    ]
   ] &,
 Range@100]

Hope this helps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.