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