|
| 1 | +ato_dep_from_glatos <- function(glatos_file, type = "meta") { |
| 2 | + # Read in the file we've been given if we haven't been handed a dataframe. |
| 3 | + if (!is.data.frame(glatos_file)) { |
| 4 | + # Grab the extension. |
| 5 | + extension <- tools::file_ext(otn_detections) |
| 6 | + # If it's a parquet, read it in as one... |
| 7 | + if (extension == "parquet") { |
| 8 | + otn_file <- read_parquet(otn_detections) |
| 9 | + } else if (extension == "xlsx" || extension == "xls") { |
| 10 | + otn_file <- read_excel(otn_detections) |
| 11 | + } else { |
| 12 | + # Otherwise bring it in as a CSV. |
| 13 | + otn_file <- read.csv(otn_detections, na = c("", "null", "NA")) |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + # If we've been given a metadata file, we read it in as one. |
| 18 | + if (type == "meta") { |
| 19 | + # This we can pull directly from the metadata. |
| 20 | + dep <- make_dep( |
| 21 | + receiver_model = otn_file$INS_MODEL_NO, |
| 22 | + receiver_serial = otn_file$INS_SERIAL_NO, |
| 23 | + receiver_codeset = otn_file$CODE_SET, |
| 24 | + deploy_location = otn_file$STATION_NO, |
| 25 | + deploy_datetime = otn_file$DEPLOY_DATE_TIME, |
| 26 | + deploy_lat = otn_file$DEPLOY_LAT, |
| 27 | + deploy_lon = otn_file$DEPLOY_LON, |
| 28 | + deploy_z = otn_file$BOTTOM_DEPTH, |
| 29 | + recover_datetime = otn_file$RECOVER_DATE_TIME, |
| 30 | + recover_lat = otn_file$RECOVER_LAT, |
| 31 | + recover_lon = otn_file$RECOVER_LON, |
| 32 | + transmitter = otn_file$TRANSMITTER, |
| 33 | + transmitter_manufacturer = NA_character_, |
| 34 | + transmitter_ping_rate = NA_character_, |
| 35 | + transmitter_model = otn_file$TRANSMIT_MODEL, |
| 36 | + transmitter_serial = NA_character_ |
| 37 | + ) |
| 38 | + return(dep) |
| 39 | + } else if (type == "extract") { |
| 40 | + # In the case where we don't have a metadata file to work with, we can use a similar setup to how we derive |
| 41 | + # receiver data from detection extracts for IMOS. |
| 42 | + |
| 43 | + # To start, we will filter the releases out of our detections dataframe. |
| 44 | + no_releases <- otn_file %>% filter(receiver != "release") |
| 45 | + |
| 46 | + # The first thing we need to do is gin up some inferred min and max deployment dates. |
| 47 | + # We'll use the following code to do so. |
| 48 | + rcvr_grouped <- NULL |
| 49 | + |
| 50 | + # Start by grouping the detections by station, and ordering them by date. |
| 51 | + rcvr_grouped_list <- no_releases %>% |
| 52 | + group_by(station) %>% |
| 53 | + arrange(dateCollectedUTC, .by_group = TRUE) |
| 54 | + |
| 55 | + # Set min date and max date to null. |
| 56 | + minDate <- NULL |
| 57 | + maxDate <- NULL |
| 58 | + |
| 59 | + # Create a 'lead' dataframe for us to compare our current dataframe against. |
| 60 | + rcvr_grouped_list_next <- lead(rcvr_grouped_list) |
| 61 | + |
| 62 | + # For each row in the list |
| 63 | + for (i in 1:nrow(rcvr_grouped_list)) { |
| 64 | + row <- rcvr_grouped_list[i, ] |
| 65 | + |
| 66 | + # If minDate is null, set it to the currently available date. minDate being null implies that |
| 67 | + # we're just starting with this station (see where it's set to Null, below) |
| 68 | + if (is.null(minDate)) { |
| 69 | + minDate <- row$dateCollectedUTC |
| 70 | + } |
| 71 | + |
| 72 | + # Get the next row from our "lead" frame. |
| 73 | + nextStation <- rcvr_grouped_list_next[i, ] |
| 74 | + |
| 75 | + # If our next station is Null (i.e, we're at the end of the frame), or the next station is different from |
| 76 | + # the current one (i.e, we've reached the end of this time chunk)... |
| 77 | + if (is.na(nextStation$receiver) || nextStation$receiver != row$receiver) { |
| 78 | + # Set Maxdate to our current date. |
| 79 | + maxDate <- row$dateCollectedUTC |
| 80 | + |
| 81 | + # Add the min and max dates as entries in the row. |
| 82 | + row <- row %>% mutate( |
| 83 | + minDetectionDate = minDate, |
| 84 | + maxDetectionDate = maxDate |
| 85 | + ) |
| 86 | + |
| 87 | + # if rcvr_group hasn't been instantiated yet, use row to create it. |
| 88 | + if (is.null(rcvr_grouped)) { |
| 89 | + rcvr_grouped <- row |
| 90 | + } |
| 91 | + # Otherwise, just add the row to the group of receivers. |
| 92 | + else { |
| 93 | + rcvr_grouped <- rbind(rcvr_grouped, row) |
| 94 | + } |
| 95 | + |
| 96 | + # reset our min and max date to null so the next group will be handled properly. |
| 97 | + minDate <- NULL |
| 98 | + maxDate <- NULL |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + dep <- make_dep( |
| 103 | + receiver_model = NA_character_, |
| 104 | + receiver_serial = as.integer(rcvr_grouped$receiver), |
| 105 | + receiver_codeset = rcvr_grouped$codeSpace, |
| 106 | + deploy_location = rcvr_grouped$station, |
| 107 | + deploy_datetime = as.POSIXct(NA_real_), |
| 108 | + deploy_lat = rcvr_grouped$decimalLatitude, |
| 109 | + deploy_lon = rcvr_grouped$decimalLongitude, |
| 110 | + recover_datetime = as.POSIXct(NA_real_), |
| 111 | + recover_lat = NA_real_, |
| 112 | + recover_lon = NA_real_, |
| 113 | + transmitter = NA_character_, |
| 114 | + transmitter_model = NA_character_, |
| 115 | + transmitter_serial = NA_integer_, |
| 116 | + tz = "UTC" |
| 117 | + ) |
| 118 | + |
| 119 | + return(dep) |
| 120 | + } else { |
| 121 | + message("Invalid type specified. Use either 'meta' (if loading from a metadata file) or 'extract' (if deriving deployment metadata from a detection extract).") |
| 122 | + } |
| 123 | +} |
0 commit comments