Sunday, August 13, 2000

P1/90 Tide correction script

This is an old C-shell script I wrote for a applying tide corrections to P1/90 files. I was running on HP-UX 9.20, so this is rather old :)

This script will apply tide corrections to a number of files
in the current directory.

It will need some things to work:

A tide correction file in the following format
(This file needs to be in the same directory as the p190 file)

Julian seconds:
23673600       .25     274  0 0: 0  11098

Woodside format:
0: 0  11098     0.25


The script will not change the original files, it will make
a copy with tidal corrections applied.


Tide corrected p190 files will be put at : ./tide_corrected/
Original p190 files will be moved to     : ./uncorrected/


#/bin/csh -f
###############################################################################
#                     SEARCH AND REPLACE TEXT STRINGS                         #
###############################################################################
set find_text        = ", NOT FOR TIDES"    
set replace_text     = ", AND FOR TIDES"


###############################################################################
#     DO NOT TOUCH ANYTHING BEYOND THIS POINT UNLESS YOU KNOW WHAT YOU        #
#      ARE DOING. THE CHANGES THAT YOU MAKE TO THE SCRIPT BEYOND THIS         #
#             POINT COULD MAKE THE SCRIPT MISBEHAVE OR CRASH.                 #
#                 IN WORST CASE IT MIGHT AFFECT YOU DATA                      #
#                                                                             #      
#                                                                             #
#                                                                             #
#                              KENNETH THORMAN                                #
#                                18/10-1998                                   #
#                                                                             #
###############################################################################

###############################################################################
#                         CONSTANTS AND VARIABLES                             #
###############################################################################

set file_extension   = "tc"            
set temp_directory   = "./temp"    

############################## OUTPUT TO USER #################################
clear
echo "This script will apply tide corrections to a number of files"
echo "in the current directory."
echo " "
echo "It will need some things to work:"
echo " "
echo "A tide correction file in the following format "
echo "(This file needs to be in the same directory as the p190 file)"   
echo " "
echo "Julian seconds:"
echo "23673600       .25     274  0 0: 0  11098"
echo " "
echo "Woodside format:"
echo "0: 0  11098     0.25"
echo " "
echo " "
echo "The script will not change the original files, it will make"
echo "a copy with tidal corrections applied." 
echo " "
echo " "
echo "Tide corrected p190 files will be put at : ./tide_corrected/"
echo "Original p190 files will be moved to     : ./uncorrected/"
echo " "
echo " "
tput bold
echo -n "        DO YOU WISH TO CONTINUE (Y/N):"
tput sgr0
set continue = $<




##################### YES I WANT TO APPLY TIDE CORRECTIONS ####################

if (($continue == "y") || ($continue == "Y")) then
  repeat 2 echo " "
  tput bold
  


  # ASKING THE USER FOR THE FILE PREFIX
  echo -n "What is the prefix for the files you want to tide correct(HZI98..): "
  tput sgr0
  set file_prefix = $<
  


  # WRITING INFORMATION HEADER TO THE LOG (Tide.log)
  echo "        TIDE CORRECTION LOG" > Tide.log
  echo "        -------------------" >>Tide.log
  repeat 4 echo " " >> Tide.log
  


  # GETTING A LIST OVER ALL THE FILES IN THE CURRENT DIRECTORY THAT MATCH 
  # THE FILE PREFIX THE USER HAS TYPED IN
  set file_list = `ls -1 ${file_prefix}*`
  


  # WRITING ALL THE FILES OCCURING IN THE FILE_LIST TO THE LOG
  echo "Files specified for tidal correction" >> Tide.log

  foreach file($file_list)
    echo "${file}" >> Tide.log
  end
    


  # IF THE file_list IS EMPTY THEN THER IS NO FILES IN THE CURRENT
  # DIRECTORY, WITH THE USER SPECIFIED FIL PREFIX. WRITING THE OUTPUT
  # TO BOTH THE SCREEN AND TO THE LOG. IF THERE IS NO FILES WITH THE
  # SAME PREFIX AS SPECIFIED THEN EXIT
  if ($#file_list == 0) then
    tput bold
    echo " "
    echo "There is no file with the prefix ${file_prefix} in `pwd`" | tee -a Tide.log
    echo "Exiting..." | tee -a Tide.log
    tput sgr0
    exit
  endif  
  
  

  # ASKING THE USER FOR WHICH TIDAL CORRECTION FILE HE/SHE WANTS TO USE
  repeat 2 echo " "
  tput bold
  echo -n "What is the name of the tide correction file: " 
  tput sgr0
  set tide_cor_file = $<      

  

  # CHECKING IF THE TIDE CORRECTION FILE SPECIFIED BY THE USER 
  # EXISTS IN THE CURRENT DIRECTORY. KEEPS ON ASKING THE USER 
  # UNTIL A FILE THAT ACTUALLY EXITS IN THE CURRENT DIRECTORY IS
  # SPECIFIED. TO BREAK THE LOOP PRESS <CTRL> + C
  set tide_file_found = `ls -1 ${tide_cor_file}`
  
  while ($#tide_file_found == 0) 
    echo "There is no tide correction file named '$tide_cor_file' in this directory" | tee -a Tide.log
    repeat 2 echo " "
    tput bold
    echo -n "What is the name of the tide correction file: " 
    tput sgr0
    set tide_cor_file = $<      
    set tide_file_found = `ls -1 ${tide_cor_file}`
  end
  
  

  # WRITING INFORMATION TO THE LOG
  echo "Tide correction file set to ${tide_cor_file}" >> Tide.log
  
  

  # ASKING THE UDER WHAT FORMAT THE TIDE CORRECTION FILE IS IN
  repeat 2 echo " "
  tput bold
  echo "What format is ${tide_cor_file} (1, 2 or 3)? "
  tput sgr0
  echo " "
  echo "1. Julian seconds:"
  echo "   23673600       .25     274  0 0: 0  11098"
  echo "2. Woodside format:"
  echo "   0: 0  11098     0.25"
  echo "3. Other Format"
  echo " "
  tput bold
  echo -n "Format(1, 2 or 3): "
  tput sgr0
  set format = $<
  


  # IF THE FORMAT SPECIFIED BY THE USER IS JULIAN SECONDS THEN THE 
  # TIDE CORRECTION PROGRAM CAN USE THE FILE DIRECTLY. 
  if ($format == 1) then
    echo "Tide corretion file format is Julian seconds" >> Tide.log
  
  

  # IF THE FORMAT SPECIFIED BY THE USER IS WOODSIDE TIDE CORRECTION
  # FILE FORMAT THEN THE PROGRAM CANNOT USE THE FILE DIRECTLY. THE SCRIPT
  # WILL THEN CHANGE THE FORMAT TO JULIAN SECONDS.
  else if ($format == 2) then
    repeat 2 echo " "
    tput bold



    # WRITING INFORMATION TO THE LOG
    echo "Tide correction file is in Woodside format." >> Tide.log
    echo "Woodside format not compatible." >> Tide.log
    echo "Converting Woodside format to Julian seconds format" >> Tide.log



    # LETTING THE USER KNOW WHAT IS HAPPENING
    echo "Creating Julian seconds formatted tide correction file."
    echo "Please wait, this may take a while..."
    tput sgr0
    echo " "
    echo " "
    echo " "
    
    

    # CREATING THE TEXT FILE NEEDED FOR THE CONVERTION
    # FORMAT FOR THE TEXT FILE IS:
    #
    # LINE 1: NAME OF TIDE CORRECTION FILE IN WOODSIDE FORMAT
    # LINE 2: NAME OF THE OUTPUT FILE IN JULIAN SECONDS FORMAT
    #         (THIS IS THE ORIGINAL NAME + .js)
    echo $tide_cor_file > c2jtime_hp.script
    echo "$tide_cor_file.js" >> c2jtime_hp.script
    
    

    # USE THE TEXT FILE JUST CREATED TO CREATE THE NEW FORMATED
    # TIDE CORRECTION FILE AUTOMATICALLY. CALL TO C PROGRAM.
    # THE PROGRAM IS RESIDING IN /home.sprint/bin/
    c2jtime_hp <c2jtime_hp.script >> Tide.log
    
    

    # REMOVE THE TEXT FILE
    rm -f c2jtime_hp.script
  
  

  # IF THE USER SPECIFIES ANOTHER FORMAT THAN EITHER WOODSIDE OR
  # JULIAN SECONDS THEN THE SCRIPT CANNOT HANDLE IT SO EXIT
  else                         # If other format then abort
    repeat 2 echo " "
    tput bold



    # WRITE INFORMATION TO THE LOG
    echo "The format for the tide correction file is neither Woodside format" >> Tide.log
    echo "or Julian seconds format. Format not supported." >> Tide.log
    echo "Aborting." >> Tide.log
    echo "Exiting script" >> Tide.log



    # OUTPUT INFORMATION FOR THE USER
    echo "            FORMAT NOT SUPPORTED - ABORTING."
    tput sgr0
    exit
  endif
  
  

  # NOW GETTING READY TO TIDE CORRECT THE P190 FILES IN THIS DIRECTORY
  # THIS IS DONE BY A PROGRAM. THIS PROGRAM NORMALLY NEEDS USER INPUT
  # IN THIS FORM:
  # 1. WHAT IS THE TIDE CORRECTION YOU WANT TO USE CALLED?
  # 2. WHAT P190 FILE DO YOU WANT TO TIDE CORRECT?
  # 3. WHAT IS THE NAME FOR THE NEW P190 OUTPUT FILE?
  # 4. USE THE SAME TIDE CORRECTION FILE FOR ANOTHER P190 FILE?  
  repeat 2 echo " "
  tput bold
  echo "Creating script file needed by tide correction program: p190tide_hp"  
  echo "Please wait, this may take some time"
  echo "Creating script file needed for tide correction program." >> Tide.log
  echo "Script file named: tide.script" >> Tide.log
  tput sgr0
  echo " "
  
  set IsJulianSeconds = `ls -1 $tide_cor_file | grep -c ".js"`
  
  

  # IF THE TIDE CORRECTION FILE FORMAT IS JULIAN SECONDS
  # THEN JUST USE THE FILENAME AS SPECIFIED BY THE USER
  # BUT IF THE INITIAL FILE GIVEN BY THE USER IS WOODSIDE FORMAT
  # THEN THE SCRIPT HAS RENAMED THE CONVERTED FILE TO FILENAME + .js
  # THEN UED THIS FILENAME.
  # WRITE THIS INFORMATION TO THE SCRIPT FILE REQUIRED BY THE
  # P190 CONVERTION PROGRAM
  if ($IsJulianSeconds > 0) then 
    echo "${tide_cor_file}" > tide_script
  else
    echo "${tide_cor_file}.js" > tide_script
  endif
  
  

  # CREATING THE SCRIPT FILE FOR ALL OF THE P190 FILES THAT NEED
  # TIDE CORRECTIONS
  set file_list = `ls -1 ${file_prefix}*`
  foreach file ($file_list)
    


    # WRITE INPUT P190 FILE TO FILE
    echo $file >> tide_script
    


    # WRITE INFO TO LOG
    echo "${file} added to scriptfile." >> Tide.log
    


    # WRITE WHERE TO PUT THE TIDE CORRECTED OUTPUT FILE
    echo "./tide_corrected/${file}" >> tide_script
  


    # IF THE FILE IS NOT THE LAST FILE THE WRITE AN "y"
    # THIS IS THE ANSWER TO THE QUESTION
    # USE THE SAME TIDE CORRECTION FILE FOR ANOTHER P190 FILE?
    # IF IT IS THE LAST FILE THEN WRITE "n"
    if $file != $file_list[$#file_list] then
      echo "y" >> tide_script
    else
      echo "n" >> tide_script
    endif  
  end #foreach
  
  

  # TELL THE USER AND WRITE TO THE LOG THAT THE SCRIPT FILE IS FINISHED
  echo "Finished creating script file." | tee -a Tide.log
  
  

  # CHECKING THAT THE NECESARY DIRECTORY STRUCTURE IS EXISTING
  # IF IT IS NOT THEN CREATE WHAT IS MISSING
  echo "Now checking directory structure" | tee -a Tide.log
  if ! -d tide_corrected then         # checking if directory exits
    mkdir ./tide_corrected
    echo "Directory ./tide_corrected not found." | tee -a Tide.log
    echo "Creating directory ./tide_corrected." | tee -a Tide.log 
  else
    echo "Directory ./tide_corrected allready exists, so do not create." | tee -a Tide.log
  endif

  if ! -d uncorrected then        # checking if directory exits
    mkdir ./uncorrected
    echo "Directory ./uncorrected not found." | tee -a Tide.log
    echo "Creating directory ./uncorrected." | tee -a Tide.log 
  else
    echo "Directory ./uncorrected allready exists, so do not create." | tee -a Tide.log
  endif
  
  
  # TELL THE USER THAT THE SCRIPT IS FINISHED CREATING DIRECTORY
  # STRUCTURE
  echo "Finished checking directory structure" | tee -a Tide.log
  echo " "
  echo " "
  tput bold
  echo "Now tide correcting p190 files." | tee -a Tide.log
  echo "This may take a long time depending on how many files you are correcting"
  echo "Note: If you files contains SRG's this may take hours"
  tput sgr0
  repeat 2 echo " " | tee -a Tide.log
  
  
  
  # CALL TO THE TIDE CORRECTION PROGRAM. THIS PROGRAM IS A C PROGRAM
  # RESIDING IN /home/sprint/bin/.
  # THE PROGRAM IS CALLED WITH THE TEST FILE THAT CONTAINS THE
  # INFORMATION ABOUT WHICH FILES TO CORRECT, WHAT TO NAME THE OUTPUT
  # FILES AND WHAT TIDE CORRECTION FILE TO USE
  p190tide_hp <tide_script | tee -a Tide.log
  
  repeat 2 echo " " | tee -a Tide.log
  


  # WRINTING INFORMATION TO THE LOG
  cd ./tide_corrected
  set file_list = `ls ${file_prefix}*.p190`
  foreach file ($file_list)
    echo "${file} has been tide corrected" >> ../Tide.log 
  end
  cd ..
  

  
  # INFORMATION
  echo " " >> Tide.log
  echo "All files have now been tide corrected." | tee -a Tide.log
  
  
  
  # REMOVING THE TEXTFILE / SCRIPTFILE USED BY THE TIDE CCORRECTION
  # PROGRAM 
  rm -f tide_script
  echo "Removing tide.script" >> Tide.log
  
  
  
  # MOVING THE ORIGINAL FILES TO DIRECTORY ./uncorrected/ 
  tput bold
  echo " " 
  echo " "
  echo "Moving original p190 files to ./uncorrected" | tee -a Tide.log
  echo " "
  tput sgr0
  mv ./$file_prefix* ./uncorrected/
  
  cd tide_corrected
  
  
  
  # IF THERE IS NO TEMPORARY DIRECTORY IN ./tide_corrected THEN MAKE ONE
  if (! -d temp) then
    echo "Creating temp directory." >> ../Tide.log
    mkdir temp
  endif



  # GETTING READY TO TRY TO CHANGE THE HEADER IN THE TIDE
  # CORRECTED P190 FILES
  set file_list = `ls -1 ${file_prefix}*.p190`
  
  repeat 4 echo " "
  tput bold
  echo "The script is now going to try to change the header on the files"
  echo "that has been tide corrected."
  echo " "
  echo "The script makes a search/replace from the first line of the p190"
  echo "to line 40."
  echo " "
  echo "The search and replace values are specified in the script."
  echo "If the search and replace values does not suit you then"
  echo "you will have to change them in the script."
  echo " "
  echo " "
  echo "The current search value is : ${find_text}"
  echo "The current replace value is: ${replace_text}"
  echo " "
  echo " "
  echo "If you know that you p190 header specifies tidal correction in"
  echo "different way or with other textstrings than these mentioned"
  echo "above, then exit the script now and change the search and"
  echo "replace strings in the script before continuing"
  echo " "
  echo "You can find the script /home/sprint/bin/tide_correct "
  echo " "
  echo -n "          Do you wish to continue(Y/N):"
  set exit = $<
  echo " "
  
  if (($exit == "n") || ($exit == "N")) then
    echo "Script aborted by user." >> ../Tide.log
    exit
  endif
  
  
  
  # INFORMATION
  echo "Trying to change header for tide corrected files." | tee -a ../Tide.log
  tput sgr0
     
    
    
    # GET A LIST OVER ALL THE FILES THAT JUST HAVE BEEN TIDE CORRECTED
    # IF THE USER TIDE CORRECTS HIS FILES SEVERAL TIMES THE SCRIPT WILL
    # AUTOMATICALLY KNOW THIS AND IT WILL NOT TOUCH ANY FILES WITH THE
    # CORRECT HEADER
    set file_list = `ls -1 ${file_prefix}*{.p190,.cmb,.srg}` 
    foreach file ($file_list)
      
      
      
      # LOOKING FOR THE FIND_TEST IN THE P190.
      # IF THE USER USES ANOTHER WAY OF DESCRIBING IF THE P190 FILE IS 
      #    TIDE CORRECTED OR NOT TIDE CORRECTED THEN THE USER MUST CHANGE 
      # THE SEARCH STRING IN THE TOP OF THE SCRIPT. AS DEFAULT THE SCRIPT 
      # LOOKS FOR ", NOT FOR TIDES" AND REPLACES THIS WITH ", AND FOR TIDES"
      #
      # THIS IS A NORMAL HEADER IN A P190 (NO TIDE CORRECTION)
      #
      # H2600DEPTH DATA REDUCTION   CORRECTED FOR TRANSDUCER DEPTH, NOT FOR TIDES 
      #
      #
      #
      # THE SCRIPT COPIES THE P190 TO A TEMP DIRECTORY AND CHANGES THE 
      # HEADER TO THIS
      #
      # H2600DEPTH DATA REDUCTION   CORRECTED FOR TRANSDUCER DEPTH, AND FOR TIDES
      set number_of_finds = `cat ${file} | grep -c -E -e ", ${find_text}"`
      
      if ($#number_of_finds > 0) then
        echo "${#number_of_finds} match(es) to '${find_text}' found in ${file}" | tee -a ../Tide.log 
        echo "Now changing header for file: ${file}" | tee -a ../Tide.log
        sed "1,40 s/${find_text}/${replace_text}/g" ${file} > ${temp_directory}/${file}.${file_extension}
        rm -f ./${file}
      else
        echo "${file} does not contain any matches to '${find_text}'" | tee -a ../Tide.log
      endif  
    end
    
    
    
    # MOVING ALL THE TIDE CORRECTED P190 FILES WITH THE CORRECT HEADER
    # TO ./tide_corrected
    mv $temp_directory/* ./
    echo "Moving all files from temp directory to ./tide_corrected" >> ../Tide.log
    rm -f -r $temp_directory
  echo " "
  tput bold
 
 
 
 
  # CHECKING DIRECTORY ""./tide_corrected" IF ANY COMBINED FILES EXISTS
  # IF ANY .cmb    IS FOUND THEN CREATE README FILE.
  # BEFORE CREATING THE README FILE CHECK IF IT ALLREAY EXISTS
  echo "Checking ./tide_corrected for any .cmb files" | tee -a ../Tide.log
  set temp = `ls -1 $file_prefix* | grep -c 'cmb'`
  if ($temp > 0) then
    echo ".cmb files found."
    echo "Checking for README file in ./tide_corrected" | tee -a ../Tide.log 
    set temp = `ls -1 | grep -c 'README'`
    if ($temp == 0) then
      echo "No README file found in ./tide_corrected" |tee -a ../Tide.log
      echo "Creating README file in ./tide_corrected" |tee -a ../Tide.log
      makeREADME
    else
      echo "README exists" |tee -a ../Tide.log
    endif
  else
    echo "No .cmb files found" | tee -a ../Tide.log
  endif
  
  
  
  cd ..
  cd uncorrected
  echo " "
  
 
 
  # CHECKING DIRECTORY ""./uncorrected" IF ANY COMBINED FILES EXISTS
  # IF ANY .cmb    IS FOUND THEN CREATE README FILE.
  # BEFORE CREATING THE README FILE CHECK IF IT ALLREAY EXISTS
  echo "Checking ./uncorrected for any .cmb files" | tee -a ../Tide.log
  set temp = `ls -1 $file_prefix* | grep -c 'cmb'`
  if ($temp > 0) then
    echo ".cmb files found."
    echo "Checking for README file in ./uncorrected" | tee -a ../Tide.log 
    set temp = `ls -1 | grep -c 'README'`
    if ($temp == 0) then
      echo "No README file found in ./uncorrected" |tee -a ../Tide.log
      echo "Creating README file in /uncorrected" |tee -a ../Tide.log
      makeREADME
    else
      echo "README exists" |tee -a ../Tide.log
    endif
  else
    echo "No .cmb files found" | tee -a ../Tide.log
  endif
  
  cd ..
   
  echo " "
  tput sgr0
  echo "Finished" 
  
  ############### NO, I DON'T WANT TO APPLY TIDE CORRECTIONS ##################
else 
  exit
endif

3 comments:

Anonymous said...

Hi there,

Nice script, but confused about the Julian second. Day is straight forward 274 for 01/10/1998, but Julian date is 2451087.500000 for 00:00 am I missing something?

Guess you were on a Veritas DGC boat?! Do I need the other script from the sprint workstation referenced?

Thanks.

Kenneth Thorman said...

Hi

There were tide correction files in 2 main format to be used for correcting the tides.

The 2 samples described in the script were mainly to allow the user to choose what format their tidal correction files was in. There is no relations between the data that is used in the prompt to allow the user to select what tidal input format they want.

There are 2 external commands used in the script:

- c2jtime_hp (which converts from the Woodside format to the Julian time format)

and

- p190tide_hp (which applies the actual tidal correction)

These 2 scripts are needed but I have not been able to locate them. So either it is because I did not author them or because I simply have lost them in the mean time.

I suppose that I might still have them on the harddisk on my old laptop which I have somewhere.

You can send me a email if this is something you really need and I can look for it.

Regards
Kenneth Thorman

Anonymous said...

Hi Kenneth,

No problem. I am currently using TPXO 7.2 model data as the source for my P190 corrections.. I just stumbled across your script and thought it was pretty cool, and would be nice to have in the back pocket in case of an emergency if I had to apply corrections from a file..

Cheers,

Mark