forked from bolidozor/js9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitsy.min.js
executable file
·43 lines (43 loc) · 18.8 KB
/
fitsy.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var Fitsy;if(Fitsy&&("object"!==typeof Fitsy||Fitsy.NAME))throw Error("Namespace 'Fitsy' already exists");
Fitsy={NAME:"Fitsy",VERSION:"1.0",clone:function(b){var a,d,e;if(null===b||"object"!==typeof b)return b;if(b instanceof Date)return a=new Date,a.setTime(b.getTime()),a;if(b instanceof Array){a=[];d=0;for(e=b.length;d<e;d++)a[d]=Fitsy.clone(b[d]);return a}if(b instanceof Object){a={};for(d in b)b.hasOwnProperty(d)&&(a[d]=Fitsy.clone(b[d]));return a}throw Error("Unable to copy obj! Its type isn't supported.");},strrepeat:function(b,a){if(1>a)return"";for(var d="";1<a;)a&1&&(d+=b),a>>=1,b+=b;return d+
b}};ArrayBuffer.prototype.slice||(ArrayBuffer.prototype.slice=function(b,a){var d,e,c,f=new Uint8Array(this);void 0===a&&(a=f.length);e=new ArrayBuffer(a-b);c=new Uint8Array(e);for(d=0;d<c.length;d++)c[d]=f[d+b];return e});Fitsy.hostEndian=function(){return 1===(new Int8Array((new Int32Array([1])).buffer))[0]}();Fitsy.memcpy=function(b,a,d,e,c){b=new Uint8Array(b,a,c);d=new Uint8Array(d,e,c);b.set(d)};
Fitsy.getDataSlice=function(b,a,d,e,c,f){var g,j,k,l,h,m,q,p=Array.prototype.slice.call(arguments,6);switch(b.ftype){case "file":c&&(b.read.onloadend=function(){b.result="asString"===a?Fitsy.getBinaryString(b.read.result):b.read.result;c.apply(null,p)});f&&(b.read.onerror=function(){f.apply(null,p)});g=d||e?Fitsy.getFileSlice(b.file,d,e):b.file;b.read.readAsArrayBuffer(g);break;case "array":"asString"===a?(g=d||e?b.file.slice(d,e):b.file,b.result=Fitsy.getBinaryString(g)):(b.result=b.file,d&&e?(b.rlo=
d,b.rhi=e):(delete b.rlo,delete b.rhi));c&&c.apply(null,p);break;case "gzarray":d!==b.gz.here&&(Astroem.gzseek(b.gz.fd,d,0),b.gz.here=d);j=e-d;try{g=new Uint8Array(j)}catch(n){Fitsy.error("can't allocate enough memory to slice this FITS file",n)}k=Math.min(j,Fitsy.options.gzheapsize);m=Module._malloc(k);q=new Uint8Array(Module.HEAPU8.buffer,m,k);h=j;for(j=0;h;)l=Math.min(h,k),l=Astroem.gzread(b.gz.fd,q.byteOffset,l),0<l?(Fitsy.memcpy(g.buffer,j,Module.HEAPU8.buffer,m,l),j+=l,h-=l):h=0;Module._free(q.byteOffset);
b.gz.here=d+j;0<j?b.result="asString"===a?Fitsy.getBinaryString(g):g.buffer:b.eof=!0;c&&c.apply(null,p)}};Fitsy.getBinaryString=function(b){return String.fromCharCode.apply(null,new Uint8Array(b))};
Fitsy.tableFilter=function(b,a){var d;d=a.replace(/([a-zA-Z][a-zA-Z0-9_]*) *= *([\-]?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+\-]?[0-9]+)?) *: *([\-]?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+\-]?[0-9]+)?)/g,"($1 >= $2 && $1 <= $3)");d=d.replace(/([a-zA-Z][a-zA-Z0-9_]*) *= *([\-]?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+\-]?[0-9]+)?) *:/g,"$1 >= $2");d=d.replace(/([a-zA-Z][a-zA-Z0-9_]*) *= *:([\-]?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+\-]?[0-9]+)?)/g,"$1 <= $2");return d.replace(/([ (&|+\-\/*%<>=]|^)?([a-zA-Z][a-zA-Z0-9_]*)([ )&|+\-\/*%<>=]|$)/g,
function(a,d,f,g){a=f;var j=f.toUpperCase();d=d||"";g=g||"";void 0!==b.table[a]?a=d+"view.get"+b.table[a].type+"(vstart + i * {{table.width}} + "+b.table[a].offs+")"+g:void 0!==b.table[j]?a=d+"view.get"+b.table[j].type+"(vstart + i * {{table.width}} + "+b.table[j].offs+")"+g:Fitsy.error("the requested column '"+f+"' is missing from this FITS table");return a})};Fitsy.getFileSlice=function(b,a,d){var e;b.slice?e=b.slice(a,d):b.mozSlice?e=b.mozSlice(a,d):b.webkitSlice&&(e=b.webkitSlice(a,d));return e};
Fitsy.isNumber=function(b){return!isNaN(parseFloat(b))&&isFinite(b)};Fitsy.xtypeof=function(b){return Object.prototype.toString.call(b).slice(8,-1)};Fitsy.isFloat=function(b){return b===+b&&b!==(b|0)};Fitsy.cardpars=function(b){var a;if("="===b[8])return a=b.slice(0,8).trim(),b=b.slice(10).replace(/\'/g," ").replace(/\/.*/,"").trim(),"T"===b?b=!0:"F"===b?b=!1:Fitsy.isNumber(b)&&(b=parseFloat(b)),[a,b]};
Fitsy.readImageHDUDataConverter=function(b,a,d,e){var c,f,g,j,k,l,h;g=0;var m;a.dmin=Number.MAX_VALUE;a.dmax=Number.MIN_VALUE;a.filename=b.name;switch(b.ftype){case "array":b.rlo&&(g=b.rlo)}switch(a.bitpix){case 8:c=DataView.prototype.getUint8;f=DataView.prototype.setUint8;try{a.filedata=new Uint8Array(b.result,g)}catch(q){l=!0}break;case -16:c=DataView.prototype.getUint16;f=DataView.prototype.setUint16;try{a.filedata=new Uint16Array(b.result,g)}catch(p){l=!0}break;case 16:if(a.bzero&&32768===a.bzero){c=
DataView.prototype.getInt16;f=DataView.prototype.setUint16;try{a.filedata=new Uint16Array(b.result,g)}catch(n){l=!0}a.bitpix=-16}else{c=DataView.prototype.getInt16;f=DataView.prototype.setInt16;try{a.filedata=new Int16Array(b.result,g)}catch(r){l=!0}}break;case 32:c=DataView.prototype.getInt32;f=DataView.prototype.setInt32;try{a.filedata=new Int32Array(b.result,g)}catch(s){l=!0}break;case -32:c=DataView.prototype.getFloat32;f=DataView.prototype.setFloat32;try{a.filedata=new Float32Array(b.result,
g)}catch(t){l=!0}break;case -64:c=DataView.prototype.getFloat64;f=DataView.prototype.setFloat64;try{a.filedata=new Float64Array(b.result,g)}catch(u){l=!0}break;default:Fitsy.error("unsupported FITS BITPIX value: "+a.bitpix)}l&&Fitsy.error("can't allocate enough memory for this FITS image");a.image=a.filedata;h=a.bzero||0;l=a.bscale||1;b=new DataView(b.result);m=a.converted?Fitsy.hostEndian:!1;j=0;for(k=g;j<a.datapixls;j++,k+=a.pixlbytes)g=c.call(b,k,m)*l+h,isNaN(g)||(a.dmin=Math.min(a.dmin,g),a.dmax=
Math.max(a.dmax,g)),a.converted||f.call(b,k,g,Fitsy.hostEndian);a.converted=!0;e(a,d)};Fitsy.readImageHDUData=function(b,a,d,e){Fitsy.getDataSlice(b,"asArray",a.dataseek,a.dataseek+2880*a.databloks,Fitsy.readImageHDUDataConverter,Fitsy.readError,b,a,d,e)};
var TableTFORM={L:{type:"UChar",size:1},X:{type:"BITS",size:1},B:{type:"UChar",size:1},I:{type:"Int16",size:2},J:{type:"Int32",size:4},K:{type:"Int64",size:8},A:{type:"Char",size:1},E:{type:"Float32",size:4},D:{type:"Float64",size:8},C:{type:"Complex64",size:8},M:{type:"Complex128",size:16},P:{type:"Pointer32",size:8},Q:{type:"Pointer64",size:16}};Fitsy.readError=function(b){Fitsy.error("an error occurred while reading the FITS file. (Often this is due to the browser running out of memory.)",b)};
Fitsy.waiting=function(b){Fitsy.options.waiting&&Fitsy.options.waiting(b)};Fitsy.error=function(b,a){Fitsy.waiting(!1);if(Fitsy.options.error)Fitsy.options.error(b,a);else throw Error(b);};
Fitsy.readForDeCompress=function(b){var a=new Uint8Array(b.result);if(253===a[0]&&55===a[1]&&122===a[2]&&88===a[3]){if(void 0!==window.LZMA){LZMA.decompress(a,function(a){b.file=new Blob([a]);Fitsy.getDataSlice(b,"asString",0,2880,Fitsy.readHeaderBlock,Fitsy.readError,b)});return}Fitsy.error("lzip support not available")}else 66===a[0]&&90===a[1]?void 0!==window.bzip2?(a=bzip2.simple(bzip2.array(a)),b.file=new Blob(a)):Fitsy.error("bzip2 support not available"):31===a[0]&&139===a[1]?(delete b.file,
delete b.hdu,b.hdu=[],window.hasOwnProperty("Astroem")?a.buffer.byteLength>Fitsy.options.gzfilesize?(b.ftype="gzarray",b.gz={},b.gz.arrfile=Astroem.arrfile(b.name,a),b.gz.fd=Astroem.gzopen(b.gz.arrfile.path,"rb"),b.gz.here=0,b.size=-1,b.eof=!1):(a=Astroem.decompress(a),b.ftype="array",b.file=a.buffer,b.size=b.file.byteLength):window.hasOwnProperty("Zee")?(a=Zee.decompress(a),b.ftype="array",b.file=a.buffer,b.size=b.file.byteLength):window.hasOwnProperty("pako")?(a=pako.inflate(a),b.ftype="array",
b.file=a.buffer,b.size=b.file.byteLength):Fitsy.error("gzip support not available")):Fitsy.error("not a recognized FITS file (compressed or otherwise): "+b.name);Fitsy.getDataSlice(b,"asString",0,2880,Fitsy.readHeaderBlock,Fitsy.readError,b)};
Fitsy.readHeaderBlock=function(b){var a,d,e=0;b.eof&&(Fitsy.waiting(!1),b.handler(b));b.hdu[b.nhdu]||(b.hdu[b.nhdu]={});var c=b.hdu[b.nhdu];if(!c.card&&(c.headseek=b.here,c.card=[],c.head={},c.ncard=0,c.fits=b,c.nth=b.nhdu,0===b.here&&"SIMPLE"!==b.result.slice(0,6))){Fitsy.waiting(!0);setTimeout(function(){Fitsy.getDataSlice(b,"asArray",null,null,Fitsy.readForDeCompress,Fitsy.readError,b)},Fitsy.options.wtimeout);return}if(0===b.here){if("SIMPLE"!==b.result.slice(0,6))return}else if(0===c.ncard&&
"XTENSION"!==b.result.slice(0,8))return;b.here+=2880;for(a=0;2880>a;c.ncard++,a+=80){d=b.result.slice(a,a+80);c.card[c.ncard]=d;if("END "===d.slice(0,8)){e=1;break}d=Fitsy.cardpars(d);void 0!==d&&(c.head[d[0]]=d[1])}if(e){c.axis=[];c.dataseek=b.here;c.naxis=c.head.NAXIS;c.bitpix=c.head.BITPIX;c.bscale=c.head.BSCALE;c.bzero=c.head.BZERO;c.pixlbytes=Math.abs(c.bitpix)/8;c.datapixls=0!==c.naxis?1:0;for(a=1;a<=c.naxis;a++)c.axis[a]=c.head["NAXIS"+a],c.datapixls*=c.axis[a];c.databytes=c.datapixls*
c.pixlbytes;c.databloks=(c.databytes+2879)/2880|0;b.here+=2880*c.databloks;b.nhdu++;c.type="image";c.filehead=c.head;c.filecard=c.card;if("BINTABLE"===c.head.XTENSION||"A3DTABLE"===c.head.XTENSION||"3DTABLE"===c.head.XTENSION){c.type="table";c.table={};c.width=c.axis[1];c.length=c.axis[2];var f,g,j=0;for(a=1;a<=c.head.TFIELDS;a++)f=c.head["TFORM"+a].match(/([0-9]*)([LXBIJKAEDCMPQ])/),e=""===f[1]?1:+f[1],d=TableTFORM[f[2]].type,f=TableTFORM[f[2]].size,g=e*f,c.table[c.head["TTYPE"+a]]={type:d,size:f,
width:g,rept:e,offs:j,unit:c.head["TUNIT"+a],disp:c.head["TDISP"+a],zero:c.head["TZERO"+a],scale:c.head["TSCAL"+a],min:c.head["TDMIN"+a]||c.head["TLMIN"+a],max:c.head["TDMAX"+a]||c.head["TLMAX"+a],ith:a},j+=g}}b.here>=b.size&&-1!==b.size?(Fitsy.waiting(!1),b.handler(b)):Fitsy.getDataSlice(b,"asString",b.here,b.here+2880,Fitsy.readHeaderBlock,Fitsy.readError,b)};
Fitsy.fitsopen=function(b,a){var d={hdu:[]};d.read=new FileReader;d.handler=a;b||Fitsy.readError(null);d.name=b.name;d.size=b.size;d.file=b;d.ftype="file";d.nhdu=0;d.here=0;Fitsy.getDataSlice(d,"asString",0,2880,Fitsy.readHeaderBlock,Fitsy.readError,d)};
Fitsy.template=function(b,a){return b.replace(/\{\{([a-zA-Z0-9_.]+)(%([sfd])(\.([0-9]+))?)?\}\}/g,function(b,e,c,f,g,j){c=a;e=e.split(".");for(b=0;b<e.length;b++)if(c.hasOwnProperty(e[b]))c=c[e[b]];else return"";switch(f){case "f":c=c.toFixed(j);break;case "d":c=c.toFixed(0)}return c})};Fitsy.BinTableTemplate="\t\t\t\t\t\t\t\t\t\n return function (view, vstart, image, length) {\t\t\t\t\t\t\t\n var i, x, y;\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\n var xoff = ((-( {{table.x.range}}/2 + ({{table.cx}}-{{table.x.range}}/2 )) {{BinText}}) | 0)\n \t + (( {{image.nx}}/2 ) | 0);\t\t\t\t\t\t\t\n var yoff = ((-( {{table.y.range}}/2 + ({{table.cy}}-{{table.x.range}}/2 )) {{BinText}}) | 0)\n \t + (( {{image.ny}}/2 ) | 0);\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\n for (i = 0; i < length; i++) {\t\t\t\t\t\t\t\t\n if( {{FilterText}} ){\t\t\t\t\t\t\t\t\t\n \t x = view.get{{table.x.type}}(vstart + i * {{table.width}} + {{table.x.offs}});\t\n\t y = view.get{{table.y.type}}(vstart + i * {{table.width}} + {{table.y.offs}});\t\n\t\t\t\t\t\t\t\t\t\t\t\t\n\t x = (((x - {{table.x.min}}) {{BinText}}) | 0) + xoff;\t\t\t\t\t\n\t y = (((y - {{table.y.min}}) {{BinText}}) | 0) + yoff;\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\n\t if (x >= 0 && x < {{image.nx}} && y >= 0 && y < {{image.ny}}) {\t\t\t\n\t image.data[y * {{image.width}} + x] += 1;\t\t\t\t\t\t\n\t }\t\t\t\t\t\t\t\t\t\t\t\n }\t\t\t\t\t\t\t\t\t\t\t\n }\t\t\t\t\t\t\t\t\t\t\t\t\n}";
Fitsy.readTableHDUDataBinner=function(b,a,d,e,c){var f,g,j,k="true",l=0,h=e.table||Fitsy.options.table;if(!a.nev){a.filename=b.name;if(a.image&&a.image.length>=h.nx*h.ny)for(f=0;f<a.image.length;f++)a.image[f]=0;else a.image=new Int32Array(h.nx*h.ny);a.data=a.image;a.head=Fitsy.clone(a.filehead);a.card=Fitsy.clone(a.filecard);for(f=0;f<h.xcol.length&&void 0===a.table[h.xcol[f]];f++);a.colx=a.table[h.xcol[f]];for(f=0;f<h.ycol.length&&void 0===a.table[h.ycol[f]];f++);a.coly=a.table[h.ycol[f]]}a.filedata=
b.result;var m=a.colx,q=a.coly,p={nx:h.nx,ny:h.ny,width:h.nx,data:a.image},n={x:{type:m.type,offs:m.offs,min:Number(m.min),range:m.max-m.min+1},y:{type:q.type,offs:q.offs,min:Number(q.min),range:q.max-q.min+1},cx:h.cx,cy:h.cy,width:a.width,length:a.length,bin:h.bin,filter:h.filter};void 0===n.cx&&(n.cx=(m.max-m.min+1)/2);void 0===n.cy&&(n.cy=(q.max-q.min+1)/2);f={table:n,image:p,BinText:1===n.bin?"":"/"+Number(n.bin)};n.filter&&(j=Fitsy.tableFilter(a,n.filter),k=Fitsy.template(j,f));f.FilterText=
k;k=Fitsy.template(Fitsy.BinTableTemplate,f);try{g=(new Function(k))()}catch(r){Fitsy.error("An error occurred while generating the filter/binning function. Please check the filter syntax: "+n.filter)}a.view=new DataView(a.filedata);switch(b.ftype){case "array":b.rlo&&(l=b.rlo)}try{g(a.view,l,p,d)}catch(s){j?Fitsy.error("An error occurred while binning the table. Please check the filter syntax: "+j):Fitsy.error("An unknown error occurred while binning the table")}a.nev+=d;if(a.nev===a.length){a.table.bin=
h.bin;a.table.filter=h.filter;a.table.nx=h.nx;a.table.ny=h.ny;a.table.cx=n.cx;a.table.cy=n.cy;a.dmin=Number.MAX_VALUE;a.dmax=Number.MIN_VALUE;for(f=0;f<p.nx*p.ny;f++)a.dmin=Math.min(a.dmin,a.data[f]),a.dmax=Math.max(a.dmax,a.data[f]);a.axis[1]=p.nx;a.axis[2]=p.ny;a.bitpix=32;a.head.NAXIS1=p.nx;a.head.NAXIS2=p.ny;a.card[0]=Fitsy.cardfmt("SIMPLE",0,!0,"Standard FITS Image");a.card[Fitsy.cardfind(a.card,"BITPIX",0)]=Fitsy.cardfmt("BITPIX",0,32,"FITS Data type.");a.card[Fitsy.cardfind(a.card,"NAXIS",
0)]=Fitsy.cardfmt("NAXIS",0,2,"Number of Axes");a.card[Fitsy.cardfind(a.card,"NAXIS1",1)]=Fitsy.cardfmt("NAXIS1",0,p.nx,"Axis Dimension");a.card[Fitsy.cardfind(a.card,"NAXIS2",1)]=Fitsy.cardfmt("NAXIS2",0,p.ny,"Axis Dimension");f=m.ith;Fitsy.cardcopy(a,"TCTYP"+f,a,"CTYPE1");Fitsy.cardcopy(a,"TCRVL"+f,a,"CRVAL1");Fitsy.cardcopy(a,"TCDLT"+f,a,"CDELT1",void 0,function(a){return a*n.bin});Fitsy.cardcopy(a,"TCRPX"+f,a,"CRPIX1",void 0,function(b){return b-(a.table.cx-a.table.nx/2)-0.5});Fitsy.cardcopy(a,
"TCROT"+f,a,"CROTA1");f=q.ith;Fitsy.cardcopy(a,"TCTYP"+f,a,"CTYPE2");Fitsy.cardcopy(a,"TCRVL"+f,a,"CRVAL2");Fitsy.cardcopy(a,"TCDLT"+f,a,"CDELT2",void 0,function(a){return a*n.bin});Fitsy.cardcopy(a,"TCRPX"+f,a,"CRPIX2",void 0,function(b){return b-(a.table.cy-a.table.ny/2)-0.5});Fitsy.cardcopy(a,"TCROT"+f,a,"CROTA2");Fitsy.cardcopy(a,"LTV1",a,"LTV1",0,function(){return p.nx/2-a.table.cx/n.bin});Fitsy.cardcopy(a,"LTV2",a,"LTV2",0,function(){return p.ny/2-a.table.cy/n.bin});Fitsy.cardcopy(a,"LTM1_1",
a,"LTM1_1",1,function(a){return a/n.bin});Fitsy.cardcopy(a,"LTM1_2",a,"LTM1_2",0,function(a){return a/n.bin});Fitsy.cardcopy(a,"LTM2_1",a,"LTM2_1",0,function(a){return a/n.bin});Fitsy.cardcopy(a,"LTM2_2",a,"LTM2_2",1,function(a){return a/n.bin});c(a,e)}else d=Math.min(a.length-a.nev,Fitsy.options.maxev),g=a.dataseek+a.nev*a.width,Fitsy.getDataSlice(b,"asArray",g,g+d*a.width,Fitsy.readTableHDUDataBinner,Fitsy.readError,b,a,d,e,c)};
Fitsy.cardfmt=function(b,a,d,e){switch(Fitsy.xtypeof(d)){case "Boolean":d=d?"T":"F";break;case "String":d="'"+d+"'";break;case "Number":Fitsy.isFloat(d)&&(d=d.toPrecision(15))}0!==a&&(b=(b+a.toFixed(0)).slice(0,8));b+=Fitsy.strrepeat(" ",8-b.length);b=b+"= "+d+" /"+e;return b+Fitsy.strrepeat(" ",80-b.length)};Fitsy.cardfind=function(b,a,d){var e;a+=Fitsy.strrepeat(" ",8-a.length);for(e=0;e<b.length;e++)if(b[e].slice(0,8)===a)return e;if(!0===d)return b[e]=b[e-1],e-1};
Fitsy.cardcopy=function(b,a,d,e,c,f){a=Fitsy.cardfind(b.card,a);void 0===a&&void 0===c||(void 0!==a&&(b=Fitsy.cardpars(b.card[a]),void 0!==b&&(c=b[1])),void 0!==f&&(c=f(c)),d.card[Fitsy.cardfind(d.card,e,!0)]=Fitsy.cardfmt(e,0,c,""),d.head[e]=c)};Fitsy.readTableHDUData=function(b,a,d,e){a.nev=0;var c=Math.min(a.length,Fitsy.options.maxev),f=a.dataseek;Fitsy.getDataSlice(b,"asArray",f,f+c*a.width,Fitsy.readTableHDUDataBinner,Fitsy.readError,b,a,c,d,e)};
Fitsy.convertPixel=function(b,a,d){b=new DataView(b);switch(a){case 8:return b.getUint8(0,!1);case -16:return b.getInt16(0,!1)+d;case 16:return 32768===d?b.getInt16(0,!1)+d:b.getInt16(0,!1);case 32:return b.getInt32(0,!1);case -32:return b.getFloat32(0,!1);case -64:return b.getFloat64(0,!1);default:Fitsy.error("unknown BITPIX value: "+a)}};Fitsy.getTableValue=function(b,a,d){d=b.table[d];return void 0!==b.view["get"+d.type]?b.view["get"+d.type](a*b.width+d.offs):""};
Fitsy.readPixel=function(b,a,d,e){d=a.dataseek+(d[0]*a.axis[1]+d[1])*Math.abs(a.bitpix/8);Fitsy.getDataSlice(b,"asArray",d,d+Math.abs(a.bitpix/8),function(){b.pixel=Fitsy.convertPixel(b.result,a.bitpix,a.bzero||0);e()},Fitsy.readError)};
Fitsy.defaultDispatchFITS=function(b,a,d){var e,c,f=a.events||"EVENTS";for(e=0;e<b.hdu.length;e++)if(c=b.hdu[e],0<c.databytes){if(c.head.SIMPLE||"IMAGE"===c.head.XTENSION){Fitsy.readImageHDUData(b,c,a,d);break}if(c.table&&c.head.EXTNAME===f){Fitsy.readTableHDUData(b,c,a,d);break}}};Fitsy.handleFITSFile=function(b,a,d){void 0===a&&(a=Fitsy.options);void 0===d&&(d=Fitsy.handler);Fitsy.fitsopen(b,function(b){Fitsy.defaultDispatchFITS(b,a,d)})};Fitsy.datahandler=function(b){Fitsy.handler=b};
Fitsy.dataoptions=function(b){Fitsy.options=b};Fitsy.dragenter=function(b,a){a.stopPropagation();a.preventDefault()};Fitsy.dragover=function(b,a){a.stopPropagation();a.preventDefault()};Fitsy.dragexit=function(b,a){a.stopPropagation();a.preventDefault()};Fitsy.dragdrop=function(b,a){a.stopPropagation();a.preventDefault();Fitsy.onFile(a.target.files||a.dataTransfer.files,{display:b})};
Fitsy.onFile=function(b,a,d){var e;for(e=0;e<b.length;e++)if("image/fits"!==b[e].type&&-1!==b[e].type.indexOf("image/"))Fitsy.handleImageFile(b[e],a,d);else try{Fitsy.handleFITSFile(b[e],a,d)}catch(c){Fitsy.error("could not load FITS file: "+b[e].name)}};Fitsy.options={maxev:20480,gzfilesize:104857600,gzheapsize:4194304,xtimeout:1E4,wtimeout:100,table:{nx:1024,ny:1024,bin:1,xcol:["X","x"],ycol:["Y","y"]}};
Fitsy.fetchURL=function(b,a,d,e){var c=new XMLHttpRequest;void 0===a&&(a=b,b=/([^\\\/]+)$/.exec(a)[1]);d=d||Fitsy.options;c.open("GET",a,!0);c.responseType="blob";c.onload=function(){var c;4===this.readyState&&(200===this.status||0===this.status?(c=new Blob([this.response]),c.name=b,d.messages&&d.messages(""),Fitsy.onFile([c],d,e)):404===this.status?Fitsy.error("could not find "+a):Fitsy.error("can't load "+a+" ("+this.status+")"))};c.onerror=function(){Fitsy.error("can't load "+a)};c.onreadystatechange=
function(){c.xtimeout&&(clearTimeout(c.xtimeout),delete c.xtimeout)};d.messages&&(c.addEventListener("progress",function(a){d.messages("progress "+a.loaded.toString())}),c.addEventListener("error",function(){d.messages("error loading FITS file")}),c.addEventListener("abort",function(){d.messages("abort while loading FITS file")}));try{c.send()}catch(f){Fitsy.error("request to load "+a+" failed",f)}c.xtimeout=setTimeout(function(){Fitsy.error("timeout while waiting for response from server; "+a+" might not exist")},
Fitsy.options.xtimeout)};
Fitsy.handleImageFile=function(b,a,d){void 0===a&&(a=Fitsy.options);void 0===d&&(d=Fitsy.handler);var e=new FileReader;e.onload=function(c){var f=new Image;f.src=c.target.result;f.onload=function(){var c,e,k=0,l;c=document.createElement("canvas");e=c.getContext("2d");var h=f.height,m=f.width;c.width=m;c.height=h;e.drawImage(f,0,0);var q=e.getImageData(0,0,m,h).data,p=new Float32Array(h*m);for(e=0;e<h;e++)for(c=0;c<m;c++)l=0.299*q[k]+0.587*q[k+1]+0.114*q[k+2],p[(h-e)*m+c]=l,k+=4;c={head:{},name:b.name,
filedata:p,image:p,naxis:2,axis:[0,m,h],bitpix:-32};c.dmin=Number.MAX_VALUE;c.dmax=Number.MIN_VALUE;for(k=0;k<h*m;k++)c.dmin=Math.min(c.dmin,c.image[k]),c.dmax=Math.max(c.dmax,c.image[k]);d(c,a)}};e.readAsDataURL(b)};