;+ ; NAME: ; NUMBER_FORMATTER ;########################################################################### FUNCTION Number_Formatter, number, DECIMALS=decimals On_Error, 2 IF N_Elements(number[0]) EQ 0 THEN Message, 'A number must be passed as an argument to the function.' IF N_Elements(decimals) EQ 0 THEN decimals = 2 ; If the number is a byte, convert it to an integer and return it. IF Size(number[0], /TNAME) EQ 'BYTE' THEN RETURN, String(Fix(number), Format='(I3)') ; If the number is a string, return it directly. IF Size(number[0], /TNAME) EQ 'STRING' THEN RETURN, number ; Is the number a negative value? Deal only with positive values, until the end. IF number LT 0.0 THEN minus = 1 ELSE minus = 0 theNumber = Abs(number[0]) ; Do the appropriate thing. CASE Size(theNumber, /TNAME) OF 'INT': retValue = StrTrim(String(theNumber),2) 'LONG': retValue = StrTrim(String(theNumber),2) 'FLOAT': BEGIN ; Format the number with G format. aString = StrTrim(String(theNumber, Format='(G)'), 2) ; Split the number into a whole part and a fractional part. parts = StrSplit(aString, '.', /Extract) IF N_Elements(parts) EQ 1 THEN parts = [parts, '0'] parts[1] = StrTrim(parts[1],2) ; Does the fractional part have an E or a D in it? loc = StRegEx(parts[1], '[DE]') CASE loc OF -1: BEGIN ; No exponent. ; Round to the number of decimals you want. fracpart = StrTrim(Round(Double('1.' + parts[1]) * (10L^decimals), /L64), 2) IF StrMid(fracpart,0,1) EQ '2' THEN BEGIN parts[0] = StrTrim(Long64(parts[0]) + 1, 2) ENDIF parts[1] = StrMid(fracpart, 1) retValue = StrTrim(parts[0],2) + '.' + parts[1] END ELSE: BEGIN ; Divide the fractional part of the number up into parts. ; Treat p[0] as you treated parts[1] above. Then put it all ; back together. p = StrSplit(parts[1], '[DdEe]', /RegEx, /Extract) ; Round to the number of decimals you want. fracpart = StrTrim(Round(Double('1.' + p[0]) * (10L^decimals), /L64), 2) IF StrMid(fracpart,0,1) EQ '2' THEN BEGIN parts[0] = StrTrim(Long64(parts[0]) + 1, 2) ENDIF p[0] = StrMid(fracpart, 1) ; Get the exponent sign and exponent part. expSign = StrMid(p[1],0,1) expPart = StrMid(p[1],1) ; Trim zeros in exponent. firstChar = StrMid(expPart, 0, 1) WHILE firstChar EQ '0' DO BEGIN expPart = StrMid(expPart, 1) firstChar = StrMid(expPart, 0, 1) ENDWHILE ; Put it all back together. retValue = StrTrim(parts[0],2) + '.' + p[0] + StrLowCase(StrMid(parts[1],loc,1)) + expSign + expPart END ENDCASE END 'DOUBLE': BEGIN ; Format the number with G format. aString = StrTrim(String(theNumber, Format='(G)'), 2) ; Split the number into a whole part and a fractional part. parts = StrSplit(aString, '.', /Extract) IF N_Elements(parts) EQ 1 THEN parts = [parts, '0'] parts[1] = StrTrim(parts[1],2) ; Does the fractional part have an E or a D in it? loc = StRegEx(parts[1], '[DE]') CASE loc OF -1: BEGIN ; No exponent. ; Round to the number of decimals you want. fracpart = StrTrim(Round(Double('1.' + parts[1]) * (10L^decimals), /L64), 2) IF StrMid(fracpart,0,1) EQ '2' THEN BEGIN parts[0] = StrTrim(Long64(parts[0]) + 1, 2) ENDIF parts[1] = StrMid(fracpart, 1) retValue = StrTrim(parts[0],2) + '.' + parts[1] END ELSE: BEGIN ; Divide the fractional part of the number up into parts. ; Treat p[0] as you treated parts[1] above. Then put it all ; back together. p = StrSplit(parts[1], '[DdEe]', /RegEx, /Extract) ; Round to the number of decimals you want. fracpart = StrTrim(Round(Double('1.' + p[0]) * (10L^decimals), /L64), 2) IF StrMid(fracpart,0,1) EQ '2' THEN BEGIN parts[0] = StrTrim(Long64(parts[0]) + 1, 2) ENDIF p[0] = StrMid(fracpart, 1) ; Get the exponent sign and exponent part. expSign = StrMid(p[1],0,1) expPart = StrMid(p[1],1) ; Trim zeros in exponent. firstChar = StrMid(expPart, 0, 1) WHILE firstChar EQ '0' DO BEGIN expPart = StrMid(expPart, 1) firstChar = StrMid(expPart, 0, 1) ENDWHILE ; Put it all back together. retValue = StrTrim(parts[0],2) + '.' + p[0] + StrLowCase(StrMid(parts[1],loc,1)) + expSign + expPart END ENDCASE END 'UINT': retValue = StrTrim(String(theNumber),2) 'ULONG': retValue = StrTrim(String(theNumber),2) 'LONG64': retValue = StrTrim(String(theNumber),2) 'ULONG64': retValue = StrTrim(String(theNumber),2) ELSE: Message, 'Cannot format a number of this type: ' + Size(theNumber, /TNAME) + '.' ENDCASE ; Need a minus sign? IF minus THEN retValue = '-' + retValue RETURN, retValue END ;---------------------------------------------------------------------------------------- PRO draw_current_cm, draw_widget_id, x_size, min_v, max_v, return_strings=rstrings @common_blocks.inc image = image_stack[image_index] if verbose then print, 'in draw_current_cm' WIDGET_CONTROL, draw_area, GET_VALUE=old_win ; last color is white, no matter what WIDGET_CONTROL, draw_cm, GET_VALUE=win2 WSET, win2 label_num = 3 ; store curent window index if x_size gt 0 then widget_control, xsize = x_size, draw_widget_id wset, win2 erase min_v = FLOAT(min_v) & max_v = FLOAT(max_v) unitfactor = 1. titlestring = '' slstr = '' zstring = '' unitlabel = '' lengthlabel = '' timelabel='' ; which time to display? timevalue = times[time_index] ; also show time difference if there are ; more than one timesteps loaded dt = max(times)-times[time_index] dtlabel = '' tstring = 'time='+Number_Formatter(timevalue,decimals=3)+timelabel ; if (dt gt 0.) then tstring += ', remaining: ' +string(dt,FORMAT = ; '(1F9.4)')+dtlabel if (dt gt 0.) then tstring += ', remaining: ' +Number_Formatter(dt,decimals=2)+dtlabel depthlabel ='' depth_length = depth_value viewlength = (image.slice_size[2]-image.slice_size[0]) currentlengthunit = get_unit('Length', value=viewlength, ulabel=currentlengthlabel) ;currentlengthlabel = get_unit('Length', , value=viewlength) if useunits then begin viewlength = currentlengthunit*viewlength depth_length = currentlengthunit*depth_value timevalue = nice_time_unit(timevalue*timeunit, timelabel=timelabel) dt = nice_time_unit(dt*timeunit, timelabel=dtlabel) tstring = 'time='+string(timevalue,FORMAT = '(1F8.2)')+' '+timelabel if dt gt 0. then tstring += ', remaining: ' +string(dt,FORMAT = '(1F7.2)')+' '+dtlabel unitfactor = get_unit(image.title, ulabel=unitlabel, value=max_v) print, 'max_v:', max_v, unitfactor if (last_was eq 2) and (weight_index eq 0) then begin ; this must be a column density unitlabel += ' '+currentlengthlabel unitfactor *= depth_length endif if image.is_log then begin min_v = alog10(10.D^min_v*unitfactor) max_v = alog10(10.D^max_v*unitfactor) endif else begin min_v = min_v*unitfactor max_v = max_v*unitfactor endelse endif ;rotation = !p.t ;!p.t = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]] ncolors = !d.table_size im_arr = INTARR(!d.x_size,!d.y_size) height = 20 width = FiX((!d.x_size)/2.5) x_off = FiX((!d.x_size)/1.8) y_off = FiX((!d.y_size-height)/1.1) xcenter = x_off+width/2 ; print, x_off, y_off im_arr[x_off:(x_off +width-1), y_off:(y_off+height-1)] = $ BYTSCL(CONGRID(INDGEN(ncolors-1),width,/MINUS_ONE,/INTERP,/CENTER) # REPLICATE(1, height)) +1 if last_was ne 6 then begin ; draw color bar TVSCL, im_arr ;draw bounding box box = [[x_off,y_off], [x_off,y_off+height], [x_off+width,y_off+height], $ [x_off+width,y_off], [x_off,y_off]] plots, box, /device, thick=1.5, COLOR=ncolors-1 ;write labels if (n_elements(min_v) eq 0 ) then min_v = 0. if (n_elements(max_v) eq 0 ) then max_v = 1. min_s = STRING(min_v, FORMAT = '(1F5.2)') help = indgen(label_num) ; labels= STRING(min_v+(max_v-min_v)/FLOAT(label_num)*FLOAT(help+1),FORMAT='(1F7.2)') ; labels= strarr(label_num+1) labelv = findgen(label_num+1) labelv = min_v + (max_v-min_v)/FLOAT(label_num)*labelv labels = string(labelv) for i=0, label_num do labels[i] = Number_formatter(labelv[i], decimals=4) if image.is_log then begin labelse = labels FOR i=0,label_num DO BEGIN dc = CEIL(0.-labelv[i]+2) > 0 if (labelv[i] lt -1.) then dc = round(abs(labelv[i])+2) < 6 labelse[i] = Number_formatter(10.^labelv[i], decimals=dc) xyouts, x_off+(i)*width/N_elements(help),y_off-30, labelse[i], $ ALIGNMENT = 0.5, /DEVICE, CHARSIZE=1., CHARTHICK=1, COLOR=ncolors-1 endfor endif FOR i=0,label_num DO BEGIN xyouts, x_off+(i)*width/N_elements(help),y_off-15, labels[i], $ ALIGNMENT = 0.5, /DEVICE, CHARSIZE=1.4, CHARTHICK=1, COLOR=ncolors-1 ; put tick marks plots, x_off+(i)*width/N_elements(help),y_off, /DEVICE, COLOR=ncolors-1 plots, x_off+(i)*width/N_elements(help),y_off+ROUND(height/5), /CONTINUE, /DEVICE, COLOR=ncolors-1 ENDFOR endif ; put label: fontnum=0 ; -1: vector fonts, 0: device fonts 1: true type fonts if use_nice_font then !p.font = fontnum if use_nice_font then device, set_font='-*-helvetica-*-r-*-*-*-240-*-*-*-*-*-*' if image.is_log then logstring = 'Log ' else logstring = '' if unitlabel ne '' then unitstring = ' ['+unitlabel+']' else unitstring = '' if (last_was eq 6) then begin ; powerspectrum if useunits then unitstring += '^2 per unit k' endif if last_was ne 0 then titlestring=logstring+image.title+unitstring else titlestring = 'AMR Level' xyouts, 10, 55, titlestring, /DEVICE, CHARSIZE=2., CHARTHICK=1, COLOR=ncolors-1 ;device, set_font='9x15bold' if use_nice_font then device, set_font='-*-helvetica-*-r-*-*-*-160-*-*-*-*-*-*' axes = (slice_ori gt 1.e-30) & const_sub = where(axes eq max(axes)) slstr = (['x','y','z'])[const_sub] if last_was eq 0 then slstr = slstr+'-grid chart' if last_was eq 1 then slstr = slstr+'-slice' if last_was eq 2 then slstr = slstr+'-proj.' if last_was eq 3 then begin slstr = '' if hi.weight ne 'none' then begin if hi.option eq 0 then slstr='weighted ' else slstr = 'average ' endif slstr += 'histogram in cubic box ' endif if last_was eq 6 then begin slstr = '' slstr += 'Powerspectrum, cubic box of'+ string(cube_dim, format='(i4)')+ '^3' endif slstr += $ ': '+ Number_formatter(viewlength, Decimals=2)+' '+currentlengthlabel+' wide' if (last_was eq 2) then slstr += $ ', depth: '+ Number_formatter(depth_length, Decimals=5)+' '+currentlengthlabel xyouts, 5, 5, slstr, $ /DEVICE, CHARSIZE=1.5, CHARTHICK=1.5, ALIGNMENT=0., COLOR=ncolors-1 zstring='' IF N_elements(redshifts) eq N_elements(times) then $ IF (redshifts[time_index] ne -1) THEN zstring='z='+string(redshifts[time_index],FORMAT='(1F9.4)')+', ' xyouts, xcenter-50, 5, zstring+ tstring, $ /DEVICE, CHARSIZE=1.5, CHARTHICK=1.5, COLOR=ncolors-1, ALIGNMENT=0.5 WIDGET_CONTROL, draw_area, GET_VALUE=old_win wset, old_win rstrings=[titlestring, tstring, slstr, zstring] if verbose then print, 'leave draw_current_cm' return ;!p.t = rotation END ; .compile draw_current_cm