This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TSGFileLoader.m
177 lines (140 loc) · 7.19 KB
/
TSGFileLoader.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
%TSGFileLoader - Opens a TSG data file and creates new TemperatureData, GPSData, and SalinityData objects
% TSGFileLoader iterates through a list of files specified in the constructor and uses the
% specified import method to create new TemperatureData, GPSData, and SalinityData objects
%
% Syntax: obj = TSGFileLoader(fileNameListIn, importmethodIn, varargin)
%
% Inputs:
% fileNameListIn - a list of TSG files to import
% importMethodIn - the import method to use for reading in files
% varargin - a set of name/value pairs to specify units for
% various data objects going to be created
%
%
% Example:
% tfl = TSGFileLoader( tsgFiles, params.TSG_IMPORT_METHOD_NAME, 'temperature', ...
% params.TEMP_UNITS, 'salinity', params.SAL_UNITS, 'gps', params.GPS_UNITS );
%
% Other m-files required: TemperatureData, SalinityData, GPSData
% Subfunctions: none
% MAT-files required: none
%
% See also: TemperatureData, SalinityData, GPSData
% Author: Wendy Neary
% MISCLab, University of Maine
% email address: wendy.neary@maine.edu
% Website: http://misclab.umeoce.maine.edu/index.php
% May 2015; Last revision: 13-08-15
%------------- BEGIN CODE --------------
classdef TSGFileLoader
properties
FileNameList %a list of TSG files to import
ImportMethodName %the import method to use for reading in files
end
properties (SetAccess = private, GetAccess = private)
TemperatureUnits % Temperature units of measurement
SalinityUnits % Salinity units of measurement
GPSUnits % GPS units of measurement
end
methods
%
% So far this is identical to FlowFileLoader
%
function obj = TSGFileLoader(fileNameListIn, importmethodIn, varargin)
% constructor -- takes a list of files and sets object properties
% check incoming arguments
if nargin > 0
% check there is a file list
if ~isempty( fileNameListIn )
obj.FileNameList = fileNameListIn;
else
error('Need file list')
end
% check there is a import method specified
if ischar( importmethodIn )
obj.ImportMethodName = importmethodIn;
else
error('Need import method')
end
if ~isempty(varargin)
for i = 1:2:length(varargin)
val = lower(varargin{i});
switch val
case 'temperature'
% disp('flow')
obj.TemperatureUnits = varargin{i+1};
case 'salinity'
% disp('valve')
obj.SalinityUnits = varargin{i+1};
case 'gps'
obj.GPSUnits = varargin{1+1};
otherwise
error(['Unexpected option: ' varargin{1}])
end
end
end % end if ~isempty(varargin)
else
error('Supply an input argument')
end % end if nargin > 0
end % end constructor
function dataOut = loadData(obj) %tscOut = loadData(obj)
%loadData creates a cell array of data objects for the imported data.
%It creates a function handle to the import method for this
%file, allowing the function name to be dynamically set. The
%import method currently returns: [TemperatureBoat, TemperatureInstr, Salinity, LatDegrees, ...
% LatMinutes, LatSign, LonDegrees, LonMinutes, LonSign, ...
% Time1, Date1]
%
% SYNOPSIS dataOut = loadData(obj)
% INPUT obj - this object
% OUTPUT dataOut - a cell array of TemperatureData,
% SalinityData and GPSDataobjects
%
nFiles = length(obj.FileNameList);
for iFiles = 1:nFiles;
% open the data file and retrieve data from it
fh = str2func(obj.ImportMethodName);
[TemperatureBoat, TemperatureInstr, Salinity, LatDegrees, ...
LatMinutes, LatSign, LonDegrees, LonMinutes, LonSign, ...
Time1, Date1] = fh(obj.FileNameList{iFiles});
% Data Processing Section:
%
% Logic is here because this is the object that knows about
% the specific FILE types -- different files might have
% different lat/lon type data. Process here -- GPSData
% object doesn't need to know format from file
%
% date format here hardcoded -- change?
fullDate = datenum(strcat(Date1, Time1), 'ddmmyyHHMMSS');
% -------------------------------------------------------
% Convert Lat & Lon:
Latitude = LatDegrees + LatMinutes/60;
Longitude = LonDegrees + LonMinutes/60;
% Set sign appropriate for N/S and E/W
Latitude(LatSign=='S') = -Latitude(LatSign=='S');
Longitude(LonSign=='W') = -Latitude(LonSign=='W');
% Put latitude and longitude into one matrix
LatLon = [Latitude, Longitude];
% ------------------------------------------------------
% if FlowData already exists
if ismember({'td'}, who)
%append data
% Use TemperatureInstr as the temperature measurement
td.addData('Temperature', [TemperatureInstr, TemperatureBoat], fullDate);
sd.addData('Salinity', Salinity, fullDate);
% append GPS Data
gd.addData('GPS', LatLon, fullDate);
else
% create new TemperatureData object with the timeseries object
td = TemperatureData('Temperature', [TemperatureInstr, TemperatureBoat], fullDate, obj.TemperatureUnits);
% create new SalinityData object with the timeseries object
sd = SalinityData('Salinity', Salinity, fullDate, obj.SalinityUnits);
% create new GPSData object with the timeseries object
gd = GPSData('LatAndLon', LatLon, fullDate, obj.GPSUnits);
end % end check for existing FlowData object
end % end for loop
% create a cell array to hold both
dataOut = {td, sd, gd};
end % end loadData function
end % end methods block
end % end classDef