i have long lookup table (~40,000 lines) using code. currently, have set grab 4 arrays lookup table in subroutine uses it, call subroutine ~3,000 times. rather not waste processing time grabbing table arrays repeatedly. there way grab them in main program, store them, , source them later in subroutine?
my current code grabs lookup table in 4 separate arrays of 39,760 lines, , calling this:
readcol, 'lookuptable2.txt', f='d,d,d,d',albedo, inertia, nightt, dayt
edit: should note have idl 6.2, if there way in newer version, still appreciate knowing how.
edit 2: current program has function saves 4 arrays , executes main function. can call function arrays argument? way wouldn't have keep creating same array
something like:
pro func(array1, array2, var1, var2, var3) end
there several ways can this.
it looks have 4 columns , 40,000 lines, correct?
then can following. first, assume there no header data in ascii file following commands.
function read_my_file,file_name ;; assume file_name full path , including file name extension fname = file_name[0] ;; 1 find file following ;; fname = file_search([path file],[file name extension]) ;; define number of lines in file nl = file_lines(fname[0]) ;; define empty arrays fill col1 = dblarr(nl[0]) col2 = dblarr(nl[0]) col3 = dblarr(nl[0]) col4 = dblarr(nl[0]) dumb = dblarr(4) ;; open file openr,gunit,fname[0],error=err,/get_lun if (err ne 0) print, -2, !error_state.msg ;; prints error message n=0l, nl[0] - 1l begin ;; read in file data readf,gunit,format='(4d)',dumb ;; fill arrays col1[n] = dumb[0] col2[n] = dumb[1] col3[n] = dumb[2] col4[n] = dumb[3] endfor ;; close file free_lun,gunit ;; define output output = [[col1],[col2],[col3],[col4]] ;; return calling routine return,output end
note work better if provide explicit width format statement, e.g., '(4d15.5)
, means 15 character input 5 decimal places.
this return col1
through col4
user or calling routine [n,4]-element array, e.g., col1 = output[*,0]
. use structure each tag contains 1 of colj
arrays or return them through keywords
.
then can pass these arrays function/program in following way:
pro my_algorithm_wrapper,file_name,results=results ;; data files columns = read_my_file(file_name) ;; pass data [algorithm] function results = my_algorithm(columns[*,0],columns[*,1],columns[*,2],columns[*,3]) ;; return user return end
to call command line (after making sure both routines compiled), following:
idl> my_algorithm_wrapper,file_name,results=results idl> help,results ;; see function my_algorithm.pro returned
the above code should work idl 6.2.
general notes
- try avoid using uppercase letters in idl routine names can cause issues when idl searches routine during call or compilation statement.
- you need name program/function in line
pro
/function
statement @ beginning of file. name must come afterpro
/function
statement. - it wise use explicit formatting statements avoid ambiguities/errors when reading data files.
- you can pass variable type (e.g., scalar integer, array, structure, object, etc.) programs/functions long handled appropriately within program/function.