updated top-level README and version_decl for V4.5 (#1847)
[WRF.git] / chem / KPP / util / wkc / tuv_kpp.c
blobcd878c5df3107a4cf110695a7bd03b29c2e2ea03
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #include <sys/resource.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <strings.h>
9 #define NAMELEN 132
10 #define JLEN 32
12 typedef struct tuv_node_struct {
13 char name[NAMELEN];
14 int ndx;
15 int dup;
16 struct tuv_node_struct *next;
17 } tuv_node;
19 typedef struct wrf_node_struct {
20 char name[NAMELEN];
21 int ndx;
22 struct wrf_node_struct *next;
23 struct tuv_node_struct *tuv_node;
24 } wrf_node;
26 int tuv_match( wrf_node *head, char *match_name );
28 int main( int argc, char *argv[], char *env[] ) {
29 char fname_in[NAMELEN], dir[NAMELEN], fname_inc[NAMELEN];
30 char inln[NAMELEN], outln[NAMELEN], piece[JLEN];
31 char squezzed[NAMELEN];
32 char *wrf_jname, *cwrk, *tuv_jspec, *token;
33 char *tuv_jname;
34 FILE * fp_in, *fp_set, *fp_def;
35 wrf_node *Wrf_node;
36 wrf_node *Wrf_HEAD;
37 tuv_node *Tuv_node, *Tuv_node_wrk;
39 int l, m, n, nwrf, ntuv, nltuv;
40 int j_o2_ndx;
42 char mech[NAMELEN];
44 strcpy( fname_in , "" ) ;
46 argv++;
47 strcpy( mech,*argv );
49 fprintf(stderr,"tuv_kpp: Argument = %s\n",mech);
50 // open and write inc files
52 if( !strcmp(mech,"LAST") )
53 strcpy(fname_inc,"../../inc/tuv2wrf_jvals.inc");
54 else
55 strcpy(fname_inc,"../../../../inc/tuv2wrf_jvals.inc");
56 if( (fp_set = fopen( fname_inc,"a" )) == NULL ) {
57 fprintf(stderr,"Can not open %s\n",fname_inc );
58 return(-1);
60 if( !strcmp(mech,"LAST") )
61 strcpy(fname_inc,"../../inc/tuvdef_jvals.inc");
62 else
63 strcpy(fname_inc,"../../../../inc/tuvdef_jvals.inc");
64 if( (fp_def = fopen( fname_inc,"a" )) == NULL ) {
65 fprintf(stderr,"Can not open %s\n",fname_inc );
66 return(-1);
69 if( !strcmp(mech,"FIRST") ) {
70 fprintf(fp_set," select case( config_flags%%chem_opt )\n");
71 fprintf(fp_def," select case( config_flags%%chem_opt )\n");
73 else if( !strcmp(mech,"LAST") ) {
74 fprintf(fp_set," end select\n");
75 fprintf(fp_def," end select\n");
77 else {
78 fprintf(stderr,"tuv_kpp: Mechanism = %s\n",mech);
79 sprintf( fname_in,"%s.tuv.jmap",mech );
80 if( (fp_in = fopen( fname_in,"r" )) == NULL ) {
81 fprintf(stderr,"File %s does not exist\n",fname_in );
82 return(-1);
85 Wrf_node = (wrf_node *)malloc( sizeof(wrf_node) );
86 if( Wrf_node == NULL ) {
87 fprintf(stderr,"Failed to allocate Wrf_node\n");
88 return(-1);
90 Wrf_HEAD = Wrf_node;
92 nwrf = 0; ntuv = 0;
93 while( fgets( inln,NAMELEN,fp_in ) != NULL ) {
94 if( nwrf > 0 ) {
95 Wrf_node->next = (wrf_node *)malloc( sizeof(wrf_node) );
96 if( Wrf_node == NULL ) {
97 fprintf(stderr,"Failed to allocate Wrf_node\n");
98 return(-1);
100 Wrf_node = Wrf_node->next;
102 // remove white space from input line
103 l = 0;
104 for( m = 0; m < strlen( inln ); m++ ) {
105 if( inln[m] != ' ' )
106 squezzed[l++] = inln[m];
108 squezzed[l-1] = '\0';
109 tuv_jspec = index( squezzed,':' );
110 if( tuv_jspec == NULL ) {
111 fprintf(stderr,"Input j mapping is invalid\n");
112 return(-1);
114 *tuv_jspec = '\0';
115 tuv_jspec++;
116 strcpy( Wrf_node->name,squezzed );
117 nwrf++;
118 Wrf_node->ndx = nwrf;
119 token = strtok( tuv_jspec,"+" );
120 nltuv = 0;
121 for( ;; ) {
122 if( token != NULL ) {
123 Tuv_node_wrk = (tuv_node *)malloc( sizeof(tuv_node) );
124 if( Tuv_node_wrk == NULL ) {
125 fprintf(stderr,"Failed to allocate Tuv_node\n");
126 return(-1);
128 strcpy( Tuv_node_wrk->name,token );
129 n = tuv_match( Wrf_HEAD, token );
130 if( n == 0 ) {
131 ntuv++;
132 Tuv_node_wrk->ndx = ntuv;
134 else {
135 Tuv_node_wrk->dup = 1;
136 Tuv_node_wrk->ndx = n;
138 if( nltuv == 0 ) Wrf_node->tuv_node = Tuv_node_wrk;
139 else Tuv_node->next = Tuv_node_wrk;
140 Tuv_node = Tuv_node_wrk;
141 token = strtok( NULL,"+" );
142 nltuv++;
144 else break;
148 fclose( fp_in );
150 // enumerate the wrf jspecs
152 // fprintf(stderr,"\n");
153 // fprintf(stderr,"WRF photo rates\n");
154 // for( Wrf_node = Wrf_HEAD; Wrf_node != NULL; Wrf_node = Wrf_node->next ) {
155 // fprintf(stderr,"%s\n",Wrf_node->name);
156 // }
158 // enumerate the tuv jspecs
160 // fprintf(stderr,"\n");
161 // fprintf(stderr,"TUV photo rates\n");
162 // for( Wrf_node = Wrf_HEAD; Wrf_node != NULL; Wrf_node = Wrf_node->next ) {
163 // for( Tuv_node = Wrf_node->tuv_node; Tuv_node != NULL; Tuv_node = Tuv_node->next ) {
164 // fprintf(stderr,"%s\n",Tuv_node->name);
165 // }
168 // write inc files
170 fprintf(fp_set," case( %s_kpp )\n",mech);
171 fprintf(fp_def," case( %s_kpp )\n",mech);
172 n = 0;
173 j_o2_ndx = 0;
174 for( Wrf_node = Wrf_HEAD; Wrf_node != NULL; Wrf_node = Wrf_node->next ) {
175 sprintf(outln," ph_%s(i,kts:kte,j) = ",Wrf_node->name);
176 if( Wrf_node == Wrf_HEAD) {
177 fprintf(fp_def," nj = %d\n",ntuv);
178 fprintf(fp_def," allocate( tuv_jname(nj) )\n");
180 nltuv = 0;
181 for( Tuv_node = Wrf_node->tuv_node; Tuv_node != NULL; Tuv_node = Tuv_node->next ) {
182 if( nltuv == 0 )
183 sprintf(piece,"tuv_prate(kts:kte,%d)",Tuv_node->ndx);
184 else
185 sprintf(piece," + tuv_prate(kts:kte,%d)",Tuv_node->ndx);
186 strcat( outln,piece );
187 nltuv++;
188 if( !Tuv_node->dup ) {
189 n++;
190 fprintf(fp_def," tuv_jname(%d) = '%s'\n",n,Tuv_node->name);
191 if( !strcmp( Tuv_node->name,"j_o2" ) )
192 j_o2_ndx = n;
195 fprintf(fp_set,"%s\n",outln);
197 fprintf(fp_def," j_o2_ndx = %d\n",j_o2_ndx);
199 fclose( fp_set );
200 fclose( fp_def );
201 return(0);
204 int tuv_match( wrf_node *head, char *match_name ) {
206 wrf_node *wrfnode;
207 tuv_node *thisnode;
209 for( wrfnode = head; wrfnode != NULL; wrfnode = wrfnode->next ) {
210 for( thisnode = wrfnode->tuv_node; thisnode != NULL; thisnode = thisnode->next ) {
211 if( !strcmp( thisnode->name,match_name ) )
212 return( thisnode->ndx );
216 return( 0 );