Exemplo n.º 1
0
def regrid_ancil(infile, outfile, new_nlon, new_nlat):
    fh = open(infile, 'rb')
    fixhdr = read_fixed_header(fh)
    pp_hdrs = read_pp_headers(fh, fixhdr)
    intc = read_integer_constants(fh, fixhdr)
    realc = read_real_constants(fh, fixhdr)
    # see if there are level constants
    if (fixhdr[109] > 0):
        levc = read_level_constants(fh, fixhdr)
    else:
        levc = numpy.zeros([0], 'f')
    # read in the data
    data = read_data(fh, fixhdr, intc, pp_hdrs)
    # reform the data to (number_of_variables, n_time, n_levels, lat, lon)
    if (intc[14] > 0):
        n_vars = intc[14]
    else:
        n_vars = 1
    data = data.reshape(n_vars, intc[2], intc[7], intc[6], intc[5])
    rg_data = regrid_data(data, intc, realc, new_nlon, new_nlat)
    fix_real_constants(realc, intc, new_nlon, new_nlat)
    fix_integer_constants(intc, new_nlon, new_nlat)
    fix_field_headers(pp_hdrs, fixhdr, intc, realc)
    write_ancil(outfile, fixhdr, intc, realc, pp_hdrs, rg_data, levc)
    fh.close()
Exemplo n.º 2
0
def regrid_ancil(infile, outfile, new_nlon, new_nlat):
    fh = open(infile, 'rb')
    fixhdr = read_fixed_header(fh)
    pp_hdrs = read_pp_headers(fh, fixhdr)
    intc = read_integer_constants(fh, fixhdr)
    realc = read_real_constants(fh, fixhdr)
    # see if there are level constants
    if (fixhdr[109] > 0):
        levc = read_level_constants(fh, fixhdr)
    else:
        levc = numpy.zeros([0], 'f')
    # read in the data
    data = read_data(fh, fixhdr, intc, pp_hdrs)
    # reform the data to (number_of_variables, n_time, n_levels, lat, lon)
    if (intc[14] > 0):
        n_vars = intc[14]
    else:
        n_vars = 1
    data = data.reshape(n_vars, intc[2], intc[7], intc[6], intc[5])
    rg_data = regrid_data(data, intc, realc, new_nlon, new_nlat)
    fix_real_constants(realc, intc, new_nlon, new_nlat)
    fix_integer_constants(intc, new_nlon, new_nlat)
    fix_field_headers(pp_hdrs, fixhdr, intc, realc)
    write_ancil(outfile, fixhdr, intc, realc, pp_hdrs, rg_data, levc) 
    fh.close()
def create_so2dms_file(fnames, si, out_fname):
    dms_ac_data, dms_ac_pp_hdrs = get_dms_annual_cycle()
    so2_data, so2_pp_hdrs, so2_fix_hdr, so2_intc, so2_realc = get_so2(fnames, si)
    so2_data, so2_pp_hdrs = remove_ammonia(so2_data, so2_pp_hdrs)
    intlv_data, intlv_hdrs = interleave_data_and_headers(dms_ac_data, dms_ac_pp_hdrs, so2_data, so2_pp_hdrs)
    intlv_hdrs = fix_dates_in_headers(intlv_hdrs)
    # fix the fixed header - start date
    fix_hdr = fix_fixed_header(so2_fix_hdr, intlv_hdrs)
    # fix the integer constants - number of variables and fields etc
    intc = fix_integer_constants(so2_intc, intlv_hdrs)
    # fix the field header offsets - i.e. where the data starts
    intlv_hdrs = fix_field_header_offsets(intlv_hdrs, fix_hdr, intc)
    # write out the file
    write_ancil(out_fname, fix_hdr, intc, so2_realc, intlv_hdrs, intlv_data)
Exemplo n.º 4
0
def concat_files(inputs, output):
    # load the input files one by one and append the pp headers and the
    # data to lists
    data_list = []
    pp_hdrs_list = []
    sum_tsteps = 0
    for file in inputs:
        fh = open(file, "rb")
        fixhdr = read_fixed_header(fh)
        pp_hdrs = read_pp_headers(fh, fixhdr)
        intc = read_integer_constants(fh, fixhdr)
        realc = read_real_constants(fh, fixhdr)
        data = read_data(fh, fixhdr, intc, pp_hdrs)
        data_list.append(numpy.array(data, 'f'))
        pp_hdrs_list.append(pp_hdrs)
        # keep a record of how many timesteps so far
        sum_tsteps += intc[2]
        fh.close()
        
    # convert lists to numpy arrays and flatten
    pp_hdrs_all = numpy.array(pp_hdrs_list[0], 'i4')
    for pp in pp_hdrs_list[1:]:
        pp_hdrs_all = numpy.append(pp_hdrs_all, pp, axis=0)
    
    data_all = numpy.array(data_list[0], 'f')
    for d in data_list[1:]:
        data_all = numpy.append(data_all, d, axis=0)
        
    # read the first fixed header, intc and realc again
    fh = open(inputs[0], "rb")
    fixhdr = read_fixed_header(fh)
    intc = read_integer_constants(fh, fixhdr)
    realc = read_real_constants(fh, fixhdr)
    # see if there are level constants
    if (fixhdr[109] > 0):
        levc = read_level_constants(fh, fixhdr)
    else:
        levc = numpy.zeros([0], 'f')
    fh.close()
    
    # fix the fixed header
    fix_fixed_header(fixhdr, intc, pp_hdrs_all, sum_tsteps)
    # fix the integer constants
    intc[2] = sum_tsteps
    # fix the offsets in the pp header
    fix_field_header_offsets(pp_hdrs_all, fixhdr, intc)
    fix_field_header_dates(pp_hdrs_all, fixhdr, intc)
    fix_field_header_processing(pp_hdrs_all, fixhdr, intc)
    # write out the ancil
    write_ancil(output, fixhdr, intc, realc, pp_hdrs_all, data_all, levc)
def create_so2dms_file(fnames, si, out_fname):
    dms_ac_data, dms_ac_pp_hdrs = get_dms_annual_cycle()
    so2_data, so2_pp_hdrs, so2_fix_hdr, so2_intc, so2_realc = get_so2(
        fnames, si)
    so2_data, so2_pp_hdrs = remove_ammonia(so2_data, so2_pp_hdrs)
    intlv_data, intlv_hdrs = interleave_data_and_headers(
        dms_ac_data, dms_ac_pp_hdrs, so2_data, so2_pp_hdrs)
    intlv_hdrs = fix_dates_in_headers(intlv_hdrs)
    # fix the fixed header - start date
    fix_hdr = fix_fixed_header(so2_fix_hdr, intlv_hdrs)
    # fix the integer constants - number of variables and fields etc
    intc = fix_integer_constants(so2_intc, intlv_hdrs)
    # fix the field header offsets - i.e. where the data starts
    intlv_hdrs = fix_field_header_offsets(intlv_hdrs, fix_hdr, intc)
    # write out the file
    write_ancil(out_fname, fix_hdr, intc, so2_realc, intlv_hdrs, intlv_data)
Exemplo n.º 6
0
def subset_ancil(infile, outfile, year, month, n_months):
    # subset an ancil based on the start year, start month and number of
    # months
    fh = open(infile, 'rb')
    fixhdr = read_fixed_header(fh)
    pp_hdrs = read_pp_headers(fh, fixhdr)
    intc = read_integer_constants(fh, fixhdr)
    realc = read_real_constants(fh, fixhdr)
    # see if there are level constants
    if (fixhdr[109] > 0):
        levc = read_level_constants(fh, fixhdr)
    else:
        levc = numpy.zeros([0], 'f')

    # get the year index and the number of timesteps we want to subset over
    start_idx = find_month_and_year_index(fixhdr, pp_hdrs, year, month)
    # time frequency between
    time_freq = get_time_frequency(fixhdr)
    n_tsteps = 30 / time_freq * n_months
    # number of fields
    if (intc[14] > 0):
        n_vars = intc[14]
    else:
        n_vars = 1
    n_fields = n_tsteps * intc[
        7] * n_vars  # 7 is number of levels in each field
    # 14 is number of field types
    # read in the data for the subset
    data = read_data(fh, fixhdr, intc, pp_hdrs, start_idx, n_fields)
    data = numpy.array(data)
    sub_pp_hdrs = pp_hdrs[start_idx:start_idx + n_fields]

    fix_fixed_header(fixhdr, intc, sub_pp_hdrs, n_tsteps)
    intc[2] = n_tsteps
    fix_field_header_offsets(sub_pp_hdrs, fixhdr, intc)
    fix_field_header_dates(sub_pp_hdrs, fixhdr, intc)

    write_ancil(outfile, fixhdr, intc, realc, sub_pp_hdrs, data, levc)
    fh.close()
Exemplo n.º 7
0
def subset_ancil(infile, outfile, year, month, n_months):
    # subset an ancil based on the start year, start month and number of 
    # months
    fh = open(infile, 'rb')
    fixhdr = read_fixed_header(fh)
    pp_hdrs = read_pp_headers(fh, fixhdr)
    intc = read_integer_constants(fh, fixhdr)
    realc = read_real_constants(fh, fixhdr)
    # see if there are level constants
    if (fixhdr[109] > 0):
        levc = read_level_constants(fh, fixhdr)
    else:
        levc = numpy.zeros([0], 'f')
    
    # get the year index and the number of timesteps we want to subset over
    start_idx = find_month_and_year_index(fixhdr, pp_hdrs, year, month)
    # time frequency between
    time_freq = get_time_frequency(fixhdr)
    n_tsteps = 30 / time_freq * n_months
    # number of fields
    if (intc[14] > 0):
        n_vars = intc[14]
    else:
        n_vars = 1
    n_fields = n_tsteps * intc[7] * n_vars     # 7 is number of levels in each field
                                               # 14 is number of field types
    # read in the data for the subset
    data = read_data(fh, fixhdr, intc, pp_hdrs, start_idx, n_fields)
    data = numpy.array(data)
    sub_pp_hdrs = pp_hdrs[start_idx:start_idx+n_fields]
    
    fix_fixed_header(fixhdr, intc, sub_pp_hdrs, n_tsteps)
    intc[2] = n_tsteps
    fix_field_header_offsets(sub_pp_hdrs, fixhdr, intc)
    fix_field_header_dates(sub_pp_hdrs, fixhdr, intc)
    
    write_ancil(outfile, fixhdr, intc, realc, sub_pp_hdrs, data, levc)
    fh.close()
def write_data_sst_sice(date, period, grid, sst_data, ice_data, mv, sst_fname, sice_fname):
    # date       = first date in the file - format [day, month, year]
    # grid       = 'N144' | 'N96' | 'N48' | 'HadGEM'
    # sst_data   = matrix containing sea surface temperature data
    # ice_data   = matrix containing ice fraction data - must match grid size and be [z, lat, lon]
    # mv         = missing value
    # period     = number of days per time step
    # sst_fname  = output filename for sst
    # sice_fname = output filename for sice

    # number of data points
    t_steps = sst_data.shape[0]

    # create the headers for the SST
    fixhdr = create_fixed_header(grid, t_steps, 1, date, period, 1)
    pphdr  = create_sst_sice_header(grid, t_steps, date, 'SST', mv, period)
    intc   = create_integer_constants(grid, t_steps, 1, 1)
    realc  = create_real_constants(grid)
    write_ancil(sst_fname, fixhdr, intc, realc, pphdr, sst_data)

    # create the headers for the SICE
    # fixhdr, intc, realc the same
    pphdr = create_sst_sice_header(grid, t_steps, date, 'SICE', mv, period)
    write_ancil(sice_fname, fixhdr, intc, realc, pphdr, ice_data)