Exemplo n.º 1
0
def gettrack_roms(jdmat_m, lon_v, lat_v, u, v, startdate, numdays, daystep, la, lo): # tracks particle at surface
    # calculate the points near la,lo
    distance, index_location = nearxy(lon_v, lat_v, lo, la)
    ####### get index of startdate in jdmat_m#####
    jdmat_m_num = [] # this is the date in the form of a number
    jdmat_m_list, jdmat = [], []
    # convert array to list
    for jdmat_m_i in jdmat_m:
        jdmat_m_list.append(jdmat_m_i)
    for i in jdmat_m_list:  # convert time to number
        jdmat_m_num.append(i)
    dts = date2num(dt.datetime(2001, 1, 1, 0, 0, 0))
    jdmat_m = [i + dts for i in jdmat_m]
    index_startdate = int(round(np.interp(startdate, jdmat_m, range(len(jdmat_m)))))#get the index of startdate
    print "index_startdate = ", index_startdate, " inside getreack_roms  "          
    print "the start u's location", index_location
    u1 = float(u[index_startdate][index_location])
    v1 = float(v[index_startdate][index_location])
    if u1 == -999.0:  # case of no good data
        u1 = 0
        v1 = 0
    
    nsteps = scipy.floor(min(numdays, jdmat_m_num[-1]) / daystep)
    print "nsteps =", nsteps
    
    uu, vv, lon_k, lat_k, time = [], [], [], [], []
    uu.append(u1)
    vv.append(v1)
    lat_k.append(la)
    lon_k.append(lo)
    time.append(startdate)
              
    for i in range(1, int(nsteps)):
        # first, estimate the particle move to its new position using velocity of previous time steps
        lat1 = lat_k[i - 1] + float(vv[i - 1] * daystep * 24 * 3600) / 1000 / 1.8535 / 60
        lon1 = lon_k[i - 1] + float(uu[i - 1] * daystep * 24 * 3600) / 1000 / 1.8535 / 60 * (scipy.cos(float(lat_k[i - 1])) / 180 * np.pi)
        # find the closest model time for the new timestep
        jdmat_m_num_i = time[i - 1] + daystep
        time.append(jdmat_m_num_i)
        if jdmat_m_num_i > max(jdmat_m_num):
            print "This time is not available in the model"
        index_startdate = int(round(np.interp(jdmat_m_num_i, jdmat_m_num, range(len(jdmat_m_num)))))
               
        #find the point's index of near lat1,lon1
        index_location = nearxy(lon_v, lat_v, lon1, lat1)[1]
         
        ui = u[index_startdate][index_location]
        vi = v[index_startdate][index_location]
                
        vv.append(vi)
        uu.append(ui)
        # estimate the particle move from its new position using velocity of previous time steps
        lat_k.append(float(lat1 + lat_k[i - 1] + float(vv[i] * daystep * 24 * 3600) / 1000 / 1.8535 / 60) / 2)
        lon_k.append(float(lon1 + lon_k[i - 1] + float(uu[i] * daystep * 24 * 3600) / 1000 / 1.8535 / 60 * scipy.cos(float(lat_k[i]) / 180 * np.pi)) / 2)
             
    return lat_k, lon_k, time
Exemplo n.º 2
0
def getFVCOM_bottom_temp(url, time0, mlon, mlat):
    """get depth from the getmodel"""
    dataset = get_dataset(url)
    tri = dataset['nv']
    jdmat_m = list(dataset['Times'])
    temp = dataset['temp']
    h_vel = list(dataset['h'])
    siglay = dataset['siglay']
    lat_vel = list(dataset['lat'])
    lon_vel = list(dataset['lon'])

    # get the tri0,tri1,tri2 as the location of three points
    tri0 = [i for i in tri[0]]
    tri1 = [i for i in tri[1]]
    tri2 = [i for i in tri[2]]

    lat_vel_1, lon_vel_1 = [], []
    for i in range(len(tri0)):
        if tri1[i] == len(lat_vel):
            tri1[i] = len(lat_vel) - 1
        if tri2[i] == len(lat_vel):
            tri2[i] = len(lat_vel) - 1
        lat_vel_1.append(
            float((lat_vel[tri0[i]] + lat_vel[tri1[i]] + lat_vel[tri2[i]])) /
            float(3))
        lon_vel_1.append(
            float((lon_vel[tri0[i]] + lon_vel[tri1[i]] + lon_vel[tri2[i]])) /
            float(3))
    # get the min distance
    (distance, index_location) = nearxy(lon_vel_1, lat_vel_1, mlon, mlat)
    (disth, index_location_h) = nearxy(lon_vel, lat_vel, mlon, mlat)

    #exclude incorrect data
    jdmat = [jd for jd in jdmat_m if jd[17:19] != 60]

    jdmat_m_num = []  # this is the data in the form of a number
    for i in jdmat:
        jdmat_m_num.append(
            date2num(dt.datetime.strptime(i, '%Y-%m-%dT%H:%M:%S.%f')))

    depths = h_vel[index_location_h] * siglay[:, index_location_h]

    #get the ids of depths
    depths_list = []
    for d in range(0, len(depths)):
        depths_list.append(depths[d])
    depths_list.reverse()  #Where is this used???

    jdmat_index = range(len(jdmat_m_num))
    index_time = int(round(np.interp(time0, jdmat_m_num, jdmat_index)))
    temperature = list(temp[index_time, :, index_location_h])

    return depths, temperature
Exemplo n.º 3
0
def getFVCOM_bottom_temp(url, time0, mlon, mlat):
    """get depth from the getmodel"""
    dataset = get_dataset(url)
    tri = dataset["nv"]
    jdmat_m = list(dataset["Times"])
    temp = dataset["temp"]
    h_vel = list(dataset["h"])
    siglay = dataset["siglay"]
    lat_vel = list(dataset["lat"])
    lon_vel = list(dataset["lon"])

    # get the tri0,tri1,tri2 as the location of three points
    tri0 = [i for i in tri[0]]
    tri1 = [i for i in tri[1]]
    tri2 = [i for i in tri[2]]

    lat_vel_1, lon_vel_1 = [], []
    for i in range(len(tri0)):
        if tri1[i] == len(lat_vel):
            tri1[i] = len(lat_vel) - 1
        if tri2[i] == len(lat_vel):
            tri2[i] = len(lat_vel) - 1
        lat_vel_1.append(float((lat_vel[tri0[i]] + lat_vel[tri1[i]] + lat_vel[tri2[i]])) / float(3))
        lon_vel_1.append(float((lon_vel[tri0[i]] + lon_vel[tri1[i]] + lon_vel[tri2[i]])) / float(3))
    # get the min distance
    (distance, index_location) = nearxy(lon_vel_1, lat_vel_1, mlon, mlat)
    (disth, index_location_h) = nearxy(lon_vel, lat_vel, mlon, mlat)

    # exclude incorrect data
    jdmat = [jd for jd in jdmat_m if jd[17:19] != 60]

    jdmat_m_num = []  # this is the data in the form of a number
    for i in jdmat:
        jdmat_m_num.append(date2num(dt.datetime.strptime(i, "%Y-%m-%dT%H:%M:%S.%f")))

    depths = h_vel[index_location_h] * siglay[:, index_location_h]

    # get the ids of depths
    depths_list = []
    for d in range(0, len(depths)):
        depths_list.append(depths[d])
    depths_list.reverse()  # Where is this used???

    jdmat_index = range(len(jdmat_m_num))
    index_time = int(round(np.interp(time0, jdmat_m_num, jdmat_index)))
    temperature = list(temp[index_time, :, index_location_h])

    return depths, temperature
Exemplo n.º 4
0
def gettrack_codar(jdmat_m,lon_vel,lat_vel,u,v,startdate,numdays,daystep,la,lo):
    """
    tracks particle at surface
    """
    # calculate the points near la,la
    distance,index_location=nearxy(lon_vel,lat_vel,lo,la)
    
    ####### get index of startdate in jdmat_m#####
    jdmat_m_num=[] # this is the date in the form of a number
    jdmat_m_list,jdmat=[],[]
    # convert array to list
    for jdmat_m_i in jdmat_m:
        jdmat_m_list.append(jdmat_m_i)
    for i in jdmat_m_list:  # convert time to number
        jdmat_m_num.append(i)
    dts=date2num(datetime.datetime(2001,1,1,0,0,0))
    jdmat_m=[i+dts for i in jdmat_m]
    index_startdate=int(round(np.interp(startdate,jdmat_m,range(len(jdmat_m)))))#get the index of startdate
    # get u,v
    u1=float(u[index_startdate][index_location])
    v1=float(v[index_startdate][index_location])
    if u1==-999.0/100:# case of no good data
        u1=0
        v1=0
    nsteps=scipy.floor(min(numdays,jdmat_m_num[-1])/daystep)
    # get the velocity data at this first time & place
    lat_k='lat'+str(1)
    lon_k='lon'+str(1)
    uu,vv,lon_k,lat_k,time=[],[],[],[],[]
    uu.append(u1)
    vv.append(v1)
    lat_k.append(la)
    lon_k.append(lo)
    time.append(startdate)
              
    for i in range(1,int(nsteps)):
        # first, estimate the particle move to its new position using velocity of previous time steps
        lat1=lat_k[i-1]+float(vv[i-1]*daystep*24*3600)/1000/1.8535/60
        lon1=lon_k[i-1]+float(uu[i-1]*daystep*24*3600)/1000/1.8535/60*(scipy.cos(float(lat_k[i-1]))/180*np.pi)
        # find the closest model time for the new timestep
        jdmat_m_num_i=time[i-1]+daystep
        time.append(jdmat_m_num_i)
        #print jdmat_m_num_i

        index_startdate=int(round(np.interp(jdmat_m_num_i,jdmat_m_num,range(len(jdmat_m_num)))))
        #find the point's index of near lat1,lon1
        index_location=nearxy(lon_vel,lat_vel,lon1,lat1)[1]
        ui=u[index_startdate][index_location]
        vi=v[index_startdate][index_location]
        #if u1<>-999.0/100:# case of good data
        vv.append(vi)
        uu.append(ui)
        # estimate the particle move from its new position using velocity of previous time steps
        lat_k.append(float(lat1+lat_k[i-1]+float(vv[i]*daystep*24*3600)/1000/1.8535/60)/2)
        lon_k.append(float(lon1+lon_k[i-1]+float(uu[i]*daystep*24*3600)/1000/1.8535/60*scipy.cos(float(lat_k[i])/180*np.pi))/2)
        #else:
        #  vv.append(0)
        #  uu.append(0)
          # estimate the particle move from its new position using velocity of previous time steps
        #  lat_k.append(float(lat1))
        #  lon_k.append(float(lon1))      
    return lat_k,lon_k,time
Exemplo n.º 5
0
[lati,loni,on]=getemolt_latlon(site) # extracts lat/lon based on site code
[lati,loni]=dm2dd(lati,loni) #converts decimal-minutes to decimal degrees
[obs_dt,obs_temp]=getemolt_temp(site) # extracts time series
for kk in range(len(obs_temp)):
  obs_temp[kk]=f2c(obs_temp[kk]) # converts to Celcius

# now get the model output
urlfvcom = 'http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3'
nc = netCDF4.Dataset(urlfvcom)
nc.variables
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
times = nc.variables['time']
jd = netCDF4.num2date(times[:],times.units)
vname = 'temp'
var = nc.variables[vname]

# find nearest point to desired location and time
inode = nearxy(lon,lat,loni,lati)
index=netCDF4.date2index([obs_dt[0],obs_dt[-1]],times,select='nearest')#find the model time index at start & end pf obs

#plot them
fig=plt.figure(figsize=(16,4))
ax=fig.add_subplot(111)
ax.plot_date(obs_dt,obs_temp,fmt='-')
plt.grid()
ax.plot_date(jd[index[0]:index[1]],var[index[0]:index[1],44,inode],fmt='-',color='red')
plt.ylabel(var.units)
plt.title('eMOLT site '+site+' temp vs FVCOM '+'%s at node=%d (Lon=%.4f, Lat=%.4f)' % (vname, inode+1, lon[inode], lat[inode]))
plt.legend(['observed','modeled'],loc='best')
plt.show()
Exemplo n.º 6
0
def model_plot_track(depth, jdmat_m, lat_vel_1, lon_vel_1, u, v, lat_vel,
                     lon_vel, h_vel, siglay, startdate, numdays, daystep, x,
                     la, lo):
    fig = plt.figure(1)
    if len(depth) == 1:
        panels = len(startdate)
    if len(depth) != 1:
        panels = len(depth)
    for j in range(1, panels + 1):
        print int(str(panels) + str(2) + str(j))
        if panels == 1:
            ax = fig.add_subplot(111)
        else:
            ax = fig.add_subplot(int(str(2) + str(2) + str(j)))
        if j == 3:
            plt.xlabel('Longitude W')
        if j == 1:
            plt.ylabel('Latitude N')
        for k in range(len(la)):
            for m in range(len(lo)):
                (distance, index_location) = nearxy(lon_vel_1, lat_vel_1,
                                                    lo[m], la[k])
                (disth, index_location_h) = nearxy(lon_vel, lat_vel, lo[m],
                                                   la[k])

                depths = h_vel[index_location_h] * siglay[:, index_location_h]
                # make depths to be descending order
                new_depths = list(depths)
                new_depths.reverse()
                # depths range
                range_depths = range(len(depths))
                range_depths.reverse()
                if len(depth) == 1:
                    idz = 0
                else:
                    idz = int(
                        round(np.interp(depth[j - 1], new_depths,
                                        range_depths)))

                ####### get index of startdate in jdmat_m
                jdmat_m_num = []  # this is the data in the form of a number
                del_list, jdmat, del_index = [], [], []
                # convert array to list
                jdmat_m_list = [jdmat_m_i for jdmat_m_i in jdmat_m]

                # while seconds=60, can change the datetime to number, so delete
                for i in range(1, len(jdmat_m_list)):
                    if jdmat_m_list[i][17:19] == '60' or jdmat_m_list[
                            i - 1] == jdmat_m_list[i]:
                        del_list.append(jdmat_m_list[i])
                        del_index.append(i)  #get the index of deleted datetime
                jdmat = [val for val in jdmat_m_list if val not in del_list
                         ]  # delete the wrong value, jdmat just
                for i in jdmat:  # convert time to number
                    jdmat_m_num.append(
                        date2num(
                            dt.datetime.strptime(i, '%Y-%m-%dT%H:%M:%S.%f')))
                # get index of startdate in jdmat_m_num
                index_startdate_1 = int(
                    round(
                        np.interp(date2num(startdate[j - 1]), jdmat_m_num,
                                  range(len(jdmat_m_num)))))
                if del_index != []:
                    index_add = int(
                        np.ceil(
                            np.interp(index_startdate_1, del_index,
                                      range(len(del_index)))))
                    index_startdate = index_add + index_startdate_1
                else:
                    index_startdate = index_startdate_1
                # get u,v
                u1 = float(u[index_startdate, idz, index_location])
                v1 = float(v[index_startdate, idz, index_location])
                nsteps = scipy.floor(min(numdays, jdmat_m_num[-1]) / daystep)
                # get the velocity data at this first time & place
                lat_k = 'lat' + str(k)
                lon_k = 'lon' + str(k)
                uu, vv, lon_k, lat_k, time = [], [], [], [], []
                uu.append(u1)
                vv.append(v1)
                lat_k.append(la[k])
                lon_k.append(lo[m])
                time.append(date2num(startdate[j - 1]))

            for i in range(1, int(nsteps)):
                # first, estimate the particle move to its new position using velocity of previous time steps
                lat1 = lat_k[i - 1] + float(
                    vv[i - 1] * daystep * 24 * 3600) / 1000 / 1.8535 / 60
                lon1 = lon_k[i - 1] + float(
                    uu[i - 1] * daystep * 24 * 3600) / 1000 / 1.8535 / 60 * (
                        scipy.cos(float(lat_k[i - 1])) / 180 * np.pi)
                # find the closest model time for the new timestep
                jdmat_m_num_i = time[i - 1] + daystep
                time.append(jdmat_m_num_i)

                index_startdate_1 = int(
                    round(
                        np.interp(jdmat_m_num_i, jdmat_m_num,
                                  range(len(jdmat_m_num)))))
                if del_index != []:
                    index_add = int(
                        np.ceil(
                            np.interp(index_startdate_1, del_index,
                                      range(len(del_index)))))
                    index_startdate = index_add + index_startdate_1
                else:
                    index_startdate = index_startdate_1
                #find the point's index of near lat1,lon1
                index_location = nearxy(lon_vel_1, lat_vel_1, lon1, lat1)[1]
                ui = u[index_startdate, 1, index_location]
                vi = v[index_startdate, 1, index_location]
                vv.append(vi)
                uu.append(ui)
                # estimate the particle move from its new position using velocity of previous time steps
                lat_k.append(
                    float(lat1 + lat_k[i - 1] +
                          float(vv[i] * daystep * 24 * 3600) / 1000 / 1.8535 /
                          60) / 2)
                lon_k.append(
                    float(lon1 + lon_k[i - 1] +
                          float(uu[i] * daystep * 24 * 3600) / 1000 / 1.8535 /
                          60 * scipy.cos(float(lat_k[i]) / 180 * np.pi)) / 2)

            plt.plot(lon_k,
                     lat_k,
                     "-",
                     linewidth=1,
                     marker=".",
                     markerfacecolor='r')
            basemap([min(lat_k) - 2, max(lat_k) + 2],
                    [min(lon_k) - 2, max(lon_k) + 2])
            # make same size for the panels
            min_lat, max_lon, max_lat, min_lon = [], [], [], []
            min_lat.append(min(lat_k))
            max_lat.append(max(lat_k))
            min_lon.append(min(lon_k))
            max_lon.append(max(lon_k))

            text1 = ax.annotate(str(num2date(time[0]).month) + "-" +
                                str(num2date(time[0]).day),
                                xy=(lon_k[0], lat_k[0]),
                                xycoords='data',
                                xytext=(8, 10),
                                textcoords='offset points',
                                arrowprops=dict(arrowstyle="->"))
            text1.draggable()
            text1 = ax.annotate(str(num2date(time[-1]).month) + "-" +
                                str(num2date(time[-1]).day),
                                xy=(lon_k[-1], lat_k[-1]),
                                xycoords='data',
                                xytext=(8, 10),
                                textcoords='offset points',
                                arrowprops=dict(arrowstyle="->"))

        #set the numbers formatter
        majorFormatter = FormatStrFormatter('%.2f')
        ax.yaxis.set_major_formatter(majorFormatter)
        ax.xaxis.set_major_formatter(majorFormatter)
        if len(depth) == 1:
            plt.title(str(num2date(time[i - 1]).year))
        else:
            plt.title(
                str(num2date(time[i - 1]).year) + " year" + " depth: " +
                str(depth[j - 1]))

        ax.xaxis.set_label_coords(0.5, -0.005)  #set the position of the xlabel
        ax.yaxis.set_label_coords(-0.1, 0.5)
        plt.xlim([min(min_lon), max(max_lon)])
        plt.ylim([min(min_lat), max(max_lat)])
    plt.show()
Exemplo n.º 7
0
def gettrack_roms(jdmat_m, lon_v, lat_v, u, v, startdate, numdays, daystep, la,
                  lo):  # tracks particle at surface
    # calculate the points near la,lo
    distance, index_location = nearxy(lon_v, lat_v, lo, la)
    ####### get index of startdate in jdmat_m#####
    jdmat_m_num = []  # this is the date in the form of a number
    jdmat_m_list, jdmat = [], []
    # convert array to list
    for jdmat_m_i in jdmat_m:
        jdmat_m_list.append(jdmat_m_i)
    for i in jdmat_m_list:  # convert time to number
        jdmat_m_num.append(i)
    dts = date2num(dt.datetime(2001, 1, 1, 0, 0, 0))
    jdmat_m = [i + dts for i in jdmat_m]
    index_startdate = int(
        round(np.interp(startdate, jdmat_m,
                        range(len(jdmat_m)))))  #get the index of startdate
    print "index_startdate = ", index_startdate, " inside getreack_roms  "
    print "the start u's location", index_location
    u1 = float(u[index_startdate][index_location])
    v1 = float(v[index_startdate][index_location])
    if u1 == -999.0:  # case of no good data
        u1 = 0
        v1 = 0

    nsteps = scipy.floor(min(numdays, jdmat_m_num[-1]) / daystep)
    print "nsteps =", nsteps

    uu, vv, lon_k, lat_k, time = [], [], [], [], []
    uu.append(u1)
    vv.append(v1)
    lat_k.append(la)
    lon_k.append(lo)
    time.append(startdate)

    for i in range(1, int(nsteps)):
        # first, estimate the particle move to its new position using velocity of previous time steps
        lat1 = lat_k[i - 1] + float(
            vv[i - 1] * daystep * 24 * 3600) / 1000 / 1.8535 / 60
        lon1 = lon_k[i - 1] + float(
            uu[i - 1] * daystep * 24 * 3600) / 1000 / 1.8535 / 60 * (
                scipy.cos(float(lat_k[i - 1])) / 180 * np.pi)
        # find the closest model time for the new timestep
        jdmat_m_num_i = time[i - 1] + daystep
        time.append(jdmat_m_num_i)
        if jdmat_m_num_i > max(jdmat_m_num):
            print "This time is not available in the model"
        index_startdate = int(
            round(
                np.interp(jdmat_m_num_i, jdmat_m_num,
                          range(len(jdmat_m_num)))))

        #find the point's index of near lat1,lon1
        index_location = nearxy(lon_v, lat_v, lon1, lat1)[1]

        ui = u[index_startdate][index_location]
        vi = v[index_startdate][index_location]

        vv.append(vi)
        uu.append(ui)
        # estimate the particle move from its new position using velocity of previous time steps
        lat_k.append(
            float(lat1 + lat_k[i - 1] +
                  float(vv[i] * daystep * 24 * 3600) / 1000 / 1.8535 / 60) / 2)
        lon_k.append(
            float(lon1 + lon_k[i - 1] +
                  float(uu[i] * daystep * 24 * 3600) / 1000 / 1.8535 / 60 *
                  scipy.cos(float(lat_k[i]) / 180 * np.pi)) / 2)

    return lat_k, lon_k, time
Exemplo n.º 8
0
def gettrack_FVCOM(depth, jdmat_m, lat_vel_1, lon_vel_1, u, v, lat_vel,
                   lon_vel, h_vel, siglay, startdate, numdays, daystep, la,
                   lo):
    """get the track for models"""

    # calculate the points near la,la
    (disth, index_location_h) = nearxy(lon_vel, lat_vel, lo, la)
    (distance, index_location) = nearxy(lon_vel_1, lat_vel_1, lo, la)
    # calculate all the depths
    depths = h_vel[index_location_h] * siglay[:, index_location_h]
    # make depths to be descending order
    new_depths = list(depths).reverse()
    # depths range
    range_depths = range(len(depths)).reverse()
    # calculate the index of depth in the depths
    idz = int(round(np.interp(depth, new_depths, range_depths)))
    print "the depth index is:", idz
    ####### get index of startdate in jdmat_m#####
    jdmat_m_num = []  # this is the data in the form of a number
    del_list, jdmat, del_index = [], [], []
    # convert array to list
    jdmat_m_list = [jdmat_m_i for jdmat_m_i in jdmat_m]
    # while seconds=60, can change the datetime to number, so delete

    for i in range(1, len(jdmat_m_list)):
        if jdmat_m_list[i][17:19] == '60' or jdmat_m_list[
                i - 1] == jdmat_m_list[i]:
            del_list.append(jdmat_m_list[i])
            del_index.append(i)  #get the index of deleted datetime
    jdmat = [val for val in jdmat_m_list
             if val not in del_list]  # delete the wrong value, jdmat just
    for i in jdmat:  # convert time to number
        jdmat_m_num.append(
            date2num(dt.datetime.strptime(i, '%Y-%m-%dT%H:%M:%S.%f')))

    index_startdate_1 = int(
        round(
            np.interp(date2num(startdate), jdmat_m_num,
                      range(len(jdmat_m_num)))))  #get the index of startdate
    print "the index of start date is:", index_startdate_1
    # calculate the delete index of time for u and v

    if del_index != []:
        index_add = int(
            np.ceil(
                np.interp(index_startdate_1, del_index,
                          range(len(del_index)))))
        index_startdate = index_add + index_startdate_1
    else:
        index_startdate = index_startdate_1

    # get u,v
    u1 = float(u[index_startdate, idz, index_location])
    v1 = float(v[index_startdate, idz, index_location])

    nsteps = scipy.floor(min(numdays, jdmat_m_num[-1]) / daystep)
    # get the velocity data at this first time & place
    lat_k = 'lat' + str(1)
    lon_k = 'lon' + str(1)
    uu, vv, lon_k, lat_k, time = [], [], [], [], []
    uu.append(u1)
    vv.append(v1)
    lat_k.append(la)
    lon_k.append(lo)
    time.append(date2num(startdate))
    delete_id = []

    for i in range(1, int(nsteps)):
        # first, estimate the particle move to its new position using velocity of previous time steps

        lat1 = lat_k[i - 1] + vv[i - 1] * daystep * 0.7769085
        lon1 = lon_k[i - 1] + uu[i - 1] * daystep * 0.7769085 * scipy.cos(
            lat_k[i - 1] / 180 * np.pi)
        # find the closest model time for the new timestep
        jdmat_m_num_i = time[i - 1] + daystep
        time.append(jdmat_m_num_i)

        index_startdate_1 = int(
            round(
                np.interp(jdmat_m_num_i, jdmat_m_num,
                          range(len(jdmat_m_num)))))
        if del_index != []:
            index_add = int(
                np.ceil(
                    np.interp(index_startdate_1, del_index,
                              range(len(del_index)))))
            index_startdate = index_add + index_startdate_1
        else:
            index_startdate = index_startdate_1
        #find the point's index of near lat1,lon1
        index_location = nearxy(lon_vel_1, lat_vel_1, lon1, lat1)[1]
        # calculate the model depth
        depth_model = getdepth(lat_k[i - 1], lon_k[i - 1])
        #get u and v
        ui = u[index_startdate, idz, index_location]
        vi = v[index_startdate, idz, index_location]
        vv.append(vi)
        uu.append(ui)
        # estimate the particle move from its new position using velocity of previous time steps

        lat_k.append((lat1 + lat_k[i - 1] + vv[i] * daystep * 0.7769085) / 2.)
        lon_k.append(
            (lon1 + lon_k[i - 1] +
             uu[i] * daystep * 0.7769085 * scipy.cos(lat_k[i] / 180 * np.pi)) /
            2.)

    if depth_model > 0:
        delete_id.append(i)
        #delete the depth is greater than 0
        delete_id.reverse()
        for i in delete_id:
            del lat_k[i]
            del lon_k[i]
            del time[i]
            del uu[i]
            del vv[i]
        if delete_id != []:
            del lat_k[-1]
            del lon_k[-1]
            del time[-1]
            del uu[-1]
            del vv[-1]
        return lat_k, lon_k, time, uu, vv
Exemplo n.º 9
0
def gettrack_codar(jdmat_m, lon_vel, lat_vel, u, v, startdate, numdays,
                   daystep, la, lo):
    """
    tracks particle at surface
    """
    # calculate the points near la,la
    distance, index_location = nearxy(lon_vel, lat_vel, lo, la)

    ####### get index of startdate in jdmat_m#####
    jdmat_m_num = []  # this is the date in the form of a number
    jdmat_m_list, jdmat = [], []
    # convert array to list
    for jdmat_m_i in jdmat_m:
        jdmat_m_list.append(jdmat_m_i)
    for i in jdmat_m_list:  # convert time to number
        jdmat_m_num.append(i)
    dts = date2num(datetime.datetime(2001, 1, 1, 0, 0, 0))
    jdmat_m = [i + dts for i in jdmat_m]
    index_startdate = int(
        round(np.interp(startdate, jdmat_m,
                        range(len(jdmat_m)))))  #get the index of startdate
    # get u,v
    u1 = float(u[index_startdate][index_location])
    v1 = float(v[index_startdate][index_location])
    if u1 == -999.0 / 100:  # case of no good data
        u1 = 0
        v1 = 0
    nsteps = scipy.floor(min(numdays, jdmat_m_num[-1]) / daystep)
    # get the velocity data at this first time & place
    lat_k = 'lat' + str(1)
    lon_k = 'lon' + str(1)
    uu, vv, lon_k, lat_k, time = [], [], [], [], []
    uu.append(u1)
    vv.append(v1)
    lat_k.append(la)
    lon_k.append(lo)
    time.append(startdate)

    for i in range(1, int(nsteps)):
        # first, estimate the particle move to its new position using velocity of previous time steps
        lat1 = lat_k[i - 1] + float(
            vv[i - 1] * daystep * 24 * 3600) / 1000 / 1.8535 / 60
        lon1 = lon_k[i - 1] + float(
            uu[i - 1] * daystep * 24 * 3600) / 1000 / 1.8535 / 60 * (
                scipy.cos(float(lat_k[i - 1])) / 180 * np.pi)
        # find the closest model time for the new timestep
        jdmat_m_num_i = time[i - 1] + daystep
        time.append(jdmat_m_num_i)
        #print jdmat_m_num_i

        index_startdate = int(
            round(
                np.interp(jdmat_m_num_i, jdmat_m_num,
                          range(len(jdmat_m_num)))))
        #find the point's index of near lat1,lon1
        index_location = nearxy(lon_vel, lat_vel, lon1, lat1)[1]
        ui = u[index_startdate][index_location]
        vi = v[index_startdate][index_location]
        #if u1<>-999.0/100:# case of good data
        vv.append(vi)
        uu.append(ui)
        # estimate the particle move from its new position using velocity of previous time steps
        lat_k.append(
            float(lat1 + lat_k[i - 1] +
                  float(vv[i] * daystep * 24 * 3600) / 1000 / 1.8535 / 60) / 2)
        lon_k.append(
            float(lon1 + lon_k[i - 1] +
                  float(uu[i] * daystep * 24 * 3600) / 1000 / 1.8535 / 60 *
                  scipy.cos(float(lat_k[i]) / 180 * np.pi)) / 2)
        #else:
        #  vv.append(0)
        #  uu.append(0)
        # estimate the particle move from its new position using velocity of previous time steps
        #  lat_k.append(float(lat1))
        #  lon_k.append(float(lon1))
    return lat_k, lon_k, time
Exemplo n.º 10
0
                for n in range(nsta):
                    # Only use if model cell is within 0.04 degree of requested
                    # location.
                    if dd[n] <= max_dist:
                        arr = a[istart:istop, j[n], i[n]].data
                        if arr.std() >= min_var:
                            c = mod_df(arr, timevar, istart, istop, mod_name,
                                       ts)
                            name = station_list[n]['long_name']
                            model_df[n] = pd.concat([model_df[n], c], axis=1)
                            model_df[n].name = name

            elif len(r) == 2:
                print('[Unstructured grid model]:', url)
                # Find the closest point from an unstructured grid model.
                index, dd = nearxy(lon.flatten(), lat.flatten(), obs_lon,
                                   obs_lat)
                for n in range(nsta):
                    # Only use if model cell is within 0.04 degree of requested
                    # location.
                    if dd[n] <= max_dist:
                        arr = a[istart:istop, index[n]].data
                        if arr.std() >= min_var:
                            c = mod_df(arr, timevar, istart, istop, mod_name,
                                       ts)
                            name = station_list[n]['long_name']
                            model_df[n] = pd.concat([model_df[n], c], axis=1)
                            model_df[n].name = name
            elif len(r) == 1:
                print('[Data]:', url)
    except (ValueError, RuntimeError, CoordinateNotFoundError,
            ConstraintMismatchError) as e:
Exemplo n.º 11
0
wis_lats = []
wis_lons = []
wis_stations = []
for station in location_data:
    wis_lats.append(location_data[station]['lat'])
    wis_lons.append(location_data[station]['lon'])
    wis_stations.append(station)

station_lats = []
station_lons = []
for station in st_list.keys():    
    if st_list[station]['hasObsData']:
        station_lats.append(st_list[station]['lat'])
        station_lons.append(st_list[station]['lon'])
        
ind, dd = nearxy(wis_lons, wis_lats, station_lons, station_lats)       

# Now get read the wis data
with open("./WIS_extremes.txt") as extremes_file:
    wis_extremes = json.load(extremes_file)

# Get the extremes from the closest station
wis_station_id = wis_stations[ind[0]]
wis_lat = wis_lats[ind[0]]
wis_lon = wis_lons[ind[0]]

wis_maximums = []
wis_directions = []
for year in wis_extremes[wis_station_id].keys():
    wis_maximums.append(wis_extremes[wis_station_id][year]['ws_at_max'])
    wis_directions.append(wis_extremes[wis_station_id][year]['wdir_at_max'])
Exemplo n.º 12
0
wis_lats = []
wis_lons = []
wis_stations = []
for station in location_data:
    wis_lats.append(location_data[station]['lat'])
    wis_lons.append(location_data[station]['lon'])
    wis_stations.append(station)

station_lats = []
station_lons = []
for station in st_list.keys():
    if st_list[station]['hasObsData']:
        station_lats.append(st_list[station]['lat'])
        station_lons.append(st_list[station]['lon'])

ind, dd = nearxy(wis_lons, wis_lats, station_lons, station_lats)

# Now get read the wis data
with open("./WIS_extremes.txt") as extremes_file:
    wis_extremes = json.load(extremes_file)

# Get the extremes from the closest station
wis_station_id = wis_stations[ind[0]]
wis_lat = wis_lats[ind[0]]
wis_lon = wis_lons[ind[0]]

wis_maximums = []
wis_directions = []
for year in wis_extremes[wis_station_id].keys():
    wis_maximums.append(wis_extremes[wis_station_id][year]['ws_at_max'])
    wis_directions.append(wis_extremes[wis_station_id][year]['wdir_at_max'])
Exemplo n.º 13
0
def model_plot_track(
    depth, jdmat_m, lat_vel_1, lon_vel_1, u, v, lat_vel, lon_vel, h_vel, siglay, startdate, numdays, daystep, x, la, lo
):
    fig = plt.figure(1)
    if len(depth) == 1:
        panels = len(startdate)
    if len(depth) != 1:
        panels = len(depth)
    for j in range(1, panels + 1):
        print int(str(panels) + str(2) + str(j))
        if panels == 1:
            ax = fig.add_subplot(111)
        else:
            ax = fig.add_subplot(int(str(2) + str(2) + str(j)))
        if j == 3:
            plt.xlabel("Longitude W")
        if j == 1:
            plt.ylabel("Latitude N")
        for k in range(len(la)):
            for m in range(len(lo)):
                (distance, index_location) = nearxy(lon_vel_1, lat_vel_1, lo[m], la[k])
                (disth, index_location_h) = nearxy(lon_vel, lat_vel, lo[m], la[k])

                depths = h_vel[index_location_h] * siglay[:, index_location_h]
                # make depths to be descending order
                new_depths = list(depths)
                new_depths.reverse()
                # depths range
                range_depths = range(len(depths))
                range_depths.reverse()
                if len(depth) == 1:
                    idz = 0
                else:
                    idz = int(round(np.interp(depth[j - 1], new_depths, range_depths)))

                ####### get index of startdate in jdmat_m
                jdmat_m_num = []  # this is the data in the form of a number
                del_list, jdmat, del_index = [], [], []
                # convert array to list
                jdmat_m_list = [jdmat_m_i for jdmat_m_i in jdmat_m]

                # while seconds=60, can change the datetime to number, so delete
                for i in range(1, len(jdmat_m_list)):
                    if jdmat_m_list[i][17:19] == "60" or jdmat_m_list[i - 1] == jdmat_m_list[i]:
                        del_list.append(jdmat_m_list[i])
                        del_index.append(i)  # get the index of deleted datetime
                jdmat = [val for val in jdmat_m_list if val not in del_list]  # delete the wrong value, jdmat just
                for i in jdmat:  # convert time to number
                    jdmat_m_num.append(date2num(dt.datetime.strptime(i, "%Y-%m-%dT%H:%M:%S.%f")))
                # get index of startdate in jdmat_m_num
                index_startdate_1 = int(
                    round(np.interp(date2num(startdate[j - 1]), jdmat_m_num, range(len(jdmat_m_num))))
                )
                if del_index != []:
                    index_add = int(np.ceil(np.interp(index_startdate_1, del_index, range(len(del_index)))))
                    index_startdate = index_add + index_startdate_1
                else:
                    index_startdate = index_startdate_1
                # get u,v
                u1 = float(u[index_startdate, idz, index_location])
                v1 = float(v[index_startdate, idz, index_location])
                nsteps = scipy.floor(min(numdays, jdmat_m_num[-1]) / daystep)
                # get the velocity data at this first time & place
                lat_k = "lat" + str(k)
                lon_k = "lon" + str(k)
                uu, vv, lon_k, lat_k, time = [], [], [], [], []
                uu.append(u1)
                vv.append(v1)
                lat_k.append(la[k])
                lon_k.append(lo[m])
                time.append(date2num(startdate[j - 1]))

            for i in range(1, int(nsteps)):
                # first, estimate the particle move to its new position using velocity of previous time steps
                lat1 = lat_k[i - 1] + float(vv[i - 1] * daystep * 24 * 3600) / 1000 / 1.8535 / 60
                lon1 = lon_k[i - 1] + float(uu[i - 1] * daystep * 24 * 3600) / 1000 / 1.8535 / 60 * (
                    scipy.cos(float(lat_k[i - 1])) / 180 * np.pi
                )
                # find the closest model time for the new timestep
                jdmat_m_num_i = time[i - 1] + daystep
                time.append(jdmat_m_num_i)

                index_startdate_1 = int(round(np.interp(jdmat_m_num_i, jdmat_m_num, range(len(jdmat_m_num)))))
                if del_index != []:
                    index_add = int(np.ceil(np.interp(index_startdate_1, del_index, range(len(del_index)))))
                    index_startdate = index_add + index_startdate_1
                else:
                    index_startdate = index_startdate_1
                # find the point's index of near lat1,lon1
                index_location = nearxy(lon_vel_1, lat_vel_1, lon1, lat1)[1]
                ui = u[index_startdate, 1, index_location]
                vi = v[index_startdate, 1, index_location]
                vv.append(vi)
                uu.append(ui)
                # estimate the particle move from its new position using velocity of previous time steps
                lat_k.append(float(lat1 + lat_k[i - 1] + float(vv[i] * daystep * 24 * 3600) / 1000 / 1.8535 / 60) / 2)
                lon_k.append(
                    float(
                        lon1
                        + lon_k[i - 1]
                        + float(uu[i] * daystep * 24 * 3600)
                        / 1000
                        / 1.8535
                        / 60
                        * scipy.cos(float(lat_k[i]) / 180 * np.pi)
                    )
                    / 2
                )

            plt.plot(lon_k, lat_k, "-", linewidth=1, marker=".", markerfacecolor="r")
            basemap([min(lat_k) - 2, max(lat_k) + 2], [min(lon_k) - 2, max(lon_k) + 2])
            # make same size for the panels
            min_lat, max_lon, max_lat, min_lon = [], [], [], []
            min_lat.append(min(lat_k))
            max_lat.append(max(lat_k))
            min_lon.append(min(lon_k))
            max_lon.append(max(lon_k))

            text1 = ax.annotate(
                str(num2date(time[0]).month) + "-" + str(num2date(time[0]).day),
                xy=(lon_k[0], lat_k[0]),
                xycoords="data",
                xytext=(8, 10),
                textcoords="offset points",
                arrowprops=dict(arrowstyle="->"),
            )
            text1.draggable()
            text1 = ax.annotate(
                str(num2date(time[-1]).month) + "-" + str(num2date(time[-1]).day),
                xy=(lon_k[-1], lat_k[-1]),
                xycoords="data",
                xytext=(8, 10),
                textcoords="offset points",
                arrowprops=dict(arrowstyle="->"),
            )

        # set the numbers formatter
        majorFormatter = FormatStrFormatter("%.2f")
        ax.yaxis.set_major_formatter(majorFormatter)
        ax.xaxis.set_major_formatter(majorFormatter)
        if len(depth) == 1:
            plt.title(str(num2date(time[i - 1]).year))
        else:
            plt.title(str(num2date(time[i - 1]).year) + " year" + " depth: " + str(depth[j - 1]))

        ax.xaxis.set_label_coords(0.5, -0.005)  # set the position of the xlabel
        ax.yaxis.set_label_coords(-0.1, 0.5)
        plt.xlim([min(min_lon), max(max_lon)])
        plt.ylim([min(min_lat), max(max_lat)])
    plt.show()
Exemplo n.º 14
0
def gettrack_FVCOM(
    depth, jdmat_m, lat_vel_1, lon_vel_1, u, v, lat_vel, lon_vel, h_vel, siglay, startdate, numdays, daystep, la, lo
):
    """get the track for models"""

    # calculate the points near la,la
    (disth, index_location_h) = nearxy(lon_vel, lat_vel, lo, la)
    (distance, index_location) = nearxy(lon_vel_1, lat_vel_1, lo, la)
    # calculate all the depths
    depths = h_vel[index_location_h] * siglay[:, index_location_h]
    # make depths to be descending order
    new_depths = list(depths).reverse()
    # depths range
    range_depths = range(len(depths)).reverse()
    # calculate the index of depth in the depths
    idz = int(round(np.interp(depth, new_depths, range_depths)))
    print "the depth index is:", idz
    ####### get index of startdate in jdmat_m#####
    jdmat_m_num = []  # this is the data in the form of a number
    del_list, jdmat, del_index = [], [], []
    # convert array to list
    jdmat_m_list = [jdmat_m_i for jdmat_m_i in jdmat_m]
    # while seconds=60, can change the datetime to number, so delete

    for i in range(1, len(jdmat_m_list)):
        if jdmat_m_list[i][17:19] == "60" or jdmat_m_list[i - 1] == jdmat_m_list[i]:
            del_list.append(jdmat_m_list[i])
            del_index.append(i)  # get the index of deleted datetime
    jdmat = [val for val in jdmat_m_list if val not in del_list]  # delete the wrong value, jdmat just
    for i in jdmat:  # convert time to number
        jdmat_m_num.append(date2num(dt.datetime.strptime(i, "%Y-%m-%dT%H:%M:%S.%f")))

    index_startdate_1 = int(
        round(np.interp(date2num(startdate), jdmat_m_num, range(len(jdmat_m_num))))
    )  # get the index of startdate
    print "the index of start date is:", index_startdate_1
    # calculate the delete index of time for u and v

    if del_index != []:
        index_add = int(np.ceil(np.interp(index_startdate_1, del_index, range(len(del_index)))))
        index_startdate = index_add + index_startdate_1
    else:
        index_startdate = index_startdate_1

    # get u,v
    u1 = float(u[index_startdate, idz, index_location])
    v1 = float(v[index_startdate, idz, index_location])

    nsteps = scipy.floor(min(numdays, jdmat_m_num[-1]) / daystep)
    # get the velocity data at this first time & place
    lat_k = "lat" + str(1)
    lon_k = "lon" + str(1)
    uu, vv, lon_k, lat_k, time = [], [], [], [], []
    uu.append(u1)
    vv.append(v1)
    lat_k.append(la)
    lon_k.append(lo)
    time.append(date2num(startdate))
    delete_id = []

    for i in range(1, int(nsteps)):
        # first, estimate the particle move to its new position using velocity of previous time steps

        lat1 = lat_k[i - 1] + vv[i - 1] * daystep * 0.7769085
        lon1 = lon_k[i - 1] + uu[i - 1] * daystep * 0.7769085 * scipy.cos(lat_k[i - 1] / 180 * np.pi)
        # find the closest model time for the new timestep
        jdmat_m_num_i = time[i - 1] + daystep
        time.append(jdmat_m_num_i)

        index_startdate_1 = int(round(np.interp(jdmat_m_num_i, jdmat_m_num, range(len(jdmat_m_num)))))
        if del_index != []:
            index_add = int(np.ceil(np.interp(index_startdate_1, del_index, range(len(del_index)))))
            index_startdate = index_add + index_startdate_1
        else:
            index_startdate = index_startdate_1
        # find the point's index of near lat1,lon1
        index_location = nearxy(lon_vel_1, lat_vel_1, lon1, lat1)[1]
        # calculate the model depth
        depth_model = getdepth(lat_k[i - 1], lon_k[i - 1])
        # get u and v
        ui = u[index_startdate, idz, index_location]
        vi = v[index_startdate, idz, index_location]
        vv.append(vi)
        uu.append(ui)
        # estimate the particle move from its new position using velocity of previous time steps

        lat_k.append((lat1 + lat_k[i - 1] + vv[i] * daystep * 0.7769085) / 2.0)
        lon_k.append((lon1 + lon_k[i - 1] + uu[i] * daystep * 0.7769085 * scipy.cos(lat_k[i] / 180 * np.pi)) / 2.0)

    if depth_model > 0:
        delete_id.append(i)
        # delete the depth is greater than 0
        delete_id.reverse()
        for i in delete_id:
            del lat_k[i]
            del lon_k[i]
            del time[i]
            del uu[i]
            del vv[i]
        if delete_id != []:
            del lat_k[-1]
            del lon_k[-1]
            del time[-1]
            del uu[-1]
            del vv[-1]
        return lat_k, lon_k, time, uu, vv
Exemplo n.º 15
0
def getmodel(stime, etime, mlon, mlat, depth_i, modsdate, modedate, dataset):  # after getmodel_GOMPOM_step1

    tri = dataset["nv"]
    jdmat_m = dataset["Times"]
    while (stime < date2num(modsdate)) or (etime > date2num(modedate)):
        print "sorry no model data available for that time"
        sys.exit(0)
    u = dataset["u"]
    v = dataset["v"]
    # zeta=dataset['zeta']
    h = dataset["h"]
    siglay = dataset["siglay"]
    lat_array = dataset["lat"]
    lon_array = dataset["lon"]
    # get the lat & lon
    lat_vel = [i for i in lat_array]
    lon_vel = [i for i in lon_array]
    # get the tri0,tri1,tri2 as the location of three points
    tri0 = [i for i in tri[0]]
    tri1 = [i for i in tri[1]]
    tri2 = [i for i in tri[2]]

    h_vel = [h_i for h_i in h]

    lat_vel_1, lon_vel_1 = [], []
    for i in range(len(tri0)):
        if tri1[i] == len(lat_vel):
            tri1[i] = len(lat_vel) - 1
        if tri2[i] == len(lat_vel):
            tri2[i] = len(lat_vel) - 1
        lat_vel_1.append(float((lat_vel[tri0[i]] + lat_vel[tri1[i]] + lat_vel[tri2[i]])) / float(3))
        lon_vel_1.append(float((lon_vel[tri0[i]] + lon_vel[tri1[i]] + lon_vel[tri2[i]])) / float(3))
    # get the min distance
    # if url=='http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3':
    #    (distance,index_location) = nearxy(lon_vel,lat_vel,mlon,mlat)
    # else:
    print "finding index of this location"
    (distance, index_location) = nearxy(lon_vel_1, lat_vel_1, mlon, mlat)
    (disth, index_location_h) = nearxy(lon_vel, lat_vel, mlon, mlat)

    print "making list of jdmat"
    jdmat_m_list = list(jdmat_m)
    print "removing the bad data from jdmat"
    del_list, jdmat, del_index = [], [], []
    for i in range(len(jdmat_m_list)):
        if jdmat_m_list[i][17:19] == "60":  # delete the wrong data
            del_list.append(jdmat_m_list[i])
            del_index.append(i)
    jdmat = [val for val in jdmat_m_list if val not in del_list]
    jdmat_m_num = []  # this is the data in the form of a number
    print "reformatting jdmat using strptime"
    for i in jdmat:
        jdmat_m_num.append(date2num(dt.datetime.strptime(i, "%Y-%m-%dT%H:%M:%S.%f")))

    depths = h_vel[index_location_h] * siglay[:, index_location_h]
    print "found the depths of the model at this location"

    # get the ids of depths
    depths_list = [depths[id] for id in range(0, len(depths))]
    depths_list.reverse()
    ids = range(len(depths_list))
    ids.reverse()
    idz = int(round(np.interp(depth_i, depths_list, ids)))

    print "finding index of this date"
    index_sdate = int(round(np.interp(stime, jdmat_m_num, range(len(jdmat_m_num)))))
    index_edate = int(round(np.interp(etime, jdmat_m_num, range(len(jdmat_m_num)))))

    del_index_idz = [i for i in range(len(del_index))]

    if del_index_idz:
        index_u_sdate = int(np.ceil(np.interp(index_sdate, del_index, del_index_idz)))
        index_u_edate = int(np.ceil(np.interp(index_edate, del_index, del_index_idz)))
    else:
        index_u_sdate = 0
        index_u_edate = 0

    index_sdate1 = index_u_sdate + index_sdate
    index_edate1 = index_u_edate + index_edate
    v_part = v[index_sdate1:index_edate1, idz, index_location]
    u_part = u[index_sdate1:index_edate1, idz, index_location]
    time1 = num2date(jdmat_m_num[index_sdate:index_edate])

    del_index.reverse()

    v_list = [i for i in v_part]
    u_list = [i for i in u_part]

    # beginning of each month time is same, so delete
    del_index_time = []
    for i in range(1, len(time1)):
        if time1[i - 1] == time1[i]:
            del_index_time.append(i)
    if del_index_time:
        del u_list[del_index_time[0]]
        del v_list[del_index_time[0]]
        del time1[del_index_time[0]]
    print "done getmodel function"
    return time1, u_list, v_list, lat_vel, lon_vel
                            arr = np.sqrt(u_arr ** 2 + v_arr ** 2)
                            if u_arr.std() >= min_var:
                                c = mod_df(arr, timevar, istart, istop, mod_name, ts)
                                name = obs_df[n].name
                                model_df[n] = pd.concat([model_df[n], c], axis=1)
                                model_df[n].name = name
                            else:
                                print "min_var error"
                        else:
                            print "Max dist error"
                if len(r) == 3:
                    # NECOFS
                    print ("[Structured grid model]:", url)
                    d = u[0, 0:1, :].data
                    # Find the closest non-land point from a structured grid model.
                    index, dd = nearxy(lon.flatten(), lat.flatten(), obs_lon, obs_lat)

                    # Keep the lat lon of the grid point
                    model_lat = lat[index].tolist()
                    model_lon = lon[index].tolist()

                    for n in range(nsta):
                        # Only use if model cell is within max_dist of station
                        if dd[n] <= max_dist:
                            u_arr = u[istart:istop, 0:1, index[n]].data
                            v_arr = v[istart:istop, 0:1, index[n]].data
                            # Model data is in m/s so convert to cm/s
                            arr = np.sqrt((u_arr * 100.0) ** 2 + (v_arr * 100.0) ** 2)
                            if u_arr.std() >= min_var:
                                c = mod_df(arr, timevar, istart, istop, mod_name, ts)
                                name = obs_df[n].name
Exemplo n.º 17
0
for kk in range(len(obs_temp)):
    obs_temp[kk] = f2c(obs_temp[kk])  # converts to Celcius

# now get the model output
urlfvcom = 'http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3'
nc = netCDF4.Dataset(urlfvcom)
nc.variables
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
times = nc.variables['time']
jd = netCDF4.num2date(times[:], times.units)
vname = 'temp'
var = nc.variables[vname]

# find nearest point to desired location and time
inode = nearxy(lon, lat, loni, lati)
index = netCDF4.date2index(
    [obs_dt[0], obs_dt[-1]], times,
    select='nearest')  #find the model time index at start & end pf obs

#plot them
fig = plt.figure(figsize=(16, 4))
ax = fig.add_subplot(111)
ax.plot_date(obs_dt, obs_temp, fmt='-')
plt.grid()
ax.plot_date(jd[index[0]:index[1]],
             var[index[0]:index[1], 44, inode],
             fmt='-',
             color='red')
plt.ylabel(var.units)
plt.title('eMOLT site ' + site + ' temp vs FVCOM ' +
Exemplo n.º 18
0
def getmodel(stime, etime, mlon, mlat, depth_i, modsdate, modedate,
             dataset):  # after getmodel_GOMPOM_step1

    tri = dataset['nv']
    jdmat_m = dataset['Times']
    while (stime < date2num(modsdate)) or (etime > date2num(modedate)):
        print "sorry no model data available for that time"
        sys.exit(0)
    u = dataset['u']
    v = dataset['v']
    # zeta=dataset['zeta']
    h = dataset['h']
    siglay = dataset['siglay']
    lat_array = dataset['lat']
    lon_array = dataset['lon']
    #get the lat & lon
    lat_vel = [i for i in lat_array]
    lon_vel = [i for i in lon_array]
    # get the tri0,tri1,tri2 as the location of three points
    tri0 = [i for i in tri[0]]
    tri1 = [i for i in tri[1]]
    tri2 = [i for i in tri[2]]

    h_vel = [h_i for h_i in h]

    lat_vel_1, lon_vel_1 = [], []
    for i in range(len(tri0)):
        if tri1[i] == len(lat_vel):
            tri1[i] = len(lat_vel) - 1
        if tri2[i] == len(lat_vel):
            tri2[i] = len(lat_vel) - 1
        lat_vel_1.append(
            float((lat_vel[tri0[i]] + lat_vel[tri1[i]] + lat_vel[tri2[i]])) /
            float(3))
        lon_vel_1.append(
            float((lon_vel[tri0[i]] + lon_vel[tri1[i]] + lon_vel[tri2[i]])) /
            float(3))

# get the min distance
# if url=='http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3':
#    (distance,index_location) = nearxy(lon_vel,lat_vel,mlon,mlat)
#else:
    print 'finding index of this location'
    (distance, index_location) = nearxy(lon_vel_1, lat_vel_1, mlon, mlat)
    (disth, index_location_h) = nearxy(lon_vel, lat_vel, mlon, mlat)

    print 'making list of jdmat'
    jdmat_m_list = list(jdmat_m)
    print 'removing the bad data from jdmat'
    del_list, jdmat, del_index = [], [], []
    for i in range(len(jdmat_m_list)):
        if jdmat_m_list[i][17:19] == '60':  #delete the wrong data
            del_list.append(jdmat_m_list[i])
            del_index.append(i)
    jdmat = [val for val in jdmat_m_list if val not in del_list]
    jdmat_m_num = []  # this is the data in the form of a number
    print 'reformatting jdmat using strptime'
    for i in jdmat:
        jdmat_m_num.append(
            date2num(dt.datetime.strptime(i, '%Y-%m-%dT%H:%M:%S.%f')))

    depths = h_vel[index_location_h] * siglay[:, index_location_h]
    print 'found the depths of the model at this location'

    #get the ids of depths
    depths_list = [depths[id] for id in range(0, len(depths))]
    depths_list.reverse()
    ids = range(len(depths_list))
    ids.reverse()
    idz = int(round(np.interp(depth_i, depths_list, ids)))

    print 'finding index of this date'
    index_sdate = int(
        round(np.interp(stime, jdmat_m_num, range(len(jdmat_m_num)))))
    index_edate = int(
        round(np.interp(etime, jdmat_m_num, range(len(jdmat_m_num)))))

    del_index_idz = [i for i in range(len(del_index))]

    if del_index_idz:
        index_u_sdate = int(
            np.ceil(np.interp(index_sdate, del_index, del_index_idz)))
        index_u_edate = int(
            np.ceil(np.interp(index_edate, del_index, del_index_idz)))
    else:
        index_u_sdate = 0
        index_u_edate = 0

    index_sdate1 = index_u_sdate + index_sdate
    index_edate1 = index_u_edate + index_edate
    v_part = v[index_sdate1:index_edate1, idz, index_location]
    u_part = u[index_sdate1:index_edate1, idz, index_location]
    time1 = num2date(jdmat_m_num[index_sdate:index_edate])

    del_index.reverse()

    v_list = [i for i in v_part]
    u_list = [i for i in u_part]

    #beginning of each month time is same, so delete
    del_index_time = []
    for i in range(1, len(time1)):
        if time1[i - 1] == time1[i]:
            del_index_time.append(i)
    if del_index_time:
        del u_list[del_index_time[0]]
        del v_list[del_index_time[0]]
        del time1[del_index_time[0]]
    print 'done getmodel function'
    return time1, u_list, v_list, lat_vel, lon_vel