updated top-level README and version_decl for V4.5 (#1847)
[WRF.git] / chem / KPP / util / wkc / compare_kpp_to_species.c
blob8a6151148cf3ad4e0157696b1f77f926fec4a04e
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <dirent.h>
6 #include <unistd.h>
7 #include <sys/stat.h>
10 #include "protos.h"
11 #include "protos_kpp.h"
12 #include "registry.h"
13 #include "data.h"
14 #include "kpp_data.h"
17 #define DEBUGR 0
19 /* variable name of molecular nitrogen in KPP file*/
20 char * kpp_n2 = "N2" ;
22 /* variable name of molecular oxygen in KPP file*/
23 char * kpp_o2 = "O2" ;
25 /* variable name of CO2 in KPP file*/
26 char * kpp_co2 = "CO2" ;
28 /* variable name of water vapor in KPP file*/
29 char * kpp_h2o = "H2O" ;
32 /* variable name of third body in KPP file*/
33 char * kpp_third_body = "M" ;
36 int
37 compare_kpp_to_species ( char * kpp_dirname)
39 knode_t * p1, *p2, * p, * pm1, * pm2, * p3, * pm3 , wrf_kpp_equivs;
40 char name1[NAMELEN], name2[NAMELEN], name3[NAMELEN] ;
41 char equivfilename[NAMELEN];
42 FILE * equivFile;
43 char inln[NAMELEN], newln[NAMELEN];
44 int in_comment, got_it;
45 char wrf_name[NAMELEN], kpp_name[NAMELEN];
46 int i;
47 int got_h2o;
49 /* first find matching packages */
52 for ( p1 = KPP_packs ; p1 != NULL ; p1 = p1->next ) {
53 for ( p2 = WRFC_packs ; p2 != NULL ; p2 = p2->next ) {
55 /* printf(" ... test0 %s %s\n",p1->name, p2->name ); */
58 if ( strcmp (p1->name, p2->name) == 0) {
62 /* point from a KPP-pack to the corresponding WRFC_pack */
63 p1->assoc_wrf_pack = p2;
73 for ( p1 = KPP_packs ; p1 != NULL ; p1 = p1->next ) {
74 p2 = p1->assoc_wrf_pack;
75 printf(" ... testing %s %s\n",p1, p2 );
76 if ( p2 ) {
77 fprintf(stderr, "\n \n FOUND match between WRF-Chem/KPP for mechanism: %s \n", p2->name);
80 for ( pm1 = p1 -> members; pm1 != NULL ; pm1 = pm1->next ) {
81 pm1->found_match = 0;
82 pm1->is_radical =0;
87 /* if file *_wrfkpp.equiv exists store contents in wrf_kpp_equivs struct */
89 sprintf( equivfilename, "%s/%s/%s_wrfkpp.equiv", kpp_dirname, p2->name, p2->name);
91 /* printf("%s \n", equivfilename ); */
93 equivFile = fopen (equivfilename, "r" );
95 if ( equivFile == NULL ) {
96 fprintf(stderr," Did NOT find file %s\n",equivfilename);
98 else
100 fprintf(stderr," Found file %s\n",equivfilename);
102 /* loop over lines in wrf_kpp_equiv file */
103 while ( fgets ( inln , NAMELEN , equivFile ) != NULL ){
104 if ( DEBUGR == 1 ) printf(" i %s ", inln );
107 int j;
108 for(j = 0; j < NAMELEN ; j++) wrf_name[j]='\0';
109 for(j = 0; j < NAMELEN ; j++) kpp_name[j]='\0';
111 int n=0;
112 in_comment = 0;
113 for(j = 0; j < NAMELEN ; j++) newln[j]='\0';
115 while ( inln[n] != '\0' ){
116 if (inln[n] == '!') {
117 in_comment = 1;
119 if ( !in_comment ) {
120 newln[n] = inln[n];
122 n++;
126 if ( DEBUGR == 1 ) printf(" n %s ", newln );
127 if ( !in_comment && strlen(newln) > 1 ) {
128 n=0;
129 while ( newln[n] != '\0' ){
130 i=0;
132 while ( newln[n] != ' ' ){
133 wrf_name[i]=newln[n];
134 i++;
135 n++;
138 n++;
140 i=0;
141 while ( newln[n] != ' ' && newln[n] != '\0'){
142 if (newln[n] != '\n'){
143 kpp_name[i]=newln[n];
145 i++;
146 n++;
155 make_upper_case(wrf_name);
157 make_upper_case(kpp_name);
159 /* wrf_name=strtok(newln, " ");
160 kpp_name=strtok(NULL, " "); */
161 printf(" kpp_name, wrf_name %s %s \n ", kpp_name, wrf_name );
165 /* check whether wrf_name was found in Registry */
166 /* either as associated 4D chem var in a package */
168 got_it=0;
169 for ( pm2 = p2 -> members; pm2 != NULL ; pm2 = pm2->next ) {
171 strcpy( name2, pm2->name );
172 make_upper_case(name2);
174 if ( DEBUGR > 1 ) {
175 fprintf(stderr,"comp %s %s \n",pm2->name, wrf_name);
177 if ( strcmp(name2, wrf_name) == 0 ) {
178 got_it=1;
188 /* (b) or declared as a non-transported radical */
191 p3 = WRFC_radicals;
194 for ( pm3 = p3 -> members; pm3 != NULL ; pm3 = pm3->next ) {
197 strcpy( name3, pm3->name );
198 make_upper_case(name3);
201 if ( DEBUGR > 1 ) {
202 fprintf(stderr,"comp ra %s %s \n",name3, wrf_name);
204 if ( strcmp(name3, wrf_name) == 0 ) {
205 got_it=1;
213 if ( got_it != 1 ) {
214 fprintf(stderr, "ERROR: variable name %s was found in file %s but not in the Registry \n", wrf_name, equivfilename);
215 exit (0);
219 /*-------------------------------------------------*/
221 /* check whether kpp_name was found in species file and
222 store name from wrf_kpp_equiv file in assoc_wrf_name */
223 got_it=0;
224 for ( pm1 = p1 -> members; pm1 != NULL ; pm1 = pm1->next ) {
227 strcpy( name1, pm1->name );
228 make_upper_case(name1);
230 if ( strcmp(name1, kpp_name) == 0 ) {
231 got_it=1;
232 pm1->found_match = 1;
233 strcpy( pm1->assoc_wrf_name, wrf_name);
237 /* still have to check if it is a radical .. */
239 for ( pm3 = p3 -> members; pm3 != NULL ; pm3 = pm3->next ) {
242 strcpy( name3, pm3->name );
243 make_upper_case(name3);
246 if ( DEBUGR > 1 ) {
247 fprintf(stderr,"comp ra %s %s \n",name3, wrf_name);
249 if ( strcmp(name3, wrf_name) == 0 ) {
251 pm1->is_radical = 1;
257 if ( DEBUGR == 1 ) {
258 fprintf(stderr,"matching %s %s %i \n",pm1->name, kpp_name, got_it);
261 if ( DEBUGR > 1 ) {
262 fprintf(stderr,"comp %s %s %i \n",pm1->name, kpp_name, got_it);
270 if ( got_it != 1 ) {
271 fprintf(stderr, "ERROR: variable name %s was not found in species file in directory %s/%s but it was found in %s \n", kpp_name, kpp_dirname, p2->name, equivfilename);
272 exit (0);
283 /* compare compound names, use variable */
284 for ( pm1 = p1 -> members; pm1 != NULL ; pm1 = pm1->next ) {
287 if ( DEBUGR == 1 ) {
288 fprintf(stderr, " SEARCHING FOR %s \n", pm1->name );
292 strcpy( name1, pm1->name );
293 make_upper_case(name1);
295 for ( pm2 = p2 -> members; pm2 != NULL ; pm2 = pm2->next ) {
298 strcpy( name2, pm2->name );
299 make_upper_case(name2);
303 if ( strcmp (name1, name2) == 0) {
304 /* store matching name of WRF var in members of KPP_packs */
305 strcpy( pm1->assoc_wrf_name, pm2->name);
306 pm1->found_match = 1;
307 if ( DEBUGR == 1 ) {
308 fprintf(stderr, " matching names: %s %s \n", pm1->name, pm1->assoc_wrf_name);
314 /* the same for radicals */
315 if ( pm1->found_match != 1 ) {
317 if ( DEBUGR == 1 ){
318 fprintf(stderr, " STILL SEARCHING FOR %s \n", pm1->name );
320 p3 = WRFC_radicals;
323 for ( pm3 = p3 -> members; pm3 != NULL ; pm3 = pm3->next ) {
326 strcpy( name3, pm3->name );
327 make_upper_case(name3);
330 /* fprintf(stderr, " comparing radicals %s %s \n", name1, name3); */
332 if ( strcmp (name1, name3) == 0) {
333 /* store matching name of WRF var in members of KPP_packs */
334 strcpy( pm1->assoc_wrf_name, pm3->name);
335 pm1->found_match = 1;
336 pm1->is_radical = 1;
337 if ( DEBUGR == 1 ) {
338 fprintf(stderr, " matching radical name : %s %s \n", pm1->name, pm1->assoc_wrf_name);
348 /* special handling for fixed species */
350 p1 -> got_air = 0;
351 p1 -> got_o2 = 0;
352 p1 -> got_n2 = 0;
353 p1 -> got_co2 = 0;
354 got_h2o = 0;
355 /* take care of water, third body, N2, O2 */
356 for ( pm1 = p1 -> members; pm1 != NULL ; pm1 = pm1->next ) {
357 strcpy( name1, pm1->name );
358 make_upper_case(name1);
359 if ( strcmp (name1, kpp_third_body) == 0) {
360 pm1->found_match = 2;
361 strcpy( pm1->assoc_wrf_name, "THIRD BODY");
362 p1 -> got_air = 1;
364 if ( strcmp (name1, kpp_h2o) == 0) {
365 pm1->found_match = 2;
366 strcpy( pm1->assoc_wrf_name, "WATER VAPOR");
367 got_h2o = 1;
369 if ( strcmp (name1, kpp_n2) == 0) {
370 pm1->found_match = 2;
371 strcpy( pm1->assoc_wrf_name, "MOLECULAR NITROGEN");
372 p1 -> got_n2 = 1;
374 if ( strcmp (name1, kpp_o2) == 0) {
375 pm1->found_match = 2;
376 strcpy( pm1->assoc_wrf_name, "MOLECULAR OXYGEN");
377 p1 -> got_o2 = 1;
379 if ( strcmp (name1, kpp_co2) == 0) {
380 pm1->found_match = 2;
381 strcpy( pm1->assoc_wrf_name, "CO2");
382 p1 -> got_co2 = 1;
386 if ( p1 -> got_air != 1 ) {
387 fprintf(stderr, "ERROR: variable name for third body in KPP species file is expected to be %s, but was not found in %s species file \n", kpp_third_body, p2->name);
388 /* exit (0); */
391 if ( got_h2o != 1 ) {
392 fprintf(stderr, "ERROR: variable name for water in KPP species file is expected to be %s, but was not found in %s species file\n", kpp_h2o, p2->name);
393 /* exit (0); */
404 /* now in screen_out
405 for ( p1 = KPP_packs ; p1 != NULL ; p1 = p1->next ) {
406 p2 = p1->assoc_wrf_pack;
407 if ( p2 ) {
408 fprintf(stderr, "1 MATCHING PACKS: %s \n", p2->name);
409 for ( pm1 = p1 -> members; pm1 != NULL ; pm1 = pm1->next ) {
411 if ( pm1 -> found_match == 1 ) {
412 fprintf(stderr, " 1 found %s %s \n", pm1->name, pm1 -> assoc_wrf_name );
414 else if ( pm1 -> found_match == 2 ) {
415 fprintf(stderr, " 1 found %s %s \n", pm1->name, pm1 -> assoc_wrf_name );
417 else {
418 fprintf(stderr, " 0 NOT found %s \n", pm1->name );
419 exit (0);
430 return(0) ;