예제 #1
0
def checkifdzexists(video_id, skip_frames, skip_row, skip_col, mintol, sigma,
                    filter_size, startF, stopF, diff_frames):
    """
    checks if the dz_array is already computed and return dz_id if it does and
    returns none otherwise

    """
    print("checking if dz already exists..")
    db = labdb.LabDB()
    # check if this dz array has already been computed?
    if diff_frames is None:
        diff_frames = "NULL"
    sql = """SELECT dz_id FROM dz WHERE video_id=%d AND skip_frames=%d \
            AND skip_row = %d AND skip_col = %d AND mintol=%d AND sigma=%.1f \
            AND filter_size=%d AND startF=%d AND stopF=%d AND diff_frames=%s""" %\
            (video_id,skip_frames,skip_row,skip_col,mintol,sigma,filter_size,startF,stopF,diff_frames)

    rows = db.execute(sql)
    if (len(rows) > 0):
        # dz array already computed
        dz_id = rows[0][0]
        print("Loading cached dz %d..." % dz_id)
        # load the array from the disk
        dz_path = "/data/dz/%d" % dz_id
        dz_filename = dz_path + '/' + 'dz.nc'

        if not os.path.exists(dz_filename):
            print("file", dz_filename, "not found.")
            return None

        #dz_array = numpy.load(dz_filename)
        #loading the nc file
        print(dz_filename)
        nc = netCDF4.Dataset(dz_filename, 'a')
        # some information about the nc file
        print("dimensions of nc file -> ", list(nc.dimensions.keys()))
        print("variables of nc file -> ", list(nc.variables.keys()))
        # loading the data
        dz_arr = nc.variables['dz_array']
        nt, nrows, ncols = dz_arr.shape

        print("shape of nc file -> ", dz_arr.shape)

        if 'calculation_complete' not in nc.ncattrs(
        ) or not nc.calculation_complete:
            # previous dz_array was not completed computed => redo.
            print("Previous dz_array was not complete -- redo")
            return None

        return dz_id
    else:
        return None
예제 #2
0
def compute_dz(video_id, skip_frames=10):
    """
    Given video_id, calculate the dz array. Output is cached on disk.

    returns the array dz

    skip_frames is the number of frames to jump before computing dz
    """

    print("skip_frames is", skip_frames)
    db = labdb.LabDB()

    # check if this dz array has already been computed?
    sql = """SELECT dz_id 
             FROM dz 
             WHERE video_id = %d AND skip_frames = %d""" % (video_id,
                                                            skip_frames)
    rows = db.execute(sql)
    if len(rows) > 0:
        # dz array already computed
        dz_id = rows[0][0]
        print("Loading cached dz %d..." % dz_id)
        # load the array from the disk
        dz_path = "/Volumes/HD3/dz/%d" % dz_id
        dz_filename = os.path.join(dz_path, "dz.npy")
        dz_array = numpy.load(dz_filename)
        return dz_array

    # Need to compute dz for the first time

    # Set path to the two images
    path = "/Volumes/HD3/video_data/%d/frame%05d.png"
    path2time = "/Volumes/HD3/video_data/%d/time.txt" % video_id

    #declare the array to hold the value of dz for every image
    dz_array = []
    n = 5
    count = n + 1
    filename1 = path % (video_id, count - n)
    filename2 = path % (video_id, count)

    image1 = numpy.array(Image.open(filename1))
    image2 = numpy.array(Image.open(filename2))

    H = 52.0
    dz = H / image1.shape[0]

    mintol = 15
    C = getTol(image1, mintol=mintol)
    delz = compute_dz_image(image1, image2, dz)
    delz = numpy.nan_to_num(delz) * C
    dz_array = generate_dz_array(delz, dz_array)

    vmax = 0.01
    """ old ploting code
    fig = pylab.figure()
    ax = fig.add_subplot(111)
    img = pylab.imshow(delz, interpolation='nearest', vmin=-vmax, vmax=vmax,
                    animated=False, label='delz', aspect='auto')
    pylab.colorbar()
    pylab.show(block=False) """

    from scipy.ndimage import gaussian_filter
    from numpy import ma

    sql = """SELECT num_frames FROM video
             WHERE video_id = %d""" % video_id
    rows = db.execute(sql)
    num_frames = rows[0][0]

    index = 0
    while True:
        print("render frame %d of %d" % (count, num_frames))
        index += 1

        filename1 = path % (video_id, count - n)
        filename2 = path % (video_id, count)

        if not os.path.exists(filename2):
            break

        image1 = numpy.array(Image.open(filename1))
        image2 = numpy.array(Image.open(filename2))

        C = getTol(image1, mintol=mintol)
        delz = compute_dz_image(image1, image2, dz)
        delz = numpy.nan_to_num(delz) * C
        #delz_m = ma.masked_where(C==False, delz)
        dz_array = generate_dz_array(delz, dz_array)

        # clip large values
        bound = 1.0
        delz[delz > bound] = bound
        delz[delz < -bound] = -bound

        # fill in missing values
        filt_delz = ndimage.gaussian_filter(delz, (21, 21))
        i = abs(delz) > 1e-8
        filt_delz[i] = delz[i]

        # smooth
        filt_delz = ndimage.gaussian_filter(filt_delz, 7)

        # Old ploting
        """ img.set_data(filt_delz)
        fig.canvas.draw()
        ax.set_title('n = %d' % count)"""

        count += skip_frames
        time.sleep(0.1)

    dz_array = numpy.array(dz_array)
    print("final dz_array.shape", dz_array.shape)
    print(dz_array)

    # cache dz_array to disk
    sql = """INSERT INTO dz (video_id, skip_frames)
             VALUES (%d, %d)""" % (video_id, skip_frames)
    print(sql)
    db.execute(sql)
    sql = """SELECT LAST_INSERT_ID()"""
    rows = db.execute(sql)
    dz_id = rows[0][0]

    dz_path = "/Volumes/HD3/dz/%d" % dz_id
    os.mkdir(dz_path)

    dz_filename = os.path.join(dz_path, "dz.npy")
    numpy.save(dz_filename, dz_array)
    db.commit()

    return dz_array
예제 #3
0
def createncfile(dz_id,t,x,z,
        a_xi_id = None
        ):
    """ 
    create the nc file in which we will store a_xi array.
    create a row in the database for the nc file stored
    update the database

    Axi_id will be chosen as the next available id if a_xi_id is None.
    """
    print("inside createncfile function")
    db = labdb.LabDB()
    #create the directory in which to store the nc file
    if a_xi_id is None:
        sql = """INSERT into vertical_displacement_amplitude (dz_id) VALUES (%d)"""%(dz_id)  
        print(sql)
        db.execute(sql)
        sql = """SELECT LAST_INSERT_ID()""" 
        rows = db.execute(sql)
        a_xi_id = rows[0][0]
    else:
        # this is incase cache is set to False and the nc file needs to be
        # recomputed again
        sql = "SELECT dz_id FROM vertical_displacement_amplitude WHERE a_xi_id = %s" % a_xi_id
        previous_dz_id, = db.execute_one(sql)
        if previous_dz_id != dz_id:
            print("dz_id, a_xi_id mismatch!")
            return None

    a_xi_path = "/data/vertical_displacement_amplitude/%d" % a_xi_id 
    if not os.path.exists(a_xi_path):
        os.mkdir(a_xi_path)

    a_xi_filename = os.path.join(a_xi_path,"a_xi.nc")
    print("A xi : ",a_xi_filename)

    # open the dz file corresponding to the vertical displacement nc file so as
    # to get info about the number of rows and column.
    dzncfile = netCDF4.Dataset('/data/dz/%d/dz.nc' % dz_id)
    Nrow = dzncfile.variables['row'].size
    Ncol = dzncfile.variables['column'].size
    dzncfile.close()

    # Declare the axi nc file for the first time
    nc = netCDF4.Dataset(a_xi_filename,'w',format = 'NETCDF4')
    row_dim = nc.createDimension('row',Nrow)
    col_dim = nc.createDimension('column',Ncol)
    lenT=t.shape[0]  #lenT is the length of the dz file
    
    # changing time to underfined as the variable is set to being contiguous
    # despite setting contiguous=False which is also the default setting
    #print "time axis  length",lenT     # debug info
    t_dim = nc.createDimension('time',lenT)
    #t_dim = nc.createDimension('time', None)

    # Dimensions are also variable
    ROW = nc.createVariable('row',numpy.float32,('row'),contiguous=False)
    print(ROW.shape,ROW.dtype)
    COLUMN = nc.createVariable('column',numpy.float32,('column'),contiguous=False)
    print(COLUMN.shape, COLUMN.dtype)
    TIME = nc.createVariable('time',numpy.float32,('time'),contiguous=False)
    print(TIME.shape, TIME.dtype)
    
    # chunk the data intelligently
    valSize = numpy.float32().itemsize
    chunksizes = chunk_shape_3D( ( lenT, Nrow, Ncol), valSize=valSize )

    # declare the 3D data variable 
    a_xi = nc.createVariable('a_xi_array',numpy.float32,('time','row','column'),
            chunksizes = chunksizes)
    print(list(nc.dimensions.keys()) ,a_xi.shape,a_xi.dtype)

    # assign the values
    
    #TIME[:] = t[:]
    ROW[:] = z
    COLUMN[:] = x

    nc.close()
    db.commit()
    return a_xi_id,a_xi_filename
예제 #4
0
def compute_a_xi(dz_id, cache=True):
    """
        Given an dz_id, compute the vertical displacement Axi
    """
    # access database
    db = labdb.LabDB()

    #check if the dataset already exists
    sql = """SELECT a_xi_id FROM vertical_displacement_amplitude WHERE\
             dz_id = %d""" % (dz_id)
    rows = db.execute(sql)

    a_xi_id = None
    if len(rows) > 0:
    
        # A xi array already computed
        a_xi_id = rows[0][0]

        a_xi_path = "/data/vertical_displacement_amplitude/%d/" % a_xi_id
        a_xi_filename = a_xi_path + 'a_xi.nc'

        if os.path.exists(a_xi_filename) and cache:
            return a_xi_id
        else:
            # delete a_xi.nc if it exists and recreate
            if os.path.exists(a_xi_filename):
                os.unlink(a_xi_filename)
    
    #  open the dataset dz.nc for calculating a_xi
    filepath = "/data/dz/%d/dz.nc"  % dz_id
    print("dz filepath: WC.py" , filepath) 
    if not os.path.exists(filepath):
        print(filepath, "not found")
        return
    nc = netCDF4.Dataset(filepath,'r')
    
    # loading the dz data from the nc file
    dz = nc.variables['dz_array']
    t = nc.variables['time']
    z = nc.variables['row']
    x = nc.variables['column']
    # print information about dz dataset
    print("variables  of the nc file :", list(nc.variables.keys()))
    print("dz shape : " , dz.shape)
    
    # call get_info function from Energy_flux program :: to get info
    sql = """ SELECT expt_id  FROM dz WHERE dz_id = %d """ % dz_id
    rows = db.execute(sql)
    expt_id = rows[0][0]

    vid_id, N, omega,kz,theta = Energy_flux.get_info(expt_id)

    print("V_ID:",  vid_id,"\n N:", N, "\n OMEGA: ", omega,"\n kz:",kz,"\n theta : ", theta)
    # calculate dt
    dt = numpy.mean(numpy.diff(t))
    print("dt of delta z:",dt)
    
    #call the function to create the nc file in which we are going to store the dz array
    a_xi_id,a_xi_filename = createncfile(dz_id,t,x,z,a_xi_id=a_xi_id)
    
    #get info about the nc file to see if the var is contiguous
#    print " info axi nc file :  chk if the var is contiguous"
#    os.system('ncdump -h -s %s' % a_xi_filename)
    
    # open the a_xi nc file for appending data
    axi_nc=netCDF4.Dataset(a_xi_filename,'a')
    a_xi = axi_nc.variables['a_xi_array']
    # setting the time axis
    axi_time = axi_nc.variables['time']
    axi_time[:] = t[:]

    # implementing the new correction... AXI refers to Afa... a change that should be
    # implemented across the database and the library labtools
    #dN2t_to_axi = -1.0 /( omega* N * N * kz)
    dN2t_to_axi = 1.0/((omega/N)**2 * kz * (1.0-(omega/N)**2)**-0.5)
    print("dN2t_to_axi::", dN2t_to_axi)

    widgets = [progressbar.Percentage(), ' ', progressbar.Bar(), ' ', progressbar.ETA()]
    pbar = progressbar.ProgressBar(widgets=widgets, maxval=dz.shape[0]).start()
    for num in range(dz.shape[0]):
        pbar.update(num)
        var1 = dN2t_to_axi * dz[num,:,:]/ (N**3)  # The var dz is dn2t which should be non dimensionalized in SyntheticSchlieren.py
        append2ncfile(a_xi,var1,num)
    pbar.finish()

    axi_nc.close()
    nc.close()
    return a_xi_id
예제 #5
0
def compute_dz(video_id,
               min_tol,
               sigma,
               filter_size,
               skip_frames=1,
               skip_row=1,
               skip_col=1,
               startF=0,
               stopF=None,
               diff_frames=1,
               cache=True):
    """
    > Given video_id, calculate the dz array. Output is cached on disk.
    > returns the array dz
    > skip_frames is the number of frames to jump before computing dz

    Returns dz_id
    """
    db = labdb.LabDB()

    #check if the dz file already exists. function checkifdzexists() returns
    #dz_id if the dz file exists else returns 0
    #dz_flag = checkifdzexists(video_id,skip_frames)

    # get the number of frames if stopF is unspecified
    if (stopF is None):
        sql = """ SELECT num_frames FROM video WHERE video_id = %d""" % video_id
        rows = db.execute(sql)
        stopF = rows[0][0]
        print("stop_frames = ", stopF)
    num_frames = stopF - startF
    print("num_frames:", num_frames)

    dz_id = checkifdzexists(video_id, skip_frames, skip_row, skip_col, min_tol,
                            sigma, filter_size, startF, stopF, diff_frames)
    if (dz_id is not None) and cache:
        return dz_id

    # Create the dz nc file to write data to
    dz_filename,dz_id,dt,dz,dx,chunkshape =create_nc_file(video_id,skip_frames,skip_row,skip_col,min_tol,\
            sigma,filter_size,startF,stopF,diff_frames,dz_id = dz_id)

    # Create a temporary dz file.
    temp_dz_filename = create_temp_nc_file(dz_filename)

    # count: start from the second frame. count is the variable that tracks the
    # current frame
    if diff_frames is None:
        count = startF
    else:
        count = startF + diff_frames

    # Set path to the two images
    path = "/Volumes/HD3/video_data/%d/frame%05d.png"

    hostname = socket.gethostname()
    cpu_count = multiprocessing.cpu_count()
    if hostname == 'taylor.physics.mun.ca':
        PROCESSES = cpu_count
    else:
        PROCESSES = cpu_count / 2
    PROCESSES = 8

    # Create pool
    pool = multiprocessing.Pool(PROCESSES)

    p = {}
    p['filename1'] = None
    p['filename2'] = None
    p['skip_row'] = skip_row
    p['skip_col'] = skip_col
    p['filter_size'] = filter_size
    p['min_tol'] = min_tol
    p['dz'] = dz
    p['sigma'] = sigma
    Ls = 13.5  # distance from front of screen to back of tank
    Lb = 0.9  # thickness of barrier
    Ld = 14.9  # width of rear channel
    Lp = 2.4  # thickness of back and front walls
    Lw = 29.5  # width of experimental region of tank
    # index of refraction
    na = 1.00
    np = 1.49
    nb = 1.49
    nw = 1.33
    gamma = 0.0001878  # See eqn 2.8 in Sutherland1999

    # const2 = -1.0/(gamma*((0.5*L_tank*L_tank)+(L_tank*win_l*n_water)))
    # Has been recalculated for our experiment
    dN2dz = (-1.0 / (Lw * gamma)) * (
        1.0 / (0.5 * Lw + nw / nb * Lb + Ld + nw / nb * Lp + nw / na * Ls))

    # if diff_frames is given, we are computing dN2dt
    if diff_frames is not None:
        dN2dz = dN2dz / (dt * diff_frames)

    logger.debug('dN2dz = {:f}'.format(dN2dz))

    # progress bar
    widgets = [
        progressbar.Percentage(), ' ',
        progressbar.Bar(), ' ',
        progressbar.ETA()
    ]
    pbar = progressbar.ProgressBar(widgets=widgets)

    # multiprocessing the task of writing data into nc file
    lock = multiprocessing.Lock()

    def cb(r):
        with lock:
            i, dz = r
            pbar.update(i)

            nc = netCDF4.Dataset(temp_dz_filename, 'a')
            temp_dz = nc.variables['temp_dz_array']
            temp_dz[i, :, :] = dN2dz * dz
            nc.close()

    tasks = []
    #counter for the while loop
    i = 0

    # submit tasks to perform in the while loop and this is in parallel
    while count < stopF:
        if diff_frames is not None:
            ref_frame = count - diff_frames
        else:
            ref_frame = startF

        filename1 = path % (video_id, ref_frame)
        filename2 = path % (video_id, count)

        if not os.path.exists(filename2):
            logger.info('%s not found but expected' % filename2)
            break

        # add filename1, filename2 to list of tasks
        p['filename1'] = filename1
        p['filename2'] = filename2
        p['i'] = i
        #tasks.append( (schlieren_lines, (dict(p),)))

        count += skip_frames

        pool.apply_async(schlieren_lines, (dict(p), ), callback=cb)
        i += 1

    if i == 0:
        raise Exception('No pairs of image files found')

    # submit all tasks to worker pool
    pbar.maxval = i

    logger.debug('Schlieren - frame by frame')
    pbar.start()

    # wait for all schlieren task to complete
    pool.close()
    pool.join()

    pbar.finish()

    # open the temporary nc file to put in the time axis
    nc = netCDF4.Dataset(temp_dz_filename, 'a')
    temp_dz = nc.variables['temp_dz_array']

    nc.close()

    # The last Step of Synthetic Schlieren... Uniform filtering in time
    # open the temp dz nc file
    nc = netCDF4.Dataset(temp_dz_filename, 'r')
    temp_dz_array = nc.variables['temp_dz_array']
    T = nc.variables['time'][:]

    # open the dz nc file for writing in the data
    dz_nc = netCDF4.Dataset(dz_filename, 'a')
    DZarray = dz_nc.variables['dz_array']
    ZZ = dz_nc.variables['row'][:]
    CC = dz_nc.variables['column'][:]
    TT = dz_nc.variables['time']
    #set the time axis for the dz array.
    TT[:] = T[:]

    t_chunk, r_chunk, c_chunk = chunkshape

    print("chunk t,z,x :", t_chunk, r_chunk, c_chunk)

    print("CC size, zz size", CC.size, ZZ.size)
    col_count = CC.size - 1
    row_count = ZZ.size - 1

    # step 12 of Schlieren :: apply uniform filter in the time axis with the filter size of 6 (about
    # 1second). This should smoothen the dz along time.

    logger.debug('Schlieren - chunk by chunk')
    # (i, j) should index the ith and jth chunk
    logger.debug('size = {}'.format(temp_dz_array.shape))
    logger.debug('chunkshape = {}'.format(chunkshape))
    nt, nz, nx = temp_dz_array.shape
    chunk_nt, chunk_nz, chunk_nx = chunkshape

    chunk_nz = chunk_nz * 2
    #chunk_nx = chunk_nx * 8

    widgets = [
        progressbar.Percentage(), ' ',
        progressbar.Bar(), ' ',
        progressbar.ETA()
    ]
    pbar = progressbar.ProgressBar(widgets=widgets,
                                   maxval=(nz // chunk_nz)).start()

    for j in range(nz // chunk_nz):

        pbar.update(j)
        temp = temp_dz_array[:, j * chunk_nz:(j + 1) * chunk_nz, :]

        # TODO: why (6, 1, 1)?
        temp_filt = ndimage.uniform_filter(temp, size=(18, 1, 1))

        DZarray[:, j * chunk_nz:(j + 1) * chunk_nz, :] = temp_filt

        #for i in range(nx // chunk_nx):


#
#            pbar.update(j * (nx//chunk_nx) + i)
#
#            temp = temp_dz_array[:,
#                                 j*chunk_nz:(j+1)*chunk_nz,
#                                 i*chunk_nx:(i+1)*chunk_nx]
#
#            # TODO: why (6, 1, 1)?
#            temp_filt = ndimage.uniform_filter(temp,size = (6,1,1))
#
#            DZarray[:,
#                    j*chunk_nz:(j+1)*chunk_nz,
#                    i*chunk_nx:(i+1)*chunk_nx] = temp_filt

    pbar.finish()

    dz_nc.calculation_complete = 1

    dz_nc.close()
    nc.close()

    # remove temp dz file
    os.unlink(temp_dz_filename)

    return dz_id
예제 #6
0
def create_nc_file(video_id,
                   skip_frames,
                   skip_row,
                   skip_col,
                   mintol,
                   sigma,
                   filter_size,
                   startF,
                   stopF,
                   diff_frames,
                   dz_id=None):
    """ 
    Given a video id create a dz nc file
    return dz_filename,dz_id,dt,dz,dx
    Need to compute dz for the first time.
    Need to create the path for the dz file and create the empty nc file
    """

    logger.debug('creating dz nc file to store the final data in..')

    # Get experiment ID
    db = labdb.LabDB()
    sql = "SELECT expt_id FROM video_experiments WHERE video_id =  %d" % video_id
    rows = db.execute(sql)
    expt_id = rows[0][0]
    logger.debug('experiment ID: %d' % expt_id)

    # get the window length and window height
    sql = """SELECT length FROM video WHERE video_id = %d  """ % video_id
    rows = db.execute(sql)
    win_l = rows[0][0] * 1.0

    sql = """SELECT height FROM video WHERE video_id = %d  """ % video_id
    rows = db.execute(sql)
    win_h = rows[0][0] * 1.0

    print("length", win_l, "\nheight", win_h)
    print(
        "video_id,skip_frames,skip_row,skip_col,expt_id,mintol,sigma,filter_size,startF,stopF,diff_frames"
    )
    print(video_id, skip_frames, skip_row, skip_col, expt_id, mintol, sigma,
          filter_size, startF, stopF, diff_frames)

    if dz_id is None:

        # CHECK IF there is already a dz_id already exists in the database
        sql = """SELECT dz_id FROM dz WHERE video_id=%d AND skip_frames=%d \
            AND skip_row = %d AND skip_col = %d AND mintol=%d AND sigma=%.1f \
            AND filter_size=%d AND startF=%d AND stopF=%d AND diff_frames=%s""" %\
            (video_id,skip_frames,skip_row,skip_col,mintol,sigma,filter_size,startF,stopF,diff_frames)
        rows = db.execute(sql)
        if len(rows) == 1:
            # there is already a dz_id; reuse it
            dz_id = rows[0][0]
        elif len(rows) > 1:
            print(
                "MULTIPLE dz_id exist in database with same parameters!!!  FIXME!"
            )
            sys.exit(1)

    print("dz_ID ###", dz_id)
    if dz_id is None:

        if diff_frames is None:
            diff_frames_s = "NULL"
            diff_frames = 0
        else:
            diff_frames_s = "%d" % diff_frames

        sql = """INSERT INTO dz (video_id,skip_frames,skip_row,skip_col,expt_id,mintol,\
                sigma,filter_size,startF,stopF,diff_frames)\
                VALUES (%d,%d,%d,%d,%d,%d,%f,%d,%d,%d,%s)""" %\
                (video_id,skip_frames,skip_row,skip_col,expt_id,\
                mintol,sigma,filter_size,startF,stopF,diff_frames_s)
        print(sql)
        db.execute(sql)
        sql = """SELECT LAST_INSERT_ID()"""
        rows = db.execute(sql)
        dz_id = rows[0][0]
    else:
        # need the sql to include mintol,filtersize,sigma too REMINDER
        sql = """ SELECT video_id,skip_frames,skip_row,skip_col,\
                startF,stopF FROM dz WHERE dz_id = %s""" \
                %dz_id
        previous_row = db.execute_one(sql)
        print(previous_row)
        if (previous_row[0] != video_id or previous_row[1] !=skip_frames  or\
                 previous_row[2]!=skip_row or previous_row[3]!=skip_col\
                 or  previous_row[4]!=startF or previous_row[5]!=stopF):
            print("video id/startF/stopF mismatch!")
            return None

    dz_path = "/data/dz/%d" % dz_id
    if not os.path.exists(dz_path):
        # Create the directory in which to store the nc file
        os.mkdir(dz_path)

    dz_filename = os.path.join(dz_path, "dz.nc")
    print("dz_ filename: ", dz_filename)

    # Declare the nc file for the first time
    if os.path.exists(dz_filename):
        # ensure you delete the nc file if it exists so as to create new nc file
        os.unlink(dz_filename)

    # find the number for rows and column of the frames and Ntime for dz field
    Nrow = 964 / skip_row
    Ncol = 1292 / skip_col
    Ntime = (stopF - startF - 1) // skip_frames + 1 - diff_frames

    print("no of rows : %d and no of columns : %d and Ntime : %d" %
          (Nrow, Ncol, Ntime))

    # create the nc file and set the dimensions of z and x axis.
    nc = netCDF4.Dataset(dz_filename, 'w', format='NETCDF4')

    nc.calculation_complete = 0

    row_dim = nc.createDimension('row', Nrow)
    col_dim = nc.createDimension('column', Ncol)
    t_dim = nc.createDimension('time', Ntime)

    # the dimensions are also variables
    ROW = nc.createVariable('row', numpy.float32, ('row'))
    COLUMN = nc.createVariable('column', numpy.float32, ('column'))
    TIME = nc.createVariable('time', numpy.float32, ('time'))

    # the length and height dimensions are variables containing the length and
    # height of each pixel in cm
    R = numpy.arange(0, win_h, win_h / Nrow, dtype=float)
    C = numpy.arange(0, win_l, win_l / Ncol, dtype=float)

    # given a video_id, look up filename and extract out a time.txt file
    mkvfile = '20140225T210351.mkv'  ## TEMPORARY

    cmd = 'mkvextract attachments /data/dv/%s 1:time.txt' % mkvfile
    os.system(cmd)
    t = numpy.loadtxt('time.txt')

    #path2time = "/Volumes/HD3/video_data/%d/time.txt" % video_id
    #t=numpy.loadtxt(path2time)
    print(t)

    dt = numpy.mean(numpy.diff(t[:, 1]))

    # compute dt,dz,dx
    dt = dt * skip_frames
    dz = win_h / Nrow
    dx = win_l / Ncol

    T = numpy.mgrid[0:Ntime * dt:Ntime * 1.0j]

    ROW[:] = R
    COLUMN[:] = C
    TIME[:] = T

    # chunk the data intelligently
    valSize = numpy.float32().itemsize
    chunksizes = chunk_shape_3D((Ntime, Nrow, Ncol), valSize=valSize)
    logger.debug('chunksizes = {}'.format(chunksizes))

    # declare the 3D data variable
    DZ = nc.createVariable('dz_array',
                           numpy.float32, ('time', 'row', 'column'),
                           chunksizes=chunksizes)

    db.commit()
    nc.close()

    return dz_filename, dz_id, dt, dz, dx, chunksizes
예제 #7
0
def compute_deltaN2(video_id,min_tol,sigma,filter_size,skip_frames=1,startF=0,stopF=0,diff_frames=1):
    """ Computes deltaN2 
    """
    db = labdb.LabDB()

    # check if DELTAN2 ARRAY has already been computed?
    # if deltaN2_flag =0 means deltaN2 is not computed else it holds the value
    # of the deltaN2 id.
    
    # get the number of frames if stopF is unspecified
    if (stopF == 0):
        sql = """ SELECT num_frames FROM video where video_id = %d""" % video_id
        rows = db.execute(sql)
        stopF = rows[0][0]
    num_frames = stopF-startF
    deltaN2_flag = checkifdeltaN2exists_test(video_id,min_tol,sigma,\
            filter_size,skip_frames,startF,stopF,diff_frames)     

    if (deltaN2_flag != 0) :
        return deltaN2_flag # deltaN2_flag is returning the deltaN2 id of the video_id
    

    # COMPUTE DELTAN2 FOR THE FIRST TIME
    #deltaN2_filename,dt,num_frames,win_l,win_h,deltaN2_id = create_nc_file(video_id,skip_frames)
    deltaN2_filename,dt,win_l,win_h,deltaN2_id =\
            create_nc_file_test(video_id,min_tol,sigma,filter_size,skip_frames,startF,stopF,diff_frames)
    
    # check if the dz file already exists 
    dz_id = SyntheticSchlieren.compute_dz(video_id,min_tol,sigma,filter_size,skip_frames,startF,stopF,diff_frames)
    print "dz_id::" ,dz_id

    # Define the constants used in the computation of deltaN2
    n_water = 1.3330
    n_air = 1.0
    L_tank = 453.0
    gamma = 0.0001878
    const = 1.0/(gamma * ((0.5*L_tank*L_tank)+(L_tank*win_l*n_water)))
    print "constant:",const

    # load the nc file of dz 
    data = netCDF4.Dataset("/Volumes/HD3/dz/%d/dz.nc" % dz_id, 'r')
    dz_array = data.variables['dz_array']
    dz_limit = dz_array.shape[0]

    from scipy.ndimage import gaussian_filter
    from numpy import ma
    index = 0
    while index < (dz_limit-1):
        print "computing deltaN2 of dz_frame %d of %d" % (index,(dz_limit-1))

        # compute deltaN2
        deltaN2_arr = -1.0* dz_array[index] * const
        #debug messages
        print "max: ",numpy.max(deltaN2_arr)
        print "min: ",numpy.min(deltaN2_arr)
        # append to nc file
        append2ncfile(deltaN2_filename,deltaN2_arr)

        index += 1

    data.close()

    # define time axis for the nc time variable
    nc = netCDF4.Dataset(deltaN2_filename,'a')
    deltaN2 = nc.variables['deltaN2_array']
    tl = deltaN2.shape[0]
    Tm=nc.variables['time']
    t_array = numpy.mgrid[0:tl*dt:tl*1.0j]
    print "length of T axis: " ,tl, "\n array length: " ,t_array.shape
    Tm[:] = t_array
    nc.close()
    return deltaN2_id
예제 #8
0
import matplotlib
#matplotlib.use('module://mplh5canvas.backend_h5canvas')
import argparse
form PIL import Image
import pylab
import numpy
import time
import os
import labdb
import netCDF4
from scipy import ndimage
import SyntheticSchlieren

global db
db = labdb.LabDB()


def append2ncfile(deltaN2_filename,deltaN2_arr):
    """
    Open the nc file
    Append the array to the end of the nc file
    Close the nc file 
    """
    nc=netCDF4.Dataset(deltaN2_filename,'a')
    deltaN2 = nc.variables['deltaN2_array']
    i= len(deltaN2)
    deltaN2[i,:,:]=deltaN2_arr
    print "len(deltaN2): ", i 
    print "appending"
    nc.close()