瀏覽分類:

程式 Program

[Py] Median of Two Sorted Arrays

Median of Two Sorted Arrays (leetcode.com)

2085 / 2085 test cases passed.
Status: Accepted
Runtime: 120 ms (19.98%)
Memory Usage: 14.1 MB

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        n1n = len(nums1)
        n2n = len(nums2)
        if n1n == 0: # if num1 is empty
            if (n2n % 2) == 1: # Odd 
                return nums2[n2n//2]
            else:
                return (nums2[n2n//2]+nums2[(n2n//2)-1])/2
        elif n2n == 0 :# if num2 is empty
            if (n1n % 2) == 1: # Odd 
                return nums1[n1n//2]
            else:
                return (nums1[n1n//2]+nums1[(n1n//2)-1])/2
        else: # both are not empty
            Arr=[]
            n1i=0 #cursor1 for num1
            n2i=0 #cursor2 for num2
            while(n1i<n1n or n2i<n2n):
                if n1i >= n1n and n2i<n2n: #num1 is all added to new array
                    Arr.append(nums2[n2i])
                    n2i+=1
                    continue
                elif n1i<n1n and n2i>=n2n: #num2 is all added to new array
                    Arr.append(nums1[n1i])
                    n1i+=1
                    continue
                else: # both nums1 and nums2 have elements
                    if nums1[n1i] <= nums2[n2i]:
                        Arr.append(nums1[n1i])
                        n1i+=1
                        continue
                    else:
                        Arr.append(nums2[n2i])
                        n2i+=1
                        continue
            # After all elements added to new array, find the median.
            An = len(Arr)
            if (An%2)==1: #Odd
                return Arr[An//2]
            else:
                return (Arr[An//2]+Arr[(An//2)-1])/2

[筆記] FFmpeg 裁剪影片

本篇是一個使用 FFmpeg 裁剪影片的小筆記,主要參考自 stackoverflow 的問答

目的:

當已經使用剪輯軟體輸出好一個影片,而想要擷取一個影片片段,但又不想要重新編碼影片(花費很多時間),使用 FFmpeg 可以輕鬆完成這樣的要求。

使用軟體: FFmepg,跨平台的影片應用軟體,文件下載

閱讀更多

[Py] Improve the the geotag of the photo from flight record (DJI Spark)

The location difference between geotags from the photos (red dot) and flight record from DJI Go 4 app (blue solid line). DJI Spark is an entry-class drone with affordable price; however, after several field trip, I found that the geotag of the photo marked by the drone is actually not precise enough for building a digital model. Two figure shows the difference can reach 10 to 20 meters. I wrote a small python script to substitute the GPS tag of the photo.
(中文在下面)
Symbol:
Red Dots: original geotag of the photo.
Green Diamond: modified geotag of the photo
Blue Line: Flight Record (GPS track)
Gray Line: Comparison between old and new location of specific photo.
 
大疆的Spark是比較負擔的起的空拍機,家父很久以前買了一台被我搶來用了。在野外飛過幾次以後,因為要建立數值模型或是正射影像,發現像片的地理座標精度很差,網路上也有這樣的說法。於是研究了一下,寫了個小小的程式用時間對比飛行記錄軌跡,並替換像片的GPS座標。下面兩張圖可以看出原像片與軌跡記錄的差距大概是十到二十公尺。
 
圖例:
紅圓點:原記錄。
綠方點:對比軌跡後新位置。
藍線:飛行軌跡記錄。
灰線:同張像片新舊位置。

[GMT] GMT Wednesday – Install GMT in Ubuntu under Win10

A new semester has begun. I am once again the teaching assistant of the Generic Mapping Tool (GMT) class in NTU, Taiwan. From this week, I will share some class materials to help those who just start to learn GMT.

This week, I’ll share install GMT in Ubuntu on Win10. From last few months, Microsoft has enabled the Linux subsystem on the latest version of Windows 10. We will not need install the dual-OS which costs lots of resources.

The main benefit install the GMT under Linux/Unix-like system is that you can run all the GMT examples. Also many useful programs is pre-installed in Linux like ‘awk’.

Enable the support of Windows Subsystem for Linux

  • Open “Control Panel” (控制台)
  • Search “optional features”(開啟或關閉 Windows 功能)
  • check ” Windows Subsystem for Linux”(適用於Linux的Windows子系統)
  • ckick “OK”

You may be ask to reboot after installed the Windows subsystem for Linux.

Install Linux distibutions

  • Open “Microsoft Store”
  • Search “Ubuntu”
  • Click “get” to install
  • After installation, launch the ubuntu app.
  • It needs some time to install. After few minutes, ubuntu will ask you to set an account name and password. (they are not related to Windows account)

Update the Ubuntu system

  • Use the following command to get the update list:
    sudo apt update
  • Use the following command to upgrade programs:
    sudo apt upgrade

Follow the instructions on the screen.

Install Generic Mapping Tools (GMT)

Use the following command to install the GMT program:

sudo apt install gmt gmt-dcw gmt-gshhg ghostscript

Note

To access the Windows files, the Windows drives will be mount under “/mnt” directory. e.g, /mnt/d/example = D:\example

It takes about 400MB to install GMT and ghostscript

References

  • https://docs.microsoft.com/en-us/windows/wsl/install-win10
  • https://answers.microsoft.com/en-us/insider/forum/insider_wintp-insider_install/how-to-enable-the-windows-subsystem-for-linux/16e8f2e8-4a6a-4325-a89a-fd28c7841775?auth=1

 

[程式] Earthworm 學習筆記:20170725 [email protected](II)

Earthworm 是一套模組化、可擴張、穩定且跨系統的地震資料處理程式,早在1993年便開始開發,是一個非常成熟且具有彈性的地震資料處理程式。今天到臺大參加 Earthworm 的 Pre-workshop(正式的 Earthworm workshop在7/27星期四),由達逸學長教我們建立基本的 earthworm 系統,並建立一個 Wave server 給其他的客戶端使用。下面會精簡的記述課堂練習的成果。

下篇記述利用 slink2ew 與 wave_serverV 從 IRIS 接收資料並建立波形伺服器。

閱讀更多