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