---
title: "aircore"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{aircore}
  %\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: 479
#>               sector     N
#>               <char> <int>
#>  1:     aircraft-pfp    42
#>  2:  aircraft-insitu    17
#>  3:    surface-flask   106
#>  4:   surface-insitu   121
#>  5:   aircraft-flask     4
#>  6:          aircore     1
#>  7:      surface-pfp    33
#>  8:     tower-insitu   150
#>  9:  shipboard-flask     4
#> 10: shipboard-insitu     1
#> 11:    Total sectors   479
#> Detected 203 files with agl
#> Detected 276 files without agl
```

Now we read the `aircore` using the function `obs_read_nc`. To this date,
`solar_time` is not included for aircraft, so we `FALSE` that argument.

```{r readnc, eval = F}
datasetid <- "aircore"
df <- obs_read_nc(index = index,
                  categories = datasetid,
                  solar_time = FALSE,
                  verbose = T)

```

```
#> Searching aircore...
#> 1: ch4_aircorenoaa_aircore_1_allvalid.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:  2012     1    14    20     16     52 1326572212 1326572212
#>     2:  2012     1    14    20     16     54 1326572214 1326572214
#>     3:  2012     1    14    20     16     57 1326572217 1326572217
#>     4:  2012     1    14    20     16     59 1326572219 1326572219
#>     5:  2012     1    14    20     17      1 1326572221 1326572221
#>    ---                                                            
#> 72003:  2021     7    28    18     48      8 1627498088 1627498088
#> 72004:  2021     7    28    18     48     10 1627498090 1627498090
#> 72005:  2021     7    28    18     48     11 1627498091 1627498091
#> 72006:  2021     7    28    18     48     14 1627498094 1627498094
#> 72007:  2021     7    28    18     48     17 1627498097 1627498097
#>        midpoint_time                    datetime time_decimal time_interval
#>                <int>                      <char>        <num>         <int>
#>     1:    1326572212 2012-01-14T20:16:52.500000Z     2012.038             1
#>     2:    1326572214 2012-01-14T20:16:54.500000Z     2012.038             1
#>     3:    1326572217 2012-01-14T20:16:57.500000Z     2012.038             1
#>     4:    1326572219 2012-01-14T20:16:59.500000Z     2012.038             1
#>     5:    1326572221 2012-01-14T20:17:01.500000Z     2012.038             1
#>    ---                                                                     
#> 72003:    1627498088 2021-07-28T18:48:08.500000Z     2021.572             1
#> 72004:    1627498090 2021-07-28T18:48:10.500000Z     2021.572             1
#> 72005:    1627498091 2021-07-28T18:48:11.500000Z     2021.572             1
#> 72006:    1627498094 2021-07-28T18:48:14.500000Z     2021.572             1
#> 72007:    1627498097 2021-07-28T18:48:17.500000Z     2021.572             1
#>              value value_unc latitude  longitude altitude pressure temperature
#>              <num>     <num>    <num>      <num>    <num>    <num>       <num>
#>     1: 1.28550e-06  1.88e-09 36.85327  -98.20967 21685.83    42.22          NA
#>     2: 1.28640e-06  1.88e-09 36.85339  -98.20983 21626.17    42.63          NA
#>     3: 1.28762e-06  1.87e-09 36.85342  -98.21008 21565.31    43.03          NA
#>     4: 1.28904e-06  1.87e-09 36.85336  -98.21020 21508.82    43.43          NA
#>     5: 1.29071e-06  1.87e-09 36.85347  -98.21030 21453.79    43.81          NA
#>    ---                                                                        
#> 72003: 1.92660e-06  7.00e-10 39.95629 -104.16959  1801.49   820.30      303.02
#> 72004: 1.92784e-06  7.00e-10 39.95627 -104.16962  1787.17   828.84      303.20
#> 72005: 1.92688e-06  7.00e-10 39.95625 -104.16962  1776.80   822.42      303.32
#> 72006: 1.92769e-06  7.00e-10 39.95626 -104.16955  1757.47   824.24      303.39
#> 72007: 1.92808e-06  7.00e-10 39.95625 -104.16945  1735.74   826.09      303.66
#>           source_id  profile_id flight_id obs_flag obspack_num
#>              <char>      <char>    <char>    <int>       <int>
#>     1: NOAA AirCore ACBIG002_AC  ACBIG002        1     1174889
#>     2: NOAA AirCore ACBIG002_AC  ACBIG002        1     1174890
#>     3: NOAA AirCore ACBIG002_AC  ACBIG002        1     1174891
#>     4: NOAA AirCore ACBIG002_AC  ACBIG002        1     1174892
#>     5: NOAA AirCore ACBIG002_AC  ACBIG002        1     1174893
#>    ---                                                        
#> 72003: NOAA AirCore ACGMD009_AC  ACGMD009        1     1246891
#> 72004: NOAA AirCore ACGMD008_AC  ACGMD008        1     1246892
#> 72005: NOAA AirCore ACGMD009_AC  ACGMD009        1     1246893
#> 72006: NOAA AirCore ACGMD009_AC  ACGMD009        1     1246894
#> 72007: NOAA AirCore ACGMD009_AC  ACGMD009        1     1246895
#>                                                                                     obspack_id
#>                                                                                         <char>
#>     1: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_aircorenoaa_aircore_1_allvalid~1174889
#>     2: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_aircorenoaa_aircore_1_allvalid~1174890
#>     3: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_aircorenoaa_aircore_1_allvalid~1174891
#>     4: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_aircorenoaa_aircore_1_allvalid~1174892
#>     5: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_aircorenoaa_aircore_1_allvalid~1174893
#>    ---                                                                                        
#> 72003: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_aircorenoaa_aircore_1_allvalid~1246891
#> 72004: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_aircorenoaa_aircore_1_allvalid~1246892
#> 72005: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_aircorenoaa_aircore_1_allvalid~1246893
#> 72006: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_aircorenoaa_aircore_1_allvalid~1246894
#> 72007: obspack_ch4_1_GLOBALVIEWplus_v5.1_2023-03-08~ch4_aircorenoaa_aircore_1_allvalid~1246895
#>        unique_sample_location_num         scale dataset_project
#>                             <int>        <char>          <char>
#>     1:                   24769354 WMO CH4 X2004         aircore
#>     2:                   24769358 WMO CH4 X2004         aircore
#>     3:                   24769357 WMO CH4 X2004         aircore
#>     4:                   24769356 WMO CH4 X2004         aircore
#>     5:                   24769364 WMO CH4 X2004         aircore
#>    ---                                                         
#> 72003:                   46264921 WMO CH4 X2004         aircore
#> 72004:                   46264920 WMO CH4 X2004         aircore
#> 72005:                   46264901 WMO CH4 X2004         aircore
#> 72006:                   46264919 WMO CH4 X2004         aircore
#> 72007:                   46264902 WMO CH4 X2004         aircore
#>        dataset_selection_tag     site_name site_elevation site_latitude
#>                       <char>        <char>          <num>         <num>
#>     1:              allvalid NOAA AirCore          -1e+34        -1e+34
#>     2:              allvalid NOAA AirCore          -1e+34        -1e+34
#>     3:              allvalid NOAA AirCore          -1e+34        -1e+34
#>     4:              allvalid NOAA AirCore          -1e+34        -1e+34
#>     5:              allvalid NOAA AirCore          -1e+34        -1e+34
#>    ---                                                                 
#> 72003:              allvalid NOAA AirCore          -1e+34        -1e+34
#> 72004:              allvalid NOAA AirCore          -1e+34        -1e+34
#> 72005:              allvalid NOAA AirCore          -1e+34        -1e+34
#> 72006:              allvalid NOAA AirCore          -1e+34        -1e+34
#> 72007:              allvalid NOAA AirCore          -1e+34        -1e+34
#>        site_longitude  site_country   site_code lab_1_abbr
#>                 <num>        <char>      <char>     <char>
#>     1:         -1e+34 United States AirCoreNOAA       NOAA
#>     2:         -1e+34 United States AirCoreNOAA       NOAA
#>     3:         -1e+34 United States AirCoreNOAA       NOAA
#>     4:         -1e+34 United States AirCoreNOAA       NOAA
#>     5:         -1e+34 United States AirCoreNOAA       NOAA
#>    ---                                                    
#> 72003:         -1e+34 United States AirCoreNOAA       NOAA
#> 72004:         -1e+34 United States AirCoreNOAA       NOAA
#> 72005:         -1e+34 United States AirCoreNOAA       NOAA
#> 72006:         -1e+34 United States AirCoreNOAA       NOAA
#> 72007:         -1e+34 United States AirCoreNOAA       NOAA
#>        dataset_calibration_scale altitude_final type_altitude
#>                           <char>          <num>         <num>
#>     1:             WMO CH4 X2004       21685.83             1
#>     2:             WMO CH4 X2004       21626.17             1
#>     3:             WMO CH4 X2004       21565.31             1
#>     4:             WMO CH4 X2004       21508.82             1
#>     5:             WMO CH4 X2004       21453.79             1
#>    ---                                                       
#> 72003:             WMO CH4 X2004        1801.49             1
#> 72004:             WMO CH4 X2004        1787.17             1
#> 72005:             WMO CH4 X2004        1776.80             1
#> 72006:             WMO CH4 X2004        1757.47             1
#> 72007:             WMO CH4 X2004        1735.74             1
```


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

```

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")]
```

```
#>        altitude altitude_final
#>           <num>          <num>
#>     1: 21685.83       21685.83
#>     2: 21626.17       21626.17
#>     3: 21565.31       21565.31
#>     4: 21508.82       21508.82
#>     5: 21453.79       21453.79
#>    ---                        
#> 72003:  1801.49        1801.49
#> 72004:  1787.17        1787.17
#> 72005:  1776.80        1776.80
#> 72006:  1757.47        1757.47
#> 72007:  1735.74        1735.74
```

The temporal range of data is
```{r range_year, eval = F}
range(df$year)
```

```
#> [1] 2012 2021
```

We also check for dimensions of data

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

```
#> [1] 72007    39
```

## 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] 3256   39
```

## Adding time as POSIXct class

```{r add_time1, eval = F}
df <- obs_addtime(df)
df[, "timeUTC"]
```

```
#>                   timeUTC
#>                    <POSc>
#>    1: 2020-01-30 19:37:28
#>    2: 2020-01-30 19:37:32
#>    3: 2020-01-30 19:37:36
#>    4: 2020-01-30 19:37:40
#>    5: 2020-01-30 19:37:43
#>   ---                    
#> 3252: 2020-12-17 18:56:09
#> 3253: 2020-12-17 18:56:10
#> 3254: 2020-12-17 18:56:12
#> 3255: 2020-12-17 18:56:12
#> 3256: 2020-12-17 18:56:15
```

## Cut time

now we can cut time every 20 seconds. We can chosse other frequency as well.

```{r cut_sec, eval = F}
df$sec2 <- obs_freq(x = df$second,
                     freq = seq(0, 59, 20))
df[, c("second", "sec2")]
```

```
#>       second  sec2
#>        <int> <num>
#>    1:     28    20
#>    2:     32    20
#>    3:     36    20
#>    4:     40    20
#>    5:     43    40
#>   ---             
#> 3252:      9     0
#> 3253:     10     0
#> 3254:     12     0
#> 3255:     12     0
#> 3256:     15     0
```

## Aggregate data

now we need to add the column key_time, that it will be used
to aggregate other variables

```{r key_time, eval = F}
df$key_time <- ISOdatetime(year = df$year,
                           month = df$month,
                           day = df$day,
                           hour = df$hour,
                           min = df$minute,
                           sec = df$sec2,
                           tz = "UTC")
df[, c("timeUTC", "key_time")]

```

```
#>                   timeUTC            key_time
#>                    <POSc>              <POSc>
#>    1: 2020-01-30 19:37:28 2020-01-30 19:37:20
#>    2: 2020-01-30 19:37:32 2020-01-30 19:37:20
#>    3: 2020-01-30 19:37:36 2020-01-30 19:37:20
#>    4: 2020-01-30 19:37:40 2020-01-30 19:37:20
#>    5: 2020-01-30 19:37:43 2020-01-30 19:37:40
#>   ---                                        
#> 3252: 2020-12-17 18:56:09 2020-12-17 18:56:00
#> 3253: 2020-12-17 18:56:10 2020-12-17 18:56:00
#> 3254: 2020-12-17 18:56:12 2020-12-17 18:56:00
#> 3255: 2020-12-17 18:56:12 2020-12-17 18:56:00
#> 3256: 2020-12-17 18:56:15 2020-12-17 18:56:00
```
         
```{r aggregatingdata, eval = F}
df2 <- obs_agg(df, cols =  c("year",
                             "month",
                             "day",
                             "hour",
                             "minute",
                             "second",
                             "time",
                             "time_decimal",
                             "value",
                             "latitude",
                             "longitude",
                             "altitude_final",
                             # "elevation",
                             # "intake_height",
                             # "gps_altitude",
                             # "pressure",
                             # "pressure_altitude",
                             # "u", "v", "temperature",
                             "type_altitude"))
```
## Add local time

Now we add local time

```{r addlt, eval = F}
df3 <- obs_addltime(df2)
setorderv(df3, cols = c("site_code", "timeUTC"),
          order = c(-1, 1))
df3
```

```
#>                  timeUTC   site_code lab_1_abbr dataset_calibration_scale  year
#>                   <POSc>      <char>     <char>                    <char> <int>
#>   1: 2020-01-30 19:37:20 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>   2: 2020-01-30 19:37:40 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>   3: 2020-01-30 19:38:00 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>   4: 2020-01-30 19:38:20 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>   5: 2020-01-30 19:38:40 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>  ---                                                                           
#> 334: 2020-12-17 18:54:40 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#> 335: 2020-12-17 18:55:00 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#> 336: 2020-12-17 18:55:20 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#> 337: 2020-12-17 18:55:40 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#> 338: 2020-12-17 18:56:00 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>      month    day  hour minute second       time time_decimal        value
#>      <int> <char> <int>  <int>  <int>      <num>        <num>        <num>
#>   1:     1     30    19     37     20 1580413040     2020.081 1.912662e-06
#>   2:     1     30    19     37     40 1580413060     2020.081 1.913034e-06
#>   3:     1     30    19     38      0 1580413080     2020.081 1.913344e-06
#>   4:     1     30    19     38     20 1580413100     2020.081 1.913346e-06
#>   5:     1     30    19     38     40 1580413120     2020.081 1.913906e-06
#>  ---                                                                      
#> 334:    12     17    18     54     40 1608231280     2020.961 1.958356e-06
#> 335:    12     17    18     55      0 1608231300     2020.961 1.958784e-06
#> 336:    12     17    18     55     20 1608231320     2020.961 1.959021e-06
#> 337:    12     17    18     55     40 1608231340     2020.961 1.958858e-06
#> 338:    12     17    18     56      0 1608231360     2020.961 1.959788e-06
#>      latitude longitude altitude_final type_altitude          local_time    lh
#>         <num>     <num>          <num>         <num>              <POSc> <int>
#>   1: 39.68173 -103.7711       7926.282             1 2020-01-30 12:42:14    12
#>   2: 39.67923 -103.7686       7795.292             1 2020-01-30 12:42:35    12
#>   3: 39.67596 -103.7657       7647.354             1 2020-01-30 12:42:56    12
#>   4: 39.67261 -103.7625       7500.024             1 2020-01-30 12:43:16    12
#>   5: 39.66949 -103.7596       7357.494             1 2020-01-30 12:43:37    12
#>  ---                                                                          
#> 334: 39.47656 -103.8517       2353.402             1 2020-12-17 11:59:15    11
#> 335: 39.47749 -103.8506       2233.901             1 2020-12-17 11:59:35    11
#> 336: 39.47844 -103.8497       2121.761             1 2020-12-17 11:59:56    11
#> 337: 39.47984 -103.8488       2007.779             1 2020-12-17 12:00:16    12
#> 338: 39.48127 -103.8481       1905.712             1 2020-12-17 12:00:36    12
```

## Master

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

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

```
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\RtmpQ33K7x\file8b4862a53ca7_aircore.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\RtmpQ33K7x\file8b4862a53ca7_aircore.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),
           "data: Data averaged every 20 seconds",
           paste0("altitude: < ", max_altitude),
           "hours: All",
           "local_time: if var `site_utc2lst` is not available, calculated as",
           "longitude/15*60*60 (John Miller)")

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: aircore
#> timespan: 2020
#> spatial_limits: north = 80, south = 10, east = -50, west = -170
#> data: Data averaged every 20 seconds
#> altitude: < 8000
#> hours: All
#> local_time: if var `site_utc2lst` is not available, calculated as
#> longitude/15*60*60 (John Miller)
#> C:\Users\sibarrae\AppData\Local\Temp\RtmpQ33K7x\file8b4862a53ca7_aircore.csvy

```


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

```
#>  [1] "---"                                                               
#>  [2] "name: Metadata "                                                   
#>  [3] "sector: aircore"                                                   
#>  [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: All"                                                        
#>  [9] "local_time: if var `site_utc2lst` is not available, calculated as" 
#> [10] "longitude/15*60*60 (John Miller)"                                  
#> [11] "structure: "                                                       
#> [12] "Classes 'data.table' and 'data.frame':\t338 obs. of  19 variables:"
#> [13] " $ timeUTC                  : chr  \"2020-01-30 19:37:20\" \".."   
#> [14] " $ site_code                : chr  \"AirCoreNOAA\" \"AirCore\".."  
#> [15] " $ lab_1_abbr               : chr  \"NOAA\" \"NOAA\" ..."          
#> [16] " $ dataset_calibration_scale: chr  \"WMO CH4 X2004\" \"WMO C\".."  
#> [17] " $ year                     : int  2020 2020 2020 2020 202.."      
#> [18] " $ month                    : int  1 1 1 1 1 ..."                  
#> [19] " $ day                      : chr  \"30\" \"30\" ..."              
#> [20] " $ hour                     : int  19 19 19 19 19 ..."             
#> [21] " $ minute                   : int  37 37 38 38 38 ..."             
#> [22] " $ second                   : int  20 40 0 20 40 ..."              
#> [23] " $ time                     : num  1.58e+09 1.58e+09 ..."          
#> [24] " $ time_decimal             : num  2020 2020 ..."                  
#> [25] " $ value                    : num  1.91e-06 1.91e-06 ..."          
#> [26] " $ latitude                 : num  39.7 39.7 ..."                  
#> [27] " $ longitude                : num  -104 -104 ..."                  
#> [28] " $ altitude_final           : num  7926 7795 ..."                  
#> [29] " $ type_altitude            : num  1 1 1 1 1 ..."                  
#> [30] " $ local_time               : chr  \"2020-01-30 12:42:14.9\".."    
#> [31] " $ lh                       : int  12 12 12 12 12 ..."             
#> [32] " - attr(*, \".internal.selfref\")=<externalptr> "                  
#> [33] "NULL"                                                              
#> [34] "---"
#>                  timeUTC   site_code lab_1_abbr dataset_calibration_scale  year
#>                   <POSc>      <char>     <char>                    <char> <int>
#>   1: 2020-01-30 19:37:20 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>   2: 2020-01-30 19:37:40 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>   3: 2020-01-30 19:38:00 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>   4: 2020-01-30 19:38:20 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>   5: 2020-01-30 19:38:40 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>  ---                                                                           
#> 334: 2020-12-17 18:54:40 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#> 335: 2020-12-17 18:55:00 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#> 336: 2020-12-17 18:55:20 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#> 337: 2020-12-17 18:55:40 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#> 338: 2020-12-17 18:56:00 AirCoreNOAA       NOAA             WMO CH4 X2004  2020
#>      month   day  hour minute second       time time_decimal        value
#>      <int> <int> <int>  <int>  <int>      <int>        <num>        <num>
#>   1:     1    30    19     37     20 1580413040     2020.081 1.912662e-06
#>   2:     1    30    19     37     40 1580413060     2020.081 1.913034e-06
#>   3:     1    30    19     38      0 1580413080     2020.081 1.913344e-06
#>   4:     1    30    19     38     20 1580413100     2020.081 1.913346e-06
#>   5:     1    30    19     38     40 1580413120     2020.081 1.913906e-06
#>  ---                                                                     
#> 334:    12    17    18     54     40 1608231280     2020.961 1.958356e-06
#> 335:    12    17    18     55      0 1608231300     2020.961 1.958784e-06
#> 336:    12    17    18     55     20 1608231320     2020.961 1.959021e-06
#> 337:    12    17    18     55     40 1608231340     2020.961 1.958858e-06
#> 338:    12    17    18     56      0 1608231360     2020.961 1.959788e-06
#>      latitude longitude altitude_final type_altitude          local_time    lh
#>         <num>     <num>          <num>         <int>              <POSc> <int>
#>   1:  39.6817 -103.7711       7926.282             1 2020-01-30 12:42:14    12
#>   2:  39.6792 -103.7686       7795.292             1 2020-01-30 12:42:35    12
#>   3:  39.6760 -103.7657       7647.354             1 2020-01-30 12:42:56    12
#>   4:  39.6726 -103.7625       7500.024             1 2020-01-30 12:43:16    12
#>   5:  39.6695 -103.7596       7357.494             1 2020-01-30 12:43:37    12
#>  ---                                                                          
#> 334:  39.4766 -103.8517       2353.402             1 2020-12-17 11:59:15    11
#> 335:  39.4775 -103.8506       2233.901             1 2020-12-17 11:59:35    11
#> 336:  39.4784 -103.8497       2121.761             1 2020-12-17 11:59:56    11
#> 337:  39.4798 -103.8488       2007.779             1 2020-12-17 12:00:16    12
#> 338:  39.4813 -103.8481       1905.712             1 2020-12-17 12:00:36    12
```

## 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")]

```

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\RtmpQ33K7x\file8b4862a53ca7_aircore_receptor_ASL.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_aircore.png", width = 1500, height = 1000, res = 200)
obs_plot(df3, time = "timeUTC", yfactor = 1e9)
dev.off()
```

```{r obs_plot, fig.width=7, fig.height=5, eval = F}
obs_plot(df3, time = "timeUTC", yfactor = 1e9)
```

```
#> Found the following sites: 
#> [1] AirCoreNOAA
#> Plotting the following sites: 
#> [1] AirCoreNOAA
#> png 
#>   2
```

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


```{r savesf, fig.width=5, fig.height=3, eval = F, echo = F}
library(sf)
x <- st_as_sf(df3, coords = c("longitude", "latitude"), crs = 4326)
png("../man/figures/obsplot_aircore_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=7, fig.height=5, eval = F}
library(sf)
x <- st_as_sf(df3, 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_aircore_map.png?raw=true)



```{r vertplot, fig.width=5, fig.height=3, eval = F, echo = F, message=F, warning=F}
x <- df3
x$ch4 <- x$value*1e+9
png("../man/figures/obsplot_aircore_vert.png", width = 1500, height = 1000, res = 200)
obs_plot(x, 
         time = "ch4", 
         y = "altitude_final", 
         colu = "month", n = c(1L, 3L, 6L, 8L, 9L, 11L, 12L), 
         type = "b", 
         xlab = expression(CH[4]~ppb), 
         ylab = "altitude (m)")
dev.off()
```

```{r obs_plot_vertt, fig.width=7, fig.height=5, eval = F}
x <- df3
x$ch4 <- x$value*1e+9obs_plot(x, 
         time = "ch4", 
         y = "altitude_final", 
         colu = "month", n = c(1L, 3L, 6L, 8L, 9L, 11L, 12L), 
         type = "b", 
         xlab = expression(CH[4]~ppb), 
         ylab = "altitude (m)")
```


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

