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)
# -*- coding: utf-8 -*-s import numpy as np import create_timeseries as cts import xarray as xr import matplotlib.pyplot as plt lons, lats, lonbs, latbs, nlon, nlat, nlonb, nlatb = cts.create_grid( manual_grid_option=False) time_arr, day_number, ntime, time_units, time_bounds = cts.create_time_arr( num_years=1, is_climatology=True, time_spacing=72) data_file = '/scratch/rg419/Data_moist/climatologies/sn_1.000.nc' data = xr.open_dataset(data_file, decode_times=False) data = data.t_surf # Take the zonal mean of the data data_mean = data.mean('lon') # Make sure DJF is the opposite of JJA etc. data_sym = np.zeros(data_mean.values.shape) for i in range(0, 36): data_sym[i, :] = (data_mean[i, :].values + data_mean[i + 36, ::-1].values) / 2. 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