1#!/bin/tcsh -f
2
3#
4# Change log:
5#
6# [PT: Oct 15, 2018]
7# + Use 'afni -get_processed_env' to see if AFNI env variables are
8#   set, for broader generality/utility.
9# + Also, allow NIFTI files to be found in more places.  At the
10#   moment, they can only be found in the `which afni` directory.
11#   That is now modernized.
12#
13# [PT: Nov 25, 2018]
14# + Bug fix: dsets weren't being found in places specified by env vars
15#   like AFNI_GLOBAL_SESSION, etc.
16# [DRG: 13 Aug 2019]
17# + Added AFNI_SUPP_ATLAS_DIR to the list of environment paths.
18# + Added atlas name -> dataset file search via whereami
19# + append file name to output
20# [DRG: 31 Aug 2019]
21# + full_path option - include full path in output instead of .
22# + better option processing of argv
23# [DRG: 24 Sep 2019]
24# + full_path - really full path even for directories like ../
25
26set retval = 0
27# default is to print only path and not file name
28set append_file = 0
29# if in current directory, default is to print ./
30set full_path = 0
31
32set ref_in = ""
33
34if ($#argv == 0) then
35   # bad usage sets error status        29 Dec 2015 [rickr]
36   set retval = 1
37   goto HELP
38endif
39
40# process user options
41set ac = 1
42while ($ac <= $#argv)
43    if ("$argv[$ac]" == "-help" || "$argv[$ac]" == "-h") then
44        goto HELP
45    else if ( "$argv[$ac]" == "-append_file") then
46         set append_file = 1
47    else if ( "$argv[$ac]" == "-full_path") then
48         set full_path = 1
49    else
50         set ref_in = $argv[$ac]
51    endif
52    @ ac ++
53end
54
55if ($ref_in == "") then
56   echo "No datasets on command line?"
57   exit 1
58endif
59
60set dataset_path = './'
61
62# check if this dataset might be an atlas name  13 Aug 2019 [drg]
63# see if the filename ends with .nii, .nii.gz, +orig, +tlrc, .HEAD,.BRIK,...
64set storageguess = ` ParseName ${ref_in} | grep StorageModeNm|awk -F: '{print $2}'`
65# if not a regular dataset, then we might find it as an atlas instead
66if ($storageguess  == "UNDEFINED") then
67   # find the dataset file associated with the atlas (cf. whereami help)
68   set possibleatlas = `whereami -atlas ${ref_in} -show_atlas_dset`
69   if ($possibleatlas != "") then
70      set ref_in = $possibleatlas
71   endif
72endif
73# get the file name now
74set ref_file = ` ParseName ${ref_in} | grep "FileName "|awk -F: '{print $2}'`
75
76# ------------------- search first with possible path -------------
77
78# if path to dset, test separately    31 Jul 2015 [rickr]
79# (differs from ./$ref_in if path is absolute)
80# [PT: Oct 15, 3018] Update to allow for finding NIFTIs in this way
81set dir = "$ref_in:h"
82if ( "$dir" != "$ref_in" ) then
83    set result = `@CheckForAfniDset ${ref_in}`
84    if ($full_path == 1) then
85       if ($dir == ".") then
86          set dir = `pwd`
87       else
88          cd $dir ; set dir = `pwd` ; cd -
89       endif
90    endif
91    if ( "$result" == 2 || "$result" == 3 ) then
92        if ($append_file == 0) then
93           echo $dir
94        else
95           echo ${dir}/${ref_file}
96        endif
97        exit 0
98    endif
99endif
100
101# ------------------- search using `afni -get_processed_env` -------------
102
103# Make a list of the possible variables
104set list_of_vars = ( "AFNI_GLOBAL_SESSION" \
105                     "AFNI_SUPP_ATLAS_DIR" \
106                     "AFNI_ATLAS_PATH"     \
107                     "AFNI_PLUGINPATH" )
108
109# Loop through each of those to see if AFNI knows about any (and which)
110foreach vv ( $list_of_vars )
111
112    set aaa = `afni -get_processed_env  | grep "${vv}"`
113
114    if ( "$aaa" != "" ) then
115
116        # remove first occurence of the variable in the string (which
117        # should be the LHS of assignment
118        set bbb = `echo $aaa | sed -E "s/${vv}//"`
119
120        # then remove the equals sign
121        set ccc = `echo $bbb | sed -E "s/=//"`
122        # then remove any colon separating things, and throw in all
123        # vals to the dataset array
124        set dataset_path = ( ${dataset_path} \
125                             `echo $ccc | tr ':' ' '`)
126         #echo $dataset_path
127    endif
128end
129
130# ----------------------- search atlas dir --------------------------
131
132# As before, check about the atlas dir
133if ( -d $HOME/.afni/atlases ) then
134    set dataset_path = (${dataset_path} $HOME/.afni/atlases)
135endif
136
137# ----------------------- check the $dataset_path --------------------------
138
139# [PT: Oct 15, 2018]
140# + Updating this to be fine with finding NIFTI data sets via the
141#   above (using the '... == 3')!
142foreach dir ( ${dataset_path} )
143    set result = `@CheckForAfniDset ${dir}/${ref_in}`
144    if ( "$result" == 2 || "$result" == 3 ) then
145
146        if ($full_path == 1) then
147            if ($dir == "./") then
148               set dir = `pwd`
149            else
150               cd $dir ; set dir = `pwd` ; cd -
151            endif
152        endif
153        # remove last character if "/"
154        set out = `echo $dir | sed 's/\/$//'`
155        if ($append_file == 0) then
156           echo $out
157        else
158           echo ${out}/${ref_file}
159        endif
160
161        exit 0
162    endif
163end
164
165# ------------------- search (final): `which afni` ------------------------
166
167# Check afni bin directory, for compatibility with older installations
168# that installed atlas datasets there.
169### This can *already* find NIFTI sets
170set wa = `which afni`
171if ( $status != 0) then
172   exit 1
173endif
174set ref_path = "$wa:h"
175if ( "$ref_path" == "$wa" ) then
176   exit 1
177endif
178if ( `@CheckForAfniDset ${ref_path}/${ref_in}` ) then
179    if ($append_file == 0) then
180       echo $ref_path
181    else
182       echo ${ref_path}/${ref_file}
183    endif
184    exit 0
185endif
186
187# not found
188exit 1
189
190
191HELP:
192cat << EOF
193
194Usage: `basename $0` <dsetname>
195
196Search AFNI_GLOBAL_SESSION, AFNI_SUPP_ATLAS_DIR, AFNI_ATLAS_PATH, 
197AFNI_PLUGINPATH and afni bin directory  (in that order) for named 
198dataset. If found, echo the first valid path discovered and return 
199zero status. If not found, return non-zero status. If atlas name is
200given, then atlas dataset file name is found (and possibly printed with
201append_file option below
202
203   -append_file show the file appended too (even with atlas name)
204   -full_path print full path instead of '.'   
205   -help to get this message
206
207+ [Oct 15, 2018] Updated to do a better job searching for NIFTIs and
208    to possibly use the environment variables set in ~/.afnirc.
209
210Jason W. Bacon
211Medical College of Wisconsin
212Sep 27, 2006
213
214EOF
215
216# be explicit
217exit $retval
218
219