ncss-tech / soilDB

soilDB: Simplified Access to National Cooperative Soil Survey Databases
http://ncss-tech.github.io/soilDB/
79 stars 19 forks source link

SPC Setup From Local Selected Set Failing #324

Closed MollicMeyer closed 7 months ago

MollicMeyer commented 7 months ago

I'm working with a selected set from eastern SD. I've never had this issue before. Any ideas why the dataframe won't build?

x <- fetchNASIS(from = 'pedons', SS = TRUE, rmHzErrors = TRUE, 
                nullFragsAreZero = FALSE, soilColorState = 'moist', lab = TRUE)
NOTE: some records are missing surface fragment covermixing dry colors ... [980 of 24763 horizons]
rgb2munsell() will be deprecated in aqp 2.1, please use col2Munsell() instead.
mixing moist colors ... [1842 of 33673 horizons]
rgb2munsell() will be deprecated in aqp 2.1, please use col2Munsell() instead.
NOTICE: multiple phiid values exist in the `phlabresults` table, computing weighted averages and dominant values based on horizon thickness
Error in data.frame(peiid = unique(x[["peiid"]]), lapply(x[2:ncol(x)],  : 
  arguments imply differing number of rows: 1, 0
In addition: Warning message:
non-standard notation in Munsell value, use getClosestMunsellChip()
MollicMeyer commented 7 months ago

When we select a handful, the function works properly. So it must be something on our end in NASIS. Perhaps incongruent numbers of matching sites and pedons?

dylanbeaudette commented 7 months ago

Not sure without knowing the exact record IDs or some other way to "find" these sites/pedons. Try setting lab = FALSE.

Also, consider updating to the latest versions of soilDB and aqp.

MollicMeyer commented 7 months ago

Sites = 8921; Pedons = 9341 The latest versions are installed. And setting lab = FALSE caused it to work for the whole selected set. However, the lab data is what we're chasing after.

brownag commented 7 months ago

My guess is this is due to pedon horizons with more than one phlabresults record which are missing "sampledepthtop" and/or "sampledepthbottom" values. This causes the weights used in the case of many:1 relationship to be missing i.e. NOTICE: multiple phiid values exist in the phlabresults table, computing weighted averages and dominant values based on horizon thickness

I will see if I can come up with a workaround based on this diagnosis, but in the meantime look for instances that meet these conditions (multiple lab sample records per pedon horizon without sample depths) and fix/remove them from your selected set.

dylanbeaudette commented 7 months ago

OK good.

I very much doubt you want these "lab data", unless you or someone you know has done some local characterization and put those values into the Horizon Lab Results table. If you are after the Soil Characterization Database records, it will take multiple requests to two different databases.

MollicMeyer commented 7 months ago

That makes sense @brownag thank you, I will take a look. And @dylanbeaudette that's a valid point, but we have a student expanding on some other work from a previous grad that looked at the relationship between field and lab particle size fractions estimates and their utility.

brownag commented 7 months ago

@MollicMeyer I think this is fixed by https://github.com/ncss-tech/soilDB/commit/a5375d47e2454c929cbcdd99113229340c7e3191. Please install latest off GitHub master branch and let me know

I believe only the categorical variables in the many:1 case needed fixing, the numeric cases correctly return NA value (rather than 0-length) if the weights are missing.

Now, in the case where the selection of categorical data can't be made based on the relative thickness of the subsample, the first record is taken--which corresponds to the first record with shallowest sampledepthtop within phiid (based on the sort order set by the phlabresults query).

Possibly the message needs to be further updated to indicate that some of the weights were missing, and that the source data should be inspected (done in https://github.com/ncss-tech/soilDB/commit/3c63e3de671c5b4f7a4de0eab57ad43f75796574)

MollicMeyer commented 7 months ago

Thank you! I will test it out shortly and let you know.

MollicMeyer commented 7 months ago

That seemed to solve the problem. The download completed correctly!