if not modules then modules = { } end modules ['t-readdata'] = { version = "0.13", comment = "Readdata", author = "Tomáš Hála", copyright = "Tomáš Hála, 2020", email = "thala@mendelu.cz", license = "GNU General Public License" } ------------------------------ ctx = context ------------------------------ local err = function (kod) local msg={ECOL="Error in column name", INCN="Incorrect area (column numbers)", MDSN="Missing dataset name", MFNM="Missing file name", MKEY="Missing key data", NDTS="No dataset of this name", RCND="No row or column defined",} ctx("\\errmessage{"..msg[kod]..".}") inspect("\\errmessage{"..msg[kod]..".}") end ------------------------------ thirddata = thirddata or {} thirddata.readdata = thirddata.readdata or {} local d = thirddata.readdata d.methods,d.sepdefault={},"," ------------------------------ function d.readdata (keyvals) local kv = utilities.parsers.settings_to_hash(keyvals) local nocurves = not kv.curve and not kv.curves d.method = (kv.method or "plain"):strip() local data = kv.data or d.method:strip()=="dataset" or d.method:strip()=="installdataset" if not data and nocurves then err("MKEY") end -- local list = {"c","r","cx","cy","rx","ry" } for i=1,#list do d[list[i]]=nil if kv[list[i]] then d[list[i]]=kv[list[i]]:strip() end end d.data,d.curves,d.dataset={},{},d.dataset or {} d.decimal = kv.decimal or "dot" d.sep = kv.sep or d.sepdefault d.xlabels=(kv.xlabels or ""):split(d.sep) d.ylabels=(kv.ylabels or ""):split(d.sep) if kv.curves then d.curves=(kv.curves:strip()):split(",") end for i=1,#d.curves do d.curves[i]=d.curves[i]:sub(2,-2) end if kv.curve then table.insert(d.curves,1,kv.curve) end if data then d.methods[d.method](kv) end --inspect(":::") inspect(d) os.exit() return d end local function string2array (r,sep,decimal) local x = r:split(sep) local d={} for i=1,#x do d[i]=x[i]:strip() local ok=d[i]:find("^[+-]*%d+,%d+e?%d+$") if decimal=="comma" and ok==1 then local x=d[i]:gsub(',','.') d[i]=tonumber(x) else d[i]=tonumber(d[i]) or d[i] end end return d end local function readfile(fn,sep,decimal) local f,d,i = io.input(fn),{},0 for line in f:lines() do i=i+1 d[i]=string2array(line,sep,decimal) end f:close() return d end local function columntonumber(r,pos) pos=pos or r:len() c=tonumber(r:sub(pos+1)) or nil r=('@'..r):sub(pos,pos+1) -- max 2 letters r=tonumber((r:byte(1)-64)*26+(r:byte(2)-64)) return r,c end local function maxdata(i,ds) if i then return #ds[1] else return #ds end end ------------------------------ d.methods.plain = function (kv) d.data[1]=string2array(kv.data,d.sep,d.decimal) --inspect(">>>") inspect(d) os.exit() end d.methods.joined = function (kv) local x = kv.data:split(d.sep) -- local d = documentdata.statcharts j,k=0,0 for i=1,#x do k = k + 1 if k == 1 then j=j+1 d.data[j]={} end d.data[j][k]=tonumber(x[i]:strip()) if k == tonumber(kv.per) then k=0 end end -- return d end d.methods.struct = function (kv) -- local dd = documentdata.statcharts local transpose = (kv.r or ""):strip() =="c" or (kv.c or ""):strip()=="r" local kd = kv.data:strip() kd = kd:sub(2,-2) local x = kd:split("}"..d.sep.."{") for i=1,#x do d.data[i]=x[i]:split(d.sep) for j=1,#d.data[i] do d.data[i][j]=tonumber(d.data[i][j]:strip()) end end if transpose then local xx=d.data d.data={} for i=1,#xx[1] do d.data[i]={} for j=1,#xx do d.data[i][j]=xx[j][i] end end end end d.methods.installdataset = function (kv) if lfs.isfile(kv.filename) then d.dataset[kv.dsname]=readfile(kv.filename,d.sep,d.decimal) else err("MFNM") end end d.methods.dataset = function (kv) if not kv.dsname or kv.dsname:strip() == "" then err("MDSN") end if not kv.rx and not kv.cx then err("RCND") end if kv.filename and lfs.isfile(kv.filename) then d.dataset[kv.dsname]=readfile(kv.filename,d.sep,d.decimal) else if not d.dataset[kv.dsname] then err("NDTS") end end local axis={} axis[1],axis[2]=((d.rx or d.cx):strip()):split(":"),(((d.ry or d.cy) or ""):strip()):split(":") local xx,yy=axis[1],axis[2] for a=1,#axis do local ax=axis[a] for i=1,#ax do local bn=string.find(ax[i],"%d+$") or 0 local bl=ax[i]:find("^[A-Z]+") or 0 if bn ==1 then ax[i]={tonumber(ax[i])} elseif bl ==1 and bn<1 then ax[i]={columntonumber(ax[i])} elseif bl ==1 and bn>1 then ax[i]={columntonumber(ax[i],bn-1)} else err("ECOL") end end end d.data={{},{}} for a=1,#axis do local ax=axis[a] ax[2]=ax[2] or {} ax[2][1]=ax[2][1] or ax[1][1] ax[1][2]=ax[1][2] or 1 ax[2][2]=ax[2][2] or maxdata(kv.rx,d.dataset[kv.dsname]) if ax[1][1]~=ax[2][1] then err("INCN") end for i=ax[1][2],ax[2][2] do local xx,yy,kam=ax[1][1],i,i-ax[1][2]+1 if not kv.rx then xx,yy=i,ax[1][1] end d.data[a][kam]=d.dataset[kv.dsname][xx][yy] end end end return d ------------------------------