<commit_before>// Package output contains reaction and visualization data related functionality
//
// Copyright 2015 <NAME>
// Licensed under BSD license, see LICENSE file for details
package output

import (
	"encoding/binary"
	"fmt"
	"io"
	"math"
	"os"
	"path"

	"github.com/haskelladdict/mcellLite/mol"
)

// WriteCB writes the current molecule position to a CellBlender compatible
// input format
// NOTE: This function does a lot of type casting mostly due to the way
// CellBlender format was designed. E.g., while positions are internally stored
// as 64bit doubles, CellBlender format writes them as 32bit floats. Eventually,
// the CellBlender format should probably be overhauled or completele redesigned.
func WriteCB(molMap mol.MolMap, outPath, fileName string, iter int64) error {
	// if output dir does not exist we create it
	if err := os.MkdirAll(outPath, 0700); err != nil {
		return fmt.Errorf("in writeCB: %s", err)
	}

	p := fmt.Sprintf("%s.cellbin.%04d.dat", fileName, iter)
	p = path.Join(outPath, p)
	file, err := os.Create(p)
	if err != nil {
		return fmt.Errorf("in writeCB: %s", err)
	}
	defer file.Close()

	// write version info
	var version uint32 = 1
	if err := writeUint32(file, version); err != nil {
		return err
	}

	// write molecules
	for sp, mols := range molMap {
		// write species info
		if err := writeUint8(file, uint8(len(sp))); err != nil {
			return err
		}
		if n, err := file.Write([]byte(sp)); err != nil || len(sp) != n {
			return err
		}

		// write mol type (0 = volume molecule)
		if err := writeUint8(file, 0); err != nil {
			return err
		}

		// write number of molecules and then molecule info
		if err := writeUint32(file, 3*uint32(len(mols))); err != nil {
			return err
		}
		for _, mol := range mols {
			if err := writeUint32(file, math.Float32bits(float32(mol.R.X))); err != nil {
				return err
			}
			if err := writeUint32(file, math.Float32bits(float32(mol.R.Y))); err != nil {
				return err
			}
			if err := writeUint32(file, math.Float32bits(float32(mol.R.Z))); err != nil {<commit_msg>Switch to buffered IO for CellBlender output.<commit_after>// Package output contains reaction and visualization data related functionality
//
// Copyright 2015 <NAME>
// Licensed under BSD license, see LICENSE file for details
package output

import (
	"bufio"
	"encoding/binary"
	"fmt"
	"io"
	"math"
	"os"
	"path"

	"github.com/haskelladdict/mcellLite/mol"
)

// WriteCB writes the current molecule position to a CellBlender compatible
// input format
// NOTE: This function does a lot of type casting mostly due to the way
// CellBlender format was designed. E.g., while positions are internally stored
// as 64bit doubles, CellBlender format writes them as 32bit floats. Eventually,
// the CellBlender format should probably be overhauled or completele redesigned.
func WriteCB(molMap mol.MolMap, outPath, fileName string, iter int64) error {
	// if output dir does not exist we create it
	if err := os.MkdirAll(outPath, 0700); err != nil {
		return fmt.Errorf("in writeCB: %s", err)
	}

	p := fmt.Sprintf("%s.cellbin.%04d.dat", fileName, iter)
	p = path.Join(outPath, p)
	file, err := os.Create(p)
	if err != nil {
		return fmt.Errorf("in writeCB: %s", err)
	}
	defer file.Close()

	w := bufio.NewWriter(file)

	// write version info
	var version uint32 = 1
	if err := writeUint32(w, version); err != nil {
		return err
	}

	// write molecules
	for sp, mols := range molMap {
		// write species info
		if err := writeUint8(w, uint8(len(sp))); err != nil {
			return err
		}
		if n, err := w.Write([]byte(sp)); err != nil || len(sp) != n {
			return err
		}

		// write mol type (0 = volume molecule)
		if err := writeUint8(w, 0); err != nil {
			return err
		}

		// write number of molecules and then molecule info
		if err := writeUint32(w, 3*uint32(len(mols))); err != nil {
			return err
		}
		for _, mol := range mols {
			if err := writeUint32(w, math.Float32bits(float32(mol.R.X))); err != nil {
				return err
			}
			if err := writeUint32(w, math.Float32bits(float32(mol.R.Y))); err != nil {
				return err
			}
			if err := writeUint32(w, math.Float32bits(float32(mol.R.Z))); err != nil {
{'fraction_non_alphanumeric': 0.12980769230769232, 'fraction_numerical': 0.023557692307692307, 'mean_word_length': 3.426595744680851, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5239344', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1364, 'n_words': 483}