source: trunk/etc/src/yrms.c @ 503

Last change on this file since 503 was 503, checked in by lnalod, 13 years ago

CeCILL license.

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1/*
2Copyright or © or Copr. Charles SORROR (2011/01/13)
3
4mail of the author: cslod@locean-ipsl.upmc.fr
5
6This software is a framework for variational data assimilation in
7numerical models.
8
9This software is governed by the CeCILL license under French law and
10abiding by the rules of distribution of free software.  You can  use,
11modify and/ or redistribute the software under the terms of the CeCILL
12license as circulated by CEA, CNRS and INRIA at the following URL
13"http://www.cecill.info".
14
15As a counterpart to the access to the source code and  rights to copy,
16modify and redistribute granted by the license, users are provided only
17with a limited warranty  and the software's author,  the holder of the
18economic rights,  and the successive licensors  have only  limited
19liability.
20
21In this respect, the user's attention is drawn to the risks associated
22with loading,  using,  modifying and/or developing or reproducing the
23software by the user in light of its specific status of free software,
24that may mean  that it is complicated to manipulate,  and  that  also
25therefore means  that it is reserved for developers  and  experienced
26professionals having in-depth computer knowledge. Users are therefore
27encouraged to load and test the software's suitability as regards their
28requirements in conditions enabling the security of their systems and/or
29data to be ensured and,  more generally, to use and operate it in the
30same conditions as regards security.
31
32The fact that you are presently reading this means that you have had
33knowledge of the CeCILL license and that you accept its terms.
34*/
35
36
37/************************************************************************/
38/*              yrms : is a part of Yao utilities and tools             */
39/************************************************************************/
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <math.h>
44
45#define BUFSIZE 80
46
47int main (int argc, char *argv[])
48{    /* . Syntaxe: yrms f1 f2 zspace [f3] */
49     /* . Purpose: calculer l'erreur rms entre 2 trajectoires d'espace
50                 dont les valeurs sont respectvement dans les fichiers
51                 f1 et f2 passes en parametres qui doivent contenir
52                 le meme nombre d'elements qui doit d'ailleurs etre
53                 un multiple de zspace qui est la taille de l'espace
54                 le resultat, {pour chaque pas d'espace, zspace} est
55                 ecrit dans f3 s'il est mentionne sinon sur la sortie
56                 standard.
57                 la somme des rms est affichee a la fin
58        . Warning: la detection de ligne blanche ou vide n'est pas geree
59        . Compilation : cc -Wall -o yrms yrms.c -lm                     */
60
61        FILE    *f1, *f2, *f3;
62        char    buf1[BUFSIZE+1], buf2[BUFSIZE+1];
63        int     zspace, rstelt, nbt;
64        double  x1, x2, errsumt, errsumallt, rms, sumrms;
65
66        if (argc<4 || argc > 5)
67        {       printf("syntaxe error !!! : yrms f1 f2 zspace [f3]\n");
68                exit(0);
69        }
70
71        if ((f1 = fopen(argv[1], "r")) <= 0)
72        {       printf ("file1 %s not found !!!\n", argv[1]);
73                exit(0);
74        }
75        if ((f2 = fopen(argv[2], "r")) <= 0)
76        {       printf ("file2 %s not found !!!\n", argv[2]);
77                exit(0);
78        }
79        if (argc==5)
80        {       if ((f3 = fopen(argv[4], "w")) <= 0)
81                {       printf ("failed while opening file3 %s !!!\n", argv[3]);
82                        exit(0);
83                }
84        }
85        else
86                f3 = stdout;
87
88        zspace = atoi(argv[3]);
89        if (zspace<=0)
90        {       printf ("zspace must be a integer greater than zero !!!\n");
91                exit(0);
92        }
93
94        rstelt=0; nbt=0; errsumt=0.0; errsumallt=0.0, sumrms=0.0;
95        while (1) /* boucle de */
96        {       /* lecture paralle des 2 fichiers */
97                if (fgets (buf1, BUFSIZE, f1) == NULL)
98                {  /* fin du 1er fichier => fin du 2eme fichier */
99                   if (fgets (buf2, BUFSIZE, f2) != NULL)
100                   {  printf("erratum 1: the files have not the same size !!!\n");
101                      exit(0);
102                   }
103
104                   /* fin: le nombre d'elt devait etre un multiple de zspace */
105                   if (rstelt!=0)
106                   {  printf("erratum 3: number of element is not o multiple of zspace\n");
107                      exit(0);
108                   }
109
110                   /* fin fin */
111                   break;
112                }
113
114                /* on doit pouvoir lire le 2eme fichier */
115                if (fgets (buf2, BUFSIZE, f2) == NULL)
116                {  printf("erratum 2: the files have not the same size !!!\n");
117                   exit(0);
118                }
119
120                x1 = atof(buf1);        x2 = atof(buf2);
121                //printf("x1=%f x2=%f \n", x1, x2);
122
123                errsumt += ((x1-x2)*(x1-x2));
124                errsumallt += errsumt;
125
126                rstelt = (rstelt+1)%zspace;
127                if (rstelt==0)
128                {       //printf ("errsumt=%f \n", errsumt);
129                        rms = sqrt(errsumt/zspace);
130                        fprintf(f3, "%f\n",  rms);
131                        errsumt = 0.0;
132                        ++nbt;
133                        sumrms += rms;
134                }
135
136        } /* fin du while de la boucle de lecture parallele */
137        printf("SUMt(rms) = %30.20e\n", sumrms);
138
139        /* rms sur tout la trajectoire */
140        rms = sqrt(errsumallt/(zspace*nbt));
141        printf("RMS(trajectoire::%i pas de temps) = %30.20e\n", nbt, rms);
142
143        printf("yrms: process end normaly\n");
144        return (0);
145}
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
Note: See TracBrowser for help on using the repository browser.