How to convert FORTRAN subroutine to awk function?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert FORTRAN subroutine to awk function?
# 1  
Old 04-04-2013
How to convert FORTRAN subroutine to awk function?

Hi All I am trying to convert some Fortran subroutine to awk function , those who know kindly explain how to define function in awk, and how to call function

Fortran subroutine looks somewhat like this
Code:
  SUBROUTINE GDATE (JD, YEAR,MONTH,DAY)
C
C---COMPUTES THE GREGORIAN CALENDAR DATE (YEAR,MONTH,DAY)
C   GIVEN THE JULIAN DATE (JD).
C

     INTEGER JD,YEAR,MONTH,DAY,I,J,K
C
    L= JD+68569
    N= 4*L/146097
    L= L-(146097*N+3)/4
    I= 4000*(L+1)/1461001
    L= L-1461*I/4+31
    J= 80*L/2447
    K= L-2447*J/80
    L= J/11
    J= J+2-12*L
    I= 100*(N-49)+I+L
C
    YEAR= I
    MONTH= J
    DAY= K
C
    RETURN
    END

Code:
Example: YEAR = 1970, MONTH = 1, DAY = 1, JD = 2440588.

Code:
If input is 2440588

output should result.
Code:
Gregorian date for Julian date[JD] 2440588 is 01/01/1970

I just want to understand user defined function in awk here.

Last edited by Akshay Hegde; 04-05-2013 at 12:59 AM.. Reason: to modify body according to title.
# 2  
Old 04-04-2013
I guess this page should help you Smilie
# 3  
Old 04-04-2013
Thanks My problem is I didn't understand awk function, I am new to shell scripting, if someone explains I hope I can convert it.
# 4  
Old 04-04-2013
This is how you simply define a function Smilie

Code:
awk '
function JD2GD ( value )
{
#
# DO ALL YOUR CONVERSION MATH HERE
#
return (printf "%2d/%2d/%4d", d,m,y) #or you can just print here
}
{
GD=JD2GD($1);
print GD;
} ' file_with_juliandates

This User Gave Thanks to PikK45 For This Post:
# 5  
Old 04-04-2013
Quote:
Originally Posted by Akshay Hegde
Thanks My problem is I didn't understand awk function, I am new to shell scripting, if someone explains I hope I can convert it.
You would be well advised to follow PikK45's advice to look up an algorithm that works to calculate the Gregorian date from an astronomical Julian day number. The algorithm you originally posted in this thread doesn't work.

The only tricks to converting a FORTRAN subroutine to an awk function are to realize that awk performs divisions using floating point while FORTRAN uses integer division when dividing an integer by an integer and that all variables in awk are global except those declared to be local to a function (by including them in the function declaration arg list). (I use a tab (or two) between the input arguments that are required to be passed into the function to make it work and the list of local variables used by the function. If you don't like my convention for doing this, you should pick another method and use it consistently in any awk functions you write.)

You might also note that you had some variables (L and N) in your original FORTRAN code that were not declared.

The following awk script provides two awk functions (GDATE() and GDATE2()) that are straight translations of your original FORTRAN code and the algorithm specified by the page PikK45 suggested, respectively. If feeds two known astonomical Julian day numbers and prints the MONTH, DAY, and YEAR values set by both algorithms. The script is:
Code:
printf "%d\n" 2440588 2451545|awk '
# GDATE(JD)
# Compute and export YEAR, MONTH, and DAY of the Gregorian calendar date
# corresponding to the Julian date JD.
#
# Algorithm specified by Akshay Hegde
function GDATE(JD,      I, J, L, N) {
        L = JD + 68569
        N = int(4 * L / 146097)
        L = L - int((146097 * N + 3) / 4)
        I = int(4000 * (L + 1) / 1461001)
        L = L - 1461 * int(I / 4) + 31
        J = int(80 * L / 2447)
        DAY = L - int(2447 * J / 80)
        L = int(J / 11)
        MONTH = J + 2 - 12 * L
        YEAR = 100 * (N - 49) + I + L
}
# GDATE2(JD)
# Compute and export YEAR, MONTH, and DAY of the Gregorian calendar date
# corresponding to the Julian date JD.
#
# Algorithm specified by http://pmyers.pcug.org.au/General/JulianDates.htm
function GDATE2(j) {
        j = j - 1721119
        YEAR = int((4 * j - 1) / 146097)
        j = 4 * j - 1 - 146097 * YEAR 
        DAY = int(j / 4)
        j = int((4 * DAY + 3) / 1461)
        DAY = 4 * DAY + 3 - 1461 * j 
        DAY = int((DAY + 4) / 4)
        MONTH = int((5 * DAY - 3) / 153)
        DAY = 5 * DAY - 3 - 153 * MONTH 
        DAY = int((DAY + 5) / 5)
        YEAR = 100 * YEAR + j 
        if(MONTH < 10) MONTH = MONTH + 3
        else {
                MONTH = MONTH - 9
                YEAR = YEAR + 1
        }
}
{       GDATE($1)
        printf("Akshay Gregorian date for Julian date[JD] %d is %02d/%02d/%04d\n",
                $1, MONTH, DAY, YEAR)
        GDATE2($1)
        printf("PCUG Gregorian date for Julian date[JD] %d is %02d/%02d/%04d\n",
                $1, MONTH, DAY, YEAR)
}'

The output produced by this script is:
Code:
Akshay Gregorian date for Julian date[JD] 2440588 is 00/30/1971
PCUG Gregorian date for Julian date[JD] 2440588 is 01/01/1970
Akshay Gregorian date for Julian date[JD] 2451545 is 00/25/2003
PCUG Gregorian date for Julian date[JD] 2451545 is 01/01/2000

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 04-05-2013
anyways thank you so much for teaching me how to use function in awk, I will start to write my own functions

Last edited by Akshay Hegde; 04-06-2013 at 06:09 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

Subroutine or Function Summary

I have a fortran file with code declarations such as Subroutine str_tnum_tu & ( & s, dl, tu, pos & ) ! Class (*), Intent (InOut) :: tu(:) Character (Len=*), Intent (In) :: s, dl Character (Len=*), Intent (In), Optional :: pos ... or ... (11 Replies)
Discussion started by: kristinu
11 Replies

3. Programming

FORTRAN Simplification of Function

I have the following code that counts the number of consecuitive logicals from the first one. Any way I can simplify this function? Function count_present & ( & p1, p2, p3, p4, p5, p6, p7, p8 & ) ... (4 Replies)
Discussion started by: kristinu
4 Replies

4. Shell Programming and Scripting

Passing parameters to bash script function (or subroutine)

I've found a few posts regarding passing parameters to a function or subroutine, but for some reason when I try to run a command based on part with these parameters it's not working. If I have the function echo the parameters they show correctly so I believe they are being passed right but the... (2 Replies)
Discussion started by: withanh
2 Replies

5. UNIX for Dummies Questions & Answers

Help with Subroutine

Okay I have a 1TB drive that is almost completely full with vids. I am in the process of converting them to mp4. I have two scripts right now. One is a shell script to convert them with Handbrake. The other is a script to get a sort of progress report. To make things easier to understand, I will... (0 Replies)
Discussion started by: Dalton63841
0 Replies

6. Programming

c++ function to convert a linear list to circular list

hi all, i need a c++ function which converts a linear list to circular. presently i am working with two files. i.e., one linear list file. and one circular list file to do some operations. i thought it will be helpful if there is a function that converts a linear list to circular n undo the... (1 Reply)
Discussion started by: vidyaj
1 Replies

7. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

8. Shell Programming and Scripting

Running more than one function from a subroutine

Hi All I am new to perl so had one question. I have one Subroutine which works as expected and below is just an example. where I ran a command and check the output. so my question is if I have more than one command then how to check the results, meaning I want to add more command and check... (2 Replies)
Discussion started by: tannu
2 Replies

9. Shell Programming and Scripting

Using Subroutine / Function in case statement

I have issue running functions under case statement #!/bin/bash single() { Commands } multiple() { Commands } until ; do echo -e " \t \t M A I N - M E N U Perforce delete script \n" (1 Reply)
Discussion started by: sriram003
1 Replies

10. Programming

How to convert the "select" function into a "poll" function

i have a program using the select function but i want to convert it to poll... how can i do this? thanks in advance... :) (1 Reply)
Discussion started by: rbolante
1 Replies
Login or Register to Ask a Question