function getRunInfo, inputfile openr, un, /get, inputfile line='' lines=strarr(8) for i=0,7 do begin if not eof(un) then begin readf, un, line lines[i]=line endif else print, "----- Missing values in input file! -----" endfor close, un free_lun, un return, {dir:lines[0], modelInputFile:lines[1], $ obsFile:lines[2], parFile:lines[3], $ MoscemInputFile:lines[4], configFile:lines[5], $ dataFile:lines[6], command:lines[7]} end pro writeMoscemInput, data, file, info openw, oun, /get, file printf,oun, 'nOptPar 4' printf,oun, 'nOptObj 1' printf,oun, 'nSamples 200' printf,oun, 'nComplex 5' printf,oun, 'nMaxDraw 50000' printf,oun, 'Par_Control_File '+info.dir+'/'+info.parFile printf,oun, 'Obj_Flag_File '+info.dir+'/shpfit/objoptflag.in' printf,oun, 'Input_File '+info.dir+'/'+info.modelinputFile printf,oun, 'Val_File '+info.dir+'/'+info.obsFile printf,oun, 'Obj_Out_File output/shpfit/obj_shpfit_'+ $ strcompress(data[0], /remove_all)+'.out' printf,oun, 'Par_Out_File output/shpfit/par_shpfit_'+ $ strcompress(data[0], /remove_all)+'.out' printf,oun, 'Cvg_Out_File output/shpfit/cvg_shpfit_'+ $ strcompress(data[0], /remove_all)+'.out' close, oun free_lun, oun end pro writeObsFile, record, name openw, oun, /get, name printf, oun, "SoilSample SMC" printf, oun, record[[0,2],*] close, oun free_lun, oun end pro writeModelInput, record, name openw, oun, /get, name printf, oun, "SoilSample head" printf, oun, record[[0,1],*] close, oun free_lun, oun end pro writeparVG, record, file openw, oun, file, /get Ts=[max(record[2,*]),max(record[2,*])-0.02,(max(record[2,*])+0.2)<1.0] Tr=[min(record[2,*]),(min(record[2,*])-0.2)>0,(min(record[2,*])+0.02)>0] printf, oun, 'No Name Default Lower Upper OptIdx Description' printf, oun, '1 n 2.0 1.001 40 1 van Genuchten n' printf, oun, '2 alpha 0.15 0.001 5.0 1 van Genuchten alpha' printf, oun, '3 Ks 1 0.001 100 0 Sat. Conductivity' printf, oun, '4 Ts '+string(Ts[0])+string(Ts[1])+string(Ts[2])+' 1 Saturated Moisture Content' printf, oun, '5 Tr '+string(Tr[0])+string(Tr[1])+string(Tr[2])+' 1 Residual Moisture Content' close, oun free_lun, oun end pro writeparCambell, record, file openw, oun, file, /get Ts=[max(record[2,*]),max(record[2,*])-0.02,(max(record[2,*])+0.2)<1.0] Tr=[min(record[2,*]),(min(record[2,*])-0.2)>0,(min(record[2,*])+0.02)>0] printf, oun, 'No Name Default Lower Upper OptIdx Description' printf, oun, '1 b 4.0 0.001 40 1 Cambell b' printf, oun, '2 psis 0.15 1 100 1 Cambell psis' printf, oun, '3 Ks 1 0.001 100 0 Sat. Conductivity' printf, oun, '4 Ts '+string(Ts[0])+string(Ts[1])+string(Ts[2])+' 1 Saturated Moisture Content' printf, oun, '5 Tr '+string(Tr[0])+string(Tr[1])+string(Tr[2])+' 1 Residual Moisture Content' close, oun free_lun, oun end PRO writeConfigIn, n, file, vg=vg openw, oun, file, /get printf, oun, '# MOSCEM configration automatically generated by runMoscemSHP.pro' printf, oun, '' printf, oun, '#' printf, oun, '# parameter settings' printf, oun, '#' printf, oun, 'MOD_VER = C' IF keyword_set(vg) THEN printf, oun, 'MOD_DIR = vgshpfit' $ ELSE printf, oun, 'MOD_DIR = cambellshpfit' printf, oun, 'MOD_ALGO = SINGL' printf, oun, 'IDUM = -103' printf, oun, 'NPAR = 5' printf, oun, 'NINPUT = 1' printf, oun, 'NFLUX = 1' printf, oun, 'NTSTEP1 = '+strcompress(n, /remove_all) printf, oun, 'NTSTEP2 = '+strcompress(n, /remove_all) printf, oun, 'HAS_MISSING_VALUE = NO' printf, oun, 'MISSING_VALUE = ' printf, oun, 'OBJ_FUNC = RMSE' printf, oun, 'LAMBDA = ' close, oun free_lun, oun enD pro writeDataFiles, record, info, vg=vg, cambell=cambell writeModelInput, record, info.dir+'/'+info.modelInputFile writeObsFile, record, info.dir+'/'+info.ObsFile IF keyword_set(vg) THEN writeparVG, record, info.dir+'/'+info.parFile IF keyword_set(cambell) THEN writeparCambell, record, info.dir+'/'+info.parFile writeMoscemInput, record, info.dir+'/'+info.MoscemInputFile, info writeConfigIn, n_elements(record[0,*]), info.configFile, vg=vg end ;; read the theta-conductivity relationship function getRecord, un if eof(un) then return, -1 line="" ;; read initial data line readf, un, line curData=float(strsplit(line, ',',/extract)) if n_elements(curData) eq 1 then return, -1 ;no data for this record recordNumber=curData[0] ;; this is the record number we want to look for Data=curData ;; initialize data ;; loop until we reach the next record while curData[0] eq recordNumber $ and not eof(un) $ and n_elements(curData) gt 1 do begin ;; store the current data Data=[[Data], [curData]] ;;store our current pointer into the file incase this is the next record point_lun, -1*un, pos readf, un, line curData=float(strsplit(line,',', /extract)) ENDWHILE IF n_elements(data[0,*]) LT 4 THEN return, -1 data=data[*,1:*] ;; point back to where we were before we hit the wrong record if not eof(un) then point_lun, un, pos return, Data end pro runMoscemSHP, inputfile, vg=vg, cambell=cambell IF NOT keyword_set(cambell) THEN vg=1 ;default to van Genuchten info=getRunInfo(inputfile) openr, un, /get, info.dataFile while not eof(un) do begin rec=getRecord(un) if rec[0] ne -1 then BEGIN print, rec[0] writeDataFiles, rec, info, vg=vg, cambell=cambell spawn, "make clean" print, 'compiling' spawn, "make ../../run_moscem >&/dev/null" print, 'running...' spawn, "run_moscem >>outputfile" endif endwhile close, un free_lun, un end