Merge branch 'release-v4.6.0' of github.com:wrf-model/WRF
[WRF.git] / tools / registry.c
blobb2dd0a5f6aebd590a717c2f9bae4dd2ac11ee72f
1 #include <stdio.h>
2 #include <stdlib.h>
3 #ifdef _WIN32
4 # include <io.h>
5 # define rindex(X,Y) strrchr(X,Y)
6 # define index(X,Y) strchr(X,Y)
7 #else
8 # include <sys/time.h>
9 # include <sys/resource.h>
10 # include <unistd.h>
11 # include <string.h>
12 # include <strings.h>
13 #endif
15 #define DEFINE_GLOBALS
16 #include "protos.h"
17 #include "registry.h"
18 #include "data.h"
19 #include "sym.h"
21 // Helper macro to actually do return checks
22 #define EXIT_ON_NONZERO( A ) { int result = A; if ( result != 0 ) { printf( "Error in %s, zero return expected, received %i\n", #A, result ); exit(result); } }
24 /* SamT: bug fix: main returns int */
25 int
26 main( int argc, char *argv[], char *env[] )
28 char fname_in[NAMELEN], dir[NAMELEN], fname_tmp[NAMELEN], command[NAMELEN] ;
29 char fname_wrk[NAMELEN] ;
30 FILE * fp_in, *fp_tmp ;
31 char * thisprog ;
32 char *env_val ;
33 int mypid ;
34 int do_irr_diag ;
35 #ifndef _WIN32
36 struct rlimit rlim ;
37 #endif
39 mypid = (int) getpid() ;
40 strcpy( thiscom, argv[0] ) ;
41 argv++ ;
43 sw_deref_kludge = 0 ;
44 sw_io_deref_kludge = 0 ;
45 sw_3dvar_iry_kludge = 0 ;
46 sw_distrib_io_layer = 1 ;
47 sw_limit_args = 0 ; /* usually set -- except for GRAPS */
48 sw_dm_parallel = 0 ;
49 sw_all_x_staggered = 0 ;
50 sw_move = 0 ;
51 sw_all_y_staggered = 0 ;
52 sw_fort_kludge = 1 ; /* unconditionally true for v3 */
53 sw_dm_serial_in_only = 0 ; /* input and bdy data set is distributed by node 0,
54 other data streams are written to file per process */
55 sw_new_bdys = 0 ;
56 sw_unidir_shift_halo = 0 ;
58 strcpy( fname_in , "" ) ;
60 #ifndef _WIN32
61 rlim.rlim_cur = RLIM_INFINITY ;
62 rlim.rlim_max = RLIM_INFINITY ;
63 setrlimit ( RLIMIT_STACK , &rlim ) ;
64 #endif
66 sym_forget() ;
67 thisprog = *argv ;
68 while (*argv) {
69 if (*argv[0] == '-') { /* an option */
70 if (!strncmp(*argv,"-D",2)) {
71 char * p ;
72 p = *argv ;
73 sym_add(p+2) ;
76 if (!strcmp(*argv,"-DDEREF_KLUDGE")) {
77 sw_deref_kludge = 1 ;
79 if (!strcmp(*argv,"-DIO_DEREF_KLUDGE")) {
80 sw_io_deref_kludge = 1 ;
82 if (!strcmp(*argv,"-DLIMIT_ARGS")) {
83 sw_limit_args = 1 ;
85 if (!strcmp(*argv,"-DMOVE_NESTS")) {
86 sw_move = 1 ;
88 if (!strcmp(*argv,"-DMOVE_NL_OUTSIDE_MODULE_CONFIGURE")) {
89 sw_fort_kludge = 1 ;
91 if (!strcmp(*argv,"-DD3VAR_IRY_KLUDGE")) {
92 #if 0
93 sw_3dvar_iry_kludge = 1 ;
94 #else
95 fprintf(stderr,"WARNING: -DD3VAR_IRY_KLUDGE option obsolete (it is now disabled by default). Ignored.\n") ;
96 #endif
98 if (!strcmp(*argv,"-DALL_X_STAGGERED")) {
99 sw_all_x_staggered = 1 ;
101 if (!strcmp(*argv,"-DALL_Y_STAGGERED")) {
102 sw_all_y_staggered = 1 ;
104 if (!strcmp(*argv,"-DDM_PARALLEL")) {
105 sw_dm_parallel = 1 ;
107 if (!strcmp(*argv,"-DNEW_BDYS")) {
108 sw_new_bdys = 1 ;
110 if (!strcmp(*argv,"-DEM_CORE=1")) {
111 sw_unidir_shift_halo = 1 ;
113 if (!strcmp(*argv,"-DNEW_WITH_OLD_BDYS")) {
114 sw_new_with_old_bdys = 1 ;
116 if (!strcmp(*argv,"-DDISTRIB_IO_LAYER")) {
117 #if 0
118 sw_distrib_io_layer = 1 ;
119 #else
120 fprintf(stderr,"WARNING: -DDISTRIB_IO_LAYER option obsolete (it is now default). Ignored.\n") ;
121 #endif
123 if (!strcmp(*argv,"-DDM_SERIAL_IN_ONLY")) {
124 sw_dm_serial_in_only = 1 ;
126 if (!strncmp(*argv,"-h",2)) {
127 fprintf(stderr,"Usage: %s [-DDEREF_KLUDGE] [-DDM_PARALLEL] [-DDISTRIB_IO_LAYER] [-DDM_SERIAL_IN_ONLY] [-DD3VAR_IRY_KLUDGE] registryfile\n",thisprog) ;
128 exit(1) ;
131 else /* consider it an input file */
133 strcpy( fname_in , *argv ) ;
135 argv++ ;
138 EXIT_ON_NONZERO( gen_io_boilerplate() ); /* 20091213 jm. Generate the io_boilerplate_temporary.inc file */
140 EXIT_ON_NONZERO( init_parser() );
141 EXIT_ON_NONZERO( init_type_table() );
142 EXIT_ON_NONZERO( init_dim_table() );
144 // possible IRR diagnostcis?
146 do_irr_diag = 0;
147 env_val = getenv( "WRF_CHEM" );
148 if( env_val != NULL && !strncmp( env_val, "1", 1 ) ) {
149 env_val = getenv( "WRF_KPP" );
150 if( env_val != NULL && !strncmp( env_val, "1", 1 ) ) do_irr_diag = 1;
152 if( do_irr_diag ) {
153 if( access( fname_in,F_OK ) ) {
154 fprintf(stderr,"Registry program %s does not exist. Ending.\n", fname_in ) ;
155 exit(2) ;
157 { char *e ;
158 strcpy( dir , fname_in ) ;
159 if ( ( e = rindex ( dir , '/' ) ) != NULL ) { *e = '\0' ; } else { strcpy( dir, "." ) ; }
160 sprintf( fname_wrk,"%s/Registry_irr_diag",dir ) ;
162 // fprintf(stderr,"Registry tmp file = %s\n",fname_wrk);
163 sprintf(command,"/bin/cp %s %s\n",fname_in,fname_wrk);
164 // fprintf(stderr,"Command = %s\n",command);
165 if( system( command ) ) {
166 fprintf(stderr,"Could not copy %s to %s\n",fname_in,fname_wrk);
167 exit(2) ;
169 if (( fp_tmp = fopen( fname_wrk , "a" )) == NULL )
171 fprintf(stderr,"Registry program cannot open %s for appending. Ending.\n", fname_tmp ) ;
172 exit(2) ;
174 if( !access( "Registry/registry.irr_diag",F_OK ) ) {
175 sprintf(command,"/bin/rm -f Registry/registry.irr_diag\n");
176 if( system( command ) ) {
177 fprintf(stderr,"Could not remove Registry/registry.irr_diag\n");
178 exit(2) ;
182 int ndx = 0;
183 int retcod;
184 retcod = AppendReg( "mozcart",ndx );
185 if( !retcod ) ndx++;
186 retcod = AppendReg( "t1_mozcart",ndx );
187 if( !retcod ) ndx++;
188 retcod = AppendReg( "mozart_mosaic_4bin",ndx );
189 if( !retcod ) ndx++;
190 retcod = AppendReg( "mozart_mosaic_4bin_aq",ndx );
193 fprintf(fp_tmp,"\n");
194 fprintf(fp_tmp,"include registry.irr_diag\n");
195 fclose(fp_tmp);
196 strcpy( fname_in,fname_wrk );
197 irr_diag_scalar_indices( "inc" );
198 // fprintf(stderr,"fname_in = %s\n",fname_in);
201 if ( !strcmp(fname_in,"") ) fp_in = stdin ;
202 else
203 if (( fp_in = fopen( fname_in , "r" )) == NULL )
205 fprintf(stderr,"Registry program cannot open %s for reading. Ending.\n", fname_in ) ;
206 exit(2) ;
209 sprintf( fname_tmp , "Registry_tmp.%d",mypid) ;
210 if (( fp_tmp = fopen( fname_tmp , "w" )) == NULL )
212 fprintf(stderr,"Registry program cannot open temporary %s for writing. Ending.\n", fname_tmp ) ;
213 exit(2) ;
216 { char *e ;
217 strcpy( dir , fname_in ) ;
218 if ( ( e = rindex ( dir , '/' ) ) != NULL ) { *e = '\0' ; } else { strcpy( dir, "." ) ; }
220 if ( pre_parse( dir, fp_in, fp_tmp ) ) {
221 fprintf(stderr,"Problem with Registry File %s\n", fname_in ) ;
222 goto cleanup ;
224 sym_forget() ;
226 fclose(fp_in) ;
227 fclose(fp_tmp) ;
229 if (( fp_tmp = fopen( fname_tmp , "r" )) == NULL )
231 fprintf(stderr,"Registry program cannot open %s for reading. Ending.\n", fname_tmp ) ;
232 goto cleanup ;
236 EXIT_ON_NONZERO( reg_parse(fp_tmp) );
238 fclose(fp_tmp) ;
240 check_dimspecs();
242 EXIT_ON_NONZERO( gen_state_struct( "inc" ) );
243 EXIT_ON_NONZERO( gen_state_subtypes( "inc" ) );
244 EXIT_ON_NONZERO( gen_alloc( "inc" ) );
245 /* gen_alloc_count( "inc" ) ; */
246 EXIT_ON_NONZERO( gen_dealloc( "inc" ) ) ;
247 EXIT_ON_NONZERO( gen_scalar_indices( "inc" ) ) ;
248 EXIT_ON_NONZERO( gen_module_state_description( "frame" ) ) ;
249 EXIT_ON_NONZERO( gen_actual_args( "inc" ) ) ;
250 EXIT_ON_NONZERO( gen_actual_args_new( "inc" ) ) ;
251 EXIT_ON_NONZERO( gen_dummy_args( "inc" ) ) ;
252 EXIT_ON_NONZERO( gen_dummy_args_new( "inc" ) ) ;
253 EXIT_ON_NONZERO( gen_dummy_decls( "inc" ) ) ;
254 EXIT_ON_NONZERO( gen_dummy_decls_new( "inc" ) ) ;
255 EXIT_ON_NONZERO( gen_i1_decls( "inc" ) ) ;
256 EXIT_ON_NONZERO( gen_namelist_statements("inc") ; )
257 EXIT_ON_NONZERO( gen_namelist_defines ( "inc", 0 ) ) ; /* without dimension statements */
258 EXIT_ON_NONZERO( gen_namelist_defines ( "inc", 1 ) ) ; /* with dimension statements */
259 EXIT_ON_NONZERO( gen_namelist_defaults ( "inc" ) ) ;
260 EXIT_ON_NONZERO( gen_namelist_script ( "inc" ) ) ;
261 EXIT_ON_NONZERO( gen_get_nl_config( "inc" ) ) ;
262 EXIT_ON_NONZERO( gen_config_assigns( "inc" ) ) ;
263 EXIT_ON_NONZERO( gen_config_reads( "inc" ) ) ;
264 EXIT_ON_NONZERO( gen_wrf_io( "inc" ) ) ;
265 EXIT_ON_NONZERO( gen_model_data_ord( "inc" ) ) ;
266 EXIT_ON_NONZERO( gen_nest_interp( "inc" ) ) ;
267 EXIT_ON_NONZERO( gen_nest_v_interp( "inc") ; ) /*KAL added this for vertical interpolation*/
268 EXIT_ON_NONZERO( gen_scalar_derefs( "inc" ) ) ;
269 EXIT_ON_NONZERO( gen_streams("inc") ; )
271 /* this has to happen after gen_nest_interp, which adds halos to the AST */
272 EXIT_ON_NONZERO( gen_comms( "inc" ) ); /* this is either package supplied (by copying a */
273 /* gen_comms.c file into this directory) or a */
274 /* stubs routine. */
276 cleanup:
277 #ifdef _WIN32
278 if( do_irr_diag ) {
279 sprintf(command,"del /F /Q %s\n",fname_wrk );
280 system( command ) ;
282 sprintf(command,"del /F /Q %s\n",fname_tmp );
283 #else
284 if( do_irr_diag ) {
285 sprintf(command,"/bin/rm -f %s\n",fname_wrk );
286 system( command ) ;
288 sprintf(command,"/bin/rm -f %s\n",fname_tmp );
289 #endif
290 return system( command ) ;