|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Globalization; |
3 | 4 | using System.IO;
|
4 | 5 |
|
5 | 6 | namespace UCNLSalinity
|
@@ -114,6 +115,70 @@ public static List<SDE> Read1x1DegreeSalinityData(string fileName, int[] profile
|
114 | 115 | return result;
|
115 | 116 | }
|
116 | 117 |
|
| 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 | + |
117 | 182 | public static Dictionary<double, Dictionary<double, Dictionary<double, double>>> ConvertToDictionary(List<SDE> salinityList)
|
118 | 183 | {
|
119 | 184 | 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
|
165 | 230 | return result;
|
166 | 231 | }
|
167 | 232 |
|
| 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 | + |
168 | 323 | #endregion
|
169 | 324 | }
|
170 | 325 | }
|
0 commit comments