lx
2025-09-05 0b108f56b2d0c35d01ee361dc593119ac52a14e1
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
using ErrorAnalysis.Repository;
using ErrorAnalysis.Repository.Entity;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
 
namespace ErrorAnalysis.Service
{
    public class COMergeCalcService
    {
        //Ncoef=1e8*2.54*2.54*2*6*11*2*77*0.2/0.6*speed;
        //Fcoef=1e8*2.54*2.54*4*6*2*11*77*0.2/0.6*speed;
 
        const double _gg = (9.0 - 0.0) / 255.0;//0.03529
        const double _offset = 0.0;
 
        const int _cWinStartIndex = (int)(3.3 / _gg + _offset); //C窗口index
        const int _cWinEndIndex = (int)(4.78 / _gg + _offset); // width= 4.78-3.3  = 1.48
 
        const int _oWinStartIndex = (int)(4.88 / _gg + _offset); // org
        const int _oWinEndIndex = (int)(6.36 / _gg + _offset); // width = 6.36-4.88=1.48
 
        //const double _coef = 1e8 * 2.54 * 2.54 * 4 * 6 * 2 * 11 * 77 * 0.2 / 0.6;
 
        private static (double, double) GetFarInterplolateResult(string modelId, double porosity, double sw, double speed, double depth, double yieldCounting)
        {
            var cWinLength = _cWinEndIndex - _cWinStartIndex + 1;
            var oWinLength = _oWinEndIndex - _oWinStartIndex + 1;
            var oilLine = RepositoryInstance.Instance.COFarResultRepository?.GetCOFarResult(modelId, (int)porosity, 0);
            var waterLine = RepositoryInstance.Instance.COFarResultRepository?.GetCOFarResult(modelId, (int)porosity, 100);
            if (oilLine == null || waterLine == null)
                throw new InvalidDataException("COFarResult Line is null");
            var oilLineValArr = oilLine.CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
            var waterLineValArr = waterLine.CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
 
            var oilLineCR = oilLineValArr.Skip(_cWinStartIndex - 1).Take(cWinLength).Sum() / oilLineValArr.Skip(17).Take(237).Sum();
            var oilLineOR = oilLineValArr.Skip(_oWinStartIndex - 1).Take(oWinLength).Sum() / oilLineValArr.Skip(17).Take(237).Sum();
 
            var oilLineC = 36d / 160d * oilLineCR * yieldCounting / speed * depth * 0.0762d;
            var oilLineO = 36d / 160d * oilLineOR * yieldCounting / speed * depth * 0.0762d;
 
            var waterLineCR = waterLineValArr.Skip(_cWinStartIndex - 1).Take(cWinLength).Sum() / waterLineValArr.Skip(17).Take(237).Sum();
            var waterLineOR = waterLineValArr.Skip(_oWinStartIndex - 1).Take(oWinLength).Sum() / waterLineValArr.Skip(17).Take(237).Sum();
            var waterLineC = 36d / 160d * waterLineCR * yieldCounting / speed * depth * 0.0762d;
            var waterLineO = 36d / 160d * waterLineOR * yieldCounting / speed * depth * 0.0762d;
 
            double cRes = 0, oRes = 0;
            if (sw == 0)
            {
                cRes = oilLineC;
                oRes = oilLineO;
            }
            else if (sw == 100)
            {
                cRes = waterLineC;
                oRes = waterLineO;
            }
            else if (sw > 0 && sw < 100)
            {
                cRes = Utility.Interpolate(sw, 100, waterLineC, 0, oilLineC);
                oRes = Utility.Interpolate(sw, 0, oilLineO, 100, waterLineO);
            }
            return (cRes, oRes);
        }
 
        private static (double, double) GetNearInterplolateResult(string modelId, double porosity, double sw, double speed, double depth, double yieldCounting)
        {
            var cWinLength = _cWinEndIndex - _cWinStartIndex + 1;
            var oWinLength = _oWinEndIndex - _oWinStartIndex + 1;
            var oilLine = RepositoryInstance.Instance.CONearResultRepository?.GetCONearResult(modelId, (int)porosity, 0);
            var waterLine = RepositoryInstance.Instance.CONearResultRepository?.GetCONearResult(modelId, (int)porosity, 100);
            if (oilLine == null || waterLine == null)
                throw new InvalidDataException("COFarResult Line is null");
            var oilLineValArr = oilLine.CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
            var waterLineValArr = waterLine.CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
 
            var oilLineCR = oilLineValArr.Skip(_cWinStartIndex - 1).Take(cWinLength).Sum() / oilLineValArr.Skip(17).Take(237).Sum();
            var oilLineOR = oilLineValArr.Skip(_oWinStartIndex - 1).Take(oWinLength).Sum() / oilLineValArr.Skip(17).Take(237).Sum();
            var oilLineC = (36d / 160d) * oilLineCR * yieldCounting / speed * depth * 0.0762d;
            var oilLineO = (36d / 160d) * oilLineOR * yieldCounting / speed * depth * 0.0762d;
 
            var waterLineCR = waterLineValArr.Skip(_cWinStartIndex - 1).Take(cWinLength).Sum() / waterLineValArr.Skip(17).Take(237).Sum();
            var waterLineOR = waterLineValArr.Skip(_oWinStartIndex - 1).Take(oWinLength).Sum() / waterLineValArr.Skip(17).Take(237).Sum();
            var waterLineC = (36d / 160d) * waterLineCR * yieldCounting / speed * depth * 0.0762d;
            var waterLineO = (36d / 160d) * waterLineOR * yieldCounting / speed * depth * 0.0762d;
 
            double cRes = 0, oRes = 0;
            if (sw == 0)
            {
                cRes = waterLineC;
                oRes = waterLineO;
            }
            else if (sw == 100)
            {
                cRes = waterLineC;
                oRes = waterLineO;
            }
            else if (sw > 0 && sw < 100)
            {
                cRes = Utility.Interpolate(sw, 100, waterLineC, 0, oilLineC);
                oRes = Utility.Interpolate(sw, 0, oilLineO, 100, waterLineO);
            }
            return (cRes, oRes);
        }
 
        /// <summary>
        /// 获取远探头C或O值(废弃)
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="modelId">管柱ID</param>
        /// <param name="porosity">孔隙度</param>
        /// <param name="sw">含水饱和度</param>
        /// <param name="readSw">读取含水饱和度(0为碳,100为氧)</param>
        /// <param name="speed">测速</param>
        /// <param name="depth">深度</param>
        /// <returns>碳和氧值</returns>
        /// <exception cref="InvalidDataException">孔隙度超过范围</exception>
        public static (double, double) GetFarMergeCOResult(string modelId, double porosity, double sw, double speed, double depth, double yieldCounting)
        {
            if (porosity > 40)
                throw new InvalidDataException("Porosity value out of range!");
 
            (double, double) result = (0, 0);
            if (porosity % 5 == 0)
            {
                result = GetFarInterplolateResult(modelId, porosity, sw, speed, depth, yieldCounting);
            }
            else
            {
                var ceilingPorosity = Math.Ceiling(porosity / 5) * 5;
                var ceilingResult = GetFarInterplolateResult(modelId, ceilingPorosity, sw, speed, depth, yieldCounting);
 
                var floorPorosity = Math.Floor(porosity / 5) * 5;
                var floorResult = GetFarInterplolateResult(modelId, floorPorosity, sw, speed, depth, yieldCounting);
 
                result.Item1 = Utility.Interpolate(porosity, floorPorosity, floorResult.Item1, ceilingPorosity, ceilingResult.Item1);
                result.Item2 = Utility.Interpolate(porosity, floorPorosity, floorResult.Item2, ceilingPorosity, ceilingResult.Item2);
            }
            return result;
        }
 
        /// <summary>
        /// 获取近探头C或O值(废弃)
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="modelId">管柱ID</param>
        /// <param name="porosity">孔隙度</param>
        /// <param name="sw">含水饱和度</param>
        /// <param name="readSw">读取含水饱和度(0为碳,100为氧)</param>
        /// <param name="speed">测速</param>
        /// <param name="depth">深度</param>
        /// <returns>碳或氧值</returns>
        /// <exception cref="InvalidDataException">孔隙度超过范围</exception>
        public static (double, double) GetNearMergeCOResult(string modelId, double porosity, double sw, double speed, double depth, double yieldCounting)
        {
            if (porosity > 40)
                throw new InvalidDataException("Porosity value out of range!");
 
            (double, double) result = (0, 0);
            if (porosity % 5 == 0)
            {
                result = GetNearInterplolateResult(modelId, porosity, sw, speed, depth, yieldCounting);
            }
            else
            {
                var ceilingPorosity = Math.Ceiling(porosity / 5) * 5;
                var ceilingResult = GetNearInterplolateResult(modelId, ceilingPorosity, sw, speed, depth, yieldCounting);
 
                var floorPorosity = Math.Floor(porosity / 5) * 5;
                var floorResult = GetNearInterplolateResult(modelId, floorPorosity, sw, speed, depth, yieldCounting);
 
                result.Item1 = Utility.Interpolate(porosity, floorPorosity, floorResult.Item1, ceilingPorosity, ceilingResult.Item1);
                result.Item2 = Utility.Interpolate(porosity, floorPorosity, floorResult.Item2, ceilingPorosity, ceilingResult.Item2);
            }
            return result;
        }
 
        public static List<double[]> GetFarSpectrum(string modelId, double porosity, double sw)
        {
            var result = new List<double[]>();
            if (porosity > 40)
                throw new InvalidDataException("Porosity value out of range!");
 
            double[] oilLineSpec;
            double[] waterLineSpec;
 
            if (porosity % 5 == 0)
            {
                oilLineSpec = RepositoryInstance.Instance.COFarResultRepository.GetCOFarResult(modelId, (int)porosity, 0).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
                waterLineSpec = RepositoryInstance.Instance.COFarResultRepository.GetCOFarResult(modelId, (int)porosity, 100).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
            }
            else
            {
                var ceilingPorosity = Math.Ceiling(porosity / 5) * 5;
                double[] ceilingOilLineSpec = RepositoryInstance.Instance.COFarResultRepository.GetCOFarResult(modelId, (int)ceilingPorosity, 0).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
                double[] ceilingWaterLineSpec = RepositoryInstance.Instance.COFarResultRepository.GetCOFarResult(modelId, (int)ceilingPorosity, 100).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
 
                var floorPorosity = Math.Floor(porosity / 5) * 5;
                double[] floorOilLineSpec = RepositoryInstance.Instance.COFarResultRepository.GetCOFarResult(modelId, (int)floorPorosity, 0).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
                double[] floorWaterLineSpec = RepositoryInstance.Instance.COFarResultRepository.GetCOFarResult(modelId, (int)floorPorosity, 100).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
 
                oilLineSpec = ceilingOilLineSpec;
                waterLineSpec = ceilingWaterLineSpec;
 
                for (int i = 0; i < ceilingOilLineSpec.Length; i++)
                {
                    oilLineSpec[i] = Utility.Interpolate(porosity, floorPorosity, floorOilLineSpec[i], ceilingPorosity, ceilingOilLineSpec[i]);
                    waterLineSpec[i] = Utility.Interpolate(porosity, floorPorosity, floorWaterLineSpec[i], ceilingPorosity, ceilingWaterLineSpec[i]);
                }
            }
            if (sw == 0)
            {
                for (int i = 0; i < oilLineSpec.Length; i++)
                {
                    result.Add([i, oilLineSpec[i]]);
                }
            }
            else if (sw == 100)
            {
                for (int i = 0; i < waterLineSpec.Length; i++)
                {
                    result.Add([i, waterLineSpec[i]]);
                }
            }
            else if (sw > 0 && sw < 100)
            {
                for (int i = 0; i < waterLineSpec.Length; i++)
                {
                    var val = Utility.Interpolate(sw, 0, oilLineSpec[i], 100, waterLineSpec[i]);
                    result.Add([i, val]);
                }
            }
 
            return result;
        }
 
        public static List<double[]> GetNearSpectrum(string modelId, double porosity, double sw)
        {
            var result = new List<double[]>();
            if (porosity > 40)
                throw new InvalidDataException("Porosity value out of range!");
 
            double[] oilLineSpec;
            double[] waterLineSpec;
 
            if (porosity % 5 == 0)
            {
                oilLineSpec = RepositoryInstance.Instance.CONearResultRepository.GetCONearResult(modelId, (int)porosity, 0).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
                waterLineSpec = RepositoryInstance.Instance.CONearResultRepository.GetCONearResult(modelId, (int)porosity, 100).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
            }
            else
            {
                var ceilingPorosity = Math.Ceiling(porosity / 5) * 5;
                double[] ceilingOilLineSpec = RepositoryInstance.Instance.CONearResultRepository.GetCONearResult(modelId, (int)ceilingPorosity, 0).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
                double[] ceilingWaterLineSpec = RepositoryInstance.Instance.CONearResultRepository.GetCONearResult(modelId, (int)ceilingPorosity, 100).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
 
                var floorPorosity = Math.Floor(porosity / 5) * 5;
                double[] floorOilLineSpec = RepositoryInstance.Instance.CONearResultRepository.GetCONearResult(modelId, (int)floorPorosity, 0).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
                double[] floorWaterLineSpec = RepositoryInstance.Instance.CONearResultRepository.GetCONearResult(modelId, (int)floorPorosity, 100).CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
 
                oilLineSpec = ceilingOilLineSpec;
                waterLineSpec = ceilingWaterLineSpec;
 
                for (int i = 0; i < ceilingOilLineSpec.Length; i++)
                {
                    oilLineSpec[i] = Utility.Interpolate(porosity, floorPorosity, floorOilLineSpec[i], ceilingPorosity, ceilingOilLineSpec[i]);
                    waterLineSpec[i] = Utility.Interpolate(porosity, floorPorosity, floorWaterLineSpec[i], ceilingPorosity, ceilingWaterLineSpec[i]);
                }
            }
            if (sw == 0)
            {
                for (int i = 0; i < oilLineSpec.Length; i++)
                {
                    result.Add([i, oilLineSpec[i]]);
                }
            }
            else if (sw == 100)
            {
                for (int i = 0; i < waterLineSpec.Length; i++)
                {
                    result.Add([i, waterLineSpec[i]]);
                }
            }
            else if (sw > 0 && sw < 100)
            {
                for (int i = 0; i < waterLineSpec.Length; i++)
                {
                    var val = Utility.Interpolate(sw, 0, oilLineSpec[i], 100, waterLineSpec[i]);
                    result.Add([i, val]);
                }
            }
 
            return result;
        }
 
        public static (double, double) GetFarCROR(string modelId, double porosity, double sw)
        {
            var cWinLength = _cWinEndIndex - _cWinStartIndex + 1;
            var oWinLength = _oWinEndIndex - _oWinStartIndex + 1;
            var oilLine = RepositoryInstance.Instance.COFarResultRepository?.GetCOFarResult(modelId, (int)porosity, 0);
            var waterLine = RepositoryInstance.Instance.COFarResultRepository?.GetCOFarResult(modelId, (int)porosity, 100);
            if (oilLine == null || waterLine == null)
                throw new InvalidDataException("COFarResult Line is null");
            var oilLineValArr = oilLine.CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
            var waterLineValArr = waterLine.CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
 
            var oilLineCR = oilLineValArr.Skip(_cWinStartIndex - 1).Take(cWinLength).Sum() / oilLineValArr.Skip(17).Take(237).Sum();
            var oilLineOR = oilLineValArr.Skip(_oWinStartIndex - 1).Take(oWinLength).Sum() / oilLineValArr.Skip(17).Take(237).Sum();
 
            var waterLineCR = waterLineValArr.Skip(_cWinStartIndex - 1).Take(cWinLength).Sum() / waterLineValArr.Skip(17).Take(237).Sum();
            var waterLineOR = waterLineValArr.Skip(_oWinStartIndex - 1).Take(oWinLength).Sum() / waterLineValArr.Skip(17).Take(237).Sum();
 
            double cRes = 0, oRes = 0;
            if (sw == 0)
            {
                cRes = oilLineCR;
                oRes = oilLineOR;
            }
            else if (sw == 100)
            {
                cRes = waterLineCR;
                oRes = waterLineOR;
            }
            else if (sw > 0 && sw < 100)
            {
                cRes = Utility.Interpolate(sw, 100, waterLineCR, 0, oilLineCR);
                oRes = Utility.Interpolate(sw, 0, oilLineOR, 100, waterLineOR);
            }
            return (cRes, oRes);
        }
 
        public static (double, double) GetNearCROR(string modelId, double porosity, double sw)
        {
            var cWinLength = _cWinEndIndex - _cWinStartIndex + 1;
            var oWinLength = _oWinEndIndex - _oWinStartIndex + 1;
            var oilLine = RepositoryInstance.Instance.CONearResultRepository?.GetCONearResult(modelId, (int)porosity, 0);
            var waterLine = RepositoryInstance.Instance.CONearResultRepository?.GetCONearResult(modelId, (int)porosity, 100);
            if (oilLine == null || waterLine == null)
                throw new InvalidDataException("COFarResult Line is null");
            var oilLineValArr = oilLine.CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
            var waterLineValArr = waterLine.CInelasticSpec.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
 
            var oilLineCR = oilLineValArr.Skip(_cWinStartIndex - 1).Take(cWinLength).Sum() / oilLineValArr.Skip(17).Take(237).Sum();
            var oilLineOR = oilLineValArr.Skip(_oWinStartIndex - 1).Take(oWinLength).Sum() / oilLineValArr.Skip(17).Take(237).Sum();
 
            var waterLineCR = waterLineValArr.Skip(_cWinStartIndex - 1).Take(cWinLength).Sum() / waterLineValArr.Skip(17).Take(237).Sum();
            var waterLineOR = waterLineValArr.Skip(_oWinStartIndex - 1).Take(oWinLength).Sum() / waterLineValArr.Skip(17).Take(237).Sum();
 
            double crRes = 0, orRes = 0;
            if (sw == 0)
            {
                crRes = oilLineCR;
                orRes = oilLineOR;
            }
            else if (sw == 100)
            {
                crRes = waterLineCR;
                orRes = waterLineOR;
            }
            else if (sw > 0 && sw < 100)
            {
                crRes = Utility.Interpolate(sw, 100, waterLineCR, 0, oilLineCR);
                orRes = Utility.Interpolate(sw, 0, oilLineOR, 100, waterLineOR);
            }
            return (crRes, orRes);
        }
 
        public static (double, double) GetFarCOORResult(string modelId, double porosity, double sw)
        {
            if (porosity > 40)
                throw new InvalidDataException("Porosity value out of range!");
 
            (double, double) result = (0, 0);
            if (porosity % 5 == 0)
            {
                result = GetFarCROR(modelId, porosity, sw);
            }
            else
            {
                var ceilingPorosity = Math.Ceiling(porosity / 5) * 5;
                var ceilingResult = GetFarCROR(modelId, ceilingPorosity, sw);
 
                var floorPorosity = Math.Floor(porosity / 5) * 5;
                var floorResult = GetFarCROR(modelId, floorPorosity, sw);
 
                result.Item1 = Utility.Interpolate(porosity, floorPorosity, floorResult.Item1, ceilingPorosity, ceilingResult.Item1);
                result.Item2 = Utility.Interpolate(porosity, floorPorosity, floorResult.Item2, ceilingPorosity, ceilingResult.Item2);
            }
            return result;
        }
 
        public static (double, double) GetNearCOORResult(string modelId, double porosity, double sw)
        {
            if (porosity > 40)
                throw new InvalidDataException("Porosity value out of range!");
 
            (double, double) result = (0, 0);
            if (porosity % 5 == 0)
            {
                result = GetNearCROR(modelId, porosity, sw);
            }
            else
            {
                var ceilingPorosity = Math.Ceiling(porosity / 5) * 5;
                var ceilingResult = GetNearCROR(modelId, ceilingPorosity, sw);
 
                var floorPorosity = Math.Floor(porosity / 5) * 5;
                var floorResult = GetNearCROR(modelId, floorPorosity, sw);
 
                result.Item1 = Utility.Interpolate(porosity, floorPorosity, floorResult.Item1, ceilingPorosity, ceilingResult.Item1);
                result.Item2 = Utility.Interpolate(porosity, floorPorosity, floorResult.Item2, ceilingPorosity, ceilingResult.Item2);
            }
            return result;
        }
 
    }
}