Skip to content

Commit 511d54d

Browse files
WOD09 support
1 parent 056413e commit 511d54d

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

NODCDataReader.cs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.IO;
45

56
namespace UCNLSalinity
@@ -114,6 +115,70 @@ public static List<SDE> Read1x1DegreeSalinityData(string fileName, int[] profile
114115
return result;
115116
}
116117

118+
public static List<SDE> ReadQuarterDegreeSalinityData(string fileName, int[] profileDepths)
119+
{
120+
List<SDE> result = new List<SDE>();
121+
try
122+
{
123+
double latIdx = 0;
124+
double lonIdx = 0;
125+
126+
double lon = lonIdx + 0.5;
127+
if (lon > 180)
128+
lon = lon - 360;
129+
130+
double lat = latIdx - 89.5;
131+
132+
int depthLevelIdx = 0;
133+
134+
using (StreamReader sr = new StreamReader(fileName))
135+
{
136+
String line;
137+
while ((line = sr.ReadLine()) != null)
138+
{
139+
var splits = line.Split(" -".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
140+
141+
for (int i = 0; i < splits.Length; i++)
142+
{
143+
144+
double salinity = double.Parse(splits[i]);
145+
if (salinity < 99)
146+
{
147+
result.Add(new SDE(lat, lon, profileDepths[depthLevelIdx], salinity));
148+
}
149+
150+
depthLevelIdx++;
151+
152+
if (depthLevelIdx >= profileDepths.Length)
153+
{
154+
depthLevelIdx = 0;
155+
lonIdx += 0.25;
156+
157+
if (lonIdx >= 360)
158+
{
159+
lonIdx = 0;
160+
latIdx++;
161+
}
162+
163+
lon = lonIdx + 0.25;
164+
if (lon > 180)
165+
lon = lon - 360;
166+
167+
lat = latIdx - 89.875;
168+
}
169+
}
170+
}
171+
}
172+
}
173+
catch (Exception e)
174+
{
175+
176+
}
177+
178+
return result;
179+
}
180+
181+
117182
public static Dictionary<double, Dictionary<double, Dictionary<double, double>>> ConvertToDictionary(List<SDE> salinityList)
118183
{
119184
Dictionary<double, Dictionary<double, Dictionary<double, double>>> result = new Dictionary<double, Dictionary<double, Dictionary<double, double>>>();
@@ -165,6 +230,96 @@ public static List<MSDE> BuildMeanSalinityData(Dictionary<double, Dictionary<dou
165230
return result;
166231
}
167232

233+
234+
235+
private static int WOD09_GetColNumber(string line)
236+
{
237+
int pIdx = line.IndexOf('.');
238+
int sIdx = line.IndexOf('#');
239+
int result = -1;
240+
241+
if ((pIdx > sIdx) && (sIdx >= 0))
242+
{
243+
result = int.Parse(line.Substring(sIdx + 1, pIdx - sIdx - 1)) - 1;
244+
}
245+
246+
return result;
247+
}
248+
249+
public static List<MSDE> WOD09_SalinityData_Read(string fileName)
250+
{
251+
List<MSDE> result = new List<MSDE>();
252+
try
253+
{
254+
255+
//#ANNUAL SALINITY (unitless) at 0 meters depth
256+
//# 1 degree lat/lon grid
257+
//#Column definitions:
258+
//#1. Latitude (degrees,negative=south),
259+
//#2. Longitude (degrees,negative=west),
260+
//#3. Depth (meters),
261+
//#4. Objectively analyzed mean,
262+
//#5. statistical mean,
263+
//#6. standard deviation of statistical mean,
264+
//#7. standard error of statistical mean,
265+
//#8. objectively analyzed mean minus statistical mean,
266+
//#9. objectively analyzed mean minus objectively analyzed annual mean,
267+
//#10. number of grids with statistical means within radius of influence,
268+
//#11. number of data used to calculate statistical mean
269+
270+
int latCol = -1;
271+
int lonCol = -1;
272+
int oanCol = -1;
273+
274+
double lat, lon, salinity;
275+
276+
using (StreamReader sr = new StreamReader(fileName))
277+
{
278+
String line;
279+
while (((line = sr.ReadLine()) != null) && (line.Contains("#"))) // read header
280+
{
281+
if (line.Contains("Latitude"))
282+
latCol = WOD09_GetColNumber(line);
283+
else if (line.Contains("Longitude"))
284+
lonCol = WOD09_GetColNumber(line);
285+
else if (line.Contains("Objectively analyzed mean"))
286+
oanCol = WOD09_GetColNumber(line);
287+
}
288+
289+
if ((latCol >= 0) &&
290+
(lonCol >= 0) &&
291+
(oanCol >= 0))
292+
{
293+
while ((line = sr.ReadLine()) != null)
294+
{
295+
var splits = line.Split(",".ToCharArray());
296+
297+
lat = double.Parse(splits[latCol], CultureInfo.InvariantCulture);
298+
lon = double.Parse(splits[lonCol], CultureInfo.InvariantCulture);
299+
salinity = double.Parse(splits[oanCol], CultureInfo.InvariantCulture);
300+
301+
result.Add(new MSDE(lat, lon, salinity));
302+
}
303+
304+
}
305+
else
306+
{
307+
throw new InvalidDataException("Error parsing header of the file");
308+
}
309+
}
310+
311+
}
312+
catch (Exception e)
313+
{
314+
/// TODO:
315+
throw e;
316+
}
317+
318+
return result;
319+
}
320+
321+
322+
168323
#endregion
169324
}
170325
}

0 commit comments

Comments
 (0)