Skip to content

Commit

Permalink
Added support for well status the we get from the plotsymbol
Browse files Browse the repository at this point in the history
  • Loading branch information
vandresen committed Jul 27, 2023
1 parent 5ff6ac2 commit f524480
Show file tree
Hide file tree
Showing 18 changed files with 357 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using PPDMLoaderLibrary.Models;

namespace TexasPPDMLoader.Data
namespace PPDMLoaderLibrary.Data
{
public interface IWellboreData
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using PPDMLoaderLibrary.DataAccess;
using PPDMLoaderLibrary.Models;

namespace TexasPPDMLoader.Data
namespace PPDMLoaderLibrary.Data
{
public class WellboreDataCsv: IWellboreData
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using PPDMLoaderLibrary.Extensions;
using PPDMLoaderLibrary.Models;

namespace TexasPPDMLoader.Data
namespace PPDMLoaderLibrary.Data
{
public class WellboreDataDapper : IWellboreData
{
Expand All @@ -23,12 +23,15 @@ public async Task SaveWellbores(List<Wellbore> wellbores, string connectionStrin
wellbores.Where(c => string.IsNullOrEmpty(c.OPERATOR)).Select(c => { c.OPERATOR = "UNKNOWN"; return c; }).ToList();
wellbores.Where(c => string.IsNullOrEmpty(c.ASSIGNED_FIELD)).Select(c => { c.ASSIGNED_FIELD = "UNKNOWN"; return c; }).ToList();
wellbores.Where(c => string.IsNullOrEmpty(c.DEPTH_DATUM)).Select(c => { c.DEPTH_DATUM = "UNKNOWN"; return c; }).ToList();
wellbores.Where(c => string.IsNullOrEmpty(c.CURRENT_STATUS)).Select(c => { c.CURRENT_STATUS = "UNKNOWN"; return c; }).ToList();
await SaveWellboreRefData(wellbores, connectionString);
string sql = "IF NOT EXISTS(SELECT 1 FROM WELL WHERE UWI = @UWI) " +
"INSERT INTO WELL (UWI, SURFACE_LONGITUDE, SURFACE_LATITUDE, BOTTOM_HOLE_LATITUDE, BOTTOM_HOLE_LONGITUDE, " +
"FINAL_TD, OPERATOR, ASSIGNED_FIELD, LEASE_NAME, WELL_NUM, COMPLETION_DATE, DEPTH_DATUM_ELEV, DEPTH_DATUM) " +
"FINAL_TD, OPERATOR, ASSIGNED_FIELD, LEASE_NAME, WELL_NUM, COMPLETION_DATE, DEPTH_DATUM_ELEV, DEPTH_DATUM, " +
"CURRENT_STATUS) " +
"VALUES(@UWI, @SURFACE_LONGITUDE, @SURFACE_LATITUDE, @BOTTOM_HOLE_LATITUDE, @BOTTOM_HOLE_LONGITUDE, " +
"@FINAL_TD, @OPERATOR, @ASSIGNED_FIELD, @LEASE_NAME, @WELL_NUM, @COMPLETION_DATE, @DEPTH_DATUM_ELEV, @DEPTH_DATUM)";
"@FINAL_TD, @OPERATOR, @ASSIGNED_FIELD, @LEASE_NAME, @WELL_NUM, @COMPLETION_DATE, " +
"@DEPTH_DATUM_ELEV, @DEPTH_DATUM, @CURRENT_STATUS)";
await _da.SaveData(connectionString, wellbores,sql);
}

Expand All @@ -43,18 +46,28 @@ public async Task SaveWellboreRefData(List<Wellbore> wellbores, string connectio
refDict.Add(tables.RefTables[1].Table, refs);
refs = wellbores.Select(x => x.DEPTH_DATUM).Distinct().ToList().CreateReferenceDataObject();
refDict.Add(tables.RefTables[2].Table, refs);

refs = wellbores.Select(x => x.CURRENT_STATUS).Distinct().ToList().CreateReferenceDataObject();
refDict.Add(tables.RefTables[3].Table, refs);
foreach (var table in tables.RefTables)
{
refs = refDict[table.Table];
string sql = $"IF NOT EXISTS(SELECT 1 FROM {table.Table} WHERE {table.KeyAttribute} = @Reference) " +
string sql = "";
if (table.Table == "R_WELL_STATUS")
{
sql = $"IF NOT EXISTS(SELECT 1 FROM {table.Table} WHERE {table.KeyAttribute} = @Reference) " +
$"INSERT INTO {table.Table} " +
$"(STATUS_TYPE, {table.KeyAttribute}, {table.ValueAttribute}) " +
$"VALUES('STATUS', @Reference, @Reference)";
}
else
{
sql = $"IF NOT EXISTS(SELECT 1 FROM {table.Table} WHERE {table.KeyAttribute} = @Reference) " +
$"INSERT INTO {table.Table} " +
$"({table.KeyAttribute}, {table.ValueAttribute}) " +
$"VALUES(@Reference, @Reference)";
}
await _da.SaveData(connectionString, refs, sql);
}


}

public Task<List<WellHeaderData>> ReadWellInfo(string connectionString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using PPDMLoaderLibrary.Models;
using System.Text;

namespace TexasPPDMLoader.Data
namespace PPDMLoaderLibrary.Data
{
public class WellboreDataInfoDbase : IWellboreData
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using DbfDataReader;
using PPDMLoaderLibrary.DataAccess;
using PPDMLoaderLibrary.Extensions;
using PPDMLoaderLibrary.Models;
using System.Text;

namespace TexasPPDMLoader.Data
namespace PPDMLoaderLibrary.Data
{
public class WellboreDataLocationsDbase : IWellboreData
{
Expand All @@ -16,6 +17,8 @@ public WellboreDataLocationsDbase(IDataAccess da)

public async Task<List<Wellbore>> ReadWellbores(string connectionString)
{
Dictionary<int, string> plotSymbols = await ReadPlotSymbolsToDictionary("PlotSymbolDictionary.txt");

List<Wellbore> wellbores = new List<Wellbore>();

string file = connectionString + "l.dbf";
Expand Down Expand Up @@ -56,7 +59,8 @@ public async Task<List<Wellbore>> ReadWellbores(string connectionString)
SURFACE_LONGITUDE = surfLon,
BOTTOM_HOLE_LATITUDE = well.Lat27,
BOTTOM_HOLE_LONGITUDE = well.Long27,
WELL_NUM = well.WellNumber
WELL_NUM = well.WellNumber,
CURRENT_STATUS = plotSymbols[well.SymbolNumber]
};
if (uwiLength == 8)
{
Expand All @@ -82,7 +86,8 @@ public async Task<List<Wellbore>> ReadWellbores(string connectionString)
SURFACE_LATITUDE = surfLat,
SURFACE_LONGITUDE = surfLon,
BOTTOM_HOLE_LATITUDE = well.Lat27,
BOTTOM_HOLE_LONGITUDE = well.Lon27
BOTTOM_HOLE_LONGITUDE = well.Lon27,
CURRENT_STATUS = plotSymbols[well.SymbolNumber]
};
int uwiLength = well.Api.Length;
if (uwiLength == 8)
Expand Down Expand Up @@ -250,5 +255,36 @@ public Task<List<WellHeaderData>> ReadWellInfo(string connectionString)
{
throw new NotImplementedException();
}

private async Task<Dictionary<int,string>> ReadPlotSymbolsToDictionary(string filePath)
{
IFileStorageService fileStorage = new EmbeddedFileStorageService();
string plotSymbols = await fileStorage.ReadFile("", filePath);
Dictionary<int, string> dictionary = new Dictionary<int, string>();
try
{
string[] lines = plotSymbols.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
int spaceIndex = line.IndexOf(' ');
if (spaceIndex >= 0 && int.TryParse(line.Substring(0, spaceIndex), out int number))
{
string text = line.Substring(spaceIndex + 1);
if (text.Length > 40)
{
text = text.Truncate(40);
}
dictionary[number] = text;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}

return dictionary;
}

}
}
33 changes: 33 additions & 0 deletions PPDMLoaderLibrary/DataAccess/EmbeddedFileStorageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PPDMLoaderLibrary.DataAccess
{
public class EmbeddedFileStorageService : IFileStorageService
{
public async Task<string> ReadFile(string fileShare, string fileName)
{
string fileContent = "";
try
{
Assembly asm = Assembly.GetExecutingAssembly();
string[] names = asm.GetManifestResourceNames();
string streamName = @"PPDMLoaderLibrary." + fileName;
using Stream stream = asm.GetManifestResourceStream(streamName);
using StreamReader reader = new(stream);
fileContent = await reader.ReadToEndAsync();
}
catch (Exception ex)
{
Console.WriteLine($"Error reading plotsymbol file; {ex}");

}

return fileContent;
}
}
}
13 changes: 13 additions & 0 deletions PPDMLoaderLibrary/DataAccess/IFileStorageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPDMLoaderLibrary.DataAccess
{
public interface IFileStorageService
{
Task<string> ReadFile(string fileShare, string fileName);
}
}
49 changes: 42 additions & 7 deletions PPDMLoaderLibrary/DownloadDataFromWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ namespace PPDMLoaderLibrary
public class DownloadDataFromWeb
{
private readonly string _path;
private readonly string wellBoreUrl = @"https://mft.rrc.texas.gov/link/4e9023eb-e4ee-45b8-81b7-aec1494c1e8e";
private readonly string wellApiUrl = @"https://mft.rrc.texas.gov/link/7ed6883a-875d-4e24-a7e5-1614d5968389";
private readonly string wellBoreUrl = @"https://mft.rrc.texas.gov/link/d551fb20-442e-4b67-84fa-ac3f23ecabb4";
private readonly string wellApiUrl = @"https://mft.rrc.texas.gov/link/1eb94d66-461d-4114-93f7-b4bc04a70674";
private readonly string wellPermitUrl = @"https://mft.rrc.texas.gov/link/91a36fea-4dad-4f26-96c3-30843d0e0315";
private readonly string fullWellboreUrl = @"https://mft.rrc.texas.gov/link/9ef1955f-cf26-4bd4-8030-1253eb772cf9";
private readonly string fullWellboreUrl = @"https://mft.rrc.texas.gov/link/b070ce28-5c58-4fe2-9eb7-8b70befb7af9";

public DownloadDataFromWeb(string path = "C:\temp")
{
Expand All @@ -29,7 +29,19 @@ public void DownloadWells(string countyCode)
{
string url = wellBoreUrl;
string file = "well" + countyCode + ".zip";
ChromeDownload(url, file);
string filePath = _path + "/" + file;
bool download = true;
if (File.Exists(filePath))
{
DateTime lastWriteTime = File.GetLastWriteTime(filePath);
DateTime currentDate = DateTime.Now;
if (currentDate < lastWriteTime.AddDays(14))
{
Console.WriteLine("The wellbore file was last written less than 14 days ago.");
download = false;
}
}
if (download) ChromeDownload(url, file);

string zipPath = _path + @"\" + file;
string extractPath = _path + @"\extract";
Expand All @@ -45,15 +57,38 @@ public void DownloadApiData(string countyCode)
{
string url = wellApiUrl;
string file = "api" + countyCode + ".dbf";
ChromeDownload(url, file);
string filePath = _path + "/" + file;
bool download = true;
if (File.Exists(filePath))
{
DateTime lastWriteTime = File.GetLastWriteTime(filePath);
DateTime currentDate = DateTime.Now;
if (currentDate < lastWriteTime.AddDays(14))
{
Console.WriteLine("The wellbore API file was last written less than 14 days ago.");
download = false;
}
}
if (download) ChromeDownload(url, file);
}

public void DownloadFullWellboreData()
{
string url = fullWellboreUrl;
string file = "dbf900.txt.gz";
ChromeDownload(url, file);

string filePath = _path + "/" + file;
bool download = true;
if (File.Exists(filePath))
{
DateTime lastWriteTime = File.GetLastWriteTime(filePath);
DateTime currentDate = DateTime.Now;
if (currentDate < lastWriteTime.AddMonths(1))
{
Console.WriteLine("The full wellbore file was last written less than 1 month ago.");
download= false;
}
}
if (download) ChromeDownload(url, file);
string extractedFilePath = Path.Combine(_path, Path.GetFileNameWithoutExtension(file));
string extractPath = _path + @"\extract";
string zipPath = _path + @"\" + file;
Expand Down
5 changes: 5 additions & 0 deletions PPDMLoaderLibrary/Extensions/CommonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,10 @@ public static string[] ParseString(this string ln, int[] widths)
}
return ret;
}

public static string Truncate(this string value, int maxLength)
{
return value?.Substring(0, Math.Min(value.Length, maxLength));
}
}
}
2 changes: 2 additions & 0 deletions PPDMLoaderLibrary/Models/ReferenceTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public ReferenceTables()
{ KeyAttribute = "FIELD_ID", Table = "FIELD", ValueAttribute= "FIELD_NAME"},
new ReferenceTable()
{ KeyAttribute = "WELL_DATUM_TYPE", Table = "R_WELL_DATUM_TYPE", ValueAttribute= "LONG_NAME"},
new ReferenceTable()
{ KeyAttribute = "STATUS", Table = "R_WELL_STATUS", ValueAttribute= "LONG_NAME"},
};
}
}
Expand Down
1 change: 1 addition & 0 deletions PPDMLoaderLibrary/Models/Wellbore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Wellbore
public double? BOTTOM_HOLE_LATITUDE { get; set; }
public double? BOTTOM_HOLE_LONGITUDE { get; set; }
public string LEASE_NAME { get; set; }
public string CURRENT_STATUS { get; set; }
public string OPERATOR { get; set; }
public string ASSIGNED_FIELD { get; set; }
public string WELL_NUM { get; set; }
Expand Down
9 changes: 9 additions & 0 deletions PPDMLoaderLibrary/PPDMLoaderLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="PlotSymbolDictionary.txt" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="PlotSymbolDictionary.txt" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="DbfDataReader" Version="0.9.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" />
<PackageReference Include="Selenium.WebDriver" Version="4.7.0" />
<PackageReference Include="WebDriverManager" Version="2.16.2" />
Expand Down
Loading

0 comments on commit f524480

Please sign in to comment.