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

as.unit() ought to use a csv driver file, like rename() does #2255

Open
dankelley opened this issue Oct 13, 2024 · 4 comments
Open

as.unit() ought to use a csv driver file, like rename() does #2255

dankelley opened this issue Oct 13, 2024 · 4 comments
Assignees

Comments

@dankelley
Copy link
Owner

Doing that will simplify the code, and also make it a lot easier for users to extend the capabilities of as.unit().

This is in-line with the way rename() works, and how (frankly) things ought to have worked for a long time. I think this can be done without causing problems, but I'll do it in a new branch (named "as_unit") to avoid any problems, since I expect I'll be adding quite a few units, to handle the data that I lately download from IOOS servers.

@dankelley dankelley self-assigned this Oct 13, 2024
@dankelley
Copy link
Owner Author

The snapshot shows the idea. We need to enclose the first column in quotes, to allow commas in regular expressions. Quotes would also be needed in the 3rd column if the scale contains a comma. I suppose we don't really need quotes in most of those lines, so I might remove them.

Anyway, the code will work a lot like how rename() works, so that users can supply their own unit mappings as files or data frames, or use a built-in mapping.

I won't bother explaining the format more here; the docs will be the place for that.

Screenshot 2024-10-13 at 10 45 58 AM

@dankelley
Copy link
Owner Author

Actually, there is no need for anchors (the ^ and $ characters) because we don't have that for the rename() definition files.

@dankelley
Copy link
Owner Author

Protection is needed for superscripts, e.g. if we use

kg/m\^3,kg/m^3,

in the file, it can be decoded with (e.g. after reading the file into U):

> U[which(sapply(U[,1], \(p) grepl(paste0("^",p,"$"),"kg/m^3"))),2]
[1] "kg/m^3"
> U[66,]
         V1     V2 V3
66 kg/m\\^3 kg/m^3

@dankelley
Copy link
Owner Author

These are the units (as read from the csv file I'm working on) as of now. I'll think this reproduces what I have hard-wired at the moment, but I have not checked yet. My next step will be to give as.unit() a new parameter to read a CSV file like this. (The idea is to keep the old behaviour as a default, and to enable this new behaviour slowly.)

V1 V2 V3
MicroEinstein[s]{0,1} m-2 s-1 mu*Einsten/m^2/s
count[s]{0,1} count
dbar[s]{0,1} dbar
decibar[s]{0,1} dbar
degree[s]{0,1} decimal minutes degree dec. min.
degree[s]{0,1} degree
degree[s]{0,1}.C degree*C
degree[s]{0,1}.celsius degree*C
degree[s]{0,1}.east degree*E
degree[s]{0,1}.north degree*N
degree[s]{0,1}.south degree*S
degree[s]{0,1}.west degree*W
degree[s]{0,1}_C degree*C
degree[s]{0,1}_east degree*East
degree[s]{0,1}_north degree*North
pH unit[s]{0,1} pH
% %
/m/sr m^-1*sr^-1
1
1/m 1/m
NTU NTU
PSS-78 PSS-78
S m-1 S/m
S m^-1 S/m
S/m S/m
ah A*hr
amp A
amp-hrs A*hr
bar bar
bool
boolean
byte
cc cm^3
celsius degree*C
db dbar
dbar dbar
deg degree
degc degree*C
degree decimal minutes degree dec. min.
enum
inHg inch*Hg
inch inch
ipts[ -]68 degree*C IPTS-68
its[ -]90 degree*C ITS-90
kg m-3 kg/m^3
kg m-3 kg/m^3
kg/m^3 kg/m^3
m s-1 m/s
m s^-2 m/s^2
m m
m m
m m
m-1 1/m
m/s m/s
m/s^2 m/s^2
mS cm-1 mS/cm
mS cm^-1 mS/cm
mS/cm mS/cm
mV mV
mg m-3 mg/m^3
mg m-3 mg/m^3
mg/m^3 mg/m^3
micromol l-1 mu*mol/l
micromol l^-1 mu*mol/l
micromol/l mu*mol/l
micromole kg-1 mu*mol/kg
micromole kg^-1 mu*mol/kg
micromole/kg mu*mol/kg
ml [lL]^-1 ml/l
ml [lL]-1 ml/l
ml/[lL] ml/l
muS cm-1 mu*S/cm
muS cm^-1 mu*S/cm
muS/cm mu*S/cm
nodim
ppb ppb
pss-78 PSS-78
psu PSS-78
rad rad
s m-1 s/m
s m^-1 s/m
s s
s/m s/m
sec s
seconds since 1970-01-01T00:00:00Z s since 1970-01-01
seconds since 1990-01-01 s since 1990-01-01
uEinsteins s-1 m-2 mu*Einstein/m^2/s^1
uEinsteins s^-1 m^-2 mu*Einstein/m^2/s^1
uEinsteins/s/m^2 mu*Einstein/m^2/s^1
ug/kg mu*g/kg
ug/l mu*g/l
umol kg-1 mu*mol/kg
umol l-1 mu*mol/l
umol l^-1 mu*mol/l
umol/kg mu*mol/kg
umol/l mu*mol/l
unixtimestamp s since 1970-01-01
uw cm^-2 nm^-1 mu*W/cm^2/nm
uw/cm-2 nm-1 mu*W/cm^2/nm
uw/cm^2/nm mu*W/cm^2/nm
volt V
volts V
watt W
watts W

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

No branches or pull requests

1 participant