1 | package com.medias.integration.prevalidation; |
---|
2 | |
---|
3 | import com.medias.Constantes; |
---|
4 | import com.medias.DateTime; |
---|
5 | import com.medias.EcritureLog; |
---|
6 | import com.medias.database.objects.Capteur; |
---|
7 | import com.medias.database.objects.Parametre; |
---|
8 | import com.medias.integration.lecture_ecriture.LectureCapteurs; |
---|
9 | import com.medias.integration.lecture_ecriture.LectureParametres; |
---|
10 | import com.medias.integration.lecture_ecriture.LectureUnites; |
---|
11 | import com.medias.integration.vues.ParametreModele; |
---|
12 | |
---|
13 | import java.io.BufferedReader; |
---|
14 | import java.io.File; |
---|
15 | import java.io.FileInputStream; |
---|
16 | import java.io.IOException; |
---|
17 | import java.io.InputStreamReader; |
---|
18 | import java.util.ArrayList; |
---|
19 | import java.util.Date; |
---|
20 | |
---|
21 | /** |
---|
22 | * Prévalidation d'un jeu ayant des données de type METEO |
---|
23 | * |
---|
24 | */ |
---|
25 | public class PreValidationMETEOFILE extends PreValidation |
---|
26 | { |
---|
27 | public PreValidationMETEOFILE(File fichier) { |
---|
28 | super(fichier); |
---|
29 | } |
---|
30 | |
---|
31 | /** |
---|
32 | * Méthode de prévalidation permmettent de distinguer l'entéte et les données d'un fichier METEO |
---|
33 | * NB: La premiére ligne ne commenéant pas par # indique la ligne é laquelle commence la série de données |
---|
34 | * @return |
---|
35 | */ |
---|
36 | |
---|
37 | public void lireFichier(LectureParametres lectureParametres, LectureUnites lectureUnites, LectureCapteurs lectureCapteurs |
---|
38 | , ArrayList<Parametre> parametres, ArrayList<Capteur> capteurs) { |
---|
39 | try { |
---|
40 | //************************************************************************************** |
---|
41 | // On récupére le contenu du fichier pour le lire. |
---|
42 | //**************************************************************************************** |
---|
43 | FileInputStream fis = new FileInputStream(fichier); |
---|
44 | InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); |
---|
45 | BufferedReader buf = new BufferedReader(isr); |
---|
46 | // Lecture du fichier ligne aprés ligne |
---|
47 | String ligne = buf.readLine(); |
---|
48 | while (ligne != null) { |
---|
49 | if (!ligne.startsWith("#")) { |
---|
50 | mesures.add(ligne); |
---|
51 | } else { |
---|
52 | entete.add(ligne); |
---|
53 | } |
---|
54 | ligne = buf.readLine(); |
---|
55 | } |
---|
56 | // Récupération des informations de l'entéte |
---|
57 | // Dates de début et de fin |
---|
58 | int nbLignesMesures = mesures.size(); |
---|
59 | Date dateDeb = DateTime.stringToDate( mesures.get( 0 ).substring( 0, 10 ).trim(), "yyyy MM dd" ); |
---|
60 | Date dateFin = DateTime.stringToDate(mesures.get(nbLignesMesures-1).substring(0,10).trim(), "yyyy MM dd"); |
---|
61 | // System.out.println("deb="+dateDeb+", fin="+dateFin); |
---|
62 | // Infos supplémentaires (pour trouver les capteurs) |
---|
63 | infoSup = new ArrayList<String>(); |
---|
64 | infoSup.add(entete.get(1)); |
---|
65 | // On recherche les capteurs apparaissant dans les infos supplémentaires du fichier |
---|
66 | ArrayList<Integer> indicesCaptInFile = findIndicesCaptInFile(lectureCapteurs,capteurs); |
---|
67 | // Infos sur les paramétres et la valeur absente |
---|
68 | infoParams = new ArrayList<ParametreModele>(); |
---|
69 | ParametreModele infoParam; |
---|
70 | int nbLignesEntete = entete.size(); |
---|
71 | int diff; |
---|
72 | String ligneEntete, nombre1, nombre2, valAbs = null; |
---|
73 | ArrayList<String> lignesParam = new ArrayList<String>(); |
---|
74 | for(int numL=0; numL < nbLignesEntete;numL++){ |
---|
75 | ligneEntete = entete.get(numL).trim(); |
---|
76 | // Recherche des lignes de description des paramétres |
---|
77 | if(ligneEntete.toLowerCase().startsWith("# col.")){ |
---|
78 | if (ligneEntete.indexOf("->") == -1) { |
---|
79 | lignesParam.add(ligneEntete); |
---|
80 | nbParams++; |
---|
81 | } else { |
---|
82 | nombre1 = ligneEntete.substring(ligneEntete.indexOf("ol.")+3,ligneEntete.indexOf("->")).trim(); |
---|
83 | nombre2 = ligneEntete.substring(ligneEntete.indexOf("->")+2, ligneEntete.indexOf(":")).trim(); |
---|
84 | diff = Integer.parseInt(nombre2)-Integer.parseInt(nombre1)+1; |
---|
85 | nbParams += diff; |
---|
86 | for (int i = 0; i < diff; i++) { |
---|
87 | lignesParam.add("# col. x : none (no unit)"); |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | // Recherche de la ligne oé est indiquée la valeur absente |
---|
92 | else if (ligneEntete.indexOf("corresponds to missed data") != -1){ |
---|
93 | valAbs = ligneEntete.substring(ligneEntete.indexOf("\"")+1, ligneEntete.lastIndexOf("\"")); |
---|
94 | } |
---|
95 | } |
---|
96 | String nomParam, nomUnite, codeParam; |
---|
97 | String[] unite; |
---|
98 | int indiceParam, indiceCapt; |
---|
99 | for (int numParam = 0; numParam < nbParams; numParam++) { |
---|
100 | infoParam = new ParametreModele(); |
---|
101 | nomParam = lignesParam.get(numParam); |
---|
102 | nomUnite = nomParam.substring(nomParam.indexOf("(")+1,nomParam.indexOf(")")).trim(); |
---|
103 | nomParam = nomParam.substring(nomParam.indexOf(":")+1,nomParam.indexOf("(")).trim(); |
---|
104 | infoParam.setNom(nomParam); |
---|
105 | infoParam.setValAbsente(valAbs); |
---|
106 | infoParam.setCoef("1"); |
---|
107 | unite = findUnite(nomUnite, lectureUnites); |
---|
108 | infoParam.setNomUnite(unite[0]); |
---|
109 | infoParam.setType(unite[1]); |
---|
110 | if (nomParam.toLowerCase().equals( Constantes.nomParametreLatitude) || nomParam.toLowerCase().equals(Constantes.nomParametreLongitude) |
---|
111 | || nomParam.toLowerCase().equals(Constantes.nomParametreAltitude) || nomParam.toLowerCase().equals("height")) { |
---|
112 | infoParam.setFonction(Constantes.nomFonctionInsererLoc); |
---|
113 | } else if (nomParam.startsWith("none") || nomParam.startsWith("Altitude") || nomParam.startsWith("QF") |
---|
114 | || nomParam.startsWith("Quality")) { |
---|
115 | infoParam.setFonction(Constantes.nomFonctionNone); |
---|
116 | } else if (nomParam.startsWith("Year") || nomParam.startsWith("Month") || nomParam.startsWith("Day") |
---|
117 | || nomParam.startsWith("Hour") || nomParam.startsWith("Minute") || nomParam.startsWith("Second")) { |
---|
118 | infoParam.setDateDeb(dateDeb); |
---|
119 | infoParam.setDateFin(dateFin); |
---|
120 | infoParam.setDateSince(null); |
---|
121 | infoParam.setFonction(Constantes.nomFonctionDate); |
---|
122 | infoParam.setFormat(nomUnite); |
---|
123 | infoParam.setType(nomParam); |
---|
124 | } else { |
---|
125 | infoParam.setFonction(Constantes.nomFonctionInsererVal); |
---|
126 | // Nom des paramétres correspondant aux variables primaires |
---|
127 | indiceParam = findIndiceParam(nomParam,lectureParametres,parametres); |
---|
128 | infoParam.setIndiceParam(indiceParam); |
---|
129 | if (indiceParam == -1) { |
---|
130 | codeParam = "???"; |
---|
131 | EcritureLog.ecrire("\nErreur : le paramétre '"+nomParam+"' n'a pas été trouvé","err"); |
---|
132 | } else { |
---|
133 | codeParam = parametres.get(indiceParam).getParametreCode(); |
---|
134 | infoParam.setDelta(parametres.get(indiceParam).getDelta()); |
---|
135 | infoParam.setLod(parametres.get(indiceParam).getLod()); |
---|
136 | } |
---|
137 | indiceCapt = findIndiceCapt(codeParam,indicesCaptInFile,capteurs); |
---|
138 | infoParam.setIndiceCapt(indiceCapt); |
---|
139 | } |
---|
140 | // EcritureLog.ecrire("param="+infoParam.getNom()+", format="+infoParam.getFormat()+", fct="+infoParam.getFonction()+", type="+infoParam.getType()+", unite="+infoParam.getNomUnite(), "out"); |
---|
141 | infoParams.add(infoParam); |
---|
142 | } |
---|
143 | } catch (IOException e) { |
---|
144 | System.out.println(e.getMessage()+ "\nErreur dans la lecture du fichier "+ fichier.getName() + " !"); |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|