Skip to content

Commit 4298674

Browse files
committed
dev
1 parent b65e609 commit 4298674

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

cf/data/array/umarray.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,19 @@ def _get_rec(self, f, header_offset):
273273
274274
"""
275275
return Rec.from_file_and_offsets(f, header_offset)
276-
for v in f.vars:
277-
for r in v.recs:
278-
if r.hdr_offset == header_offset:
279-
print (r.__dict__)
280-
return r
276+
277+
# ------------------------------------------------------------
278+
# Leave the following commented code here for debugging
279+
# purposes. If you replace the above line with this commented
280+
# code, then you must aslo set 'parse=True' in the `open`
281+
# method.
282+
# ------------------------------------------------------------
283+
284+
# for v in f.vars:
285+
# for r in v.recs:
286+
# if r.hdr_offset == header_offset:
287+
# return r
288+
281289
def _set_units(self, int_hdr):
282290
"""The units and calendar properties.
283291

cf/read_write/um/umread.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ class UMField:
480480

481481
def __init__(
482482
self,
483+
f,
483484
var,
484485
fmt,
485486
byte_ordering,
@@ -497,6 +498,8 @@ def __init__(
497498
498499
:Parameters:
499500
501+
f: `umfile.File`
502+
500503
var: `umfile.Var`
501504
502505
byte_ordering: `str`
@@ -553,6 +556,7 @@ def __init__(
553556

554557
self.verbose = verbose
555558

559+
self.f = f
556560
self.fmt = fmt
557561
self.height_at_top_of_model = height_at_top_of_model
558562
self.byte_ordering = byte_ordering
@@ -2465,13 +2469,15 @@ def data_type_in_file(self, rec):
24652469
`numpy.dtype`
24662470
24672471
"""
2472+
int_hdr = rec.int_hdr
2473+
24682474
# Find the data type
2469-
if rec.int_hdr.item(lbuser2) == 3:
2475+
if int_hdr.item(lbuser2) == 3:
24702476
# Boolean
24712477
return np.dtype(bool)
2472-
else:
2473-
# Int or float
2474-
return rec.get_type_and_num_words()[0]
2478+
2479+
# Int or float
2480+
return rec.get_type_and_num_words()[0]
24752481

24762482
def printfdr(self, display=False):
24772483
"""Print out the contents of PP field headers.
@@ -3445,6 +3451,7 @@ def read(
34453451

34463452
um = [
34473453
UMField(
3454+
f,
34483455
var,
34493456
f.fmt,
34503457
f.byte_ordering,
@@ -3567,6 +3574,7 @@ def file_open(self, filename):
35673574
byte_ordering=g.get("byte_ordering"),
35683575
word_size=g.get("word_size"),
35693576
fmt=g.get("fmt"),
3577+
parse=True
35703578
)
35713579

35723580

cf/umread_lib/umfile.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,9 @@ def __init__(
289289
if file:
290290
self.file = file
291291

292-
# import cf; pp = cf.umread_lib.umfile.File('/home/david/test2.pp', fmt='PP', byte_ordering= 'little_endian' , word_size=4, parse=False)
293-
# cf.umread_lib.umfile.Rec.from_file_and_offsets(pp, 4, 268, 46640)
294-
295292
@classmethod
296293
def from_file_and_offsets(
297-
cls, file, hdr_offset, data_offset=None, disk_length=None
294+
cls, file, header_offset, data_offset=None, disk_length=None
298295
):
299296
"""Instantiate a `Rec` object from the `File` object and the
300297
header and data offsets.
@@ -308,7 +305,7 @@ def from_file_and_offsets(
308305
A view of a file including sets of PP records combined
309306
into variables.
310307
311-
hdr_offset: `int`
308+
header_offset: `int`
312309
The file start address of the header, in bytes.
313310
314311
data_offset: `int`, optional
@@ -329,41 +326,31 @@ def from_file_and_offsets(
329326
c = file._c_interface
330327
word_size = file.word_size
331328
int_hdr, real_hdr = c.read_header(
332-
file.fd, hdr_offset, file.byte_ordering, word_size
329+
file.fd, header_offset, file.byte_ordering, word_size
333330
)
334331
PP = file.fmt == "PP"
335-
print (c.lib.arse)
332+
336333
if data_offset is None:
337334
# Calculate the data offset from the integer header
338335
if PP:
339336
# We only support 64-word headers, so the data starts
340-
# 66 words after the header, i.e. 64 words of the
341-
# header plus two block control words.
342-
data_offset = hdr_offset + 66 * word_size
337+
# 66 words after the header_offset, i.e. 64 words of the
338+
# header, plus 2 block control words.
339+
data_offset = header_offset + 66 * word_size
343340
else:
344341
# Fields file
345342
data_offset = int_hdr[LBEGIN] * word_size
346343

347344
if disk_length is None:
348345
# Calculate the disk length from the integer header
349-
lbpack = int_hdr[LBPACK]
350-
lbnrec = int_hdr[LBNREC]
351-
lblrec = int_hdr[LBLREC]
352-
353-
if not lbpack:
354-
disk_length = (lblrec - int_hdr[LBEXT]) * word_size
355-
elif lbpack != 0 and lblrec * word_size:
356-
disk_length = lblrec * word_size
357-
elif lbpack % 10 == 2:
358-
lbnpt = int_hdr[LBNPT]
359-
lbrow = int_hdr[LBROW]
360-
if lbnpt > 0 and lbrow > 0:
361-
disk_length = lbnpt * lbrow * 4
362-
else:
363-
disk_length = (lblrec - int_hdr[LBEXT]) * 4
364-
346+
if int_hdr[LBPACK] % 10 == 2:
347+
# Cray 32-bit packing
348+
disk_length = int_hdr[LBLREC] * 4
349+
else:
350+
disk_length = int_hdr[LBLREC] * word_size
351+
365352
return cls(
366-
int_hdr, real_hdr, hdr_offset, data_offset, disk_length, file=file
353+
int_hdr, real_hdr, header_offset, data_offset, disk_length, file=file
367354
)
368355

369356
def read_extra_data(self):

0 commit comments

Comments
 (0)