-
Notifications
You must be signed in to change notification settings - Fork 1
/
seg2js.min.js
1 lines (1 loc) · 6.43 KB
/
seg2js.min.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).seg2js=t()}(this,(function(){"use strict";return{createHeader:function(){return{ntraces:0,offsets:[],versionNumber:0,st:0,lt:0,size_st:0,fileDescriptor:{},sizeTracePointer:0,trace:[]}},readHeader:function(e,t){if(!e||!t)return void console.log("wrong input");t.ntraces=0,t.offsets=[],t.versionNumber=0,t.st=0,t.lt=0,t.size_st,t.fileDescriptor={},t.sizeTracePointer=0,t.trace=[];let r={nsamples:0,dataFormat:0,sizeof_tdb:0,sizeof_db:0,traceDescriptor:{}},a=function(r){return new Promise((a,n)=>{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<e.length;a++){let t=Number(e[a]);NaN!=t&&r.push(t)}}}u[t]=r}},g=n-32,d=[];for(var h=2;h<g;h++){if(l(h)==t.st)f(String.fromCharCode.apply(null,d)),d=[],h+=2;else d.push(o.getUint8(h,!0))}a({nsamples:i,dataFormat:c,sizeof_tdb:n,sizeof_db:s,traceDescriptor:u})},l.readAsArrayBuffer(f)}catch(e){n(e)}},s.readAsArrayBuffer(i)})};return new Promise((a,n)=>{!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;i<t.ntraces;i++)t.offsets[i]=s.getUint32(4*i,!0);r()}catch(e){a(e)}},n.readAsArrayBuffer(i)}(r,a)})).then(()=>new 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;i<l;i++){if(u(i)==t.st){var c=String.fromCharCode.apply(null,f);o(c),f=[],i+=2}else f.push(a.getUint8(i,!0))}r()}catch(e){a(e)}},n.readAsArrayBuffer(i)}(r,a)})).then(()=>new Promise((e,r)=>{!async function(e,r){for(var n=0;n<t.ntraces;n++)await a(n).then((function(e){t.trace[n]=Object.assign({},e)}));e()}(e)})).then(()=>{}).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;c<o;c++)a[c]=i.getInt16(2*c,!0);break;case 2:for(c=0;c<o;c++)a[c]=i.getInt32(4*c,!0);break;case 4:for(c=0;c<o;c++)a[c]=i.getFloat32(4*c,!0);break;case 5:for(c=0;c<o;c++)a[c]=i.getFloat64(8*c,!0)}n()},i.readAsArrayBuffer(l)})},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;t<e.ntraces;t++)r[t]=Math.sqrt(e.trace[t].traceDescriptor.RECEIVER_LOCATION.reduce((e,t)=>e+t*t,0));let a=0;for(let t=1;t<e.ntraces;t++)a+=Math.abs(r[t]-r[t-1]);a/=e.ntraces;let n=new Array(e.ntraces);for(let i=0;i<e.ntraces;i++){let o=t[i].length,c=e.trace[i].traceDescriptor.SAMPLE_INTERVAL;if(0==o){console.log("trace "+i+" is empty. Omitting it.");continue}if(isNaN(c)){console.log("Sampling interval of trace "+i+" is not a number");continue}let l=0;for(var s=0;s<o;s++)l=Math.abs(t[i][s])>l?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;s<o;s++)n[i].y[s]=f,n[i].x[s]=t[i][s]/(2*l)*a+r[i],t[i][s],f+=c}return n},buildWiggleData:function(e){if(!e||!data)return void console.log("missing seg2file data");if(e.ntraces!=data.length)return void console.log("Dimensions of header and data do not match");let t=[];for(let i=0;i<e.ntraces;i++){let e=seg2.traces[i].data.length,o=seg2.traces[i].traceDescriptor().SAMPLE_INTERVAL;if(0==e){console.log("trace "+i+" is empty. Omitting it.");continue}if(isNaN(o)){console.log("Sampling interval of trace "+i+" is not a number");continue}let c=0;for(var r=0;r<e;r++)c=Math.abs(seg2.traces[i].data[r])>c?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;s<a.length;s++)n[s]=(r+s)*o;r+=a.length,1!=a.length?a[0]-i>0?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<e)}return t}}}));