mirror of
https://github.com/cheminfo/netcdfjs.git
synced 2026-02-01 16:57:35 +00:00
feat: validates that it's a NetCDF file
This commit is contained in:
parent
b9e3824f8b
commit
1439756c4b
@ -17,6 +17,7 @@ Read and explore NetCDF files
|
||||
## Example
|
||||
|
||||
```js
|
||||
const fs = require('fs');
|
||||
const netcdfjs = require('netcdfjs');
|
||||
```
|
||||
|
||||
|
||||
@ -34,5 +34,8 @@
|
||||
"mocha": "^3.1.2",
|
||||
"mocha-better-spec-reporter": "^3.0.2",
|
||||
"should": "^11.1.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"iobuffer": "^2.1.0"
|
||||
}
|
||||
}
|
||||
|
||||
29
src/index.js
29
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;
|
||||
|
||||
BIN
test/files/madis-sao.nc
Normal file
BIN
test/files/madis-sao.nc
Normal file
Binary file not shown.
1
test/files/not_nc.txt
Normal file
1
test/files/not_nc.txt
Normal file
@ -0,0 +1 @@
|
||||
This is not a NetCDF file
|
||||
BIN
test/files/test_hgroups.nc
Normal file
BIN
test/files/test_hgroups.nc
Normal file
Binary file not shown.
16
test/test.js
16
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);
|
||||
});
|
||||
});
|
||||
|
||||
@ -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"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user