<div dir="ltr">Hello ncl-talkers,<div><br></div><div>I have a little bit of a conundrum with trying to write a binary file for a colleague that is destined to be used in TELEMAC-2D. The binary file that I am trying to write needs to include characters, integers and floats, and will be used on a machine that is Big Endian. I am creating the file on a Linux machine that is Little Endian, hence when I write the binary file I am using "setfileoption("bin","WriteByteOrder","BigEndian")". This all works fine for integers and floats, but the characters are not correctly represented in the output binary.</div><div><br></div><div>I have written a short piece of code that demonstrates the problem succinctly:</div><div><br></div><div>--------------------------------------------------------</div><div><div>begin</div><div><br></div><div>endianness = isbigendian()</div><div>words = "blah_de_blah_de_blah"</div><div>words_char = new(strlen(words),character)</div><div>words_char = tochar(" ")</div><div>words_char(0:strlen(words)-1) = tochar(words)</div><div>outfil = "temp.bin"</div><div><br></div><div>if (.not.endianness) then</div><div>Â print("Endianness is LittleEndian")</div><div><br></div><div>; run and look at results, then uncomment the next two lines and re-run</div><div>; Â setfileoption("bin","ReadByteOrder","BigEndian")</div><div>; Â setfileoption("bin","WriteByteOrder","BigEndian")</div><div><br></div><div>Â if (fileexists(outfil)) then</div><div>Â Â system("rm -f "+outfil)</div><div>Â end if</div><div>Â fbinrecwrite (outfil,0, (/ words_char /))</div><div>Â new_words = fbinrecread(outfil, 0, -1, "character")</div><div>Â print("WORDS (original): $$ "+words+" $$")</div><div>Â print("WORDS (original, char to str): $$ "+tostring(words_char)+" $$")</div><div>Â print("NEW_WORDS (char to str): $$ "+tostring(new_words)+" $$")</div><div>else</div><div>Â print("Already BigEndian")</div><div>end if</div><div><br></div><div>end</div></div><div><div>--------------------------------------------------------</div><div><br></div><div>I have tested this on two completely different Linux machines running NCL 6.3.0 (both 64-bit Ubuntu) and also on one of them using NCL 6.2.1. All give the same result.</div><div><br></div><div>Running the script first time with the two setfileoption lines commented out (i.e. native LittleEndian binary):</div><div><div>(0) Â Â Endianness is LittleEndian</div><div>(0) Â Â WORDS (original): $$ blah_de_blah_de_blah $$</div><div>(0) Â Â WORDS (original, char to str): $$ blah_de_blah_de_blah $$</div><div>(0) Â Â NEW_WORDS (char to str): $$ blah_de_blah_de_blah $$</div></div><div><br></div><div>Running with the two setfileoption lines un-commented:</div><div><div>(0) Â Â Endianness is LittleEndian</div><div>(0) Â Â WORDS (original): $$ blah_de_blah_de_blah $$</div><div>(0) Â Â WORDS (original, char to str): $$ blah_de_blah_de_blah $$</div><div>(0) Â Â NEW_WORDS (char to str): $$ Â $$</div></div><div><br></div><div>As you can see with the BigEndian option the characters are not correctly written to the file.</div><div><br></div><div>Is this correct behaviour? Have I missed something I should have included to get this to work? (This is entirely possible and I apologise for wasting your time if this is a simple user-error.)</div><div><br></div><div>I have searched the ncl-talk archives and have not found this problem encountered by others, so it may well be an issue exclusive to my machines. If it is, I can work around it by wrapping in some fortran to handle the reading and writing of the binary correctly. I would prefer to avoid such a work around as the size of the arrays in the binary files I have to read and write will vary, and I would prefer not to have to hard-code the array sizes when NCL makes calls to the fortran code.<br></div><div><br></div><div><div>[Note: on the machine running NCL 6.2.1 I also get the following warning message on both attempts:</div><div>warning:fbinrecwrite: end of file reached before record number, writing record as last record in file]</div></div><div><br></div><div>Many thanks for your help,</div><div>Kay</div><div></div></div></div>