args = parser.parse_args()

d = cdms2.open(args.ncfile)
try:
    # Remove singleton dimensions (time or level in surface fields)
    ncvar = d.variables[args.ncvarname](squeeze=1)
except KeyError:
    print "\nError: variable %s not in %s" % (args.ncvarname, args.ncfile)
    sys.exit(1)
    

f = umfile.UMFile(args.target, "r+")

# Set new missing value to match the UM missing value 
arr = MV2.array(ncvar[:])
arr.setMissing(f.missval_r)
arr = MV2.filled(arr).astype(f.float)

# Loop over all the fields
replaced = False
for k in range(f.fixhd[FH_LookupSize2]):
    ilookup = f.ilookup[k]
    lbegin = ilookup[LBEGIN] # lbegin is offset from start
    if lbegin == -99:
        break
    if ilookup[ITEM_CODE] == args.varcode:
        print "Replacing field", k, ilookup[ITEM_CODE]
        if not (ilookup[LBROW], ilookup[LBNPT]) == arr.shape:
            print "\nError: array shape mismatch"
            print "UM field shape", (ilookup[LBROW], ilookup[LBNPT])
Exemplo n.º 2
0
parser.add_argument('target', help='UM File to change')

args = parser.parse_args()

d = cdms2.open(args.ncfile)
try:
    # Remove singleton dimensions (time or level in surface fields)
    ncvar = d.variables[args.ncvarname](squeeze=1)
except KeyError:
    print "\nError: variable %s not in %s" % (args.ncvarname, args.ncfile)
    sys.exit(1)

f = umfile.UMFile(args.target, "r+")

# Set new missing value to match the UM missing value
arr = MV2.array(ncvar[:])
arr.setMissing(f.missval_r)
arr = MV2.filled(arr).astype(f.float)

# Loop over all the fields
replaced = False
for k in range(f.fixhd[FH_LookupSize2]):
    ilookup = f.ilookup[k]
    lbegin = ilookup[LBEGIN]  # lbegin is offset from start
    if lbegin == -99:
        break
    if ilookup[ITEM_CODE] == args.varcode:
        print "Replacing field", k, ilookup[ITEM_CODE]
        if not (ilookup[LBROW], ilookup[LBNPT]) == arr.shape:
            print "\nError: array shape mismatch"
            print "UM field shape", (ilookup[LBROW], ilookup[LBNPT])