JustinGOSSES / wellio.js

JavaScript for converting well-log standard .las file format to json format
https://justingosses.github.io/wellio.js/
MIT License
34 stars 5 forks source link

Enable jupyter notebook to read json with fs #47

Closed dcslagel closed 4 years ago

dcslagel commented 4 years ago

This is a draft discussion branch to clarify how we should handle reading files in the different environments: Node.js command line, Jupyter notebook, Angular bundle. This draft branch is related to issue #40.

This iteration is a rough-draft that enables the demonstration Jupyter notebook to read lasio-json using the current wellio.read_lasio_json_file() implementation.

There are 2 main changes:

The latest on the Lasio master branch translates 'NaN' to 'null' when writing to json files. However, it only does this in the ASCII data section so far.

This draft will probably not work for Angular bundles or other web frontends as node.fs. We will need a test environment to workout that solution.

Thanks, DC

dcslagel commented 4 years ago

@JustinGOSSES,

Commits ccbb41e updates this branch to have 3 examples for reading from Lasio generated LAS/JSON to Wellio.js/Wellioviz.js. I included a markdown version of the ipynb file. It is helpful set 'display the rich diff' setting in-order to read the markdown file easily.

After generating a LAS structure in Lasio, output the structure to a LAS 2.0 file with the following code example, then one of the subsequent 3 methods can be used in Jupyter to successfully read the structure into wellio.js format.

Create LAS structure, and set LAS version to 2.0 by writing out 'scratch_v2.las'

# python
las = lasio.LASFile()

las.well.DATE = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
las.params['ENG'] = lasio.HeaderItem('ENG', value='Kent Inverarity')
las.params['LMF'] = lasio.HeaderItem('LMF', value='GL')
las.other = 'Example of how to create a LAS file from scratch using lasio'
depths = np.arange(10, 50, 0.5)
synth = np.log10(depths)*5+np.random.random(len(depths))
synth[:8] = np.nan
las.add_curve('DEPT', depths, unit='m')
las.add_curve('SYNTH', synth, descr='fake data')
# Write internal structure to a LAS file
las.write('scratch_v2.las', version=2)
# Create an in-memory lasio-json structure
json_images = json.dumps(las, cls=lasio.JSONEncoder)

Lasio LAS access/translation methods

  1. Translate the Lasio LAS structure to an in-memory JSON structure, then translate this structure to Wellio.js json via: JSON.parse() -> wellio.lasio_obj_2_wellio_obj(). This is the most flexible method because it doesn't require interacting with the host file system.
// node
let lasio_obj = '';
let wellio_obj = '';

try {
    lasio_obj = JSON.parse(json_images);
    wellio_obj = wellio.lasio_obj_2_wellio_obj(lasio_obj);
} catch (e) {
    console.log('[');
    console.log(e.name + ":: " + e.message);
    console.log(']');
}
console.log(wellio_obj);
  1. Have Lasio dump a Lasio-JSON encoded version to a file ('data.json'). Then read this Lasio-JSON file into Wellio and translate it to Wellio JSON. The example demonstrated in the markdown and ipynb files show how to obtain the current directory. The current directory is needed to find and read the file successfully.
# python
las_json_dict =json.loads(json_images)
with open('data.json', 'w') as outfile:
    json.dump(las_json_dict, outfile)
// node
const path = require('path');
let mydir = process.env.PWD;
let myfile = mydir + path.sep + 'data.json';

let lasio_json_str = '';
let lasio_obj_2 = '';
let wellio_obj_2 = '';

try {
    lasio_json_str = wellio.read_lasio_json_file(myfile);
    lasio_obj_2 = JSON.parse(lasio_json_str);
    wellio_obj_2 = wellio.lasio_obj_2_wellio_obj(lasio_obj_2);
} catch (e) {
    console.log('[');
    console.log(e.name + ":: " + e.message);
    console.log(']');
}

console.log(wellio_obj_2);
  1. Have Wellio.js read the LAS file that Lasio wrote. In these examples the file is named 'scratch_v2.las'. The example in the markdown and .ipynb files show how to obtain the current directory. The current directory is needed to find and read the file successfully.
// node
const path = require('path');
let mydir_3 = process.env.PWD;
let myfile_3 = mydir + path.sep + 'scratch_v2.las';

let las_str_3 = '';
let wellio_obj_3 = '';

try {
    las_str_3 = wellio.loadLAS(myfile_3);
    wellio_obj_3 = wellio.las2json(las_str_3);
} catch (e) {
    console.log('[');
    console.log(e.name + ":: " + e.message);
    console.log(']');
}
console.log(wellio_obj_3);

Summary These have been tested on a local jupiter instance. They don't require any changes to Wellio.js to work. So for issue #40, this would resolve the issue just by implementing one of these methods in Jupyter notebooks. Could you check these methods work for you. If they do should we close issue 40 ? (We could merge this branch in-order to have these examples available and documented).

Thank you,

DC

dcslagel commented 4 years ago

Closing this draft and will post a cleanup review/merge request.

DC