#! /usr/bin/gawk -f # Last edited on 2015-07-03 21:33:32 by stolfilocal # Functions to read price series. # Require {useful_functions.gawk} function price_series_read_file \ ( fname,tstep, \ date_ix,time_ix,pop_ix,phi_ix,plo_ix,pcl_ix,vbt_ix,vcr_ix,pav_ix, \ \ nlin,lin,ndata,fld,nfld,dy,tm,dt, \ pop,phi,plo,pcl,vbt,vcr,pav,j,odt \ ) { # Reads a file "{fname}" with price data, total BTC and currency volumes, in {tstep} second intervals. # Stores the data in {date_ix[ix],time_ix[ix],pop_ix[ix],phi_ix[ix],plo_ix[ix],pcl_ix[ix], # vbt_ix[ix],vcr_ix[ix],pav_ix[ix]}, where {ix} is the index of the data line. # The dates and times must be {tstep} seconds apart. # Uses global parameters {ulp_vbt,ulp_vcr,ulp_pav,ulp_phl}. # Returns the number of data lines read. if ((ulp_vbt+0 == 0) || (ulp_vcr+0 == 0) || (ulp_pav == 0) || (ulp_phl == 0)) { prog_error(("undefined precision parameter")); } printf "reading file %s ...\n", fname > "/dev/stderr"; ERRNO = ""; # Read the file: nlin = 0; # Number of lines read. ndata = 0; # Number of non-blank, non-header, non-comment lines. odt = ""; # Date on previous data line. 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 != "") && (! match(lin, /[!]/))) { /* Data line: */ nfld = split(lin, fld, " "); if (nfld != 16) { file_error(fname, nlin, ("wrong field count = \"" lin "\"")); } for (j = 3; j <= NF; j = j + 2) { if (fld[j] != "|") { file_error(fname, nlin, ("missing '|' in column " j ", line = \"" lin "\"")); } } # Get the input fields: dy = usf_check_date(fname,nlin,fld[1]); tm = usf_check_time(fname,nlin,fld[2]); dt = (dy " " tm); # Date and time. pop = usf_check_num(fname, nlin, fld[4]); phi = usf_check_num(fname, nlin, fld[6]); plo = usf_check_num(fname, nlin, fld[8]); pcl = usf_check_num(fname, nlin, fld[10]); vbt = usf_check_num(fname, nlin, fld[12]); vcr = usf_check_num(fname, nlin, fld[14]); pav = usf_check_num(fname, nlin, fld[16]); # Consistency checks: if ((odt != "") && (! usf_datetimes_are_consecutive(odt,dt,tstep))) { file_error(fname,nlin, ("non-consecutive datetimes \"" odt "\" \"" dt "\"")); } odt = dt; if (pav != 0) { # Adjust {vcr} to be consistent with {vbt,pav}: vcr = pav*vbt; } usf_check_prices(fname,nlin, pop,phi,plo,pcl,vbt,vcr,pav, ulp_phl,ulp_vbt,ulp_vcr,ulp_pav); # Save in arrays: date_ix[ndata] = dy; time_ix[ndata] = tm; pop_ix[ndata] = pop; phi_ix[ndata] = phi; plo_ix[ndata] = plo; pcl_ix[ndata] = pcl; vbt_ix[ndata] = vbt; vcr_ix[ndata] = vcr; pav_ix[ndata] = pav; ndata++; } } if ((ERRNO != "0") && (ERRNO != "")) { file_error(fname, nlin, ERRNO); } close (fname); if (nlin == 0) { arg_error(("file \"" fname "\" empty or missing")); } printf "%6d lines read\n", nlin > "/dev/stderr" printf "%6d data lines found (%s %s -- %s %s)\n", \ ndata, \ date_ix[0], time_ix[0], \ date_ix[ndata-1], time_ix[ndata-1] > "/dev/stderr"; return ndata; }