import { isGroup , hasChildren } from '../guards' ; import type { Attribute , Datatype , Entity , Group , ScalarShape , Shape , ArrayShape , NumericType , StringType , DType , BooleanType , ComplexType , CompoundType , LinkClass , UnresolvedEntity , GroupWithChildren , } from '../models-hdf5' ; import { EntityKind , DTypeClass , Endianness } from '../models-hdf5' ; import type { NxInterpretation , SilxStyle } from '../models-nexus' ; import { buildEntityPath } from '../utils' ; import type { MockAttribute , MockDataset , MockValueId } from './models' ; import { mockValues } from './values' ; export const intType = { class : DTypeClass . Integer , endianness : Endianness . LE , size : 32 , } ; export const unsignedType = { class : DTypeClass . Unsigned , endianness : Endianness . LE , size : 16 , } ; export const floatType = { class : DTypeClass . Float , endianness : Endianness . LE , size : 64 , } ; export const stringType = { class : DTypeClass . String , charSet : 'ASCII' , } ; export const compoundType = { class : DTypeClass . Compound , fields : { int : intType } , } ; export const booleanType = { class : DTypeClass . Bool , } ; export const complexType = { class : DTypeClass . Complex , realType : floatType , imagType : floatType , } ; export function makeAttr < S extends Shape , T extends DType > ( name , type , shape , value ) { return { name , type , shape , value } ; } export function makeScalarAttr < T extends DType > ( name , type , value ) { return makeAttr ( name , type , [ ] , value ) ; } export function makeStrAttr ( name , value ) { return makeScalarAttr ( name , stringType , value ) ; } export function makeIntAttr ( name , value ) { return makeScalarAttr ( name , intType , value ) ; } export function withAttributes < T extends Entity > ( entity , attributes ) { return { ... entity , attributes : [ ... entity . attributes , ... attributes ] , } ; } export function withImageAttributes < T extends Entity > ( entity ) { return withAttributes ( entity , [ makeStrAttr ( 'CLASS' , 'IMAGE' ) , makeStrAttr ( 'IMAGE_VERSION' , '1.2' ) , ] ) ; } type EntityOpts = Partial < Pick < Entity , 'attributes' | 'link' > > ; type GroupOpts = EntityOpts & { isRoot ? : boolean ; children ? : Entity [ ] ; } ; type DatasetOpts = EntityOpts & { valueId ? : MockValueId ; } ; function prefixChildrenPaths ( group , parentPath ) { group . children . forEach ( ( c ) => { c . path = buildEntityPath ( parentPath , c . path . slice ( 1 ) ) ; if ( isGroup ( c ) && hasChildren ( c ) ) { prefixChildrenPaths ( c , parentPath ) ; } } ) ; } export function makeGroup ( name , children = [ ] , opts = { } ) { const { attributes = [ ] , link , isRoot = false } = opts ; const path = isRoot ? '/' : `/${ name }` ; const group = { name , path , kind : EntityKind . Group , children , attributes , link , } ; prefixChildrenPaths ( group , path ) ; return group ; } export function makeDataset < S extends Shape , T extends DType > ( name , type , shape , opts = { } ) { const { attributes = [ ] , valueId = name , link } = opts ; return { name , path : `/${ name }` , kind : EntityKind . Dataset , attributes , shape , type , value : mockValues [ valueId as MockValueId ] , link , } ; } export function makeScalarDataset < T extends DType > ( name , type , opts = { } ) { return makeDataset ( name , type , [ ] , opts ) ; } export function makeDatatype < T extends DType > ( name , type , opts = { } ) { const { attributes = [ ] , link } = opts ; return { name , path : `/${ name }` , kind : EntityKind . Datatype , attributes , type , link , } ; } export function makeUnresolvedEntity ( name , linkClass , pathToEntity ? , file ? ) { return { name , path : `/${ name }` , kind : EntityKind . Unresolved , attributes : [ ] , link : { class : linkClass , file , path : pathToEntity } , } ; } export function makeNxAxesAttr ( axes ) { return makeAttr ( 'axes' , stringType , [ axes . length ] , axes ) ; } export function makeNxAuxAttr ( aux ) { return makeAttr ( 'auxiliary_signals' , stringType , [ aux . length ] , aux ) ; } export function makeSilxStyleAttr ( style ) { const { signalScaleType , axisScaleTypes } = style ; return makeStrAttr ( 'SILX_style' , JSON . stringify ( { signal_scale_type : signalScaleType , axes_scale_type : axisScaleTypes , } ) ) ; } export function makeNxGroup ( name , type , opts = { } ) { const { defaultPath , children , ... groupOpts } = opts ; return makeGroup ( name , children , { ... groupOpts , attributes : [ ... ( groupOpts . attributes ?? [ ] ) , makeStrAttr ( 'NX_class' , type ) , ... ( defaultPath ? [ makeStrAttr ( 'default' , defaultPath ) ] : [ ] ) , ] , } ) ; } export function makeNxDataGroup < T extends Record < string , MockDataset < ArrayShape , NumericType > > > ( name , opts ) { const { signal , title , errors , silxStyle , axes = { } , axesAttr , auxiliary = { } , auxAttr , ... groupOpts } = opts ; return makeNxGroup ( name , 'NXdata' , { ... groupOpts , attributes : [ ... ( groupOpts . attributes ?? [ ] ) , makeStrAttr ( 'signal' , signal . name ) , ... ( axesAttr ? [ makeNxAxesAttr ( axesAttr ) ] : [ ] ) , ... ( silxStyle ? [ makeSilxStyleAttr ( silxStyle ) ] : [ ] ) , ... ( auxAttr ? [ makeNxAuxAttr ( auxAttr ) ] : [ ] ) , ] , children : [ signal , ... ( title ? [ title ] : [ ] ) , ... ( errors ? [ errors ] : [ ] ) , ... Object . values < MockDataset > ( axes ) , ... Object . values < MockDataset > ( auxiliary ) , ] , } ) ; } export function makeNxDataset < T extends NumericType | ComplexType > ( name , type , dims , opts = { } ) { const { interpretation , longName , units , ... datasetOpts } = opts ; return makeDataset ( name , type , dims , { ... datasetOpts , attributes : [ ... ( datasetOpts . attributes ?? [ ] ) , ... ( interpretation ? [ makeStrAttr ( 'interpretation' , interpretation ) ] : [ ] ) , ... ( longName ? [ makeStrAttr ( 'long_name' , longName ) ] : [ ] ) , ... ( units ? [ makeStrAttr ( 'units' , units ) ] : [ ] ) , ] , } ) ; } export function withNxInterpretation < T extends MockDataset < ArrayShape , NumericType | ComplexType > > ( dataset , interpretation ) { return withAttributes ( dataset , [ makeStrAttr ( 'interpretation' , interpretation ) , ] ) ; }