def Main_Fun(dump_time_index, case, date): """Loops over ensemble cases. Pulls temperature, pressure, height from nc output files using nchap_class gets height of mixed layer at each horizontal point using fast_fit and saves as a txt file. Arguments: dump_time -- time of output eg '0000000720' case -- integer Returns: ML_Heights -- 128*192 array as txt file """ dump_time_list, time_hrs = Make_Timelists(1, 3600, 28800) ncfile = "/tera2/nchaparr/"+ date +"/runs/sam_case" + str(case) + "/OUT_3D/keep/NCHAPP1_testing_doscamiopdata_24_" + dump_time_list[dump_time_index] + ".nc" thefile = ncfile print thefile ncdata = Dataset(thefile,'r') press = np.squeeze(ncdata.variables['p'][...])#pressure already horizontally averaged height = np.squeeze(ncdata.variables['z'][...]) temp = np.squeeze(ncdata.variables['TABS'][...]) #tracer = np.squeeze(ncdata.variables['TRACER'][...]) ncdata.close() #calculate thetas theta = np.zeros_like(temp) thetafact = np.array([(1.0*1000/k)**(1.0*287/1004) for k in press]) for j in range(312): theta[j, :, :] = temp[j, :, :]*thetafact[j] ML_Heights = np.empty([128, 192]) for i in range(128): #change to 128 for j in range(192):#change to 192 top = np.where(np.abs(height-2300)<100)[0][0] RSS, J, K = fsft.get_fit(theta[:, i, j], height, top) print i, j, J ML_Heights[i, j] = height[J] #if height[J] < 500: #print i, j, J, height[J]#TODO:this could definitely be a function b_1 = (np.sum(np.multiply(height[:J], theta[:J, i, j])) - 1.0/J*np.sum(height[:J])*np.sum(theta[:J, i, j]))/(np.sum(height[:J]**2) - 1.0/J*np.sum(height[:J])**2) a_1 = np.sum(np.multiply(height[:J], theta[:J, i, j]))/np.sum(height[:J]) - b_1*np.sum(height[:J]**2)/np.sum(height[:J]) b_2 = (np.sum(theta[J:K, i, j]) - (K-J)*(a_1+b_1*height[J]))/(np.sum(height[J:K]) - (K-J)*height[J]) a_2 = np.sum(np.multiply(height[J:K], theta[J:K, i, j]))/np.sum(height[J:K]) - b_2*np.sum(height[J:K]**2)/np.sum(height[J:K]) b_3 = (np.sum(theta[K:290, i, j]) - (290-K)*(a_2+b_2*height[K]))/(np.sum(height[K:290]) - (290-K)*height[K]) if b_3-b_2>.002: #print i, j, J, height[J], height[K], b_3, b_2, b_3-b_2 ML_Heights[i, j] = height[K] print '/tera/phil/nchaparr/python/Plotting/'+date+'/data/mixed_layer_height_'+ str(case) + '_' + dump_time_list[dump_time_index] np.savetxt('/tera/phil/nchaparr/python/Plotting/'+date+'/data/mixed_layer_height_'+ str(case) + '_' + dump_time_list[dump_time_index], ML_Heights, delimiter=' ') return ML_Heights
# getting points of maximum theta gradient, getting rid of this soon # [dvardz, grad_peaks] = nc.Domain_Grad(theta, height) # tops_indices=np.where(np.abs(grad_peaks - 1400)<10) # choosing one horizontal point for i in range(1): # top_index = [tops_indices[0][i], tops_indices[1][i]] # [i, j] = top_index [i, j] = [50, 50] thetavals = theta[:, i, j] startTime = datetime.now() # print 'Start', startTime#1 top = np.where(np.abs(height - 2300) < 100)[0][0] print top, height[top] RSS, J, K = fsft.get_fit(thetavals, height, top) # print J, height[J] # print 'RSS time', (datetime.now()-startTime) fitvals = np.zeros_like(thetavals[:top]) b_1 = ( np.sum(np.multiply(height[9:J], thetavals[9:J])) - 1.0 / (J - 9) * np.sum(height[9:J] * np.sum(thetavals[9:J])) ) / (np.sum(height[9:J] ** 2) - 1.0 / (J - 9) * np.sum(height[9:J]) ** 2) # print np.sum(np.multiply(height[9:J], thetavals[9:J])), - 1.0/(J-9)*np.sum(height[9:J]*np.sum(thetavals[9:J])), np.sum(height[9:J]**2), - 1.0/(J-9)*np.sum(height[9:J])**2 a_1 = np.sum(np.multiply(height[9:J], thetavals[9:J])) / np.sum(height[9:J]) - b_1 * np.sum( height[9:J] ** 2 ) / np.sum(height[9:J]) b_2 = (np.sum(thetavals[J:K]) - (K - J) * (a_1 + b_1 * height[J])) / (np.sum(height[J:K]) - (K - J) * height[J]) a_2 = np.sum(np.multiply(height[J:K], thetavals[J:K])) / np.sum(height[J:K]) - b_2 * np.sum(