New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

netcdfjs

Package Overview
Dependencies
Maintainers
5
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

netcdfjs - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

27

package.json
{
"name": "netcdfjs",
"version": "1.0.0",
"version": "2.0.0",
"description": "Read and explore NetCDF files",
"main": "lib/index.js",
"module": "src/index.js",
"keywords": [

@@ -11,10 +13,13 @@ "netcdf",

],
"files": [
"src",
"lib"
],
"author": "Miguel Asencio <maasencioh@gmail.com> (https://github.com/maasencioh)",
"repository": "cheminfo-js/netcdfjs",
"repository": "cheminfo/netcdfjs",
"bugs": {
"url": "https://github.com/cheminfo-js/netcdfjs/issues"
"url": "https://github.com/cheminfo/netcdfjs/issues"
},
"homepage": "https://github.com/cheminfo-js/netcdfjs",
"homepage": "https://github.com/cheminfo/netcdfjs",
"license": "MIT",
"main": "./src/index.js",
"scripts": {

@@ -30,11 +35,11 @@ "eslint": "eslint src",

"cheminfo-tools": "^1.23.2",
"eslint": "^5.16.0",
"eslint-config-cheminfo": "^1.20.1",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-jest": "^22.6.4",
"jest": "^24.8.0"
"eslint": "^7.32.0",
"eslint-config-cheminfo": "^5.4.0",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jest": "^24.4.0",
"jest": "^27.1.0"
},
"dependencies": {
"iobuffer": "^4.0.1"
"iobuffer": "^5.0.3"
}
}

@@ -14,139 +14,19 @@ # netcdfjs

## [API Documentation](https://cheminfo-js.github.io/netcdfjs/)
## [API Documentation](https://cheminfo.github.io/netcdfjs/)
For further information about the grammar you should go to [this link](https://www.unidata.ucar.edu/software/netcdf/docs/file_format_specifications.html).
### Example I: NodeJS
### Example
```js
const fs = require('fs');
const NetCDFReader = require('netcdfjs');
const { readFileSync } = require("fs");
const { NetCDFReader } = require("netcdfjs");
// http://www.unidata.ucar.edu/software/netcdf/examples/files.html
const data = fs.readFileSync('madis-sao.nc');
const data = readFileSync("madis-sao.nc");
var reader = new NetCDFReader(data); // read the header
reader.getDataVariable('wmoId'); // go to offset and read it
reader.getDataVariable("wmoId"); // go to offset and read it
```
### Example II: Load from URL (does not require node)
```js
// First load the netcdfjs library as normal : <script src='./dist/netcdfjs.js'></script>
// You could use the oficial CDN: <script src='http://www.lactame.com/lib/netcdfjs/0.3.0/netcdfjs.min.js'></script>
var urlpath =
'http://www.unidata.ucar.edu/software/netcdf/examples/madis-sao.nc';
var reader;
var oReq = new XMLHttpRequest();
oReq.open('GET', urlpath, true);
oReq.responseType = 'blob';
oReq.onload = function(oEvent) {
var blob = oReq.response;
reader_url = new FileReader();
reader_url.onload = function(e) {
reader = new netcdfjs(this.result);
};
reader_url.readAsArrayBuffer(blob);
};
oReq.send(); //start process
reader.getDataVariable('wmoId'); // go to offset and read it
```
### Example III: Client side file upload
This example creates a file input element and allows the user to select a file from their personal machine.
```js
var reader;
var progress = document.querySelector('.percent');
function abortRead() {
reader.abort();
}
function handleFileSelect(evt) {
// Reset progress indicator on new file selection.
progress.style.width = '0%';
progress.textContent = '0%';
reader = new FileReader();
reader.onerror = errorHandler;
reader.onprogress = updateProgress;
reader.onabort = function(e) {
alert('File read cancelled');
};
reader.onloadstart = function(e) {
document.getElementById('progress_bar').className = 'loading';
};
reader.onload = function(e) {
// Ensure that the progress bar displays 100% at the end.
progress.style.width = '100%';
progress.textContent = '100%';
setTimeout("document.getElementById('progress_bar').className='';", 2000);
//var reader = new NetCDFReader(reader.result);
//replace reader with NetCDF reader
reader = new netcdfjs(this.result);
reader.getDataVariable('wmoId'); // go to offset and read it
//... your program here ..//
};
reader.readAsArrayBuffer(evt.target.files[0]);
}
// Make input element <input type="file" id="files" name="file" />
var input = document.createElement('input');
input.id = 'files';
input.type = 'file';
input.className = 'file';
document.body.appendChild(input); // put it into the DOM
// Make a Progress bar <div id="progress_bar"><div class="percent">0%</div></div>
var progress = document.createElement('div');
progress.id = 'progress_bar';
inner = document.createElement('div');
inner.className = 'percent';
inner.id = 'innerdiv'; // set the CSS class
progress.appendChild(inner);
document.body.appendChild(progress); // put it into the DOM
//Start event listener to check if a file has been selected
run = document
.getElementById('files')
.addEventListener('change', handleFileSelect, false);
///Progress bar and other functions
function errorHandler(evt) {
switch (evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
alert('File Not Found!');
break;
case evt.target.error.NOT_READABLE_ERR:
alert('File is not readable');
break;
case evt.target.error.ABORT_ERR:
break;
default:
alert('An error occurred reading this file.');
}
}
function updateProgress(evt) {
// evt is an ProgressEvent. Updates progress bar
if (evt.lengthComputable) {
var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
// Increase the progress bar length.
if (percentLoaded < 100) {
progress.style.width = percentLoaded + '%';
progress.textContent = percentLoaded + '%';
}
}
}
```
## License

@@ -158,7 +38,5 @@

[npm-url]: https://www.npmjs.com/package/netcdfjs
[travis-image]: https://img.shields.io/travis/cheminfo-js/netcdfjs/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/cheminfo-js/netcdfjs
[coveralls-image]: https://img.shields.io/coveralls/cheminfo-js/netcdfjs.svg?style=flat-square
[coveralls-url]: https://coveralls.io/github/cheminfo-js/netcdfjs
[coveralls-image]: https://img.shields.io/coveralls/cheminfo/netcdfjs.svg?style=flat-square
[coveralls-url]: https://coveralls.io/github/cheminfo/netcdfjs
[download-image]: https://img.shields.io/npm/dm/netcdfjs.svg?style=flat-square
[download-url]: https://www.npmjs.com/package/netcdfjs

@@ -1,5 +0,3 @@

'use strict';
import { num2bytes, str2num, readType } from "./types.js";
const types = require('./types');
// const STREAMING = 4294967295;

@@ -14,13 +12,13 @@

*/
function nonRecord(buffer, variable) {
export function nonRecord(buffer, variable) {
// variable type
const type = types.str2num(variable.type);
const type = str2num(variable.type);
// size of the data
var size = variable.size / types.num2bytes(type);
let size = variable.size / num2bytes(type);
// iterates over the data
var data = new Array(size);
for (var i = 0; i < size; i++) {
data[i] = types.readType(buffer, type, 1);
let data = new Array(size);
for (let i = 0; i < size; i++) {
data[i] = readType(buffer, type, 1);
}

@@ -39,18 +37,18 @@

*/
function record(buffer, variable, recordDimension) {
export function record(buffer, variable, recordDimension) {
// variable type
const type = types.str2num(variable.type);
const width = variable.size ? variable.size / types.num2bytes(type) : 1;
const type = str2num(variable.type);
const width = variable.size ? variable.size / num2bytes(type) : 1;
// size of the data
// TODO streaming data
var size = recordDimension.length;
let size = recordDimension.length;
// iterates over the data
var data = new Array(size);
let data = new Array(size);
const step = recordDimension.recordStep;
for (var i = 0; i < size; i++) {
var currentOffset = buffer.offset;
data[i] = types.readType(buffer, type, width);
for (let i = 0; i < size; i++) {
let currentOffset = buffer.offset;
data[i] = readType(buffer, type, width);
buffer.seek(currentOffset + step);

@@ -61,4 +59,1 @@ }

}
module.exports.nonRecord = nonRecord;
module.exports.record = record;

@@ -1,6 +0,4 @@

'use strict';
import { num2str, readType } from "./types.js";
import { padding, notNetcdf, readName } from "./utils.js";
const utils = require('./utils');
const types = require('./types');
// Grammar constants

@@ -23,6 +21,6 @@ const ZERO = 0;

*/
function header(buffer, version) {
export function header(buffer, version) {
// Length of record dimension
// sum of the varSize's of all the record variables.
var header = { recordDimension: { length: buffer.readUint32() } };
let header = { recordDimension: { length: buffer.readUint32() } };

@@ -33,3 +31,3 @@ // Version

// List of dimensions
var dimList = dimensionsList(buffer);
let dimList = dimensionsList(buffer);
header.recordDimension.id = dimList.recordId; // id of the unlimited dimension

@@ -43,3 +41,3 @@ header.recordDimension.name = dimList.recordName; // name of the unlimited dimension

// List of variables
var variables = variablesList(buffer, dimList.recordId, version);
let variables = variablesList(buffer, dimList.recordId, version);
header.variables = variables.variables;

@@ -59,4 +57,4 @@ header.recordDimension.recordStep = variables.recordStep;

* * `dimensions` that is an array of dimension object:
* * `name`: String with the name of the dimension
* * `size`: Number with the size of the dimension dimensions: dimensions
* * `name`: String with the name of the dimension
* * `size`: Number with the size of the dimension dimensions: dimensions
* * `recordId`: the id of the dimension that has unlimited size or undefined,

@@ -66,20 +64,25 @@ * * `recordName`: name of the dimension that has unlimited size

function dimensionsList(buffer) {
var recordId, recordName;
let recordId, recordName;
const dimList = buffer.readUint32();
let dimensions;
if (dimList === ZERO) {
utils.notNetcdf((buffer.readUint32() !== ZERO), 'wrong empty tag for list of dimensions');
notNetcdf(
buffer.readUint32() !== ZERO,
"wrong empty tag for list of dimensions"
);
return [];
} else {
utils.notNetcdf((dimList !== NC_DIMENSION), 'wrong tag for list of dimensions');
notNetcdf(dimList !== NC_DIMENSION, "wrong tag for list of dimensions");
// Length of dimensions
const dimensionSize = buffer.readUint32();
var dimensions = new Array(dimensionSize);
for (var dim = 0; dim < dimensionSize; dim++) {
dimensions = new Array(dimensionSize);
for (let dim = 0; dim < dimensionSize; dim++) {
// Read name
var name = utils.readName(buffer);
let name = readName(buffer);
// Read dimension size
const size = buffer.readUint32();
if (size === NC_UNLIMITED) { // in netcdf 3 one field can be of size unlimmited
if (size === NC_UNLIMITED) {
// in netcdf 3 one field can be of size unlimmited
recordId = dim;

@@ -91,3 +94,3 @@ recordName = name;

name: name,
size: size
size: size,
};

@@ -97,5 +100,5 @@ }

return {
dimensions: dimensions,
recordId: recordId,
recordName: recordName
dimensions,
recordId,
recordName,
};

@@ -115,30 +118,34 @@ }

const gAttList = buffer.readUint32();
let attributes;
if (gAttList === ZERO) {
utils.notNetcdf((buffer.readUint32() !== ZERO), 'wrong empty tag for list of attributes');
notNetcdf(
buffer.readUint32() !== ZERO,
"wrong empty tag for list of attributes"
);
return [];
} else {
utils.notNetcdf((gAttList !== NC_ATTRIBUTE), 'wrong tag for list of attributes');
notNetcdf(gAttList !== NC_ATTRIBUTE, "wrong tag for list of attributes");
// Length of attributes
const attributeSize = buffer.readUint32();
var attributes = new Array(attributeSize);
for (var gAtt = 0; gAtt < attributeSize; gAtt++) {
attributes = new Array(attributeSize);
for (let gAtt = 0; gAtt < attributeSize; gAtt++) {
// Read name
var name = utils.readName(buffer);
let name = readName(buffer);
// Read type
var type = buffer.readUint32();
utils.notNetcdf(((type < 1) || (type > 6)), `non valid type ${type}`);
let type = buffer.readUint32();
notNetcdf(type < 1 || type > 6, `non valid type ${type}`);
// Read attribute
var size = buffer.readUint32();
var value = types.readType(buffer, type, size);
let size = buffer.readUint32();
let value = readType(buffer, type, size);
// Apply padding
utils.padding(buffer);
padding(buffer);
attributes[gAtt] = {
name: name,
type: types.num2str(type),
value: value
name,
type: num2str(type),
value,
};

@@ -169,15 +176,19 @@ }

const varList = buffer.readUint32();
var recordStep = 0;
let recordStep = 0;
let variables;
if (varList === ZERO) {
utils.notNetcdf((buffer.readUint32() !== ZERO), 'wrong empty tag for list of variables');
notNetcdf(
buffer.readUint32() !== ZERO,
"wrong empty tag for list of variables"
);
return [];
} else {
utils.notNetcdf((varList !== NC_VARIABLE), 'wrong tag for list of variables');
notNetcdf(varList !== NC_VARIABLE, "wrong tag for list of variables");
// Length of variables
const variableSize = buffer.readUint32();
var variables = new Array(variableSize);
for (var v = 0; v < variableSize; v++) {
variables = new Array(variableSize);
for (let v = 0; v < variableSize; v++) {
// Read name
var name = utils.readName(buffer);
let name = readName(buffer);

@@ -188,4 +199,4 @@ // Read dimensionality of the variable

// Index into the list of dimensions
var dimensionsIds = new Array(dimensionality);
for (var dim = 0; dim < dimensionality; dim++) {
let dimensionsIds = new Array(dimensionality);
for (let dim = 0; dim < dimensionality; dim++) {
dimensionsIds[dim] = buffer.readUint32();

@@ -195,7 +206,7 @@ }

// Read variables size
var attributes = attributesList(buffer);
let attributes = attributesList(buffer);
// Read type
var type = buffer.readUint32();
utils.notNetcdf(((type < 1) && (type > 6)), `non valid type ${type}`);
let type = buffer.readUint32();
notNetcdf(type < 1 && type > 6, `non valid type ${type}`);

@@ -208,5 +219,5 @@ // Read variable size

// Read offset
var offset = buffer.readUint32();
let offset = buffer.readUint32();
if (version === 2) {
utils.notNetcdf((offset > 0), 'offsets larger than 4GB not supported');
notNetcdf(offset > 0, "offsets larger than 4GB not supported");
offset = buffer.readUint32();

@@ -217,3 +228,3 @@ }

// Count amount of record variables
if ((typeof recordId !== 'undefined') && (dimensionsIds[0] === recordId)) {
if (typeof recordId !== "undefined" && dimensionsIds[0] === recordId) {
recordStep += varSize;

@@ -226,6 +237,6 @@ record = true;

attributes,
type: types.num2str(type),
type: num2str(type),
size: varSize,
offset,
record
record,
};

@@ -236,7 +247,5 @@ }

return {
variables: variables,
recordStep: recordStep
variables,
recordStep,
};
}
module.exports = header;

@@ -1,10 +0,8 @@

'use strict';
import { IOBuffer } from "iobuffer";
const { IOBuffer } = require('iobuffer');
import { record, nonRecord } from "./data.js";
import { header } from "./header.js";
import { toString } from "./toString.js";
import { notNetcdf } from "./utils.js";
const utils = require('./utils');
const data = require('./data');
const readHeader = require('./header');
const toString = require('./toString');
/**

@@ -16,3 +14,3 @@ * Reads a NetCDF v3.x file

*/
class NetCDFReader {
export class NetCDFReader {
constructor(data) {

@@ -23,10 +21,10 @@ const buffer = new IOBuffer(data);

// Validate that it's a NetCDF file
utils.notNetcdf(buffer.readChars(3) !== 'CDF', 'should start with CDF');
notNetcdf(buffer.readChars(3) !== "CDF", "should start with CDF");
// Check the NetCDF format
const version = buffer.readByte();
utils.notNetcdf(version > 2, 'unknown version');
notNetcdf(version > 2, "unknown version");
// Read the header
this.header = readHeader(buffer, version);
this.header = header(buffer, version);
this.buffer = buffer;

@@ -40,5 +38,5 @@ }

if (this.header.version === 1) {
return 'classic format';
return "classic format";
} else {
return '64-bit offset format';
return "64-bit offset format";
}

@@ -97,3 +95,3 @@ }

const variable = this.getDataVariable(variableName);
if (variable) return variable.join('');
if (variable) return variable.join("");
return null;

@@ -127,5 +125,5 @@ }

let variable;
if (typeof variableName === 'string') {
if (typeof variableName === "string") {
// search the variable
variable = this.header.variables.find(function (val) {
variable = this.header.variables.find((val) => {
return val.name === variableName;

@@ -138,6 +136,3 @@ });

// throws if variable not found
utils.notNetcdf(
variable === undefined,
`variable not found: ${variableName}`
);
notNetcdf(variable === undefined, `variable not found: ${variableName}`);

@@ -149,6 +144,6 @@ // go to the offset position

// record variable case
return data.record(this.buffer, variable, this.header.recordDimension);
return record(this.buffer, variable, this.header.recordDimension);
} else {
// non-record variable case
return data.nonRecord(this.buffer, variable);
return nonRecord(this.buffer, variable);
}

@@ -163,3 +158,3 @@ }

dataVariableExists(variableName) {
const variable = this.header.variables.find(function (val) {
const variable = this.header.variables.find((val) => {
return val.name === variableName;

@@ -182,3 +177,1 @@ });

}
module.exports = NetCDFReader;

@@ -1,7 +0,5 @@

'use strict';
function toString() {
export function toString() {
let result = [];
result.push('DIMENSIONS');
result.push("DIMENSIONS");
for (let dimension of this.dimensions) {

@@ -11,4 +9,4 @@ result.push(` ${dimension.name.padEnd(30)} = size: ${dimension.size}`);

result.push('');
result.push('GLOBAL ATTRIBUTES');
result.push("");
result.push("GLOBAL ATTRIBUTES");
for (let attribute of this.globalAttributes) {

@@ -19,4 +17,4 @@ result.push(` ${attribute.name.padEnd(30)} = ${attribute.value}`);

let variables = JSON.parse(JSON.stringify(this.variables));
result.push('');
result.push('VARIABLES:');
result.push("");
result.push("VARIABLES:");
for (let variable of variables) {

@@ -31,5 +29,3 @@ variable.value = this.getDataVariable(variable);

}
return result.join('\n');
return result.join("\n");
}
module.exports = toString;

@@ -1,5 +0,3 @@

'use strict';
import { notNetcdf } from "./utils.js";
const notNetcdf = require('./utils').notNetcdf;
const types = {

@@ -11,3 +9,3 @@ BYTE: 1,

FLOAT: 5,
DOUBLE: 6
DOUBLE: 6,
};

@@ -21,19 +19,19 @@

*/
function num2str(type) {
export function num2str(type) {
switch (Number(type)) {
case types.BYTE:
return 'byte';
return "byte";
case types.CHAR:
return 'char';
return "char";
case types.SHORT:
return 'short';
return "short";
case types.INT:
return 'int';
return "int";
case types.FLOAT:
return 'float';
return "float";
case types.DOUBLE:
return 'double';
/* istanbul ignore next */
return "double";
/* istanbul ignore next */
default:
return 'undefined';
return "undefined";
}

@@ -48,3 +46,3 @@ }

*/
function num2bytes(type) {
export function num2bytes(type) {
switch (Number(type)) {

@@ -63,3 +61,3 @@ case types.BYTE:

return 8;
/* istanbul ignore next */
/* istanbul ignore next */
default:

@@ -76,17 +74,17 @@ return -1;

*/
function str2num(type) {
export function str2num(type) {
switch (String(type)) {
case 'byte':
case "byte":
return types.BYTE;
case 'char':
case "char":
return types.CHAR;
case 'short':
case "short":
return types.SHORT;
case 'int':
case "int":
return types.INT;
case 'float':
case "float":
return types.FLOAT;
case 'double':
case "double":
return types.DOUBLE;
/* istanbul ignore next */
/* istanbul ignore next */
default:

@@ -106,4 +104,4 @@ return -1;

if (size !== 1) {
var numbers = new Array(size);
for (var i = 0; i < size; i++) {
let numbers = new Array(size);
for (let i = 0; i < size; i++) {
numbers[i] = bufferReader();

@@ -125,3 +123,3 @@ }

*/
function readType(buffer, type, size) {
export function readType(buffer, type, size) {
switch (type) {

@@ -140,3 +138,3 @@ case types.BYTE:

return readNumber(size, buffer.readFloat64.bind(buffer));
/* istanbul ignore next */
/* istanbul ignore next */
default:

@@ -160,7 +158,1 @@ notNetcdf(true, `non valid type ${type}`);

}
module.exports = types;
module.exports.num2str = num2str;
module.exports.num2bytes = num2bytes;
module.exports.str2num = str2num;
module.exports.readType = readType;

@@ -1,3 +0,1 @@

'use strict';
/**

@@ -9,3 +7,3 @@ * Throws a non-valid NetCDF exception if the statement it's true

*/
function notNetcdf(statement, reason) {
export function notNetcdf(statement, reason) {
if (statement) {

@@ -21,4 +19,4 @@ throw new TypeError(`Not a valid NetCDF v3.x file: ${reason}`);

*/
function padding(buffer) {
if ((buffer.offset % 4) !== 0) {
export function padding(buffer) {
if (buffer.offset % 4 !== 0) {
buffer.skip(4 - (buffer.offset % 4));

@@ -28,3 +26,2 @@ }

/**

@@ -36,6 +33,6 @@ * Reads the name

*/
function readName(buffer) {
export function readName(buffer) {
// Read name
var nameLength = buffer.readUint32();
var name = buffer.readChars(nameLength);
let nameLength = buffer.readUint32();
let name = buffer.readChars(nameLength);

@@ -49,5 +46,1 @@ // validate name

}
module.exports.notNetcdf = notNetcdf;
module.exports.padding = padding;
module.exports.readName = readName;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc