Example #1
0
def upgrade_gem(empowered, choose_gem):
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        upgrade = transform_coordinates(handle, 280, 550)

        if not choose_gem:
            first_gem = transform_coordinates(handle, 100, 640)
            send_mouse(handle, 'LM', first_gem[0], first_gem[1])
            sleep(0.1)
        try:
            if not empowered:
                for i in range(4):
                    print("gemup " + str(i))
                    if i == 3:
                        send_key(handle, 't')
                    send_mouse(handle, 'LM', upgrade[0], upgrade[1])
                    macro_sleep(1.8)
            else:
                for i in range(5):
                    print("gemup emp " + str(i))
                    if i == 4:
                        send_key(handle, 't')
                    send_mouse(handle, 'LM', upgrade[0], upgrade[1])
                    macro_sleep(1.8)
        except StopMacro:
            pass
Example #2
0
def open_rift():
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        rift = transform_coordinates(handle, 270, 300)
        accept = transform_coordinates(handle, 260, 850)

        send_mouse(handle, 'LM', rift[0], rift[1])
        send_mouse(handle, 'LM', accept[0], accept[1])
Example #3
0
def gamble(screenshot, handle, item):
    x1, y1, x2, y2 = win32gui.GetClientRect(handle)
    img_to_find = os.path.join(wd, f'./images/{x2 - x1}_{y2 - y1}/kadala.png')
    x1, y1 = transform_coordinates(handle, 220, 30)
    x2, y2 = transform_coordinates(handle, 300, 100)
    img = crop_image(screenshot, x1, y1, x2, y2)
    if image_search(img_to_find, img, precision=0.8):
        print('FOUND Kadala!')
        macros.gamble(item)
Example #4
0
def start_game(screenshot, handle):
    x1, y1, x2, y2 = win32gui.GetClientRect(handle)
    img_to_find = os.path.join(wd,
                               f'./images/{x2 - x1}_{y2 - y1}/start_game.png')
    x1, y1 = transform_coordinates(handle, 160, 500)
    x2, y2 = transform_coordinates(handle, 320, 540)
    img = crop_image(screenshot, x1, y1, x2, y2)
    if image_search(img_to_find, img, precision=0.8):
        print('FOUND START GAME!')
        send_mouse(handle, 'LM', x1, y1)
Example #5
0
def gamble(item):
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        tab_coords = kadala_tab_by_name(item)
        item_coords = kadala_item_by_name(item)
        tab = transform_coordinates(handle, tab_coords[0], tab_coords[1])
        item = transform_coordinates(handle, item_coords[0], item_coords[1])

        send_mouse(handle, 'LM', tab[0], tab[1])
        for i in range(60):
            send_mouse(handle, 'RM', item[0], item[1])
Example #6
0
def accept_gr(screenshot, handle):
    x1, y1, x2, y2 = win32gui.GetClientRect(handle)
    img_to_find = os.path.join(wd,
                               f'./images/{x2 - x1}_{y2 - y1}/keystone.png')
    x1, y1 = transform_coordinates(handle, 705, 755)
    x2, y2 = transform_coordinates(handle, 790, 815)
    accept = transform_coordinates(handle, 800, 900)
    img = crop_image(screenshot, x1, y1, x2, y2)
    if image_search(img_to_find, img, precision=0.8):
        print('FOUND accept GR')
        send_mouse(handle, 'LM', accept[0], accept[1])
Example #7
0
def open_rift(screenshot, handle, rift_type):
    x1, y1, x2, y2 = win32gui.GetClientRect(handle)
    img_to_find = os.path.join(wd, f'./images/{x2 - x1}_{y2 - y1}/obelisk.png')
    x1, y1 = transform_coordinates(handle, 220, 30)
    x2, y2 = transform_coordinates(handle, 300, 100)
    img = crop_image(screenshot, x1, y1, x2, y2)
    if image_search(img_to_find, img, precision=0.8):
        print('FOUND OPEN RIFT!')
        if rift_type == 'grift':
            macros.open_gr()
        else:
            macros.open_rift()
Example #8
0
def drop_inventory(spare_columns):
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        item = transform_coordinates(handle, 1875, 585, rel='right')
        step = transform_coordinates(handle, 50, 50)
        x, y = win32gui.ScreenToClient(handle, win32api.GetCursorPos())

        send_key(handle, 'c')
        for i in range(6):
            for j in range(10 - spare_columns):
                send_mouse(handle, 'LM', item[0] - j * step[0], item[1] + i * step[1])
                send_mouse(handle, 'LM', x, y)
        send_key(handle, 'c')
Example #9
0
def salvage(spare_columns):
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        menu = transform_coordinates(handle, 517, 480)
        anvil = transform_coordinates(handle, 165, 295)
        item = transform_coordinates(handle, 1875, 585, rel='right')
        step = transform_coordinates(handle, 50, 50)

        send_mouse(handle, 'LM', menu[0], menu[1])  # Salvage Menu
        send_mouse(handle, 'LM', anvil[0], anvil[1])  # Click Salvage Button
        for i in range(6):
            for j in range(10 - spare_columns):
                send_mouse(handle, 'LM', item[0] - j * step[0], item[1] + i * step[1])
                send_key(handle, 'enter')
                send_key(handle, 'enter')
        send_key(handle, 'esc')
Example #10
0
def leave_game():
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        leave = transform_coordinates(handle, 230, 475)

        send_key(handle, 'esc')
        send_mouse(handle, 'LM', leave[0], leave[1])
Example #11
0
def port_town(act):
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        act_coords = map_act_coords_by_act(act)
        town_coords = map_town_coords_by_act(act)

        bw_map = transform_coordinates(handle, 895, 125, rel='middle')
        act = transform_coordinates(handle, act_coords[0], act_coords[1], rel='middle')
        town = transform_coordinates(
            handle, town_coords[0], town_coords[1], rel='middle'
        )

        send_key(handle, 'm')
        send_mouse(handle, 'LM', bw_map[0], bw_map[1])
        send_mouse(handle, 'LM', act[0], act[1])
        send_mouse(handle, 'LM', town[0], town[1])
Example #12
0
def port_pool(poolspotlist):
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        poolspot = poolspotlist.next_spot()
        if poolspot:
            bw_map = transform_coordinates(handle, 895, 125, rel='middle')
            act = transform_coordinates(
                handle, poolspot[0][0], poolspot[0][1], rel='middle'
            )
            wp = transform_coordinates(
                handle, poolspot[1][0], poolspot[1][1], rel='middle'
            )

            # send_key(handle, 'm')
            send_mouse(handle, 'LM', bw_map[0], bw_map[1])
            send_mouse(handle, 'LM', act[0], act[1])
            send_mouse(handle, 'LM', wp[0], wp[1])
Example #13
0
def tpa1HoA1():
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        # open map
        send_key(handle, 'm')
        sleep(0.2)
        # reduce map
        map = transform_coordinates(handle, 895, 130)
        send_mouse(handle, 'LM', map[0], map[1])
        sleep(0.2)
        # select A1
        map = transform_coordinates(handle, 740, 620)
        send_mouse(handle, 'LM', map[0], map[1])
        sleep(0.2)
        # select HoA1
        map = transform_coordinates(handle, 467, 380)
        send_mouse(handle, 'LM', map[0], map[1])
Example #14
0
def tpA2Temple():
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        # open map
        send_key(handle, 'm')
        sleep(0.2)
        # reduce map
        map = transform_coordinates(handle, 895, 130)
        send_mouse(handle, 'LM', map[0], map[1])
        sleep(0.2)
        # select A2
        map = transform_coordinates(handle, 1090, 520)
        send_mouse(handle, 'LM', map[0], map[1])
        sleep(0.2)
        # select temple bounty
        map = transform_coordinates(handle, 1430, 380)
        send_mouse(handle, 'LM', map[0], map[1])
Example #15
0
def reforge():
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        item = transform_coordinates(handle, 1425, 580, rel='right')
        fill = transform_coordinates(handle, 710, 840)
        trans = transform_coordinates(handle, 250, 830)
        bw = transform_coordinates(handle, 580, 850)
        fw = transform_coordinates(handle, 850, 850)

        send_mouse(handle, 'RM', item[0], item[1])  # Item
        sleep(0.1)
        send_mouse(handle, 'LM', fill[0], fill[1])  # Fill
        send_mouse(handle, 'LM', trans[0], trans[1])  # Transmute
        sleep(0.1)
        send_mouse(handle, 'LM', bw[0], bw[1])  # Backwards
        send_mouse(handle, 'LM', fw[0], fw[1])  # Forth
        # send_mousemove(handle, item[0], item[1])
        send_mouse(handle, 'RM', item[0], item[1])  # Item
Example #16
0
def lower_difficulty():
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        lower = transform_coordinates(handle, 1700, 400, rel='right')

        send_key(handle, 'esc')
        for i in range(19):
            send_mouse(handle, 'LM', lower[0], lower[1])
            send_key(handle, 'enter')
        send_key(handle, 'esc')
Example #17
0
def reforge_ancient_primal(speed):
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')

    stop = False
    nmax = 1000

    try:
        if handle:
            while not stop and nmax > 0:

                if interrupt_key():
                    break

                nmax = nmax - 1

                item = transform_coordinates(handle, 1425, 580, rel='right')
                fill = transform_coordinates(handle, 710, 840)
                trans = transform_coordinates(handle, 250, 830)
                bw = transform_coordinates(handle, 580, 850)
                fw = transform_coordinates(handle, 850, 850)

                send_mouse(handle, 'RM', item[0], item[1])  # Item
                sleepSpeedConv(speed)
                send_mouse(handle, 'LM', fill[0], fill[1])  # Fill
                sleepSpeedConv(speed)
                send_mouse(handle, 'LM', trans[0], trans[1])  # Transmute
                sleepSpeedConv(speed)
                send_mouse(handle, 'LM', bw[0], bw[1])  # Backwards
                sleepSpeedConv(speed)
                send_mouse(handle, 'LM', fw[0], fw[1])  # Forth
                sleepSpeedConv(speed)
                send_mouse(handle, 'RM', item[0], item[1])  # Item
                sleepSpeedConv(speed)
                send_mousemove(handle, item[0], item[1])  # Hover
                sleep(0.1)

                # screenshot
                screenshot = get_image(handle)
                if foundAncient(screenshot, handle) or foundPrimal(
                        screenshot, handle):
                    stop = True
    except StopMacro:
        pass
Example #18
0
def cube_conv_sm(speed):
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        item = transform_coordinates(handle, 1425, 580, rel='right')
        step = transform_coordinates(handle, 50, 50)
        fill = transform_coordinates(handle, 710, 840)
        trans = transform_coordinates(handle, 250, 830)
        bw = transform_coordinates(handle, 580, 850)
        fw = transform_coordinates(handle, 850, 850)

        try:
            if speed == 'normal':
                for i in range(6):
                    for j in range(10):
                        send_mouse(handle, 'RM', item[0] + j * step[0],
                                   item[1] + i * step[1])
                        macro_sleep(0.13)
                        # macro_sleep(0.1)
                        send_mouse(handle, 'LM', fill[0], fill[1])  # Fill
                        send_mouse(handle, 'LM', trans[0],
                                   trans[1])  # Transmute
                        macro_sleep(0.13)
                        # macro_sleep(0.1)
                        send_mouse(handle, 'LM', bw[0], bw[1])  # Backwards
                        send_mouse(handle, 'LM', fw[0], fw[1])  # Forwards
            elif speed == 'sol':
                for _ in range(2):
                    for i in range(6):
                        for j in range(10):
                            send_mouse(
                                handle,
                                'RM',
                                item[0] + j * step[0],
                                item[1] + i * step[1],
                            )
                            macro_sleep(0.06)  # 0.025
                            send_mouse(handle, 'LM', fill[0], fill[1])  # Fill
                            send_mouse(handle, 'LM', trans[0],
                                       trans[1])  # Transmute
                            macro_sleep(0.06)  # 0.025
                            send_mouse(handle, 'LM', bw[0], bw[1])  # Backwards
                            send_mouse(handle, 'LM', fw[0], fw[1])  # Forwards
            elif speed == 'slow':
                for i in range(6):
                    for j in range(10):
                        send_mouse(handle, 'RM', item[0] + j * step[0],
                                   item[1] + i * step[1])
                        macro_sleep(0.2)
                        send_mouse(handle, 'LM', fill[0], fill[1])  # Fill
                        macro_sleep(0.2)
                        send_mouse(handle, 'LM', trans[0],
                                   trans[1])  # Transmute
                        macro_sleep(0.2)
                        send_mouse(handle, 'LM', bw[0], bw[1])  # Backwards
                        macro_sleep(0.2)
                        send_mouse(handle, 'LM', fw[0], fw[1])  # Forwards
                        macro_sleep(0.2)
        except StopMacro:
            pass
Example #19
0
    def forward(self, v):
        """
        integrate a 3D stationary velocity field through scaling and squaring
        """

        displacement = transform_coordinates(v) / float(2 ** self.no_steps)

        for _ in range(self.no_steps):
            transformation = self.identity_grid + displacement.permute([0, 2, 3, 4, 1])
            displacement = displacement + F.grid_sample(displacement, transformation,
                                                        padding_mode='border', align_corners=True)

        transformation = self.identity_grid.permute([0, 4, 1, 2, 3]) + displacement
        return transformation, transform_coordinates_inv(displacement)
Example #20
0
def upgrade_gem(screenshot, handle):
    x1, y1, x2, y2 = win32gui.GetClientRect(handle)
    uhrsi = os.path.join(wd, f'./images/{x2 - x1}_{y2 - y1}/urhsi.png')
    x1, y1 = transform_coordinates(handle, 220, 30)
    x2, y2 = transform_coordinates(handle, 300, 100)
    img = crop_image(screenshot, x1, y1, x2, y2)
    if image_search(uhrsi, img, precision=0.8):
        x1, y1, x2, y2 = win32gui.GetClientRect(handle)
        images_to_find = [
            os.path.join(
                wd, f'./images/{x2 - x1}_{y2 - y1}/urhsi_upgrade_{i}.png')
            for i in range(1, 6)
        ]
        x1, y1 = transform_coordinates(handle, 270, 530)
        x2, y2 = transform_coordinates(handle, 355, 560)
        upgrade = transform_coordinates(handle, 280, 550)

        upgrade_img = crop_image(screenshot, x1, y1, x2, y2)

        if image_search(images_to_find[4], upgrade_img, precision=0.95):
            send_mouse(handle, 'LM', upgrade[0], upgrade[1])
            print('Found 5 Upgrades left!')
        elif image_search(images_to_find[3], upgrade_img, precision=0.95):
            send_mouse(handle, 'LM', upgrade[0], upgrade[1])
            print('Found 4 Upgrades Left!')
        elif image_search(images_to_find[2], upgrade_img, precision=0.95):
            send_mouse(handle, 'LM', upgrade[0], upgrade[1])
            #send_key(handle, 't')
            print('Found 3 Upgrades Left!')
        elif image_search(images_to_find[1], upgrade_img, precision=0.95):
            send_mouse(handle, 'LM', upgrade[0], upgrade[1])
            send_key(handle, 't')
            print('Found 2 Upgrades Left!')
        elif image_search(images_to_find[0], upgrade_img, precision=0.95):
            send_mouse(handle, 'LM', upgrade[0], upgrade[1])
            send_key(handle, 't')
            print('Found 1 Upgrades Left!')
Example #21
0
def swap_armor(items):
    handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III')
    if handle:
        item_coords = []
        for i in range(items):
            item_coords.append(
                transform_coordinates(handle, 1425, 580 + i * 100,
                                      rel='right'))

        send_key(handle, 'c')
        for i, coords in enumerate(item_coords):
            if i == 1:
                send_key_down(handle, 'alt')
                send_mouse(handle, 'RM', coords[0], coords[1])
                send_key_up(handle, 'alt')
            else:
                send_mouse(handle, 'RM', coords[0], coords[1])
        send_key(handle, 'c')
Example #22
0
    def run(self):
        '''
        '''
        #pdb.set_trace()
        # define polarity based on the warp requested
        self.polarity = 1.
        if self.warp:
            self.polarity = -1.

        # transform RAS-coordinates into LAI-coordinates
        m_ras2lai = np.array([[-1.0, 0.0, 0.0, 0.0],
                             [0.0, 1.0, 0.0, 0.0],
                             [0.0, 0.0, -1.0, 0.0],
                             [0.0, 0.0, 0.0, 1.0]], dtype=np.float)
        m_rcs2lai = np.dot(m_ras2lai, self.m_rcs2ras)

        # indices of image volume
        nr, nc, ns = self.vol.shape[:3]
        vc3, vr3, vs3 = utils.meshgrid(np.arange(nr), np.arange(nc), np.arange(ns), dtype=np.float32)
        vrcs = CV(x=vr3, y=vc3, z=vs3)
        vxyz = utils.transform_coordinates(vrcs, m_rcs2lai)

        # account for half-voxel shift in R and C directions
        halfvox = np.zeros((4, 4))
        halfvox[0, 3] = m_rcs2lai[0, 0] / 2.0
        halfvox[1, 3] = m_rcs2lai[1, 1] / 2.0
        m_rcs2lai = m_rcs2lai + halfvox

        # extract rotational and scaling parts of the transformation matrix
        # ignore the translation part
        r_rcs2lai = np.eye(4, 4)
        r_rcs2lai[:3, :3] = m_rcs2lai[:3, :3]

        # Jon Polimeni:
        # Since partial derivatives in Jacobian Matrix are differences
        # that depend on the ordering of the elements of the 3D array, the
        # coordinates may increase in the opposite direction from the array
        # indices, in which case the differential element should be negative.
        # The differentials can be determined by mapping a vector of 1s
        # through rotation and scaling, where any mirror
        # will impose a negation
        ones = CV(1., 1., 1.)
        dxyz = utils.transform_coordinates_old(ones, r_rcs2lai)

        # # convert image vol coordinates from RAS to LAI
        # vxyz = utils.transform_coordinates(vrcs, m_rcs2lai)

        # # compute new coordinates and the jacobian determinant
        # # TODO still not clear about what to return
        # self.out, self.vjacmult_lps = self.non_linear_unwarp(vxyz, dxyz,
        #                                                     m_rcs2lai)


        # for each slice
        '''
        for slice in xrange(ns):
            sys.stdout.flush()
            if (slice + 1) % 10 == 0:
                print(slice + 1),
            else:
                print('.'),

            # we are doing it slice by slice
            vs = np.ones(vr.shape) * slice

            vrcs2d = CV(vr, vc, slice)
            # rcs2lai
            vxyz2d = utils.transform_coordinates(vrcs2d, m_rcs2lai)

            # compute new coordinates and the jacobian determinant
            moddv, modxyz = eval_spherical_harmonics(self.coeffs, self.vendor, vxyz2d)
            dvx[..., slice] = moddv.x
            dvy[..., slice] = moddv.y
            dvz[..., slice] = moddv.z
            '''

        print
        # Evaluate spherical harmonics on a smaller grid 
        dv, grcs, g_xyz2rcs = self.eval_spharm_grid(self.vendor, self.coeffs)
        # do the nonlinear unwarp
        self.out, self.vjacmult_lps = self.non_linear_unwarp(vxyz, grcs, dv, dxyz,
                                                                 m_rcs2lai, g_xyz2rcs)
Example #23
0
    def non_linear_unwarp(self, vxyz, grcs, dv, dxyz, m_rcs2lai, g_xyz2rcs):
        ''' Performs the crux of the unwarping.
        It's agnostic to Siemens or GE and uses more functions to
        do the processing separately.

        Needs self.vendor, self.coeffs, self.warp, self.nojac to be set

        Parameters
        ----------
        vxyz : CoordsVector (namedtuple) contains np.array
            has 3 elements x,y and z each representing the grid coordinates
        dxyz : CoordsVector (namedtuple)
           differential coords vector

        Returns
        -------
        TODO still vague what to return
        vwxyz : CoordsVector (namedtuple) contains np.array
            x,y and z coordinates of the unwarped coordinates
        vjacmult_lps : np.array
            the jacobian multiplier (determinant)
        '''
        # Jacobian multiplier is unitless but calculated in terms of
        # displacements and coordinates in LPS orientation
        # (right handed form of p.o.v of patient )
        if self.vendor == 'siemens':

            log.info('Interpolating the spherical harmonics grid')
            vrcsg = utils.transform_coordinates(vxyz, g_xyz2rcs)
            vrcsg_m = CV(vrcsg.y, vrcsg.x, vrcsg.z)
            del vrcsg
            dvx = ndimage.interpolation.map_coordinates(dv.y,
                                                        vrcsg_m,
                                                        output=np.float32,
                                                        order=self.order)
            dvy = ndimage.interpolation.map_coordinates(dv.x,
                                                        vrcsg_m,
                                                        output=np.float32,
                                                        order=self.order)
            dvz = ndimage.interpolation.map_coordinates(dv.z,
                                                        vrcsg_m,
                                                        output=np.float32,
                                                        order=self.order)

            log.info('Calculating the new locations of voxels')
            # new locations of the image voxels in XYZ ( LAI ) coords
            vxyzw = CV(x=vxyz.x + self.polarity * dvx,
                       y=vxyz.y + self.polarity * dvy,
                       z=vxyz.z + self.polarity * dvz)

            # hopefully, free memory
            del dvx, dvy, dvz, vxyz

            # if polarity is negative, the jacobian is also inversed
            if self.polarity == -1:
                vjacdet_lps = 1. / vjacdet_lps

            # convert the locations got into RCS indices
            vrcsw = utils.transform_coordinates(vxyzw,
                                                np.linalg.inv(m_rcs2lai))

            # hopefully, free memory
            del vxyzw
            # resample the image
            log.info('Interpolating the image')
            if self.vol.ndim == 3:
                #out = utils.interp3(self.vol, vrcsw.x, vrcsw.x, vrcsw.z)
                out = ndimage.interpolation.map_coordinates(self.vol,
                                                            vrcsw,
                                                            output=np.float32,
                                                            order=self.order)
            if self.vol.ndim == 4:
                nframes = self.vol.shape[3]
                out = np.zeros(self.vol.shape)
                for f in nframes:
                    _out = ndimage.interpolation.map_coordinates(self.vol[..., f],
                                                                vrcsw,
                                                                output=np.float32,
                                                                order=self.order)

            # find NaN voxels, and set them to 0
            out[np.where(np.isnan(out))] = 0.
            out[np.where(np.isinf(out))] = 0.

            vjacdet_lpsw = None
            # Multiply the intensity with the Jacobian det, if needed
            if not self.nojac:
                log.info('Evaluating the Jacobian multiplier')
                if dxyz == 0:
                    vjacdet_lps = 1
                else:
                    vjacdet_lps = eval_siemens_jacobian_mult(dv, dxyz)
                log.info('Interpolating the Jacobian multiplier')
                vjacdet_lpsw = ndimage.interpolation.map_coordinates(vjacdet_lps,
                                                            vrcsg_m,
                                                            output=np.float32,
                                                            order=self.order)
                vjacdet_lpsw[np.where(np.isnan(out))] = 0.
                vjacdet_lpsw[np.where(np.isinf(out))] = 0.

                log.info('Performing Jacobian multiplication')
                if out.ndim == 3:
                    out = out * vjacdet_lpsw
                elif out.ndim == 4:
                    for f in out.shape[3]:
                        out[..., f] = out[..., f] * vjacdet_lpsw

            # return image and the jacobian
            return out, vjacdet_lpsw

        if self.vendor == 'ge':
            pass  # for now
Example #24
0
def satdownload(product_id, geojson, download_path='./downloads/',
                remove_trash=False, api=None, download_only=False):
    """
    Downloads, extracts and crops products.
    Args:
        product_id: str
            Example: "e3fea737-a83b-4fec-8a5a-68ed8d647c71"
        geojson: str
            Path to geojson file.
        download_path: str, optional
            location to download products.
        remove_trash: bool, default Fasle
            remove unnecessary files after downloading.
        download_only: bool, default False
            Download only (Do not extract).
        api: SentinelAPI api object
    """

    print('Satdownload for ' + product_id)
    logging.debug('satdownload: ' + product_id)
    # create downloads folder
    if os.path.isdir(download_path) is False:
        os.mkdir(download_path)

    if api is None:
        api = SentinelAPI(USERNAME, PASSWORD,
                          'https://scihub.copernicus.eu/dhus')

    # query product information
    product_info = api.get_product_odata(product_id, full=True)

    sentinel = product_info['Satellite']

    # directory for images only
    target_directory = os.path.join(download_path, product_info['title'])

    if os.path.isdir(target_directory):
        print('Product is already processed, skipping product...')
        return

    # download
    if os.path.isfile(os.path.join(
            download_path, product_info['title'] + '.zip')) is True:
        print(product_info['title'] + '.zip' + ' exist.')
    else:
        satdownload_zip(product_info['id'], download_path, api=api)
    # skip extraction part
    if download_only is True:
        return

    # extract zip file
    zipfile_path = os.path.join(download_path, product_info['title'] + '.zip')
    zip_ref = zipfile.ZipFile(zipfile_path, 'r')
    zip_ref.extractall(download_path)
    zip_ref.close()

    if os.path.isdir(
            os.path.join(download_path, product_info['Filename'])) is False:
        raise Exception('Directory not found after unzipping.')

    # clearing target directory
    if os.path.isdir(target_directory) is True:
        shutil.rmtree(target_directory)
    os.mkdir(target_directory)

    selection = transform_coordinates(coordinates_from_geojson(geojson))

    if sentinel == 'Sentinel-2':
        # product can contain many tails (located in ./GRANULE/)
        granule = os.path.join(download_path, product_info['Filename'],
                               'GRANULE')
        for i, tail_name in enumerate(os.listdir(granule)):
            print('\ttail name: ' + tail_name)
            tail_folder_name = 'tail.{}'.format(i)
            os.mkdir(os.path.join(target_directory, tail_folder_name))

            # image directories are different for different product types
            image_dir = os.path.join(granule, tail_name, 'IMG_DATA')
            if product_info['Product type'] == 'S2MSI2Ap':
                image_dir = os.path.join(image_dir, 'R10m')

            # move bands into target directory
            for image in os.listdir(image_dir):
                image_prime = image
                if product_info['Product type'] == 'S2MSI2Ap':
                    image_prime = image_prime[4:-8] + '.jp2'
                os.rename(os.path.join(image_dir, image),
                          os.path.join(target_directory,
                                       tail_folder_name, image_prime))

    elif sentinel == 'Sentinel-1':
        # shift selection for sentinel-1 products
        dx, dy = 130.54544882194287, 20.162166196209284
        selection[:, 0] = selection[:, 0] + dx
        selection[:, 1] = selection[:, 1] - dy

        # create tail folder
        tail_folder_name = 'tail.{}'.format(0)
        os.mkdir(os.path.join(target_directory, tail_folder_name))

        # image directories are different for different product types
        image_dir = os.path.join(download_path, product_info['Filename'],
                                 'measurement')

        # move bands into target directory
        for image in os.listdir(image_dir):
            image_path = os.path.join(image_dir, image)
            gdal.Warp(image_path, gdal.Open(image_path), dstSRS='EPSG:32638')
            os.rename(image_path, os.path.join(target_directory,
                      tail_folder_name, image))
    else:
        print('Unknown satellite')

    # save info file
    product_info_series = pandas.Series(product_info)
    with open(os.path.join(target_directory, 'info.txt'), 'w') as f:
        f.write(product_info_series.to_string())
    with open(os.path.join(target_directory, 'info.json'), 'w') as f:
        product_info_series.to_json(f)

    # remove unnecessary files
    if remove_trash is True:
        os.remove(zipfile_path)
        shutil.rmtree(os.path.join(download_path, product_info['Filename']))

    # cropping images
    print(target_directory)
    for tail_name in os.listdir(target_directory):
        if os.path.isdir(os.path.join(target_directory, tail_name)) is False:
            continue
        print('\tprocessing ' + tail_name + ' ...')
        process_tail(os.path.join(target_directory, tail_name), selection,
                     remove_trash=remove_trash)
    print('\n\n')
Example #25
0
    def non_linear_unwarp_siemens(self, volshape, dv, dxyz, m_rcs2lai, g_xyz2rcs):
        ''' Performs the crux of the unwarping.
        It's agnostic to Siemens or GE and uses more functions to
        do the processing separately.

        Needs self.vendor, self.coeffs, self.warp, self.nojac to be set

        Parameters
        ----------
        vxyz : CoordsVector (namedtuple) contains np.array
            has 3 elements x,y and z each representing the grid coordinates
        dxyz : CoordsVector (namedtuple)
           differential coords vector

        Returns
        -------
        TODO still vague what to return
        vwxyz : CoordsVector (namedtuple) contains np.array
            x,y and z coordinates of the unwarped coordinates
        vjacmult_lps : np.array
            the jacobian multiplier (determinant)
        '''
        log.info('Evaluating the jacobian multiplier')
        nr, nc, ns = self.vol.shape[:3]
        if not self.nojac:
            jim2 = np.zeros((nr, nc), dtype=np.float32)
            vjacdet_lpsw = np.zeros((nr, nc), dtype=np.float32)
            if dxyz == 0:
                vjacdet_lps = 1
            else:
                vjacdet_lps = eval_siemens_jacobian_mult(dv, dxyz)

        # essentially pre-allocating everything 
        out = np.zeros((nr, nc, ns), dtype=np.float32)
        fullWarp = np.zeros((nr, nc, ns, 3), dtype=np.float32)

        vjacout = np.zeros((nr, nc, ns), dtype=np.float32)
        im2 = np.zeros((nr, nc), dtype=np.float32)
        dvx = np.zeros((nr, nc), dtype=np.float32)
        dvy = np.zeros((nr, nc), dtype=np.float32)
        dvz = np.zeros((nr, nc), dtype=np.float32)
        im_ = np.zeros((nr, nc), dtype=np.float32)
        # init jacobian temp image
        vc, vr = utils.meshgrid(np.arange(nc), np.arange(nr))

        log.info('Unwarping slice by slice')
        # for every slice
        for s in xrange(ns):
            # pretty print
            sys.stdout.flush()
            if (s+1) % 10 == 0:
                print s+1,
            else:
                print '.',
                
            # hopefully, free memory
            gc.collect()
            # init to 0
            dvx.fill(0.)
            dvy.fill(0.)
            dvz.fill(0.)
            im_.fill(0.)
            
            vs = np.ones(vr.shape) * s
            vrcs = CV(vr, vc, vs)
            vxyz = utils.transform_coordinates(vrcs, m_rcs2lai)
            vrcsg = utils.transform_coordinates(vxyz, g_xyz2rcs)
            ndimage.interpolation.map_coordinates(dv.x,
                                                  vrcsg,
                                                  output=dvx,
                                                  order=self.order)
            ndimage.interpolation.map_coordinates(dv.y,
                                                  vrcsg,
                                                  output=dvy,
                                                  order=self.order)
            ndimage.interpolation.map_coordinates(dv.z,
                                                  vrcsg,
                                                  output=dvz,
                                                  order=self.order)
            # new locations of the image voxels in XYZ ( LAI ) coords

            #dvx.fill(0.)
            #dvy.fill(0.)
            #dvz.fill(0.)
            
            vxyzw = CV(x=vxyz.x + self.polarity * dvx,
                       y=vxyz.y + self.polarity * dvy,
                       z=vxyz.z + self.polarity * dvz)

            # convert the locations got into RCS indices
            vrcsw = utils.transform_coordinates(vxyzw,
                                                np.linalg.inv(m_rcs2lai))
            # map the internal voxel coordinates to FSL scaled mm coordinates
            pixdim1=float((subprocess.Popen(['fslval', self.name,'pixdim1'], stdout=subprocess.PIPE).communicate()[0]).strip())
            pixdim2=float((subprocess.Popen(['fslval', self.name,'pixdim2'], stdout=subprocess.PIPE).communicate()[0]).strip())
            pixdim3=float((subprocess.Popen(['fslval', self.name,'pixdim3'], stdout=subprocess.PIPE).communicate()[0]).strip())
            dim1=float((subprocess.Popen(['fslval', self.name,'dim1'], stdout=subprocess.PIPE).communicate()[0]).strip())
            outputOrient=subprocess.Popen(['fslorient', self.name], stdout=subprocess.PIPE).communicate()[0]
            if outputOrient.strip() == 'NEUROLOGICAL':
                # if neurological then flip x coordinate (both here in premat and later in postmat)
                m_vox2fsl = np.array([[-1.0*pixdim1, 0.0, 0.0, pixdim1*(dim1-1)],
                                  [0.0, pixdim2, 0.0, 0.0],
                                  [0.0, 0.0, pixdim3, 0.0],
                                  [0.0, 0.0, 0.0, 1.0]], dtype=np.float)
            else:
                m_vox2fsl = np.array([[pixdim1, 0.0, 0.0, 0.0],
                                  [0.0, pixdim2, 0.0, 0.0],
                                  [0.0, 0.0, pixdim3, 0.0],
                                  [0.0, 0.0, 0.0, 1.0]], dtype=np.float)
                
            vfsl = utils.transform_coordinates(vrcsw, m_vox2fsl)


            #im_ = utils.interp3(self.vol, vrcsw.x, vrcsw.y, vrcsw.z)
            ndimage.interpolation.map_coordinates(self.vol,
                                                  vrcsw,
                                                  output=im_,
                                                  order=self.order)
            # find NaN voxels, and set them to 0
            im_[np.where(np.isnan(im_))] = 0.
            im_[np.where(np.isinf(im_))] = 0.
            im2[vr, vc] = im_

            #img = nib.Nifti1Image(dvx,np.eye(4))
            #nib.save(img,"x"+str(s).zfill(3)+".nii.gz")
            #img = nib.Nifti1Image(dvy,np.eye(4))
            #nib.save(img,"y"+str(s).zfill(3)+".nii.gz")
            #img = nib.Nifti1Image(dvz,np.eye(4))
            #nib.save(img,"z"+str(s).zfill(3)+".nii.gz")

            # Multiply the intensity with the Jacobian det, if needed
            if not self.nojac:
                vjacdet_lpsw.fill(0.)
                jim2.fill(0.)
                # if polarity is negative, the jacobian is also inversed
                if self.polarity == -1:
                    vjacdet_lps = 1. / vjacdet_lps

                ndimage.interpolation.map_coordinates(vjacdet_lps,
                                                      vrcsg,
                                                      output=vjacdet_lpsw,
                                                      order=self.order)
                vjacdet_lpsw[np.where(np.isnan(vjacdet_lpsw))] = 0.
                vjacdet_lpsw[np.where(np.isinf(vjacdet_lpsw))] = 0.
                jim2[vr, vc] = vjacdet_lpsw
                im2 = im2 * jim2
                vjacout[..., s] = jim2

            fullWarp[...,s,0]=vfsl.x
            fullWarp[...,s,1]=vfsl.y
            fullWarp[...,s,2]=vfsl.z
            out[..., s] = im2

        print
       
        img=nib.Nifti1Image(fullWarp,self.m_rcs2ras)
        nib.save(img,"fullWarp_abs.nii.gz")
        # return image and the jacobian
        return out, vjacout
Example #26
0
    def non_linear_unwarp_siemens(self, volshape, dv, dxyz, m_rcs2lai,
                                  m_rcs2lai_nohalf, g_xyz2rcs):
        ''' Performs the crux of the unwarping.
        It's agnostic to Siemens or GE and uses more functions to
        do the processing separately.

        Needs self.vendor, self.coeffs, self.warp, self.nojac to be set

        Parameters
        ----------
        vxyz : CoordsVector (namedtuple) contains np.array
            has 3 elements x,y and z each representing the grid coordinates
        dxyz : CoordsVector (namedtuple)
           differential coords vector

        Returns
        -------
        TODO still vague what to return
        vwxyz : CoordsVector (namedtuple) contains np.array
            x,y and z coordinates of the unwarped coordinates
        vjacmult_lps : np.array
            the jacobian multiplier (determinant)
        '''
        log.info('Evaluating the jacobian multiplier')
        nr, nc, ns = self.vol.shape[:3]
        if not self.nojac:
            jim2 = np.zeros((nr, nc), dtype=np.float32)
            vjacdet_lpsw = np.zeros((nr, nc), dtype=np.float32)
            if dxyz == 0:
                vjacdet_lps = 1
            else:
                vjacdet_lps = eval_siemens_jacobian_mult(dv, dxyz)

        # essentially pre-allocating everything
        out = np.zeros((nr, nc, ns), dtype=np.float32)
        fullWarp = np.zeros((nr, nc, ns, 3), dtype=np.float32)

        vjacout = np.zeros((nr, nc, ns), dtype=np.float32)
        im2 = np.zeros((nr, nc), dtype=np.float32)
        dvx = np.zeros((nr, nc), dtype=np.float32)
        dvy = np.zeros((nr, nc), dtype=np.float32)
        dvz = np.zeros((nr, nc), dtype=np.float32)
        im_ = np.zeros((nr, nc), dtype=np.float32)
        # init jacobian temp image
        vc, vr = utils.meshgrid(np.arange(nc), np.arange(nr))

        log.info('Unwarping slice by slice')
        # for every slice
        for s in xrange(ns):
            # pretty print
            sys.stdout.flush()
            if (s + 1) % 10 == 0:
                print s + 1,
            else:
                print '.',

            # hopefully, free memory
            gc.collect()
            # init to 0
            dvx.fill(0.)
            dvy.fill(0.)
            dvz.fill(0.)
            im_.fill(0.)

            vs = np.ones(vr.shape) * s
            vrcs = CV(vr, vc, vs)
            vxyz = utils.transform_coordinates(vrcs, m_rcs2lai_nohalf)
            vrcsg = utils.transform_coordinates(vxyz, g_xyz2rcs)
            ndimage.interpolation.map_coordinates(dv.x,
                                                  vrcsg,
                                                  output=dvx,
                                                  order=self.order)
            ndimage.interpolation.map_coordinates(dv.y,
                                                  vrcsg,
                                                  output=dvy,
                                                  order=self.order)
            ndimage.interpolation.map_coordinates(dv.z,
                                                  vrcsg,
                                                  output=dvz,
                                                  order=self.order)
            # new locations of the image voxels in XYZ ( LAI ) coords

            #dvx.fill(0.)
            #dvy.fill(0.)
            #dvz.fill(0.)

            vxyzw = CV(x=vxyz.x + self.polarity * dvx,
                       y=vxyz.y + self.polarity * dvy,
                       z=vxyz.z + self.polarity * dvz)

            # convert the locations got into RCS indices
            vrcsw = utils.transform_coordinates(vxyzw,
                                                np.linalg.inv(m_rcs2lai))
            # map the internal voxel coordinates to FSL scaled mm coordinates
            pixdim1 = float((subprocess.Popen(
                ['fslval', self.name, 'pixdim1'],
                stdout=subprocess.PIPE).communicate()[0]).strip())
            pixdim2 = float((subprocess.Popen(
                ['fslval', self.name, 'pixdim2'],
                stdout=subprocess.PIPE).communicate()[0]).strip())
            pixdim3 = float((subprocess.Popen(
                ['fslval', self.name, 'pixdim3'],
                stdout=subprocess.PIPE).communicate()[0]).strip())
            dim1 = float((subprocess.Popen(
                ['fslval', self.name, 'dim1'],
                stdout=subprocess.PIPE).communicate()[0]).strip())
            outputOrient = subprocess.Popen(
                ['fslorient', self.name],
                stdout=subprocess.PIPE).communicate()[0]
            if outputOrient.strip() == 'NEUROLOGICAL':
                # if neurological then flip x coordinate (both here in premat and later in postmat)
                m_vox2fsl = np.array(
                    [[-1.0 * pixdim1, 0.0, 0.0, pixdim1 *
                      (dim1 - 1)], [0.0, pixdim2, 0.0, 0.0],
                     [0.0, 0.0, pixdim3, 0.0], [0.0, 0.0, 0.0, 1.0]],
                    dtype=np.float)
            else:
                m_vox2fsl = np.array(
                    [[pixdim1, 0.0, 0.0, 0.0], [0.0, pixdim2, 0.0, 0.0],
                     [0.0, 0.0, pixdim3, 0.0], [0.0, 0.0, 0.0, 1.0]],
                    dtype=np.float)

            vfsl = utils.transform_coordinates(vrcsw, m_vox2fsl)

            #im_ = utils.interp3(self.vol, vrcsw.x, vrcsw.y, vrcsw.z)
            ndimage.interpolation.map_coordinates(self.vol,
                                                  vrcsw,
                                                  output=im_,
                                                  order=self.order)
            # find NaN voxels, and set them to 0
            im_[np.where(np.isnan(im_))] = 0.
            im_[np.where(np.isinf(im_))] = 0.
            im2[vr, vc] = im_

            #img = nib.Nifti1Image(dvx,np.eye(4))
            #nib.save(img,"x"+str(s).zfill(3)+".nii.gz")
            #img = nib.Nifti1Image(dvy,np.eye(4))
            #nib.save(img,"y"+str(s).zfill(3)+".nii.gz")
            #img = nib.Nifti1Image(dvz,np.eye(4))
            #nib.save(img,"z"+str(s).zfill(3)+".nii.gz")

            # Multiply the intensity with the Jacobian det, if needed
            if not self.nojac:
                vjacdet_lpsw.fill(0.)
                jim2.fill(0.)
                # if polarity is negative, the jacobian is also inversed
                if self.polarity == -1:
                    vjacdet_lps = 1. / vjacdet_lps

                ndimage.interpolation.map_coordinates(vjacdet_lps,
                                                      vrcsg,
                                                      output=vjacdet_lpsw,
                                                      order=self.order)
                vjacdet_lpsw[np.where(np.isnan(vjacdet_lpsw))] = 0.
                vjacdet_lpsw[np.where(np.isinf(vjacdet_lpsw))] = 0.
                jim2[vr, vc] = vjacdet_lpsw
                im2 = im2 * jim2
                vjacout[..., s] = jim2

            fullWarp[..., s, 0] = vfsl.x
            fullWarp[..., s, 1] = vfsl.y
            fullWarp[..., s, 2] = vfsl.z
            out[..., s] = im2

        print

        img = nib.Nifti1Image(fullWarp, self.m_rcs2ras)
        nib.save(img, "fullWarp_abs.nii.gz")
        # return image and the jacobian
        return out, vjacout
Example #27
0
    Disposs of images below May 15 and over Oct 15
    :param data: str, path to products
    :param date_inf: str, infimum date to consider product as seasonal
    :param date_sup: str, supremum date to consider product as seasonal

    """
    os.makedirs(os.path.join(data, 'removed'), exist_ok=True)
    for product in os.listdir(data):
        if product.startswith('product') is False:
            continue
        path = os.path.join(data, product)
        if os.path.isdir(path) is False:
            continue
        if seasonal(path, date_inf, date_sup) is False:
            print('\tRemoving ' + path)
            shutil.copy(os.path.join(path, 'TCI.tiff'),
                        os.path.join(data, 'removed', product + '.tiff'))
            shutil.rmtree(path)


if __name__ == '__main__':
    args = parse_arguments()
    selection = None
    if args.geojson is not None:
        selection = transform_coordinates(
            coordinates_from_geojson(args.geojson))
    copy_and_format_names(args.input_dir, args.output_dir, selection)
    if args.remove_unseasonal_images:
        remove_unseasonal_images(args.output_dir)
    remove_unactionable_images(args.output_dir)