Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[modules/hydro.git] / src / HYDROTools / interpolS.py
old mode 100644 (file)
new mode 100755 (executable)
index aaf3135..55c017a
 # -*- coding: utf-8 -*-
 
+"""
+assignStrickler
+"""
+__revision__ = "V2.02"
+
 import os
-import sys
-import shutil
 
 import salome
 
-from med import medfile
-from med import medmesh
-from med import medfield
-from med import medenum
-
 import HYDROPy
 
+import numpy as np
+import MEDLoader as ml
+import MEDCoupling as mc
+
 # Get current study id
 salome.salome_init()
 theStudy = salome.myStudy
 theStudyId = salome.myStudyId
 
-"""Bad parameters exception"""
 class BadParamsError(ValueError):
+  """Bad parameters exception"""
   pass
 
-"""
-Creates MED file with scalar field associated with the mesh of the calculation case. 
-The scalar value associated with the mesh node is the Strickler coefficient of the land cover containing the node.
-case_name: calculation case name in the study
-med_file_name: path to input MED file with mesh on nodes
-output_file_name: path to output MED file with
-med_field_name: field name
-"""
-def assignStrickler(case_name, med_file_name, output_file_name, med_field_name='FRICTION'):
-  # Check calculation case
-  doc = HYDROPy.HYDROData_Document.Document( theStudyId )
-  case = doc.FindObjectByName(case_name)
-  if case is None:
-       raise BadParamsError("Calculation case '%s' not found" % case_name)
-       
-  # Check input MED file
-  if not os.path.exists(med_file_name):
-       raise BadParamsError("Input file '%s' not exists" % med_file_name)
-
-  # Copy input file to the output path, if the output path is empty - use the input path for the output
-  file_path = med_file_name
-  if output_file_name and (output_file_name != file_path):
-       shutil.copyfile(med_file_name, output_file_name)
-       file_path = output_file_name
-
-  # Open input MED file
-  fid = medfile.MEDfileOpen(file_path, medenum.MED_ACC_RDEXT)
-
-  # Get mesh info
-  mesh_name, sdim, mdim, meshtype, desc, dtunit, sort, nstep,  repere, axisname, axisunit = medmesh.MEDmeshInfo(fid, 1)
-  
-  nb_nodes, chgt, trsf = medmesh.MEDmeshnEntity(fid, mesh_name, medenum.MED_NO_DT, medenum.MED_NO_IT,
-                                                                                               medenum.MED_NODE, medenum.MED_NONE,
-                                                                                               medenum.MED_COORDINATE, medenum.MED_NO_CMODE)
-  
-  
-  # Create field
-  if nb_nodes > 0:
-    # Get node coordinates    
-    coords = medfile.MEDFLOAT(nb_nodes*sdim)
-    medmesh.MEDmeshNodeCoordinateRd(fid, mesh_name, medenum.MED_NO_DT, medenum.MED_NO_IT,
-                                                                       medenum.MED_FULL_INTERLACE, coords)
-  
-    # Get list of Strickler coefficient values for the nodes
-    values = []
-    x_coords = coords[0::sdim]
-    y_coords = coords[1::sdim]
-    #print "case.GetStricklerCoefficientForPoints"
+
+def assignStrickler(nomCas, input_file_name, output_file_name="", med_field_name='BOTTOM FRICTION', verbose=False):
+  """
+  assignStrickler creates the scalar field associated with the mesh of the calculation case that
+  represents the Strickler coefficient of the land cover.
+
+  In:
+    nomCas: Calculation Case Name in module HYDRO
+    input_file_name: med file name corresponding to the HYDRO case
+    output_file_name: med file name with the Strickler coefficient; default is the same as fichierMaillage
+    med_field_name: name of the field of the Strickler coefficient
+                     default value is 'BOTTOM FRICTION'.
+  Out:
+  """
+  erreur = 0
+  message = ""
+
+  while not erreur:
+
+    if verbose:
+      print "nomCas:", nomCas
+      print "input_file_name:", input_file_name
+      print "output_file_name:", output_file_name
+      print "med_field_name:", med_field_name
+
+# 1. Controls
+# 1.1. Check calculation case
+    doc = HYDROPy.HYDROData_Document.Document(theStudyId)
+    case = doc.FindObjectByName(nomCas)
+    if case is None:
+      raise BadParamsError("Calculation case '%s' not found" % nomCas)
+
+# 1.2. Check input MED file
+    if not os.path.exists(input_file_name):
+      raise BadParamsError("Input file '%s' not exists" % input_file_name)
+
+# 2. The output file
+# 2.1. If output MED file is not defined, it is equal to the input file
+    if not output_file_name:
+      output_file_name = input_file_name
+
+# 2.2. Copy input file to the output path, if the output path is different from the input path
+    if output_file_name != input_file_name:
+      import shutil
+      shutil.copyfile(input_file_name, output_file_name)
+#
+# 3. Reads the mesh
+#
+    meshMEDFileRead = ml.MEDFileMesh.New(output_file_name)
+#
+# 4. Gets the information about the nodes
+#
+    nbnodes = meshMEDFileRead.getNumberOfNodes()
+    if verbose:
+      print "Number of nodes: %d" % nbnodes
+#
+    coords = meshMEDFileRead.getCoords()
+    #print "coords =\n", coords
+#
+# 5. Calculation of the Strickler coefficient
+    values = list()
+    x_coords = coords[:, 0]
+    y_coords = coords[:, 1]
+    #print "x_coords =\n", x_coords
+    #print "y_coords =\n", y_coords
     values = case.GetStricklerCoefficientForPoints(x_coords, y_coords, 0.0, True)
-  
-    # Write the values to the field    
-    if len(values) > 0:
-      values = medfile.MEDFLOAT(values)
-
-      comp  = "strickler coeff "
-      medfield.MEDfieldCr(fid, med_field_name, medfile.MED_FLOAT64, 
-                          1, comp, '', '', mesh_name)
-                                         
-      medfield.MEDfieldValueWr(fid, med_field_name, medenum.MED_NO_DT, medenum.MED_NO_IT, 0.0,
-                               medenum.MED_NODE, medenum.MED_NONE,
-                               medenum.MED_FULL_INTERLACE, medenum.MED_ALL_CONSTITUENT, len(values), values)
-                                                          
-      print "MED field '%s' on %s nodes has been created." % (med_field_name, nb_nodes)
-                                                        
-  # close MED file
-  medfile.MEDfileClose(fid)
\ No newline at end of file
+#
+# 6. Field MED file
+#
+    coeff = mc.DataArrayDouble(np.asfarray(values, dtype='float'))
+    coeff.setInfoOnComponents(["Strickler [SI]"])
+    #print "coeff =\n", coeff
+    if verbose:
+      print ".. Ecriture du Strickler sous le nom '"+med_field_name+"'"
+    fieldOnNodes = ml.MEDCouplingFieldDouble(ml.ON_NODES)
+    fieldOnNodes.setName(med_field_name)
+    fieldOnNodes.setMesh(meshMEDFileRead.getMeshAtLevel(0))
+    fieldOnNodes.setArray(coeff)
+#   Ces valeurs d'instants sont mises pour assurer la lecture par TELEMAC
+#   instant = 0.0
+#   numero d'itération : 0
+#   pas de numero d'ordre (-1)
+    fieldOnNodes.setTime(0.0, 0, -1)
+#
+    fMEDFile_ch_d = ml.MEDFileField1TS()
+    fMEDFile_ch_d.setFieldNoProfileSBT(fieldOnNodes)
+    fMEDFile_ch_d.write(output_file_name, 0)
+#
+    break
+
+  if erreur:
+    print message
+
+  return