# awk script to parse the model parameter file and generate a chunk of C containing # the same parameters. # The model parameter file looks like this: # # IGRF00 1900.00 10 0 0 1900.00 1905.00 -1.0 600.0 IGRF00 0 # 0 1-31543.0 0.0 0.0 0.0 IGRF00 1 # 1 1 -2298.0 5922.0 0.0 0.0 IGRF00 2 # 0 2 -677.0 0.0 0.0 0.0 IGRF00 3 # 1 2 2905.0 -1061.0 0.0 0.0 IGRF00 4 # # This shows the start of the 1900-1905 model; it contains 66 parameters and is followed # by the 1905-1910 model with its own header line, etc. # # Note that the fields are not always space-delimited e.g. the "1-31543.0" in the first # parameter line above. The C code seems to treat the first two columns specially to allow # for this: it reads 2 chars, then 2 chars, then the rest of the line. Instead, we'll # pre-process the file to insert another space after the first 4 characters of each line; # we can then treat it as space-delimited. (We can almost get away with inserting this space # on the header lines, but not quite; the post-2000 headers have only three spaces before # $1 starts.) # # The two columns of zeros seen above are differential terms; they are only non-zero # for the last model which is used for extrapolation. This can be identified by a non-zero # max2 field in its header. BEGIN { print "struct model models[] = {" > "models.c"; first=1; } # Match a header line; it starts with 3 spaces: # Field names from geomag61.c line 677. /^ / { if (!first) print "};"; first=0; model=$1; epoch=$2; max1=$3; max2=$4; max3=$5; yrmin=$6; yrmax=$7; altmin=$8; altmax=$9; have_diffs=(max2!=0); printf("{%f,%d,%d,%d,%f,%f,%f,%f,%s_params,%s},\n",epoch,max1,max2,max3,yrmin,yrmax,altmin,altmax,model,have_diffs?(model "_diffs"):"NULL") >> "models.c"; printf("float %s_params[] = {",model); if (have_diffs) printf("float %s_diffs[] = {",model) > "diffs.c"; } # Match a parameter line. # Field names from geomag61.c lines 1783 to 1806. # Each line has both g and hh values which are both output, except when m is zero; in this # case only g is needed. !/^ / { m=$1; n=$2; g=$3; hh=$4; dg=$5; dhh=$6; irat=$7; line_num=$8; printf("%f,",g); if (m!=0) printf("%f,",hh); if (max2!=0) { printf("%f,",dg) >> "diffs.c"; if (m!=0) printf("%f,",dhh) >> "diffs.c"; } } END { print "};"; print "};" >> "models.c"; print "};" >> "diffs.c"; }