Skip to content

Instantly share code, notes, and snippets.

@gridley
Created September 10, 2018 13:10
Show Gist options
  • Save gridley/6d8c107b83e7c71f60a660bfb1407b42 to your computer and use it in GitHub Desktop.
Save gridley/6d8c107b83e7c71f60a660bfb1407b42 to your computer and use it in GitHub Desktop.
print numpy array as rounded fortran array
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