Fortran 77 le lt ge gt ne eq

  • Slides: 19
Download presentation

邏輯關係判斷 (與 Fortran 77 相同). le. . lt. . ge. . gt. . ne.

邏輯關係判斷 (與 Fortran 77 相同). le. . lt. . ge. . gt. . ne. . eq. . and. . not. . or. less than or equal less than greater than or equal to greater than not equal and not or . and. . not. . or. combine logical expressions

Algebraic Operators (可用在純數或陣列運算) + * / % > < # ^ Subtraction / Negation

Algebraic Operators (可用在純數或陣列運算) + * / % > < # ^ Subtraction / Negation Addition / String concatenation Multiplication Divide Modulus (integers only) Greater than selection Less-than selection Matrix multiply Exponentiation

+ 號也用在將字串結合 + is an overloaded operator (…) allows you to circumvent precedence rules

+ 號也用在將字串結合 + is an overloaded operator (…) allows you to circumvent precedence rules algebraic operator: x = 5. 3 + 7. 95 x = 13. 25 concatenate string: str = “pine” + “apple” str = “pineapple” algebraic operator and string concatenator: x = “alpha_” + (5. 3 + 7) + “_beta” x = “alpha_12. 3_beta”

與陣列有關的事項 手動建立陣列並給值 • array constructor characters (/…/) – a_integer = (/1, 9, -4/) –

與陣列有關的事項 手動建立陣列並給值 • array constructor characters (/…/) – a_integer = (/1, 9, -4/) – a_float = (/1. 0, -2 e 3, 3. 0/) – a_double = (/1, 2. 0, 3. 2 d /) – a_string = (/"abc", ” 12345", ”hello world"/) – a_logical = (/True, False, True/) – a_2 darray = (/ (/1, 2, 3/), (/4, 5, 6/), (/7, 8, 9/) /)

手動建立陣列,不給值 • new function [Fortran dimension, allocate ; C malloc] x = new (array_size/shape,

手動建立陣列,不給值 • new function [Fortran dimension, allocate ; C malloc] x = new (array_size/shape, type, _Fill. Value) – _Fill. Value is optional [assigned default if not specified] – “No_Fill. Value” means no missing value assigned – a = new(3, float) – b = new(10, double, 1 d 20) – c = new( (/5, 6, 7/), integer) – d = new(dimsizes(U), string) –

自動建立陣列 • data importation via supported format u = f->U ; all associated meta

自動建立陣列 • data importation via supported format u = f->U ; all associated meta data – same for subset of data: u = f->U(: , 3: 9: 2, : , 10: 20) – meta data (coordinate array will reflect subset) – • variable to variable assignment y=x y => same size, type as x plus meta data – no need to pre-allocate space for y –

減少或保留維度 • singleton dimensions eliminated (subtle point) • let T(12, 64, 128) – Tjan

減少或保留維度 • singleton dimensions eliminated (subtle point) • let T(12, 64, 128) – Tjan = T(0, : ) Tjan(64, 128) – Tjan automatically becomes 2 D: Tjan(64, 128) – array rank reduced; ‘degenerate’ dimension – all applicable meta data copied • can override dimension rank reduction – Tjan = T(0: 0, : ) Tjan(1, 64, 128) – TJAN = new( (/1, 64, 128/), typeof(T), T@_Fill. Value) TJAN(0, : ) = T(0, : )

Conform (一致化) • Arrays must conform: same size and shape • Scalars automatically conform

Conform (一致化) • Arrays must conform: same size and shape • Scalars automatically conform to all array sizes 範例:計算位溫 • let T and P be (10, 30, 180, 360) ; conforming arrays • theta = T*(1000/P)^0. 286 theta(10, 30, 180, 360) • non-conforming arrays; use built-in conform function • Let T be (10, 30, 180, 360) and P be (30) then • theta = T*(1000/conform(T, P, 1))^0. 286

條件判斷與迴圈 • if : conditional execution of one or more statements • do :

條件判斷與迴圈 • if : conditional execution of one or more statements • do : loops; fixed repetitions; for other languages • do while : until some condition is met • where : conditional/repetitive execution

 • if-then-end if if blocks (1) if ( all(a. gt. 0. ) )

• if-then-end if if blocks (1) if ( all(a. gt. 0. ) ) then …statements end if ; note: end if has space ; then is optional ; space is required • if-then-else-end if if ( any(ismissing(a)) ) then …statements else …statements end if • lazy expression evaluation [left-to-right] if ( any(b. lt. 0. ). and. all(a. gt. 0. ) ) then …statements end if

 • Technically, no ‘else if’ block but if blocks can be nested ifstrblocks

• Technically, no ‘else if’ block but if blocks can be nested ifstrblocks = "MAR” (2) if (str. eq. "JAN") then print("January") else if (str. eq. "FEB") then print("February") else if (str. eq. "MAR") then print("March") else if (str. eq. "APR") then print("April") else print("Enough of this!") end if ; must group all ‘end if’ end if ; at the end if ; not very clear end if

有條件的 do loops break: 離開迴圈 do i= 0, n-1. . . statements. . .

有條件的 do loops break: 離開迴圈 do i= 0, n-1. . . statements. . . if (foo. gt. 1000) then. . . statements. . . break end if. . . statements. . . end do. . . statements. . . continue: 跳過剩下的程式 碼,進入下一次的迴圈 do i=0, n-1. . . statements. . . if (foo. gt 1000) then. . statements. . . continue end if. . . statements. . . end do

不知道要跑幾圈時用 do while: 符合某些條件時結束迴圈 do while (foo. gt. 1000) ; 當foo>1000時結束迴圈. . . statements.

不知道要跑幾圈時用 do while: 符合某些條件時結束迴圈 do while (foo. gt. 1000) ; 當foo>1000時結束迴圈. . . statements. . . foo =. . . statements. . . end do. . . statements. . .

where 依照條件分別處理陣列中的不同元素 function where(條件, 符合時這樣做, 不符合時這樣做) similar to f 90 “where” statement ; q

where 依照條件分別處理陣列中的不同元素 function where(條件, 符合時這樣做, 不符合時這樣做) similar to f 90 “where” statement ; q is an array; q<0 => q=q+256 ; f 90: where(q. lt. 0) q=q+256 ; NCL: q = where (q. lt. 0, q+256, q) x = where (T. ge. 0. and. ismissing(Z) , a+25 , 1. 8*b) can not do: y = where(y. eq. 0, y@_Fill. Value, 1. /y) instead use: y = 1. 0/where(y. eq. 0, y@_Fill. Value, y)

mask • sets values to _Fill. Value that DO NOT equal mask array in

mask • sets values to _Fill. Value that DO NOT equal mask array in = addfile(“atmos. nc", "r") ts = in->TS(0, : ) oro = in->ORO(0, : ) ; mask ocean ; [ocean=0, land=1, sea_ice=2] ts = mask(ts, oro, 1)