@@ -3849,27 +3849,113 @@ def OnCopy1Val(event):
38493849 # TODO: check if values need to be copied to editing widgets
38503850 wx.CallAfter(UpdateSampleGrid,G2frame,data)
38513851
3852+ def OnSearchComments(event):
3853+ '''Set a selected Sample parameter from a selected entry in the comments
3854+ for a selected set of histograms
3855+ '''
3856+ # get possible keys from current histogram's comments
3857+ Comments = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(
3858+ G2frame, G2frame.PatternId, 'Comments'))
3859+ keyList = []
3860+ for line in Comments:
3861+ if ':' in line:
3862+ key = line.split(':')[0]
3863+ elif '=' in line:
3864+ key = line.split('=')[0]
3865+ else:
3866+ continue
3867+ keyList.append(key.strip())
3868+ sampleVar,commentKey = G2G.SelectSearchVars(G2frame,labelLst,keyList)
3869+ if sampleVar is None or commentKey is None: return
3870+ # find the array element tied to the sample var
3871+ if sampleVar not in labelLst:
3872+ print(f'OnSearchComments: how did we get {sampleVar} not in {labelLst}?')
3873+ return
3874+ i = labelLst.index(sampleVar)
3875+ sampleArrKeys = elemKeysLst[i]
3876+ # need to select histograms to search/set
3877+ hst = G2frame.GPXtree.GetItemText(G2frame.PatternId)
3878+ histList = GetHistsLikeSelected(G2frame)
3879+ histList.insert(0,hst)
3880+ if len(histList) == 1: # only current histogram present -- don't ask
3881+ selection = [0]
3882+ else:
3883+ dlg = G2G.G2MultiChoiceDialog(G2frame,f'Set "{sampleVar}" value in histograms...',
3884+ 'Set value from comments', histList)
3885+ try:
3886+ if dlg.ShowModal() == wx.ID_OK:
3887+ selection = dlg.GetSelections()
3888+ finally:
3889+ dlg.Destroy()
3890+ # cycle through selected histograms, get comments & values
3891+ count = 0
3892+ for i in selection:
3893+ hist = histList[i]
3894+ hId = G2gd.GetGPXtreeItemId(G2frame,G2frame.root, hist)
3895+ if not hId:
3896+ print(f'{hist} not found! strange')
3897+ continue
3898+ Comments = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,hId,'Comments'))
3899+ Sample = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,hId, 'Sample Parameters'))
3900+ # search comments for value matching commentKey
3901+ for item in Comments:
3902+ if '=' in item:
3903+ itemSp = item.split('=')
3904+ elif ':' in item:
3905+ itemSp = item.split(':')
3906+ else:
3907+ continue
3908+ if commentKey == itemSp[0]:
3909+ try:
3910+ value = float(itemSp[1])
3911+ break
3912+ except:
3913+ print(f'"{item.strip()}" has an invalid value in Comments for {hist}')
3914+ else:
3915+ print(f'"{commentKey}" not in Comments for {hist}')
3916+ continue
3917+ # set the value in the selected entry in Sample Parameters
3918+ try:
3919+ d = Sample
3920+ for k in sampleArrKeys[:-1]:
3921+ d = d[k]
3922+ d[sampleArrKeys[-1]]
3923+ except KeyError:
3924+ print(f'Strange: "{commentKey}" not in Sample Parameters for {hist}')
3925+ continue
3926+ count += 1
3927+ d[sampleArrKeys[-1]] = value
3928+ print(f'Done. Set "{sampleVar}" in {count} histograms')
3929+ wx.CallAfter(UpdateSampleGrid,G2frame,data)
3930+
38523931 def SearchAllComments(value,tc,*args,**kwargs):
38533932 '''Called when the label for a FreePrm is changed: the comments for all PWDR
3854- histograms are searched for a "label=value" pair that matches the label (case
3933+ histograms are searched for a "label=value" or "label:value" pair that matches the label (case
38553934 is ignored) and the values are then set to this value, if it can be converted
38563935 to a float.
38573936 '''
38583937 Id, cookie = G2frame.GPXtree.GetFirstChild(G2frame.root)
3938+ count = 0
38593939 while Id:
38603940 name = G2frame.GPXtree.GetItemText(Id)
38613941 if 'PWDR' in name:
38623942 Comments = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,Id,'Comments'))
38633943 Sample = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,Id, 'Sample Parameters'))
38643944 for i,item in enumerate(Comments):
3865- itemSp = item .split ('=' )
3866- if value .lower () == itemSp [0 ].lower ():
3945+ if '=' in item:
3946+ itemSp = item.split('=')
3947+ elif ':' in item:
3948+ itemSp = item.split(':')
3949+ else:
3950+ continue
3951+ if value.lower() in itemSp[0].lower():
38673952 try:
38683953 Sample[tc.key] = float(itemSp[1])
38693954 except:
38703955 print('"{}" has an invalid value in Comments from {}'
38713956 .format(item.strip(),name))
38723957 Id, cookie = G2frame.GPXtree.GetNextChild(G2frame.root, cookie)
3958+ print(f'{count} values were found in the histogram comments')
38733959 wx.CallLater(100,UpdateSampleGrid,G2frame,data)
38743960
38753961 # start of UpdateSampleGrid
@@ -3886,6 +3972,7 @@ def SearchAllComments(value,tc,*args,**kwargs):
38863972 G2frame.Bind(wx.EVT_MENU, OnSampleSave, id=G2G.wxID_SAMPLESAVE)
38873973 G2frame.Bind(wx.EVT_MENU, OnSampleLoad, id=G2G.wxID_SAMPLELOAD)
38883974 G2frame.Bind(wx.EVT_MENU, OnCopy1Val, id=G2G.wxID_SAMPLE1VAL)
3975+ G2frame.Bind(wx.EVT_MENU, OnSearchComments, id=G2G.wxID_SEARCHVAL)
38893976 G2frame.Bind(wx.EVT_MENU, OnAllSampleLoad, id=G2G.wxID_ALLSAMPLELOAD)
38903977 G2frame.Bind(wx.EVT_MENU, OnRescaleAll, id=G2G.wxID_RESCALEALL)
38913978 if histName[:4] in ['SASD','REFD','PWDR']:
0 commit comments