#! /usr/bin/gawk -f # Last edited on 2015-02-21 20:58:38 by stolfilocal # Functions for reading a list of price files # e.g. for computing relative prices. # To be loaded with "-f" into other gawk programs. function ixf_initialize_index_tables() { # Initializes global tables that describe the files to be merged. # The tables are indexed {0..nfiles-1}: # Namely: split("", inidate_fi); # Nominal start date of file. split("", findate_fi); # Nominal end date of file. split("", extag_fi); # Tag of exchange ("MGOX", "OKCO", etc). split("", crtag_fi); # Currency symbol ("USD", "CNY", etc.). split("", exname_fi); # Name of exchange. split("", rate_fi); # Default units of {crtag_fi[kf]} worth 1 USD. split("", rlodate_fi); # Initial date of range to consider. split("", rhidate_fi); # Final date of range to consider. split("", color_fi); # Color to use in plots. } function ixf_read_index_file \ ( fname, \ inidate_fi,findate_fi,extag_fi,crtag_fi,exname_fi,rate_fi,rlodate_fi,rhidate_fi,color_fi, \ nlin,lin,nfiles,fld,nfld,extag,crtag,exname,rate,rlodate,rhidate,color \ ) { # Reads the index file that describes the data files # to be merged. Each line of the index file must contain: # # "{INIDATE} {FINDATE} {EXTAG} {CRTAG} {EXNAME} {RATE} {RLODATE} {RHIDATE} {COLOR}" # # This line describes the file whose nominal date span is {INIDATE .. FINDATE}, # for the exchange whose tag is {EXTAG} (e.g. "MGOX"), with trade currency {CRTAG} (e.g. "CNY). # The {EXNAME} (a string without blanks) is for documentation purposes only, # and the {RATE} may be used in price plots. The file is to be clipped to the range # {RLODATE .. RHIDATE}, inclusive both. The {COLOR} may be used in plots. # # Those fields are returned in the arguments {inidate_fi,findate_fi,extag_fi}, etc. # which should be pre-allocated arrays, indexed {0..nfiles-1}: # Retuns the number of files {nfiles}. nlin = 0; # Number of lines read. nfiles = 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 != 9) { file_error(fname, nlin, ("bad file index entry = \"" lin "\"")); } kf = nfiles; inidate_fi[kf] = usf_check_date(fname,nlin,fld[1]); findate_fi[kf] = usf_check_date(fname,nlin,fld[2]); extag_fi[kf] = fld[3]; crtag_fi[kf] = fld[4]; ex_name_fi[kf] = fld[5]; rate_fi[kf] = usf_check_num(fname,nlin,fld[6]); rlodate_fi[kf] = usf_check_date(fname,nlin,fld[7]); rhidate_fi[kf] = usf_check_date(fname,nlin,fld[8]); color_fi[kf] = fld[9]; nfiles++; } } if ((ERRNO != "0") && (ERRNO != "")) { file_error(fname, nlin, ERRNO); } close (fname); if (nlin == 0) { arg_error(("file \"" fname "\" empty or missing")); } printf "%6d index file lines read\n", nlin > "/dev/stderr"; printf "%6d data files found\n", nfiles > "/dev/stderr"; return nfiles; }