protected override void ImportLevel(int idx) { // T, P, H must be read and built in this order // Calculation of P depends on T; DenseMatrix t = ImportLevelData(TempFiles[idx], "Temperature", idx, (d) => (d > 1000) ? 0 : (d - AbsoluteConstants.WaterFreezePoint)); if (t != null) { DenseMatrix z = ImportLevelData(null, "Geopotential height", idx, (d) => (d)); if (z != null) { DenseMatrix p = DenseMatrix.Create(z.RowCount, z.ColumnCount, (r, c) => { float dz = z[r, c] - SimConstants.LevelHeights[idx]; float rt = AbsoluteConstants.Rsd * (t[r, c] + AbsoluteConstants.WaterFreezePoint); float levelPressure = 0; switch (idx) { case LevelType.SeaLevel: levelPressure = LevelPressure.SeaLevelPressure; break; case LevelType.MidLevel: levelPressure = LevelPressure.MidLevelPressure; break; case LevelType.TopLevel: levelPressure = LevelPressure.TopLevelPressure; break; } return(levelPressure * (float)Math.Exp(AbsoluteConstants.g * dz / rt)); }); if (p != null) { FileSupport.SaveMatrixToFile(p, PressureFiles[idx], false); DenseMatrix q = ImportLevelData(HumidityFiles[idx], "Relative humidity", idx, (d) => (d)); } } } }
private DenseMatrix ImportLevelData(string dataFile, string paramName, int levelIdx, Func <float, float> conversionFunc) { if (string.IsNullOrEmpty(dataFile) == false) { if (File.Exists(dataFile)) { File.Delete(dataFile); } } int level = (levelIdx < 0) ? 0 : _levels[levelIdx]; var messages = _messages.Where(m => m.Name.Contains(paramName) && m.Level == level).First(); var nodes = messages.GeoSpatialValues.Where(gs => gs.IsMissing == false && (gs.Latitude >= EarthModel.MinLat && gs.Latitude <= EarthModel.MaxLat) && (gs.Longitude >= 180 + EarthModel.MinLon && gs.Longitude <= 180 + EarthModel.MaxLon) && (gs.Latitude == Math.Truncate(gs.Latitude)) && gs.Longitude == Math.Truncate(gs.Longitude)) .ToArray(); if (!time) { DateTime dt = messages.ReferenceTime; SimDateTime sdt = new SimDateTime(dt); string timeSeedFile = "timeSeed.thd"; CorrectFilePath(ref timeSeedFile); File.WriteAllText(timeSeedFile, sdt.Title); time = true; } DenseMatrix mat = MatrixFactory.Init(); foreach (var node in nodes) { int r = EarthModel.MaxLat - (int)node.Latitude; int c = ((int)node.Longitude - EarthModel.MinLon) % 360; mat[r, c] = conversionFunc((float)node.Value); } if (string.IsNullOrEmpty(dataFile) == false) { FileSupport.SaveMatrixToFile(mat, dataFile, false); } return(mat); }
private DenseMatrix ImportNcFile <T>(string netCdfFile, string netCdfVariable, int netCdfLevelCount, int netCdfLevelIdx, string dataFile, Func <T, float> conversionFunc, bool flipUpDown = true) { DenseMatrix mat = null; if (File.Exists(netCdfFile)) { if (File.Exists(dataFile)) { File.Delete(dataFile); } T[] data = GetData <T>(netCdfFile, netCdfVariable, netCdfLevelCount, netCdfLevelIdx); mat = DataToMatrix(data, (d) => conversionFunc(d), flipUpDown); if (string.IsNullOrEmpty(dataFile) == false) { FileSupport.SaveMatrixToFile(mat, dataFile, false); } } return(mat); }
protected override void ImportLevel(int idx) { if (_sstOnly) { return; } // T, P, H must be read and built in this order // Calculation of P depends on T; // Calculation of H depends on both T and P. DenseMatrix t = ImportNcFile <float>(TemperatureNcFile, "Temperature", TempFiles.Length, idx, TempFiles[idx], (d) => (d > 1000) ? 0 : (d - AbsoluteConstants.WaterFreezePoint)); if (t != null) { DenseMatrix z = ImportNcFile <float>(GeopotentialNcFile, "Geopotential_height", GeopotentialFiles.Length, idx, null, (d) => (d)); if (z != null) { DenseMatrix p = DenseMatrix.Create(z.RowCount, z.ColumnCount, (r, c) => { float dz = z[r, c] - SimConstants.LevelHeights[idx]; float rt = AbsoluteConstants.Rsd * (t[r, c] + AbsoluteConstants.WaterFreezePoint); float levelPressure = 0; switch (idx) { case LevelType.SeaLevel: // Caution: use 1000 and not LevelPressure.SeaLevelPressure // because we imported 1000 hPa geopotential which is not // quite the same as sea level geopotential. // works with sea leve levelPressure = 1000; break; case LevelType.MidLevel: levelPressure = LevelPressure.MidLevelPressure; break; case LevelType.TopLevel: levelPressure = LevelPressure.TopLevelPressure; break; } return(levelPressure * (float)Math.Exp(AbsoluteConstants.g * dz / rt)); }); if (p != null) { FileSupport.SaveMatrixToFile(p, PressureFiles[idx], false); DenseMatrix q = ImportNcFile <float>(HumidityNcFile, "Specific_humidity", GeopotentialFiles.Length, idx, null, (d) => (d)); if (q != null) { DenseMatrix h = DenseMatrix.Create(q.RowCount, q.ColumnCount, (r, c) => { float temp = t[r, c]; float qair = q[r, c]; float press = p[r, c]; float es = 6.112f * (float)Math.Exp((17.67f * temp) / (temp + 243.5f)); float e = qair * press / (0.378f * qair + 0.622f); float rh = e / es; return(100 * Math.Min(1, Math.Max(0, rh))); }); FileSupport.SaveMatrixToFile(h, HumidityFiles[idx], false); } } } } }
private void mnuSubmatrix_Click(object sender, RoutedEventArgs e) { var viewPort = App.ControlPanelModel.SelectedViewport; var allFiles = Directory.GetFiles(SimulationData.DataFolder); if (allFiles != null) { string submatrixFolder = Path.Combine(SimulationData.WorkFolder, $"subMatrix_{viewPort.Name}"); if (Directory.Exists(submatrixFolder)) { Directory.Delete(submatrixFolder, true); } foreach (string file in allFiles) { string title = Path.GetFileNameWithoutExtension(file).ToUpperInvariant(); string type = title.Substring(0, 4); DenseMatrix rawMatrix = null; string subMatrixPath = null; switch (type) { case "C_00": case "L_00": case "N_00": case "T_01": case "T_SH": case "T_SL": case "T_TE": case "T_TS": case "F_SI": case "T_NL": case "T_NH": rawMatrix = FileSupport.LoadSubMatrixFromFile(file, viewPort.MinLon, viewPort.MaxLon, viewPort.MinLat, viewPort.MaxLat); subMatrixPath = file.Replace(SimulationData.DataFolder, submatrixFolder); break; case "P_00": case "P_01": { var temp = FileSupport.LoadSubMatrixFromFile(file, viewPort.MinLon, viewPort.MaxLon, viewPort.MinLat, viewPort.MaxLat); rawMatrix = temp.Gradient2(); subMatrixPath = file.Replace(SimulationData.DataFolder, submatrixFolder).Replace("P_0", "W_0"); } break; default: continue; } DenseMatrix output = null; if (rawMatrix.RowCount < 10) { output = rawMatrix.Interpolate(); } else { output = rawMatrix; } FileSupport.SaveMatrixToFile(output, subMatrixPath, false); } } System.Windows.MessageBox.Show("Done."); }