Created
September 10, 2018 13:10
-
-
Save gridley/6d8c107b83e7c71f60a660bfb1407b42 to your computer and use it in GitHub Desktop.
print numpy array as rounded fortran array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mag(x): | |
return int(np.log10(np.abs(x))) | |
def fortranPrint(array, unc) | |
""" | |
Prints a numpy array in format for F90 hard-coding. | |
array - a numpy array | |
unc - fractional uncertainty in entries, for rounding. | |
""" | |
shap = np.shape(array) | |
for i in range(shap[0]): | |
arr = [] | |
fmts = [] | |
for j in range(shap[1]): | |
arr.append(array[i,j]) | |
fmts.append('%.{}e, '.format(mag(array[i,j])-mag(unc*array[i,j]))) | |
print('arr(%i,:) = [' % (i) + ''.join([fmts[jj] % arr[jj] for jj in range(len(arr)-1)]) + (fmts[-1][:-2]+']') %arr[-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment