def __init__(self, src_file ): super(data_reader, self).__init__(src_file, f=ncfile) f = ncfile(self.src,'r'): self.__labels = f.variables() self.__timevar = f.variables.keys()[0] f.close()
def variables(self): try: f = ncfile(self.src,'r') variables = f.variables.keys() time_vec = f.variables[timevar][:] f.close() except: msg = 'error reading header labels from {}'.format(self.src) self.logger.info(msg) raise Exception(msg) return labels
def get_timestamp(self, timestep = 0, time_header = None): try: f = ncfile(self.src,'r') timevar = time_header.keys()[0] time_vec = f.variables(timevar)[:] outtime = time_vec[timestep] f.close() except: self.logger.info('cannot return time at timestep {}'.format(timestep)) raise Exception('cannot return time at timestep {}'.format(timestep)) return outtime
def gen_vec(self, vname, args = {} ): self.logger.info(' generating {} '.format(vname) ) if vname in self.__labels: column = vname else: column = self.__labels[0] self.logger.info('calling nc reader with: {} '.format(str(arg))) try: f = ncfile(self.src,'r') vec = f.variables[column].get_values() f.close() except: msg = 'error reading {} from {}'.format(columnm,self.src) self.logger.info(msg) raise Exception(msg) return vec
vtype = 'Amon' gtype = '*' # this could be gn or gr, this is the grid type (gn = native, gr = regridded) # Path for data path_input = '/badc/cmip6/data/CMIP6/'+project+'/' # Path to read in input files data_path = '/home/users/dmitchell/Radiosonde/Data/Models/' # Path to write out final product # If the file already exists, should it be overwritten? overw = True # Specifics if you interpolate on to a grid. Note that this is set up for radio sonde data, but you will have to change to your require obs if (inter == True): path_obs = './Data/Obs/' nc = ncfile(path_obs + 'raobcore15_gridded_2017.nc') data_rao2 = nc.variables['anomalies'][:,:,::-1,:] # reverse direction of lats data_rao = np.ma.zeros((data_rao2.shape)) data_rao[:,:,:,0:18] = data_rao2[:,:,:,18:] # Shift lon data to be consistent with model data_rao[:,:,:,18:] = data_rao2[:,:,:,:18] # Shift lon data to be consistent with model lat_obs = nc.variables['lat'][::-1] # Reverse lat values so they are asscending, for interp lon_obs = nc.variables['lon'][:] + 180 # Shift lon data to be consistent with model p_obs = nc.variables['pressure'][:] # Specify a mask if one is used if (mask == True): mask_vals = np.ma.getmask(data_rao) # ========================================= Main body of code ========================================== # Do not alter anything below this line unless you really hve to========================================
def main(): cwd = os.getcwd() subpath = r'NEWINPUT/WAH_MODEL/' fname = r'NEWINPUT/WAH_MODEL/item5216_monthly_mean_h000_1985-12_1986-12.nc' path = os.path.join(cwd, fname) dir_path = os.path.join(cwd, subpath) print dir_path print path #get lat/lon dims dataset = ncfile(path) lats = dataset.variables['global_latitude0'][:] lons = dataset.variables['global_longitude0'][:] lat_dim = lats.shape[0] lon_dim = lats.shape[1] #get all file names onlyfiles = [ f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f)) ] #print onlyfiles files_split = [[f, f[0:-3].split('_')] for f in onlyfiles] for el in files_split: fname = el.pop(0) el[0].append(fname) files_split = [item for sublist in files_split for item in sublist] #files_split = [item for sublist in files_split for item in sublist] #print files_split keys = [ 'variable_name', 'time_period', 'stat', 'ensemble_member', 'start_date', 'end_date', 'filename' ] files_dict = [dict(zip(keys, f)) for f in files_split] #print files_dict for el in files_dict: el['end_year'] = el['end_date'][0:4] el['start_year'] = el['start_date'][0:4] selection = [ el['filename'] for el in files_dict if el['end_date'][0:4] == '1990' ] #get number of years years = list(set([el['end_year'] for el in files_dict])) print years year_dim = len(years) #get max number of runs n_runs = [] for year in years: selection = [el for el in files_dict if el['end_year'] == year] n_runs.append(len(selection)) max_runs = max(n_runs) print max_runs #initialise the data arrays precip = np.zeros((13, year_dim, lat_dim, lon_dim)) precip_ens = np.zeros((13, year_dim, max_runs, lat_dim, lon_dim)) #path = '/ouce-home/staff/sedm4922/Validation/' # factors to convert units to mm/month fac = 86400 # for the model fac2 = 30.5 # for the obs spatial = 'GHoA' # Define months array mon = [ '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12' ] #precip = np.zeros((12,25,146,209)) #precip_ens = np.zeros((12,25,100,146,209)) # get all the model run data into shape for year_ind in range(year_dim): selection = [ el['filename'] for el in files_dict if el['end_year'] == years[year_ind] ] for run_ind in range(max_runs): file_path = os.path.join(dir_path, selection[run_ind]) #print file_path ds = ncfile(path) ds_precip = ds.variables['item5216_monthly_mean'][:] ds_precip = np.squeeze(ds_precip) precip_ens[:, year_ind, run_ind, :, :] = ds_precip print 'precip', precip_ens.shape #average it by axis precip_output = np.nanmean(precip_ens, axis=4) #lat precip_output = np.nanmean(precip_output, axis=3) #lon precip_output = np.nanmean(precip_output, axis=1) #year -> 13 mos x N runs #get the average of all the runs precip_mean = np.nanmean(precip_output, axis=1) #filter GHOA extents min_lat = min(lats.flatten()) max_lat = max(lats.flatten()) min_lon = min(lons.flatten()) max_lon = max(lons.flatten()) #print min_lat, max_lat, min_lon, max_lon #load in the historic data subpath = r'NEWINPUT/' fname = r'chirps_clim.nc' file_path = os.path.join(subpath, fname) chirps_ds = ncfile(file_path) #print chirps_ds.dimensions.keys() #print chirps_ds.variables.keys() chirps_precip = chirps_ds.variables['precip'][:] #get the lats and lons and times lats = chirps_ds.variables['latitude'][:] lons = chirps_ds.variables['longitude'][:] times = chirps_ds.variables['time'][:] print times #create masks using the GHOA extents lat_mask = ((lats > min_lat) & (lats < max_lat)) lon_mask = ((lons > min_lon) & (lons < max_lon)) #tile one dimension of those badboys out tile = np.tile(lat_mask, (len(lons), 1)) #create a mask of same global shape chirps_mask = np.ones((12, len(lats), len(lons)), dtype=bool) #multiply the boolean tile and lon mask, broadcast into the global mask chirps_mask[:, :, :] = np.multiply(tile.T, lon_mask) print chirps_mask.shape #get the masked data, (convert to 'not ~') np.ma.where(data, mask) #chirps_ghoa = chirps_precip[~chirps_mask] #print chirps_ghoa.shape chirps_ghoa = np.ma.masked_where(chirps_precip, ~chirps_mask) chirps_mean = np.nanmean(chirps_ghoa, axis=(1, 2)) #years, months, basemap #gen months print chirps_mean precip_mean = np.mean(precip_ens, axis=(3, 4)) precip_mean = np.delete(precip_mean, (0), axis=0) print precip_mean.shape print precip_mean shape = precip_mean.shape dats = np.random.rand(shape[0], shape[1], shape[2]) #gr_monthly(chirps_mean, dats) gr_map(chirps_ghoa, precip_ens, min_lat, max_lat, min_lon, max_lon)
from netCDF4 import Dataset as ncfile import numpy as np from tools import calc_es, calc_rh, calc_ws, col_av import matplotlib.pyplot as plt #filepath = 'E:/ATM115 Data/SST310k-selected/' filepath = '/home/lsterzinger/Documents/ATM115-Data/SST310k-selected/' data3d = '/home/lsterzinger/Documents/ATM115-Data/SST310k-selected/sam3d.nc' data2d = '/home/lsterzinger/Documents/ATM115-Data/SST310k-selected/sam2d.nc' outputfilename = '310K_vars.nc' sam3d = ncfile(data3d) sam2d = ncfile(data2d) x = sam3d.variables['x'][:] z = sam3d.variables['z'][:] t = sam3d.variables['time'][:] qv = sam3d['QV'][:] temp = sam3d['TABS'][:] pressure_ref = sam3d['p'][:] pres_pert = sam3d['PP'][:] pressure = np.zeros((3600, 71, 1024)) # Pressure perturbarion + base state for i in range(0, 1024): for j in range(0, 3600): pressure[j, :, i] = pres_pert[j, :, i] + pressure_ref[:] #Delete unneeded variables pressure_ref = None
from netCDF4 import Dataset as ncfile import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap path = 'E:\\project\\oilspill_rescue\\rescue\\output\\' nc_file = ncfile(path + 'qz_ty1_100p.nc', 'r') # print(nc_file) lon_list = nc_file.variables['lon'] # 读取NC文件中的变量 lat_list = nc_file.variables['lat'] lon = np.array(lon_list) # 转成数组 lat = np.array(lat_list) # lon_0 = lon_list[0, :] # 读取数组第1行的所有列 # lon_0[lon_0 > 360] = np.nan # 数组中大于360的数据都赋值NAN # lon_mean = np.nanmean(lon_0) # 求数组中除NAN外数据的平均值 lon_mean = [] lat_mean = [] for i in range(len(lon[0, :])): lon_temp = lon[:, i] lat_temp = lat[:, i] lon_temp[lon_temp > 360] = np.nan lat_temp[lat_temp > 360] = np.nan lon_temp_mean = np.nanmean(lon_temp) lat_temp_mean = np.nanmean(lat_temp)
vtype = 'Amon' gtype = '*' # this could be gn or gr, this is the grid type (gn = native, gr = regridded) # Path for data path_input = '/badc/cmip6/data/CMIP6/' + project + '/' # Path to read in input files data_path = '/home/users/dmitchell/Radiosonde/Data/Models/' # Path to write out final product # If the file already exists, should it be overwritten? overw = False # Specifics if you interpolate on to a grid. Note that this is set up for radio sonde data, but you will have to change to your require obs if (inter == True): path_obs = './Data/Obs/' nc = ncfile(path_obs + 'raobcore15_gridded_2017.nc') data_rao2 = nc.variables['anomalies'][:, :, :: -1, :] # reverse direction of lats data_rao = np.ma.zeros((data_rao2.shape)) data_rao[:, :, :, 0: 18] = data_rao2[:, :, :, 18:] # Shift lon data to be consistent with model data_rao[:, :, :, 18:] = data_rao2[:, :, :, : 18] # Shift lon data to be consistent with model lat_obs = nc.variables[ 'lat'][::-1] # Reverse lat values so they are asscending, for interp lon_obs = nc.variables[ 'lon'][:] + 180 # Shift lon data to be consistent with model p_obs = nc.variables['pressure'][:]
fname = el.pop(0) el[0].append(fname) files_split = [item for sublist in files_split for item in sublist] #files_split = [item for sublist in files_split for item in sublist] #print files_split keys = ['variable_name','time_period','stat','ensemble_member','start_date','end_date','filename'] files_dict = [dict(zip(keys,f)) for f in files_split] #print files_dict selection = [el['filename'] for el in files_dict if el['end_date'][0:4]=='1990'] print selection #convert filenames to a dict #write a loop that opens each file and adds to an empty np array os.chdir(subpath) for s in selection: print s nc=ncfile(s) temp = nc.variables['item5216_monthly_mean'][:] print temp.shape exit() #sarah test """ dataset = ncfile(path) print dataset.file_format print dataset.dimensions.keys() print dataset.variables.keys() lats = dataset.variables['global_latitude0'][:] lons = dataset.variables['global_longitude0'][:] time = dataset.variables['global_longitude0'][:] pr = dataset.variables['item5216_monthly_mean'][:]
#plt.xticks(time2) #plt.xticks(np.arange(1,525,130)) #fig.autofmt_xdate() #plt.show() plt.savefig('/home/sps/Desktop/ursi_images/windmagnitude.png') ############################################################################################################################# #!/usr/bin/env python import matplotlib.pyplot as plt import numpy as np from netCDF4 import Dataset as ncfile from cfplot import * import math nc = ncfile('/home/sps/Desktop/python/gdata.nc') lats = nc.variables['lat'][:] p = nc.variables['p'][:] temp = nc.variables['temp'][:, :, 0] p0 = 1013.25 H = 8 p1 = (p / p0) p2 = np.log(p1) z = (-8) * p2 print z fig = plt.figure(figsize=(11, 8)) ax1 = fig.add_subplot(111) #ax1.axis([-90, 90, 0, 20]) #ax1.tick_params(direction='out', which='both')
@author: anonymous """ # Python3 Script to read and plot example of Era 5 SST from netCDF4 import Dataset as ncfile import numpy as np from matplotlib.colors import BoundaryNorm import matplotlib.pyplot as plt import cartopy # ------------------------- # LOAD DATA: # ------------------------- fil = ncfile('./e5.moda.an.sfc.128_034_sstk.ll025sc.2018010100_2018120100.nc','r') lat = fil.variables['latitude'][:] lon = fil.variables['longitude'][:] sst = fil.variables['SSTK'][:,:,:] - 273.15 # SST in Degrees C # ------------------------- # PLOT January 2018 Sea Surface Temperature # ------------------------- # map_proj = cartopy.crs.LambertCylindrical(central_longitude=200.0) LAND_highres = cartopy.feature.NaturalEarthFeature('physical', 'land', '50m', edgecolor='face', facecolor = 'black', linewidth=.1) fig, axs = plt.subplots(1,1, figsize=(10, 3), facecolor='w', edgecolor='k') axs = plt.subplot(1, 1, 1, projection = map_proj) axs.add_feature(LAND_highres) axs.set_title('Era5 SST ($^\circ$C): January 2018', fontsize = 12)
lon_lower = 33.5 # compare with: 1 = None, 2 = TRMM, 3 = CRU, 4 = None_TRMM, 5 = None_CRU compare = 'TRMM' # ============================================================================== # Path where data is stored path = '/ouce-home/staff/sedm4922/Validation/' # factors to convert units to mm/month fac = 3600 * 24 * 30 # for the model fac2 = 24 * 30.5 # for the obs # load landsea mask nc = ncfile(path + 'lsm_africa.nc') lsm = nc.variables['lsm'][0, 0, :, :] * -1. lsm[lsm == 0] = np.nan # Define months array mon = [ 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ] dimo = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if test_mode == 1: precip = np.zeros((12, 25, 138, 200)) precip_ens = np.zeros((12, 25, 100, 138, 200)) for moncount in np.arange(len(mon)):
# IMPORTANT: THIS SCRIPT ASSUMES YOU HAVE 3X0_VARS.NC GENERATED FROM MAIN.PY # AND LOCATED IN THE SAME FOLDER AS THIS FILE. THE VARIABLES PREC_DAILY, # PREC_MONTHLY, RH_DAILY, AND RH_MONTHLY WILL BE APPENDED from netCDF4 import Dataset as ncfile import numpy as np from tools import calc_es, calc_rh, calc_ws, run_param, col_av import matplotlib.pyplot as plt #filepath = 'E:/ATM115 Data/SST300k-selected/' filepath = './SST300k-selected/' data3d = './SST300k-selected/sam3d.nc' data2d = './SST300k-selected/sam2d.nc' outputfilename = '300K_vars.nc' sam3d = ncfile(data3d) sam2d = ncfile(data2d) varfile = ncfile('./300K_vars.nc','r+') prec = sam2d['Prec'][:] rh = varfile['rh'][:] # Daily Averages prec_daily = np.zeros((150,1024)) rh_daily = np.zeros((150,71,1024)) tmin = 0 tmax = 23 for day in range(0,150): for x in range(0,1024):
# -*- coding: utf-8 -*- """ Created on Fri Dec 6 11:38:22 2019 @author: 25006773 """ from netCDF4 import Dataset as ncfile import datetime as dt import pandas as pd import matplotlib.pyplot as plt import numpy as np # Give the location of the MJO file and open it infile = '/Users/seren/Documents/University/Year 3/MT37J (Boundary Layer Met)/Assignment 2/MJO_rmm1_rmm2.jan-dec_dmeans_ts.1979-2018 (1) - Copy.nc' nc_infile = ncfile(infile) # Read in variables mjo_phase = nc_infile['phase_ts'][:] mjo_amp = nc_infile['amplitude_ts'][:] rmm1 = nc_infile['rmm1_ts'][:] rmm2 = nc_infile['rmm2_ts'][:] #Read in time variable to get list of dates time = nc_infile['time'][:] #create empty MJO date lists phase0_dates = [] phase1_dates = [] phase2_dates = [] phase3_dates = [] phase4_dates = []
fac = 3600 * 24 * 30 # for the model fac2 = 24 * 30.5 # for the obs #mon = ['01','02','03','04','05','06','07','08','09','10','11','12'] mon = [ 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ] dimo = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] precip = np.zeros((12, 25, 138, 200)) precip_ens = np.zeros((12, 25, 100, 138, 200)) for moncount in np.arange(len(mon)): if var == 'prp': nc = ncfile(path + mon[moncount] + '_precip_Africa_1986-2010_Dec-Nov.nc') temp = nc.variables['precip'][:] * fac temp[temp > np.std(temp) * 10] = np.nan temp[temp < 0] = np.nan temp[temp == 0] = np.nan precip[moncount, :, :, :] = np.nanmean(temp, axis=1) precip_ens[moncount, :, :, :, :] = temp precip_ens[:, :, 40, :, :] = np.nan precip_ens[:, :, 11, :, :] = np.nan lat = nc.variables['latitude'][:] lon = nc.variables['longitude'][:] if spatial == 'Whole': lat1 = np.arange(len(lat))
import cartopy.crs as ccrs import matplotlib as mpl from scipy import ndimage from sys import exit #Choose variable to look at: var = 'prp' #Choose path where data stored path = '/ouce-home/staff/sedm4922/Validation/' #Choose factor to convert to mm/month fac = 3600*24*30 # for the model fac2 = 24*30.5 # for the obs #Choose mask nc=ncfile(path+'ethiopiamask_12.nc') eth = nc.variables['mask'][:,:,:] mon = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'] dimo = [31,28,31,30,31,30,31,31,30,31,30,31] precip = np.zeros((12,25,138,200)) precip_ens = np.zeros((12,25,100,138,200)) # compare with: 1 = None, 2 = TRMM, 3 = CRU, 4 = CHIRPS compare = 'None' precip = np.zeros((12,25,138,200)) precip_ens = np.zeros((12,25,100,138,200))