#!/bin/sh

# $Id: maestro2dx.sh,v 1.5 2007/05/17 18:10:11 marc Exp $
#
# Very simple generator for OpenDX import of maestro sitemaps
#
# Copyright CNRS
# contributor : Marc Baaden
#
# baaden@smplinux.de
# http://www.baaden.ibpc.fr
#
# This software is governed by the CeCILL  license under French law and
# abiding by the rules of distribution of free software.  You can  use,
# modify and/or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and  rights to copy,
# modify and redistribute granted by the license, users are provided
# only with a limited warranty  and the software's author,  the holder
# of the economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading,  using,  modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean  that it is complicated to manipulate,  and  that  also
# therefore means  that it is reserved for developers  and  experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards
# their
# requirements in conditions enabling the security of their systems
# and/or
# data to be ensured and,  more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.


########################################################################
# SYSTEM SETTINGS - adapt to your environment

# Where the visump executable lives
VISDUMP=$SCHRODINGER/utilities/visdump

# Offset for the start of the actual binary data
byte_offset=4096

### you shouldn't need to change anything below
########################################################################
#

if ! test "$1" -a "$2"
then
cat <<_EOF_

NAME
        sh shell script maestro2dx.sh MBR231205a
        \$Id: maestro2dx.sh,v 1.5 2007/05/17 18:10:11 marc Exp $

SYNOPSIS
        maestro2dx <visfile> <dataset name>

        This shell script creates an OpenDX  .general import filter
        for direct import of binary .vis files generated by Maestro
        options like sitemaps.
        The name of the dataset is given as second argument.

AUTHOR
        Marc Baaden <baaden@smplinux.de>.

_EOF_
  exit
fi

# Get initial OpenDX filename
input=$1
setname=$2

# Generate .general file for OpenDX

# Header
echo -e "# OpenDX import filter for Maestro file $input"
echo -e "# generated by maestro2dx.sh v0.1, Marc Baaden <baaden@smplinux.de>"
echo -e "#"

# Filename
echo "file = "$input

# Dimensions
$VISDUMP $input \
| grep -A 4 Dimensions \
| tail -1 \
| sed -e 's/^/grid = /g' \
| sed -e 's/  */ /g' \
| sed -e 's/, / x /g' \

cat <<_EOF_
format = lsb ieee
interleaving = record
majority = row
_EOF_

# Data offset
echo "header = bytes "$byte_offset

# Data set name
echo "field = "$setname

cat <<_EOF_
structure = scalar
type = float
dependency = positions
_EOF_

# Origin
$VISDUMP $input \
| grep -A 4 Origin \
| tail -1 \
| sed -e 's/, /, 1,  /g' \
| sed -e 's/^/positions = regular, regular, regular, /g' \
| sed -e 's/  */ /g' \
| sed -e 's/$/, 1/g' \

# NB: we impose fixed 1 A distance !!! for now

# End of file
cat <<_EOF_

end
_EOF_

exit
