/source/NCDFDimension.m

http://github.com/paleoterra/PaleoNetCDF · Objective C · 257 lines · 190 code · 53 blank · 14 comment · 18 complexity · cc8c560841f5beee18238e8c660b088c MD5 · raw file

  1. //
  2. // NCDFDimension.m
  3. // netcdf
  4. //
  5. // Created by tmoore on Wed Feb 13 2002.
  6. // Copyright (c) 2002 Argonne National Laboratory. All rights reserved.
  7. //
  8. #import "NCDFNetCDF.h"
  9. @implementation NCDFDimension
  10. -(id)initWithFileName:(NSString *)thePath dimID:(int)number name:(NSString *)name length:(size_t)aLength handle:(NCDFHandle *)handle
  11. {
  12. self = [super init];
  13. fileName = [thePath copy];
  14. dimID = number;
  15. dimName = [name copy];
  16. length = aLength;//Dimension length is a count.
  17. theHandle = handle;
  18. return self;
  19. }
  20. -(id)initNewDimWithName:(NSString *)name length:(size_t)aLength
  21. {
  22. self = [super init];
  23. fileName = nil;
  24. dimID = -1;
  25. dimName = [name copy];
  26. length = aLength;//Dimension length is a count.
  27. theHandle = nil;
  28. return self;
  29. }
  30. -(id)initWithDimension:(NCDFDimension *)aDim makeUnlimited:(BOOL)limit
  31. {
  32. if(aDim)
  33. {
  34. self = [super init];
  35. fileName = nil;
  36. dimID = -1;
  37. dimName = [[aDim dimensionName] copy];
  38. if(limit)
  39. length = NC_UNLIMITED;
  40. else
  41. length = [aDim dimLength];//Dimension length is a count.
  42. theHandle = nil;
  43. return self;
  44. }
  45. else
  46. return nil;
  47. }
  48. -(NSLock *)handleLock
  49. {
  50. return [theHandle handleLock];
  51. }
  52. -(NSString *)dimensionName
  53. {
  54. /*Returns the name of the reciever*/
  55. /*Accessor*/
  56. /*Validated*/
  57. return dimName;
  58. }
  59. -(size_t)dimLength
  60. {
  61. /*Returns the dimentional length of the reciever*/
  62. /*Accessor*/
  63. /*Validated*/
  64. return length;
  65. }
  66. // Writing functions
  67. -(BOOL)renameDimension:(NSString *)newName
  68. {
  69. int32_t ncid;
  70. int32_t status;
  71. char *theCName;
  72. if(theErrorHandle==nil)
  73. theErrorHandle = [theHandle theErrorHandle];
  74. newName = [self parseNameString:newName];
  75. ncid = [theHandle ncidWithOpenMode:NC_WRITE status:&status];
  76. if(status!=NC_NOERR)
  77. {
  78. [theErrorHandle addErrorFromSource:fileName className:@"NCDFDimension" methodName:@"renameDimension" subMethod:@"Opening netCDF file" errorCode:status];
  79. return NO;
  80. }
  81. status = nc_redef(ncid);
  82. if(status!=NC_NOERR)
  83. {
  84. [theErrorHandle addErrorFromSource:fileName className:@"NCDFDimension" methodName:@"renameDimension" subMethod:@"redefine mode" errorCode:status];
  85. return NO;
  86. }
  87. theCName = (char *)malloc(sizeof(char)*[newName length]+1);
  88. [newName getCString:theCName maxLength:[newName length]+1 encoding:NSUTF8StringEncoding];
  89. status = nc_rename_dim(ncid,dimID,theCName);
  90. free(theCName);
  91. if(status!=NC_NOERR)
  92. {
  93. [theErrorHandle addErrorFromSource:fileName className:@"NCDFDimension" methodName:@"renameDimension" subMethod:@"Renaming dimension" errorCode:status];
  94. return NO;
  95. }
  96. dimName = [newName copy];
  97. [theHandle closeNCID:ncid];
  98. [theHandle refresh];
  99. return YES;
  100. }
  101. -(NSString *)parseNameString:(NSString *)theString
  102. {
  103. NSString *newString;
  104. NSMutableString *mutString;
  105. NSRange theRange;
  106. NSScanner *theScanner = [NSScanner scannerWithString:theString];
  107. NSCharacterSet *theSet = [NSCharacterSet whitespaceCharacterSet];
  108. mutString = [NSMutableString stringWithString:theString];
  109. theRange.length = 1;
  110. while(![theScanner isAtEnd])
  111. {
  112. [theScanner scanUpToCharactersFromSet:theSet intoString:nil];
  113. if(![theScanner isAtEnd])
  114. {
  115. theRange.location = [theScanner scanLocation];
  116. [mutString replaceCharactersInRange:theRange withString:@"_"];
  117. }
  118. }
  119. newString = [NSString stringWithString:mutString];
  120. return newString;
  121. }
  122. -(int)dimensionID
  123. {
  124. return dimID;
  125. }
  126. -(BOOL)isEqualToDim:(NCDFDimension *)aDimension
  127. {
  128. if(![dimName isEqualToString:[aDimension dimensionName]])
  129. return NO;
  130. if(length != [aDimension dimLength])
  131. return NO;
  132. return YES;
  133. }
  134. -(void)setDimLength:(size_t)newLength
  135. {
  136. length = newLength;
  137. }
  138. -(BOOL)isUnlimited
  139. {
  140. int32_t ncid,pid,status;
  141. if(theErrorHandle == nil)
  142. theErrorHandle = [theHandle theErrorHandle];
  143. ncid = [theHandle ncidWithOpenMode:NC_NOWRITE status:&status];
  144. if(status!=NC_NOERR)
  145. {
  146. [theErrorHandle addErrorFromSource:fileName className:@"NCDFDimension" methodName:@"isUnlimited" subMethod:@"Opening netCDF file" errorCode:status];
  147. return NO;
  148. }
  149. status = nc_inq_unlimdim(ncid,&pid);
  150. if(status!=NC_NOERR)
  151. {
  152. [theErrorHandle addErrorFromSource:fileName className:@"NCDFDimension" methodName:@"isUnlimited" subMethod:@"Is dimension unlimited?" errorCode:status];
  153. return NO;
  154. }
  155. [theHandle closeNCID:ncid];
  156. if(dimID==pid)
  157. return YES;
  158. return NO;
  159. }
  160. -(NSDictionary *)propertyList
  161. {
  162. NSDictionary *thePropertyList;
  163. NSMutableDictionary *theTemp;
  164. theTemp = [[NSMutableDictionary alloc] init];
  165. [theTemp setObject:fileName forKey:@"fileName"];
  166. [theTemp setObject:[NSNumber numberWithInt:dimID] forKey:@"dimID"];
  167. [theTemp setObject:dimName forKey:@"dimName"];
  168. if([self isUnlimited])
  169. [theTemp setObject:[NSNumber numberWithInt:0] forKey:@"length"];
  170. else
  171. [theTemp setObject:[NSNumber numberWithInt:(int)length] forKey:@"length"];
  172. thePropertyList = [NSDictionary dictionaryWithDictionary:theTemp];
  173. return thePropertyList;
  174. }
  175. -(void)updateDimensionWithDimension:(NCDFDimension *)aDim
  176. {
  177. dimID = [aDim dimensionID];
  178. dimName = [[aDim dimensionName] copy];
  179. length = [aDim dimLength];//Dimension length is a count.
  180. }
  181. -(NSString *)description
  182. {
  183. return [NSString stringWithFormat:@"NCDFDimension: %@\nID: %zi\nLength: %i\n",[self dimensionName],[self dimLength],[self dimensionID]];
  184. }
  185. -(NSComparisonResult)compare:(id)object
  186. {
  187. if([object isKindOfClass:[NCDFDimension class]])
  188. {
  189. if([self dimensionID] < [(NCDFDimension *)object dimensionID])
  190. return NSOrderedAscending;
  191. else
  192. return NSOrderedDescending;
  193. }
  194. else
  195. return NSOrderedSame;
  196. }
  197. -(void)dealloc
  198. {
  199. fileName = nil;
  200. dimName = nil;
  201. theHandle = nil;
  202. theErrorHandle = nil;
  203. }
  204. @end