netcdfjs
Read and explore NetCDF files
Installation
$ npm install netcdfjs
For further information about the grammar you should go to this link.
Example I: NodeJS
const fs = require('fs');
const NetCDFReader = require('netcdfjs');
const data = fs.readFileSync('madis-sao.nc');
var reader = new NetCDFReader(data);
reader.getDataVariable('wmoId');
Example II: Load from URL (does not require node)
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();
reader.getDataVariable('wmoId');
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.
var reader;
var progress = document.querySelector('.percent');
function abortRead() { reader.abort(); }
function handleFileSelect(evt) {
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) {
progress.style.width = '100%';
progress.textContent = '100%';
setTimeout("document.getElementById('progress_bar').className='';", 2000);
reader = new netcdfjs(this.result);
reader.getDataVariable('wmoId');
}
reader.readAsArrayBuffer(evt.target.files[0]);
}
var input = document.createElement("input");
input.id='files'
input.type = "file";
input.className = "file";
document.body.appendChild(input);
var progress = document.createElement("div");
progress.id='progress_bar';
inner = document.createElement("div");
inner.className = "percent";
inner.id='innerdiv'
progress.appendChild(inner);
document.body.appendChild(progress);
run = document.getElementById('files').addEventListener('change', handleFileSelect, false);
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) {
if (evt.lengthComputable) {
var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
if (percentLoaded < 100) {
progress.style.width = percentLoaded + '%';
progress.textContent = percentLoaded + '%';
}
}
}
License
MIT