1; -----------------------------------------------------------------------------
2;
3;  Copyright (C) 1997-2013  Krzysztof M. Gorski, Eric Hivon, Anthony J. Banday
4;
5;
6;
7;
8;
9;  This file is part of HEALPix.
10;
11;  HEALPix is free software; you can redistribute it and/or modify
12;  it under the terms of the GNU General Public License as published by
13;  the Free Software Foundation; either version 2 of the License, or
14;  (at your option) any later version.
15;
16;  HEALPix is distributed in the hope that it will be useful,
17;  but WITHOUT ANY WARRANTY; without even the implied warranty of
18;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19;  GNU General Public License for more details.
20;
21;  You should have received a copy of the GNU General Public License
22;  along with HEALPix; if not, write to the Free Software
23;  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24;
25;  For more information about HEALPix see http://healpix.sourceforge.net
26;
27; -----------------------------------------------------------------------------
28pro hpx_xface_generic, fullpath, param_file, usrpath, init=init, run=run, silent=silent, clean=clean, cxx=cxx, tmpdir=tmpdir
29;+
30; does general operations for interface to Healpix f90 codes:
31; - initialize path and temporary directory (when init is set to a structure)
32; - run the code              (when run is set)
33; - clean up temporary files (when clean is set)
34;
35; June 2007
36; Feb 2008: increase stack size on Unix
37; 2008-02-24: use File_Delete instead of spawn,'\rm', and path_sep
38; 2008-02-28: deal with init.options
39; 2008-05-30: send to /dev/null error message generated by ulimit
40; 2008-07-04: set stack size to hard limit
41; 2009-04-30: deal with tmpdir, using IDL_TMPDIR as default
42; 2009-09-09: uses !healpix.path.* sub-structure
43; 2013-05-02: do not use /SH keyword with SPAWN under Windows
44; 2015-04-17: add /allow_nonexistent to file_delete
45;-
46
47do_init = keyword_set(init)
48do_run  = keyword_set(run)
49do_clean  = keyword_set(clean)
50do_cxx = keyword_set(cxx)
51
52
53
54common hpx_xface_com, tmp_head, to_remove, uid
55
56if (do_init) then begin
57; define path to executable
58    defsysv, '!healpix', EXISTS = healpix_done
59    if (~ healpix_done) then init_healpix
60
61;    slash = (strupcase(!version.os_family) eq 'WINDOWS') ? '\' : '/'
62    slash = path_sep()
63    if (do_cxx) then begin
64;         default_path = !healpix.directory+slash+'src'+slash+'cxx'+slash+'$HEALPIX_TARGET'+slash+'bin'+slash+init.exe_cxx
65;         if (~file_test(default_path)) then default_path = !healpix.directory+slash+'src'+slash+'cxx'+slash+'generic_gcc'+slash+'bin'+slash+init.exe_cxx
66        default_path = !healpix.path.bin.cxx+slash+init.exe_cxx
67    endif else begin
68;         default_path = '$HEXE'+slash+init.exe
69;         if (~file_test(default_path)) then default_path =
70;         !healpix.directory+slash+'bin'+slash+init.exe
71        default_path = !healpix.path.bin.f90+slash+init.exe
72    endelse
73    fullpath = defined(usrpath) ? strtrim(usrpath,2) : default_path
74    case 0 of
75        strpos(fullpath,'~')*strpos(fullpath,slash)*strpos(fullpath,'$'): fullpath = expand_path(fullpath)
76        strpos(fullpath,'./') : fullpath = fullpath
77        else: fullpath = !healpix.directory+slash+fullpath
78    endcase
79    if (~file_test(fullpath,/noexpand)) then begin
80        message,'Executable '+fullpath+' not found!'
81    endif
82    junk = where(tag_names(init) eq 'DOUBLE', nd)
83    if (nd gt 0 && init.double eq 1 && ~do_cxx)       then fullpath = fullpath + ' --double '
84    junk = where(tag_names(init) eq 'OPTIONS', no)
85    if (no gt 0 && size(/tname,init.options) eq 'STRING')  then fullpath = fullpath + ' '+init.options+ ' '
86
87; define temporary directory
88;    tmp_dir = defined(tmpdir) ? add_prefix(tmpdir,slash,/suff) :    slash+'tmp'+slash
89    tmp_dir = defined(tmpdir) ? tmpdir : getenv('IDL_TMPDIR')  ; 2009-04-30
90    tmp_dir = add_prefix(tmp_dir,slash,/suff) ; make sure there is a trailing /
91    stime = string(long(systime(1)*100 mod 1.e8), form='(i8.8)')
92    tmp_head = tmp_dir+init.routine+'_'+stime+'_'
93
94; define parameter file
95    param_file = tmp_head+'param.txt'
96    to_remove = param_file
97endif
98;------------------
99if (do_run) then begin
100    ; increase stack size to its hard limit on Unix systems
101    push_stack_up = (strupcase(!version.os_family) eq 'UNIX') ? 'ulimit -s `ulimit -s -H` > /dev/null 2>&1 ; ' : ''
102    command = push_stack_up + fullpath+' '+param_file
103    if (keyword_set(silent)) then begin
104        tmp_log_file = tmp_head+'log.txt'
105        command = command + ' > ' + tmp_log_file
106        to_remove = [to_remove, tmp_log_file]
107    endif else begin
108        print,command
109;;;;        spawn,'cat '+param_file
110    endelse
111    if (strupcase(!version.os_family) eq 'WINDOWS') then begin
112        spawn,command
113    endif else begin
114        spawn,/sh,command
115    endelse
116endif
117
118if (do_clean) then begin
119;     for i=0,n_elements(to_remove)-1 do spawn,/sh,'\rm '+to_remove[i]
120    file_delete,to_remove, /allow_nonexistent
121endif
122
123
124return
125end
126