Skip to content

Commit

Permalink
gdsfilename now supports storing a path prefix and the file extension…
Browse files Browse the repository at this point in the history
…, and ToString() now takes 2 arguments to specify if they should be included in the string generated
  • Loading branch information
Peter Nemere committed Jan 6, 2025
1 parent 0fae6b2 commit dc80c3c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 22 deletions.
2 changes: 1 addition & 1 deletion api/dataimport/internal/converters/pixlfm/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (p PIXLFM) Import(importPath string, pseudoIntensityRangesPath string, data
}
// Stop after first file
log.Infof("Extracting metadata from file name: %v", file)
log.Infof("Meta extracted: %v", housekeepingFileNameMeta.ToString())
log.Infof("Meta extracted: %v", housekeepingFileNameMeta.ToString(false, false))
break
}
case "pseudoIntensityDir":
Expand Down
7 changes: 3 additions & 4 deletions api/ws/wsHelpers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ func GetLatestImagesOnly(images []*protos.ScanImage) ([]*protos.ScanImage, error
meta.SetVersionStr("__")

// Preserve the path
l := path.Dir(img.ImagePath)
if len(l) > 0 {
imagePath := path.Join(img.ImagePath[0:len(l)], meta.ToString())
if len(meta.FilePath) > 0 {
imagePath := meta.ToString(true, false)

// If it doesn't exist, just add it
if existingImg, ok := latestImage[imagePath]; ok {
Expand Down Expand Up @@ -146,7 +145,7 @@ func GetDBImageFilter(imageName string) bson.D {
imagePath := imageName
if len(root) > 0 {
meta.SetVersionStr("..")
imagePath = path.Join(root, meta.ToString())
imagePath = meta.ToString(true, false)
}

filter = bson.D{{"_id", primitive.Regex{Pattern: imagePath, Options: ""}}}
Expand Down
40 changes: 33 additions & 7 deletions core/gdsfilename/fmFileNameMeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package gdsfilename
import (
"errors"
"fmt"
"path"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -52,6 +53,10 @@ type FileNameMeta struct {
versionStr string // 01-99=1-9, A0-A9=100-109, AA-AZ=110-135, B0-B9=136-145, __=out of range
// . always before...
// EXT - file extension, which we get through conventional Go filepath.Ext()

// Non-PDS fields that exist in our own file name conventions
FilePath string // We store images with the RTT as a folder
Extension string // see EXT above
}

func (m *FileNameMeta) SetColourFilter(colourFilter string) {
Expand Down Expand Up @@ -118,7 +123,7 @@ func (m FileNameMeta) Version() (int32, error) {
return stringToVersion(m.versionStr)
}

func (m FileNameMeta) ToString() string {
func (m FileNameMeta) ToString(includePath bool, includeExt bool) string {
var s strings.Builder

s.WriteString(m.Instrument)
Expand All @@ -141,7 +146,23 @@ func (m FileNameMeta) ToString() string {
s.WriteString(m.Producer)
s.WriteString(m.versionStr)

return s.String()
fileName := s.String()

if includePath {
// We may have a path too
if len(m.FilePath) > 0 {
fileName = path.Join(m.FilePath, fileName)
}
}

if includeExt {
// Add the extension
if len(m.Extension) > 0 {
fileName = fileName + "." + m.Extension
}
}

return fileName
}

func (m *FileNameMeta) SetInstrumentType(instrumentType string) {
Expand All @@ -158,11 +179,16 @@ func (m FileNameMeta) Timestamp() (int32, error) {
}
*/

func ParseFileName(fileName string) (FileNameMeta, error) {
func ParseFileName(fileNameOptPathPrefix string) (FileNameMeta, error) {
result := FileNameMeta{}

// We often get passed paths so here we ensure we're just dealing with the file name at the end
fileName = filepath.Base(fileName)
fileName := filepath.Base(fileNameOptPathPrefix)

result := FileNameMeta{}
if len(fileNameOptPathPrefix) > len(fileName) {
// Something got snipped... that's our path
result.FilePath = fileNameOptPathPrefix[0 : len(fileNameOptPathPrefix)-len(fileName)-1]
}

if len(fileName) != 58 {
return result, errors.New("Failed to parse meta from file name")
Expand All @@ -189,8 +215,8 @@ func ParseFileName(fileName string) (FileNameMeta, error) {
result.compression = fileName[49:51]
result.Producer = fileName[51:52]
result.versionStr = fileName[52:54]
// "." = fileName[53:54]
// EXT = fileName[54:57]
// "." = fileName[54:55]
result.Extension = fileName[55:58]
// Length 58 chars

return result, nil
Expand Down
51 changes: 41 additions & 10 deletions core/gdsfilename/fmFileNameMeta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func Example_parseFileName() {
fmt.Printf("%v|%v\n", e, m)
printFNValues(m)

// context image with path
fmt.Println("Context image with path:")
m, e = ParseFileName("01234/PCR_D078T0637741562_000FDR_N00100360009835610066000J01.PNG")
fmt.Printf("%v|%v\n", e, m)
printFNValues(m)

// bulk MSA
fmt.Println("MSA (bulk) file:")
m, e = ParseFileName("PS__D077T0637746318_000RBS_N001003600098356103760__J01.MSA")
Expand Down Expand Up @@ -100,7 +106,7 @@ func Example_parseFileName() {

// Output:
// Field test:
// <nil>|{IN C S PRIM V SECONDARYT TER PRO G T SIT DRIV SEQNUMRTT CAMS D CO P VE}
// <nil>|{IN C S PRIM V SECONDARYT TER PRO G T SIT DRIV SEQNUMRTT CAMS D CO P VE EXT}
// PMC=0|PMC only stored for PIXL files
// RTT=SEQNUMRTT|<nil>
// SCLK=0|Failed to get SCLK from: SECONDARYT
Expand All @@ -110,56 +116,63 @@ func Example_parseFileName() {
// Failed to parse meta from file name
//
// Pseudo-intensity file:
// <nil>|{PS _ _ D077 T 0637741109 000 RPM _ N 001 0036 000983561 0064 0 __ J 01}
// <nil>|{PS _ _ D077 T 0637741109 000 RPM _ N 001 0036 000983561 0064 0 __ J 01 CSV}
// PMC=64|<nil>
// RTT=000983561|<nil>
// SCLK=637741109|<nil>
// SOL=D077|<nil>
//
// Context image file:
// <nil>|{PC R _ D077 T 0637741562 000 EDR _ N 001 0036 000983561 0066 0 00 J 01}
// <nil>|{PC R _ D077 T 0637741562 000 EDR _ N 001 0036 000983561 0066 0 00 J 01 PNG}
// PMC=66|<nil>
// RTT=000983561|<nil>
// SCLK=637741562|<nil>
// SOL=D077|<nil>
//
// Context image with path:
// <nil>|{PC R _ D078 T 0637741562 000 FDR _ N 001 0036 000983561 0066 0 00 J 01 01234 PNG}
// PMC=66|<nil>
// RTT=000983561|<nil>
// SCLK=637741562|<nil>
// SOL=D078|<nil>
//
// MSA (bulk) file:
// <nil>|{PS _ _ D077 T 0637746318 000 RBS _ N 001 0036 000983561 0376 0 __ J 01}
// <nil>|{PS _ _ D077 T 0637746318 000 RBS _ N 001 0036 000983561 0376 0 __ J 01 MSA}
// PMC=376|<nil>
// RTT=000983561|<nil>
// SCLK=637746318|<nil>
// SOL=D077|<nil>
//
// Housekeeping (RSI) file:
// <nil>|{PE _ _ D077 T 0637741109 000 RSI _ N 001 0036 000983561 0066 0 __ J 01}
// <nil>|{PE _ _ D077 T 0637741109 000 RSI _ N 001 0036 000983561 0066 0 __ J 01 CSV}
// PMC=66|<nil>
// RTT=000983561|<nil>
// SCLK=637741109|<nil>
// SOL=D077|<nil>
//
// All spectra CSV file:
// <nil>|{PS _ _ D077 T 0637741109 000 RFS _ N 001 0036 000983561 0064 0 __ J 01}
// <nil>|{PS _ _ D077 T 0637741109 000 RFS _ N 001 0036 000983561 0064 0 __ J 01 CSV}
// PMC=64|<nil>
// RTT=000983561|<nil>
// SCLK=637741109|<nil>
// SOL=D077|<nil>
//
// Testing SCLK and SOL fields:
// <nil>|{PS _ _ 1033 _ 0012345678 000 RFS _ N 001 0036 000983561 0064 0 __ J 01}
// <nil>|{PS _ _ 1033 _ 0012345678 000 RFS _ N 001 0036 000983561 0064 0 __ J 01 CSV}
// PMC=64|<nil>
// RTT=000983561|<nil>
// SCLK=12345678|<nil>
// SOL=1033|<nil>
//
// Watson file:
// <nil>|{SI F _ 0614 _ 0721455441 734 RAS _ N 030 1172 SRLC00643 _000 0 LM J 01}
// <nil>|{SI F _ 0614 _ 0721455441 734 RAS _ N 030 1172 SRLC00643 _000 0 LM J 01 PNG}
// PMC=0|PMC only stored for PIXL files
// RTT=SRLC00643|<nil>
// SCLK=721455441|<nil>
// SOL=0614|<nil>
//
// Sherloc file:
// <nil>|{SS _ _ 0614 _ 0721475157 600 RRS _ _ 030 1172 SRLC11360 W208 C GN J 01}
// <nil>|{SS _ _ 0614 _ 0721475157 600 RRS _ _ 030 1172 SRLC11360 W208 C GN J 01 CSV}
// PMC=0|PMC only stored for PIXL files
// RTT=SRLC11360|<nil>
// SCLK=721475157|<nil>
Expand Down Expand Up @@ -189,9 +202,27 @@ func Example_makeComparableName() {
func Example_stringFileName() {
name := "PS__D077T0637741109_000RPM_N001003600098356100640__J01.CSV"
m, e := ParseFileName(name)
fmt.Printf("%v|%v\n", e, m.ToString())
fmt.Printf("%v|%v\n", e, m.ToString(false, false))
fmt.Printf("%v|%v\n", e, m.ToString(true, false))
fmt.Printf("%v|%v\n", e, m.ToString(false, true))
fmt.Printf("%v|%v\n", e, m.ToString(true, true))

name = "0987654321/PS__D077T0637741109_000RPM_N001003600098356100640__J01.CSV"
m, e = ParseFileName(name)
fmt.Printf("%v|%v\n", e, m.ToString(false, false))
fmt.Printf("%v|%v\n", e, m.ToString(true, false))
fmt.Printf("%v|%v\n", e, m.ToString(false, true))
fmt.Printf("%v|%v\n", e, m.ToString(true, true))

// Output:
// <nil>|PS__D077T0637741109_000RPM_N001003600098356100640__J01
// <nil>|PS__D077T0637741109_000RPM_N001003600098356100640__J01
// <nil>|PS__D077T0637741109_000RPM_N001003600098356100640__J01.CSV
// <nil>|PS__D077T0637741109_000RPM_N001003600098356100640__J01.CSV
// <nil>|PS__D077T0637741109_000RPM_N001003600098356100640__J01
// <nil>|0987654321/PS__D077T0637741109_000RPM_N001003600098356100640__J01
// <nil>|PS__D077T0637741109_000RPM_N001003600098356100640__J01.CSV
// <nil>|0987654321/PS__D077T0637741109_000RPM_N001003600098356100640__J01.CSV
}

func Example_stringToIDSimpleCase() {
Expand Down

0 comments on commit dc80c3c

Please sign in to comment.