Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Commit e71466a

Browse files
committed
v0.6.4
1 parent 8af2b8b commit e71466a

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

nuget.package/Grib.Api.nuspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<projectUrl>https://github.com/0x1mason/GribApi.NET</projectUrl>
1111
<iconUrl>https://raw.githubusercontent.com/0x1mason/GribApi.NET/master/build/GribApi.png</iconUrl>
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
13-
<description>GribApi.NET is a C# library for reading and writing GRIB 1 and 2 files. It wraps the European Centre for Medium Range Weather Forecasting's powerful C library, grib_api.</description>
13+
<description>GribApi.NET is a C# library for reading and writing GRIB 1 and 2 files. It wraps the European Centre for Medium Range Weather Forecasting's powerful C library, grib_api. NOTE: This library requires the 2013 MSVC redistributable package (https://www.microsoft.com/en-us/download/details.aspx?id=40784)</description>
1414
<copyright>Copyright 2015</copyright>
1515
<tags>Geospatial Geo GRIB Weather Forecast Meteorology</tags>
1616
</metadata>

src/GribApi.NET/Grib.Api.Tests/Get.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void TestInvalidFiles ()
2626
// shouldn't get here
2727
Assert.IsTrue(false);
2828
}
29-
} catch (GribApiException) { }
29+
} catch (FileLoadException) { }
3030

3131
try
3232
{

src/GribApi.NET/Grib.Api/GribFile.cs

+47-10
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public GribFile (string fileName)
5353
FileInfo fi = new FileInfo(fileName);
5454

5555
// need a better check
56-
if (fi.Length < 8)
56+
if (!GribFile.FileIsValid(fileName))
5757
{
5858
throw new FileLoadException("This file is empty or invalid.");
5959
}
@@ -74,15 +74,7 @@ public GribFile (string fileName)
7474
// set the message count here; the result seems to be connected to the message iterator so
7575
// that after you begin iterating messages, the count decreases until it reaches 1.
7676
int count = 0;
77-
78-
try
79-
{
80-
GribApiProxy.GribCountInFile(Context, this, out count);
81-
} catch (GribApiException ex)
82-
{
83-
throw new GribApiException("Failed to parse and count GRIB messages.", ex);
84-
}
85-
77+
GribApiProxy.GribCountInFile(Context, this, out count);
8678
MessageCount = count;
8779
}
8880

@@ -167,6 +159,51 @@ public static void Write (string path, IEnumerable<GribMessage> messages, FileMo
167159
}
168160
}
169161

162+
/// <summary>
163+
/// Performs a basic test to determine if the file is in valid GRIB format.
164+
/// </summary>
165+
/// <param name="fileName">Name of the file.</param>
166+
/// <returns></returns>
167+
private static bool FileIsValid(string fileName)
168+
{
169+
bool isValid = false;
170+
171+
try
172+
{
173+
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
174+
{
175+
if (fs.Length < 8) { return isValid; }
176+
177+
Debug.Assert(fs.CanRead && fs.CanSeek);
178+
179+
long offset = -1;
180+
fs.Seek(offset, SeekOrigin.End);
181+
182+
while (fs.Position > 0 && fs.ReadByte() == 0x00)
183+
{
184+
fs.Seek(--offset, SeekOrigin.End);
185+
}
186+
187+
long start = Math.Abs(offset);
188+
fs.Seek(offset, SeekOrigin.End);
189+
190+
while (fs.Position > 0 &&
191+
fs.ReadByte() == 0x37 &&
192+
start + offset > -4)
193+
{
194+
fs.Seek(--offset, SeekOrigin.End);
195+
}
196+
197+
isValid = start + offset == -4;
198+
}
199+
} catch (Exception)
200+
{
201+
isValid = false;
202+
}
203+
204+
return isValid;
205+
}
206+
170207
/// <summary>
171208
/// Gets the name of the file.
172209
/// </summary>

0 commit comments

Comments
 (0)