-
-
Save willonboy/3059274 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Reads in a set of raw data files, and outputs smoothed files. (lots of libdispatch magic) | |
| #define SIGMA (50) | |
| - (IBAction)smoothDataSets:(id)sender { | |
| [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger ret){ | |
| if(ret == NSOKButton){ | |
| // Apply to each file and then output appropriately. | |
| dispatch_group_t smooth_group = dispatch_group_create(); | |
| dispatch_queue_t global_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | |
| for(NSURL *url in [openPanel URLs]){ | |
| const char *in_path = [[url path] UTF8String]; | |
| const char *out_path = [[[url URLByAppendingPathExtension:@"smoothed"] path] UTF8String]; | |
| dispatch_group_async(smooth_group, global_queue, ^{ | |
| // Open file for reading... | |
| dispatch_io_t input_file = dispatch_io_create_with_path(DISPATCH_IO_STREAM, in_path, O_RDONLY, 0, dispatch_get_current_queue(), ^(int error) { | |
| printf("Error while opening file for smoothing: %d", error); | |
| }); | |
| // Set low water mark to SIZE_MAX for no incomplete handlers | |
| dispatch_io_set_low_water(input_file, SIZE_MAX); | |
| // Create new file for writing... | |
| dispatch_io_t output_file = dispatch_io_create_with_path(DISPATCH_IO_STREAM, out_path, O_RDWR | O_CREAT, 0, dispatch_get_current_queue(), ^(int error) { | |
| printf("Error creating file for writing for smoothing: %d", error); | |
| }); | |
| dispatch_io_read(input_file, 0, SIZE_MAX, dispatch_get_current_queue(), ^(bool done, dispatch_data_t data, int error) { | |
| if (data == dispatch_data_empty) return; | |
| const char *sim_data; size_t sim_data_len; | |
| // Close the input file | |
| dispatch_io_close(input_file, 0); | |
| // Create source data | |
| dispatch_data_t map_data = dispatch_data_create_map(data, (const void **)&sim_data, &sim_data_len); | |
| dispatch_group_t gaussian_group = dispatch_group_create(); | |
| // WARNING: NO SAFETY OR ERROR HANDLING BELOW. :( | |
| // Get the number of items in the file... | |
| // Confirm the meta data header is stuck on the top of the file | |
| NSInteger num_items; | |
| sscanf(sim_data, "# %ld\n", &num_items); | |
| double *src_buf = malloc(num_items * sizeof(double)); | |
| double *dst_buf = malloc(num_items * sizeof(double)); | |
| // For each item to parse, search for a newline, then scan from there. | |
| const char *p = sim_data; | |
| NSInteger j; | |
| for(int n = 0; n < num_items; n++){ | |
| p = strchr(p, '\n') + sizeof(char); | |
| sscanf(p, "%ld", &j); | |
| sscanf(p, "%*ld %lf", &src_buf[j]); | |
| } | |
| dispatch_apply(num_items, dispatch_get_current_queue(), ^(size_t i) { | |
| dispatch_group_async(gaussian_group, dispatch_get_current_queue(), ^(void) { | |
| // TODO: Apply gaussian transformation to each point, and then | |
| // output data point to output_file. | |
| // For now, 1 to 1 transfer | |
| dst_buf[i] = src_buf[i]; | |
| }); | |
| }); | |
| dispatch_group_notify(gaussian_group, dispatch_get_current_queue(), ^(void) { | |
| NSLog(@"Outputing results of smoothing."); | |
| // Write header to dispatch_data | |
| // Write output to dispatch_data | |
| for (int n = 0; n < num_items; n++) { | |
| } | |
| // Release resources and close everything up. We're done! | |
| free(src_buf); | |
| free(dst_buf); | |
| dispatch_io_close(output_file, 0); | |
| NSLog(@"Smoothing and output finished on a file."); | |
| }); | |
| dispatch_release(map_data); | |
| dispatch_release(gaussian_group); | |
| }); | |
| }); | |
| dispatch_group_notify(smooth_group, global_queue, ^{ | |
| NSLog(@"All smoothing dispatched."); | |
| }); | |
| } | |
| dispatch_release(smooth_group); | |
| } | |
| }]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment