PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/filters/rstfilter/satmap/nbspsatmap.tcl

https://bitbucket.org/noaaport/nbsp
TCL | 269 lines | 181 code | 38 blank | 50 comment | 57 complexity | e140bcbd864df37d0e95a155d522b2ef MD5 | raw file
Possible License(s): 0BSD, AGPL-3.0
  1. #!%TCLSH%
  2. #
  3. # $Id: 7a82353cd47d0e3a8b020d3edda08aa0618e340d $
  4. #
  5. # Usage: nbspsatmap [-b] [-d <outputdir>] [-g <gpmap_gif>] [-k]
  6. # [-K] [-L <logfile>] [-o <outname>] [-p] [-q] [-s <outputsize>]
  7. # [-t <tmpdir>] [-D <defs>] <inputfile> [<rcfile>]
  8. #
  9. # -b => background mode
  10. # -d => output directory
  11. # -D => key=value,... comma separated list of gpmap(key)=var pairs
  12. # -g => the name (or full path of the gpmap_gif program)
  13. # -k => keep the gif file when [-p] is given
  14. # -K => keep the log file when finished
  15. # -L => specify the logfile instead of the default
  16. # -o => name of outputfile (otherwise the default used by nbspsat is used)
  17. # -p => output png instead of the default gif (gpmap_gif only outputs gif)
  18. # -q => silent (no normal output) [except for errors]
  19. # -s => image size. It is specified as, e.g, "1024;768".
  20. # If it is ";" then the original image size is used.
  21. # The default is "800;600".
  22. # -t => tmp dir (all paths are relative to current directory)
  23. #
  24. # The input file can the one with the compressed frames
  25. # or the uncompressed gini file.
  26. # If the <rcfile> is not specified, the program uses the same logic as the
  27. # rstfilter to search for the default and use that (in this case the program
  28. # uses the filterlib file, and also the rstfilter configuration file).
  29. #
  30. package require cmdline;
  31. set usage {nbspsatmap [-b] [-d outputdir] [-D <defs>] [-g gmap_gif]
  32. [-k] [-K] [-L <logfile>] [-o <outname>] [-p] [-q]
  33. [-s outputsize] [-t <tmpdir>] <inputfile> [<rcfile>]};
  34. set optlist {b {d.arg ""} {D.arg ""} {g.arg "gpmap_gif"} k K {L.arg ""}
  35. {o.arg ""} p q {s.arg "800;600"} {t.arg ""} };
  36. proc log_warn s {
  37. global argv0;
  38. global option;
  39. set name [file tail $argv0];
  40. if {$option(b) == 0} {
  41. puts "$name: $s";
  42. } else {
  43. exec logger -t $name $s;
  44. }
  45. }
  46. proc log_err s {
  47. log_warn $s;
  48. exit 1;
  49. }
  50. proc source_template {rcfile} {
  51. #
  52. # The template is sourced in a function so that the template cannot affect
  53. # the main script environment.
  54. #
  55. global gpmap;
  56. source $rcfile;
  57. }
  58. ## The common defaults (to get the location of the netpbm progs).
  59. set defaultsfile "/usr/local/etc/nbsp/filters.conf";
  60. if {[file exists $defaultsfile] == 0} {
  61. log_err "$defaultsfile not found.";
  62. }
  63. source $defaultsfile;
  64. set gpenvfile $gempak(envfile);
  65. if {[file exists $gpenvfile] == 0} {
  66. log_err "$gpenvfile not found.";
  67. }
  68. source $gpenvfile;
  69. #
  70. # main
  71. #
  72. array set option [::cmdline::getoptions argv $optlist $usage];
  73. set argc [llength $argv];
  74. if {$argc == 2} {
  75. set gpmap(inputfile) [lindex $argv 0];
  76. set option(rcfile) [lindex $argv 1];
  77. } elseif {$argc == 1} {
  78. set gpmap(inputfile) [lindex $argv 0];
  79. # Search for the rcfile using the same logic in the rstfilter
  80. source $common(filterslib);
  81. source [file join $common(libdir) "rstfilter.init"];
  82. if {$rstfilter(satmap_rcfile_fpath) eq ""} {
  83. set option(rcfile) [filterlib_find_conf $rstfilter(satmap_rcfile) \
  84. $rstfilter(satmap_rcdirs) $rstfilter(satmap_rcsubdir)];
  85. } else {
  86. set option(rcfile) $rstfilter(satmap_rcfile_fpath);
  87. }
  88. } else {
  89. log_err $usage;
  90. }
  91. # Definitions
  92. if {$option(D) ne ""} {
  93. set Dlist [split $option(D) ","];
  94. foreach pair $Dlist {
  95. set p [split $pair "="];
  96. set var [lindex $p 0];
  97. set val [lindex $p 1];
  98. set gpmap(sat,$var) $val;
  99. }
  100. }
  101. set gpmapbin $option(g);
  102. # gpmap_gif only outputs gif, even if "png" is set in the DEVICE in $gpmaprc
  103. set gpmap(fmt) "gif";
  104. if {$option(s) eq ";"} {
  105. set gpmap(devsize) "";
  106. } else {
  107. set gpmap(devsize) $option(s);
  108. }
  109. # Construct the command line for nbspsat. We pass [-i] to nbspsat
  110. # to extract the information without procesing.
  111. set nbspsatopts [list "-i"];
  112. if {$option(b) == 1} {
  113. lappend nbspsatopts "-b";
  114. }
  115. set params [eval exec nbspsat $nbspsatopts $gpmap(inputfile)];
  116. set sector [lindex $params 2];
  117. set channel [lindex $params 3];
  118. set res [lindex $params 4];
  119. set time [lindex $params 5];
  120. set nbspsat_outfbasename [lindex $params 6];
  121. if {$option(o) ne ""} {
  122. set outrootname [file rootname $option(o)];
  123. if {$option(p) == 1} {
  124. # This will be the temporary gif file name which later
  125. # will be converted to png and renamed.
  126. append gpmap(outputfile) $outrootname "." $gpmap(fmt);
  127. } else {
  128. set gpmap(outputfile) $option(o);
  129. }
  130. } else {
  131. # nbspsat creates png files and the outfname parameter here has the .png.
  132. set outrootname [file rootname $nbspsat_outfbasename];
  133. append gpmap(outputfile) $outrootname "." $gpmap(fmt);
  134. }
  135. file mkdir [file dirname $gpmap(outputfile)];
  136. if {$option(L) eq ""} {
  137. append logfile $outrootname ".log";
  138. } else {
  139. set logfile $option(L);
  140. }
  141. if {$option(d) ne ""} {
  142. set gpmap(outputfile) [file join $option(d) $gpmap(outputfile)];
  143. }
  144. if {$option(t) ne ""} {
  145. if {[file isdirectory $option(t)] == 0} {
  146. log_err "No such directory: $option(t)";
  147. } else {
  148. set cwd [pwd];
  149. set gpmap(inputfile) [file join $cwd $gpmap(inputfile)];
  150. set option(rcfile) [file join $cwd $option(rcfile)];
  151. set gpmap(outputfile) [file join $cwd $gpmap(outputfile)];
  152. cd $option(t);
  153. }
  154. }
  155. file delete $gpmap(outputfile);
  156. file delete "gemglb.nts" "last.nts";
  157. if {$option(K) == 0} {
  158. file delete $logfile;
  159. }
  160. # Temporary file names
  161. set outputfile $gpmap(outputfile);
  162. set gpmap(outputfile) ${outputfile}.lock.[pid];
  163. set _savedir "";
  164. set status [catch {
  165. source_template $option(rcfile);
  166. if {[info exists gpmap(script)] == 0} {
  167. log_err "gpmap(script) undefined.";
  168. }
  169. # To work around the gempak path length limitation
  170. set _savedir [pwd];
  171. cd [file dirname $gpmap(outputfile)];
  172. set gpmap(outputfile) [file tail $gpmap(outputfile)];
  173. set fout [open "|$gpmapbin >& $logfile" w];
  174. fconfigure $fout -translation binary -encoding binary;
  175. set script [subst $gpmap(script)];
  176. puts $fout $script;
  177. } errmsg];
  178. if {[info exists fout]} {
  179. catch {close $fout};
  180. }
  181. file delete "gemglb.nts" "last.nts";
  182. if {$option(K) == 0} {
  183. file delete $logfile;
  184. }
  185. # Restore working directory
  186. if {${_savedir} ne ""} {
  187. set gpmap(outputfile) [file join [pwd] $gpmap(outputfile)];
  188. cd ${_savedir};
  189. }
  190. if {$status != 0} {
  191. # In case gpmap created the file.
  192. file delete $gpmap(outputfile);
  193. log_err $errmsg;
  194. }
  195. # It is possible that gpmap_gif did not produce the image.
  196. if {[file exists $gpmap(outputfile)] == 0} {
  197. set _msg "Inputfile: $gpmap(inputfile).\n";
  198. append _msg "gpmap_gif did not produce a map for $outputfile.\n";
  199. append _msg "Probably: No entry in the image type table, imgtyp.tbl.";
  200. log_err ${_msg};
  201. } else {
  202. file rename -force $gpmap(outputfile) $outputfile;
  203. set gpmap(outputfile) $outputfile;
  204. }
  205. if {$option(p) == 1} {
  206. if {$option(o) eq ""} {
  207. set pngoutfile [file join \
  208. [file dirname $gpmap(outputfile)] $nbspsat_outfbasename];
  209. } else {
  210. set pngoutfile [file join \
  211. [file dirname $gpmap(outputfile)] $option(o)];
  212. }
  213. set status [catch {exec $filtersprogs(giftopnm) $gpmap(outputfile) \
  214. | $filtersprogs(pnmtopng) > $pngoutfile} errmsg];
  215. if {$option(k) == 0} {
  216. file delete $gpmap(outputfile);
  217. }
  218. if {[file exists $pngoutfile] && ([file size $pngoutfile] != 0)} {
  219. set gpmap(outputfile) $pngoutfile;
  220. set status 0;
  221. } else {
  222. file delete $gpmap(outputfile);
  223. log_err "Could not convert $gpmap(outputfile) to png." ;
  224. }
  225. }
  226. if {$option(q) == 0} {
  227. puts [lreplace $params 6 6 $gpmap(outputfile)];
  228. }
  229. if {[info exists gpmap(post)]} {
  230. eval $gpmap(post);
  231. }