dustem_set_up_fortran.pro 3.08 KB
FUNCTION dustem_set_up_fortran,random_name=random_name,user_number=user_number

;du=dustem_set_up_fortran(/random_name,user_number=user_number)

;The idea is to modify the dustem fortran to make it run independently between users
;Method is:
;1/ modify input file that needs to be
;2/ compile fortran to a executable with a given name
;3/ modify the dustem system variables accordingly

;Please do:
;cp DM_constants.f90 DM_constants_saved.f90
;cp dustem dustem_saved
;before first use of this routine (!!)

dustem_user='dustem_user' ;default name
user_number=''
IF keyword_set(random_name) THEN BEGIN
    user_number=strtrim(long(abs(randomn(seed))*1.e5),2)
	dustem_user='dustem_user'+user_number
ENDIF

tmp_dir='/tmp/'+getenv('USER')+'/'

dustem_dat=tmp_dir+dustem_user+'/'
dustem_res=tmp_dir+dustem_user+'/'

;1/
;read DM_constants.f90
fortran_file_name=!DUSTEM_SOFT_DIR+'/src/'+'DM_constants.f90'
fortran_file_name_modified=!DUSTEM_SOFT_DIR+'/src/'+'DM_constants_modified.f90'

openr,unit,fortran_file_name,/get_lun
line=' '
lines=[' ']
WHILE NOT eof(unit) DO BEGIN
	readf,unit,line
	lines=[lines,line]
ENDWHILE
lines=lines[1:*]
close,unit
free_lun,unit

original_lines=lines     ;This is to be able to recompile the original dustem when over

Nlines=n_elements(lines)
search_what="data_path='"
search_length=strlen(search_what)
FOR i=0L,Nlines-1 DO BEGIN
	pos=strposmulti(lines[i],search_what,count)
	IF count NE 0 THEN BEGIN
		new_line=strmid(lines[i],0,pos[0]+search_length)
		new_line=new_line+dustem_dat
		new_line=new_line+"'"
		print,new_line
		lines[i]=new_line
		;stop
	ENDIF
ENDFOR

search_what="dir_PDR='"
search_length=strlen(search_what)
FOR i=0L,Nlines-1 DO BEGIN
	pos=strposmulti(lines[i],search_what,count)
	IF count NE 0 THEN BEGIN
		new_line=strmid(lines[i],0,pos[0]+search_length)
		new_line=new_line+dustem_dat
		new_line=new_line+"'"
		print,new_line
		lines[i]=new_line
		;stop
	ENDIF
ENDFOR

;stop

;modify DM_constants.f90
openw,unit,fortran_file_name_modified,/get_lun
FOR i=0L,Nlines-1 DO BEGIN
	printf,unit,lines[i]
ENDFOR
close,unit
free_lun,unit

;stop

;move modified file into used file
str='mv '+fortran_file_name_modified+' '+fortran_file_name
message,'Issuing '+str,/cont
spawn,str

;2/ compile dustem
str='rm '+!DUSTEM_SOFT_DIR+'/src/*.o'
message,'Issuing '+str,/cont
spawn,str

str='rm '+!DUSTEM_SOFT_DIR+'/src/*.mod'
message,'Issuing '+str,/cont
spawn,str

str='rm '+!DUSTEM_SOFT_DIR+'/src/dustem'
message,'Issuing '+str,/cont
spawn,str

;3/ compile

;str='make -f '+!DUSTEM_SOFT_DIR+'/src/Makefile'
;message,'Issuing '+str,/cont

;str='cd '+!DUSTEM_SOFT_DIR+'/src'+' & make'
str='make -C '+!DUSTEM_SOFT_DIR+'/src/'+' -f '+'Makefile'
message,'Issuing '+str,/cont
spawn,str

;rename executable
;dustem_f90_name=!DUSTEM_SOFT_DIR+'/src/'+dustem_user
;dustem_f90_name='/tmp/'+dustem_user+'_exec'
dustem_f90_name=tmp_dir+dustem_user+'_exec'
str='mv '+!DUSTEM_SOFT_DIR+'/src/dustem '+dustem_f90_name
message,'Issuing '+str,/cont
spawn,str

;stop

;change dustemwrap system variables
defsysv,'!dustem_f90_exec',dustem_f90_name
defsysv,'!DUSTEM_DAT',dustem_dat
defsysv,'!DUSTEM_RES',dustem_res

;stop

RETURN,dustem_user

END