diff --git a/src/main/resources/n5.jq b/src/main/resources/n5.jq index c5f5c65..c87d840 100644 --- a/src/main/resources/n5.jq +++ b/src/main/resources/n5.jq @@ -290,6 +290,8 @@ def scaleTransform( $scales ): { "type" : "scale", "scale" : $scales }; def toTreePath: ltrimstr( "/") | split("/") | map_values( ["children", . ] ) | flatten; +def fromTreePath: if (length == 0) then "" else [.[range(1;length;2)]] | join("/") end; + def getSubTree( $path ): getpath( $path | toTreePath ); def moveSubTree( $srcPath; $dstPath ): getSubTree( $srcPath ) as $subTree | setpath( $dstPath | toTreePath; $subTree ) diff --git a/src/main/resources/zarr.jq b/src/main/resources/zarr.jq new file mode 100644 index 0000000..98c434c --- /dev/null +++ b/src/main/resources/zarr.jq @@ -0,0 +1,56 @@ +def cleanPath: . | sub("^\\.(?.*)\\/.+"; "\(.x)"; "g"); + +def containsExactString($query): reduce .[] as $i (false; . or ($i == $query)); + +def hasAllKeys($queryKeys): keys as $keys | + reduce ($queryKeys | .[] ) as $k (true; . and ( $keys | containsExactString($k))); + +def inExact($arr): reduce ($arr | .[]) as $v ([., false]; [.[0], .[1] or ($v == .[0])]) | .[1]; + +def zarrArrayKeys: ["zarr_format", "shape", "chunks", "dtype", "compressor", "fill_value", "order", + "dimension_separator", "filters"]; + +def zarrGroupKeys: ["zarr_format"]; + +def isZarrArray: type == "object" and hasAllKeys( zarrArrayKeys ); + +def zarrArrayObj: zarrArrayKeys as $za | with_entries(select(.key | inExact( $za ))); + +def zarrArrayAttrs: zarrArrayKeys as $za | with_entries(select(.key | inExact( $za ) | not)); + +def zarrGroupObj: { "zarr_format" : .zarr_format }; + +def zarrGroupAttrs: zarrGroupKeys as $zg | with_entries(select(.key | inExact( $zg ) | not)); + +def zarrAttrs: . as $obj | + if ($obj | isZarrArray) then + { + ".zarray" : ($obj | zarrArrayObj), + ".zattrs" : ($obj | zarrArrayAttrs) + } + else + { + ".zgroup" : ($obj | zarrGroupObj), + ".zattrs" : ($obj | zarrGroupAttrs) + } + end; + +def hasAttributes: type == "object" and has("attributes"); + +def zomTreePath: ltrimstr( "/") | split("/") | map_values( ["members", . ] ) | flatten; + +def zomAdd( $path; $attrs; $suffix ): + ($path | zomTreePath | . + $suffix) as $p | + getpath( $p ) as $currentAttrs | + setpath( $p; $currentAttrs + $attrs ); + +def zomAddFiles: + reduce inputs as $i ({}; + input_filename as $f | + ($f | cleanPath) as $p | + if ($f | endswith(".zattrs")) then + zomAdd( ($p | cleanPath); $i; ["attributes"] ) + else + zomAdd( ($p | cleanPath); $i; [] ) + end); +