Feature | Fortran Fortran 95 | c | Python | IDL | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compiler | f77 (g77) -o f95 (gfortran) -o cc (gcc) -o | not compiled, execute with | python #/usr/bin/env python within IDL, .run progname
| Overall structure
| program name | variable declarations program statements end include statments | main() { program statements return(0); } #!/usr/bin/env python | (to allow import, def main : program statements if __name__ == "__main__" : main() ) comments
| ! (C in f77)
| // /*...*/
| #
| ;
| line continuation / termination
| f95: & at end of line | f77: character in column 6 lines must end with ;
| implicit continuation using | expression in parentheses $ at end of line
| variables
| implicit declaration (i-o are integer), | but implicit none is preferred character, real, integer, logical f95: {TYPE} :: variable[=value] (f95) can be initialized INTEGER :: i=0 REAL :: f=3.4 CHARACTER(n) :: variable (f95 string of length n) f77: {TYPE} variable INTEGER i REAL f CHARACTER*n variable initialization through DATA statement must be declared (char, short, | int, float, double); can be initialized int i,j,k=0; real o,p=3.9; char str[20]; not declared
| not declared
| type conversion
|
|
|
|
| arrays
| e.g., real arr(m,n)
| e.g., float arr[n][m];
| e.g., numpy.zeros(n,m), | numpy.array([[a,b,c,d][e,f,g]]), numpy.arange(n) e.g., fltarr(m,n), indgen(n)
| dynamic memory allocation
| allocate, deallocate, after giving variable allocatable attribute
| malloc, free
|
|
| structures
| (derived data types) | type mytype character(12):: cval = '' real :: rval = 0. end type mytype type (mytype) :: var var%name = 'test' var%ra = 1. struct { | char cval[12]; real rval; } var; strcpy(var.name, "test"); var.ra = 1.; (dictionaries, see also lists) | var = { 'name' : 'test', ra : 1. } print var['name'], var['test'] var = { name: 'test', ra: 1. } | print,var.name, var.test structure arrays
|
type (mytype), dimension=nelem :: var |
|
sarray = np.zeros(nelem, | dtype=[('name','a12'),('ra','f4'),('dec','f4')]) sarray = np.zeros(nelem, dtype={'names': ('name', 'ra', 'dec'), 'formats': ('S12', 'f4', 'f4')}) sarray = replicate (str, nelem)
| operators | add, sub, mul, div, expon +, -, *, /, **
| +, -, *, /, pow(), +=, -=, *=, /=, ++, --
| +, -, *, /, **, +=, -=, *=, /=, ++, --
| +, -, *, /, ^, +=, -=, *=, /=, ++, --
| bitwise operators | and, or, xor, not iand, ior, ieor, not
| &, |, ^, ~
| &, |, ^, !
| and, or, xor, not
| string operators
| //, len, str(i1:i2)
| strcat(str,add), | strcpy(dest,src), strncpy(dest,src,n) strlen(str), strncpy(sub,str+i1,(i2-i1+1)) +, str[i1:i2] (string slice, | but string is immutable) +, strpos, strmid, strsplit, strlen
| matrix operations
| dot_product(a,b), matmul(a,b)
|
|
| #, ##
| conditionals
| if ( condition) then | statements else statements end if (for a single statement, can omit then and put statement on same line as if) (see also SELECT CASE) if (condition) { | statements } else { statements } (see also switch statement) if ( condition ) : | statements else : statements (extent of conditional statements specified by indentation) if (condition) then begin | statements endif else begin statements endelse (see also CASE statment) conditions | (logical operators) .eq. (==), .ne. (/=), .gt. (>), ge. (>=), .lt. (<), .le. (<=), .and., .or.
| ==, !=, >, >=, <, <=, &&, ||
| == != > >= < <=, and, or, not
| eq, ne, gt, ge, lt, le, &&, ||, ~
| looping
| do i=start,end,increment | statements end do for ( i=start; condition; action) { | statements } for i in ( list) : | statements (extent of loop statements specified by indentation) for i=start,end,increment | statements endfor looping with condition
| do while (condition) | statements enddo while (condition) { | statements } while (condition): | statements (extent of loop statements specified by indentation) while (condition)begin | statements endwhile simple output
| print *, 'characters', string, variable
| printf("characters: %s %d\n",string,variable);
| print 'characters', string, variable
| print,'characters: ', string,variable
| formatted output
| e.g., | CHARACTER :: FORMAT='(i8,f8.2)' PRINT FORMAT, i,f formats: in, fn.n, an, nx reference char format[] = "%2d %8.2f\n"; | printf(format,i,f); formats: %nd, %n.nf, %ns '{:8d}asdfad{:8.2f}'.format(i,f) | formats: {:nd}, {:n.nf}, {:ns} reference print,format='(fortranlike)'
| simple reading from terminal and file
| terminal: | READ *, var file: OPEN(lun,FILE=file) IERR=0 DO WHILE ( IERR == 0 ) READ(lun,format, IOSTAT=ierr) var PRINT *, line CLOSE(lun) terminal: | scanf("%s",char *); file: #include <stdio.h> char string[100]; FILE *fp; fp=fopen(file,"[rwa]"); while(fgets(string, 100, fp)) { printf("%s\n", string); } fclose(fp); terminal: | s=raw_input('prompt') file: f=open(file,'[rwa]') (read,write,append) s=f.readline() f.close(), alternatively: for line in f: print line
file: | line='' openr,lun,file,/get_lun while not EOF(lun) do begin readf,lun,line print,line end free_lun,lun higher level ASCII file reading routines
|
|
| data = numpy.loadtxt(file, | dtype=[('name','a12'),('ra',f4),('dec','f4')]) data = astropy.io.ascii(file) readcol,file,name,ra,dec,format='(a,f,f)' | read_ascii(file,template) FITS I/O routines
|
|
| import astropy.io.fits as fits | hdus=fits.open(file) hdus[0].header hdus[0].data
| |