def extract(files,**kwargs): if files.__class__ == str: if kwargs.has_key('x') and kwargs.has_key('y'): (data, coords) = extract_from_file.extract_from_file(file=files, **kwargs) else: (data, coords) = extract_from_file.extract_from_file(files, **kwargs) elif files.__class__ == list: (data, coords) = extract_from_series.extract_from_series(files, **kwargs) else: raise TypeError('wrong type sent to the extractor') return False return (data,coords)
def extract_from_series(file_list, extraction_type='point', varname='zeta', **kwargs): UTC = pytz.timezone('UTC') pacific = pytz.timezone('US/Pacific') #print('Hello from extractFromSeries') if extraction_type == 'point': # assume var is a 2d var for now file_list.sort() x = kwargs['x'] y = kwargs['y'] data = np.zeros((len(file_list), len(x))) for i in range(len(x)): data[0, i], junk = extract_from_file.extract_from_file( file_list[0], varname=varname, extraction_type='point', x=x[i], y=y[i]) f1 = file_list[0] f2 = file_list[1] time_list = [extract_utils.file_time(file_list[0])] for i in range(1, len(file_list)): for j in range(len(x)): data[i, j], junk = extract_from_file.extract_from_file( file_list[i], varname=varname, extraction_type='point', x=x[j], y=y[j]) time_list.append(extract_utils.file_time(file_list[i])) return (data, time_list) # for i in range(1,ntimes): # time_list.append(time_list[-1]+freq) if extraction_type == 'profile': file_list.sort() ocean_time = None data = None z = None x = kwargs['x'] y = kwargs['y'] if len(x) > 1: x = np.array(x[0]) else: x = np.array(x) if len(y) > 1: y = np.array(y[0]) else: y = np.array(y) for i in range(len(file_list)): file = file_list[i] if not os.path.exists(file): raise IOError( 'File %s could not be located on the filesystem' % file) ncf = nc.Dataset(file, mode='r') if varname not in ncf.variables: raise IOError('File %s does not have a variable named %s' % (file, varname)) ncf.close() d, junk = extract_from_file.extract_from_file( file_list[i], varname=varname, extraction_type='profile', x=x, y=y) if data == None: data = np.zeros((d.shape[0], len(file_list))) data[:, i] = d.T if z == None: z = np.zeros(data.shape) z[:, i] = junk['zm'].T # ocean_time = np.zeros(data.shape) if ocean_time == None: ocean_time = np.zeros(data.shape) ot = extract_utils.file_timestamp(file_list[i]) # ot = file_time(file) for j in range(data.shape[0]): ocean_time[j, i] = ot return (data, ocean_time, z) return
def extract_from_series(file_list,extraction_type='point',varname='zeta',**kwargs): UTC = pytz.timezone('UTC') pacific = pytz.timezone('US/Pacific') #print('Hello from extractFromSeries') if extraction_type == 'point': # assume var is a 2d var for now file_list.sort() x = kwargs['x'] y = kwargs['y'] data = np.zeros((len(file_list),len(x))) for i in range(len(x)): data[0,i],junk = extract_from_file.extract_from_file(file_list[0],varname=varname,extraction_type='point',x=x[i],y=y[i]) f1 = file_list[0] f2 = file_list[1] time_list = [extract_utils.file_time(file_list[0])] for i in range(1,len(file_list)): for j in range(len(x)): data[i,j],junk = extract_from_file.extract_from_file(file_list[i],varname=varname,extraction_type='point',x=x[j],y=y[j]) time_list.append(extract_utils.file_time(file_list[i])) return (data,time_list) # for i in range(1,ntimes): # time_list.append(time_list[-1]+freq) if extraction_type == 'profile': file_list.sort() ocean_time = None data = None z = None x = kwargs['x'] y = kwargs['y'] if len(x) > 1: x = np.array(x[0]) else: x = np.array(x) if len(y) > 1: y = np.array(y[0]) else: y = np.array(y) for i in range(len(file_list)): file = file_list[i] if not os.path.exists(file): raise IOError('File %s could not be located on the filesystem' %file) ncf = nc.Dataset(file,mode='r') if varname not in ncf.variables: raise IOError('File %s does not have a variable named %s' % (file, varname)) ncf.close() d,junk = extract_from_file.extract_from_file(file_list[i],varname=varname,extraction_type='profile',x=x,y=y) if data == None: data = np.zeros((d.shape[0],len(file_list))) data[:,i] = d.T if z == None: z = np.zeros(data.shape) z[:,i] = junk['zm'].T # ocean_time = np.zeros(data.shape) if ocean_time == None: ocean_time = np.zeros(data.shape) ot = extract_utils.file_timestamp(file_list[i]) # ot = file_time(file) for j in range(data.shape[0]): ocean_time[j,i] = ot return (data, ocean_time,z) return