Changeset 23689 for issm/trunk-jpl/src/py3/archive/arch.py
- Timestamp:
- 02/04/19 12:50:23 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/py3/archive/arch.py
r23677 r23689 1 1 import numpy as np 2 import math3 2 import struct 4 import sys 5 import os 3 from os import path 6 4 from collections import OrderedDict 7 5 … … 18 16 # open file 19 17 try: 20 if not os.path.isfile(filename):18 if not path.isfile(filename): 21 19 fid=open(filename,'wb') 22 20 else: … … 31 29 name=args[2*i] 32 30 write_field_name(fid,name) 33 31 34 32 # write data associated with field name 35 33 data=args[2*i+1] … … 43 41 else: 44 42 raise ValueError("archwrite : error writing data, invalid code entered '%d'" % code) 45 43 46 44 fid.close() 47 45 … … 55 53 """ 56 54 try: 57 if os.path.isfile(filename):55 if path.isfile(filename): 58 56 fid=open(filename,'rb') 59 57 else: … … 61 59 except IOError as e: 62 60 raise IOError("archread error : could not open file '%s' to read from" % filename) 63 61 64 62 archive_results=[] 65 63 66 64 # read first result 67 65 result=read_field(fid) 66 68 67 while result: 69 68 if fieldname == result['field_name']: … … 71 70 archive_results=result['data']; # we only want the data 72 71 break 73 72 74 73 # read next result 75 74 result=read_field(fid) 76 75 77 76 # close file 78 77 fid.close() 79 78 80 79 return archive_results 81 80 # }}} … … 88 87 """ 89 88 try: 90 if os.path.isfile(filename):89 if path.isfile(filename): 91 90 fid=open(filename,'rb') 92 91 else: … … 94 93 except IOError as e: 95 94 raise IOError("archread error : could not open file '%s' to read from" % filename) 96 95 97 96 print('Source file: ') 98 97 print('\t{0}'.format(filename)) … … 106 105 # go to next result 107 106 result=read_field(fid) 108 107 109 108 # close file 110 109 fid.close() … … 112 111 # }}} 113 112 114 # Helper functions 113 # Helper functions 115 114 def write_field_name(fid,data): # {{{ 116 115 """ … … 121 120 reclen=len(data)+4+4 122 121 fid.write(struct.pack('>i',reclen)) 123 122 124 123 # write format code 125 124 code=format_archive_code(data); … … 130 129 # write string length, and then the string 131 130 fid.write(struct.pack('>i',len(data))) 132 fid.write(struct.pack('> %ds' % len(data),data))131 fid.write(struct.pack('>{}s'.format(len(data)),data.encode('utf8'))) 133 132 # }}} 134 133 def write_scalar(fid,data): # {{{ … … 143 142 # write the format code (2 for scalar) 144 143 fid.write(struct.pack('>i',2)) 145 144 146 145 # write the double 147 146 fid.write(struct.pack('>d',data)) … … 158 157 elif isinstance(data,(list,tuple)): 159 158 data=np.array(data).reshape(-1,) 160 159 161 160 if np.ndim(data) == 1: 162 161 if np.size(data): … … 164 163 else: 165 164 data=data.reshape(0,0) 166 165 167 166 # get size of data 168 167 sz=data.shape … … 175 174 raise ValueError("archwrite error : can not write vector to binary file because it is too large") 176 175 fid.write(struct.pack('>i',reclen)) 177 176 178 177 # write format code 179 178 fid.write(struct.pack('>i',3)) … … 204 203 raise ValueError('archread error : a string was not present at the start of the arch file') 205 204 namelen=struct.unpack('>i',fid.read(struct.calcsize('>i')))[0] 206 fieldname=struct.unpack('> %ds' % namelen,fid.read(namelen))[0]207 205 fieldname=struct.unpack('>{}s'.format(namelen),fid.read(namelen))[0] 206 208 207 # then, read the data 209 208 datalen=struct.unpack('>i',fid.read(struct.calcsize('>i')))[0] … … 211 210 212 211 if data_type==2: 213 # unpack scalar212 # struct.upack scalar 214 213 data=struct.unpack('>d',fid.read(struct.calcsize('>d')))[0] 215 214 elif data_type==3: … … 218 217 raw_data=np.zeros(shape=(rows,cols),dtype=float) 219 218 for i in range(rows): 220 raw_data[i,:]=struct.unpack('> %dd' % cols,fid.read(cols*struct.calcsize('>d')))221 # The matrix will be unpacked in order and will be filled left -> right by column219 raw_data[i,:]=struct.unpack('>{}d'.format(cols),fid.read(cols*struct.calcsize('>d'))) 220 # The matrix will be struct.upacked in order and will be filled left -> right by column 222 221 # We need to reshape and transpose the matrix so it can be read correctly 223 222 data=raw_data.reshape(raw_data.shape[::-1]).T 224 223 else: 225 224 raise TypeError("Cannot read data type %d" % data_type) 226 225 227 226 # give additional data to user 228 227 if data_type==2: … … 234 233 235 234 result=OrderedDict() 236 result['field_name']=fieldname 235 result['field_name']=fieldname.decode('utf8') 237 236 result['size']=data_size 238 237 result['data_type']=data_type_str
Note:
See TracChangeset
for help on using the changeset viewer.