-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitMetaData.m
More file actions
202 lines (186 loc) · 6.03 KB
/
Copy pathinitMetaData.m
File metadata and controls
202 lines (186 loc) · 6.03 KB
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
function metaData = initMetaData(Header,GPS,seq,stat,dupl,HZ,instrumentType)
% Usage: metaData = initMetaData(Header,GPS,seq,stat,dupl,HZ,instrumentType)
if nargin < 5
dupl = [];
end
if nargin < 6
HZ = 1;
end
if nargin < 7
instrumentType = 'NIMS';
end
NO_HEADER = 0;
if nargin > 0
if isempty(Header)
NO_HEADER = 1;
end
else
Header = '';
NO_HEADER = 1;
end
try
[runID,sensorID,siteName,Ex_wire,Ey_wire,GroundEinfo,nims_comments,H_comments,operator] = parseHeader(Header);
catch exception
disp(['Header not processed due to ' exception.identifier ' error.']);
disp('Do NOT edit the original DATA.BIN file to avoid introducing data gaps in the binary part.');
disp('Instead, manually rename the file to *.nim, save the header in a *.hdr file and edit the header.');
[runID,sensorID,siteName,Ex_wire,Ey_wire,GroundEinfo,nims_comments,H_comments,operator] = parseHeader;
end
if nargin >= 4
[lat, lon, elev, decl, decl_info, gain_char, error_flag, GPS_comments] = parseGPS(GPS);
else
[lat, lon, elev, decl, decl_info, gain_char, error_flag, GPS_comments] = parseGPS;
end
% if gain_char is "Unknown" record gain change
if gain_char == 'U'
GAIN_CHANGE = 1;
else
GAIN_CHANGE = 0;
end
% error_flag non-zero if there were problems parsing the GPS string
if error_flag == 0
NO_GPS = 0;
GPS_ERROR = 0;
elseif error_flag <= 2
NO_GPS = 1;
GPS_ERROR = 1;
elseif error_flag > 2
NO_GPS = 0;
GPS_ERROR = 1;
end
% start date and time require special treatment: counting to the start
% of the time series from the so-called "GPS lock", using also the status
% byte string to find the lock, and the sequence string to find gaps
if nargin >= 4
[start_time, error_flag, time_comments] = startTimeFromGPS(GPS, seq, stat);
elseif nargin >= 2
[start_time, error_flag, time_comments] = startTimeFromGPS(GPS);
else
[start_time, error_flag, time_comments] = startTimeFromGPS;
end
if error_flag
disp(time_comments);
end
GPS_ERROR = GPS_ERROR || error_flag;
% The number of seconds could be different from the number of scans,
% when the missing data flags have been inserted. Here, nsec is the
% number of seconds, estimated using the sequence string, while nscan
% is the number of data records stored in the NIMS binary file (does
% not include any missing data).
% Edited by Anna Kelbert Nov 3, 2015: the reason for the -1 in the nsec
% calculation is to compute the correct end time.
% Note that for 8 Hz data, nscan is NOT the size of Data stream: it is
% the number of data blocks in the NIMS binary file; each block has
% 8 measurements (5 values each).
% This is different for ZEN data, where the number of scans corresponds
% to the length of the data stream. Do different things for NIMS and ZEN.
if nargin >= 4
[gaps,gap_comments,gap_errors] = find_gaps(GPS,seq,stat);
nscan = length(seq);
if strcmp(instrumentType,'NIMS')
if ~isempty(gaps.len)
nsec = length(seq) + sum(gaps.len) - 1;
else
nsec = seq(end);
end
else
if ~isempty(gaps.len)
nsec = (length(seq) + sum(gaps.len) - 1)/HZ;
else
nsec = seq(end)/HZ;
end
end
else
gaps = {};
gaps.len = [];
gaps.ind = [];
gap_errors = '';
nscan = 0;
nsec = 0;
end
% now that we have the correct number of seconds, compute end time
sec2num = 1/(3600*24);
if isempty(start_time)
startDate='yyyy-mm-dd';
startTime='HH:MM:SS';
endDate='yyyy-mm-dd';
endTime='HH:MM:SS';
else
startDate = datestr(datenum(start_time),29);
startTime = datestr(datenum(start_time),13);
endDate = datestr(datenum(start_time)+sec2num*nsec,29);
endTime = datestr(datenum(start_time)+sec2num*nsec,13);
end
% leave the runID as it is; will only use it for processing
if ~isempty(runID)
runID = char(runID);
end
% make sure the siteID is 5 char uppercase; run char is lowercase
siteID = runID; % default: unknown format
if (length(runID)==6) % e.g. ORG04a->ORG04
siteID = upper(runID(1:5));
elseif (length(runID)==7)
if isstrprop(runID(4),'digit')
if str2double(runID(4))==0 % e.g. ori006a->ORI06
siteID = [upper(runID(1:3)) upper(runID(5:6))];
else % e.g. pam486a->PA486
siteID = [upper(runID(1:2)) upper(runID(4:6))];
end
end
end
id = strsplit(char(sensorID),';');
NIMS_ID = char(id(1));
if length(id) > 1 && length(NIMS_ID) >= 4
Magn_ID = char(id(2));
if length(Magn_ID)==2
Magn_ID = [NIMS_ID(1:4) '-' Magn_ID];
end
else
Magn_ID = NIMS_ID;
end
metaData.siteName = siteName;
metaData.siteID = siteID;
metaData.runID = runID;
metaData.InstrumentType = instrumentType;
metaData.InstrumentID = NIMS_ID;
metaData.Magnetometer = Magn_ID;
metaData.Ex_Electrodes = Ex_wire.electrodes;
metaData.Ex_wire_length = Ex_wire.length;
metaData.Ex_wire_azimuth = Ex_wire.azimuth;
metaData.Ey_Electrodes = Ey_wire.electrodes;
metaData.Ey_wire_length = Ey_wire.length;
metaData.Ey_wire_azimuth = Ey_wire.azimuth;
metaData.GroundEinfo = GroundEinfo;
metaData.HighPass = 'on';
metaData.mag_azimuth = [0. 90. 0.];
metaData.system_errors = [H_comments GPS_comments time_comments gap_errors];
metaData.errors = [H_comments GPS_comments time_comments gap_errors];
metaData.comments = '';
metaData.field_comments = nims_comments;
metaData.SiteInstalledBy = operator;
metaData.MetaDataCheckedBy = '';
metaData.lat = lat;
metaData.lon = lon;
metaData.elev = elev;
metaData.decl = decl;
metaData.decl_info = decl_info;
metaData.startDate = startDate;
metaData.startTime = startTime;
metaData.endDate = endDate;
metaData.endTime = endTime;
metaData.duplicates_deleted = length(dupl);
metaData.gain_char = gain_char;
metaData.gainApplied = 0;
metaData.dt = 1/HZ;
metaData.nsec = nsec;
metaData.nscan = nscan;
metaData.ngaps = length(gaps.ind);
metaData.gaps = gaps;
metaData.gapsFilled = 0;
metaData.scaling = [1 1 1 1 1];
metaData.nch = 5;
metaData.SystemResponse = NIMsysRsp(NIMS_ID,HZ);
metaData.NO_HEADER = NO_HEADER;
metaData.NO_GPS = NO_GPS;
metaData.GPS_ERROR = GPS_ERROR;
metaData.GAIN_CHANGE = GAIN_CHANGE;