[Paraview] Annotate time as data/time using pythonannotation and programmablefilter

Eelco van Vliet eelcovv at gmail.com
Wed Jun 7 04:34:48 EDT 2017


For those interested

I turned this script into a macro which you can add. It should work on a
generic source containing a time in number of hourse since 0001-01-01

Regards


--- start macro python source code---
"""
A macro to turn a "hourse since 0001-01-01 00:00:00" floating number into a
human readable data/time
string and put it as an annotion to the current view.
Requirement: a proper numpy/python/netcdf4 version in the search path.
Current the search path is hard
added to the macro pointing to the location
C://Apps/Anaconda/Anaconda2/envs/paraview/Lib/site-packages
At this location the netCDF4 module should be installed. Should be changed
depending on current python installation
Eelco van Vliet
June 2017
"""
#### import the simple module from the paraview
from paraview.simple import *
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()
# find source
source = GetActiveSource()
# create a new 'Programmable Filter'
programmableFilter1 = ProgrammableFilter(Input=source)
programmableFilter1.RequestInformationScript = ''
programmableFilter1.RequestUpdateExtentScript = ''
programmableFilter1.PythonPath = ""
# Properties modified on programmableFilter1
programmableFilter1.OutputDataSetType = 'vtkTable'
programmableFilter1.Script = '\
import sys\n\
sys.path.append("C://Apps/Anaconda/Anaconda2/envs/paraview/Lib/site-packages")\n\
import netCDF4 as nc\n\
t = inputs[0].GetInformation().Get(vtk.vtkDataObject.DATA_TIME_STEP())\n\
date_time = nc.num2date(t, units="hours since 0001-01-01 00:00:00",
calendar="gregorian")\n\
outputarray = vtk.vtkStringArray()\n\
outputarray.SetName("datetime")\n\
outputarray.SetNumberOfTuples(1)\n\
outputarray.SetValue(0, "{}".format(date_time))\n\
print("{} -> {}".format(t, date_time))\n\
output.RowData.AddArray(outputarray)'
# find view
renderView1 = GetActiveView()
# create a new 'Python Annotation'
pythonAnnotation1 = PythonAnnotation(Input=programmableFilter1)
# Properties modified on pythonAnnotation1
pythonAnnotation1.ArrayAssociation = 'Row Data'
pythonAnnotation1.Expression =
'"{}".format(input.RowData["datetime"].GetValue(0))'
# show data in view
pythonAnnotation1Display = Show(pythonAnnotation1, renderView1)
# update the view to ensure updated data information
renderView1.Update()

2017-06-06 17:41 GMT+02:00 Eelco van Vliet <eelcovv at gmail.com>:

> Dear Utkarsh,
>
> After some more googling and your answer I found the solution. Indeed I
> have to store the data as a string in the Programmable Filter and then
> retrieve with GetValue in the Python Annoatation.
> My ProgrammableFilter with Ouput set to vtkData is now
>
> import sys
>
> sys.path.append("C://Apps/Anaconda/Anaconda2/envs/paraview/Lib/site-packages/")
>
> import netCDF4 as nc
>
> t = inputs[0].GetInformation().Get(vtk.vtkDataObject.DATA_TIME_STEP())
>
> date_time = nc.num2date(t, units="hours since 0001-01-01 00:00:00", calendar="gregorian")
>
> outputarray = vtk.vtkStringArray()
>
> outputarray.SetName("datetime")
>
> outputarray.SetNumberOfTuples(1)
>
> outputarray.SetValue(0, "{}".format(date_time))
>
> print("{} -> {}".format(t, date_time))
>
> output.RowData.AddArray(outputarray)
>
>
> and after that I add a PythonAnnotation with Row Data as Array Association and the following expression
>
>
> "{}".format(input.RowData["datetime"].GetValue(0))
>
>
>
> It is not very trivial I must say, but the result is now that I can plot a date/time string in stead of a float with number of hourse since. Excellent! Thanks for your quick help
>
>
> Regards
>
> Eelco
>
>
> Ps: for those interessed in using netcdf4 with paraview: you have to make sure to get all the versions correct. I did this by using Anaconda and than create a special paraview environment
>
> conda create -n paraview python=2.7.3 netcdf4
>
> activate paraview
>
> conda install numpy=1.8.1
>
>
>
>
> 2017-06-06 17:05 GMT+02:00 Utkarsh Ayachit <utkarsh.ayachit at kitware.com>:
>
>> Array Association: Row Data
>>> Expression: "{}".format(input.RowData)
>>> But this gives me just a reference to the row data, not the string of
>>> the date/time I just stored
>>>
>>
>> Try using `input.RowData["datatime"].GetValue(0)` instead. String arrays
>> are a little cumbersome currently. You have to use the vtkAbstractArray API
>> (GetValue) to access elements.
>>
>> Utkarsh
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/paraview/attachments/20170607/a6e0fcf6/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: number_to_datetime_annotation.py
Type: application/octet-stream
Size: 2146 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/paraview/attachments/20170607/a6e0fcf6/attachment.obj>


More information about the ParaView mailing list