[R] Narrowing values collected from .txt file

John Kane jrkrideau at inbox.com
Wed Aug 21 21:10:17 CEST 2013


I am not sure what happened but it may be that you accidentally sent your message in html but the text connection data seems unuseable.

It is much better to use ?dput to supply sample data.

Have a look at http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

John Kane
Kingston ON Canada


> -----Original Message-----
> From: emorway at usgs.gov
> Sent: Wed, 21 Aug 2013 06:50:07 -0700
> To: r-help at r-project.org
> Subject: [R] Narrowing values collected from .txt file
> 
> useRs-
> 
> The output generated from a groundwater model post-processor contains
> millions of lines of text.  Using the custom R function shown below, I
> can
> quickly gather values from this file.
> 
> As you can see in the textConnection provided below (which is only a
> small
> snippet from the file), the output is repetitive but does have some
> header
> lines I hope to make use of to narrow the collected output.  The header
> lines I'm speaking of are:
> 1) "     Flow Budget for Zone  1 at Time Step   1 of Stress Period   2"
> 2) "     Flow Budget for Zone  2 at Time Step   1 of Stress Period   2"
> 3) "     Flow Budget for Zone  3 at Time Step   1 of Stress Period   2"
> 4) "     Flow Budget for Zone  1 at Time Step   1 of Stress Period   3"
> ...
> 
> and so on for 111 different "zones" as well as 575 distinct "stress
> periods".  In the custom function that follows, currently named "g", I
> can
> collect all values of "Recharge".  If instead I want to restrict the
> collected "Recharge" values to "Zone 2" for all 575 stress periods, is
> there a way to first look for the header "Flow Budget for Zone  2",
> collect
> only the next two values of Recharge, and then skip down to the next
> header
> containing "Zone 2", collect 2 more values of "Recharge", and on like
> this
> to the end?  'Peeling' out targeted flow budget terms will facilitate
> generation of budget-specific plots through time.
> 
> The "edm" variable at the end of the R code that follows currently looks
> like this:
> edm
> # [1] 1.28980e+05 0.00000e+00 *2.74161e-01* 0.00000e+00 8.10840e+04
> 0.00000e+00
> # [7] 1.28980e+05 0.00000e+00 *2.74165e-01* 0.00000e+00 8.10840e+04
> 0.00000e+00
> 
> but with the proposed revision, which only collects Recharge values from
> Zone 2, it would look like:
> edm
> # [1] *2.74161e-01* 0.00000e+00 *2.74165e-01* 0.00000e+00
> 
> 
> txt_con<-textConnection(" mark_zone
> 
> 
>      Flow Budget for Zone  1 at Time Step   1 of Stress Period   2
>      -------------------------------------------------------------
> 
>                        Budget Term     Flow (L**3/T)
>                        -----------------------------
> 
>              IN:
>              ---
>                            STORAGE =   0.37855E-02
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =   0.12898E+06
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone  16 to   1 =    0.0000
>                    Zone  31 to   1 =    0.0000
>                    Zone  40 to   1 =    0.0000
>                    Zone  91 to   1 =    0.0000
> 
>                           Total IN =   0.12898E+06
> 
>              OUT:
>              ----
>                            STORAGE =   0.58275E-04
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =    0.0000
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone   1 to  16 =    399.88
>                    Zone   1 to  31 =    85204.
>                    Zone   1 to  40 =    12404.
>                    Zone   1 to  91 =    30968.
> 
>                          Total OUT =   0.12898E+06
> 
>                           IN - OUT =   0.14138E-03
> 
>                Percent Discrepancy =                0.00
> 1
>  mark_zone
> 
> 
>      Flow Budget for Zone  2 at Time Step   1 of Stress Period   2
>      -------------------------------------------------------------
> 
>                        Budget Term     Flow (L**3/T)
>                        -----------------------------
> 
>              IN:
>              ---
>                            STORAGE =   0.18833E-05
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =   0.274161E+06
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone  15 to   2 =    0.0000
>                    Zone  31 to   2 =    0.0000
>                    Zone  91 to   2 =    13134.
> 
>                           Total IN =   0.28729E+06
> 
>              OUT:
>              ----
>                            STORAGE =   0.10823E-04
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =    0.0000
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone   2 to  15 =    6812.7
>                    Zone   2 to  31 =   0.20820E+06
>                    Zone   2 to  91 =    72274.
> 
>                          Total OUT =   0.28729E+06
> 
>                           IN - OUT =   0.58504E-02
> 
>                Percent Discrepancy =                0.00
> 1
>  mark_zone
> 
> 
>      Flow Budget for Zone  3 at Time Step   1 of Stress Period   2
>      -------------------------------------------------------------
> 
>                        Budget Term     Flow (L**3/T)
>                        -----------------------------
> 
>              IN:
>              ---
>                            STORAGE =   0.84894E-04
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =    81084.
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone  31 to   3 =    0.0000
>                    Zone  91 to   3 =    1234.9
> 
>                           Total IN =    82319.
> 
>              OUT:
>              ----
>                            STORAGE =    0.0000
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =    0.0000
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone   3 to  31 =    53937.
>                    Zone   3 to  91 =    28382.
> 
>                          Total OUT =    82319.
> 
>                           IN - OUT =   0.81732E-03
> 
>                Percent Discrepancy =                0.00
> 1
>  mark_zone
> 
> 
>      Flow Budget for Zone  1 at Time Step   1 of Stress Period   3
>      -------------------------------------------------------------
> 
>                        Budget Term     Flow (L**3/T)
>                        -----------------------------
> 
>              IN:
>              ---
>                            STORAGE =   0.15770E-04
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =   0.12898E+06
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone  16 to   1 =    0.0000
>                    Zone  31 to   1 =    0.0000
>                    Zone  40 to   1 =    0.0000
>                    Zone  91 to   1 =    0.0000
> 
>                           Total IN =   0.12898E+06
> 
>              OUT:
>              ----
>                            STORAGE =   0.38262E-02
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =    0.0000
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone   1 to  16 =    399.88
>                    Zone   1 to  31 =    85214.
>                    Zone   1 to  40 =    12405.
>                    Zone   1 to  91 =    30958.
> 
>                          Total OUT =   0.12898E+06
> 
>                           IN - OUT =   0.88928E-03
> 
>                Percent Discrepancy =                0.00
> 1
>  mark_zone
> 
> 
>      Flow Budget for Zone  2 at Time Step   1 of Stress Period   3
>      -------------------------------------------------------------
> 
>                        Budget Term     Flow (L**3/T)
>                        -----------------------------
> 
>              IN:
>              ---
>                            STORAGE =    0.0000
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =   0.274165E+06
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone  15 to   2 =    0.0000
>                    Zone  31 to   2 =    0.0000
>                    Zone  91 to   2 =    13215.
> 
>                           Total IN =   0.28737E+06
> 
>              OUT:
>              ----
>                            STORAGE =   0.27267E-02
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =    0.0000
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone   2 to  15 =    6813.6
>                    Zone   2 to  31 =   0.20827E+06
>                    Zone   2 to  91 =    72291.
> 
>                          Total OUT =   0.28737E+06
> 
>                           IN - OUT =   0.69125E-03
> 
>                Percent Discrepancy =                0.00
> 1
>  mark_zone
> 
> 
>      Flow Budget for Zone  3 at Time Step   1 of Stress Period   3
>      -------------------------------------------------------------
> 
>                        Budget Term     Flow (L**3/T)
>                        -----------------------------
> 
>              IN:
>              ---
>                            STORAGE =    0.0000
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =    81084.
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone  31 to   3 =    0.0000
>                    Zone  91 to   3 =    1262.7
> 
>                           Total IN =    82346.
> 
>              OUT:
>              ----
>                            STORAGE =   0.18113E-03
>                      CONSTANT HEAD =    0.0000
>                           RECHARGE =    0.0000
>                     STREAM LEAKAGE =    0.0000
>                      LAKE  SEEPAGE =    0.0000
>                             UZF ET =    0.0000
>                              GW ET =    0.0000
>                       UZF INFILTR. =    0.0000
>                   SFR-DIV. INFLTR. =    0.0000
>                       UZF RECHARGE =    0.0000
>                    SURFACE LEAKAGE =    0.0000
>                    Zone   3 to  31 =    53843.
>                    Zone   3 to  91 =    28503.
> 
>                          Total OUT =    82346.
> 
>                           IN - OUT =  -0.14018E-02
> 
>                Percent Discrepancy =                0.00
> ")
> 
> 
> g<-function(txt_con, string, from, to, ...) {
>         L <- readLines(txt_con)
>         matched <- grep(string, L, value = TRUE, ...)
>         as.numeric(substring(matched, from, to))
> }
> 
> #Now, strip out values
> edm<-g(txt_con, "  RECHARGE =", 37, 50)
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!



More information about the R-help mailing list