Search code examples
idl-programming-language

String recognition in idl


I have the following strings:

F:\Sheyenne\ROI\SWIR32_subset\SWIR32_2005210_East_A.dat
F:\Sheyenne\ROI\SWIR32_subset\SWIR32_2005210_Froemke-Hoy.dat

and from each I want to extract the three variables, 1. SWIR32 2. the date and 3. the text following the date. I want to automate this process for about 200 files, so individually selecting the locations won't exactly work for me.

so I want:

variable1=SWIR32
variable2=2005210
variable3=East_A
variable4=SWIR32
variable5=2005210
variable6=Froemke-Hoy

I am going to be using these to add titles to graphs later on, but since the position of the text in each string varies I am unsure how to do this using strmid


Solution

  • I think you want to use a combination of STRPOS and STRSPLIT. Something like the following:

    s = ['F:\Sheyenne\ROI\SWIR32_subset\SWIR32_2005210_East_A.dat', $
      'F:\Sheyenne\ROI\SWIR32_subset\SWIR32_2005210_Froemke-Hoy.dat']
    name = STRARR(s.length)
    date = name
    txt = name
    foreach sub, s, i do begin
      sub = STRMID(sub, 1+STRPOS(sub, '\', /REVERSE_SEARCH))
      parts = STRSPLIT(sub, '_', /EXTRACT)
      name[i] = parts[0]
      date[i] = parts[1]
      txt[i] = STRJOIN(parts[2:*], '_')
    endforeach
    

    You could also do this with a regular expression (using just STRSPLIT) but regular expressions tend to be complicated and error prone.

    Hope this helps!