From 8b0dd1e2f2fa8ddad10914b49c0110f7ab4ed862 Mon Sep 17 00:00:00 2001 From: Jose Colmenares Date: Thu, 23 Nov 2023 13:39:08 +0100 Subject: [PATCH] change module structure --- seg2js.min.js | 2 +- src/seg2js.src.js | 1003 +++++++++++++++++++++------------------------ 2 files changed, 477 insertions(+), 528 deletions(-) diff --git a/seg2js.min.js b/seg2js.min.js index 609f9af..8029e50 100644 --- a/seg2js.min.js +++ b/seg2js.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).seg2js={})}(this,(function(e){"use strict";e.buildArraysToPlot=function(e,t){if(!e||!t)return void console.log("missing seg2file data");if(e.ntraces!=t.length)return void console.log("Dimensions of header and data do not match");let r=Array(e.ntraces);for(let t=0;te+t*t,0));let a=0;for(let t=1;tl?Math.abs(t[o][s]):l;l=0==l?1:l;let f=0;n[o]={x:[],y:[]},n[o].x=new Array(i),n[o].y=new Array(i);for(s=0;sc?Math.abs(seg2.traces[o].data[r]):c;c=0==c?1:c;r=0;let l=function(e,t,r){if(r>e.length)return t;var a=e[r]/(2*c)+o;if(0==t.length)t.push(a);else{if(Math.sign(e[r])!=Math.sign(e[r-1]))return console.log(t),t;t.push(a)}return r++,l(e,t,r)};do{var a=[];a=l(seg2.traces[o].data,a,r);for(var n=new Array(a.length),s=0;s0?t.push({x:a,y:n,fill:"toself",mode:"lines",fillcolor:"black",line:{width:2,color:"black"}}):t.push({x:a,y:n,mode:"lines",line:{width:2,color:"black"}}):console.log("single point")}while(r{let s=new FileReader,o=e.slice(t.offsets[r],t.offsets[r]+13);s.onload=function(s){let o=s.target.result,i=new DataView(o);try{if("4422"!=i.getUint16(0,!0).toString(16))throw"This is not a trace";let n=i.getUint16(2,!0),s=i.getUint32(4,!0),o=i.getUint32(8,!0),c=i.getUint8(12,!0);if(3==c)throw"The data format of the trace is not recognized. It uses an old 20 bit format used in the SEG-D format.";if(1!=c&&2!=c&&4!=c&&5!=c)throw"The data format of the trace is not recognized";let l=new FileReader,f=e.slice(t.offsets[r]+32,t.offsets[r]+32+n),u={};l.onload=function(e){let r=e.target.result,i=new DataView(r),l=1==t.size_st?e=>i.getUint8(e,!0):e=>i.getUint16(e,!0),f=function(e){var t=e.substr(0,e.indexOf(" "));if(""!=t){var r=e.substr(e.indexOf(" ")+1);switch(t){case"CDP_NUMBER":case"CDP_TRACE":case"CHANNEL_NUMBER":case"DELAY":case"DESCALING_FACTOR":case"END_OF_GROUP":case"FIXED_GAIN":case"LINE_ID":case"NOTCH_FREQUENCY":case"POLARITY":case"RECEIVER_STATION_NUMBER":case"SAMPLE_INTERVAL":case"SHOT_SEQUENCE_NUMBER":case"SKEW":NaN==(r=Number(r))&&(status=!1);break;case"RECEIVER_LOCATION":let e=r.split(" ");if(0==e.length)r=Number(e[0]);else{r=[];for(var a=0;a{!function(a,n){let s=new FileReader,o=e.slice(0,16);s.onload=function(e){var s=e.target.result;try{let e=new DataView(s);if("3a55"!=e.getUint16(0,!0).toString(16))throw"Wrong format. Not a SEG-2 file.";if(t.versionNumber=e.getUint16(2,!0),t.sizeTracePointer=e.getUint16(4,!0),t.ntraces=e.getUint16(6,!0),t.trace=new Array(t.ntraces).fill(r),t.ntraces>t.sizeTracePointer/4)throw"The number of traces is too big for the trace pointer sub-block. The SEG-2 file has errors.";if(t.ntraces<1)throw"The number of traces cannot be less than or equal to zero. The SEG-2 file has errors.";t.size_st=e.getUint8(8,!0),1==t.size_st?t.st=e.getUint8(9,!0):t.st=e.getUint16(9,!0);let n=e.getUint8(11,!0);t.lt=1==n?e.getUint8(12,!0):e.getUint16(12,!0),a()}catch(e){n(e)}},s.readAsArrayBuffer(o)}(a,n)}).then(()=>new Promise((r,a)=>{!function(r,a){let n=new FileReader,s=32+t.sizeTracePointer,o=e.slice(32,s);n.onload=function(e){var n=e.target.result;let s=new DataView(n);try{t.offsets=new Uint32Array(t.ntraces,!0);for(var o=0;onew Promise((r,a)=>{!function(r,a){let n=new FileReader,s=32+t.sizeTracePointer,o=e.slice(s,t.offsets[0]),i=function(e){var r=e.substr(0,e.indexOf(" "));if(""!=r){var a=e.substr(e.indexOf(" ")+1);t.fileDescriptor[r]=a}};n.onload=function(e){try{var n=e.target.result;let a=new DataView(n),l=t.offsets[0]-s,f=[],u=1==t.size_st?e=>a.getUint8(e,!0):e=>a.getUint16(e,!0);for(var o=2;onew Promise((e,r)=>{!async function(e,r){for(var n=0;n{}).catch(e=>(console.log(e),new Error(e)))},e.readTraceData=function(e,t,r,a){return new Promise((n,s)=>{let o=new FileReader,i=t.offsets[r]+t.trace[r].sizeof_tdb,c=i+t.trace[r].sizeof_db,l=e.slice(i,c);o.onload=function(e){let s=e.target.result,o=new DataView(s),i=t.trace[r].nsamples;switch(t.trace[r].dataFormat){case 1:for(var c=0;c{let s=new FileReader,i=e.slice(t.offsets[r],t.offsets[r]+13);s.onload=function(s){let i=s.target.result,o=new DataView(i);try{if("4422"!=o.getUint16(0,!0).toString(16))throw"This is not a trace";let n=o.getUint16(2,!0),s=o.getUint32(4,!0),i=o.getUint32(8,!0),c=o.getUint8(12,!0);if(3==c)throw"The data format of the trace is not recognized. It uses an old 20 bit format used in the SEG-D format.";if(1!=c&&2!=c&&4!=c&&5!=c)throw"The data format of the trace is not recognized";let l=new FileReader,f=e.slice(t.offsets[r]+32,t.offsets[r]+32+n),u={};l.onload=function(e){let r=e.target.result,o=new DataView(r),l=1==t.size_st?e=>o.getUint8(e,!0):e=>o.getUint16(e,!0),f=function(e){var t=e.substr(0,e.indexOf(" "));if(""!=t){var r=e.substr(e.indexOf(" ")+1);switch(t){case"CDP_NUMBER":case"CDP_TRACE":case"CHANNEL_NUMBER":case"DELAY":case"DESCALING_FACTOR":case"END_OF_GROUP":case"FIXED_GAIN":case"LINE_ID":case"NOTCH_FREQUENCY":case"POLARITY":case"RECEIVER_STATION_NUMBER":case"SAMPLE_INTERVAL":case"SHOT_SEQUENCE_NUMBER":case"SKEW":NaN==(r=Number(r))&&(status=!1);break;case"RECEIVER_LOCATION":let e=r.split(" ");if(0==e.length)r=Number(e[0]);else{r=[];for(var a=0;a{!function(a,n){let s=new FileReader,i=e.slice(0,16);s.onload=function(e){var s=e.target.result;try{let e=new DataView(s);if("3a55"!=e.getUint16(0,!0).toString(16))throw"Wrong format. Not a SEG-2 file.";if(t.versionNumber=e.getUint16(2,!0),t.sizeTracePointer=e.getUint16(4,!0),t.ntraces=e.getUint16(6,!0),t.trace=new Array(t.ntraces).fill(r),t.ntraces>t.sizeTracePointer/4)throw"The number of traces is too big for the trace pointer sub-block. The SEG-2 file has errors.";if(t.ntraces<1)throw"The number of traces cannot be less than or equal to zero. The SEG-2 file has errors.";t.size_st=e.getUint8(8,!0),1==t.size_st?t.st=e.getUint8(9,!0):t.st=e.getUint16(9,!0);let n=e.getUint8(11,!0);t.lt=1==n?e.getUint8(12,!0):e.getUint16(12,!0),a()}catch(e){n(e)}},s.readAsArrayBuffer(i)}(a,n)}).then(()=>new Promise((r,a)=>{!function(r,a){let n=new FileReader,s=32+t.sizeTracePointer,i=e.slice(32,s);n.onload=function(e){var n=e.target.result;let s=new DataView(n);try{t.offsets=new Uint32Array(t.ntraces,!0);for(var i=0;inew Promise((r,a)=>{!function(r,a){let n=new FileReader,s=32+t.sizeTracePointer,i=e.slice(s,t.offsets[0]),o=function(e){var r=e.substr(0,e.indexOf(" "));if(""!=r){var a=e.substr(e.indexOf(" ")+1);t.fileDescriptor[r]=a}};n.onload=function(e){try{var n=e.target.result;let a=new DataView(n),l=t.offsets[0]-s,f=[],u=1==t.size_st?e=>a.getUint8(e,!0):e=>a.getUint16(e,!0);for(var i=2;inew Promise((e,r)=>{!async function(e,r){for(var n=0;n{}).catch(e=>(console.log(e),new Error(e)))},readTraceData:function(e,t,r,a){return new Promise((n,s)=>{let i=new FileReader,o=t.offsets[r]+t.trace[r].sizeof_tdb,c=o+t.trace[r].sizeof_db,l=e.slice(o,c);i.onload=function(e){let s=e.target.result,i=new DataView(s),o=t.trace[r].nsamples;switch(t.trace[r].dataFormat){case 1:for(var c=0;ce+t*t,0));let a=0;for(let t=1;tl?Math.abs(t[i][s]):l;l=0==l?1:l;let f=0;n[i]={x:[],y:[]},n[i].x=new Array(o),n[i].y=new Array(o);for(s=0;sc?Math.abs(seg2.traces[i].data[r]):c;c=0==c?1:c;r=0;let l=function(e,t,r){if(r>e.length)return t;var a=e[r]/(2*c)+i;if(0==t.length)t.push(a);else{if(Math.sign(e[r])!=Math.sign(e[r-1]))return console.log(t),t;t.push(a)}return r++,l(e,t,r)};do{var a=[];a=l(seg2.traces[i].data,a,r);for(var n=new Array(a.length),s=0;s0?t.push({x:a,y:n,fill:"toself",mode:"lines",fillcolor:"black",line:{width:2,color:"black"}}):t.push({x:a,y:n,mode:"lines",line:{width:2,color:"black"}}):console.log("single point")}while(r header.sizeTracePointer / 4) + throw "The number of traces is too big for the trace pointer sub-block. The SEG-2 file has errors." + if (header.ntraces < 1) + throw "The number of traces cannot be less than or equal to zero. The SEG-2 file has errors." + + header.size_st = dv.getUint8(8, true); + if (header.size_st == 1) + header.st = dv.getUint8(9, true); + else + header.st = dv.getUint16(9, true); + + let size_lt = dv.getUint8(11, true); + if (size_lt == 1) + header.lt = dv.getUint8(12, true); + else + header.lt = dv.getUint16(12, true); + + resolve(); + } + catch (err) { + reject(err); + } } - }; - - - // export let readHeader = function(file, header) - function readHeader(file, header) - { - if (!file || !header) { - console.log("wrong input"); - return; + reader.readAsArrayBuffer(blob); + } + + let readTracePointerSubblock = function (resolve, reject) { + let reader = new FileReader(); + let finalOffset = 32 + header.sizeTracePointer; + let blob = file.slice(32, finalOffset); + reader.onload = function (e) { + var buffer = e.target.result; + let dv = new DataView(buffer); + try { + header.offsets = new Uint32Array(header.ntraces, true); + for (var ii = 0; ii < header.ntraces; ii++) + header.offsets[ii] = dv.getUint32(ii * 4, true); + resolve(); + } + catch (err) { + reject(err); + } } - - header.ntraces = 0; - header.offsets = []; - header.versionNumber = 0; - header.st = 0; - header.lt = 0; - header.size_st; - header.fileDescriptor = {}; - header.sizeTracePointer = 0; - header.trace = []; - - let traceObject = { - nsamples: 0, - dataFormat: 0, - sizeof_tdb: 0, - sizeof_db: 0, - traceDescriptor: {} + reader.readAsArrayBuffer(blob); + } + + let readFileDescritor = function (resolve, reject) { + let reader = new FileReader(); + let offset2 = 32 + header.sizeTracePointer; + let blob = file.slice(offset2, header.offsets[0]); + + let fileDescriptorParser = function (sentence) { + var key = sentence.substr(0, sentence.indexOf(' ')); + if (key == "") return; + var value = sentence.substr(sentence.indexOf(' ') + 1); + header.fileDescriptor[key] = value; } - - let readFirstPart = function(resolve, reject) - { - let reader = new FileReader(); - let blob = file.slice(0, 16); - reader.onload = function(e) - { - var buffer = e.target.result; - try - { - let dv = new DataView(buffer); - - let blockId = dv.getUint16(0,true); - if (blockId.toString(16)!="3a55"){ - throw "Wrong format. Not a SEG-2 file."; } - - header.versionNumber = dv.getUint16(2,true); - header.sizeTracePointer = dv.getUint16(4,true); - - header.ntraces = dv.getUint16(6,true); - header.trace = new Array(header.ntraces).fill(traceObject); - - if (header.ntraces > header.sizeTracePointer/4) - throw "The number of traces is too big for the trace pointer sub-block. The SEG-2 file has errors." - if (header.ntraces < 1) - throw "The number of traces cannot be less than or equal to zero. The SEG-2 file has errors." - - header.size_st = dv.getUint8(8,true); - if (header.size_st == 1) - header.st = dv.getUint8(9,true); - else - header.st = dv.getUint16(9,true); - - let size_lt = dv.getUint8(11,true); - if (size_lt == 1) - header.lt = dv.getUint8(12,true); - else - header.lt = dv.getUint16(12,true); - - resolve(); - } - catch(err) - { - reject(err); - } - } - reader.readAsArrayBuffer(blob); - } - - let readTracePointerSubblock = function(resolve, reject) - { - let reader = new FileReader(); - let finalOffset = 32 + header.sizeTracePointer; - let blob = file.slice(32, finalOffset); - reader.onload = function(e) - { + + reader.onload = function (e) { + try { var buffer = e.target.result; let dv = new DataView(buffer); - try - { - header.offsets = new Uint32Array(header.ntraces,true); - for (var ii=0; ii { return dv.getUint8(ii, true); } : (ii) => { return dv.getUint16(ii, true); }; + + for (var ii = 2; ii < nchars; ii++) { + var termChar = getTerminateChar(ii); + if (termChar == header.st) { + var wholeSentence = String.fromCharCode.apply(null, wholeSentenceBits); + fileDescriptorParser(wholeSentence); + wholeSentenceBits = []; + ii += 2; + } else { + wholeSentenceBits.push(dv.getUint8(ii, true)); + } } + + resolve(); + } + catch (err) { + reject(err); } - reader.readAsArrayBuffer(blob); } - - let readFileDescritor = function(resolve, reject) - { + reader.readAsArrayBuffer(blob); + } + + let getTraceHeader = function (jj) { + return new Promise((resolve, reject) => { let reader = new FileReader(); - let offset2 = 32 + header.sizeTracePointer; - let blob = file.slice(offset2, header.offsets[0]); - - let fileDescriptorParser = function(sentence) - { - var key = sentence.substr(0,sentence.indexOf(' ')); - if (key == "") return; - var value = sentence.substr(sentence.indexOf(' ')+1); - header.fileDescriptor[key] = value; - } - - reader.onload = function(e) - { - try - { - var buffer = e.target.result; - let dv = new DataView(buffer); - let nchars = header.offsets[0] - offset2; - let wholeSentenceBits = []; - - let getTerminateChar = (header.size_st == 1) ? - (ii) => {return dv.getUint8(ii,true);} : (ii) => {return dv.getUint16(ii,true);}; - - for (var ii=2; ii - { - let reader = new FileReader(); - let blob = file.slice(header.offsets[jj], header.offsets[jj]+13); - reader.onload = function(e) - { - let buffer = e.target.result; - let dv = new DataView(buffer); - try - { - let traceId = dv.getUint16(0,true); - if ( traceId.toString(16)!="4422"){ - throw "This is not a trace";} - let sizeof_tdb = dv.getUint16(2,true); - let sizeof_db = dv.getUint32(4,true); - let nsamples = dv.getUint32(8,true); - let dataFormat = dv.getUint8(12,true); - if ( dataFormat == 3) - throw "The data format of the trace is not recognized. It uses an old 20 bit format used in the SEG-D format." - if ( dataFormat != 1 && dataFormat != 2 && dataFormat != 4 && dataFormat != 5) - throw "The data format of the trace is not recognized" - - let reader2 = new FileReader(); - let blob2 = file.slice(header.offsets[jj] + 32, - header.offsets[jj] + 32 + sizeof_tdb); - let traceDescriptor = {}; - - reader2.onload = function(e) - { - let buffer2 = e.target.result; - let dv2 = new DataView(buffer2); - - let getTerminateChar = (header.size_st == 1) ? - (ii) => {return dv2.getUint8(ii,true);} : (ii) => {return dv2.getUint16(ii,true);}; - - let traceDescriptorParser = function(sentence) - { - var key = sentence.substr(0,sentence.indexOf(' ')); - if (key == "") return; - var value = sentence.substr(sentence.indexOf(' ')+1); - switch(key) { - case "CDP_NUMBER": - case "CDP_TRACE": - case "CHANNEL_NUMBER": - case "DELAY": - case "DESCALING_FACTOR": - case "END_OF_GROUP": - case "FIXED_GAIN": - case "LINE_ID": - case "NOTCH_FREQUENCY": - case "POLARITY": - case "RECEIVER_STATION_NUMBER": - case "SAMPLE_INTERVAL": - case "SHOT_SEQUENCE_NUMBER": - case "SKEW": - value = Number(value); - if (value == NaN) { - status = false; + let sizeof_tdb = dv.getUint16(2, true); + let sizeof_db = dv.getUint32(4, true); + let nsamples = dv.getUint32(8, true); + let dataFormat = dv.getUint8(12, true); + if (dataFormat == 3) + throw "The data format of the trace is not recognized. It uses an old 20 bit format used in the SEG-D format." + if (dataFormat != 1 && dataFormat != 2 && dataFormat != 4 && dataFormat != 5) + throw "The data format of the trace is not recognized" + + let reader2 = new FileReader(); + let blob2 = file.slice(header.offsets[jj] + 32, + header.offsets[jj] + 32 + sizeof_tdb); + let traceDescriptor = {}; + + reader2.onload = function (e) { + let buffer2 = e.target.result; + let dv2 = new DataView(buffer2); + + let getTerminateChar = (header.size_st == 1) ? + (ii) => { return dv2.getUint8(ii, true); } : (ii) => { return dv2.getUint16(ii, true); }; + + let traceDescriptorParser = function (sentence) { + var key = sentence.substr(0, sentence.indexOf(' ')); + if (key == "") return; + var value = sentence.substr(sentence.indexOf(' ') + 1); + switch (key) { + case "CDP_NUMBER": + case "CDP_TRACE": + case "CHANNEL_NUMBER": + case "DELAY": + case "DESCALING_FACTOR": + case "END_OF_GROUP": + case "FIXED_GAIN": + case "LINE_ID": + case "NOTCH_FREQUENCY": + case "POLARITY": + case "RECEIVER_STATION_NUMBER": + case "SAMPLE_INTERVAL": + case "SHOT_SEQUENCE_NUMBER": + case "SKEW": + value = Number(value); + if (value == NaN) { + status = false; + } + break; + case "RECEIVER_LOCATION": + let testArray = value.split(" "); + if (0 == testArray.length) { + value = Number(testArray[0]); + } + else { + value = []; + for (var ii = 0; ii < testArray.length; ii++) { + let tmpNumber = Number(testArray[ii]); + if (NaN != tmpNumber) value.push(tmpNumber); } - break; - case "RECEIVER_LOCATION": - let testArray = value.split(" "); - if (0 == testArray.length) - { - value = Number(testArray[0]); - } - else - { - value = []; - for (var ii = 0; ii < testArray.length; ii++) - { - let tmpNumber = Number(testArray[ii]); - if (NaN != tmpNumber) value.push(tmpNumber); - } - } - break; - default: - break; - - } - traceDescriptor[key] = value; + } + break; + default: + break; + } - - let nchars = sizeof_tdb - 32; - - let wholeSentenceBits = []; - for (var ii=2; ii { + readFirstPart(resolve, reject); + }).then(() => { + return new Promise((resolve, reject) => { + readTracePointerSubblock(resolve, reject); + }); + }).then(() => { + return new Promise((resolve, reject) => { + readFileDescritor(resolve, reject); + }); + }).then(() => { + return new Promise((resolve, reject) => { + readTracesHeaders(resolve, reject); + }); + }).then(() => { + return; + }).catch(error => { + console.log(error); + return (new Error(error)); + }) +}; + +// export let readTraceData = function(file, header, traceId, data) +function readTraceData(file, header, traceId, data) { + return new Promise((resolve, reject) => { + let reader = new FileReader(); + let offset1 = header.offsets[traceId] + header.trace[traceId].sizeof_tdb; + let offset2 = offset1 + header.trace[traceId].sizeof_db; + let blob = file.slice(offset1, offset2); + + reader.onload = function (e) { + let buffer = e.target.result; + let dv = new DataView(buffer); + let nsamples = header.trace[traceId].nsamples; + + switch (header.trace[traceId].dataFormat) { + case 1: + for (var ii = 0; ii < nsamples; ii++) { + data[ii] = dv.getInt16(2 * ii, true); + } + break; + case 2: + for (var ii = 0; ii < nsamples; ii++) { + data[ii] = dv.getInt32(4 * ii, true); + } + break; + case 4: + for (var ii = 0; ii < nsamples; ii++) { + data[ii] = dv.getFloat32(4 * ii, true); + } + break; + case 5: + for (var ii = 0; ii < nsamples; ii++) { + data[ii] = dv.getFloat64(8 * ii, true); + } + break; } - + resolve(); } - - return new Promise((resolve, reject) => - { - readFirstPart(resolve, reject); - }).then( () => { - return new Promise((resolve, reject) => { - readTracePointerSubblock(resolve, reject); - }); - }).then( () => { - return new Promise((resolve, reject) => { - readFileDescritor(resolve, reject); - }); - }).then( () => { - return new Promise((resolve, reject) => { - readTracesHeaders(resolve, reject); - }); - }).then( () => { - return; - }).catch(error => { - console.log(error); - return(new Error(error)); - }) - }; - - // export let readTraceData = function(file, header, traceId, data) - function readTraceData(file, header, traceId, data) - { - return new Promise((resolve, reject) => - { - let reader = new FileReader(); - let offset1 = header.offsets[traceId] + header.trace[traceId].sizeof_tdb; - let offset2 = offset1 + header.trace[traceId].sizeof_db; - let blob = file.slice(offset1, offset2); - - reader.onload = function(e) - { - let buffer = e.target.result; - let dv = new DataView(buffer); - let nsamples = header.trace[traceId].nsamples; - - switch(header.trace[traceId].dataFormat) - { - case 1: - for (var ii=0; ii< nsamples; ii++) { - data[ii] = dv.getInt16(2*ii,true); - } - break; - case 2: - for (var ii=0; ii< nsamples; ii++) { - data[ii] = dv.getInt32(4*ii,true); - } - break; - case 4: - for (var ii=0; ii< nsamples; ii++) { - data[ii] = dv.getFloat32(4*ii,true); - } - break; - case 5: - for (var ii=0; ii< nsamples; ii++) { - data[ii] = dv.getFloat64(8*ii,true); - } - break; - } - - resolve(); - } - - reader.readAsArrayBuffer(blob); - }); - }; - - // export let buildArraysToPlot = function(header, data) - function buildArraysToPlot(header, data) - { - if (!header || !data) { - console.log("missing seg2file data"); - return; + + reader.readAsArrayBuffer(blob); + }); +}; + +// export let buildArraysToPlot = function(header, data) +function buildArraysToPlot(header, data) { + if (!header || !data) { + console.log("missing seg2file data"); + return; + } + + if (header.ntraces != data.length) { + console.log("Dimensions of header and data do not match"); + return; + } + + let xpos = Array(header.ntraces); + for (let nn = 0; nn < header.ntraces; nn++) { + xpos[nn] = Math.sqrt(header.trace[nn].traceDescriptor['RECEIVER_LOCATION'].reduce((a, num) => a + num * num, 0)); + } + let minDist = 0; + for (let nn = 1; nn < header.ntraces; nn++) { + minDist += Math.abs(xpos[nn] - xpos[nn - 1]) + } + minDist /= header.ntraces; + + let result = new Array(header.ntraces); + for (let nn = 0; nn < header.ntraces; nn++) { + let ns = data[nn].length; + let dt = header.trace[nn].traceDescriptor['SAMPLE_INTERVAL']; + + if (ns == 0) { + console.log("trace " + nn + " is empty. Omitting it.") + continue; } - - if (header.ntraces != data.length){ - console.log("Dimensions of header and data do not match"); - return; + + if (isNaN(dt)) { + console.log("Sampling interval of trace " + nn + " is not a number"); + continue; } - - let xpos = Array(header.ntraces); - for (let nn = 0; nn < header.ntraces; nn++) - { - xpos[nn] = Math.sqrt(header.trace[nn].traceDescriptor['RECEIVER_LOCATION'].reduce((a, num) => a + num*num, 0)); - } - let minDist = 0; - for (let nn = 1; nn < header.ntraces; nn++) - { - minDist += Math.abs(xpos[nn] - xpos[nn-1]) - } - minDist /= header.ntraces; - - let result = new Array(header.ntraces); - for (let nn = 0; nn < header.ntraces; nn++) - { - let ns = data[nn].length; - let dt = header.trace[nn].traceDescriptor['SAMPLE_INTERVAL']; - - if (ns == 0 ) { - console.log("trace " + nn + " is empty. Omitting it.") - continue; - } - - if (isNaN(dt)) { - console.log("Sampling interval of trace " + nn + " is not a number"); - continue; - } - - let maxamp = 0; - for (var ii=0; ii maxamp) ? Math.abs(data[nn][ii]) : maxamp; - } - - maxamp = maxamp == 0 ? 1 : maxamp; - - let t = 0; - result[nn] = {x : [], y : []}; - result[nn].x = new Array(ns); - result[nn].y = new Array(ns); - for (var ii=0; ii maxamp) ? Math.abs(data[nn][ii]) : maxamp; } - - return result; - }; - - // export let buildWiggleData = function(header) - function buildWiggleData(header) - { - if (!header || !data) { - console.log("missing seg2file data"); - return; + + maxamp = maxamp == 0 ? 1 : maxamp; + + let t = 0; + result[nn] = { x: [], y: [] }; + result[nn].x = new Array(ns); + result[nn].y = new Array(ns); + for (var ii = 0; ii < ns; ii++) { + result[nn].y[ii] = t; + result[nn].x[ii] = data[nn][ii] / (2 * maxamp) * minDist + xpos[nn]; + var temp = data[nn][ii]; + t += dt; } - - if (header.ntraces != data.length){ - console.log("Dimensions of header and data do not match"); - return; + } + + return result; +}; + +// export let buildWiggleData = function(header) +function buildWiggleData(header) { + if (!header || !data) { + console.log("missing seg2file data"); + return; + } + + if (header.ntraces != data.length) { + console.log("Dimensions of header and data do not match"); + return; + } + + let plotStruct = []; + for (let nn = 0; nn < header.ntraces; nn++) { + let ns = seg2.traces[nn].data.length; + let dt = seg2.traces[nn].traceDescriptor()['SAMPLE_INTERVAL']; + if (ns == 0) { + console.log("trace " + nn + " is empty. Omitting it.") + continue; } - - let plotStruct = []; - for (let nn = 0; nn < header.ntraces; nn++) - { - let ns = seg2.traces[nn].data.length; - let dt = seg2.traces[nn].traceDescriptor()['SAMPLE_INTERVAL']; - if (ns == 0 ) { - console.log("trace " + nn + " is empty. Omitting it.") - continue; - } - - if (isNaN(dt)) { - console.log("Sampling interval of trace " + nn + " is not a number"); - continue; - } - - let maxamp = 0; - for (var ii=0; ii maxamp) ? Math.abs(seg2.traces[nn].data[ii]) : maxamp; + + if (isNaN(dt)) { + console.log("Sampling interval of trace " + nn + " is not a number"); + continue; + } + + let maxamp = 0; + for (var ii = 0; ii < ns; ii++) { + maxamp = (Math.abs(seg2.traces[nn].data[ii]) > maxamp) ? Math.abs(seg2.traces[nn].data[ii]) : maxamp; + } + + maxamp = maxamp == 0 ? 1 : maxamp; + + var ii = 0; + + let getSingleWiggle = function (input, output, cc) { + + if (cc > input.length) return output; + + var valueToPlot = input[cc] / (2 * maxamp) + nn; + + if (output.length == 0) { + output.push(valueToPlot) } - - maxamp = maxamp == 0 ? 1 : maxamp; - - var ii = 0; - - let getSingleWiggle = function(input, output, cc) { - - if (cc > input.length) return output; - - var valueToPlot = input[cc]/(2*maxamp) + nn; - - if (output.length == 0) - { + else { + if (Math.sign(input[cc]) == Math.sign(input[cc - 1])) { output.push(valueToPlot) } - else - { - if (Math.sign(input[cc]) == Math.sign(input[cc-1])) { - output.push(valueToPlot) - } - else { - console.log(output); - return output; - } + else { + console.log(output); + return output; } - - cc++; - - return getSingleWiggle(input, output, cc); } - - - do { - var wiggle = []; - - wiggle = getSingleWiggle(seg2.traces[nn].data, wiggle, ii); - - var time = new Array(wiggle.length); - - for (var tt = 0; tt < wiggle.length; tt++) { - time[tt] = (ii+tt)*dt; - } - - ii += wiggle.length; - - if (wiggle.length == 1) { - console.log("single point"); - continue; - } - - if (wiggle[0] - nn > 0) { - plotStruct.push({ - x: wiggle, - y: time, - fill: "toself", - mode: "lines", - fillcolor: 'black', - line: { - width: 2, - color: 'black'} - }) - } else { - plotStruct.push({ - x: wiggle, - y: time, - mode: "lines", - line: { - width: 2, - color: 'black'} - }) - } - - } while (ii < ns) + + cc++; + + return getSingleWiggle(input, output, cc); } - - return plotStruct; - }; - - exports.createHeader = createHeader; - exports.readHeader = readHeader; - exports.readTraceData = readTraceData; - exports.buildArraysToPlot = buildArraysToPlot; - exports.buildWiggleData = buildWiggleData; - - Object.defineProperty(exports, '__esModule', { value: true }); - - return exports; - - }({})); \ No newline at end of file + + + do { + var wiggle = []; + + wiggle = getSingleWiggle(seg2.traces[nn].data, wiggle, ii); + + var time = new Array(wiggle.length); + + for (var tt = 0; tt < wiggle.length; tt++) { + time[tt] = (ii + tt) * dt; + } + + ii += wiggle.length; + + if (wiggle.length == 1) { + console.log("single point"); + continue; + } + + if (wiggle[0] - nn > 0) { + plotStruct.push({ + x: wiggle, + y: time, + fill: "toself", + mode: "lines", + fillcolor: 'black', + line: { + width: 2, + color: 'black' + } + }) + } else { + plotStruct.push({ + x: wiggle, + y: time, + mode: "lines", + line: { + width: 2, + color: 'black' + } + }) + } + + } while (ii < ns) + } + + return plotStruct; +}; + +let seg2js = { createHeader, readHeader, readTraceData, buildArraysToPlot, buildWiggleData }; +export default seg2js;