Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate numberType in get_ncdf4_attributes #63

Open
maier-m opened this issue Jan 16, 2018 · 2 comments
Open

Automate numberType in get_ncdf4_attributes #63

maier-m opened this issue Jan 16, 2018 · 2 comments

Comments

@maier-m
Copy link
Contributor

maier-m commented Jan 16, 2018

Seems to me to be a possibly useful idea to automate the generation of eml defined numberType from netCDF files by adding

values <- ncdf4::ncvar_get(nc, attributes[i])
result[i, 'eml_numberType'] <- get_numberType(values)

between lines 41 and 42 in https://github.com/NCEAS/arcticdatautils/blob/master/R/attributes.R
Where:

get_numberType <- function(values){
  numberType=NA
  if (is.numeric(values)){
    if (any(values%%1!=0,na.rm = T)){numberType='real'}
    else{
      if (any(values<0,na.rm = T)){numberType='integer'}
      else{
        if (any(values==0,na.rm = T)){numberType='whole'}
        else{
          numberType='natural'
        }
      }
    }
  }
  return(numberType)
}

Small but possibly helpful especially when having to go through multiple netCDF files with multiple variables. (although as written would overwrite any variables in the netCDF file named 'eml_numberType')

@jeanetteclark
Copy link
Collaborator

Only issue I see here is that if the netCDF is large, this could be very, very slow. You might want to run some tests on this first

@maier-m
Copy link
Contributor Author

maier-m commented Jan 23, 2018

Yes, jeanette. I noticed this problem, it was slow with how I was determining real numbers... much faster function below (along with other fixes)

get_numberType <- function(values){
  values <- unlist(values)
  numberType=NA
  if (is.numeric(values)){
    if (all(is.nan(values))){numberType='real'}
    else{
      if (any(round(values)!=values)){numberType='real'}
      else{
        if (any(values<0,na.rm = T)){numberType='integer'}
        else{
          if (any(values==0,na.rm = T)){numberType='whole'}
          else{
            numberType='natural'
          }
        }
      }
    }
  }
  return(numberType)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants