Changeset 4093


Ignore:
Timestamp:
06/02/08 16:55:35 (12 years ago)
Author:
curtis
Message:

Fix up date parsing, and other miscellaneous cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/in/BaseTiffReader.java

    r4058 r4093  
    580580   * sub-classes that override the getters for pixel set array size, etc. 
    581581   */ 
    582   protected void initMetadataStore() { 
    583     try { 
    584       // the metadata store we're working with 
    585       MetadataStore store = 
    586         new FilterMetadata(getMetadataStore(), isMetadataFiltered()); 
    587       store.setImageName("", 0); 
    588  
    589       // set the pixel values in the metadata store 
    590       MetadataTools.populatePixels(store, this); 
    591  
    592       // populate Experimenter 
    593       String artist = (String) TiffTools.getIFDValue(ifds[0], TiffTools.ARTIST); 
    594       if (artist != null) { 
    595         String firstName = null, lastName = null; 
    596         int ndx = artist.indexOf(" "); 
    597         if (ndx < 0) lastName = artist; 
    598         else { 
    599           firstName = artist.substring(0, ndx); 
    600           lastName = artist.substring(ndx + 1); 
    601         } 
    602         String email = (String) 
    603           TiffTools.getIFDValue(ifds[0], TiffTools.HOST_COMPUTER); 
    604         store.setExperimenterFirstName(firstName, 0); 
    605         store.setExperimenterLastName(lastName, 0); 
    606         store.setExperimenterEmail(email, 0); 
     582  protected void initMetadataStore() throws FormatException { 
     583    // the metadata store we're working with 
     584    MetadataStore store = 
     585      new FilterMetadata(getMetadataStore(), isMetadataFiltered()); 
     586    store.setImageName("", 0); 
     587 
     588    // set the pixel values in the metadata store 
     589    MetadataTools.populatePixels(store, this); 
     590 
     591    // populate Experimenter 
     592    String artist = (String) TiffTools.getIFDValue(ifds[0], TiffTools.ARTIST); 
     593    if (artist != null) { 
     594      String firstName = null, lastName = null; 
     595      int ndx = artist.indexOf(" "); 
     596      if (ndx < 0) lastName = artist; 
     597      else { 
     598        firstName = artist.substring(0, ndx); 
     599        lastName = artist.substring(ndx + 1); 
    607600      } 
    608  
    609       // format the creation date to ISO 8061 
    610  
    611       String creationDate = getImageCreationDate(); 
    612       try { 
    613         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
    614         SimpleDateFormat parse = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); 
    615         Date date = parse.parse(creationDate, new ParsePosition(0)); 
    616         creationDate = sdf.format(date); 
    617       } 
    618       catch (NullPointerException e) { 
    619         try { 
    620           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
    621           SimpleDateFormat parse = 
    622             new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS"); 
    623           Date date = parse.parse(creationDate, new ParsePosition(0)); 
    624           creationDate = sdf.format(date); 
    625         } 
    626         catch (NullPointerException exc) { 
    627           if (debug) trace(exc); 
    628           creationDate = null; 
    629         } 
    630       } 
    631  
    632       // populate Image 
    633  
    634       store.setImageCreationDate(creationDate, 0); 
    635       store.setImageDescription(TiffTools.getComment(ifds[0]), 0); 
    636  
    637       // CHECK 
    638       // populate LogicalChannel 
    639       /* 
    640       for (int i=0; i<getSizeC(); i++) { 
    641         try { 
    642           setLogicalChannel(i); 
    643         } 
    644         catch (FormatException exc) { 
    645           if (debug) trace(exc); 
    646         } 
    647         catch (IOException exc) { 
    648           if (debug) trace(exc); 
    649         } 
    650       } 
    651       */ 
    652  
    653       // set the X and Y pixel dimensions 
    654  
    655       int resolutionUnit = TiffTools.getIFDIntValue(ifds[0], 
    656         TiffTools.RESOLUTION_UNIT); 
    657       TiffRational xResolution = TiffTools.getIFDRationalValue(ifds[0], 
    658         TiffTools.X_RESOLUTION, false); 
    659       TiffRational yResolution = TiffTools.getIFDRationalValue(ifds[0], 
    660         TiffTools.Y_RESOLUTION, false); 
    661       float pixX = xResolution == null ? 0f : 1 / xResolution.floatValue(); 
    662       float pixY = yResolution == null ? 0f : 1 / yResolution.floatValue(); 
    663  
    664       switch (resolutionUnit) { 
    665         case 2: 
    666           // resolution is expressed in pixels per inch 
    667           pixX /= 0.0254; 
    668           pixY /= 0.0254; 
    669           break; 
    670         case 3: 
    671           // resolution is expressed in pixels per centimeter 
    672           pixX *= 100; 
    673           pixY *= 100; 
    674           break; 
    675       } 
    676  
    677       store.setDimensionsPhysicalSizeX(new Float(pixX), 0, 0); 
    678       store.setDimensionsPhysicalSizeY(new Float(pixY), 0, 0); 
    679       store.setDimensionsPhysicalSizeZ(new Float(0), 0, 0); 
    680  
    681       // populate StageLabel 
    682       Object x = TiffTools.getIFDValue(ifds[0], TiffTools.X_POSITION); 
    683       Object y = TiffTools.getIFDValue(ifds[0], TiffTools.Y_POSITION); 
    684       Float stageX; 
    685       Float stageY; 
    686       if (x instanceof TiffRational) { 
    687         stageX = x == null ? null : new Float(((TiffRational) x).floatValue()); 
    688         stageY = y == null ? null : new Float(((TiffRational) y).floatValue()); 
    689       } 
    690       else { 
    691         stageX = x == null ? null : new Float((String) x); 
    692         stageY = y == null ? null : new Float((String) y); 
    693       } 
    694       // populate Instrument 
    695       // CTR CHECK 
    696 //      String make = (String) TiffTools.getIFDValue(ifd, TiffTools.MAKE); 
    697 //      String model = (String) TiffTools.getIFDValue(ifd, TiffTools.MODEL); 
    698 //      store.setInstrumentModel(model, 0); 
    699 //      store.setInstrumentManufacturer(make, 0); 
    700     } 
    701     catch (FormatException exc) { trace(exc); } 
     601      String email = (String) 
     602        TiffTools.getIFDValue(ifds[0], TiffTools.HOST_COMPUTER); 
     603      store.setExperimenterFirstName(firstName, 0); 
     604      store.setExperimenterLastName(lastName, 0); 
     605      store.setExperimenterEmail(email, 0); 
     606    } 
     607 
     608    // format the creation date to ISO 8061 
     609 
     610    String creationDate = getImageCreationDate(); 
     611    String date = parseDate(creationDate, "yyyy:MM:dd HH:mm:ss"); 
     612    if (date == null) date = parseDate(creationDate, "dd/MM/yyyy HH:mm:ss.SS"); 
     613    if (creationDate != null && date == null && debug) { 
     614      debug("Warning: unknown creation date format: " + creationDate); 
     615    } 
     616    creationDate = date; 
     617 
     618    // populate Image 
     619 
     620    store.setImageCreationDate(creationDate, 0); 
     621    store.setImageDescription(TiffTools.getComment(ifds[0]), 0); 
     622 
     623    // set the X and Y pixel dimensions 
     624 
     625    int resolutionUnit = TiffTools.getIFDIntValue(ifds[0], 
     626      TiffTools.RESOLUTION_UNIT); 
     627    TiffRational xResolution = TiffTools.getIFDRationalValue(ifds[0], 
     628      TiffTools.X_RESOLUTION, false); 
     629    TiffRational yResolution = TiffTools.getIFDRationalValue(ifds[0], 
     630      TiffTools.Y_RESOLUTION, false); 
     631    float pixX = xResolution == null ? 0f : 1 / xResolution.floatValue(); 
     632    float pixY = yResolution == null ? 0f : 1 / yResolution.floatValue(); 
     633 
     634    switch (resolutionUnit) { 
     635      case 2: 
     636        // resolution is expressed in pixels per inch 
     637        pixX /= 0.0254; 
     638        pixY /= 0.0254; 
     639        break; 
     640      case 3: 
     641        // resolution is expressed in pixels per centimeter 
     642        pixX *= 100; 
     643        pixY *= 100; 
     644        break; 
     645    } 
     646 
     647    store.setDimensionsPhysicalSizeX(new Float(pixX), 0, 0); 
     648    store.setDimensionsPhysicalSizeY(new Float(pixY), 0, 0); 
     649    store.setDimensionsPhysicalSizeZ(new Float(0), 0, 0); 
     650 
     651    // populate StageLabel 
     652    Object x = TiffTools.getIFDValue(ifds[0], TiffTools.X_POSITION); 
     653    Object y = TiffTools.getIFDValue(ifds[0], TiffTools.Y_POSITION); 
     654    Float stageX; 
     655    Float stageY; 
     656    if (x instanceof TiffRational) { 
     657      stageX = x == null ? null : new Float(((TiffRational) x).floatValue()); 
     658      stageY = y == null ? null : new Float(((TiffRational) y).floatValue()); 
     659    } 
     660    else { 
     661      stageX = x == null ? null : new Float((String) x); 
     662      stageY = y == null ? null : new Float((String) y); 
     663    } 
     664    // populate Instrument 
     665    //String make = (String) TiffTools.getIFDValue(ifd, TiffTools.MAKE); 
     666    //String model = (String) TiffTools.getIFDValue(ifd, TiffTools.MODEL); 
     667    //store.setInstrumentModel(model, 0); 
     668    //store.setInstrumentManufacturer(make, 0); 
    702669  } 
    703670 
     
    886853  } 
    887854 
     855  private static String parseDate(String date, String format) { 
     856    if (date == null) return null; 
     857    try { 
     858      SimpleDateFormat parse = new SimpleDateFormat(format); 
     859      Date d = parse.parse(date, new ParsePosition(0)); 
     860      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
     861      return sdf.format(d); 
     862    } 
     863    catch (NullPointerException exc) { 
     864      return null; 
     865    } 
     866  } 
     867 
    888868} 
Note: See TracChangeset for help on using the changeset viewer.