def test_write_field_2d_clean(): """ Testing write 2d field """ import os tfile = "temp.rpn" r = None try: r = RPN(tfile, mode="w") data = np.random.randn(10, 10) data = data.astype(np.float32) r.write_2d_field_clean(data, properties={"name": "RAND"}) r.close() r = RPN(tfile) data1 = r.get_first_record_for_name("RAND") v0, v1 = data.mean(), data1.mean() ok_(abs(v1 - v0) <= 1e-6, "Saved ({0}) and retrieved ({1}) means are not the same.".format(v0, v1)) finally: if r is not None: r.close() os.remove(tfile)
def save_mask_to_rpn(mask_field, in_file="", out_file=""): rin = RPN(in_file) rout = RPN(out_file, mode="w") # Read coordinates and reshape(needed for writing) x = rin.get_first_record_for_name(">>") x.shape = (-1, 1) print(x.shape) y = rin.get_first_record_for_name("^^") y.shape = (1, -1) # get parameters of the last read record coord_info = rin.get_current_info() print(coord_info) # write coordinates coord_info.update({"name": ">>", "label": "NGP", "typ_var": "X", "nbits": -coord_info["nbits"]}) rout.write_2d_field_clean(x, properties=coord_info) coord_info.update({"name": "^^"}) rout.write_2d_field_clean(y, properties=coord_info) # write the mask rout.write_2d_field_clean(mask_field, properties=dict(name="FMSK", label="NGP_MASK", ig=coord_info["ip"] + [0,])) rin.close() rout.close()
def test_copy_records(): """ Test confirming that you cannot in the middle of the get_next_recod cycle take longitudes, and continue with getting next records """ r = RPN(in_path) temp_file = "test1.rpn" r_out = RPN(temp_file, mode="w") n_in = r.get_number_of_records() n_out = 0 data = [] while data is not None: data = r.get_next_record() if data is None: break n_out += 1 info = r.get_current_info() varname = info[RPN.VARNAME_KEY].strip() print(varname) if varname not in [">>", "^^"]: for i in range(10): r_out.write_2d_field_clean(data, properties=dict(name="X{0}".format(i), ip=info["ip"], ig=info["ig"])) r_out.write_2d_field_clean(data, properties=dict(name=varname, ip=info["ip"], ig=info["ig"], grid_type=info[RPN.GRID_TYPE])) ok_(n_in == n_out, "Copied {0} recs, but there are {1} records".format(n_out, n_in)) r.close() r_out.close() r = RPN(temp_file) r.get_next_record() print(r.get_current_info()) r.get_longitudes_and_latitudes_for_the_last_read_rec() data = r.get_next_record() ok_(data is None) # Should be None if there is only one coordinate record in the file r.close() os.remove(temp_file)