riverforecastsystem 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ function t(t,e,r,n={}){return void 0!==e&&void 0!==r&&(n={...n,headers:{...n.headers,Range:`bytes=${e}-${e+r-1}`}}),fetch(t,n)}function e(t,e){const r="string"==typeof t?new URL(t):t;r.pathname.endsWith("/")||(r.pathname+="/");const n=new URL(e.slice(1),r);return n.search=r.search,n}async function r(t){if(404!==t.status){if(200===t.status||206===t.status)return new Uint8Array(await t.arrayBuffer());throw new Error(`Unexpected response status ${t.status} ${t.statusText}`)}}class n{url;#t;#e;constructor(t,e={}){this.url=t,this.#t=e.overrides??{},this.#e=e.useSuffixRequest??!1}#r(t){return e=this.#t,r=t,{...e,...r,headers:{...e.headers,...r.headers}};var e,r}async get(t,n={}){let a=e(this.url,t).href;return r(await fetch(a,this.#r(n)))}async getRange(n,a,s={}){let i,o=e(this.url,n),c=this.#r(s);return i="suffixLength"in a?await async function(e,r,n,a){if(a)return fetch(e,{...n,headers:{...n.headers,Range:`bytes=-${r}`}});let s=await fetch(e,{...n,method:"HEAD"});if(!s.ok)return s;let i=s.headers.get("Content-Length"),o=Number(i);return t(e,o-r,o,n)}(o,a.suffixLength,c,this.#e):await t(o,a.offset,a.length,c),r(i)}}class a{#n;constructor(t,e,r){"number"==typeof t?this.#n=new Uint8Array(t):t instanceof ArrayBuffer?this.#n=new Uint8Array(t,e,r):this.#n=new Uint8Array(Array.from(t,t=>t?1:0))}get BYTES_PER_ELEMENT(){return 1}get byteOffset(){return this.#n.byteOffset}get byteLength(){return this.#n.byteLength}get buffer(){return this.#n.buffer}get length(){return this.#n.length}get(t){let e=this.#n[t];return"number"==typeof e?0!==e:e}set(t,e){this.#n[t]=e?1:0}fill(t){this.#n.fill(t?1:0)}*[Symbol.iterator](){for(let t=0;t<this.length;t++)yield this.get(t)}}class s{_data;chars;#a;constructor(t,e,r,n){if(this.chars=t,this.#a=new TextEncoder,"number"==typeof e)this._data=new Uint8Array(e*t);else if(e instanceof ArrayBuffer)n&&(n*=t),this._data=new Uint8Array(e,r,n);else{let r=Array.from(e);this._data=new Uint8Array(r.length*t);for(let t=0;t<r.length;t++)this.set(t,r[t])}}get BYTES_PER_ELEMENT(){return this.chars}get byteOffset(){return this._data.byteOffset}get byteLength(){return this._data.byteLength}get buffer(){return this._data.buffer}get length(){return this.byteLength/this.BYTES_PER_ELEMENT}get(t){const e=new Uint8Array(this.buffer,this.byteOffset+this.chars*t,this.chars);return(new TextDecoder).decode(e).replace(/\x00/g,"")}set(t,e){const r=new Uint8Array(this.buffer,this.byteOffset+this.chars*t,this.chars);r.fill(0),r.set(this.#a.encode(e))}fill(t){const e=this.#a.encode(t);for(let t=0;t<this.length;t++)this._data.set(e,t*this.chars)}*[Symbol.iterator](){for(let t=0;t<this.length;t++)yield this.get(t)}}class i{#s;chars;constructor(t,e,r,n){if(this.chars=t,"number"==typeof e)this.#s=new Int32Array(e*t);else if(e instanceof ArrayBuffer)n&&(n*=t),this.#s=new Int32Array(e,r,n);else{const r=e,n=new i(t,1);this.#s=new Int32Array(function*(){for(let t of r)n.set(0,t),yield*n.#s}())}}get BYTES_PER_ELEMENT(){return this.#s.BYTES_PER_ELEMENT*this.chars}get byteLength(){return this.#s.byteLength}get byteOffset(){return this.#s.byteOffset}get buffer(){return this.#s.buffer}get length(){return this.#s.length/this.chars}get(t){const e=this.chars*t;let r="";for(let t=0;t<this.chars;t++)r+=String.fromCodePoint(this.#s[e+t]);return r.replace(/\u0000/g,"")}set(t,e){const r=this.chars*t,n=this.#s.subarray(r,r+this.chars);n.fill(0);for(let t=0;t<this.chars;t++)n[t]=e.codePointAt(t)??0}fill(t){this.set(0,t);let e=this.#s.subarray(0,this.chars);for(let t=1;t<this.length;t++)this.#s.set(e,t*this.chars)}*[Symbol.iterator](){for(let t=0;t<this.length;t++)yield this.get(t)}}function o(t){const e=(new TextDecoder).decode(t);return JSON.parse(e)}function c(t,e){const r=e/2,n=e-1;let a=0;for(let s=0;s<t.length;s+=e)for(let e=0;e<r;e+=1)a=t[s+e],t[s+e]=t[s+n-e],t[s+n-e]=a}function d(t){if("v2:object"===t)return globalThis.Array;let e=t.match(/v2:([US])(\d+)/);if(e){let[,t,r]=e;return("U"===t?i:s).bind(null,Number(r))}let r={int8:Int8Array,int16:Int16Array,int32:Int32Array,int64:globalThis.BigInt64Array,uint8:Uint8Array,uint16:Uint16Array,uint32:Uint32Array,uint64:globalThis.BigUint64Array,float16:globalThis.Float16Array,float32:Float32Array,float64:Float64Array,bool:a}[t];return _(r,`Unknown or unsupported data_type: ${t}`),r}function l(t,e){const r=t.length;"string"==typeof e&&(e="C"===e?Array.from({length:r},(t,e)=>e):Array.from({length:r},(t,e)=>r-1-e)),_(r===e.length,"Order length must match the number of dimensions.");let n=1,a=new Array(r);for(let r=e.length-1;r>=0;r--)a[e[r]]=n,n*=t[e[r]];return a}function u({name:t,configuration:e}){if("default"===t){const t=e?.separator??"/";return e=>["c",...e].join(t)}if("v2"===t){const t=e?.separator??".";return e=>e.join(t)||"0"}throw new Error(`Unknown chunk key encoding: ${t}`)}function h(t,e={}){let r=[],n=function(t){if("|O"===t)return{data_type:"v2:object"};let e=t.match(/^([<|>])(.*)$/);_(e,`Invalid dtype: ${t}`);let[,r,n]=e,a={b1:"bool",i1:"int8",u1:"uint8",i2:"int16",u2:"uint16",i4:"int32",u4:"uint32",i8:"int64",u8:"uint64",f2:"float16",f4:"float32",f8:"float64"}[n]??(n.startsWith("S")||n.startsWith("U")?`v2:${n}`:void 0);return _(a,`Unsupported or unknown dtype: ${t}`),"|"===r?{data_type:a}:{data_type:a,endian:"<"===r?"little":"big"}}(t.dtype);"F"===t.order&&r.push({name:"transpose",configuration:{order:"F"}}),"endian"in n&&"big"===n.endian&&r.push({name:"bytes",configuration:{endian:"big"}});for(let{id:e,...n}of t.filters??[])r.push({name:e,configuration:n});if(t.compressor){let{id:e,...n}=t.compressor;r.push({name:e,configuration:n})}return{zarr_format:3,node_type:"array",shape:t.shape,data_type:n.data_type,chunk_grid:{name:"regular",configuration:{chunk_shape:t.chunks}},chunk_key_encoding:{name:"v2",configuration:{separator:t.dimension_separator??"."}},codecs:r,fill_value:t.fill_value,attributes:e}}function f(t){return"sharding_indexed"===t?.name}function y(t){return"uint64"!==t.data_type&&"int64"!==t.data_type||null==t.fill_value?t.fill_value:BigInt(t.fill_value)}function p(t,...e){if(!e.some(e=>t instanceof e))throw t}function _(t,e=""){if(!t)throw new Error(e)}async function g(t,{format:e,signal:r}){const n=t instanceof Response?t:new Response(t);_(n.body,"Response does not contain body.");try{const t=new Response(n.body.pipeThrough(new DecompressionStream(e),{signal:r}));return await t.arrayBuffer()}catch{throw r?.throwIfAborted(),new Error(`Failed to decode ${e}`)}}class m{kind="array_to_array";constructor(t,e){_(t.keepbits>=0,"keepbits must be zero or positive")}static fromConfig(t,e){return new m(t,e)}encode(t){throw new Error("`BitroundCodec.encode` is not implemented. Please open an issue at https://github.com/manzt/zarrita.js/issues.")}decode(t){return t}}const b=function(){const t=new Uint32Array([305419896]);return!(18===new Uint8Array(t.buffer,t.byteOffset,t.byteLength)[0])}();function w(t){return"BYTES_PER_ELEMENT"in t?t.BYTES_PER_ELEMENT:4}class v{kind="array_to_bytes";#i;#o;#c;#d;#l;constructor(t,e){this.#l=t?.endian,this.#o=d(e.data_type),this.#d=e.shape,this.#i=l(e.shape,"C");const r=new this.#o(0);this.#c=r.BYTES_PER_ELEMENT}static fromConfig(t,e){return new v(t,e)}encode(t){let e=new Uint8Array(t.data.buffer);return b&&"big"===this.#l&&c(e,w(this.#o)),e}decode(t){return b&&"big"===this.#l&&c(t,w(this.#o)),{data:new this.#o(t.buffer,t.byteOffset,t.byteLength/this.#c),shape:this.#d,stride:this.#i}}}class k{kind="bytes_to_bytes";static fromConfig(){return new k}encode(t){throw new Error("Not implemented")}decode(t){return new Uint8Array(t.buffer,t.byteOffset,t.byteLength-4)}}class E{kind="bytes_to_bytes";static fromConfig(t){return new E}encode(t){throw new Error("Gzip encoding is not enabled by default. Please register a custom codec with `numcodecs/gzip`.")}async decode(t){const e=await g(t,{format:"gzip"});return new Uint8Array(e)}}function A(t,e){return _(!Number.isNaN(e),"JsonCodec allow_nan is false but NaN was encountered during encoding."),_(e!==Number.POSITIVE_INFINITY,"JsonCodec allow_nan is false but Infinity was encountered during encoding."),_(e!==Number.NEGATIVE_INFINITY,"JsonCodec allow_nan is false but -Infinity was encountered during encoding."),e}function U(t,e){return e instanceof Object&&!Array.isArray(e)?Object.keys(e).sort().reduce((t,r)=>(t[r]=e[r],t),{}):e}class x{configuration;kind="array_to_bytes";#u;#h;constructor(t={}){this.configuration=t;const{encoding:e="utf-8",skipkeys:r=!1,ensure_ascii:n=!0,check_circular:a=!0,allow_nan:s=!0,sort_keys:i=!0,indent:o,strict:c=!0}=t;let d=t.separators;d||(d=o?[", ",": "]:[",",":"]),this.#u={encoding:e,skipkeys:r,ensure_ascii:n,check_circular:a,allow_nan:s,indent:o,separators:d,sort_keys:i},this.#h={strict:c}}static fromConfig(t){return new x(t)}encode(t){const{indent:e,encoding:r,ensure_ascii:n,check_circular:a,allow_nan:s,sort_keys:i}=this.#u;_("utf-8"===r,"JsonCodec does not yet support non-utf-8 encoding.");const o=[];_(a,"JsonCodec does not yet support skipping the check for circular references during encoding."),s||o.push(A),i&&o.push(U);const c=Array.from(t.data);let d;c.push("|O"),c.push(t.shape),o.length&&(d=(t,e)=>{let r=e;for(let e of o)r=e(t,r);return r});let l=JSON.stringify(c,d,e);return n&&(l=l.replace(/[\u007F-\uFFFF]/g,t=>{const e=`0000${t.charCodeAt(0).toString(16)}`;return`\\u${e.substring(e.length-4)}`})),(new TextEncoder).encode(l)}decode(t){const{strict:e}=this.#h;_(e,"JsonCodec does not yet support non-strict decoding.");const r=o(t),n=r.pop();r.pop(),_(n,"0D not implemented for JsonCodec.");return{data:r,shape:n,stride:l(n,"C")}}}function T(t){if(t instanceof a||t instanceof s||t instanceof i){return new Proxy(t,{get:(t,e)=>t.get(Number(e)),set:(t,e,r)=>(t.set(Number(e),r),!0)})}return t}function z(t,e){let r=function(t,e){let r;return r=t.data instanceof s||t.data instanceof i?new t.constructor(t.data.length,t.data.chars):new t.constructor(t.data.length),{data:r,shape:t.shape,stride:l(t.shape,e)}}(t,e),n=t.shape.length,a=t.data.length,o=Array(n).fill(0),c=T(t.data),d=T(r.data);for(let e=0;e<a;e++){let a=0;for(let t=0;t<n;t++)a+=o[t]*r.stride[t];d[a]=c[e],o[0]+=1;for(let e=0;e<n;e++)if(o[e]===t.shape[e]){if(e+1===n)break;o[e]=0,o[e+1]+=1}}return r}function N(t,e){let r=function(t){return _(t.shape.length===t.stride.length,"Shape and stride must have the same length."),t.stride.map((t,e)=>({stride:t,index:e})).sort((t,e)=>e.stride-t.stride).map(t=>t.index)}(t);return _(r.length===e.length,"Orders must match"),r.every((t,r)=>t===e[r])}class M{kind="array_to_array";#f;#y;constructor(t,e){let r=t.order??"C",n=e.shape.length,a=new Array(n),s=new Array(n);if("C"===r)for(let t=0;t<n;++t)a[t]=t,s[t]=t;else if("F"===r)for(let t=0;t<n;++t)a[t]=n-t-1,s[t]=n-t-1;else a=r,a.forEach((t,e)=>{_(void 0===s[t],`Invalid permutation: ${JSON.stringify(r)}`),s[t]=e});this.#f=a,this.#y=s}static fromConfig(t,e){return new M(t,e)}encode(t){return N(t,this.#y)?t:z(t,this.#y)}decode(t){return{data:t.data,shape:t.shape,stride:l(t.shape,this.#f)}}}class ${kind="array_to_bytes";#d;#p;constructor(t){this.#d=t,this.#p=l(t,"C")}static fromConfig(t,e){return new $(e.shape)}encode(t){throw new Error("Method not implemented.")}decode(t){let e=new TextDecoder,r=new DataView(t.buffer),n=Array(r.getUint32(0,!0)),a=4;for(let s=0;s<n.length;s++){let i=r.getUint32(a,!0);a+=4,n[s]=e.decode(t.buffer.slice(a,a+i)),a+=i}return{data:n,shape:this.#d,stride:this.#p}}}class I{kind="bytes_to_bytes";static fromConfig(t){return new I}encode(t){throw new Error("Zlib encoding is not enabled by default. Please register a codec with `numcodecs/zlib`.")}async decode(t){const e=await g(t,{format:"deflate"});return new Uint8Array(e)}}const S=(new Map).set("blosc",()=>import("./chunks/BFspFXOv.esm.js").then(t=>t.default)).set("lz4",()=>import("./chunks/CeI1PA9E.esm.js").then(t=>t.default)).set("zstd",()=>import("./chunks/jUha9p1a.esm.js").then(t=>t.default)).set("gzip",()=>E).set("zlib",()=>I).set("transpose",()=>M).set("bytes",()=>v).set("crc32c",()=>k).set("vlen-utf8",()=>$).set("json2",()=>x).set("bitround",()=>m);function C(t){let e;return{async encode(r){e||(e=await L(t));for(const t of e.array_to_array)r=await t.encode(r);let n=await e.array_to_bytes.encode(r);for(const t of e.bytes_to_bytes)n=await t.encode(n);return n},async decode(r){e||(e=await L(t));for(let t=e.bytes_to_bytes.length-1;t>=0;t--)r=await e.bytes_to_bytes[t].decode(r);let n=await e.array_to_bytes.decode(r);for(let t=e.array_to_array.length-1;t>=0;t--)n=await e.array_to_array[t].decode(n);return n}}}async function L(t){let e,r=t.codecs.map(async t=>{let e=await(S.get(t.name)?.());return _(e,`Unknown codec: ${t.name}`),{Codec:e,meta:t}}),n=[],a=[];for await(let{Codec:s,meta:i}of r){let r=s.fromConfig(i.configuration,t);switch(r.kind){case"array_to_array":n.push(r);break;case"array_to_bytes":e=r;break;default:a.push(r)}}return e||(_("v2:object"!==t.data_type,`Cannot encode ${t.data_type} to bytes without a codec`),e=v.fromConfig({endian:"little"},t)),{array_to_array:n,array_to_bytes:e,bytes_to_bytes:a}}class O extends Error{constructor(t,e={}){super(`Node not found: ${t}`,e),this.name="NodeNotFoundError"}}class R extends Error{constructor(t){super(`Missing key: ${t}`),this.name="KeyError"}}const P=18446744073709551615n;function B(t,e,r,n){_(t.store.getRange,"Store does not support range requests");let a=t.store.getRange.bind(t.store),s=e.map((t,e)=>t/n.chunk_shape[e]),i=C({data_type:"uint64",shape:[...s,2],codecs:n.index_codecs}),o={};return async e=>{let n,c=e.map((t,e)=>Math.floor(t/s[e])),d=t.resolve(r(c)).path;if(d in o)n=o[d];else{let t=4,e=16*s.reduce((t,e)=>t*e,1),r=await a(d,{suffixLength:e+t});n=o[d]=r?await i.decode(r):null}if(null===n)return;let{data:l,shape:u,stride:h}=n,f=e.map((t,e)=>t%u[e]).reduce((t,e,r)=>t+e*h[r],0),y=l[f],p=l[f+1];return y!==P||p!==P?a(d,{offset:Number(y),length:Number(p)}):void 0}}class Y{store;path;constructor(t,e="/"){this.store=t,this.path=e}resolve(t){let e=new URL(`file://${this.path.endsWith("/")?this.path:`${this.path}/`}`);return new Y(this.store,decodeURIComponent(new URL(t,e).pathname))}}class j extends Y{kind="group";#_;constructor(t,e,r){super(t,e),this.#_=r}get attrs(){return this.#_.attributes}}function F(t){const e=t.find(t=>"transpose"===t.name);return e?.configuration?.order??"C"}const D=Symbol("zarrita.context");let J=class extends Y{kind="array";#_;[D];constructor(t,e,r){super(t,e),this.#_={...r,fill_value:y(r)},this[D]=function(t,e){let{configuration:r}=e.codecs.find(f)??{},n={encode_chunk_key:u(e.chunk_key_encoding),TypedArray:d(e.data_type),fill_value:e.fill_value};if(r){let a=F(r.codecs);return{...n,kind:"sharded",chunk_shape:r.chunk_shape,codec:C({data_type:e.data_type,shape:r.chunk_shape,codecs:r.codecs}),get_strides:t=>l(t,a),get_chunk_bytes:B(t,e.chunk_grid.configuration.chunk_shape,n.encode_chunk_key,r)}}let a=F(e.codecs);return{...n,kind:"regular",chunk_shape:e.chunk_grid.configuration.chunk_shape,codec:C({data_type:e.data_type,shape:e.chunk_grid.configuration.chunk_shape,codecs:e.codecs}),get_strides:t=>l(t,a),async get_chunk_bytes(e,r){let a=n.encode_chunk_key(e),s=t.resolve(a).path;return t.store.get(s,r)}}}(this,r)}get attrs(){return this.#_.attributes}get shape(){return this.#_.shape}get chunks(){return this[D].chunk_shape}get dtype(){return this.#_.data_type}async getChunk(t,e){let r=this[D],n=await r.get_chunk_bytes(t,e);if(!n){let t=r.chunk_shape.reduce((t,e)=>t*e,1),e=new r.TypedArray(t);return e.fill(r.fill_value),{data:e,shape:r.chunk_shape,stride:r.get_strides(r.chunk_shape)}}return r.codec.decode(n)}is(t){return function(t,e){if("number"!==e&&"bigint"!==e&&"boolean"!==e&&"object"!==e&&"string"!==e)return t===e;let r="bool"===t;if("boolean"===e)return r;let n=t.startsWith("v2:U")||t.startsWith("v2:S");if("string"===e)return n;let a="int64"===t||"uint64"===t;if("bigint"===e)return a;let s="v2:object"===t;return"object"===e?s:!(n||a||r||s)}(this.dtype,t)}};function V(t,e,r=null){return void 0===e&&(e=t,t=null),{start:t,stop:e,step:r}}class W extends Error{constructor(t){super(t),this.name="IndexError"}}function q(t,e){t.length>e.length&&function(t,e){throw new W(`too many indicies for array; expected ${e.length}, got ${t.length}`)}(t,e)}function G(t,e){return(t=Math.trunc(t))<0&&(t=e+t),(t>=e||t<0)&&function(t){throw new W(`index out of bounds for dimension with length ${t}`)}(e),t}class Q{dim_sel;dim_len;dim_chunk_len;nitems;constructor({dim_sel:t,dim_len:e,dim_chunk_len:r}){t=G(t,e),this.dim_sel=t,this.dim_len=e,this.dim_chunk_len=r,this.nitems=1}*[Symbol.iterator](){const t=Math.floor(this.dim_sel/this.dim_chunk_len),e=t*this.dim_chunk_len,r=this.dim_sel-e;yield{dim_chunk_ix:t,dim_chunk_sel:r}}}class H{start;stop;step;dim_len;dim_chunk_len;nitems;nchunks;constructor({dim_sel:t,dim_len:e,dim_chunk_len:r}){const[n,a,s]=function({start:t,stop:e,step:r},n){if(0===r)throw new Error("slice step cannot be zero");const a=(r=r??1)<0,[s,i]=a?[-1,n-1]:[0,n];return null===t?t=a?i:s:t<0?(t+=n)<s&&(t=s):t>i&&(t=i),null===e?e=a?s:i:e<0?(e+=n)<s&&(e=s):e>i&&(e=i),[t,e,r]}(t,e);this.start=n,this.stop=a,this.step=s,this.step<1&&function(){throw new W("only slices with step >= 1 are supported")}(),this.dim_len=e,this.dim_chunk_len=r,this.nitems=Math.max(0,Math.ceil((this.stop-this.start)/this.step)),this.nchunks=Math.ceil(this.dim_len/this.dim_chunk_len)}*[Symbol.iterator](){const t=Math.floor(this.start/this.dim_chunk_len),e=Math.ceil(this.stop/this.dim_chunk_len);for(const r of function*(t,e,r=1){void 0===e&&(e=t,t=0);for(let n=t;n<e;n+=r)yield n}(t,e)){const t=r*this.dim_chunk_len,e=Math.min(this.dim_len,(r+1)*this.dim_chunk_len),n=e-t;let a=0,s=0;if(this.start<t){const e=(t-this.start)%this.step;e&&(s+=this.step-e),a=Math.ceil((t-this.start)/this.step)}else s=this.start-t;const i=this.stop>e?n:this.stop-t,o=[s,i,this.step],c=[a,a+Math.ceil((i-s)/this.step),1];yield{dim_chunk_ix:r,dim_chunk_sel:o,dim_out_sel:c}}}}class K{dim_indexers;shape;constructor({selection:t,shape:e,chunk_shape:r}){this.dim_indexers=function(t,e){let r=[];return null===t?r=e.map(t=>V(null)):Array.isArray(t)&&(r=t.map(t=>t??V(null))),q(r,e),r}(t,e).map((t,n)=>new("number"==typeof t?Q:H)({dim_sel:t,dim_len:e[n],dim_chunk_len:r[n]})),this.shape=this.dim_indexers.filter(t=>t instanceof H).map(t=>t.nitems)}*[Symbol.iterator](){for(const t of function*(...t){if(0===t.length)return;const e=t.map(t=>t[Symbol.iterator]()),r=e.map(t=>t.next());if(r.some(t=>t.done))throw new Error("Input contains an empty iterator.");for(let n=0;;){if(r[n].done){if(e[n]=t[n][Symbol.iterator](),r[n]=e[n].next(),++n>=e.length)return}else yield r.map(({value:t})=>t),n=0;r[n]=e[n].next()}}(...this.dim_indexers)){const e=t.map(t=>t.dim_chunk_ix),r=t.map(t=>"dim_out_sel"in t?{from:t.dim_chunk_sel,to:t.dim_out_sel}:{from:t.dim_chunk_sel,to:null});yield{chunk_coords:e,mapping:r}}}}async function Z(t,e,r,n){let a=t[D];let s=new K({selection:e,shape:t.shape,chunk_shape:t.chunks}),i=n.prepare(new a.TypedArray(s.shape.reduce((t,e)=>t*e,1)),s.shape,a.get_strides(s.shape)),o=r.create_queue?.()??function(){const t=[];return{add:e=>t.push(e()),onIdle:()=>Promise.all(t)}}();for(const{chunk_coords:e,mapping:a}of s)o.add(async()=>{let{data:s,shape:o,stride:c}=await t.getChunk(e,r.opts),d=n.prepare(s,o,c);n.set_from_chunk(i,d,a)});return await o.onIdle(),0===s.shape.length?function(t,e){return"get"in t?t.get(e):t[e]}(i.data,0):i}function X(t,e=0,r){let n=r??t.length-e;return{length:n,subarray:(r,a=n)=>X(t,e+r,a-r),set(r,n=0){for(let a=0;a<r.length;a++)t[e+n+a]=r.get(a)},get:r=>t[e+r]}}function tt(t){return globalThis.Array.isArray(t.data)?{data:X(t.data),stride:t.stride,bytes_per_element:1}:{data:new Uint8Array(t.data.buffer,t.data.byteOffset,t.data.byteLength),stride:t.stride,bytes_per_element:t.data.BYTES_PER_ELEMENT}}const et={prepare:(t,e,r)=>({data:t,shape:e,stride:r}),set_scalar(t,e,r){let n=tt(t);at(n,e,function(t,e){if(globalThis.Array.isArray(t.data))return X([e]);let r=new(function(t){return"chars"in t?t.constructor.bind(null,t.chars):t.constructor}(t.data))([e]);return new Uint8Array(r.buffer,r.byteOffset,r.byteLength)}(t,r),n.bytes_per_element)},set_from_chunk(t,e,r){let n=tt(t);st(n,tt(e),n.bytes_per_element,r)}};async function rt(t,e=null,r={}){return Z(t,e,r,et)}function nt(t,e,r){return r<0&&e<t?Math.floor((t-e-1)/-r)+1:t<e?Math.floor((e-t-1)/r)+1:0}function at(t,e,r,n){if(0===e.length)return void t.data.set(r,0);const[a,...s]=e,[i,...o]=t.stride;if("number"==typeof a){return void at({data:t.data.subarray(i*a*n),stride:o},s,r,n)}const[c,d,l]=a,u=nt(c,d,l);if(0!==s.length)for(let e=0;e<u;e++){at({data:t.data.subarray(i*(c+l*e)*n),stride:o},s,r,n)}else for(let e=0;e<u;e++)t.data.set(r,i*(c+l*e)*n)}function st(t,e,r,n){const[a,...s]=n,[i,...o]=t.stride,[c,...d]=e.stride;if(null===a.from)return 0===s.length?void t.data.set(e.data.subarray(0,r),a.to*r):void st({data:t.data.subarray(i*a.to*r),stride:o},e,r,s);if(null===a.to){if(0===s.length){let n=a.from*r;return void t.data.set(e.data.subarray(n,n+r),0)}return void st(t,{data:e.data.subarray(c*a.from*r),stride:d},r,s)}const[l,u,h]=a.to,[f,y,p]=a.from,_=nt(l,u,h);if(0!==s.length)for(let n=0;n<_;n++)st({data:t.data.subarray(i*(l+n*h)*r),stride:o},{data:e.data.subarray(c*(f+n*p)*r),stride:d},r,s);else{if(1===h&&1===p&&1===i&&1===c){let n=f*r,a=_*r;return void t.data.set(e.data.subarray(n,n+a),l*r)}for(let n=0;n<_;n++){let a=c*(f+p*n)*r;t.data.set(e.data.subarray(a,a+r),i*(l+h*n)*r)}}}let it=function(){let t=new WeakMap;function e(e){let r=t.get(e)??{v2:0,v3:0};return t.set(e,r),r}return{increment(t,r){e(t)[r]+=1},version_max(t){let r=e(t);return r.v3>r.v2?"v3":"v2"}}}();async function ot(t,e){let{path:r}=t.resolve(".zarray"),n=await t.store.get(r);if(!n)throw new O("v2 array",{cause:new R(r)});return it.increment(t.store,"v2"),new J(t.store,t.path,h(o(n),e))}async function ct(t,e){let{path:r}=t.resolve(".zgroup"),n=await t.store.get(r);if(!n)throw new O("v2 group",{cause:new R(r)});return it.increment(t.store,"v2"),new j(t.store,t.path,function(t,e={}){return{zarr_format:3,node_type:"group",attributes:e}}(o(n),e))}async function dt(t,e={}){let r="store"in t?t.store:t,n=it.version_max(r),a="v2"===n?dt.v2:dt.v3,s="v2"===n?dt.v3:dt.v2;return a(t,e).catch(r=>(p(r,O),s(t,e)))}dt.v2=async function(t,e={}){let r="store"in t?t:new Y(t),n={};return(e.attrs??1)&&(n=await async function(t){let e=await t.store.get(t.resolve(".zattrs").path);return e?o(e):{}}(r)),"array"===e.kind?ot(r,n):"group"===e.kind?ct(r,n):ot(r,n).catch(t=>(p(t,O),ct(r,n)))},dt.v3=async function(t,e={}){let r="store"in t?t:new Y(t),n=await async function(t){let{store:e,path:r}=t.resolve("zarr.json"),n=await t.store.get(r);if(!n)throw new O("v3 array or group",{cause:new R(r)});let a=o(n);return"array"===a.node_type&&(a.fill_value=y(a)),"array"===a.node_type?new J(e,t.path,a):new j(e,t.path,a)}(r);if(it.increment(r.store,"v3"),void 0===e.kind)return n;if("array"===e.kind&&n instanceof J)return n;if("group"===e.kind&&n instanceof j)return n;let a=n instanceof J?"array":"group";throw new Error(`Expected node of kind ${e.kind}, found ${a}.`)};const lt=async({zarrUrl:t,riverId:e,idx:r,idVariable:n})=>{if(void 0===r&&void 0===e)throw new Error("Either 'riverId' or 'idx' must be provided.");return void 0===r&&(r=await ft({zarrUrl:t,variable:n,value:e})),r},ut=async({zarrUrl:t,variable:e,selection:r=null})=>{const a=new n(`${t}/${e}`),s=await dt.v2(a);return[...(await rt(s,r)).data]},ht=async({zarrUrl:t,variable:e})=>await ut({zarrUrl:t,variable:e,selection:[null]}),ft=async({zarrUrl:t,variable:e,value:r})=>(await ht({zarrUrl:t,variable:e})).indexOf(r),yt=async({zarrUrl:t})=>{const e=new n(`${t}/time`),r=await dt.v2(e),a=await rt(r,[null]),s=r.attrs.units,i=new Date(s.split("since")[1].trim()),o={seconds:1,minutes:60,hours:3600,days:86400}[s.split("since")[0].trim()];return[...a.data].map(t=>{let e=new Date(i);return e.setSeconds(e.getSeconds()+Number(t)*o),e})};var pt=Object.freeze({__proto__:null,forecast:async function({baseUrl:t,date:e,riverId:r,idx:n}){if(!/^\d{8}$/.test(e))throw new Error(`Invalid date format: ${e}. Must be YYYYMMDD.`);const a=`${t||"http://geoglows-v2-forecasts.s3-website-us-west-2.amazonaws.com"}/${e}00.zarr`,s=await lt({zarrUrl:a,riverId:r,idx:n,idVariable:"rivid"});let[i,o]=await Promise.all([yt({zarrUrl:a}),ut({zarrUrl:a,variable:"Qout",selection:[{start:0,stop:51,step:1},null,s]})]);const c=o.slice(0,i.length).map((t,e)=>isNaN(t)?-1:e).filter(t=>-1!==t),d=Array(51).fill(0).map((t,e)=>e*i.length);o=d.map(t=>o.slice(t,t+i.length)).map(t=>c.map(e=>t[e])),i=c.map(t=>i[t]);const l=(t=>{const e=t.length,r=t[0].length;let n={min:Array(r).fill(0),p20:Array(r).fill(0),p25:Array(r).fill(0),median:Array(r).fill(0),p75:Array(r).fill(0),p80:Array(r).fill(0),max:Array(r).fill(0),average:Array(r).fill(0)};return Array(r).fill(0).forEach((r,a)=>{const s=t.map(t=>t[a]).sort((t,e)=>t-e);n.min[a]=s[0],n.p20[a]=s[Math.floor(.2*e)],n.p25[a]=s[Math.floor(.25*e)],n.median[a]=s[Math.floor(.5*e)],n.p75[a]=s[Math.floor(.75*e)],n.p80[a]=s[Math.floor(.8*e)],n.max[a]=s[e-1],n.average[a]=s.reduce((t,e)=>t+e,0)/e}),n})(o);return{time:i,discharge:o,stats:l}},retrospective:async function({baseUrl:t,resolution:e="daily",riverId:r,idx:n}){if(!["hourly","daily","monthly","yearly"].includes(e))throw new Error(`Invalid resolution: ${e}. Must be one of 'hourly', 'daily', 'monthly', 'yearly'.`);["monthly","yearly"].includes(e)&&(e=`${e}-timeseries`);const a=`${t||"http://geoglows-v2.s3-us-west-2.amazonaws.com"}/retrospective/${e}.zarr`,s=await lt({zarrUrl:a,riverId:r,idx:n,idVariable:"river_id"}),[i,o]=await Promise.all([yt({zarrUrl:a}),ut({zarrUrl:a,variable:"Q",selection:[null,s]})]);return{time:i,discharge:o}},returnPeriods:async function({baseUrl:t,riverId:e,idx:r}){const n=`${t||"http://geoglows-v2.s3-us-west-2.amazonaws.com"}/retrospective/return-periods.zarr`,a=await lt({zarrUrl:n,riverId:e,idx:r,idVariable:"river_id"}),[s,i]=await Promise.all([ht({zarrUrl:n,variable:"return_period"}),ut({zarrUrl:n,variable:"gumbel",selection:[null,a]})]);return s.reduce((t,e,r)=>(t[Number(e)]=Number(i[r]),t),{})}});const _t={v2:pt};export{_t as default,pt as v2};
package/package.json CHANGED
@@ -1,14 +1,16 @@
1
1
  {
2
2
  "name": "riverforecastsystem",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Client for reading GEOGLOWS RFS data from publicly archived Zarr data on S3.",
5
5
  "license": "ISC",
6
6
  "author": "Riley Hales",
7
7
  "type": "module",
8
8
  "sideEffects": false,
9
+ "main": "dist/index.js",
10
+ "module": "dist/index.esm.js",
9
11
  "scripts": {
10
12
  "test": "node test/test.js",
11
- "build": "rollup -c rollup.config.js",
13
+ "build": "rm -rf dist && rollup -c rollup.config.js",
12
14
  "deploy": "npm run build && npm publish --access public"
13
15
  },
14
16
  "dependencies": {
@@ -19,5 +21,8 @@
19
21
  "@rollup/plugin-node-resolve": "^16.0.3",
20
22
  "@rollup/plugin-terser": "^0.4.4",
21
23
  "rollup": "^4.53.3"
22
- }
24
+ },
25
+ "files": [
26
+ "dist"
27
+ ]
23
28
  }