# Copyright 2024 SPC-EPFL
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------
# tested without any .login nor .cshrc

setenv PATH '.:'$HOME'/public/bin:'$PATH
module load itmenv
source $ITMSCRIPTDIR/ITMv2 test

# next commands reads shot=10, run=99 occurence=0 with cocos_in=13 and creates files in /tmp/user with "euitm10_99"
database_to_eqdsk euitm10_99 10 99 13 11

ls /tmp/g2osaute/*euitm10_99*
 /tmp/g2osaute/EQDSK_COCOS_02_POS.OUT.euitm10_99  /tmp/g2osaute/EXPEQ.OUT.euitm10_99      /tmp/g2osaute/log_database_to_eqdsk.euitm10_99
 /tmp/g2osaute/EQDSK_COCOS_11.OUT.euitm10_99      /tmp/g2osaute/EXPEQ.OUT.TOR.euitm10_99  /tmp/g2osaute/NOUT.euitm10_99
 /tmp/g2osaute/EXPEQ_KEPLER.IN.euitm10_99         /tmp/g2osaute/EXPTNZ.OUT.euitm10_99     /tmp/g2osaute/o.euitm10_99

The file /tmp/g2osaute/EQDSK_COCOS_11.OUT.euitm10_99 has cocos=11 and is the eqdsk file we can use as input to run chease within imas and save on the imas database (thus as ids)

module unload itmenv
module load imasenv
imasdb test

cd /tmp/g2osaute
eqdsk_to_database EQDSK_COCOS_11.OUT.euitm10_99 10 99 11 11

The ids files should appear in $MDSPLUS_TREE_BASE_0

-----------------------------------------------------------------------

The file /tmp/g2osaute/o.euitm10_99 contains the CHEASE output with many information and many profiles. You can do:

cd /tmp/g2osaute
o.chease_to_cols o.euitm10_99 o.euitm10_99.cols

This will create the profiles in columns that are easy to read from any programme and get j_phi, <j.B>/B0 or other profiles.
There is a tool in matlab:
cat >tmp_plotcols.m<<EOF
addpath ~g2osaute/public/chease/matlab/CHEASEgui
oo=plotdatafile('o.euitm10_99.cols',1);
EOF

matlab  -nodesktop -r "tmp_plotcols"

(then you can choose what to plot versus what and it gives you the column number for the respective variable. s-mesh is rhopolnorm)
