[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

20040121: Tcl script for compositing North/South GOES-12 scans



>From: Unidata User Support <address@hidden>
>Organization: Unidata Program Center/UCAR
>Keywords: McIDAS Unidata-Wisconsin

Hi Darren,

Here is the Tcl script I developed for compositing north and south
GOES-12 scans?

#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

#---------------------------------------------------------------------------
#
# Name:    cmdToList
#
# Purpose: return the output of a command as a list
#
# Method:  set list [cmdToList pgm fname]
#
#          pgm   - command to run
#          fname - name of file to read; if the text is output from
#                  a command, the user can either specify 'fname' as " "
#                  or leave it out altogether
#
# History: 20030519 - Adapted from Unidata MCGUI procedure of same name
#
#---------------------------------------------------------------------------

proc cmdToList { pgm {fname " "} } {

    global env

    # Insure that the process to be run recognizes that it is not a
    # child of the McIDAS text window (i.e. its output is not to go to
    # mctext)
    catch { unset env(MCENV_TEXT) }

    # Check to see if 'pgm' is likely a McIDAS executable
    if { [string match \[A-Z\] [string range $pgm 0 0]] } {
        regsub \[\"\] $fname "\\\"" fname
        set cmdname [string tolower $pgm].k
        puts "$cmdname $fname"
        set fhandle [open "|$cmdname $fname" r]
    } else {
        if { $fname != " " } {
            set vname [volName $fname]
        } else {
            set vname $fname
        }
        set fhandle [open "|$pgm $vname" r]
    }

    set readlist {}
    while { [gets $fhandle line] >= 0 } {
        lappend readlist $line
        puts $line
    }
    catch {close $fhandle}

    return $readlist

}

#---------------------------------------------------------------------------
#
# Name:    incDayTime
#
# Purpose: Increment time by a number of hours, minutes, seconds
#
# Input:   julday - CCYYDDD, YYDDD, DDD
#          time   - HH:MM:SS, HH:MM, HH
#          incsec - seconds (+/-)
#
# Returns: list containing new julday and time
#
# History: 20030519 - Written for Unidata-Wisconsin image preparation
#
#---------------------------------------------------------------------------

proc incDayTime { julday time {incsec 00} } {

    set secs [clock scan 0 -gmt 1]                   ; # 0 Z today
    set nowyear [clock format $secs -format "%Y" -gmt 1]

    set julday [string trimleft $julday "0"]

    # Extract year and Julian day of the year
    if { $julday <= 366 } {
      set year $nowyear
    } elseif { $julday >= 72001 && $julday <= 99366 } {
      set year [expr $julday / 1000 + 1900]
    } else {
      set year [expr $julday / 1000]
    }

    set jday [expr $julday % 1000]

    # Convert the input time into seconds
    set ss 0
    set mm 0
    set hh 0
    regsub -all ":" $time " " time
    scan $time "%d %d %d" hh mm ss
    set secs [expr 3600 * $hh + 60 * $mm + $ss]

    # Convert juldate to seconds from 1970
    set secs [expr [clock scan "1/1/$year" -gmt 1] + ($jday-1) * 86400 + $secs]

    # Add the day increment converted to seconds
    set secs [expr $secs + $incsec]

    # Return the converted Julian and time
    return [clock format $secs -format "%Y%j %T" -gmt 1]

}

#---------------------------------------------------------------------------
#
# Name:    printList
#
# Purpose: Print each element of a list
#
# History: 20030519 - Written for Unidata-Wisconsin image preparation
#
#---------------------------------------------------------------------------

proc printList { listing } {

    if { [llength $listing] > 0 } {
      foreach line $listing {
        puts $line
      }
    }

}

#---------------------------------------------------------------------------
#
# Name:    arraySet
#
# Purpose: Set values in array from elements in a list
#
# History: 20030519 - Written for Unidata-Wisconsin image preparation
#
#---------------------------------------------------------------------------

proc arraySet { listing } {

    if { [llength $listing] >= 6 } {
      set cur      [lindex $listing 4]
      set val(pos) [lindex $cur 0]
      set val(day) [lindex $cur 5]
      set val(tim) [lindex $cur 6]
      set nscan [scan $val(tim) "%d:%d:%d" hour min sec]
      if { $nscan == 3 } {
        set val(min) $min
      } else {
        set val(min) 0
      }
      set val(lat) [lindex $cur 7]
      set val(lon) [lindex $cur 8]
      set val(ban) [lindex $cur 9]
    } else {
      puts "listing not valid"
      printList $listing
      array set val
    }

      return [array get val]
}

    global env

    #
    # GOES-12 Northern Hemishpere scan information
    #

    set listing [cmdToList IMGLIST "EASTL/NH.-2"]
    # printList $listing

    if { [llength $listing] < 6 } {
      puts "no EASTL/NH images found.  Exiting..."
      exit 0
    }

    array set eastnh [arraySet $listing]
    puts "eastnh(pos) = $eastnh(pos)"
    set inset_nh EASTL/NH.$eastnh(pos)

    set combine 0

    if { $eastnh(lat) != 0 } {

      set listing [incDayTime $eastnh(day) $eastnh(tim) [expr -15*60]]
      set jday1 [lindex $listing 0]
      set time1 [lindex $listing 1]

      set listing [incDayTime $eastnh(day) $eastnh(tim) [expr +15*60]]
      set jday2 [lindex $listing 0]
      set time2 [lindex $listing 1]

      #
      # GOES-12 Southern Hemishpere scan information
      #
    
      set listing [cmdToList IMGLIST "EASTL/SH.ALL DAY=$jday1 $jday2 
TIME=$time1 $time2"]
      # printList $listing

      #
      # If there is at least one image that falls in the desired time range,
      # extract needed information.
      #

      if { [llength $listing] >= 6 } {

        set combine 1

        array set eastsh [arraySet $listing]
        puts "eastsh(pos) = $eastsh(pos)"
        set inset_sh EASTL/SH.$eastsh(pos)

      }

    }

    #
    # Create image sectors
    #

    foreach band { 2 3 4 6 1 } {

      set outset METFRM/BAND$band
      set keywords "LATLON=0 72 MAG=1 -2 STYPE=VISR "

      switch $band {
        1 {
            set keywords "$keywords SIZE=10000 6928"
        }
        default {
            set keywords "$keywords SIZE=2500 1732"
        }
      }

      # set listing [cmdToList IMGDEL "${outset}.1"]
      # printList $listing

      puts " "
      puts "--------- IMGCOPY $inset_nh to $outset BAND=$band ----------"
      puts " "

      set listing [cmdToList IMGCOPY "$inset_nh $outset BAND=$band $keywords"]
      # printList $listing

      if { [string first "No image moved" [lindex $listing 0]] != -1 } { 
        continue
      }
        
      if { $combine } {
        set listing [cmdToList IMGREMAP "$inset_sh ${outset}.1 BAND=$band 
MERGE=YES SMOOTH=NO NO"]
        # printList $listing
        puts "imgremap.k: Done"
      }

      set listing [cmdToList IMGCOPY "$outset METFORUM/BAND$band SIZE=ALL 
CYCLE=00:30 0"]
      # printList $listing

    }

Cheers,

Tom