[Paraview] How to execute the Plot Over Line with a Python script and export to CSV

pat marion pat.marion at kitware.com
Mon Oct 3 10:10:14 EDT 2011


Thanks for sharing, Luca!

Pat

On Mon, Oct 3, 2011 at 12:08 AM, Luca Giannelli <bilogo at gmail.com> wrote:

> Howdy.
>
> I have been looking through the Internet to find a solution for how to have
> the Plot over line
> executed automatically through a Python script. I was able to find some
> sources here on the
> Paraview mailing list but nobody reporting a fully functional script (at
> list for my case).
> So I will share what I wrote to help those people that will surely come up
> with the same problems
> I had. What does the script do:
>
> - execute a PlotOverLine filter
> - export the results in a CSV file
> - execute a subsequent second filter directly after the first
> - export this to a separate file
>
> I know it may sound an easy job and I know it is not by any means a
> "perfect" solution.... but
> the code is heavily commented so I think it can surely work well as the
> base for further
> development for other people.
>
> Good work.
>
> Luca
>
>
> -----------------------------------------------------------------------------
>
> -----------------------------------------------------------------------------
>
> # First of all, I created this script because I needed to execute the
> probing with
> # the Plot Over Line filter on 9 points for each time step in a long
> simulation with
> # the OpenFOAM CFD tool; that kind of thing that you cannot do by hand.
> # This is such a dumb script that you can of course modify it as you like
> to suit your needs!
> # That's Open Source...!
> #
> # @@ IMPORTANT NOTICE @@
> #
> # If you are running Paraview using the binaries provided by the Ubuntu
> repos, it is
> # very likely that you don't have full python support due to the lack of
> some functions
> # here and there in the provided installation.
> # THUS, THIS SCRIPT WON'T RUN!!
> # The obvious work around is to uninstall the Ubuntu version and build from
> source the
> # last stable. This was tested using Ubuntu 11.04 and Paraview 3.8.0 32bit.
> #
> # Luca Giannelli
>
>
> import os
> try: paraview.simple
> except: from paraview.simple import *
> paraview.simple._DisableFirstRenderCameraReset()
>
> # If you want to create a set of following operations and save the
> # results in different locations, you can manage the folders with
> # this code section.
> # Just write the path to your folder without forgetting the quotes "".
>
> if not os.path.exists("YOUR_WORK_DIR_GOES_HERE"):
>   os.makedirs("YOUR_WORK_DIR_GOES_HERE")
>
> # you can add all the folders that you like in this way by copying this
> # check code and inputting the new names.
>
> #
> -------------------------------------------------------------------------------
> # Moving to the core of the script instuctions for the execution of
> PlotOverLine
> #
> -------------------------------------------------------------------------------
>
> # Let's first identify the source to which we want to execute the P-O-L on:
> # This "fotobioreattore_foam" is just the name that I used and you can
> change
> # it to whatever you like. In the same way, you must provide as a variable
> in
> # the FindSource, the name of the first module in the stack of your very
> own
> # case for the script to work. For example:
> #
> # my_case = FindSource("THE_NAME_OF_YOUR_CASE_GOES_HERE")
>  fotobioreattore_foam = FindSource("fotobioreattore.foam")
>
> # Now set the source you just defined, as the active one so that the P-O-L
> filter
> # will be executed on that without need of any other definition. Example:
> #
> # SetActiveSource(my_case)
>
> SetActiveSource(fotobioreattore_foam)
>
> # Create the first P-O-L, set the extremes of the line and then commit to
> render
> PlotOverLine2 = PlotOverLine( Source="High Resolution Line Source" )
> DataRepresentation7 = Show()
>
> PlotOverLine2.Source.Point1 = [0.0, 0.0, 0.0]
> PlotOverLine2.Source.Point2 = [0.0, 0.0, 0.43]
>
> # This will calculate all the values for all the fileds along the line.
> # I was not able to identify how to show only the needed parameters.
> # However, it is trivial to run all the files through an "awk" command
> # for creating a file with just the needed parameters so I do not really
> # felt to spend time on something like this.
> # Sorry.
>
> Render()
>
> # The last part of the first batch unit is the definition of a writer for
> # the actual CSV file creation.
> # The source should be your freshly created PlotOverLine. In my case it is
> called PlotOverLine2
> source = PlotOverLine2
> writer = CreateWriter("YOUR_FILE_NAME_GOES_HERE.csv", source)
> writer.FieldAssociation = "Points" # or "Cells"
> writer.UpdatePipeline()
> del writer
>
> del PlotOverLine2
> del fotobioreattore_foam
> del DataRepresentation7
>
> #
> ---------------------------------------------------------------------------------------------------
> # I am not really sure if this is needed or not but, you know... better to
> safe than sorry. So, with
> # this line, the first batch operation is concluded. You will have your CSV
> file in your folder
> # and you will end up with the PlotOverLine filter selected in the GUI. For
> this reason, if you want
> # to proceed in the creation of a 2nd P-O-L in the same original source,
> you will need to specify
> # the active source part again here.
> #
> ---------------------------------------------------------------------------------------------------
>
> # Second batch section. You can use whatever filter you prefer here.... I
> just needed to go with another
> # P-O-L in another place.
>
> fotobioreattore_foam = FindSource("fotobioreattore.foam")
> SetActiveSource(fotobioreattore_foam)
>
>
> PlotOverLine2 = PlotOverLine( Source="High Resolution Line Source" )
>
> DataRepresentation7 = Show()
>
> PlotOverLine2.Source.Point1 = [-0.0345, 0.0, 0.019]
> PlotOverLine2.Source.Point2 = [-0.0345, 0.0, 0.43]
>
> Render()
>
> # The writer again
> source = PlotOverLine2
> writer = CreateWriter("SECOND_FILE_NAME.csv", source)
> writer.FieldAssociation = "Points" # or "Cells"
> writer.UpdatePipeline()
> del writer
>
> del PlotOverLine2
> del fotobioreattore_foam
> del DataRepresentation7
>
> # Please continue adding all the filters that you like and exporting the
> # results to all the CSV files that you want.
> #
> # Enjoy!!!!
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the ParaView Wiki at:
> http://paraview.org/Wiki/ParaView
>
> Follow this link to subscribe/unsubscribe:
> http://www.paraview.org/mailman/listinfo/paraview
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.paraview.org/pipermail/paraview/attachments/20111003/e0b23a39/attachment.htm>


More information about the ParaView mailing list