I am using an external fortran library that returns a large array of data with the typeREAL(kind=8)
. However, I need to write the data out as REAL(kind=4)
.
What is the best way to convert large arrays of data to a different KIND
type?
I am naively just doing this at the moment:
REAL(KIND=8), ALLOCATABLE :: data64(:,:)
REAL(KIND=4), ALLOCATABLE :: data32(:,:)
# allocate arrays and call external function
CALL some_external_function(data64)
# Convert to 32bit using simple assignment
data32 = data64
# Write out 32bit data....
WRITE(UOUT) data32
However, this approach requires me to allocate two large arrays instead of one and do a lot of copying of data - seems a bit inefficient with both respect to memory and processing...
Is there a smarter way?
BTW: The sizes of the matrices are not necessarily huge - they vary but probably in the order of around 1000 x 1000 (but this will be occuring 100s of times in a loop). Not a problem for modern machines, but as one of the comments suggests, I am looking for an elegant solution that is also efficient! Doing the assignement just seems.....lazy and inelagant?