diff --git a/README.md b/README.md index dde35ea..e6cdc24 100755 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Read and explore NetCDF files ## Example ```js +const fs = require('fs'); const netcdfjs = require('netcdfjs'); ``` diff --git a/package.json b/package.json index 693c949..c26a613 100755 --- a/package.json +++ b/package.json @@ -34,5 +34,8 @@ "mocha": "^3.1.2", "mocha-better-spec-reporter": "^3.0.2", "should": "^11.1.1" + }, + "dependencies": { + "iobuffer": "^2.1.0" } } diff --git a/src/index.js b/src/index.js index 8076ae0..8998162 100755 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,30 @@ 'use strict'; -module.exports = netcdf; +const IOBuffer = require('iobuffer'); -function netcdf() { - return true; +function netcdf(data) { + const buffer = new IOBuffer(data); + + // Validate that it's a NetCDF file + notNetcdf((buffer.readChars(3) !== 'CDF'), 'should start with CDF'); + + // Check the NetCDF format + const version = buffer.readByte(); + notNetcdf((version === 2), '64-bit offset format not supported yet'); + notNetcdf((version !== 1), 'unknown version'); + + return buffer.length; } + +/** + * Throws a non-valid NetCDF exception if the statement it's true + * @param {boolean} statement - Throws if true + * @param {string} reason - Reason to throw + */ +function notNetcdf(statement, reason) { + if (statement) { + throw new TypeError('Not a valid NetCDF v3.x file: ' + reason); + } +} + +module.exports = netcdf; diff --git a/test/files/madis-sao.nc b/test/files/madis-sao.nc new file mode 100644 index 0000000..3360f77 Binary files /dev/null and b/test/files/madis-sao.nc differ diff --git a/test/files/not_nc.txt b/test/files/not_nc.txt new file mode 100644 index 0000000..0b8d5e1 --- /dev/null +++ b/test/files/not_nc.txt @@ -0,0 +1 @@ +This is not a NetCDF file \ No newline at end of file diff --git a/test/files/test_hgroups.nc b/test/files/test_hgroups.nc new file mode 100644 index 0000000..d688c4c Binary files /dev/null and b/test/files/test_hgroups.nc differ diff --git a/test/test.js b/test/test.js index cc11652..96677f4 100755 --- a/test/test.js +++ b/test/test.js @@ -1,9 +1,19 @@ 'use strict'; const netcdfjs = require('..'); +const fs = require('fs'); +const pathFiles = __dirname + '/files/'; -describe('test', function () { - it('should be tested', function () { - (42).should.equal(42); +describe('Read file', function () { + it('Throws on non NetCDF file', function () { + const data = fs.readFileSync(pathFiles + 'not_nc.txt'); + netcdfjs.bind(null, data).should.throw('Not a valid NetCDF v3.x file: should start with CDF'); + }); + + it('read header information', function () { + // http://www.unidata.ucar.edu/software/netcdf/examples/files.html + const data = fs.readFileSync(pathFiles + 'madis-sao.nc'); + + netcdfjs(data).should.be.equal(266032); }); }); diff --git a/yarn.lock b/yarn.lock index 97ad11b..e7f3d7c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3096,6 +3096,10 @@ invert-kv@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6" +iobuffer: + version "2.1.0" + resolved "https://registry.yarnpkg.com/iobuffer/-/iobuffer-2.1.0.tgz#074882d24020a85db6a5042a0418ef9b6b2e616a" + irregular-plurals@^1.0.0: version "1.2.0" resolved "https://registry.yarnpkg.com/irregular-plurals/-/irregular-plurals-1.2.0.tgz#38f299834ba8c00c30be9c554e137269752ff3ac"