#! /usr/bin/gawk -f # Last edited on 2015-03-07 01:38:00 by stolfilocal # Functions for reading a list of bubble parameters # To be loaded with "-f" into other gawk programs. function buf_initialize_bubble_parms_tables() { # Initializes global tables that describe the files to be merged. # The tables are indexed {0..nbubs-1}: # Namely: split("", diup_kb); # Day of start of rally (given). split("", rtup_kb); # Daily rate of increase during rally (given). split("", dfup_kb); # Approximate day of end of increase (given). split("", didn_kb); # Approximate day of start of decrease (given). split("", rtdn_kb); # Daily rate of decay after peak (given). split("", dfdn_kb); # Day of end of dominance (given). split("", btag_kb); # Tag of bubble, eg. for plot keys. split("", color_kb); # Color to use in plots. split("", pmid_kb); # Nominal peak price (computed). } function buf_read_bubble_parms_file \ ( fname, \ diup_kb,rtup_kb,dfup_kb,didn_kb,rtdn_kb,dfdn_kb,btag_kb,color_kb, \ nlin,lin,nbubs,fld,nfld \ ) { # Reads the index file that describes the bubbles in the model # Each line of the index file must contain: # # "{DIUP} {RTUP} {DFUP} {DIDN} {RTDN} {DFDN} {BTAG} {COLOR}" # # This line describes the bubble that starts to be noticeable at # date {DIUP}, grows with daily rate {RTUP}, stops increasing at # {DFUP}, is flat until {DIDN}, decays with daily rate {RTDN} and # ceases to be relevant at {DFDN}. The {BTAG} is a unique tag string # (no blanks), for file names and plot key labels. The {COLOR} may # be used in plots. # # Those fields are returned in the arguments {diup_kb,rtup_kb,rtdn_kb}, etc. # which should be pre-allocated arrays, indexed {0..nbubs-1}: # Retuns the number of files {nbubs}. printf "reading file %s ...\n", fname > "/dev/stderr"; ERRNO = ""; nlin = 0; # Number of lines read. nbubs = 0; # Number of relevant lines. while((getline lin < fname) > 0) { nlin++; # Remove tabs, inline comments, spurious blanks gsub(/[\011]/, " ", lin); gsub(/[\#].*$/, "", lin); gsub(/^[ ]+/, "", lin); gsub(/[ ]+$/, "", lin); gsub(/[ ][ ]+/, " ", lin); if (lin != "") { nfld = split(lin, fld, " "); if (nfld != 8) { file_error(fname, nlin, ("bad file index entry = \"" lin "\"")); } kb = nbubs; diup_kb[kb] = usf_check_date(fname,nlin,fld[1]); rtup_kb[kb] = usf_check_num(fname,nlin,fld[2]); dfup_kb[kb] = usf_check_date(fname,nlin,fld[3]); didn_kb[kb] = usf_check_date(fname,nlin,fld[4]); rtdn_kb[kb] = usf_check_num(fname,nlin,fld[5]); dfdn_kb[kb] = usf_check_date(fname,nlin,fld[6]); btag_kb[kb] = fld[7]; color_kb[kb] = fld[8]; if ((diup_kb[kb] >= dfup_kb[kb])) { file_warning(fname,nlin,("bad rise date interval \"" diup_kb[kb] "\" \"" dfup_kb[kb] "\"")); } if ((dfup_kb[kb] > didn_kb[kb])) { file_error(fname,nlin,("bad flat date interval \"" dfup_kb[kb] "\" \"" didn_kb[kb] "\"")); } if ((didn_kb[kb] >= dfdn_kb[kb])) { file_warning(fname,nlin,("bad drop date interval \"" didn_kb[kb] "\" \"" dfdn_kb[kb] "\"")); } nbubs++; } } if ((ERRNO != "0") && (ERRNO != "")) { file_error(fname, nlin, ERRNO); } close (fname); if (nlin == 0) { arg_error(("file \"" fname "\" empty or missing")); } printf "%6d file lines read\n", nlin > "/dev/stderr"; printf "%6d bubbles found\n", nbubs > "/dev/stderr"; return nbubs; }