---
title: "surface-pfp"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{surface-pfp}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(rtorf)
library(data.table)
```


```{r read1, eval = F}

cate = c("aircraft-pfp",
         "aircraft-insitu",
         "aircraft-flask",
         "surface-insitu",
         "surface-flask", 
         "surface-pfp",   
         "tower-insitu",  
         "aircore",       
         "shipboard-insitu",
         "shipboard-flask") 

obs <- "Z:/obspack/obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08/data/nc/"
index <- obs_summary(obs = obs, 
                     categories = cate)
```

```
#> Number of files of index: 429
#>               sector     N
#>               <char> <int>
#>  1:     aircraft-pfp    40
#>  2:  aircraft-insitu    15
#>  3:    surface-flask   106
#>  4:   surface-insitu   174
#>  5:   aircraft-flask     4
#>  6:          aircore     1
#>  7:      surface-pfp    33
#>  8:     tower-insitu    51
#>  9:  shipboard-flask     4
#> 10: shipboard-insitu     1
#> 11:    Total sectors   429
#> Detected 190 files with agl
#> Detected 239 files without agl
```

Now we read the `surface-pfp` using the function `obs_read_nc`. 
`solar_time` is  included for surface, so we `TRUE` that argument.

```{r readnc, eval = F}
datasetid <- "surface-pfp"
df <- obs_read_nc(index = index,
                  categories = datasetid,
                  solar_time = TRUE,
                  verbose = TRUE)

```

```
#> Searching surface-pfp...
#> 1: ch4_amt_surface-pfp_1_allvalid-107magl.nc
#> 2: ch4_bao_surface-pfp_1_allvalid-300magl.nc
#> 3: ch4_crv_surface-pfp_1_allvalid-32magl.nc
#> 4: ch4_inx_surface-pfp_1_allvalid-121magl.nc
#> 5: ch4_inx_surface-pfp_1_allvalid-125magl.nc
#> 6: ch4_inx_surface-pfp_1_allvalid-129magl.nc
#> 7: ch4_inx_surface-pfp_1_allvalid-130magl.nc
#> 8: ch4_inx_surface-pfp_1_allvalid-137magl.nc
#> 9: ch4_inx_surface-pfp_1_allvalid-39magl.nc
#> 10: ch4_inx_surface-pfp_1_allvalid-40magl.nc
#> 11: ch4_inx_surface-pfp_1_allvalid-54magl.nc
#> 12: ch4_klm_surface-pfp_1_allvalid-4magl.nc
#> 13: ch4_lef_surface-pfp_1_allvalid-244magl.nc
#> 14: ch4_lef_surface-pfp_1_allvalid-396magl.nc
#> 15: ch4_lew_surface-pfp_1_allvalid-95magl.nc
#> 16: ch4_mbo_surface-pfp_1_allvalid-11magl.nc
#> 17: ch4_mrc_surface-pfp_1_allvalid-east.nc
#> 18: ch4_mrc_surface-pfp_1_allvalid-south.nc
#> 19: ch4_msh_surface-pfp_1_allvalid-46magl.nc
#> 20: ch4_mvy_surface-pfp_1_allvalid-16magl.nc
#> 21: ch4_mwo_surface-pfp_1_allvalid-46magl.nc
#> 22: ch4_nwf_surface-pfp_1_allvalid-23magl.nc
#> 23: ch4_nwf_surface-pfp_1_allvalid-2magl.nc
#> 24: ch4_nwr_surface-pfp_1_allvalid-3magl.nc
#> 25: ch4_sct_surface-pfp_1_allvalid-305magl.nc
#> 26: ch4_sgp_surface-pfp_1_allvalid-60magl.nc
#> 27: ch4_sgp_surface-pfp_1_allvalid-9magl.nc
#> 28: ch4_str_surface-pfp_1_allvalid-232magl.nc
#> 29: ch4_wbi_surface-pfp_1_allvalid-379magl.nc
#> 30: ch4_wgc_surface-pfp_1_allvalid-484magl.nc
#> 31: ch4_wgc_surface-pfp_1_allvalid-89magl.nc
#> 32: ch4_wkt_surface-pfp_1_allvalid-122magl.nc
#> 33: ch4_wkt_surface-pfp_1_allvalid-457magl.nc
```

Now we check the data

```{r checkdf, eval = F}
df
```

```
#>         year month   day  hour minute second       time start_time
#>        <int> <int> <int> <int>  <int>  <int>      <int>      <int>
#>     1:  2008    11    23    16     22     49 1227457369 1227457369
#>     2:  2008    11    23    16     29     19 1227457759 1227457759
#>     3:  2008    11    23    16     35     54 1227458154 1227458154
#>     4:  2008    11    23    16     40     21 1227458421 1227458421
#>     5:  2008    11    23    16     44     47 1227458687 1227458687
#>    ---                                                            
#> 43159:  2021    12    23    20     45      5 1640292305 1640292305
#> 43160:  2021    12    25    20     45      8 1640465108 1640465108
#> 43161:  2021    12    27    20     45      6 1640637906 1640637906
#> 43162:  2021    12    29    20     45     13 1640810713 1640810713
#> 43163:  2021    12    31    20     45     10 1640983510 1640983510
#>        midpoint_time             datetime time_decimal time_interval
#>                <int>               <char>        <num>         <int>
#>     1:    1227457369 2008-11-23T16:22:49Z     2008.895          3600
#>     2:    1227457759 2008-11-23T16:29:19Z     2008.895          3600
#>     3:    1227458154 2008-11-23T16:35:54Z     2008.895          3600
#>     4:    1227458421 2008-11-23T16:40:21Z     2008.895          3600
#>     5:    1227458687 2008-11-23T16:44:47Z     2008.895          3600
#>    ---                                                              
#> 43159:    1640292305 2021-12-23T20:45:05Z     2021.978          3600
#> 43160:    1640465108 2021-12-25T20:45:08Z     2021.983          3600
#> 43161:    1640637906 2021-12-27T20:45:06Z     2021.989          3600
#> 43162:    1640810713 2021-12-29T20:45:13Z     2021.994          3600
#> 43163:    1640983510 2021-12-31T20:45:10Z     2022.000          3600
#>              value value_unc nvalue value_std_dev latitude longitude altitude
#>              <num>     <num>  <int>         <num>    <num>     <num>    <num>
#>     1: 1.87433e-06  1.31e-09      1            NA  45.0345  -68.6821    160.4
#>     2: 1.87468e-06  1.31e-09      1            NA  45.0345  -68.6821    160.4
#>     3: 1.87651e-06  1.31e-09      1            NA  45.0345  -68.6821    160.4
#>     4: 1.87619e-06  1.31e-09      1            NA  45.0345  -68.6821    160.4
#>     5: 1.87548e-06  1.31e-09      1            NA  45.0345  -68.6821    160.4
#>    ---                                                                       
#> 43159: 2.04697e-06  5.75e-10      1            NA  31.3149  -97.3269    708.0
#> 43160: 2.01324e-06  5.75e-10      1            NA  31.3149  -97.3269    708.0
#> 43161: 1.99720e-06  5.75e-10      2  2.828427e-11  31.3149  -97.3269    708.0
#> 43162: 2.03372e-06  5.75e-10      1            NA  31.3149  -97.3269    708.0
#> 43163: 2.03007e-06  5.75e-10      1            NA  31.3149  -97.3269    708.0
#>        elevation intake_height qcflag instrument   analysis_datetime method
#>            <num>         <num> <char>     <char>              <char> <char>
#>     1:      52.4           108    .S.         H6 2008-12-03T00:35:00      A
#>     2:      52.4           108    .S.         H6 2008-12-03T00:51:00      A
#>     3:      52.4           108    .S.         H6 2008-12-03T01:07:00      A
#>     4:      52.4           108    .S.         H6 2008-12-03T01:23:00      A
#>     5:      52.4           108    .S.         H6 2008-12-03T01:39:00      A
#>    ---                                                                     
#> 43159:     251.0           457    ...        PC2 2022-01-08T07:20:56      B
#> 43160:     251.0           457    ...        PC2 2022-01-08T07:36:12      B
#> 43161:     251.0           457    ...        PC2 2022-01-08T08:06:44      B
#> 43162:     251.0           457    ...        PC2 2022-01-08T08:22:01      B
#> 43163:     251.0           457    ...        PC2 2022-01-08T08:37:18      B
#>         event_number air_sample_container_id obs_flag obspack_num
#>               <char>                  <char>    <int>       <int>
#>     1:        264955                 3065-03        0     1131726
#>     2:        264956                 3065-04        0     1131727
#>     3:        264957                 3065-05        0     1131728
#>     4:        264958                 3065-06        0     1131729
#>     5:        264959                 3065-07        0     1131730
#>    ---                                                           
#> 43159:        498370                 3939-03        1     1172783
#> 43160:        498372                 3939-05        1     1172784
#> 43161: 498374,498375         3939-07,3939-08        1     1172785
#> 43162:        498376                 3939-09        1     1172786
#> 43163:        498378                 3939-11        1     1172787
#>                                                                                         obspack_id
#>                                                                                             <char>
#>     1: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_amt_surface-pfp_1_allvalid-107magl~1131726
#>     2: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_amt_surface-pfp_1_allvalid-107magl~1131727
#>     3: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_amt_surface-pfp_1_allvalid-107magl~1131728
#>     4: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_amt_surface-pfp_1_allvalid-107magl~1131729
#>     5: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_amt_surface-pfp_1_allvalid-107magl~1131730
#>    ---                                                                                            
#> 43159: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_wkt_surface-pfp_1_allvalid-457magl~1172783
#> 43160: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_wkt_surface-pfp_1_allvalid-457magl~1172784
#> 43161: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_wkt_surface-pfp_1_allvalid-457magl~1172785
#> 43162: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_wkt_surface-pfp_1_allvalid-457magl~1172786
#> 43163: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_wkt_surface-pfp_1_allvalid-457magl~1172787
#>        unique_sample_location_num year_st month_st day_st hour_st minute_st
#>                             <int>   <int>    <int>  <int>   <int>     <int>
#>     1:                   50239326    2008       11     23      12         1
#>     2:                   50239327    2008       11     23      12         7
#>     3:                   50239328    2008       11     23      12        14
#>     4:                   50239329    2008       11     23      12        18
#>     5:                   50239330    2008       11     23      12        23
#>    ---                                                                     
#> 43159:                   48733613    2021       12     23      14        16
#> 43160:                   48733614    2021       12     25      14        15
#> 43161:                   48733615    2021       12     27      14        14
#> 43162:                   48733616    2021       12     29      14        13
#> 43163:                   48733617    2021       12     31      14        12
#>        second_st          scale site_elevation_unit dataset_project
#>            <int>         <char>              <char>          <char>
#>     1:         4 WMO CH4 X2004A                masl     surface-pfp
#>     2:        34 WMO CH4 X2004A                masl     surface-pfp
#>     3:         9 WMO CH4 X2004A                masl     surface-pfp
#>     4:        36 WMO CH4 X2004A                masl     surface-pfp
#>     5:         2 WMO CH4 X2004A                masl     surface-pfp
#>    ---                                                             
#> 43159:        33 WMO CH4 X2004A                masl     surface-pfp
#> 43160:        40 WMO CH4 X2004A                masl     surface-pfp
#> 43161:        43 WMO CH4 X2004A                masl     surface-pfp
#> 43162:        55 WMO CH4 X2004A                masl     surface-pfp
#> 43163:        57 WMO CH4 X2004A                masl     surface-pfp
#>        dataset_selection_tag     site_name site_elevation site_latitude
#>                       <char>        <char>          <num>         <num>
#>     1:      allvalid-107magl Argyle, Maine           52.4       45.0345
#>     2:      allvalid-107magl Argyle, Maine           52.4       45.0345
#>     3:      allvalid-107magl Argyle, Maine           52.4       45.0345
#>     4:      allvalid-107magl Argyle, Maine           52.4       45.0345
#>     5:      allvalid-107magl Argyle, Maine           52.4       45.0345
#>    ---                                                                 
#> 43159:      allvalid-457magl  Moody, Texas          251.0       31.3149
#> 43160:      allvalid-457magl  Moody, Texas          251.0       31.3149
#> 43161:      allvalid-457magl  Moody, Texas          251.0       31.3149
#> 43162:      allvalid-457magl  Moody, Texas          251.0       31.3149
#> 43163:      allvalid-457magl  Moody, Texas          251.0       31.3149
#>        site_longitude  site_country site_code site_utc2lst lab_1_abbr
#>                 <num>        <char>    <char>        <num>     <char>
#>     1:       -68.6821 United States       AMT           -5       NOAA
#>     2:       -68.6821 United States       AMT           -5       NOAA
#>     3:       -68.6821 United States       AMT           -5       NOAA
#>     4:       -68.6821 United States       AMT           -5       NOAA
#>     5:       -68.6821 United States       AMT           -5       NOAA
#>    ---                                                               
#> 43159:       -97.3269 United States       WKT           -6       NOAA
#> 43160:       -97.3269 United States       WKT           -6       NOAA
#> 43161:       -97.3269 United States       WKT           -6       NOAA
#> 43162:       -97.3269 United States       WKT           -6       NOAA
#> 43163:       -97.3269 United States       WKT           -6       NOAA
#>        dataset_calibration_scale altitude_final type_altitude
#>                           <char>          <num>         <num>
#>     1:            WMO CH4 X2004A            108            NA
#>     2:            WMO CH4 X2004A            108            NA
#>     3:            WMO CH4 X2004A            108            NA
#>     4:            WMO CH4 X2004A            108            NA
#>     5:            WMO CH4 X2004A            108            NA
#>    ---                                                       
#> 43159:            WMO CH4 X2004A            457             0
#> 43160:            WMO CH4 X2004A            457             0
#> 43161:            WMO CH4 X2004A            457             0
#> 43162:            WMO CH4 X2004A            457             0
#> 43163:            WMO CH4 X2004A            457             0
#>        dataset_intake_ht_unit
#>                        <char>
#>     1:                   <NA>
#>     2:                   <NA>
#>     3:                   <NA>
#>     4:                   <NA>
#>     5:                   <NA>
#>    ---                       
#> 43159:                   magl
#> 43160:                   magl
#> 43161:                   magl
#> 43162:                   magl
#> 43163:                   magl
```

Now we can process the data. We first filter for observations
within our spatial domain:

## Checks and definitions

```{r spatial, eval = F}
north <- 80
south <- 10
west <- -170
east <- -50
max_altitude <- 8000
yy <- 2020
evening <- 14

```

We check altitude, intake_height, altitude_final and elevation.
altitude_final is a column from intake_height, added to match
column from obs_read text files.

```{r checkcols, eval = F}
df[, c("altitude", "altitude_final", "intake_height", "elevation",
       "dataset_selection_tag",
              "site_name")]
```

```
#>        altitude altitude_final intake_height elevation dataset_selection_tag
#>           <num>          <num>         <num>     <num>                <char>
#>     1:    160.4            108           108      52.4      allvalid-107magl
#>     2:    160.4            108           108      52.4      allvalid-107magl
#>     3:    160.4            108           108      52.4      allvalid-107magl
#>     4:    160.4            108           108      52.4      allvalid-107magl
#>     5:    160.4            108           108      52.4      allvalid-107magl
#>    ---                                                                      
#> 43159:    708.0            457           457     251.0      allvalid-457magl
#> 43160:    708.0            457           457     251.0      allvalid-457magl
#> 43161:    708.0            457           457     251.0      allvalid-457magl
#> 43162:    708.0            457           457     251.0      allvalid-457magl
#> 43163:    708.0            457           457     251.0      allvalid-457magl
#>            site_name
#>               <char>
#>     1: Argyle, Maine
#>     2: Argyle, Maine
#>     3: Argyle, Maine
#>     4: Argyle, Maine
#>     5: Argyle, Maine
#>    ---              
#> 43159:  Moody, Texas
#> 43160:  Moody, Texas
#> 43161:  Moody, Texas
#> 43162:  Moody, Texas
#> 43163:  Moody, Texas
```
The temporal range of data is
```{r range_year, eval = F}
range(df$year)
```

```
#> [1] 2005 2021
```

We also check for dimensions of data

```{r dim_df, eval = F}
dim(df)
```

```
#> [1] 43163    53
```

## Filters

```{r spatial_temporal_filter, eval = F}
df <- df[year == yy]

df <- df[altitude_final < max_altitude &
           latitude < north &
           latitude > south &
           longitude < east &
           longitude > west]
dim(df)
```

```
#> [1] 2205   53
```

Towers can have observations at different
heights. Here we need to select one site with the observations
registered at the highest height. The column
with the height is named `altitude_final` and the
max altitude was named `max_altitude`.


```{r, eval = F}
dfa <- df[,
          max(altitude_final),
          by = site_code] |> unique()

names(dfa)[2] <- "max_altitude"
dfa
```

```
#>     site_code max_altitude
#>        <char>        <num>
#>  1:       AMT       108.00
#>  2:       CRV        31.70
#>  3:       LEF       396.00
#>  4:       LEW        95.00
#>  5:       MBO        11.30
#>  6:       MRC        60.97
#>  7:       MSH        46.30
#>  8:       MWO        45.90
#>  9:       NWR         3.20
#> 10:       SCT       304.80
#> 11:       SGP        60.00
#> 12:       WBI       378.90
#> 13:       WGC        89.10
#> 14:       WKT       122.00
```


### Key Time

Here we need to start time columns.
The function `obs_addtime` adds time columns
`timeUTC`, `timeUTC_start` which shows the start time
of each observation and `timeUTC_end`
which shows the end time for each observation.


```{r, eval = F}
df2 <- obs_addtime(df)
```

```
#> Adding timeUTC
#> Adding timeUTC_start
#> Adding timeUTC_end
#> Found time_interval
```

Then we need a *key_time* to aggregate data.
This can be done using UTC, solar, or local time.
The normal approach is using afternoon solar or local time.


#### Hierarchy of solar or local time

1) Solar time
2) Local time with columns `site_utc2lst`
3) Local time longitude


> solar time (default)

Here we select the hours of interest and then
aggregate data by year, month and day of solar time.
In this way, we will have one information per day. however
this approach is not appropriate for aircraft
which are aggregated every 10 or 20 seconds. Hence
we need to aggregate data by one time column.
Also, this helps to generate the receptor info 
files including hour, minute and second.
Hence, *we need to add solar or local time column*.

```{r, eval = F}
df2$solar_time <- obs_addstime(df2)

```


> local time with column `site_utc2lst`

Then we need to identify the local time with the function
`add_ltime`. This is important because to identifying
observations in the evening in local time.
`add_ltime` uses two methods, first identify
the time difference with utc by identifying the
metadata column "site_utc2lst". 
If solar time is not available
#now we need to cut solar time for the frequency needed. 
As we will work with 



> local time longitude

If this information
is not available, with the aircrafts for instance,
the local time is calculated with an approximation
based on longitude:


$$
lt = UTC + longitude/15 * 60 * 60
$$
Where $lt$ is the local time, $UTC$ the time,
$longitude$ the coordinate.
Then, the time is cut every two hours.
Now, we identify the local time to select evening hours.



#### Cut time

Now we have they key column time, we can cut it accordingly.

```{r, eval = F}
df2$solar_time_cut <- cut(x = df2$solar_time,
                          breaks = "1 hour") |>
  as.character()

```


How we can check the solar time and the cut solar time.
Please note that solar_time_cut,
the column that it will be used to aggregate data

How we filter for the required solar time, in this case 14.

```{r, eval = F}
df3 <- df2[hour_st %in% evening]
df3[, c("solar_time", "solar_time_cut")]


```

```
#>                solar_time      solar_time_cut
#>                    <POSc>              <char>
#>    1: 2020-01-09 14:43:43 2020-01-09 14:00:00
#>    2: 2020-01-10 14:43:23 2020-01-10 14:00:00
#>    3: 2020-03-13 14:46:01 2020-03-13 14:00:00
#>    4: 2020-03-15 14:46:20 2020-03-15 14:00:00
#>    5: 2020-03-17 14:47:01 2020-03-17 14:00:00
#>   ---                                        
#> 1047: 2020-12-22 14:14:08 2020-12-22 14:00:00
#> 1048: 2020-12-24 14:13:21 2020-12-24 14:00:00
#> 1049: 2020-12-26 14:12:29 2020-12-26 14:00:00
#> 1050: 2020-12-28 14:11:32 2020-12-28 14:00:00
#> 1051: 2020-12-30 14:10:46 2020-12-30 14:00:00
```


At this point we can calculate the averages of several columns by
the cut time. The function `obs_agg` does this aggregation as shown
in the following lines of code. The argument `gby`
establish the function used to aggregate `cols`. I need to aggregate the data
by date (year, month, date), because it is already filtered by
the hours of interest. Then, I would have 1 observation per day.

As standard, let us define `key_time` as `solar_time`. 
The `obs_agg` function will aggregate the desired data by that column.

```{r, eval = F}
df3$key_time <- df3$solar_time_cut
```


```{r, eval = F}
df4 <- obs_agg(dt = df3,
               cols = c("value",
                        "latitude",
                        "longitude",
                        "site_utc2lst"),
               verbose = T,
               byalt = TRUE)
```

```
#> Selecting by alt
#> Adding time
```

Here we add the column `max_altitude` to identify the max altitude
by site. 

```{r, eval = F}
df4[,
    max_altitude := max(altitude_final),
    by = site_code]
df4[,
    c("site_code",
      "altitude_final",
      "max_altitude")] |> unique()

```

```
#>     site_code altitude_final max_altitude
#>        <char>          <num>        <num>
#>  1:       AMT         108.00       108.00
#>  2:       LEF         396.00       396.00
#>  3:       LEW          95.00        95.00
#>  4:       MRC          60.97        60.97
#>  5:       MSH          46.30        46.30
#>  6:       NWR           3.20         3.20
#>  7:       SCT         304.80       304.80
#>  8:       SGP          60.00        60.00
#>  9:       WBI         378.90       378.90
#> 10:       WGC          89.10        89.10
#> 11:       WKT         122.00       122.00
```


## Master

Before generating the receptors list, we have the database with all the required information

```{r rename, eval = F}
master <- df4

```
We may replace missing values with a nine nines. Here is commented

#master[is.na(master)] <- 999999999

We transform the time variables to character and round coordinates with 4 digits

```{r round, eval = F}
master$timeUTC <- as.character(master$timeUTC)
master$local_time <- as.character(master$local_time)
master$latitude <- round(master$latitude, 4)
master$longitude <- round(master$longitude, 4)

```

## Save master
Finally we save the master file

```{r outfile, eval = F}
out <- tempfile()

```

### txt

```{r outtxt, eval = F}
message(paste0(out,"_", datasetid, ".txt\n"))
fwrite(master,
       paste0(out,"_", datasetid, ".txt"),
       sep = " ")
```

```
#> C:\Users\sibarrae\AppData\Local\Temp\RtmpkdE8le\file844c7c2a3817_surface-pfp.txt
```

### csv

```{r outcsv, eval = F}
message(paste0(out,"_", datasetid, ".csv\n"))
fwrite(master,
       paste0(out,"_", datasetid, ".csv"),
       sep = ",")
```

```
#> C:\Users\sibarrae\AppData\Local\Temp\RtmpkdE8le\file844c7c2a3817_surface-pfp.csv
```

### csvy

CSVY are csv files with a YAML header to include metadata in tabulated
text files

```{r csvy, eval = F}
cat("\nAdding notes in csvy:\n")
notes <- c(paste0("sector: ", datasetid),
           paste0("timespan: ", yy),
           paste0("spatial_limits: north = ", north, ", south = ", south, ", east = ", east, ", west = ", west),
           paste0("altitude: < ", max_altitude),
           paste0("hours: ", evening),
           "local_time: used solar_time")

cat(notes, sep = "\n")

message(paste0(out,"_", datasetid, ".csvy\n"))
obs_write_csvy(dt = master,
               notes = notes,
               out = paste0(out,"_", datasetid, ".csvy"))
```

```
#> Adding notes in csvy:
#> sector: surface-pfp
#> timespan: 2020
#> spatial_limits: north = 80, south = 10, east = -50, west = -170
#> data: Data averaged every 20 seconds
#> altitude: < 8000
#> hours: 14
#> local_time: used solar_time
#> C:\Users\sibarrae\AppData\Local\Temp\RtmpkdE8le\file844c7c2a3817_surface-pfp.csvy
```


```{r readcsvy, eval = F}
obs_read_csvy(paste0(out,"_", datasetid, ".csvy"))
```

```
#>  [1] "---"                                                                
#>  [2] "name: Metadata "                                                    
#>  [3] "sector: surface-pfp"                                                
#>  [4] "timespan: 2020"                                                     
#>  [5] "spatial_limits: north = 80, south = 10, east = -50, west = -170"    
#>  [6] "data: Data averaged every 20 seconds"                               
#>  [7] "altitude: < 8000"                                                   
#>  [8] "hours: 14"                                                          
#>  [9] "local_time: used solar_time"                                        
#> [10] "structure: "                                                        
#> [11] "Classes 'data.table' and 'data.frame':\t1050 obs. of  20 variables:"
#> [12] " $ timeUTC                  : chr  \"2020-01-09 14:00:00\" \".."    
#> [13] " $ site_code                : chr  \"AMT\" \"AMT\" ..."             
#> [14] " $ altitude_final           : num  108 108 108 108 108 ..."         
#> [15] " $ type_altitude            : num  NA NA NA NA NA ..."              
#> [16] " $ lab_1_abbr               : chr  \"NOAA\" \"NOAA\" ..."           
#> [17] " $ dataset_calibration_scale: chr  \"WMO CH4 X2004A\" \"WMO \".."   
#> [18] " $ value                    : num  1.97e-06 2.01e-06 ..."           
#> [19] " $ latitude                 : num  45 45 ..."                       
#> [20] " $ longitude                : num  -68.7 -68.7 ..."                 
#> [21] " $ site_utc2lst             : num  -5 -5 -5 -5 -5 ..."              
#> [22] " $ year                     : int  2020 2020 2020 2020 202.."       
#> [23] " $ month                    : int  1 1 3 3 3 ..."                   
#> [24] " $ day                      : chr  \"09\" \"10\" ..."               
#> [25] " $ hour                     : int  14 14 14 14 14 ..."              
#> [26] " $ minute                   : int  0 0 0 0 0 ..."                   
#> [27] " $ second                   : int  0 0 0 0 0 ..."                   
#> [28] " $ time                     : num  1.58e+09 1.58e+09 ..."           
#> [29] " $ time_decimal             : num  2020 2020 ..."                   
#> [30] " $ max_altitude             : num  108 108 108 108 108 ..."         
#> [31] " $ local_time               : chr  NA NA ..."                       
#> [32] " - attr(*, \".internal.selfref\")=<externalptr> "                   
#> [33] "NULL"                                                               
#> [34] "---"
#>                   timeUTC site_code altitude_final type_altitude lab_1_abbr
#>                    <POSc>    <char>          <num>         <int>     <char>
#>    1: 2020-01-09 14:00:00       AMT            108            NA       NOAA
#>    2: 2020-01-10 14:00:00       AMT            108            NA       NOAA
#>    3: 2020-03-13 14:00:00       AMT            108            NA       NOAA
#>    4: 2020-03-15 14:00:00       AMT            108            NA       NOAA
#>    5: 2020-03-17 14:00:00       AMT            108            NA       NOAA
#>   ---                                                                      
#> 1046: 2020-12-22 14:00:00       WKT            122             0       NOAA
#> 1047: 2020-12-24 14:00:00       WKT            122             0       NOAA
#> 1048: 2020-12-26 14:00:00       WKT            122             0       NOAA
#> 1049: 2020-12-28 14:00:00       WKT            122             0       NOAA
#> 1050: 2020-12-30 14:00:00       WKT            122             0       NOAA
#>       dataset_calibration_scale        value latitude longitude site_utc2lst
#>                          <char>        <num>    <num>     <num>        <int>
#>    1:            WMO CH4 X2004A 1.971160e-06  45.0345  -68.6821           -5
#>    2:            WMO CH4 X2004A 2.005080e-06  45.0345  -68.6821           -5
#>    3:            WMO CH4 X2004A 1.959600e-06  45.0345  -68.6821           -5
#>    4:            WMO CH4 X2004A 1.961400e-06  45.0345  -68.6821           -5
#>    5:            WMO CH4 X2004A 1.959420e-06  45.0345  -68.6821           -5
#>   ---                                                                       
#> 1046:            WMO CH4 X2004A 2.067470e-06  31.3149  -97.3269           -6
#> 1047:            WMO CH4 X2004A 1.987230e-06  31.3149  -97.3269           -6
#> 1048:            WMO CH4 X2004A 1.990765e-06  31.3149  -97.3269           -6
#> 1049:            WMO CH4 X2004A 2.183760e-06  31.3149  -97.3269           -6
#> 1050:            WMO CH4 X2004A 2.084820e-06  31.3149  -97.3269           -6
#>        year month   day  hour minute second       time time_decimal
#>       <int> <int> <int> <int>  <int>  <int>      <int>        <num>
#>    1:  2020     1     9    14      0      0 1578578400     2020.023
#>    2:  2020     1    10    14      0      0 1578664800     2020.026
#>    3:  2020     3    13    14      0      0 1584108000     2020.198
#>    4:  2020     3    15    14      0      0 1584280800     2020.204
#>    5:  2020     3    17    14      0      0 1584453600     2020.209
#>   ---                                                              
#> 1046:  2020    12    22    14      0      0 1608645600     2020.974
#> 1047:  2020    12    24    14      0      0 1608818400     2020.980
#> 1048:  2020    12    26    14      0      0 1608991200     2020.985
#> 1049:  2020    12    28    14      0      0 1609164000     2020.991
#> 1050:  2020    12    30    14      0      0 1609336800     2020.996
#>       max_altitude local_time
#>              <num>     <lgcl>
#>    1:          108         NA
#>    2:          108         NA
#>    3:          108         NA
#>    4:          108         NA
#>    5:          108         NA
#>   ---                        
#> 1046:          122         NA
#> 1047:          122         NA
#> 1048:          122         NA
#> 1049:          122         NA
#> 1050:          122         NA
```



## Receptors

Now we can do the last step which is generating the receptor list files.
Now we filter selected columns

```{r receptors, eval = F}
receptor <- master[, c("site_code",
                     "year",
                     "month",
                     "day",
                     "hour",
                     "minute",
                     "second",
                     "latitude",
                     "longitude",
                     "altitude_final",
                     "type_altitude",
                     "time_decimal")]

```

We can round altitude also

```{r round_alt, eval = F}
receptor$altitude_final <- round(receptor$altitude_final)
```

Now we can format time variables with two digits

```{r formatrec, eval = F}
receptor <- obs_format(receptor,
                        spf =  c("month", "day",
                                 "hour", "minute", "second"))
```

We have a column that indicate AGL or ASL

```{r aslagl, eval = F}
receptor_agl <- receptor[type_altitude == 0]
receptor_asl <- receptor[type_altitude == 1]
```

Finally, we save the receptors

```{r save_receptors, eval = F}
if(nrow(receptor_agl) > 0) {
  message(paste0(out, "_", datasetid, "_receptor_AGL.txt"), "\n")

  fwrite(x = receptor_agl,
         file = paste0(out, "_", datasetid, "_receptor_AGL.txt"),
         sep = " ")
}

if(nrow(receptor_asl) > 0) {
  message(paste0(out, "_", datasetid, "_receptor_ASL.txt"), "\n")

  fwrite(x = receptor_asl,
         file = paste0(out, "_", datasetid, "receptor_ASL.txt"),
         sep = " ")

}
```

```
#> C:\Users\sibarrae\AppData\Local\Temp\RtmpkdE8le\file844c7c2a3817_surface-pfp_receptor_AGL.txt
```


## Plot

Finally, we just plot some data, run it locally


```{r obs_plotsave, fig.width=5, fig.height=3, eval = F, echo = F, message=F, warning=F}
png("../man/figures/obsplot_surfacepfp.png", width = 1500, height = 1000, res = 200)
obs_plot(df3, time = "timeUTC", yfactor = 1e9)
dev.off()
```

```{r obs_plot, fig.width=5, fig.height=3, eval = F}
obs_plot(df4, time = "timeUTC", yfactor = 1e9)
```
```
#> Found the following sites: 
#>  [1] AMT LEF LEW MRC MSH NWR SCT SGP WBI WGC WKT
#> Plotting the following sites: 
#> [1] AMT LEF
#> png 
#>   2
```
![Time series](https://github.com/ibarraespinosa/rtorf/blob/main/man/figures/obsplot_surfacepfp.png?raw=true)


```{r savesf, fig.width=5, fig.height=3, eval = F, echo = F}
library(sf)
dx <- df4[, 
    lapply(.SD, mean),
    .SDcols = "value",
    by = .(latitude, longitude)]
x <- st_as_sf(dx, coords = c("longitude", "latitude"), crs = 4326)
png("../man/figures/obsplot_surfacepfp_map.png", width = 1500, height = 1000, res = 200)
plot(x["value"], axes = T, reset = F)
maps::map(add = T)
dev.off()
```


```{r sf, fig.width=5, fig.height=3, eval = F}
library(sf)
dx <- df4[, 
    lapply(.SD, mean),
    .SDcols = "value",
    by = .(latitude, longitude)]
x <- st_as_sf(dx, coords = c("longitude", "latitude"), crs = 4326)
plot(x["value"], axes = T, reset = F)
maps::map(add = T)
```

![Map](https://github.com/ibarraespinosa/rtorf/blob/main/man/figures/obsplot_surfacepfp_map.png?raw=true)
