Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export as JCAMP-DX includes all metadata #3162

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 68 additions & 31 deletions src/component/modal/ExportAsJcampModal.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,54 @@
/** @jsxImportSource @emotion/react */
import { Checkbox, Dialog, DialogBody, DialogFooter } from '@blueprintjs/core';
import { Dialog, DialogBody, DialogFooter } from '@blueprintjs/core';
import { css } from '@emotion/react';
import { Spectrum } from 'nmr-load-save';
import { useForm } from 'react-hook-form';
import { useRef } from 'react';

import {
DataExportStage,
ExportAsJcampOptions,
exportAsJcamp,
} from '../../data/SpectraManager';
import { DataExportStage, exportAsJcamp } from '../../data/SpectraManager';
import { useToaster } from '../context/ToasterContext';
import ActionButtons from '../elements/ActionButtons';
import Label, { LabelStyle } from '../elements/Label';
import { Select2Controller } from '../elements/Select2Controller';
import { Select2 } from '../elements/Select2';
import useSpectrum from '../hooks/useSpectrum';

const initValues: ExportAsJcampOptions = {
onlyReal: true,
dataExportStage: 'PROCESSED',
};
interface ExportDataTypeItem {
label: string;
value: DataExportStage;
}

const originalFidExportDataTypes: ExportDataTypeItem[] = [
{
label: 'Original FID',
value: 'originalFid',
},
];

const originalFtDataTypes: ExportDataTypeItem[] = [
{
label: 'Original FT real',
value: 'originalFtReal',
},
{
label: 'Original FT real and imaginary',
value: 'originalFtRealImaginary',
},
];
const processedDataTypes: ExportDataTypeItem[] = [
{
label: 'Processed real',
value: 'processedReal',
},
{
label: 'Processed real and imaginary',
value: 'processedRealImaginary',
},
];

const labelStyle: LabelStyle = {
wrapper: { display: 'flex', height: '100%', flex: 1 },
container: { alignItems: 'flex-start' },
label: { paddingTop: '5px', width: 80 },
};

const DATA_STAGES = Object.keys(DataExportStage).map((key) => ({
label: key,
value: DataExportStage[key],
}));

interface InnerExportAsJCAMPProps {
closeDialog: () => void;
spectrum?: Spectrum;
Expand All @@ -56,19 +74,37 @@ function ExportAsJcampModal(props: ExportAsJCAMPProps) {
return <InnerExportAsJcampModal spectrum={currentSpectrum} {...props} />;
}

function getExportDataTypes(spectrum: Spectrum) {
const { originalInfo, filters } = spectrum;

const menuItems: ExportDataTypeItem[] = [];
if (originalInfo?.isFt) {
menuItems.push(...originalFtDataTypes);
}

if (originalInfo?.isFid) {
menuItems.push(...originalFidExportDataTypes);
}

if (filters?.length > 0) {
menuItems.push(...processedDataTypes);
}

return menuItems;
}

function InnerExportAsJcampModal(props: Required<InnerExportAsJCAMPProps>) {
const { closeDialog, spectrum } = props;
const { control, handleSubmit, register } = useForm<ExportAsJcampOptions>({
defaultValues: initValues,
});
const toaster = useToaster();
const exportDataTypes = getExportDataTypes(spectrum);
const exportDataAsRef = useRef<DataExportStage>(exportDataTypes[0].value);

function submitHandler(options: ExportAsJcampOptions) {
function submitHandler() {
const hideLoading = toaster.showLoading({
message: 'export as JCAMP-DX in progress',
});
try {
exportAsJcamp(spectrum, options);
exportAsJcamp(spectrum, exportDataAsRef.current);
} catch (error: any) {
toaster.show({ message: error.message, intent: 'danger' });
} finally {
Expand All @@ -77,6 +113,10 @@ function InnerExportAsJcampModal(props: Required<InnerExportAsJCAMPProps>) {
}
}

function handleChangeExportDataType(data) {
exportDataAsRef.current = data.value;
}

return (
<Dialog
isOpen
Expand All @@ -91,20 +131,17 @@ function InnerExportAsJcampModal(props: Required<InnerExportAsJCAMPProps>) {
`}
>
<Label title="Data" style={labelStyle}>
<Select2Controller
control={control}
name="dataExportStage"
items={DATA_STAGES}
<Select2
defaultSelectedItem={exportDataTypes[0]}
onItemSelect={handleChangeExportDataType}
items={exportDataTypes}
/>
</Label>
<Label title="Only real" style={labelStyle}>
<Checkbox {...register('onlyReal')} />
</Label>
</DialogBody>
<DialogFooter>
<ActionButtons
style={{ flexDirection: 'row-reverse', margin: 0 }}
onDone={() => void handleSubmit(submitHandler)()}
onDone={submitHandler}
doneLabel="export"
onCancel={() => {
closeDialog?.();
Expand Down
65 changes: 23 additions & 42 deletions src/data/SpectraManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,61 +125,42 @@ export function toJSON(
}
}

export const DataExportStage = {
Original: 'ORIGINAL',
Processed: 'PROCESSED',
} as const;

export type DataExportStageStrings =
(typeof DataExportStage)[keyof typeof DataExportStage];

export interface ExportAsJcampOptions {
onlyReal?: boolean;
dataExportStage?: DataExportStageStrings;
}
export type DataExportStage =
| 'originalFid'
| 'originalFtReal'
| 'originalFtRealImaginary'
| 'processedReal'
| 'processedRealImaginary';

export function exportAsJcamp(
spectrum: Spectrum,
options: ExportAsJcampOptions = {},
dataExportStage: DataExportStage,
) {
let jcamp: string | null = null;
if (isSpectrum1D(spectrum)) {
const { dataExportStage = 'PROCESSED', onlyReal } = options;
const { originalData, originalInfo, data, info, filters } = spectrum;
if (!isSpectrum1D(spectrum)) {
throw new Error('convert 2D spectrum to JCAMP is not supported');
}
const { originalData, originalInfo, ...otherSpectrum } = spectrum;

if (onlyReal && info.isFid) {
throw new Error('FID data should be complex');
}
const exportedSpectrum: Spectrum = { ...otherSpectrum };

if (!['processedReal', 'processedRealImaginary'].includes(dataExportStage)) {
if (!originalData || !originalInfo) {
throw new Error('original data should exists');
}

if (dataExportStage === 'PROCESSED') {
jcamp = spectrum1DToJcamp(
{
...spectrum,
data,
info,
filters,
},
{ onlyReal },
);
} else {
jcamp = spectrum1DToJcamp(
{
...spectrum,
data: originalData,
info: originalInfo,
filters: [],
},
{ onlyReal },
);
}
} else {
throw new Error('convert 2D spectrum to JCAMP is not supported');
exportedSpectrum.filters = [];
exportedSpectrum.data = originalData;
exportedSpectrum.info = originalInfo;
}

const onlyReal =
dataExportStage === 'processedReal' || dataExportStage === 'originalFtReal';

jcamp = spectrum1DToJcamp(exportedSpectrum, {
onlyReal,
});

if (!jcamp) {
throw new Error('convert spectrum to JCAMP failed');
}
Expand Down
Loading