def get_doppio(lat=0,lon=0,depth='bottom',time='2018-11-12 12:00:00',fortype='temperature'):
    """
    notice:
        the format of time is like "%Y-%m-%d %H:%M:%S" this time is utctime  or it can also be datetime
        the depth is under the bottom depth
    the module only output the temperature of point location
    if fortype ='temperature',only return temperature, else return temperature and depth
    """
    if depth==99999:
       depth='bottom'
    if not doppio_coordinate(lat,lon):
        print('the lat and lon out of range in doppio')
        return np.nan,np.nan
    if type(time)==str:
        date_time=datetime.datetime.strptime(time,'%Y-%m-%d %H:%M:%S') # transform time format
    elif type(time)==datetime.datetime:
        date_time=time
    else:
        print('check the type of input time in get_doppio')
    for m in range(0,7):
        try:
            url_time=(date_time-datetime.timedelta(days=m)).strftime('%Y-%m-%d')#
            url=zl.get_doppio_url(url_time)
            #get the data 
            nc=netCDF4.Dataset(url)
            lons=nc.variables['lon_rho'][:]
            lats=nc.variables['lat_rho'][:]
            doppio_time=nc.variables['time']
            doppio_rho=nc.variables['s_rho']
            doppio_temp=nc.variables['temp']
            doppio_h=nc.variables['h']
        except:
            continue
        min_diff_time=abs(datetime.datetime(2017,11,1,0,0,0)+datetime.timedelta(hours=int(doppio_time[0]))-date_time)
        min_diff_index=0
        for i in range(1,len(doppio_time)):
            diff_time=abs(datetime.datetime(2017,11,1,0,0,0)+datetime.timedelta(hours=int(doppio_time[i]))-date_time)
            if diff_time<min_diff_time:
                min_diff_time=diff_time
                min_diff_index=i
        #calculate the min,second small and third small distance and index
        target_distance=zl.dist(lat1=lats[0][0],lon1=lons[0][0],lat2=lats[0][1],lon2=lons[0][1])
        index_1,index_2=zl.find_nd(target=target_distance,lat=lat,lon=lon,lats=lats,lons=lons)

        #calculate the optimal layer index added this section Feb 2020
        doppio_depth=nc['h'][index_1][index_2]
        if depth > doppio_depth:# case of bottom
            S_coordinate=1
        else:
            S_coordinate=float(depth)/float(doppio_depth)
        if 0<=S_coordinate<1:
            layer_index=39-int(S_coordinate/0.025)#doppio_temp=temp[itime,39-int(S_coordinate/0.025),index_1,index_2]# because there are 0.025 between each later
        elif S_coordinate==1:
            layer_index=0#doppio_temp=temp[itime][0][index_1][index_2]
        else:
            layer_index=0#doppio_temp=temp[itime][0][index_1][index_2]
        #return doppio_temp
        #layer_index=0  #specify the initial layer index
        '''if depth!='bottom':
            h_distance=depth+doppio_rho[0]*doppio_h[index_1,index_2]  #specify the initial distanc of high
            for i in range(len(doppio_rho)):
                if abs(depth+doppio_rho[0]*doppio_h[index_1,index_2])<=h_distance:
                    h_distance=depth+doppio_rho[i]*doppio_h[index_1,index_2]
                    layer_index=i
                if depth>doppio_h[index_1,index_2]:
                    print ("the depth is out of the depth of bottom:"+str(doppio_h[index_1,index_2]))
        '''
        if index_1==0:
            index_1=1
        if index_1==len(lats)-1:
            index_1=len(lats)-2
        if index_2==0:
            index_2=1
        if index_2==len(lats[0])-1:
            index_2=len(lats[0])-2
        while True:
            point=[[lats[index_1][index_2],lons[index_1][index_2],doppio_temp[min_diff_index,layer_index,index_1,index_2]],\
            [lats[index_1-1][index_2],lons[index_1-1][index_2],doppio_temp[min_diff_index,layer_index,(index_1-1),index_2]],\
            [lats[index_1+1][index_2],lons[index_1+1][index_2],doppio_temp[min_diff_index,layer_index,(index_1+1),index_2]],\
            [lats[index_1][index_2-1],lons[index_1][index_2-1],doppio_temp[min_diff_index,layer_index,index_1,(index_2-1)]],\
            [lats[index_1][index_2+1],lons[index_1][index_2+1],doppio_temp[min_diff_index,layer_index,index_1,(index_2+1)]]]
            break
        point_temp=fitting(point,lat,lon)
        if np.isnan(point_temp):
            continue
        if min_diff_time<datetime.timedelta(hours=1):
            break
    if fortype=='temperature':
        return point_temp
    else:
        return point_temp,doppio_h[index_1,index_2]
Example #2
0
def get_doppio_fitting(latp=0,
                       lonp=0,
                       depth='bottom',
                       dtime=datetime.datetime.now(),
                       fortype='temperature',
                       hour_allowed=1):
    """
    notice:
        the format of time is like "%Y-%m-%d %H:%M:%S" this time is utctime or the type of time is datetime.datetime
        the depth is under the bottom depth
    the module only output the temperature of point location
    """
    if not doppio_coordinate(latp, lonp):
        print('the lat and lon out of range in doppio')
        return np.nan, np.nan
    if type(dtime) == str:
        date_time = datetime.datetime.strptime(
            dtime, '%Y-%m-%d %H:%M:%S')  # transform time format
    else:
        date_time = dtime
    for m in range(0, 7):
        try:
            url_time = (date_time -
                        datetime.timedelta(days=m)).strftime('%Y-%m-%d')
            url = zl.get_doppio_url(url_time)
            #get the data
            nc = netCDF4.Dataset(url)
            lons = nc.variables['lon_rho'][:]
            lats = nc.variables['lat_rho'][:]
            doppio_time = nc.variables['time']
            doppio_rho = nc.variables['s_rho']
            doppio_temp = nc.variables['temp']
            doppio_h = nc.variables['h']
        except:
            continue
        #calculate the index of the minimum timedelta
        parameter = (datetime.datetime(2017, 11, 1, 0, 0, 0) -
                     date_time).days * 24 + (datetime.datetime(
                         2017, 11, 1, 0, 0, 0) - date_time).seconds / 3600.
        time_delta = abs(doppio_time[:] + parameter)
        min_diff_index = np.argmin(time_delta)
        #calculate the min distance and index
        target_distance = 2 * zl.dist(
            lat1=lats[0, 0], lon1=lons[0, 0], lat2=lats[0, 1], lon2=lons[0, 1])
        index_1, index_2 = find_nd(target=target_distance,
                                   lat=latp,
                                   lon=lonp,
                                   lats=lats,
                                   lons=lons)
        #calculate the optimal layer index
        if depth == 'bottom':
            layer_index = 0  #specify the initial layer index
        else:
            h_distance = abs(doppio_rho[:] * doppio_h[index_1, index_2] +
                             abs(depth))
            layer_index = np.argmin(h_distance)


#        fitting the data through the 5 points
        if index_1 == 0:
            index_1 = 1
        if index_1 == len(lats) - 1:
            index_1 = len(lats) - 2
        if index_2 == 0:
            index_2 = 1
        if index_2 == len(lats[0]) - 1:
            index_2 = len(lats[0]) - 2
        while True:
            point=[[lats[index_1][index_2],lons[index_1][index_2],doppio_temp[min_diff_index,layer_index,index_1,index_2]],\
            [lats[index_1-1][index_2],lons[index_1-1][index_2],doppio_temp[min_diff_index,layer_index,(index_1-1),index_2]],\
            [lats[index_1+1][index_2],lons[index_1+1][index_2],doppio_temp[min_diff_index,layer_index,(index_1+1),index_2]],\
            [lats[index_1][index_2-1],lons[index_1][index_2-1],doppio_temp[min_diff_index,layer_index,index_1,(index_2-1)]],\
            [lats[index_1][index_2+1],lons[index_1][index_2+1],doppio_temp[min_diff_index,layer_index,index_1,(index_2+1)]]]
            break
        point_temp = fitting(point, latp, lonp)
        while True:
            points_h=[[lats[index_1][index_2],lons[index_1][index_2],doppio_h[index_1,index_2]],\
            [lats[index_1-1][index_2],lons[index_1-1][index_2],doppio_h[(index_1-1),index_2]],\
            [lats[index_1+1][index_2],lons[index_1+1][index_2],doppio_h[(index_1+1),index_2]],\
            [lats[index_1][index_2-1],lons[index_1][index_2-1],doppio_h[index_1,(index_2-1)]],\
            [lats[index_1][index_2+1],lons[index_1][index_2+1],doppio_h[index_1,(index_2+1)]]]
            break
        point_temp = fitting(point, latp, lonp)
        point_h = fitting(points_h, latp, lonp)
        if np.isnan(point_temp):
            continue
        if time_delta[min_diff_index] < hour_allowed:
            break
    if fortype == 'tempdepth':
        return point_temp, point_h
    else:
        return point_temp
Example #3
0
import zlconversions as zl
import doppio_modules as dm
import numpy as np
#HARDCODES
input_date_time='2018-11-11 12:00:00'
input_lat=41.784712
input_lon=-69.231081
input_depth=0   #If you enter 99999, the default output bottom temperature,enter 0 will output the temperature of surface
output_path='/home/jmanning/Desktop/testout/doppio/'
#########################
date_time=datetime.datetime.strptime(input_date_time,'%Y-%m-%d %H:%M:%S') # transform time format
#find index of the nearest time about data
for m in range(0,7):
    try:
        url_time=(date_time-datetime.timedelta(days=m)).strftime('%Y-%m-%d')#
        url=zl.get_doppio_url(url_time)
        #get the data 
        nc=netCDF4.Dataset(url)
    except:
        continue
    lons=nc.variables['lon_rho'][:]
    lats=nc.variables['lat_rho'][:]
    temp=nc.variables['temp']
    doppio_time=nc.variables['time']
    doppio_depth=nc.variables['h'][:]
    min_diff_time=abs(datetime.datetime(2017,11,1,0,0,0)+datetime.timedelta(hours=int(doppio_time[0]))-date_time) #Set initial value
    min_diff_index=0  #Set initial value
    for i in range(0,157):
        diff_time=abs(datetime.datetime(2017,11,1,0,0,0)+datetime.timedelta(hours=int(doppio_time[i]))-date_time)
        if diff_time<min_diff_time:
            min_diff_time=diff_time
Example #4
0
def get_doppio(lat=0, lon=0, depth=99999, time='2018-11-12 12:00:00'):
    """
    notice:
        the format of time is like "%Y-%m-%d %H:%M:%S"
        the depth is under the bottom depth
    the module only output the temperature of point location
    """
    date_time = datetime.datetime.strptime(
        time, '%Y-%m-%d %H:%M:%S')  # transform time format
    for m in range(0, 7):
        try:
            url_time = (date_time - datetime.timedelta(days=m)).strftime(
                '%Y-%m-%d')  #
            url = zl.get_doppio_url(url_time)
            #get the data
            nc = netCDF4.Dataset(url)
        except:
            continue
        lons = nc.variables['lon_rho'][:]
        lats = nc.variables['lat_rho'][:]
        temp = nc.variables['temp']
        doppio_time = nc.variables['time']
        doppio_depth = nc.variables['h'][:]
        min_diff_time = abs(
            datetime.datetime(2017, 11, 1, 0, 0, 0) +
            datetime.timedelta(hours=int(doppio_time[0])) - date_time)
        min_diff_index = 0
        for i in range(1, len(doppio_time)):
            diff_time = abs(
                datetime.datetime(2017, 11, 1, 0, 0, 0) +
                datetime.timedelta(hours=int(doppio_time[i])) - date_time)
            if diff_time < min_diff_time:
                min_diff_time = diff_time
                min_diff_index = i
        #calculate the min,second small and third small distance and index
        min_distance = zl.dist(lat1=lat,
                               lon1=lon,
                               lat2=lats[0][0],
                               lon2=lons[0][0])
        #        secondmin_distance=min_distance
        #        thirdmin_distance=min_distance
        #
        index_1, index_2 = 0, 0
        secondindex_1, secondindex_2 = 0, 0
        thirdindex_1, thirdindex_2 = 0, 0
        fourthindex_1, fourthindex_2 = 0, 0
        fifthindex_1, fifthindex_2 = 0, 0
        #        sixthindex_1,sixthindex_2=0,0
        for i in range(len(lons)):
            for j in range(len(lons[i])):
                distance = zl.dist(lat1=lat,
                                   lon1=lon,
                                   lat2=lats[i][j],
                                   lon2=lons[i][j])
                if min_distance >= distance:
                    #                    thirdmin_distance=secondmin_distance
                    #                    secondmin_distance=min_distance
                    min_distance = distance
                    #                    sixthindex_1,sixthindex_2=fifthindex_1,fifthindex_2
                    fifthindex_1, fifthindex_2 = fourthindex_1, fourthindex_2
                    fourthindex_1, fourthindex_2 = thirdindex_1, thirdindex_2
                    thirdindex_1, thirdindex_2 = secondindex_1, secondindex_2
                    secondindex_1, secondindex_2 = index_1, index_2
                    index_1, index_2 = i, j

        if depth == 99999:
            S_coordinate = 1
        else:
            S_coordinate = float(depth) / float(doppio_depth[index_1][index_2])
        if 0 <= S_coordinate < 1:
            layer_index = 39 - int(S_coordinate / 0.025)
        elif S_coordinate == 1:
            layer_index = 39
        else:
            return 9999
        point=[[lats[index_1][index_2],lons[index_1][index_2],temp[min_diff_index][layer_index][index_1][index_2]],\
            [lats[secondindex_1][secondindex_2],lons[secondindex_1][secondindex_2],temp[min_diff_index][layer_index][secondindex_1][secondindex_2]],\
            [lats[thirdindex_1][thirdindex_2],lons[thirdindex_1][thirdindex_2],temp[min_diff_index][layer_index][thirdindex_1][thirdindex_2]],\
            [lats[fourthindex_1][fourthindex_2],lons[fourthindex_1][fourthindex_2],temp[min_diff_index][layer_index][fourthindex_1][fourthindex_2]],\
            [lats[fifthindex_1][fifthindex_2],lons[fifthindex_1][fifthindex_2],temp[min_diff_index][layer_index][fifthindex_1][fifthindex_2]]]
        point_temp = fitting(point, lat, lon)
        if np.isnan(point_temp):
            continue
        if min_diff_time < datetime.timedelta(hours=1):
            break
    return point_temp, index_1, index_2