Ejemplo n.º 1
0
def output_nc_file(dataset, field_name, model_params, output_dict):

    #create grid
    try:
        lons = dataset.lon.values
        lats = dataset.lat.values
        latbs = dataset.latb.values
        lonbs = dataset.lonb.values
        nlon = lons.shape[0]
        nlat = lats.shape[0]
        nlatb = latbs.shape[0]
        nlonb = lonbs.shape[0]
    except:
        lons, lats, lonbs, latbs, nlon, nlat, nlonb, nlatb = cts.create_grid(
            output_dict['manual_grid_option'])

    if output_dict['is_thd']:
        p_full, p_half, npfull, nphalf = cts.create_pressures()
    else:
        p_full = None
        p_half = None
        npfull = None
        nphalf = None

    #create times
    try:
        output_dict['num_years']
    except KeyError:
        is_climatology = True
    else:
        if output_dict['num_years'] == 1:
            is_climatology = True
        else:
            is_climatology = False
        num_years = output_dict['num_years']

    time_arr, day_number, ntime, time_units, time_bounds = cts.create_time_arr(
        num_years, is_climatology, output_dict['time_spacing_days'])

    #Output it to a netcdf file.
    file_name = output_dict['file_name']
    variable_name = output_dict['var_name']

    number_dict = {}
    number_dict['nlat'] = nlat
    number_dict['nlon'] = nlon
    number_dict['nlatb'] = nlatb
    number_dict['nlonb'] = nlonb
    number_dict['npfull'] = npfull
    number_dict['nphalf'] = nphalf
    number_dict['ntime'] = ntime

    data_out = dataset[field_name].load().data

    cts.output_to_file(data_out, lats, lons, latbs, lonbs, p_full, p_half,
                       time_arr, time_units, file_name, variable_name,
                       number_dict, time_bounds)
Ejemplo n.º 2
0
def output_to_file(dataset, output_array, output_filename, variable_name):
    """
	Simple function for outputting adjusted data as a netcdf file.
	"""
    nt, ny, nx = np.shape(output_array)

    p_full = None
    p_half = None

    npfull = None
    nphalf = None

    #Find grid and time numbers

    lats = dataset.lat.values
    lons = dataset.lon.values
    latbs = dataset.latb.values
    lonbs = dataset.lonb.values
    time_arr = dataset.time.values

    ntime = nt
    nlonb = dataset.lonb.values.shape[0]
    nlatb = dataset.latb.values.shape[0]

    #Output it to a netcdf file.

    number_dict = {}
    number_dict['nlat'] = ny
    number_dict['nlon'] = nx
    number_dict['nlatb'] = nlatb
    number_dict['nlonb'] = nlonb
    number_dict['npfull'] = npfull
    number_dict['nphalf'] = nphalf
    number_dict['ntime'] = ntime

    time_units = 'days since 0000-01-01 00:00:00.0'

    cts.output_to_file(output_array, lats, lons, latbs, lonbs, p_full, p_half,
                       time_arr, time_units, output_file_name, variable_name,
                       number_dict)
Ejemplo n.º 3
0
    data_sym[i + 36, :] = data_sym[i, ::-1]

# Put longitude back in!
data = np.repeat(np.expand_dims(data_sym, axis=2), 128, axis=2)

number_dict = {}
number_dict['nlat'] = nlat
number_dict['nlon'] = nlon
number_dict['nlatb'] = nlatb
number_dict['nlonb'] = nlonb
number_dict['ntime'] = ntime

variable_name = 'sn_1.000_sst'
file_name = './sn_1.000_sst.nc'

cts.output_to_file(data, lats, lons, latbs, lonbs, None, None, time_arr,
                   time_units, file_name, variable_name, number_dict)

time_arr, day_number, ntime, time_units, time_bounds = cts.create_time_arr(
    num_years=1, is_climatology=True, time_spacing=360)

for i in range(36, 55, 2):

    data_ss = (data[i - 1, :, :] + data[i, :, :]) / 2.

    data_ss = np.repeat(np.expand_dims(data_ss, axis=0), 360, axis=0)

    variable_name = 'sn_1.000_sst_' + str(i)
    file_name = './sn_1.000_sst_' + str(i) + '.nc'

    cts.output_to_file(data_ss, lats, lons, latbs, lonbs, None, None, time_arr,
                       time_units, file_name, variable_name, number_dict)
Ejemplo n.º 4
0
nlon=len(lons)
nlat=len(lats)

nlonb=len(lonbs)
nlatb=len(latbs)

npfull=len(p_full)
nphalf=len(p_half)

ntime=len(time_arr)


#Output it to a netcdf file. 
file_name='ozone_1990_cmip5_test.nc'
variable_name='ozone_1990_cmip5'

number_dict={}
number_dict['nlat']=nlat
number_dict['nlon']=nlon
number_dict['nlatb']=nlatb
number_dict['nlonb']=nlonb
number_dict['npfull']=npfull
number_dict['nphalf']=nphalf
number_dict['ntime']=ntime

time_units='days since 0000-01-01 00:00:00.0'

cts.output_to_file(ozone_new,lats,lons,latbs,lonbs,p_full,p_half,time_arr,time_units,file_name,variable_name,number_dict)

Ejemplo n.º 5
0
def main():

    base_directory = '/scratch/sit204/sst_amip_files/'

    amip_data_version = 'amip_data_version_1_1_0'  #s 'amip_data_version_1_1_0' or 'amip_data_version_1_0_0'

    output_name_list = {'tosbcs': 'sst', 'siconc': 'siconc'}
    #Note that we are using the bcs (boundary conditions) input4MIPs files, as instructed.
    # The theory is that by using the bcs files (which are mid-month values) the time-average
    # of the interpolated bcs files should be equal to the time-average data provided in 'tos'
    # files, not the 'tosbcs'. See http://www-pcmdi.llnl.gov/projects/amip/AMIP2EXPDSN/BCS/amip2bcs.php
    # and http://www-pcmdi.llnl.gov/projects/amip/AMIP2EXPDSN/BCS/amipbc_dwnld_files/360x180/v1.0.0/nc/readme_nc

    add_anomaly = False
    #     anomaly_type='el_nino'
    months_to_include = 'all'

    for variable_name in list(output_name_list.keys()):

        if amip_data_version == 'amip_data_version_1_0_0':
            nfiles = 50
            folder_name = '/1950_1999/'
            filename_prefix = 'amipbc_sst_360x180_19'
            sst_all = np.zeros((nfiles, 12, 180, 360))
            do_annual_mean = True
        elif amip_data_version == 'amip_data_version_1_1_0':
            nfiles = 1
            folder_name = ''
            filename_prefix = variable_name + '_input4MIPs_SSTsAndSeaIce_CMIP_PCMDI-AMIP-1-1-0_gs1x1_187001-201512'
            do_annual_mean = False
        elif amip_data_version == 'hadgem_t_surf':
            nfiles = 1
            folder_name = ''
            filename_prefix = 'ts_clim'
            do_annual_mean = False

        for file_tick in range(nfiles):

            if nfiles != 1:
                filename = filename_prefix + str(file_tick + 50)
            else:
                filename = filename_prefix

            resolution_file = Dataset(
                base_directory + amip_data_version + '/' + folder_name + '/' +
                filename + '.nc', 'r')

            try:
                lons = resolution_file.variables['longitude'][:]
                lats = resolution_file.variables['latitude'][:]
            except KeyError:
                lons = resolution_file.variables['lon'][:]
                lats = resolution_file.variables['lat'][:]

            sst_in = resolution_file.variables[variable_name][:]

            try:
                sst_all[file_tick, :, :, :] = sst_in
            except NameError:
                sst_all = sst_in
            except IndexError:
                sst_all = sst_in

        try:
            no_latb_lonb = False
            lonbs = resolution_file.variables['bounds_longitude'][:]
            latbs = resolution_file.variables['bounds_latitude'][:]
        except KeyError:
            try:
                lonbs = resolution_file.variables['lon_bnds'][:]
                latbs = resolution_file.variables['lat_bnds'][:]
            except:
                no_latb_lonb = True

        nlon = lons.shape[0]
        nlat = lats.shape[0]

        if not no_latb_lonb:
            nlonb = lonbs.shape[0]
            nlatb = latbs.shape[0]

            lonbs_adjusted = np.zeros(nlonb + 1)
            latbs_adjusted = np.zeros(nlatb + 1)

            lonbs_adjusted[0:nlonb] = lonbs[:, 0]
            lonbs_adjusted[nlonb] = lonbs[-1, 1]

            latbs_adjusted[0:nlatb] = latbs[:, 0]
            latbs_adjusted[nlatb] = latbs[-1, 1]
        else:
            latbs_adjusted = None
            lonbs_adjusted = None

        try:
            day_number = resolution_file.variables['time'][:]
        except:
            day_number = np.ones(12)

        time_arr = day_number_to_date(day_number,
                                      calendar_type='gregorian',
                                      units_in='days since 1870-1-1')
        time_arr_adj = np.arange(15, 360, 30)

        annual_mean_name = ''

        if len(sst_all.shape) == 4:
            sst_in = np.mean(sst_all, axis=0)
        else:
            sst_in = np.zeros((12, nlat, nlon))

            if months_to_include == 'all':
                for month_tick in np.arange(1, 13, 1):
                    month_idx = np.where(time_arr.month == month_tick)[0]
                    sst_in[month_tick - 1, :, :] = np.mean(
                        sst_all[month_idx, :, :], axis=0)

            elif months_to_include == 'DJF':
                djf_idx = np.where(
                    np.logical_or(
                        np.logical_or(time_arr.month == 1,
                                      time_arr.month == 2),
                        time_arr.month == 12))
                djf_mean = np.mean(sst_all[djf_idx[0], ...], axis=0)
                for month_tick in np.arange(1, 13, 1):
                    sst_in[month_tick - 1, ...] = djf_mean
                annual_mean_name = '_djf'

            elif months_to_include == 'only_month_available':
                for month_tick in np.arange(1, 13, 1):
                    month_idx = np.where(time_arr.month == month_tick)[0]
                    sst_in[month_tick - 1, :, :] = sst_all

        if do_annual_mean:
            sst_in_am = np.mean(sst_in, axis=0)
            sst_in = np.zeros((12, nlat, nlon))
            for month_tick in np.arange(1, 13, 1):
                sst_in[month_tick - 1, :, :] = sst_in_am
            annual_mean_name = '_am'

        if add_anomaly and variable_name == 'tosbcs':
            sst_in, shifted_lons = add_sst_anomaly(sst_in, anomaly_type)
            anom_name = '_' + anomaly_type
        else:
            anom_name = ''

        p_full = None
        p_half = None

        npfull = None
        nphalf = None

        #Find grid and time numbers

        ntime = time_arr.day.shape[0]
        if not no_latb_lonb:
            nlonb = lonbs_adjusted.shape[0]
            nlatb = latbs_adjusted.shape[0]

        #Output it to a netcdf file.
        variable_name = output_name_list[
            variable_name] + annual_mean_name + '_clim_' + amip_data_version[
                0:5] + anom_name
        file_name = variable_name + '_' + amip_data_version + '.nc'

        number_dict = {}
        number_dict['nlat'] = nlat
        number_dict['nlon'] = nlon
        number_dict['npfull'] = npfull
        number_dict['nphalf'] = nphalf
        number_dict['ntime'] = ntime

        if not no_latb_lonb:
            number_dict['nlatb'] = nlatb
            number_dict['nlonb'] = nlonb

        time_units = 'days since 0000-01-01 00:00:00.0'

        cts.output_to_file(sst_in, lats, lons, latbs_adjusted, lonbs_adjusted,
                           p_full, p_half, time_arr_adj, time_units, file_name,
                           variable_name, number_dict)