-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveSekonicFile.m
205 lines (180 loc) · 9.11 KB
/
SaveSekonicFile.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
function SaveSekonicFile (spectrometerStructure, pathNewFile, writeBOM, saveJSONFile)
% spectrometerStructure = a structure made with ImportSekonicFile.
% pathNewFile is the folder path and the name of the file to be saved,
% without extension.
% The numbers are stored to correspond with the original file. For example,
% spectral data always have 12 decimals in the original Sekonic files, also
% if the last 8 decimals are 0. The numbers for the TM-30 Colour Vector
% Graphic have no fixed length. They usually have decimals, but if the last
% 2 decimals are 0, they only have 5 decimals. This function respects that,
% so it generates a *.CSV file as identical as possible to the initial
% *.CSV file by Sekonic.
%% Check the input and set some variables. %%
% ===================================================================== %
% Check if the spectrometer structure exists.
if ~exist('spectrometerStructure', 'var') || ...
isempty (spectrometerStructure) || ~isstruct(spectrometerStructure)
error ('myApp:argChk', 'Abort. No spectrometer structure was provided')
else % Split in substructures with shorter names for cleaner code.
fileProp = spectrometerStructure.fileProperties;
instrProp = spectrometerStructure.instrumentProperties;
radiomData = spectrometerStructure.instrumentData.radiometricData;
photomData = spectrometerStructure.instrumentData.photometricData;
colorimData = spectrometerStructure.instrumentData.colorimetricData;
colorimData.CRM = colorimData.colourRenderingMetrics;
end
if ~exist('writeBOM', 'var') || isempty (writeBOM) || ~islogical (writeBOM)
writeBOM = false;
end
if ~exist('saveJSONFile', 'var') || isempty (saveJSONFile) ||...
~islogical (saveJSONFile)
saveJSONFile = false;
end
% [fid, message] = fopen ('C:\Users\Geert\Downloads\CHECK\YourFile1.csv', 'wt');
pathNewCSVFile = [char(pathNewFile), '.csv'];
[fileIDCSV, message] = fopen (pathNewCSVFile, 'wt');
assert (fileIDCSV > 0, message);
% Microsoft Excel never defaults to UTF-8 when opening CSV files, even when
% the file is encoded as UTF-8. Excel can, however, read the encoding
% properly if it finds a special signature string at the beginning of a CSV
% file to determine its encoding. This string is the UTF-8 BOM marker, equal
% to the hexadecimal byte sequence EF BB BF. Adding those three bytes as a
% prefix thus solves the issue.
if writeBOM
fwrite(fileIDCSV, [239; 187; 191]); % This writes the UTF-8 BOM marker.
end
fprintf(fileIDCSV, 'Date Saved, %s\n', strrep(fileProp.saveDate, '-', '/'));
fprintf(fileIDCSV, 'Title,%s\n', fileProp.name);
fprintf(fileIDCSV, '\n');
fprintf(fileIDCSV, 'Measuring Mode,%s\n', instrProp.settings.measuringMode);
fprintf(fileIDCSV, 'Viewing Angle [°],%s\n', ...
num2str(instrProp.settings.viewingAngle_degrees));
fprintf(fileIDCSV, 'Tcp [K],%s\n', num2str(colorimData.CCT_kelvin));
fprintf(fileIDCSV, '⊿uv,%.4f\n', colorimData.deltaUV);
fprintf(fileIDCSV, 'Illuminance [lx],%s\n', ...
num2str(photomData.illuminance_lux));
fprintf(fileIDCSV, 'Illuminance [fc],%s\n', ...
num2str(photomData.illuminance_footcandle));
fprintf(fileIDCSV, 'Peak Wavelength [nm],%s\n', ...
num2str(radiomData.peakWavelenght_nm));
if instrProp.settings.viewingAngle_degrees == 2
fprintf(fileIDCSV, 'Tristimulus Value X,%.4f\n', ...
colorimData.tristimulusValues.CIE1931_X);
fprintf(fileIDCSV, 'Tristimulus Value Y,%.4f\n', ...
colorimData.tristimulusValues.CIE1931_Y);
fprintf(fileIDCSV, 'Tristimulus Value Z,%.4f\n', ...
colorimData.tristimulusValues.CIE1931_Z);
fprintf(fileIDCSV, 'CIE1931 x,%.4f\n', ...
colorimData.chromaticityCoordinates.CIE1931_x);
fprintf(fileIDCSV, 'CIE1931 y,%.4f\n', ...
colorimData.chromaticityCoordinates.CIE1931_y);
fprintf(fileIDCSV, 'CIE1931 z,%.4f\n', ...
colorimData.chromaticityCoordinates.CIE1931_z);
fprintf(fileIDCSV, '%s,%.4f\n', "CIE1976 u'", ...
colorimData.chromaticityCoordinates.CIE1976_uprime);
fprintf(fileIDCSV, '%s,%.4f\n', "CIE1976 v'", ...
colorimData.chromaticityCoordinates.CIE1976_vprime);
elseif instrProp.settings.viewingAngle_degrees == 10
fprintf(fileIDCSV, 'Tristimulus Value X₁₀,%.4f\n', ...
colorimData.tristimulusValues.CIE1964_X10);
fprintf(fileIDCSV, 'Tristimulus Value Y₁₀,%.4f\n', ...
colorimData.tristimulusValues.CIE1964_Y10);
fprintf(fileIDCSV, 'Tristimulus Value Z₁₀,%.4f\n', ...
colorimData.tristimulusValues.CIE1964_Z10);
fprintf(fileIDCSV, 'CIE1964 x₁₀,%.4f\n', ...
colorimData.chromaticityCoordinates.CIE1964_x10);
fprintf(fileIDCSV, 'CIE1964 y₁₀,%.4f\n', ...
colorimData.chromaticityCoordinates.CIE1964_y10);
fprintf(fileIDCSV, 'CIE1964 z₁₀,%.4f\n', ...
colorimData.chromaticityCoordinates.CIE1964_z10);
fprintf(fileIDCSV, '%s,%.4f\n', "CIE1976 u'₁₀", ...
colorimData.chromaticityCoordinates.CIE1976_uprime10);
fprintf(fileIDCSV, '%s,%.4f\n', "CIE1976 v'₁₀", ...
colorimData.chromaticityCoordinates.CIE1976_vprime10);
else
error ('myApp:argChk', ['Abort. The spectrometer structure does ', ...
'not have the correct measurement degrees stored.'])
end
fprintf(fileIDCSV, 'Dominant Wavelength [nm],%s\n', ...
num2str(colorimData.HelmholtzCoordinates.dominantWavelength_nm));
fprintf(fileIDCSV, '%s,%s\n', 'Purity [%]', ...
num2str(colorimData.HelmholtzCoordinates.excitationPurity_percentage));
fprintf(fileIDCSV, 'PPFD [umolm⁻²s⁻¹],%s\n', ...
num2str(radiomData.PPFD_umolmDIVm2DIVs));
fprintf(fileIDCSV, 'CRI Ra,%.1f\n', colorimData.CRM.CRI.Ra);
fprintf(fileIDCSV, 'CRI R1,%.1f\n', colorimData.CRM.CRI.R1);
fprintf(fileIDCSV, 'CRI R2,%.1f\n', colorimData.CRM.CRI.R2);
fprintf(fileIDCSV, 'CRI R3,%.1f\n', colorimData.CRM.CRI.R3);
fprintf(fileIDCSV, 'CRI R4,%.1f\n', colorimData.CRM.CRI.R4);
fprintf(fileIDCSV, 'CRI R5,%.1f\n', colorimData.CRM.CRI.R5);
fprintf(fileIDCSV, 'CRI R6,%.1f\n', colorimData.CRM.CRI.R6);
fprintf(fileIDCSV, 'CRI R7,%.1f\n', colorimData.CRM.CRI.R7);
fprintf(fileIDCSV, 'CRI R8,%.1f\n', colorimData.CRM.CRI.R8);
fprintf(fileIDCSV, 'CRI R9,%.1f\n', colorimData.CRM.CRI.R9);
fprintf(fileIDCSV, 'CRI R10,%.1f\n', colorimData.CRM.CRI.R10);
fprintf(fileIDCSV, 'CRI R11,%.1f\n', colorimData.CRM.CRI.R11);
fprintf(fileIDCSV, 'CRI R12,%.1f\n', colorimData.CRM.CRI.R12);
fprintf(fileIDCSV, 'CRI R13,%.1f\n', colorimData.CRM.CRI.R13);
fprintf(fileIDCSV, 'CRI R14,%.1f\n', colorimData.CRM.CRI.R14);
fprintf(fileIDCSV, 'CRI R15,%.1f\n', colorimData.CRM.CRI.R15);
fprintf(fileIDCSV, 'TM-30 Rf,%s\n', num2str(colorimData.CRM.TM30.Rf));
fprintf(fileIDCSV, 'TM-30 Rg,%s\n', num2str(colorimData.CRM.TM30.Rg));
fprintf(fileIDCSV, 'SSIt,%s\n', num2str(colorimData.CRM.SSI.SSIt));
fprintf(fileIDCSV, 'SSId,%s\n', num2str(colorimData.CRM.SSI.SSId));
if ~isnan(colorimData.CRM.SSI.SSI1)
fprintf(fileIDCSV, 'SSI1,%s\n', num2str(colorimData.CRM.SSI.SSI1));
else
fprintf(fileIDCSV, 'SSI1,---\n');
end
if ~isnan(colorimData.CRM.SSI.SSI2)
fprintf(fileIDCSV, 'SSI1,%s\n', num2str(colorimData.CRM.SSI.SSI2));
else
fprintf(fileIDCSV, 'SSI2,---\n');
end
fprintf(fileIDCSV, 'TLCI,%s\n', num2str(colorimData.CRM.TLCI));
if ~isnan(colorimData.CRM.TLMF)
fprintf(fileIDCSV, 'TLMF,%s\n', num2str(colorimData.CRM.TLMF));
else
fprintf(fileIDCSV, 'TLMF,---\n');
end
fprintf(fileIDCSV, '\n');
for i = 1:81
variableName = ['Spectral Data ', num2str(375 + i * 5), '[nm]'];
fprintf(fileIDCSV, '%s,%.12f\n', variableName, ...
radiomData.spectralPowerDistribution_5nm.spectralIrradiance_WDIVm2DIVnm(i));
end
fprintf(fileIDCSV, '\n');
for i = 1:401
variableName = ['Spectral Data ', num2str(379 + i * 1), '[nm]'];
fprintf(fileIDCSV, '%s,%.12f\n', variableName, ...
radiomData.spectralPowerDistribution_1nm.spectralIrradiance_WDIVm2DIVnm(i));
end
fprintf(fileIDCSV, '\n');
fprintf(fileIDCSV, ['TM-30 Color Vector Graphic,Reference Illuminant x,'...
'Reference Illuminant y,Measured Illuminant x,Measured Illuminant y\n']);
for i = 1:16
variableName = ['bin', num2str(i)];
fprintf(fileIDCSV, '%s,%s,%s,%s,%s\n', variableName, ...
num2str(colorimData.CRM.TM30.colourVectorGraphic.referenceIlluminantx(i), 9), ...
num2str(colorimData.CRM.TM30.colourVectorGraphic.referenceIlluminanty(i), 9), ...
num2str(colorimData.CRM.TM30.colourVectorGraphic.measuredIlluminantx(i), 9), ...
num2str(colorimData.CRM.TM30.colourVectorGraphic.measuredIlluminanty(i), 9));
end
fclose(fileIDCSV);
clearvars fileIDCSV message variableName i pathNewCSVFile
%% Save as JSON if wanted.
if saveJSONFile
% Get the JSON file name.
pathNewJSONFile = [char(pathNewFile), '.json'];
% Convert struct to a character vector in JSON format.
jsonText = jsonencode(spectrometerStructure, 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(pathNewJSONFile, 'w');
fprintf(fileIDJSON, '%s', jsonText);
fclose(fileIDJSON);
end
clearvars fileIDJSON
end