Skip to content

Commit

Permalink
Add check for cftime Python module in NetCDF Time Annotation plugin
Browse files Browse the repository at this point in the history
The Python module cftime is newer than the netcdftime module.
The plugin first searches for cftime, then moves on to netcdftime
if it is not available.

Additionally, the plugin is disabled by default due to these dependencies.
The plugin does not build if none of them is available.
  • Loading branch information
Tiffany-Chhim authored and NicolasVuaille committed Jun 23, 2022
1 parent f44e0c0 commit f28abb8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ set(paraview_default_plugins
LegacyExodusReader
Moments
MooseXfemClip
NetCDFTimeAnnotationPlugin
NodeEditor
NonOrthogonalSource
PacMan
Expand Down
30 changes: 22 additions & 8 deletions Plugins/NetCDFTimeAnnotationPlugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ paraview_add_plugin(NetCDFTimeAnnotationPlugin
VERSION "1.0"
SERVER_MANAGER_XML NetCDFTimeAnnotation.xml)

set_property(GLOBAL APPEND
PROPERTY
vtk_required_python_modules "netcdftime")
include(FindPythonModules)

if (BUILD_TESTING)
include(FindPythonModules)
find_python_module(netcdftime netcdftime_found)
if (netcdftime_found)
# First look for the module cftime
find_python_module(cftime module_found)
if (module_found)
set(module_name "cftime")
else ()
# If cftime is not found, search for netcdftime
find_python_module(netcdftime module_found)
if (module_found)
set(module_name "netcdftime")
endif ()
endif ()

if (module_found)
set_property(GLOBAL APPEND
PROPERTY
vtk_required_python_modules "${module_name}")
if (BUILD_TESTING)
add_subdirectory(Testing)
endif()
endif ()
else ()
message(FATAL_ERROR
"The Python module cftime (preferred) or netcdftime is required to build this plugin.")
endif ()
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<Element index="0" value=""/>
</Property>
<Property name="Script" id="6868.Script" number_of_elements="1">
<Element index="0" value="from netcdftime import utime&#xa;from netcdftime import datetime&#xa;&#xa;inp = self.GetInputDataObject(0,0)&#xa;outp = self.GetOutputDataObject(0)&#xa;&#xa;currentTime = inp.GetInformation().Get(vtk.vtkDataObject.DATA_TIME_STEP())&#xa;timeUnitsArray = inp.GetFieldData().GetAbstractArray(&#x22;time_units&#x22;)&#xa;if timeUnitsArray:&#xa; timeUnits = timeUnitsArray.GetValue(0)&#xa;&#xa; cdftime = utime(timeUnits)&#xa; t = cdftime.num2date(currentTime)&#xa;&#xa; fdate = vtk.vtkStringArray()&#xa; fdate.SetName(&#x22;FullDate&#x22;)&#xa; fdate.SetNumberOfValues(1)&#xa; fdate.SetValue(0, str(t))&#xa; outp.GetFieldData().AddArray(fdate)&#xa;&#xa; date = vtk.vtkIntArray()&#xa; date.SetName(&#x22;Date&#x22;)&#xa; date.SetNumberOfValues(6)&#xa; date.SetValue(0, t.year)&#xa; date.SetValue(1, t.month)&#xa; date.SetValue(2, t.day)&#xa; date.SetValue(3, t.hour)&#xa; date.SetValue(4, t.minute)&#xa; date.SetValue(5, t.second)&#xa; outp.GetFieldData().AddArray(date)&#xa;"/>
<Element index="0" value="try:&#xa; import cftime as ct&#xa; hasCftime = True&#xa;except:&#xa; try:&#xa; import netcdftime as ct&#xa; hasCftime = False&#xa; except:&#xa; print(&#x22;Need the python cftime (or the older netcdftime) module for the NetCDFTimeAnnotation plugin!&#x22;)&#xa;&#xa;&#xa;inp = self.GetInputDataObject(0,0)&#xa;outp = self.GetOutputDataObject(0)&#xa;&#xa;currentTime = inp.GetInformation().Get(vtk.vtkDataObject.DATA_TIME_STEP())&#xa;&#xa;sdate = vtk.vtkStringArray()&#xa;sdate.SetName(&#x22;RawDate&#x22;)&#xa;sdate.SetNumberOfValues(1)&#xa;sdate.SetValue(0,str(currentTime))&#xa;outp.GetFieldData().AddArray(sdate)&#xa;timeUnitsArray = inp.GetFieldData().GetAbstractArray(&#x22;time_units&#x22;)&#xa;&#xa;if timeUnitsArray:&#xa; timeUnits = timeUnitsArray.GetValue(0)&#xa;&#xa; if hasCftime:&#xa; t = ct.num2date(currentTime, timeUnits)&#xa; else:&#xa; cdftime = ct.utime(timeUnits)&#xa; t = cdftime.num2date(currentTime)&#xa;&#xa; fdate = vtk.vtkStringArray()&#xa; fdate.SetName(&#x22;FullDate&#x22;)&#xa; fdate.SetNumberOfValues(1)&#xa; fdate.SetValue(0, str(t))&#xa; outp.GetFieldData().AddArray(fdate)&#xa;&#xa; date = vtk.vtkIntArray()&#xa; date.SetName(&#x22;Date&#x22;)&#xa; date.SetNumberOfValues(6)&#xa; date.SetValue(0, t.year)&#xa; date.SetValue(1, t.month)&#xa; date.SetValue(2, t.day)&#xa; date.SetValue(3, t.hour)&#xa; date.SetValue(4, t.minute)&#xa; date.SetValue(5, t.second)&#xa; outp.GetFieldData().AddArray(date)&#xa;"/>
</Property>
<Property name="UpdateExtentScript" id="6868.UpdateExtentScript" number_of_elements="1">
<Element index="0" value=""/>
Expand Down
6 changes: 3 additions & 3 deletions Plugins/NetCDFTimeAnnotationPlugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ you need to change the order of the Date array elements as in the following exam

## Important note

This plugin takes benefit from the third-party `netcdftime`
Python module, it is mandatory to have it installed in order to build
and use this filter.
This plugin takes benefit from the third-party `cftime` or `netcdftime`
Python modules (the former being preferred). It is mandatory to have
one of them installed in order to build and use this filter.

## Example
User can refer to this page
Expand Down

0 comments on commit f28abb8

Please sign in to comment.