def easy_cal(d,t,s,dt):			# 線形内挿を使わずに手っ取り早く計算いたします
	import numpy as np
	import subroutine
	import density
	rho=density.rho(t,s,0)
	drho=density.rho(t[0]-dt,s[0],0)-rho[0]
	ILD=max(d[np.where(t>=t[0]-dt)])
	MLD=max(d[np.where(rho<=rho[0]+drho)])
	BLT=ILD-MLD
	return [MLD,ILD,BLT]
def cal(d,t,s,dt):					# 水温と塩分それぞれのプロファイル、及びそのデータが得られた水深のデータを入れることによって、層厚を計算することができるプログラム。
	import numpy as np
	import subroutine
	import density
	tp=subroutine.interpolate(t,d)
	sp=subroutine.interpolate(s,d)
	rhop=density.rho(tp,sp,0)
	drho=density.rho(tp[0]-dt,sp[0],0)-rhop[0]
	ILD=tp[np.where(tp>=tp[0]-dt)].size
	MLD=rhop[np.where(rhop<=rhop[0]+drho)].size
	BLT=ILD-MLD
	return [MLD,ILD,BLT]
def make_data_from_ts(t, s, product_n = 3, dt = 0.2):
	_,title_name,_=subroutine.product_n_to_name(product_n)
	xgrid,ygrid,zgrid=subroutine.product_grid_info('t','data',product_n)

	# MOAA_GPVだけ、格納されているのが(ポテンシャル水温でなく)通常の水温なので、まずポテンシャル水温への変換が必要
	if title_name=='MOAA_GPV':
		theta=np.zeros((t.shape[0],t.shape[1],t.shape[2]))
		for m in range(0,zgrid.size):
			theta[:,:,m]=density.poT(t[:,:,m],s[:,:,m],zgrid[m])

		t=theta
		t[np.where(abs(t)== inf)]=nan

	rho=density.rho(t,s,0)
	rho[np.where(abs(rho)== inf)]=nan
	xn=xgrid.size
	yn=ygrid.size
	ILD=np.zeros([yn,xn])
	MLD=np.zeros([yn,xn])
	BLT=np.zeros([yn,xn])
	for i in range(0,xn):
		for j in range(0,yn):
			if np.isnan(t[j,i,0]) == False:
				tpro=t[j,i,:]
				rhopro=rho[j,i,:]
				spro = s[j, i, :]
				ILD[j, i], MLD[j, i], BLT[j, i] = make_data_from_profile(tpro, spro, rhopro, zgrid, dt)
			else:
				ILD[j,i]=nan
				MLD[j,i]=nan
				BLT[j, i] = nan


	return ILD,MLD,BLT
def make_data_from_profile(tpro, spro, rhopro, zgrid, dt = 0.2):
	drho=density.rho(tpro[0]-dt,spro[0],0)-density.rho(tpro[0],spro[0],0)
	# ILD calculating
	if np.where(tpro[0]-tpro>dt)[0].size==0:
		# 全層でsst-dt度より水温が高かった時。
		# 海底までの深さをILDとする。
		n1=max(np.where(np.isnan(tpro) == False)[0])
		z1=zgrid[n1]
		ILD=round(z1,0)
	else:
		n1=min(np.where(tpro[0]-tpro>dt)[0])-1
		n2=n1+1
		z1=zgrid[n1]
		z2=zgrid[n2]
		t1=tpro[n1]
		t2=tpro[n2]
		D=z2-z1
		dz=(t1-(tpro[0]-dt))/(t1-t2)*D
		ILD=round(z1+dz,0)
	# ILD calculating

	# MLD calculating
	if np.where(rhopro-rhopro[0]>drho)[0].size==0:
		# 全層でssrho+drhoより密度が低かった時。
		# 海底までの深さをMLDとする。
		n1=max(np.where(rhopro!=inf)[0])
		n1=max(np.where(np.isnan(rhopro) == False)[0])
		z1=zgrid[n1]
		MLD=round(z1,0)
	else:
		n1=min(np.where(rhopro-rhopro[0]>drho)[0])-1
		n2=n1+1
		z1=zgrid[n1]
		z2=zgrid[n2]
		rho1=rhopro[n1]
		rho2=rhopro[n2]
		D=z2-z1
		dz=(rho1-(rhopro[0]+drho))/(rho1-rho2)*D
		MLD=round(z1+dz,0)
	# MLD calculating

	BLT=ILD-MLD
	if BLT < 0.0:
		BLT = 0.0

	return ILD, MLD, BLT
def make_rho_from_ts(t,s, product_n = 3):
	xgrid,ygrid,zgrid=subroutine.product_grid_info('t','data',product_n)
	_, title_name, _ = subroutine.product_n_to_name(product_n)
	# MOAA_GPVだけ、格納されているのが(ポテンシャル水温でなく)通常の水温なので、まずポテンシャル水温への変換が必要
	if title_name=='MOAA_GPV':
		theta=zeros((t.shape[0],t.shape[1],t.shape[2]))
		for m in range(0,zgrid.size):
			theta[:,:,m]=density.poT(t[:,:,m],s[:,:,m],zgrid[m])

		t=theta
		t[where(abs(t)== inf)]=nan

	rho = density.rho(t, s, 0)
	rho[np.where(np.abs(rho) == sp.inf)] = np.nan
	return rho