updated top-level README and version_decl for V4.5 (#1847)
[WRF.git] / chem / KPP / util / wkc / get_kpp_chem_specs.c
blob318c21970f7a4c3b1263a53d2f770168c2244451
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"
16 #define DEBUGR 0
18 int
19 get_kpp_chem_specs ( char* kpp_dirname )
23 knode_t * q , * member ;
24 DIR * dir;
25 struct dirent * entry;
26 struct stat dir_stat;
27 char fulldirname[NAMELEN], spcfilename[NAMELEN];
28 char inln[NAMELEN], kpp_spec[NAMELEN];
29 FILE * spcFile;
30 int in_comment, got_it;
34 /* http://users.actcom.co.il/~choo/lupg/tutorials/handling-files/handling-files.html#directory_struct */
38 dir = opendir(kpp_dirname);
39 if (!dir) {
40 fprintf(stderr, "WARNING from gen_kpp: Cannot read directory: %s \n", kpp_dirname);
41 perror("");
42 return;
46 /* loop through sub directories in KPP directory */
48 while ((entry = readdir(dir))) {
49 if (entry->d_name ) {
51 if ( strcmp(entry->d_name, ".") == 0)
52 continue;
53 if ( strcmp(entry->d_name, "..") == 0)
54 continue;
58 sprintf( fulldirname, "%s/%s", kpp_dirname, entry->d_name);
60 printf("%s \n", fulldirname );
62 /* check if the given entry is a directory. */
63 if (stat(fulldirname, &dir_stat) == -1) {
64 fprintf(stderr, "WAA\n\n");
65 perror("WARNING from gen_kpp: ");
66 continue;
70 /* check if KPP species file is present. */
72 sprintf( spcfilename, "%s/%s/%s.spc", kpp_dirname, entry->d_name, entry->d_name);
75 ;fprintf(stderr, " spcfilename: %s \n",spcfilename);
77 spcFile = fopen (spcfilename, "r" );
79 if ( spcFile == NULL ) {
80 fprintf(stderr,"WARNING from gen_kpp: File %s not found. Skipping. \n", spcfilename);
81 continue;
84 printf(" Found %s \n", spcfilename );
89 /*----------------------------------------------------*/
92 /* put KPP packagename into linked list */
94 q = new_knode( );
95 q->next = NULL ;
96 strcpy( q->name, entry->d_name );
97 add_knode_to_end( q , &(KPP_packs) ) ;
99 /* loop over lines in KPP species file */
100 while ( fgets ( inln , NAMELEN , spcFile ) != NULL ){
101 if ( DEBUGR == 1 ){ printf("%s ", inln); }
102 /* strip from comments (loop through letters) */
103 int n=0;
104 int nn = 0;
105 int j;
106 in_comment = 0;
107 got_it = 0;
109 for(j = 0; j < NAMELEN ; j++) kpp_spec[j]='\0';
110 while ( inln[n] != '\0' ){
111 if ( inln[n] == '{') in_comment=1;
112 if ( in_comment == 0 ) {
113 if (inln[n] == '=' || inln[n] == '#') {
114 got_it=1;
116 if ( got_it == 0 && inln[n] != ' '){
117 /* printf("%c %i \n ", inln[n], in_comment ); */
119 kpp_spec[nn]=inln[n];
120 nn++;
125 if (inln[n] == '}') in_comment=0;
126 n++;
130 /* printf("spec: %s \n ", kpp_spec); */
132 if (kpp_spec[0] != '\0' && got_it == 1 ) {
134 if ( DEBUGR == 1 ){
135 printf("spec: %s \n ", kpp_spec);
136 fprintf(stderr," p, name %s %s \n", q->name, kpp_spec );
139 member = new_knode( ) ;
140 strcpy( member->name , kpp_spec ) ;
141 member->next = NULL ;
142 add_knode_to_end( member , &(q->members) ) ;
149 fclose(spcFile);
155 return(0) ;