Salome HOME
introducing test on mesh extension, with resources generated first
[modules/hydro.git] / src / HYDROTools / interpolS.py
1 # -*- coding: utf-8 -*-
2
3 """
4 assignStrickler
5 """
6 __revision__ = "V2.02"
7
8 import os
9
10 import salome
11
12 import HYDROPy
13
14 import numpy as np
15 import MEDLoader as ml
16 import medcoupling as mc
17
18 salome.salome_init()
19
20 class BadParamsError(ValueError):
21   """Bad parameters exception"""
22   pass
23
24
25 def assignStrickler(nomCas, input_file_name, output_file_name="", med_field_name='BOTTOM FRICTION', verbose=False):
26   """
27   assignStrickler creates the scalar field associated with the mesh of the calculation case that
28   represents the Strickler coefficient of the land cover.
29
30   In:
31     nomCas: Calculation Case Name in module HYDRO
32     input_file_name: med file name corresponding to the HYDRO case
33     output_file_name: med file name with the Strickler coefficient; default is the same as fichierMaillage
34     med_field_name: name of the field of the Strickler coefficient
35                      default value is 'BOTTOM FRICTION'.
36   Out:
37   """
38   erreur = 0
39   message = ""
40
41   while not erreur:
42
43     if verbose:
44       print("nomCas:", nomCas)
45       print("input_file_name:", input_file_name)
46       print("output_file_name:", output_file_name)
47       print("med_field_name:", med_field_name)
48
49 # 1. Controls
50 # 1.1. Check calculation case
51     doc = HYDROPy.HYDROData_Document.Document()
52     case = doc.FindObjectByName(nomCas)
53     if case is None:
54       raise BadParamsError("Calculation case '%s' not found" % nomCas)
55
56 # 1.2. Check input MED file
57     if not os.path.exists(input_file_name):
58       raise BadParamsError("Input file '%s' not exists" % input_file_name)
59
60 # 2. The output file
61 # 2.1. If output MED file is not defined, it is equal to the input file
62     if not output_file_name:
63       output_file_name = input_file_name
64
65 # 2.2. Copy input file to the output path, if the output path is different from the input path
66     if output_file_name != input_file_name:
67       import shutil
68       shutil.copyfile(input_file_name, output_file_name)
69 #
70 # 3. Reads the mesh
71 #
72     meshMEDFileRead = ml.MEDFileMesh.New(output_file_name)
73 #
74 # 4. Gets the information about the nodes
75 #
76     nbnodes = meshMEDFileRead.getNumberOfNodes()
77     if verbose:
78       print("Number of nodes: %d" % nbnodes)
79 #
80     coords = meshMEDFileRead.getCoords()
81     #print "coords =\n", coords
82 #
83 # 5. Calculation of the Strickler coefficient
84     values = list()
85     x_coords = coords[:, 0]
86     y_coords = coords[:, 1]
87     #print "x_coords =\n", x_coords
88     #print "y_coords =\n", y_coords
89     values = case.GetStricklerCoefficientForPoints(x_coords, y_coords, 0.0, True)
90 #
91 # 6. Field MED file
92 #
93     coeff = mc.DataArrayDouble(np.asfarray(values, dtype='float'))
94     coeff.setInfoOnComponents(["Strickler [SI]"])
95     #print "coeff =\n", coeff
96     if verbose:
97       print(".. Ecriture du Strickler sous le nom '"+med_field_name+"'")
98     fieldOnNodes = ml.MEDCouplingFieldDouble(ml.ON_NODES)
99     fieldOnNodes.setName(med_field_name)
100     fieldOnNodes.setMesh(meshMEDFileRead.getMeshAtLevel(0))
101     fieldOnNodes.setArray(coeff)
102 #   Ces valeurs d'instants sont mises pour assurer la lecture par TELEMAC
103 #   instant = 0.0
104 #   numero d'itération : 0
105 #   pas de numero d'ordre (-1)
106     fieldOnNodes.setTime(0.0, 0, -1)
107 #
108     fMEDFile_ch_d = ml.MEDFileField1TS()
109     fMEDFile_ch_d.setFieldNoProfileSBT(fieldOnNodes)
110     fMEDFile_ch_d.write(output_file_name, 0)
111 #
112     break
113
114   if erreur:
115     print(message)
116
117   return