def pw13spice(sal,temp,pden): beta = 7.7e-4*1000 alpha = 2.0e-4*1000 mt=reshape(mt,shape(temp)) tau=2*(temp-mt)*0.2 Sline=np.array([33.2,33.8]) Tline=np.array([7.75,6.6]) Sarray = np.arange(30,35,0.01) m = (np.diff(Tline)/np.diff(Sline)) Tarray = m*(Sarray-Sline[0])+Tline[0] Pdarray=sw.pden(Sarray,Tarray,100.+0.*Tarray,0.) T0 = np.interp(pden,sp['pdengrid'][:-1],sp['meanT'][0]) tau = 2*alpha*(temp-T0) return tau
def calc_density(S, T, p): args = S, T, p prho = sw.pden(*args) rho = sw.dens(*args) return prho, rho
def loadMVP_raw(file,depthbins): import numpy as np from matplotlib.dates import date2num import datetime import seawater.eos80 as sw import numpy.lib.recfunctions as rec #import time import scipy.signal as signal #import bindata #from binMVPdata import binMVPdata import binMVPdata import calendar # try: if 1: f = open(file, 'r') # print ("Processing " + file) header = f.read(7000) #read in 7000 bytes to get the header profile_xyt = {} i = header.find('LAT ( ddmm.mmmmmmm,N):') profile_xyt['lat_DDMMmm'] = header[i+23:i+36] profile_xyt['lat_hemisphere'] = header[i+37:i+38] profile_xyt['latitude'] = (float(profile_xyt['lat_DDMMmm'][1:3])) +(np.divide((float(profile_xyt['lat_DDMMmm'][3:])),60)) if (profile_xyt['lat_hemisphere']=='S'): profile_xyt['latitude']=-profile_xyt['latitude'] i = header.find('LON (dddmm.mmmmmmm,E):') profile_xyt['lon_DDMMmm'] = header[i+23:i+36] profile_xyt['lon_hemisphere'] = header[i+37:i+38] profile_xyt['longitude'] = (float(profile_xyt['lon_DDMMmm'][1:3])) +(np.divide((float(profile_xyt['lon_DDMMmm'][3:])),60)) if profile_xyt['lon_hemisphere']=='W': profile_xyt['longitude'] =-profile_xyt['longitude'] i = header.find('Time (hh|mm|ss.s):') profile_xyt['time'] = header[i+19:i+29] i = header.find('Date (dd/mm/yyyy):') profile_xyt['date'] = header[i+19:i+29] datetimestring = profile_xyt['date']+' '+profile_xyt['time'] datetimefmt = ('%d/%m/%Y %H:%M:%S.%f') #print datetimestring[:-5] try: profile_xyt['datetime'] = datetime.datetime.strptime(datetimestring, datetimefmt) except: datetimefmt = ('%d/%m/%Y %H:%M') profile_xyt['datetime'] = datetime.datetime.strptime(datetimestring[:-5], datetimefmt) profile_xyt['unixtime'] = calendar.timegm(profile_xyt['datetime'].utctimetuple()) profile_xyt['mtime'] = date2num(profile_xyt['datetime']) profile_xyt['matlabtime'] = date2num(profile_xyt['datetime'])+366. profile_xyt['tzone'] = 'Z' i = header.find('Index: ') profile_xyt['cast_number'] = int(header[i+7:i+11]) # profile_xyt['datetime']=date2num(profile_xyt['datetime']) # i = header.find('Bottom Depth (m):') profile_xyt['bottom']=float(header[i+17:i+22]) # print profile_xyt['bottom'] newline1 = header.find('\n',6000,len(header)) newline2 = header.find('\n',newline1+1,len(header)) rawfields = ["pressure","cond","temp","analog"] if newline2 - newline1 > 26: data = np.genfromtxt(file, usecols = (0,1,2,3), skip_header = 61, names = ["pressure","cond","temp","analog"]) else: data = np.genfromtxt(file, usecols = (0,1,2), skip_header = 61, names = ["pressure","cond","temp"] ) temparray = np.empty((data['pressure'].shape)) temparray.fill(np.NaN) data = rec.append_fields(data,'analog',temparray,dtypes = float) # lag conductivity to match w. temp cell N = np.shape(data['cond'])[0] #print N data = rec.append_fields(data,'cond0',data['cond'],dtypes = float) tt=np.arange(0,N,1.) data['cond']=np.interp(tt-2.25,tt,data['cond0']) #this loop here will find the indices for when the fish is going down pressurederivative = np.diff(data['pressure']) # take the derivative of the pressure profile B, A = signal.butter(2, 0.01, output='ba') # constants for the filter smooth_pressurederivative = signal.filtfilt(B,A, pressurederivative) #filter the pressure derivative, to smooth out the curve smooth_pressurederivative = np.append(smooth_pressurederivative,[0]) # make the arrays the same size down_inds = np.where(smooth_pressurederivative>0.05) # find the indices where the descent rate is more than 0.1 #depth_bins = np.arange(0,300,1) profile_grid = {} for k in rawfields: profile_grid[k] = binMVPdata.binMVPdata(depthbins, data['pressure'][down_inds],data[k][down_inds]) profile_grid['salinity'] = sw.salt(profile_grid['cond']/42.9140, profile_grid['temp'], profile_grid['pressure']) profile_grid['density'] = sw.dens(profile_grid['salinity'], profile_grid['temp'],profile_grid['pressure']) profile_grid['pden'] = sw.pden(profile_grid['salinity'], profile_grid['temp'],profile_grid['pressure'],0) f.close() return profile_grid, profile_xyt, data
def recalc_S_and_sigma(C, T, p): S = salt(C/c3515, T, p) sigma = pden(S, T, p) - 1000 return S, sigma
def den_1file(filename_tem, filename_sal, savename, temname, salname, depname, denname, latname, lonname, pressure_lvl, mlat, timname): """ Computes the density profile from a given file. Check the documentation of den_main for more information. """ ############# # LOAD DATA # ############# if filename_tem == filename_sal: # Launch reading routine for 1 file [tem, sal], lats, lons, tim, deps = load_main(filename_tem, [temname, salname], latname, lonname, depname, timname) else: # Launch reading routine for 2 different files [tem], _, _, _, _ = load_main(filename_tem, [temname], latname, lonname, depname, timname) [sal], lats, lons, tim, deps = load_main(filename_sal, [salname], latname, lonname, depname, timname) pdens = np.empty_like(sal) dim = lats.shape index = np.logical_and(sal == 0, tem == 0) sal[index], tem[index] = np.nan, np.nan ###################### # GET DENSITY VALUES # ###################### # Depth variable is already a pressure level if pressure_lvl: for j in range(dim[0]): for i in range(dim[1]): for t in range(sal.shape[0]): pdens[t, :, j, i] = pden(sal[t, :, j, i], tem[t, :, j, i], deps) # Compute the pressure levels with a mean latitude value elif mlat is not None: pre = pres(deps, mlat) for j in range(dim[0]): for i in range(dim[1]): for t in range(sal.shape[0]): pdens[t, :, j, i] = pden(sal[t, :, j, i], tem[t, :, j, i], pre) del pre # Compute pressure levels with all the latitudes else: for j in range(dim[0]): for i in range(dim[1]): pre = pres(deps, lats[j, i]) for t in range(sal.shape[0]): pdens[t, :, j, i] = pden(sal[t, :, j, i], tem[t, :, j, i], pre) ############# # SAVE DATA # ############# ds = {lonname: (('y', 'x'), lons), latname: (('y', 'x'), lats), depname: (('z'), deps), denname: (('t', 'z', 'y', 'x'), pdens)} if timname is not None: ds[timname] = (('t'), tim) ds = xr.Dataset(ds) ds.to_netcdf(savename+'.nc')
endtime = mdates.num2date(grid_full['mtime'][-1]).strftime('%H:%M:%S') #endtime = (datetime.datetime.fromtimestamp(grid_full['unixtime'][0,-1])).strftime('%Y-%m-%d %H:%M:%S') startendstring = ('Cast '+ str(int(grid_full['cast_number'][0])) + ' at ' + starttime +' \nCast '+ str(int(grid_full['cast_number'][-1])) + ' at ' + endtime) ax[0].set_title(startendstring,fontsize = 12) ### TS plot plt.subplot(gs3b3[0:2,2]) plt.plot(grid_full['salinity'][:,-ncasts:],grid_full['temp'][:,-ncasts:],'k.',markersize=1) plt.plot(grid_full['salinity'][:,-2],grid_full['temp'][:,-2],'m.') plt.plot(grid_full['salinity'][:,-1],grid_full['temp'][:,-1],'r.') ss=np.linspace(np.nanmin(grid_full['salinity']),np.nanmax(grid_full['salinity']),50 ) tt=np.linspace(np.nanmin(grid_full['temp']),np.nanmax(grid_full['temp']),50 ) SS,TT=np.meshgrid(ss,tt) PDen=sw.pden(SS,TT,0.*TT,0) cs=plt.contour(ss,tt,PDen-1000.,density_contours-1000.,colors='k') plt.clabel(cs,fmt='%1.1f') plt.xlabel('S [psu]') plt.ylabel('T [C]') plt.xlim(clim3[1]) plt.ylim(clim3[0]) #plt.draw() ### Spice plot grid_full['spice']=dc15bspice(grid_full['salinity'],grid_full['temp'],grid_full['pden']) aa=ax.append(plt.subplot(gs3b3[3,0:2])) plt.gca().set_axis_bgcolor('0.6') clim=[-.5,0.5] pc=contourZ(X,Y,np.ma.masked_where(np.isnan(grid_full['spice']),