-
Notifications
You must be signed in to change notification settings - Fork 0
/
INDIGOSekonicFileProcessor.m
277 lines (238 loc) · 10.4 KB
/
INDIGOSekonicFileProcessor.m
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
function spectrometerFileList = ...
INDIGOSekonicFileProcessor(defaultPathSpectrometerFiles, ...
spectrometerFileExtension, outputFolderBackUp, copyFiles, writeBOM, ...
saveJSONFile)
%INDIGOSEKONICFILEPROCESSOR imports original Sekonic C-7000 spectrometer
% *.CSV files and turns them into English only content (if the original
% file had German content). Files can also be saved as properly
% structured JSON files. A MATLAB structure called "spectrometerFileList"
% holding the data of all spectrometer files will be created as well.
%
% Input
% -----
% DEFAULTPATHSPECTROMETERFILES The default path this function will
% open when loading Sekonic files.
%
%
% Outputs
% -------
% SPECTROMETERSFILELIST The filelist generated.
%
%
% Usage
% -----
% INDIGOSekonicFileProcessor.
%
%
% Remarks
% -------
% None.
%
%
% Dependencies
% ------------
% this function relies on CreateUUID.m
% GetFilesInFolder.m
% ImportSekonicFile.m
% SaveSekonicFile.m
%
%
% History:
% --------
% 2023-09 Function created.
% 2023-09-22 Function expanded and optimised: header added, creation and
% saving of MATLAB structure optimised.
% 2023-10-12 Change GetINDIGOFilesInFolder to GetFilesInFolder.
% 2023-10-12 Updated function description.
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Created: 2023-09 by Geert J. Verhoeven @ project INDIGO
% Last modified: 2023-10-25 by Geert J. Verhoeven
% Author: Geert Verhoeven
% e-mail: geert [at] projectindigo [dot] eu
% Release: 1.0
% Release date: 2023-09-21
% Full research at: https://projectindigo.eu
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
%% Check the input and set some variables. %%
% ===================================================================== %
if ~exist('defaultPathSpectrometerFiles', 'var') || ...
isempty (defaultPathSpectrometerFiles) || ...
(~isstring(defaultPathSpectrometerFiles) && ...
~ischar(defaultPathSpectrometerFiles))
defaultPathSpectrometerFiles = "D:\SpectrometerTest";
end
if ~exist('spectrometerFileExtension', 'var') || ...
isempty (spectrometerFileExtension) || ...
(~isstring(spectrometerFileExtension) && ...
~ischar(spectrometerFileExtension))
spectrometerFileExtension = '*.csv';
end
if ~exist('outputFolderBackUp', 'var') || isempty (outputFolderBackUp) || ...
(~isstring(outputFolderBackUp) && ~ischar(outputFolderBackUp))
outputFolderBackUp = 'InitialFiles';
end
if ~exist('copyFiles', 'var') || isempty (copyFiles) || ~islogical(copyFiles)
copyFiles = false;
end
if ~exist('writeBOM', 'var') || isempty (writeBOM) || ~islogical(writeBOM)
writeBOM = false;
end
if ~exist('saveJSONFile', 'var') || isempty (saveJSONFile) || ...
~islogical(saveJSONFile)
saveJSONFile = true;
end
doSpectrometerFilesExist = true;
subtractionScalar = 0; % To ensure files start with sequence number 0001.
%% Make a list of all the spectrometer CSV files. %%
% ===================================================================== %
spectrometerFolderChosen = uigetdir(defaultPathSpectrometerFiles,...
'Choose the folder with spectrometer *.CSV files');
if spectrometerFolderChosen == 0 % The user cancelled.
doSpectrometerFilesExist = false;
else
spectrometerFileList = GetFilesInFolder (spectrometerFolderChosen, ...
spectrometerFileExtension);
if isempty (spectrometerFileList)
doSpectrometerFilesExist = false;
end
end
if ~doSpectrometerFilesExist
spectrometerFileList = [];
fprintf(['WARNING! No spectrometer *.CSV files were found in the ', ...
'selected folder. \n'])
return
end
clearvars defaultPathSpectrometerFiles spectrometerFileExtension
clearvars doSpectrometerFilesExist
%% Get all the metadata and related info for the spectrometer files. %%
% ===================================================================== %
% All metadata for the spectrometer files.
spectrometerFileCount = length (spectrometerFileList);
for kk = 1:spectrometerFileCount
spectrometerFileList(kk).allData =...
ImportSekonicFile(spectrometerFileList(kk). filePath);
end
%% Fill out the acquisition data and new file properties. %%
% ===================================================================== %
% Extract the date from the folder name.
backSlashIndices = strfind(spectrometerFolderChosen, '\');
acquisitionDate = spectrometerFolderChosen(backSlashIndices(end)+1:end);
for kk = 1:spectrometerFileCount
spectrometerFileList(kk).allData.fileProperties.acquisitionDate = ...
string(acquisitionDate);
% Generate the new file name.
tempFileName = spectrometerFileList(kk).allData.fileProperties.name;
tempFileName = char(tempFileName);
hyphenMinusIndices = strfind(tempFileName, '-');
underscoreIndices = strfind(tempFileName, '_');
allIndices = [hyphenMinusIndices, underscoreIndices];
instrument = tempFileName(allIndices(1)+1:allIndices(3)-1);
sequenceNumber = ...
str2double(tempFileName(allIndices(3)+1:allIndices(4)-1));
if kk == 1
subtractionScalar = 1 - sequenceNumber;
end
if subtractionScalar > 0
error ('myApp:argChk', ...
'Abort. The starting sequency number was lower than 1.')
end
% Subtract the value computed above to ensure all files start with
% sequence number 0001.
sequenceNumber = subtractionScalar + sequenceNumber;
% Ensure the number has four digits.
sequenceNumber = sprintf('%04d', sequenceNumber);
% One could also just generate a sequence number starting with 0001.
% The sequential numbering created in this workflow makes this it clear
% if files have been deleted after they were downloaded.
newFileName = ...
['INDIGO_', acquisitionDate, '_', instrument, '_', sequenceNumber];
spectrometerFileList(kk).allData.fileProperties.name = ...
string(newFileName);
% Update some file properties.
spectrometerFileList(kk).allData.fileProperties.derivedFrom.instanceID = ...
spectrometerFileList(kk).allData.fileProperties.instanceID;
spectrometerFileList(kk).allData.fileProperties.instanceID = ...
CreateUUID; % (xmpMM:InstanceID)
spectrometerFileList(kk).allData.fileProperties.modifyDate = ...
string(datetime('now', 'Format', 'yyyy-MM-dd HH:mm:ss'));
spectrometerFileList(kk).allData.fileProperties.creatorTool = ...
"INDIGOSekonicFileProcessor.m";
historyString = ['File name, instance ID, creator tool and modify ', ...
'date updated with the INDIGOSekonicFileProcessor.m function.'];
spectrometerFileList(kk).allData.fileProperties.history = ...
[spectrometerFileList(kk).allData.fileProperties.history; ...
string(historyString)];
end
clearvars backSlashIndices acquistionDate kk subtractionScalar instrument
clearvars tempFileName hyphenMinusIndices underscoreIndices allIndices
clearvars sequenceNumber newFileName acquisitionDate historyString
%% Make a backup of the original files. %%
% ===================================================================== %
% Create a subfolder to which the original files should be copied/moved.
pathBUFolder = [spectrometerFolderChosen, '\', outputFolderBackUp];
if ~exist(pathBUFolder, 'dir')
mkdir(pathBUFolder)
end
for kk = 1:spectrometerFileCount
fileToCopy = spectrometerFileList(kk).filePath;
if copyFiles
copyfile(fileToCopy, pathBUFolder)
else % Move image files.
movefile(fileToCopy, pathBUFolder)
end
end
clearvars outputFolderBackUp copyFiles fileToCopy pathBUFolder
%% Save the new CSV files. %%
% ===================================================================== %
% Save the files as CSV.
for kk = 1:spectrometerFileCount
pathNewFile = [char(spectrometerFolderChosen), '\', ...
char(spectrometerFileList(kk).allData.fileProperties.name)];
SaveSekonicFile (spectrometerFileList(kk).allData, pathNewFile, ...
writeBOM, saveJSONFile);
end
clearvars pathNewFile writeBOM saveJSONFile
%% Save the spectrometer filelist. %%
% ===================================================================== %
% Update the file name, folder and file paths in the structure. Move the
% file properties, the instrument properties and the instrument data one
% level up in the structure.
for kk = 1:spectrometerFileCount
spectrometerFileList(kk).fileName = ...
[char(spectrometerFileList(kk).allData.fileProperties.name), '.csv'];
spectrometerFileList(kk). fileProperties = ...
spectrometerFileList(kk).allData.fileProperties;
spectrometerFileList(kk). instrumentProperties = ...
spectrometerFileList(kk).allData.instrumentProperties;
spectrometerFileList(kk). instrumentData = ...
spectrometerFileList(kk).allData.instrumentData;
end
% Remove the date, bytes, folderPath, filePath and allData fields.
spectrometerFileList = rmfield(spectrometerFileList, ...
{'date', 'bytes', 'folderPath', 'filePath', 'allData'});
% Rename the fileName column.
[spectrometerFileList.spectrometerFile] = spectrometerFileList.fileName;
spectrometerFileList = orderfields(spectrometerFileList,[1:0,5,1:4]);
spectrometerFileList = rmfield(spectrometerFileList,'fileName');
% Save as MATLAB *.mat file.
matFileName = ...
strcat(spectrometerFileList(1).fileProperties.name, "-", ...
sprintf('%04d', spectrometerFileCount));
pathMatFile = strcat(spectrometerFolderChosen, "\", matFileName);
save(pathMatFile, 'spectrometerFileList')
% Save as JSON file. First get the JSON file name.
pathJSONFile = [char(pathMatFile), '.json'];
% Convert struct to a character vector in JSON format.
jsonText = jsonencode(spectrometerFileList, PrettyPrint=false);
% PrettyPrint makes the JSON file much nicer formatted, but it puts all
% numbers of a vector on a different line. To avoid that, do not use it
% within MATLAB but use Prettier inside Visual Studio Code.
% Write to a JSON file.
fileIDJSON = fopen(pathJSONFile, 'w');
fprintf(fileIDJSON, '%s', jsonText);
fclose(fileIDJSON);
clearvars matFileName pathMatFile kk pathJSONFile fileIDJSON jsonText
clearvars spectrometerFolderChosen ans spectrometerFileCount
end