Skip to content

Instantly share code, notes, and snippets.

@raphaeldussin
Last active January 10, 2019 15:18
Show Gist options
  • Select an option

  • Save raphaeldussin/a45dc4d074e120d9f30887ead3e46a7a to your computer and use it in GitHub Desktop.

Select an option

Save raphaeldussin/a45dc4d074e120d9f30887ead3e46a7a to your computer and use it in GitHub Desktop.
playing with MITgcm input files
#!/usr/bin/env python
import numpy as np
import sys
# playing with input files from tutorial_global_oce_latlon
filein = 'lev_t.bin'
# MITgcm likes its binary in big endian, float ('>f') or double ('>d')
raw = np.fromfile(filein, dtype='>f')
# Temp has 12 time records, 15 vertical levels, 40 in lat, 90 in lon
temp = np.reshape(raw, (12,15,40,90))
# for lev_sst.bin and bathymetry.bin, I would do :
#sst = np.reshape(raw, (12,40,90))
#bathy = np.reshape(data, (40,90))
# the numpy.tofile don't let you have control on the
# precision (float/double) so I don't recommend.
# intstead use:
def write_to_binary(data, fileout, precision='single'):
''' write variable from np.array to fileout with precision '''
# write data to binary files
fid = open(fileout, "wb")
flatdata = data.flatten()
if precision == 'single':
if sys.byteorder == 'little':
tmp = flatdata.astype(np.dtype('f')).byteswap(True).tobytes()
else:
tmp = flatdata.astype(np.dtype('f')).tobytes()
elif precision == 'double':
if sys.byteorder == 'little':
tmp = flatdata.astype(np.dtype('d')).byteswap(True).tobytes()
else:
tmp = flatdata.astype(np.dtype('d')).tobytes()
fid.write(tmp)
fid.close()
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment